Howto:Initialise a Mega Drive

From MegaDrive Wiki
Revision as of 18:36, 7 September 2011 by Tristanseifert (talk | contribs)
Jump to: navigation, search

Before you can start running your game's program on the Mega Drive, you will need to initialize it first. Initializing a Mega Drive is usually the first thing you do in your code, and involves things such as writing to the TMSS register to activate the VDP, clearing RAM, initializing controller ports, and the Z80. Optionally, you can also initialize some VDP registers for use later in your code, although you may choose to do that again every screen mode.

Header

Every Mega Drive game has a header at the first 200h bytes of it's ROM. The TMSS checks a specific address in it for the text "SEGA" and then loads your game's code. Early Model 1 Mega Drives do not have a TMSS - but you still need to have "SEGA" in your header. The header's format is like the one below:

<source lang="asm" line="GESHI_FANCY_LINE_NUMBERS">

==============================================================================
Subroutine to initialise the Z80
==============================================================================

Init_Z80: move.w #$100,($A11100)  ; Send the Z80 a bus request move.w #$100,($A11200)  ; Reset the Z80

Init_Z80_WaitZ80Loop: btst #0,($A11100)  ; Has the Z80 reset? bne.s Init_Z80_WaitZ80Loop  ; If not, keep checking.

lea (Init_Z80_InitCode), a0 ; Load the start address of the code to a0. lea ($A00000), a1 ; Load the address of start of Z80 RAM to a1 move.w #Init_Z80_InitCode_End-Init_Z80_InitCode,d1  ; Load the length of the Z80 code to d1

Init_Z80_LoadProgramLoop: move.b (a0)+, (a1)+ ; Write a byte of Z80 data. dbf d1, Init_Z80_LoadProgramLoop ; If we have bytes left to write, write them. move.w #0,($A11200) ; Disable the Z80 reset. move.w #0,($A11100) ; Give the Z80 the bus back. move.w #$100,($A11200) ; Reset the Z80 again. rts ; Return to sub.

Below is the code that the Z80 will execute.

Init_Z80_InitCode: dc.w $AF01, $D91F, $1127, $0021, $2600, $F977 dc.w $EDB0, $DDE1, $FDE1, $ED47, $ED4F, $D1E1 dc.w $F108, $D9C1, $D1E1, $F1F9, $F3ED, $5636 dc.w $E9E9 Init_Z80_InitCode_End: </source>