Gameboy programming

hi i am trying to make a program in c for the nintendo gameboy
that prints out all its character codes as they look diffrent to normal ones
(for more info http://gbdev.gg8.se and also search for the gbdk)
and for some reason instead of adding 1 with every l loop its adding 257!!!

so its output is:
0 =
257 =
514 =
etc

here is the code can you spot anything wrong with it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <gb/gb.h>

int main(){
unsigned char i=0;

printf("press a to goto next\nASCII character\npress start now\nto continue\n");
waitpad(J_START);
while(i < 256){
	printf("%d = %c\n", i, i);
	delay(130);
	waitpad(J_A);
	i++;
}

return 0;
}
%d can only be used with int values. Passing something other than int causes undefined behavior. You can use
printf("%d = %c\n", (int)i, i);
FWIW, you're going to get crap performance on the GB if you code in C.. and you're not going to be able to do any fancy raster tricks (since they're all based on timed writes to regs, and if you're compiling code you can't really time anything).

Compiled code just is too slow for those old school video game targets.

You're really better off just learning gb-z80. It isn't that hard, and you'll have much more control over what your game does... and it'll be faster.

As for your problem....

1
2
3
4
unsigned char i=0; // <- i is an unsigned char
// unsigned chars have a range of 0-255
//...
while(i < 256){ // <- this will ALWAYS be true, because i's maximum value is 255 



EDIT:

Actually that wouldn't explain the problem you're seeing. The only thing I can think of that would is a shoddy/buggy printf() implementation, which wouldn't at all surprise me. printf is extremely complex and doing it all in in gb-z80 would be nuts.


EDIT 2:

You could try casting. I don't know if that would help...

 
printf("%d = %c\n", (int)i, (int)i);


If that works, then it's a problem with how parameters are passed to functions (they should be automatically upgraded to int, AFAIK)... in which case you'll probably have bigger problems with this compiler later.
Last edited on
they should be automatically upgraded to int, AFAIK
??? Err... no. Variadic parameters are pushed to the stack verbatim. printf() then parses the stack based on what was passed as a format string. If you pass things of types that don't match what was specified in the format string, printf() will usually end up doing weird things. In OP's case, it's roughly equivalent to *(int *)&(unsigned char)c.
Ah... okay... in that case, then casting is appropriate:

printf("%d = %c\n", (int)i, i);

And that would explain the error.

On a side note... I would love to see the gb-z80 for this printf implementation.
Topic archived. No new replies allowed.