School me on crt0 for Z80

  • Adam Courchesne

    Adam Courchesne - 2013-11-20

    I'm developing a menu system for a Z80 based multigame arcade game (
    and I'm tempted to do this using sdcc as opposed to assembly.

    I'm not familiar with the concept of "startup code" and this crt0 business. Can someone describe this to me?


  • Philipp Klaus Krause

    Well, at some point "something" passes control to "your program" for the first time. "Something" might be the processor after reset executing the first instruction at a default location, it might be some BIOS or loader.

    The first thing "your program" does is execute some startup code, that typically is in the crt0. The very last instruction in the startup code will be to pass control to main().
    Depending on the system, and depending on what was already done befor by a BIOS or loader, the startup code should do the following:

    • Set the stack pointer
    • Zero the memory
    • Execute the GSINIT and GSFINAL startup code
    • Copy the data for global variables that are initialized from INTIALIZER to INITALIZED
    • Initialize some peripherals
    • pass control to main()

    For an example crt0, see sdcc/device/lib/z80/crt0.s in sdcc. Another example would be the crt0.s in the libcv library for the ColecoVision video game system.


  • Adam Courchesne

    Adam Courchesne - 2013-11-21

    Thanks for the clarification.
    Followup question... once I compile my C and SDCC compiles, links and gives me a *.ihx file, how can I convert that to a binary file to burn into the ROM?
    (assuming that's the correct process to get from the C to the binary)

  • Philipp Klaus Krause

    There are many programs to do that (and some burning programs even prefer ihx files instead of bin).
    I use objcopy (part of binutils and thus installed by default on any typical GNU/Linux system).


  • Maarten Brock

    Maarten Brock - 2013-11-22

    Keep in mind that a bin file is just a big blob of bytes, where an ihx file knows about addresses. Thus it can start at a non-zero offset and leave intermediate gaps untouched.

    I prefer srecord as it can do all sorts of extras, like calculating CRC's. And there is also hex2bin. (Or should I say: there are many hex2bin?)


  • Adam Courchesne

    Adam Courchesne - 2013-11-25

    Ah I see. So how would I go about assembling the crt.s file after I've modified it?
    For example, I took the crt0.s you mentioned, simply changed the stack pointer location and then ran it through the sdcc assembler:
    sdasz80 crt0.s

    But for the following errors:

    crt0.s:91: Error: <u> undefined symbol encountered during assembly.

    same error for lines 95 and 96. Looks like it doesn't like the double colons or some of the other namespaces:

  • Maarten Brock

    Maarten Brock - 2013-11-26

    I suggest to use:

    sdasz80 -plogff crt0.s



Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks