Difference between revisions of "Howto:Initialise a Mega Drive"

From MegaDrive Wiki
Jump to: navigation, search
Line 5: Line 5:
  
 
<source lang="asm" line="GESHI_FANCY_LINE_NUMBERS">
 
<source lang="asm" line="GESHI_FANCY_LINE_NUMBERS">
;==============================================================================
 
; Subroutine to initialise the Z80
 
;==============================================================================
 
  
Init_Z80:
+
Vectors: dc.l $FFFE00, EntryPoint, BusError, AddressError
move.w  #$100,($A11100)  ; Send the Z80 a bus request
+
dc.l IllegalInstr, ZeroDivide, ChkInstr, TrapvInstr
move.w  #$100,($A11200)  ; Reset the Z80
+
dc.l PrivilegeViol, Trace, Line1010Emu, Line1111Emu
 
+
dc.l ErrorExcept, ErrorExcept, ErrorExcept, ErrorExcept
Init_Z80_WaitZ80Loop:
+
dc.l ErrorExcept, ErrorExcept, ErrorExcept, ErrorExcept
btst #0,($A11100) ; Has the Z80 reset?
+
dc.l ErrorExcept, ErrorExcept, ErrorExcept, ErrorExcept
bne.s Init_Z80_WaitZ80Loop ; If not, keep checking.
+
dc.l ErrorExcept, ErrorTrap, ErrorTrap, ErrorTrap
+
dc.l Hint, ErrorTrap, VBlank, ErrorTrap
lea    (Init_Z80_InitCode), a0 ; Load the start address of the code to a0.
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
lea    ($A00000), a1 ; Load the address of start of Z80 RAM to a1
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
move.w  #Init_Z80_InitCode_End-Init_Z80_InitCode,d1 ; Load the length of the Z80 code to d1
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
 
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
Init_Z80_LoadProgramLoop:
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
move.b (a0)+, (a1)+ ; Write a byte of Z80 data.
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
dbf d1, Init_Z80_LoadProgramLoop ; If we have bytes left to write, write them.
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
move.w  #0,($A11200) ; Disable the Z80 reset.
+
dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap
move.w #0,($A11100) ; Give the Z80 the bus back.
+
Console: dc.b 'SEGA MEGA DRIVE ' ; Hardware system ID
move.w  #$100,($A11200) ; Reset the Z80 again.
+
Date: dc.b '(C)XXXX YEAR.MON' ; Release date
rts ; Return to sub.
+
Title_Local: dc.b 'YOUR GAME TITLE WILL GO HERE... MEEEEEEEEEEEEEP!' ; Domestic name
 
+
Title_Int: dc.b 'YOUR GAME TITLE WILL GO HERE... MEEEEEEEEEEEEEP!' ; International name
; Below is the code that the Z80 will execute.
+
Serial: dc.b 'GM 10101010-00'  ; Serial/version number
Init_Z80_InitCode:
+
Checksum: dc.w 0
dc.w    $AF01, $D91F, $1127, $0021, $2600, $F977
+
dc.b 'J              ' ; I/O support
dc.w    $EDB0, $DDE1, $FDE1, $ED47, $ED4F, $D1E1
+
RomStartLoc: dc.l StartOfRom ; ROM start
dc.w    $F108, $D9C1, $D1E1, $F1F9, $F3ED, $5636
+
RomEndLoc: dc.l EndOfRom-1 ; ROM end
dc.w    $E9E9
+
RamStartLoc: dc.l $FF0000 ; RAM start
Init_Z80_InitCode_End:
+
RamEndLoc: dc.l $FFFFFF ; RAM end
 +
SRAMSupport: dc.l $5241F820 ; change to $5241F820 (NOT $5241E020) to create SRAM
 +
dc.l $200000 ; SRAM start
 +
dc.l $200200 ; SRAM end (Gives us $200  ($100 useable) bytes of SRAM)
 +
Notes: dc.b '                                                    '
 +
Region: dc.b 'JUE            ' ; Region
 
</source>
 
</source>
  
 
[[Category:Howto]]
 
[[Category:Howto]]

Revision as of 19:51, 7 September 2011

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">

Vectors: dc.l $FFFE00, EntryPoint, BusError, AddressError dc.l IllegalInstr, ZeroDivide, ChkInstr, TrapvInstr dc.l PrivilegeViol, Trace, Line1010Emu, Line1111Emu dc.l ErrorExcept, ErrorExcept, ErrorExcept, ErrorExcept dc.l ErrorExcept, ErrorExcept, ErrorExcept, ErrorExcept dc.l ErrorExcept, ErrorExcept, ErrorExcept, ErrorExcept dc.l ErrorExcept, ErrorTrap, ErrorTrap, ErrorTrap dc.l Hint, ErrorTrap, VBlank, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap dc.l ErrorTrap, ErrorTrap, ErrorTrap, ErrorTrap Console: dc.b 'SEGA MEGA DRIVE ' ; Hardware system ID Date: dc.b '(C)XXXX YEAR.MON' ; Release date Title_Local: dc.b 'YOUR GAME TITLE WILL GO HERE... MEEEEEEEEEEEEEP!' ; Domestic name Title_Int: dc.b 'YOUR GAME TITLE WILL GO HERE... MEEEEEEEEEEEEEP!' ; International name Serial: dc.b 'GM 10101010-00' ; Serial/version number Checksum: dc.w 0 dc.b 'J ' ; I/O support RomStartLoc: dc.l StartOfRom ; ROM start RomEndLoc: dc.l EndOfRom-1 ; ROM end RamStartLoc: dc.l $FF0000 ; RAM start RamEndLoc: dc.l $FFFFFF ; RAM end SRAMSupport: dc.l $5241F820 ; change to $5241F820 (NOT $5241E020) to create SRAM dc.l $200000 ; SRAM start dc.l $200200 ; SRAM end (Gives us $200 ($100 useable) bytes of SRAM) Notes: dc.b ' ' Region: dc.b 'JUE ' ; Region </source>