C compilers targeting Game Boy, not just Zilog

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

C compilers targeting Game Boy, not just Zilog

Post by tepples »

In [url=https://forums.nesdev.com/viewtopic.php?p=202148#p202148]this post[/url], calima wrote:I was also looking at getting on the Gameboy [...] there are plenty of z80 C compilers.
I too was thinking about getting into Game Boy, largely to illustrate a point to the FamiTracker user community about using a Game Boy connected to the NES through a controller to Game Link cable as expansion audio.

As of 2017, which C compilers target LR35902? A compiler targeting Z80 is fine if it offers a choice to emit code that doesn't use IX, IY, and other Z80 features left out of the Game Boy's LR35902? Aspects to consider in your recommendation:
  • Which are free software?
  • Of non-free compilers, which are made for both Windows and GNU/Linux?
  • Of non-free compilers, which are available without charge?
  • Of non-free compilers available without charge, which may be used to emit code that will be included in a product that will be sold?
  • Which are maintained? GBDK is 16 years old.
  • Which support the largest subset of C, including useful features introduced in C99?
  • How well do they optimize?
lidnariq
Posts: 11430
Joined: Sun Apr 13, 2008 11:12 am

Re: C compilers targeting Game Boy, not just Zilog

Post by lidnariq »

GBDK uses an ancient release of SDCC, which is still maintained and still can target the LR35902.
adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Victoria, BC

Re: C compilers targeting Game Boy, not just Zilog

Post by adam_smasher »

Yes, SDCC is AFAIK the only real choice. A generic Z80 compiler wouldn't come close to working - the instruction sets are far too different. The GB "Z80" is much more like a souped-up 8080, although it's not source compatible (I can't tell - it might be binary compatible [1] [2]).

I know that there probably were a few commercial GB games written in C. Or at least GBC games (that's Game Boy Color games, not Game Boy C games :wink:) - compiler technology had improved, memory restrictions were less severe, and the GBC could run twice as fast. But honestly, the code SDCC generates is terrible. Generating decent code for the GBZ80 from C is a Herculean task, so I can't fault it for this, but I also can't see how you'd write anything more significant than a tech demo in it without dipping down into assembly an awful lot.
Pokun
Posts: 2675
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: C compilers targeting Game Boy, not just Zilog

Post by Pokun »

I guess the GBZ80 is like a Z80 with an 8080 instruction set plus a Z80 RETI and a few unique ones (including the zero page like LDH) and using a Z80 syntax. RETI has moved to a different spot compared to the Z80, but some of the other new instructions appears to have taken the place of 8080 and Z80 instructions, so I guess it's not fully binary compatible with 8080 either.

GBDK is the only C-compiler I've seen, but I've never bothered with it.

If assembler is fine I'd recommend RGBDS. It was apparently used in commercial GBC games back in the day since it's supposedly more versatile then Nintendo's official one. And it has a fork that is still maintained. It has a powerful macro language but also the syntax may be considered a bit weird at times. It seems to be the far most common assembler for Game Boy, and BGB supports its syntax and debug files in its debugger. Plus it's much better than WLA.
Rüdiger
Posts: 12
Joined: Fri Oct 16, 2015 1:14 pm

Re: C compilers targeting Game Boy, not just Zilog

Post by Rüdiger »

SDCC is probably your best option. Beware that GBDK ships with an ancient version (~2002?) of SDCC,
but the mainline version of SDCC also supports GBZ80, so you can use that instead.
And even now, SDCC sometimes generates really stupid code and has some weird quirks.

There is also gbdk-n, which makes the gbdk libraries compatible to modern SDCC versions.
I don't know how complete this is, but had no issues so far for my small test roms.
adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Victoria, BC

Re: C compilers targeting Game Boy, not just Zilog

Post by adam_smasher »

Pokun wrote:If assembler is fine I'd recommend RGBDS. It was apparently used in commercial GBC games back in the day since it's supposedly more versatile then Nintendo's official one. And it has a fork that is still maintained. It has a powerful macro language but also the syntax may be considered a bit weird at times. It seems to be the far most common assembler for Game Boy, and BGB supports its syntax and debug files in its debugger. Plus it's much better than WLA.
Agreed, RGBDS is what I use.
Post Reply