welcome to www.hangar-eleven.de - your premier resource for console development





Development Env

Installing Cygwin

Building SH4 toolchain
Making Binutils
Making crossGCC
Making Newlib
Making final crossGCC

Building ARM toolchain
Making Binutils
Making crossGCC

Compiling KallistiOS

Testing KOS and the Compilers
Writing the first program
Compiling the first program
Uploading to the Dreamcast
DC-Tool
Serial Slave

Making a CD

Back to News

 

  Writing the first KallistiOS program
  Written: September 25th, 2001 @ 22:12 GMT By: Thorsten Titze
 
 

You should have already downloaded the sourcecode of our little example. If not just download it here.

Now we'll go line-by-line through the sourcecode (I have no idea if this is useful for anyone, but I really doubt it. Everyone who has intentions to code for the DC should have some knowledge in C. But still... I post it... Just in case someone might be interested):

#include <kos.h>
Here we are including the basic KallistiOS header file which give us all needed functions and variables.

void display_screen(char *str)
We will skip this function for now, so we rather continue to

int main(int argc, char **argv)
{

This is the programs main routine which will be called whenever the program is loaded into the DCs memory and being called.

cont_cond_t cond;
The type cont_cond_t is a data-type which hold a structure that contains the status of a controller that is connected to the MAPLE-BUS. You can make bit-wise comparisons to find out the status of a controller.

char cOutput[] = "...............";
cOutput is simply an array of chars in which we'll hold the content of the screen we want to print to the TV screen.

kos_init_all(NONE_ENABLE, ROMDISK_NONE);
The procedure kos_init_all initializes the hardware and software of the Dreamcast with the parameters given. In this example (with the keyword NONE_ENABLE and ROMDISK_NONE) we are not initializing any special features and the ROMDISK feature.

vid_set_mode(DM_640x480, PM_RGB565);
With the vid_set_mode command we can set the video-mode to a specified resolution and color mode. (please look into the KOS sources for the other modes that you can use)

display_screen(cOutput);
Here we call my own routine display_screen with the text with defined above as parameter. We now go into the display_screen routine :

xpos = 2;
ypos = 1;

Here we define where to start displaying characters (this is not the position resolution wise, but character wise. So when we consider 12 as the width of a character, 2 means we start at pixel 24 !)

while(*str)
{

As long as we get something out of str we process the following code.

offset = (xpos * 12) + ((ypos * (24+4)) * 640);
Now we calculate where to put the next character. Since the BiosFont has a size of 12x24 pixels we multiply our xpos by 12. The height of one character is 24 pixels so we multiply by that value, but because we're using a resolution of 640x480 pixels this means that to get to the next line we have to go 640 pixels to the right, therefore multiplying the same value by 640.

current=*str++;
Just getting the next character out of the array.

if(current != '\n')
{
bfont_draw(vram_s + offset,640,0,current);
xpos++;
}
If we have a normal character (everything but '\n') then draw it to the screen. vram_s is the adress in Dreamcast memory where the actual screen is stored. After writing our character we increase the xpos.

else
{
xpos=2;
ypos++;
}

If we have a '\n' then we the xpos to our startvalue and increase the line-number we are in.

while(1)
{
if(cont_get_cond(maple_first_controller(), &cond) <0) break;
if(!(cond.buttons & CONT_START)) break;
timer_spin_sleep(10);
}

This small code snippet just waits for the user pressing Start on the first available controller and does nothing but sleeping for 10msecs if he does not.
maple_first_controller() returns the ID of the first controller it finds.
cont_get_cond(ID, &cond) returns a bitfield of the status of the controller. (if it returns a value lower than zero it means that it could not get a status).
cond.buttons holds the bitfield of the buttonstates and CONT_START stands for the bit that checks the start-button.
timer_spin_sleep(msec) lets the processor sleep for the specified number of miliseconds.

kos_shutdown_all();
return 0;

Shuts down the whole KallistiOS and returns control to SerialSlave or DCLoad.

 

  Go to the sourceforge homepage of KallistiOS

Back to Dreamcast Development Main


DISCLAIMERS:
Website Design © 2002 by Thorsten Titze / hangar-eleven.de
(the pictures used for the design were taken from a WindowsXP Skin and WindowsXP)
All brand names used on this site are registered trademarks of their respective owners
No copyright infringement is intended
Last updated @ 22-Feb-2002 2:19 PM Contact me via eMail