Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this working? #2

Closed
clobber opened this issue Aug 24, 2015 · 13 comments
Closed

Is this working? #2

clobber opened this issue Aug 24, 2015 · 13 comments

Comments

@clobber
Copy link

clobber commented Aug 24, 2015

Hi, I stumbled onto your project while looking for an updated GBDK using the latest SDCC.

I've built with SDCC HEAD (3.5.4 #9304 as of Aug 24 2015) and cannot get a binary created with makebin, getting error: size of the buffer is too small.

My program, test.c, looks like:

#include <gb/gb.h>
#include <gb/console.h>
#include <stdio.h>

void main()
{
    printf(" \nTest\n");
}

And I execute the following:

$ ./gbdk-n-compile.sh test.c
+ sdcc -mgbz80 --no-std-crt0 -I /gbdk-n/bin/../include -I /gbdk-n/bin/../include/asm -c test.c

$ ./gbdk-n-link.sh test.c
+ sdcc -mgbz80 --no-std-crt0 -I /gbdk-n/bin/../include -I /gbdk-n/bin/../include/asm -L /gbdk-n/bin/../lib /gbdk-n/bin/../lib/crt0.rel -l gb.lib -o a.ihx test.c

$ makebin -Z a.ihx test.gb
error: size of the buffer is too small.

Is this correct or have I done something wrong?

@andreasjhkarlsson
Copy link
Owner

Hi!

Sorry for the late reply, I should probably turn on notifications for github..

The "error: size of the buffer is too small." seems to be the goto error makebin throws when it encounters something wrong in its input data.

The problem with your commands is that the link script wants a "rel" file produced by the compile script, not the c file.

I'm running a little older SDCC here (3.4.0 on OS X Yosemite), but the following works fine for me:

$ ./gbdk-n-compile.sh test.c
$ ./gbdk-n-link.sh test.rel -o test.ihx
$ ./gbdk-n-make-rom.sh test.ihx test.gb

@clobber
Copy link
Author

clobber commented Aug 31, 2015

Thanks for the response, that worked!

It compiles but there is no output on screen, because it looks like the console library functions (found in https://github.com/rotmoset/gbdk-n/blob/master/libc/font.ms) are not compiled in. So printf()/puts() (putchar), cls(), gotoxy(), etc, won't work and will throw ?ASlink-Warning-Undefined Global '_gotoxy' referenced by module 'test' type warnings. I don't know my way around GBDK or the libraries very well, but I'm guessing all you have to do is add font.ms to your original build script so it assembles and links. Hope you can add that in when you get a chance! Also not sure if any other modules are missing.

I did try some drawing functions and those work:

#include <stdio.h>
#include <gb/gb.h>
#include <gb/drawing.h>

void main()
{
    color(BLACK, WHITE, SOLID);
    plot_point(GRAPHICS_WIDTH/2,   GRAPHICS_HEIGHT/2);

    color(DKGREY, WHITE, SOLID);
    plot_point(GRAPHICS_WIDTH/2+1, GRAPHICS_HEIGHT/2);

    color(LTGREY, WHITE, SOLID);
    plot_point(GRAPHICS_WIDTH/2+2, GRAPHICS_HEIGHT/2);

    color(WHITE, WHITE, SOLID);
    plot_point(GRAPHICS_WIDTH/2+3, GRAPHICS_HEIGHT/2);

    color(LTGREY,WHITE,SOLID);
    circle(140,20,15,M_FILL);
    color(BLACK,WHITE,SOLID);
    circle(140,20,10,M_NOFILL);
    color(DKGREY,WHITE,XOR);
    circle(120,40,30,M_FILL);
    line(0,0,159,143);
    color(BLACK,LTGREY,SOLID);
    box(0,130,40,143,M_NOFILL);
    box(50,130,90,143,M_FILL);
}

Resulting in:
screen shot 2015-08-30 at 8 01 56 pm

Note: make sure you use the gbdk-n-make-rom.sh script with the -Z flag (e.g. $ ./gbdk-n-make-rom.sh -Z test.ihx test.gb), since makebin needs that to know to add the appropriate gameboy header so the emulator can load the cart. You may just wanna add -Z to your script so it doesn't have to be remembered.

Anyway, thanks for updating GBDK so it can be used with newer SDCC -- this is wonderful because the last GBDK uses an ancient 15 year old version full of bugs, so I'm hoping your fork catches on and picked up by those doing GB dev! I'll help spread the word once it is ready to go.

@andreasjhkarlsson
Copy link
Owner

I remember removing font.ms from the build because the assembler wouldn't accept the file format. I'll take another look at it later to see if I can get it working. I'll add the Z flag as well, I don't remember needing it, but I'm sure you're right.

And yeah, the old GBDK really was ancient, it even had some really scary, basic bugs that I found when developing my own emulator (which is also why I made this).

Anyways, cool to see some interest in it, this is currently just the bare essentials that compiles with newer sdcc, but with some more cleaning up, structering and examples I'm sure it will be useful!

@andreasjhkarlsson
Copy link
Owner

Don't know if you noticed, but I added font.ms and the Z flag to the build script. The font module produces a warning about multiple putchar definitions (one implementation being in sdcc), but everything seems to work.

@clobber
Copy link
Author

clobber commented Sep 1, 2015

Thanks! When including gb/console.h there is a warning, Duplicate symbol 'size_t' causing a compile error because sdcc already defines typedef unsigned int size_t in its own stdio.h, so if you remove the old declaration from GBDK's /include/asm/gbz80/types.h (https://github.com/rotmoset/gbdk-n/blob/master/include/asm/gbz80/types.h#L33) it will compile fine:

hello 2015-09-01 16 04 02

I believe the putchar defined in sdcc's implementation is just a stub since we are meant to provide our own definition.

By the way, I submitted a pull request to homebrew for the latest stable sdcc (3.5.0) Homebrew/legacy-homebrew#43485 which I used to recompile gbdk-n and everything looks okay. Some notable changes in 3.5.0 are Z80/GBZ80 fixes and the default language dialect changed from --std-sdcc89 to --std-sdcc99, so maybe they're closer to C99 compatibility now. If you want to check it out now via homebrew, just edit the formula with brew edit sdcc and change the url and sha256.

I'm going to see about getting the old GBDK examples compiling with the new library and sdcc and then I'll submit you a PR for them.

@andreasjhkarlsson
Copy link
Owner

Ah, I noticed the size_t issue as well and forgot to commit it. Will do.

Even if it's a stub I think the warning should be surpressed, if you know how, do tell!

Looking forward to the PR.. :)

@clobber
Copy link
Author

clobber commented Sep 2, 2015

Yep, definitely. I think putchar needs to be moved into its own module or defined in a .C file where we just inline the assembly. Just a quick glance from others with the issue:

It seems SDCC's stdio.h contains the declarations which leads me to believe we just need to define them.

extern char getchar(void);
extern void putchar(char);

Also, the sdcc 3.5.0 update is now available via Homebrew (plus pre-built Bottles, too!) in case you want to check out the latest.

Regarding the examples, I've already noticed "galaxy.c" one has glitchy graphics, so it will be fun trying to figure out if that is an error in the code, the library or the compiler/assembler itself :\ I guess one thing to do is compare the assembly built from the old toolchain with the new to see if that reveals anything.

@andreasjhkarlsson
Copy link
Owner

Great work with homebrew, I'll check it out later when I'm home.

Debugging ROMs is a nightmare, when developing my emulator I had to compare entire code paths step-by-step between my emulator and working ones before finding some banal error. As a result I built in a fairly competent GB debugger into my emulator (which you can find here).

Just make sure you're testing with several emulators as I found that even mainstream one could produce incorrect graphics. I have a GBC with a flash card at home which I could verify the glitches with if you want.

@clobber
Copy link
Author

clobber commented Sep 4, 2015

Okay, regarding glitches:

I've compiled some demos with the new sdcc while comparing them with builds from the old toolchain and am getting some issues...

  1. galaxy.c / space.s from the original gbdk set (http://gbdk.sourceforge.net/examples.html)
    galaxy-old 2015-09-04 02 35 24 (old vs new) galaxy-new 2015-09-04 02 35 29 2
    • galaxy/space are supposed to draw a scrolling background, a large window sprite and a rotating globe sprite that all move around on screen.
    • Compiling galaxy.c with the new sdcc/gbdk, the globe graphics are glitched, the background scrolling stutters and the door will not open as it should when you press 'start'
    • Compiling the pure assembly version, space.s, has the globe graphics glitched but the background scrolling is smooth and the door responds to input and opens
    • Both these demos run fine with the old toolchain.
  2. thumby.c lifted from http://belial.blarzwurst.de/gbpaper/
    thumby-old 2015-09-04 02 36 59 (old vs new) thumby-new 2015-09-04 02 36 18
    • thumby is a simple demo that draws a background and rectangle sprite that you can control with the d-pad.
    • Compiling thumby.c with the new sdcc/gbdk, the rectangle sprite is hidden behind the background layer unless you hide the background layer. More strange, after 15 seconds or so a rectangular glitch starts to slowing scroll down from the top center of the screen.
    • This demo runs fine with the old toolchain.

Odd stuff happening here with graphics. I don't know GBZ80 assembly so I can't begin to tell if this is a problem with the example code, library and/or compiler+assembler -- and since most people are stuck using the 15 year old toolchain, who knows if the sdcc devs have done much testing with their GBZ80 assembled code. Could be issues there as well. The thumby.c demo is pretty simple though and the C code looked straight forward to me.

Anyway, I'm including a zip that you can check out and maybe shed some light on the problems. It contains compiled binaries from the old and new toolchains to compare with, makefile + source to compile with the new sdcc as well as all the output and objects compiled from the old. All the ROM testing was with Gambatte.

@clobber
Copy link
Author

clobber commented Sep 8, 2015

Found the trouble! I was spending all weekend testing older versions of the assembler/linker and various flags until I inspected the sdcc/sdldgb .lnk linker script and compared gbdk's old link-gbz80 linker source code.

By default sdcc's sdldgb sets the data location at 0xc000 which I noticed in the script as flag -b _DATA = 0xc000

However in gbdk's old link-gbz80 it was _DATA=0xc0a0, so simply passing --data-loc 0xc0a0 when we link with new sdcc/sdldgb fixes this.

Just add --data-loc 0xc0a0 here (https://github.com/rotmoset/gbdk-n/blob/master/bin/gbdk-n-compile.sh#L7) and we should be good to go with using this code on the new sdcc.

Now I can get back to working on cleaning up those old examples :D

@andreasjhkarlsson
Copy link
Owner

Nice work! I've been very busy with other stuff so I haven't been able to do anything with this.

Regarding the data location, it was one of the things that were incompatible in the old code, so I had to hard code it into the runtime (commit).

@clobber
Copy link
Author

clobber commented Sep 8, 2015

Yep, I see those in the old linker: https://github.com/gheja/gbdk/blob/3b9abaea9ed80e1a8b1f685213cddf700f580876/sdcc/link/z80/lkmain.c#L142-L155

We still need to add --data-loc 0xc0a0 though.

@andreasjhkarlsson
Copy link
Owner

Updated the script: 295450f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants