Controller/keyboard input without KOS

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
fox68k
DC Developer
DC Developer
Posts: 49
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Tue Aug 03, 2004 11:01 am
Has thanked: 0
Been thanked: 0
Contact:

Controller/keyboard input without KOS

Post by fox68k »

Hi,

i need VERY basic input (controller or keyboard, keyboard preferred) in my program, but since it is big to upload it through serial cable (i don't own a BBA ) i don't want to use KOS.

I've been looking at Marcus notes, but i can't figure it out.

Anyone knows an easy way to get access to the state of the controller/keyboard without KOS?

Thanks in advance.
- fox68k -
Strapping Scherzo
DC Developer
DC Developer
Posts: 2285
Joined: Fri Feb 21, 2003 7:37 am
Location: Chicago, IL
Has thanked: 0
Been thanked: 1 time
Contact:

Post by Strapping Scherzo »

I'm not quite sure, but you could get the source to KOS and see how it done there.
Image
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7498
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

Welcome, i see you took my advice =P. Yea i was thinking about it, and i would either try to recreate what KOS does (although that still might not be worth the effort) or try to just rip a lot of junk out of KOS so that it dosnt make much of an impact.

1.1.9 is just a tad over a megabyte, and i assume newer versions are bigger or something. It probably wouldnt take much to cut the size in half. Just rip out all the CD stuff, the FS stuff, the modem stuff, etc...
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
GPF
DC Developer
DC Developer
Posts: 529
Joined: Wed Oct 17, 2001 7:44 pm
Location: Texas
Has thanked: 0
Been thanked: 0
Contact:

Post by GPF »

I think there are other libaries out there that might have what you need, look for libronin or find an old version of libdream(KOS predeccesor) , also in KOS 1.3.X at some point Dan is setting it up so you can link only the libs you need to reduce the size of linkage.

Troy
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has thanked: 0
Been thanked: 0

Post by Kamjin »

here you go.. Kosfree.. if you're using Marcus's Maple stuff,
just make sure you re-organise the parameters, mine is sent
as it is seen on the maple, this should be enough to get the keyboard
going as well.. same location as digital, except 1byte=shift/alt status
next byte is the LEDs, and then 6chars of 6 keys being pressed..
a note of caution, if you start getting back 0xFFFF's then toss the
data and poll it again.. it does that sometimes...

Code: Select all

#include <math.h> 

unsigned char dmabuffer[2128]; 

void *maple_docmd(int port, int unit, int cmd, int len, void *data) 
{ 
  unsigned long *sbuf, *rbuf; 
  int dst, src;
  src = port << 6;
  if(unit==0){
        dst=0x0020;
  }else{
        dst=((1<<(unit-1))&0x1f);
  }
  dst=((port<<6)+dst);

  rbuf = (unsigned long *) ((((unsigned long)dmabuffer+0x20)& 0xffffffe0)|0xa0000000);
  sbuf = (unsigned long *) (((unsigned char *)rbuf) + 1024);
   while((*(volatile unsigned long *)(0xa05f6c18)) & 1);//DMA Busy?
  *(volatile unsigned long *)(0xa05f6c04) = ((unsigned long)sbuf) & 0xfffffff;
  *sbuf++ = len + (port * 0x10000) + 0x80000000;
  *sbuf++ = ((unsigned long)rbuf) & 0xfffffff;
  *sbuf++ = (cmd & 0xff) + (dst * 0x100) + (src *0x10000) + (len * 0x1000000);
   if(len > 0) {
          unsigned long *param = data;
          int i;
          for(i=0; i<len; i++)*sbuf++ = *param++;
  }
  *(volatile unsigned long *)(0xa05f6c18) = 1;//do dma..
   while((*(volatile unsigned long *)(0xa05f6c18)) & 1);
   return rbuf;
}//end



void delay(int t){
      float x = 1;
      while(t-->0)x=cos(x);
}//end delay


void main(void)
{
  unsigned int i=0,params[2];
  unsigned short *res,digital,trigger,joystick;

  //maple init
  *(volatile unsigned long *)(0xa05f6c8c)= 0x6155404f;
  *(volatile unsigned long *)(0xa05f6c10) = 0;
  *(volatile unsigned long *)(0xa05f6c80) = 0xc3500000;
  *(volatile unsigned long *)(0xa05f6c14) = 1;

while(i==0){
          params[0]=0x01000000; //controller
          do res = maple_docmd(0, 0, 9, 1, params);
          while(*res == -4);
          res+=4;//Dpad and buttons
          digital=*res; //for explination purposes..
          *res++;       //but just make this a simple struct
          trigger=*res;
          *res++;
          joystick=*res;//didn't bother.. but same as triggers
          
          printf("RT:%d\n",(trigger&0x00ff));
          printf("LT:%d\n\n",(trigger/=0x100));

          if(!(digital&0x0010))printf("Up\n");
          if(!(digital&0x0020))printf("Down\n");
          if(!(digital&0x0080))printf("Right\n");
          if(!(digital&0x0040))printf("Left\n");
          if(!(digital&0x0200))printf("Y\n");
          if(!(digital&0x0004))printf("A\n");
          if(!(digital&0x0002))printf("B\n");
          if(!(digital&0x0400))printf("X\n");
          if(!(digital&0x0008)){
                      	printf("START\n");
                        i=1;//and exit
          }
          delay(500000);
}//end while
User avatar
fox68k
DC Developer
DC Developer
Posts: 49
Joined: Tue Aug 03, 2004 11:01 am
Has thanked: 0
Been thanked: 0
Contact:

Post by fox68k »

Thank you very much for all this information! :D

Kamjin, i will try that code in my program. :wink:

One more question arise now. When i try to use standard C functions (like printf, cos, etc) i don't get errors or warnings in the include statement (#include <math.h> for example) but the linker throws me an error (undefined reference to cos in this case). What is happening? Could it be a missing compiler parameter?

This is my Makefile:

AS = sh-elf-as -little
LD = sh-elf-ld -EL
CC = sh-elf-gcc -ml

OBJS = crt0.o test.o video.o fame.o

fametest.srec : $(OBJS)
$(CC) -o $@ -Wl,--oformat,srec,-Ttext,0x8c010000 \
-nostartfiles -nostdlib $(OBJS) -lgcc

Thanks in advance.
- fox68k -
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7498
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

maybe im wrong here, but i dont see that you are linking in -lm which is libmath... im pretty sure you need that.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

One more question arise now. When i try to use standard C functions (like printf, cos, etc) i don't get errors or warnings in the include statement (#include <math.h> for example) but the linker throws me an error (undefined reference to cos in this case). What is happening? Could it be a missing compiler parameter?
Of course you get errors - you don't have a standard library to use. It's usually part of KOS. It's the same thing that happens when you try writing programs without any operating system - no standard library, no standard headers, nothing apart from the stuff included in your program, and the libraries it statically links to.

I think you might be better off using libdream. It doesn't have much of a standard library either, but at least stuff like printf works without having to write a serial driver, a DC-Load console driver, and a printf implementation.
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has thanked: 0
Been thanked: 0

Post by Kamjin »

Ahh.. yeah.. I'm doing COS with a taylor series..
You can just remove the delay, it was just there to slow
everything down for demo purposes..if you want replace it with a long loop..
there might be a COS func in marcus newlib you could use it,
add -libm after -lgcc.
also you'll need to change CC = sh-elf-gcc -ml -m4-single-only
(but this will cause a size increase.. so you're better off just using
a few loops waiting for a vblank)

Oversight on my part..my code is way to long to post..
you can take marcus's code for drawstr

draw_string(xpos, ypos, *text, color ); color is RGB 5:6:5 on his example
and just swap out the printf's with this funct. just remeber that you have
to erase also, just print empty spaces.
One more question arise now. When i try to use standard C functions (like printf, cos, etc) i don't get errors or warnings in the include statement (#include <math.h> for example) but the linker throws me an error (undefined reference to cos in this case). What is happening? Could it be a missing compiler parameter?
Yeah, the way you're going leaves you with some missing fucntions, math for the most part is there in the sh-gcc just you have to use -libm, you have partial stdio through marcus's newlibs using -lc. In general you have
to write you own missing stuff, like COS() I have cos in double float using a taylor series, I also have COSF() using a look up table.. printf again had to write my own. There are pluses to going this way, you gain about 20%
in speed just by not using KOS, but you loose the simplicity of having all sorts of stuff available.
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

like COS() I have cos in double float using a taylor series, I also have COSF() using a look up table..
Can't the SH-4 calculate those itself? I'm fairly sure it has some trig functions at least.
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has thanked: 0
Been thanked: 0

Post by Kamjin »

:roll: Yup.. did that before I actually read the sh-4 spec.. it probably
computes COS faster than my lookup..
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has thanked: 0
Been thanked: 0

Post by Kamjin »

Kamjin wrote::roll: Yup.. did that before I actually read the sh-4 spec.. it probably
computes COS faster than my lookup..
...meanwhile back at the ranch..

This post actually got me thinking.. why didn't I use the sh4.. since I did read the sh-4 spec when I started writing AAC.. so started looking through my docs/code.. There's a cos but though using sqrt..mult..div with the vector/dotproducts..
GPF
DC Developer
DC Developer
Posts: 529
Joined: Wed Oct 17, 2001 7:44 pm
Location: Texas
Has thanked: 0
Been thanked: 0
Contact:

Post by GPF »

in my program, but since it is big to upload it through serial cable (i don't own a BBA )
You could also try Chankast the DC emulator, thats what I use for development when I am at work, there are a few caveats but its another option. I just wrote a little script that after it compiles it builds an iso image and launches Chankast with the iso, and then used some VB script to sendkeys to the app to start it running.

Troy
Post Reply