Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Developing Sega Master System ROMs in C

Reply to topic
Author Message
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Developing Sega Master System ROMs in C
Post Posted: Sat Feb 25, 2006 10:13 pm
Last edited by haroldoop on Tue Apr 25, 2006 2:41 am; edited 10 times in total
I've been working on adapting z88dk, a C cross-compiler for Z80 systems, to generate Sega Master System ROMs.
The attached file includes both a z88dk runtime library for SMS ROMs and an very simple example project. It still doesn't do much, since I haven't finished the libraries for it yet, but it already runs perfectly on an emulator. ;)
Of course you'll also have to download z88dk in order to compile the source.

** updated **
2006-03-07

Now most of the absolutely necessary functions are implemented. You can already set the palette, set the tileset, define the bkg map, and position the sprites.


** updated **
2006-03-11

Now, you have everything you need to make SMS games in C... except sound.
- Added joystick reading routines;
- Created an include file;
- Created a tool to convert z88dk's map files into Meka/Emukon-compatible symbolic definition files. This will allow the debuggers built into those emulators to correctly label the subroutines/variables of your program.

** updated **
2006-03-26

-Added partial stdio.h support (write-only)
-Fixed a joystick reading bug
-Added an example game

** updated **
2006-04-12

-Added support for apLib decompression (based on Maxim's code)
-Improved the example game
-Added an example for the decompression routine
-Added a small utility for padding the ROM to 32k and adding the standard SEGA header to it

** updated **
2006-04-23

-Added support for the PAUSE button
-Added support for the VBL/HBL interrupt (doesn't work correctly yet)
-Added sound support

** updated **
2006-04-23

-Updated "sms.h" (I had sent an older version by mistake)
-Added some missing source
z88dk_sms-2006-03-26.zip (37.01 KB)
Sega Master System runtime for z88dk (* OLD VERSION *)
z88dk_sms-2006-04-12.zip (83.36 KB)
Sega Master System runtime for z88dk (* OLD VERSION *)
z88dk_sms-2006-04-24.zip (88.49 KB)
Sega Master System runtime for z88dk

  View user's profile Send private message Visit poster's website
  • Joined: 31 Jan 2006
  • Posts: 71
Reply with quote
Post Posted: Sat Feb 25, 2006 11:05 pm
Would be great if it runs at good speed on real system :)
I have to test that, thank you
  View user's profile Send private message
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Sun Feb 26, 2006 9:46 am
I've also had a crack at using z88dk for SMS development. What tripped me up was that I couldn't convince the compiler to put global variables and arrays in SMS ram - it seems z88dk wasn't written with ROM based z80 machines in mind so global variables are assigned to locations inside the executable, therefore they will be in ROM, not RAM. Local variables are stack allocated so they are still usable, and I suppose one could depend on them exclusively as a workaround. Alternately you could compile your code to run with an origin of $c000 and use a rom stub to copy the executable into RAM, but you'll run out of RAM real fast that way.

Other than that, some of the code it generates is painfully slow -- adding two chars, for instance, causes the compiler to promote both variables to 16 bit, push them onto the stack, and the call an adding function, at least in the version I tested. Of course compotently written hand-written assembly code is going to beat compiled code every time but it seems like a better optimizer would be needed to really do a lot with z88dk, but I suppose with a very good support library written in assembly, it should be possible to use it to do puzzle games or some very simple action games.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post Posted: Sun Feb 26, 2006 2:43 pm
Actually, I intend to use it to make RPGs and puzzle games. ;)
  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Sun Feb 26, 2006 10:55 pm
Well then, there you go.

Probably the best approach would be to have a strong game engine written in asm, and use C to control it all, as sort of a scripting language (sort of).

For accessing roms above 48k, you might want to create some sort of virtual rom file system for the C code to use to load assets into vram.
  View user's profile Send private message Visit poster's website
  • Joined: 19 Dec 2005
  • Posts: 33
  • Location: Thailand
Reply with quote
Post Posted: Wed Mar 01, 2006 3:50 pm
Quote

What tripped me up was that I couldn't convince the compiler to put global variables and arrays in SMS ram


In order to have global variables located in RAM you will need to write your own C run-time startup code (crt0.s). See the z88dk documentation on retargeting the compiler for more information.

The C runtime startup code is responsible for preparing the system for use with C code. This usualy includes (but is not limited to) initialising and or clearing RAM, initialising the stack, copying globals into RAM, initialising the heap, calling main and handling any program that returns from main (not usually done in sms carts).

This explanation of crt0.s is not limited to the z88dk but is fairly generic amongst cross-compilers (including gnu gcc). Sometimes startup.s is used in addition to or instead of crt0.s.

The z88dk uses a three or four letter prefix for its C run-time libraries so I would suggest sms_crt0.s.

Regards
Marty
  View user's profile Send private message
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Wed Mar 01, 2006 6:11 pm
Marty wrote

The C runtime startup code is responsible for preparing the system for use with C code. This usualy includes (but is not limited to) initialising and or clearing RAM, initialising the stack, copying globals into RAM, initialising the heap, calling main and handling any program that returns from main (not usually done in sms carts).


I saw that but I don't see how that would help. My crt0 could copy the contents of ROM into RAM, for initialized but volatile data (such as an "int count = 1" in a global scope, or a "static count = 1" in a local scope) but that doesn't seem to change the fact that the code generated by the C compiler and assembler is referring to globals in their location in ROM.

In the list of supported systems on the z88dk web site I see only RAM based systems like computers and graphing calculators where an executable would presumably be loaded into RAM, therefore the global variable problem does not exist for these systems. I don't think z88dk was written with ROM based systems in mind. If there is a way to place globals in RAM, I wasn't able to find it.
  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Wed Mar 01, 2006 6:26 pm
Ah, DEFVARS seems like the command you'd need to move those into static RAM. Missed that the last time around. I wonder if z88dk still lets you put constant data in ROM if you do that. I imagine so.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Library update
Post Posted: Wed Mar 08, 2006 3:53 pm
Libraries updated!
(see first post)
  View user's profile Send private message Visit poster's website
  • Joined: 31 Dec 2004
  • Posts: 76
  • Location: France
Reply with quote
Post Posted: Thu Mar 09, 2006 5:01 pm
Seem very promising :)
  View user's profile Send private message
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
New version!
Post Posted: Sun Mar 12, 2006 2:02 am
There's now a new version of the libraries. Now you can actually make games with it! ;)
(Se first post for the download link)
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Another update
Post Posted: Sun Mar 26, 2006 12:10 pm
New version posted. Now comes with an example game! :)
(See first post)
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
New version
Post Posted: Thu Apr 13, 2006 1:09 am
New release available, with data decompression support.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Library update
Post Posted: Sun Apr 23, 2006 10:41 pm
There's a new version available with pause and sound support.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Bugfix
Post Posted: Tue Apr 25, 2006 2:44 am
* NEW BUGFIX RELEASE *
The version that I had released in 2004-04-23 wouldn't work.
I have released a new version to fix this problem.
Sorry for my mistake. =P
  View user's profile Send private message Visit poster's website
  • Joined: 24 Jul 2005
  • Posts: 55
  • Location: São Luís/MA - Brazil
Reply with quote
Post Posted: Wed Apr 26, 2006 12:13 pm
I guess you wanted to say 2006-04-23 :)

Best Regards, man.
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post Posted: Wed Apr 26, 2006 3:40 pm
Okay, sorry for that mistake, too. XD
  View user's profile Send private message Visit poster's website
  • Joined: 25 Feb 2006
  • Posts: 417
  • Location: Belo Horizonte, MG, Brazil
Reply with quote
Post Posted: Thu Jun 07, 2007 1:32 pm
Good news: SMS support was added to the official z88dk.
http://www.z88dk.org/
  View user's profile Send private message Visit poster's website
Reply to topic



Back to the top of this page

Back to SMS Power!