SMS Power! Logo Forums
Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Development - Music

 View topic - Developing Sega Master System ROMs in C
Log in
Search
Memberlist
Membermap
Profile
Register
 Log in to check your private messages
Reply to topic    Forum Index - Development
Author Message
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Developing Sega Master System ROMs in C
Post Posted:  Sat Feb 25, 2006 10:13 pm Reply with quote

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



Last edited by haroldoop on Tue Apr 25, 2006 2:41 am; edited 10 times in total
Back to top

View user's profile Send private message Visit poster's website
patroclus

Joined: 31 Jan 2006
Posts: 69
Post Posted:  Sat Feb 25, 2006 11:05 pm Reply with quote

Would be great if it runs at good speed on real system :)
I have to test that, thank you
Back to top

View user's profile Send private message
Heliophobe
Site Admin

Joined: 25 Oct 1999
Posts: 2029
Location: Monterey, California
Post Posted:  Sun Feb 26, 2006 9:46 am Reply with quote

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.
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Post Posted:  Sun Feb 26, 2006 2:43 pm Reply with quote

Actually, I intend to use it to make RPGs and puzzle games. ;)
Back to top

View user's profile Send private message Visit poster's website
Heliophobe
Site Admin

Joined: 25 Oct 1999
Posts: 2029
Location: Monterey, California
Post Posted:  Sun Feb 26, 2006 10:55 pm Reply with quote

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.
Back to top

View user's profile Send private message Visit poster's website
Marty

Joined: 19 Dec 2005
Posts: 33
Location: Thailand
Post Posted:  Wed Mar 01, 2006 3:50 pm Reply with quote

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
Back to top

View user's profile Send private message
Heliophobe
Site Admin

Joined: 25 Oct 1999
Posts: 2029
Location: Monterey, California
Post Posted:  Wed Mar 01, 2006 6:11 pm Reply with quote

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.
Back to top

View user's profile Send private message Visit poster's website
Heliophobe
Site Admin

Joined: 25 Oct 1999
Posts: 2029
Location: Monterey, California
Post Posted:  Wed Mar 01, 2006 6:26 pm Reply with quote

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.
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Library update
Post Posted:  Wed Mar 08, 2006 3:53 pm Reply with quote

Libraries updated!
(see first post)
Back to top

View user's profile Send private message Visit poster's website
xavier

Joined: 31 Dec 2004
Posts: 76
Location: France
Post Posted:  Thu Mar 09, 2006 5:01 pm Reply with quote

Seem very promising :)
Back to top

View user's profile Send private message
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
New version!
Post Posted:  Sun Mar 12, 2006 2:02 am Reply with quote

There's now a new version of the libraries. Now you can actually make games with it! ;)
(Se first post for the download link)
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Another update
Post Posted:  Sun Mar 26, 2006 12:10 pm Reply with quote

New version posted. Now comes with an example game! :)
(See first post)
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
New version
Post Posted:  Thu Apr 13, 2006 1:09 am Reply with quote

New release available, with data decompression support.
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Library update
Post Posted:  Sun Apr 23, 2006 10:41 pm Reply with quote

There's a new version available with pause and sound support.
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Bugfix
Post Posted:  Tue Apr 25, 2006 2:44 am Reply with quote

* 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
Back to top

View user's profile Send private message Visit poster's website
probert

Joined: 24 Jul 2005
Posts: 55
Location: São Luís/MA - Brazil
Post Posted:  Wed Apr 26, 2006 12:13 pm Reply with quote

I guess you wanted to say 2006-04-23 :)

Best Regards, man.
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Post Posted:  Wed Apr 26, 2006 3:40 pm Reply with quote

Okay, sorry for that mistake, too. XD
Back to top

View user's profile Send private message Visit poster's website
haroldoop

Joined: 25 Feb 2006
Posts: 105
Location: Belo Horizonte, MG, Brazil
Post Posted:  Thu Jun 07, 2007 1:32 pm Reply with quote

Good news: SMS support was added to the official z88dk.
http://www.z88dk.org/
Back to top

View user's profile Send private message Visit poster's website
Reply to topic    Forum Index - Development All times are GMT
Page 1 of 1

 

Script phpBB © 2001, 2005 phpBB Group



Back to the top of this page
Back to SMS Power!