******************************************************
* Sega Genesis Technical Notes by Bart Trzynadlowski *
*                                   and many others! *
******************************************************


Third Edition: February 12, 2000
        - Current
        - Added scroll size data section
        - Added some information pertaining to line rendering
	- More control pad information
        - Horizontal scrolling stuff
        - Added Genesis version information
        - Removed auto-increment section (the information there was totally
          untrue it appears)
        - Control port decoding
        - Added sample Genesis programming routines (68K assembly)
        - Minor touch ups here and there
Second Edition: February 7, 2000
        - Made a table of contents
        - Added tile information and Scroll layer name table information
        - Added info on Genesis security system and "do nots" courtesy of 
          Flavio. He also found a mistake in the joypad notes.
	- Added SMD ROM stuff
First Edition: February 3, 2000
        - Initial release


This document is intended to be used as a reference alongside sega2.doc or any
other complete Sega Genesis technical documentation, it is not intended as a
standalone resource for learning about the Sega Genesis game console. 
        There is also some information here pertaining to specific situations
such as using the Starscream 68000 CPU core in an emulator project.
        I wrote this document while working on a Genesis emulator. I felt some
things needed more description than was available. This document is intended
for emulator developers and Genesis programmers.
        Feel free to pass this document around freely. If you use any parts of
it that were contributed by people other than me, it would be a good idea to
give them credit. 
        Any useful feedback is very much appreciated, especially corrections
and new entries. Do not ask about ROMs or anything stupid. trzy@powernet.net
Check my page at: http://www.powernet.net/~trzy as well. If anything is not
explained well enough, let me know ASAP.
        Much thanks to Joe Groff, Steve Snake, nyef, ATani, Flavio, Charles 
Mac Donald, and Joe Fenton for their invaluable help. 


-- Table of Contents --
         0. Control Port Decoding
         1. VRAM Address Decoding (for Writing)
         2. Tiles
         3. Scroll Layer Name Tables
         4. Scroll Layers and Video Resolution
         5. Obtaining Scroll Size Data
         6. Horizontal Scrolling
         7. Joypads
         8. Version
         9. Crash Course on the Genesis Security System and Common Don'ts
        10. Emulating RAM and ROM w/ Starscream
        11. SMD ROM Format
        12. Useful Genesis Routines


-- 0. Control Port Decoding --

The VDP control port, 0xC00004, has two write modes: "Register Set" (write1)
and "Address Set" (write2). The VDP is able to distinguish between which mode
you want to use by looking at bits 15 and 14 of the word you write to the
control port.

        10: Write1      Otherwise: Write2

For write2, bits 15 and 14 are CD1 and CD0 so the concern of conflict arises.
If you look at the possible access modes which are specified by the 6 CD bits
you will not find any that have 10 in CD1 and CD0; thus, there really is no
conflict.

The CD bits also hold information on wether or not DMA should be performed. If
CD5 is set, a DMA operation has been requested.
        Below is a C example of decoding the control port which may be of use
to emulator authors. The WriteCtrlPortWord() function would be called for a
word write to the control port. The DecodeCtrlPortCommand() function is called
after all data needed has been obtained (1 word for write1; write2 consists of
2 consecutive word writes.) Assume that vdp.reg[] is a 24 element array for
storing VDP data registers 0-23.

void DecodeCtrlPortCommand()
{
        /* access mode consists of cd0-cd3, cd4-cd5 are not needed */
        vdp.access_mode = (vdp.control_port >> 30) +
                          ((vdp.control_port >> 2) & 0xc);
        /* get address */
        switch (vdp.access_mode)
        {
                case 1:         /* VRAM write */
                        vdp.addr = (vdp.control_port >> 16) & 0x3fff;
                        vdp.addr += (vdp.control_port & 3) << 14;
                        break;
                case 3:         /* CRAM write */
                        vdp.addr = (vdp.control_port >> 16) & 0x7f;
                        break;
                default:
                        break;
        }

        if (vdp.control_port & 0x80)    /* cd5 = 1 for DMA */
                DoDMA();
}

void WriteCtrlPortWord(unsigned data)
{
        if ((data >> 14) == 2)          /* write1 mode */
        {
                if (((data >> 8) & 0x1f) < 24)  /* make sure it is a valid
                                                   register */
                {
                        vdp.reg[(data >> 8) & 0x1f] = data;
                }
        }
        else
        {                               /* write2 mode */
                if (vdp.ctrlport_mode == 0)     /* first word */
                {
                        vdp.control_port = data << 16;
                        vdp.ctrlport_mode++;    /* process word 2 next time */
                }
                else                            /* second word */
                {
                        vdp.control_port += data;
                        vdp.ctrlport_mode = 0;
                        DecodeCtrlPortCommand();        /* decode ctrl data */
                }
        }
}       


-- 1. VRAM Address Decoding (for Writing) --

For words and longwords, the VRAM address decoding process is not as
straightforward as some would have you believe. The A0 address bit is not used
in decoding, what this apparently means is that it is treated as 0 (thus
preventing misaligned word writes).
        A0 is used to test wether or not the bytes in a word should be swapped
or not. If A0 = 1, they are swapped, if it is 0, then bytes are not swapped.
A0 is also used when adding the auto-increment value to the VRAM address after
some data is written.

        C example of emulating this:

        if (addr & 1)
                data = ByteSwap(data);
        *((unsigned short int *) (vram + (addr & 0xfffe))) = data;
        addr += auto_inc;


-- 2. Tiles --

The Genesis tile format is quite simple. Each pixel is represented by 4 bits
thus allowing 16 colors per every tile. Tiles are 8x8. The Genesis allows for
up to 64 colors to be displayed: 16 per palette, with 4 palettes. Palettes are
not specified in the tile bitmap, that information is elsewhere and beyond the
scope of this note.

        Example of a bitmap for the letter "A". We use the color 0x7 for each
        of the pixels. Remember, color 0 is _always_ transparent in any
        palette:

        dc.l    $00077000
        dc.l    $07700770
        dc.l    $07700770
        dc.l    $07777770
        dc.l    $07700770
        dc.l    $07700770
        dc.l    $00000000
        dc.l    $00000000


-- 3. Scroll Layer Name Tables --

Scroll layers (who's addresses and sizes are specified in VDP registers) are
"name tables" where each entry contains an index to a specific 8x8 tile. These
entries are arranged in a linear fashion.
        Each entry is a word in size. Here is the format for an entry:

         15  14  13  12  11  10  9   8   7   6   5   4   3   2   1   0
        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
        |PRI|PL1|PL0|VFP|HFP|I10|I9 |I8 |I7 |I6 |I5 |I4 |I3 |I2 |I1 |I0 |
        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

        PRI             Priority (1 = highest, on top; 0 = lowest, on bottom.)
                        Some people use "Scroll A high", "Scroll A low",
                        "Scroll B high", and "Scroll B low" to refer to high
                        (PRI = 1) and low (PRI = 0) parts of a Scroll layer
        PL1-PL0         Palette (0, 1, 2, or 3)
        VFP             Vertical flipping (1 = true)
        HFP             Horizontal flipping (1 = true)
        I10-I0          Index of 8x8 tile in pattern area (VRAM $0000).
                        Multiply this by 32 to get offset in VRAM of the tile
                        bitmap

The information here applies to both Scroll A and Scroll B, please refer to
more complete documentation for information on how to set Scroll layer
addresses and what-not.

For emulator authors implementing a line-based engine, the following info may
be of use.
        To get the correct word-size entry for the first tile on the current
line within a Scroll layer (not taking into account horizontal or vertical
scroll values in the h-scroll table or VSRAM) you can use this formula:
((l / 8) * 2) + scroll_layer_addr -- where l is the line number, the rest is
obvious.
        To obtain the doubleword within a 32-byte tile which contains the 8
pixels you want to render on the current line, add the following to the base
address of the tile: (l & 7) * 4 
        For vertically flipped tiles, the following formula will get the
correct doubleword: ((l & 7) ^ 7) * 4 -- remember that ^ means XOR.        


-- 4. Scroll Layers and Video Resolution --

The Genesis supports two video modes: 32x28 cell and 40x28 cell (which in
pixels are 256x224 and 320x224.) The video resolution is how many 8x8 tiles
are displayed on-screen. The vertical portion differs with PAL I believe, but
it that is not relevant.
        Scroll A and Scroll B can have a number of different sizes which
obviously often cannot be all shown on-screen: 32x32, 32x64, 32x128, 64x32,
64x64, and 128x32. The Sega Programming FAQ says that 128x128 is possible, but
I don't think it is since that would take up 32KB of VRAM which would not 
leave room for the other Scroll layer, the Window, or the patterns.

Only a portion of the Scroll layer is visible on the screen. How it is
displayed can be represented by the following diagram:

** Assuming the Scroll size is 64x64 and the screen 40x28, everything is
   addressed in cell units (obviously not to scale) **

        - = Scroll layer
        * = Visible portion (Sega calls this the "effective display")

         H0                                     H39                 H63
        +****************************************--------------------+
    V0  |* V0                                   *                    |
        |*                                      *                    |
        |*                                      *                    |
        |*                                      *                    |
        |*                                      *                    |
        |* V27                                  *                    |
        |****************************************                    |
        |                                                            |
        |                                                            |
        |                                                            |
        |                                                            |
        |                                                            | 
        |                                                            |
    V63 |                                                            |
        +------------------------------------------------------------+

Thus you can see that there are tiles beyond the right limit of the visible
screen, and below as well. This method of having the layer larger than can be
displayed is for scrolling. The above is what would occur if one set the
Scroll size to 64x64 and the resolution to 40x28 without scrolling the screen
or anything.
        If you are having problems emulating Scroll layers because everything
is shifted over you are probably forgetting to render the invisible parts of
the Scroll or to skip over them and on to the next visible row. I hope that
made sense.


-- 5. Obtaining Scroll Size Data --

The size of the Scroll layers (both A and B) are specified in Mode Set
Register #4:

        To obtain the horizontal width in cell units:

        ((mode_set_reg4 & 3) + 1) << 5

        Vertical height is similar:

        (((mode_set_reg4 & 0x30) >> 4) + 1) << 5 

        A better way of getting the vertical height would be:

        ((mode_set_reg4 & 0x30) + 0x10) << 1

If either of the above formulas yield 96, then an illegal size was specified,
the Genesis does not have 96xXX or XXx96 modes. The only available bit
combinations are 00 (32 cell), 01 (64), and 11 (128); 10 is prohibited.


-- 6. Horizontal Scrolling --

"Full" or "Overall" mode: Look at the first entry, this scroll value applies
to every line.

"Each one cell" mode: Starting at entry 0 for the Scroll layer in question,
every 8th entry is relevant. Each of the 8 lines represented by that entry
are scrolled by the amount specified. Entry 0 represents lines 0-7, entry 8
represents lines 8-15, etc. The other entries are meaningless.

"Each one line" mode: Each of the lines has its own scroll value. Thus every
entry is relevant.

The scroll value is a pixel offset relative to the start of the line. The
horizontal scroll values are inversed. To get the proper value, try:
0x400 - hscroll_value or: ~hscroll + 1
        Bits 10-15 of the value are free for program use. Remember to mask off
your hscroll data before using it (AND with 0x3ff.)

An important topic to mention is wrapping. When you specify a value that is
beyond the limit of the line, it will start over at the beginning of that same
line and NOT at the next line. 

Below are some snippets of email I exchanged with Charles Mac Donald on the
subject of implementing proper h-scrolling in a line-based engine. In the
first email, I was asking about wether or not a mask can be used to handle the
wrapping that occurs when you reach the end of the line.

** Begin Email **
From: Charles Mac Donald
To: Bart Trzynadlowski
Subject: Re: Are you sure Sonic1 uses DMA?

> Is there a way to use a mask to handle this? What I was trying to do was
> check my nametable pointer with nametable_pointer+width_in_tiles*2 and if
> they matched (thus the end of the scroll field has been reached) I would
> subtract width_in_tiles*2 from my name table pointer (the *2 is because the

You're right about masks, since the playfields are always even in size,
you can just use a logical AND to handle wrapping. When the playfield
size register is written to (I forgot the number, it's #13 or
something), I set up a mask to 1F, 3F, or 7F, depending on the size.
Then I grab name table data like this:

for(column = 0; column < 40; column += 1) // assume the width is 40
cells
{
  offset = (column + (horizontal_scroll / 8)) & column_mask; // column
mask is 1f/3f/7f
  attribute = vram[name_table_a + row + column];
}

Where 'row' is the vertical offset into the name table. You can do the
masking thing for vertical scrolling too, though it works a little
differently. This example doesn't take into account that the attribute
data is word sized, what I'd do normally is have a pointer like
'unsigned short int *ptr' to cast the VRAM data as words, and then the
offset that's been calaculated would work OK.
** End Email **

Here is an example of an early implementation I wrote of an engine:

        scroll_width = ((vdp.reg[16] & 3) + 1) << 5;

        hscroll &= 0x3ff;
        hscroll_ntab = hscroll >> 3;
        hscroll_video = hscroll & 7;
        hscroll_mask = scroll_width - 1;
        video = ((line * scroll_width) << 3) + video_buffer - hscroll_video;

        lines_down = ((line >> 3) * scroll_width) << 1;

        for (column = 0; column < 41; column++)
        {                
                nametab = lines_down + (((column + hscroll_ntab) &
                          hscroll_mask) << 1) + scroll_addr;
                Draw(line, nametab, video);
                video += 8;                
        }

The hscroll variable is the actual inversed hscroll value, notice how I mask
it off to 10-bits. line is the current scanline being rendered, nametab is the
physical address to the Scroll field nametable, video is the physical address
to where we start drawing.
        This may not be of much help, since I do not want to go into the
details of what my algorithm really is and how it works, but this is just to
give you a general idea of how a line based engine may look like. 


-- 7. Joypads --

** Begin Email **
From: Joe Groff
To: Bart Trzynadlowski
Date: Sat, 15 Jan 2000 23:06:49 -0800 (PST)
Subject: Re: DGen/SDL 

> is there any good info anywhere on control pad interfacing?

As far as I can tell, this is how the controller works. There are two one-byte
data sets:
    v & 0x40: always set
    v & 0x20: C
    v & 0x10: B
    v & 0x08: right
    v & 0x04: left
    v & 0x02: down
    v & 0x01: up
and:
    v & 0x40: always clear
    v & 0x20: START
    v & 0x10: A
    v & 0x08: right
    v & 0x04: left
    v & 0x02: down
    v & 0x01: up
By reading a word from 0xA10002 (for first controller) or 0xA10004 (for
second), the word will have one of the above bitmasks written to both bytes.
If you write a byte to 0xA10003(for 1st) or 0xA10005(for 2nd) with the 0x40
bit set, you'll get the first set, otherwise you'll get the second.

Example:
  Player 1 is pressing left and the A and B buttons simultaneously. Player 2
  is pressing B, C, and START. As the game, in order to probe controller 1:
  - I send byte 0x00 to 0xA10003 (or word 0x0000 to 0xA10002 :)
  - I read a word from 0xA10002, which gives me 0x1414. From the lower table,
    I see A and left are being pressed.
  - I send byte 0x40 to 0xA10003.
  - I read another word from 0xA10002. This time I get 0x5454, which from the
    upper table means B and left are being pressed.
  Similarly, to probe controller 2:
  - I send byte 0x00 to 0xA10005.
  - I read from 0xA10004, and get 0x2020. So START is being depressed.
    of the controller!
  - I send byte 0x40 to 0xA10005.
  - I read from 0xA10004 again, get 0x7070, so B and C are being pressed.

There's also a lot of odd code to handle 6-button controllers in DGen, but
as it's getting a bit late, I can't quite understand it. Hopefully this is
accurate and clear enough to at least get you started.
** End Email **
 
** Begin Email **
From: ATani
To: Bart Trzynadlowski 
Date: Sat, 15 Jan 2000 23:06:43 -0800
Subject: Re: genesis tech question post

Ok, Well on the genesis the joysticks are read in by the z80 and passed to
the 68000 chip via memory addresses: A10003 & A10005.

If bit 6 of the Stored Controller 1 information is set then you return the
following information when reading Address: A10003 (Byte mode):

Bit:        Description:
0           Up
1           Down
2           Left
3           Right
4           B
5           C

If Bit 6 is not set return the following:
Bit:        Description:
4           A
5           Start

Address A10005 is the same except values returned are for joystick port 2.

The Stored data are in reference to the byte values written to A10003 and
A10005 (joystick port 1 and joystick port 2)
** End Email **

Flavio points out the information from ATani's email may be somewhat faulty:

"The Z80 has nothing to do with joypad reading. Actually, I believe the Z80 
banker thingy will go wacko, should you attempt such a stunt. Gotta test 
that."

Joe Fenton gave me this information on Eidolon's Tavern:

The 3 button control pads are easy; the port has 7 bits that are used: 1
unused bit, 1 bit used to output a select line, and 6 bits used to input data
from the controller. For the 3 button pad, driving the select high to read
CBRLDU on the six input lines. Drive the select low to read SA00DU. A 6 button
controller is a little more complex; you have to do the above reading four
times to get the following data packet:

        CBRLDU - first high
        SA00DU - first low
        CBRLDU - second high
        SA00DU - second low
        CBRLDU - third high
        SA0000 - third low
        CBMXYZ - fourth high
        SA1111 - fourth low

The controller is only read by the vertical blank routine so that it is only
read at most 60 times per second; trying to read it more often than that
results in corrupted data packets from most controllers.


-- 8. Version --

Version information on the Sega Genesis can be obtained by reading from the
port at 0xa10001.

        Version byte:

        +---+---+---+---+---+---+---+---+
        |MDL|VMD|DSK|RSV|VR3|VR2|VR1|VR0|
        +---+---+---+---+---+---+---+---+

        MDL             Model: 0 - domestic, 1 - overseas
                        This is relative to Japan (it is 0). Thus USA and
                        European models have this set to 1
        VMD             0 - NTSC CPU clock (~7.67 MHz), 1 - PAL CPU clock
                        (~7.60 MHz)
        DSK             Disk drive: 0 - floppy disk drive connected, 1 - not
                        connected
        RSV             Reserved (not used)
        VR3-VR0         System version. sega2.doc has this to say: "The
                        present hardware version is indicated by $0."

Determing wether we have an American Genesis, a European MegaDrive, or a
Japanese MegaDrive is done by looking at the upper nibble of this byte.
        If the model bit indicates 0, we know it is a Japanese system. The
Genesis/MegaDrive originated in Japan thus Japan is the domestic country.
        If the model bit indicates 1 it is not from Japan. To determine wether
it is from the USA or Europe we must look at the VMD bit. European countries
use the PAL television standards, whereas the US uses NTSC. If the VMD bit is
1, we have a European system, otherwise it is from the US.

Emulators should return the following values:

        USA: 1010 0000  (0xa0 hex)      Overseas, NTSC, no floppy drive,
                                        version 0
     Europe: 1110 0000  (0xe0 hex)      Overseas, PAL, no floppy drive,
                                        version 0
      Japan: 0010 0000  (0x20 hex)      Domestic, NTSC, no floppy drive,
                                        version 0

If you want to provide support for different version numbers for some reason,
you can obviously do so, and if you are emulating the floppy disk system (I
don't see how the situation would arise, but it is up to you) you can set that
to 0.
        I'm unclear as the what a domestic PAL system would be. Japan is an
NTSC country... although I'm not sure if it is exclusively so. Perhaps it has
something to do with other Asian countries? Let me know if you have any info
on this (trzy@powernet.net)!


-- 9. Crash Course on the Genesis Security System and Common Don'ts --

** The following information is courtesy of Flavio **

Crash course in Genesis security:

1) There must be either 'SEGA' or ' SEGA' in ASCII at offset 0x100 (256
decimal.)

2) If the four least significant bits of 0xA10001 are higher than zero, the
poor programmer must write 'SEGA' to 0xA14000.
Example:
MOVE.B $A10001, D0
ANDI.B #$F, D0
BEQ.S NO_VDP_LOCK
MOVE.L #'SEGA', $A14000.
NO_VDP_LOCK:
[Yer code here]

Yes, I know many of you can't read 68K ASM yet, but I don't know how to do
this on Paul Lee's C compiler (or any other for that matter.) :/

A list of common Caveat Gennyptor's follows:

- Be careful not to access forbidden addresses (see SEGA2.DOC), as the Genny
locks up instantly if you dare to touch them.
- Don't read from the VDP data port (C00000) if you have sent a
"VRAM/CRAM/VSRAM write" command (and vice versa.)
- The FM doesn't work if the Z80 is reset (their reset lines are wired
together.)
- Always have the Z80 busreq'ed before reading the joyports.
- Never attempt to read PSG ports.
- DMA transfers should be controlled by code placed at the Genny's work RAM
(0xFF0000-0xFFFFFF.)
- Don't attempt to transfer data to/from the VDP if a DMA is in progress.
- Z80 RAM _must_ be accessed in byte units.
- Don't toggle the joystick's select line more than four times per vertical
refresh, to ensure 6-button joypad compatibility.
- It takes at least 16 68K clock ticks for the joyport readouts to become
really stable, after you have toggled the aforementioned select line.

Flavio has also pointed out one more important thing not to attempt:

CLR, ST, and TAS cannot be used to access the C000xx range. (It's a
derivative of the "don't read with the VDP set to write" commandment.)


-- 10. Emulating RAM and ROM w/ Starscream --

Often times, Genesis games do some odd tricks that can be buggers to emulate.
One of these is jumping backwards (which causes the 24-bit address to wrap
around to 0xFFFFFF) into work RAM to execute code. Since Starscream uses
32-bit data to handle addresses, this sort of maneuver would wrap around to
0xFFFFFFFF (32-bit) which is out of the Genesis address space.
        Below is part of a Starscream context for emulating the Genesis work
RAM and ROM. I have also included the code for the handlers (Joe Groff helped
with this -- thanks Joe!) The dummy handlers are for regions of the address
space where accesses are ignored (I have removed all references to VDP and I/O
stuff since it would just clutter the example.) In reality, the data items
rom and ram would be dynamically allocated and would have to be added to these
structures during initialization time. They would be seen here as (unsigned)
NULL or (void *) NULL.

struct STARSCREAM_PROGRAMREGION fetch_instructions[] =
{
        { 0x000000, 0x3fffff, (unsigned) rom },
        { 0xff0000, 0xffffff, (unsigned) ram - 0xff0000 },
        { 0xff000000, 0xff3fffff, (unsigned) ram - 0xff000000 },
        { 0xfff00000, 0xffffffff, (unsigned) ram - 0xfff00000 },
        { -1, -1, NULL }
};
struct STARSCREAM_DATAREGION    read_data_byte[] =
{
        { 0x000000, 0x3fffff, NULL, (void *) rom },
        { 0x400000, 0xffffff, StarscreamReadRAMByte, NULL },
        { -1, -1, NULL, NULL }
};
struct STARSCREAM_DATAREGION    read_data_word[] =
{
        { 0x000000, 0x3fffff, NULL, (void *) rom },
        { 0x400000, 0xdfffff, StarscreamFakeRead, NULL },        
        { 0xe00000, 0xffffff, StarscreamReadRAMWord, NULL },
        { -1, -1, NULL, NULL }
};
struct STARSCREAM_DATAREGION    write_data_byte[] =
{
        { 0x000000, 0xdfffff, StarscreamFakeWrite, NULL },
        { 0xe00000, 0xffffff, StarscreamWriteRAMByte, NULL },
        { -1, -1, NULL, NULL }
};
struct STARSCREAM_DATAREGION    write_data_word[] =
{
        { 0x000000, 0xdfffff, StarscreamFakeWrite, NULL },
        { 0xe00000, 0xffffff, StarscreamWriteRAMWord, NULL },
        { -1, -1, NULL, NULL }
};

unsigned StarscreamReadRAMByte(unsigned address)
{
        return ram[(address ^ 1) & 0xffff];
}
        
unsigned StarscreamReadRAMWord(unsigned address)
{
        return *((unsigned short *) (ram + (address & 0xfffe)));
}

void StarscreamWriteRAMByte(unsigned address, unsigned data)
{
        ram[(address ^ 1) & 0xffff] = data;       
}

void StarscreamWriteRAMWord(unsigned address, unsigned data)
{
        *((unsigned short *) (ram + (address & 0xfffe))) = data;
}

unsigned StarscreamFakeRead(unsigned address)
{
        return 0;
}

void StarscreamFakeWrite(unsigned address, unsigned data)
{       
}

RAM is at 0xff0000-0xffffff and is mirrored every 64KB at 0xe00000-0xfeffff.

Please see the Starscream documentation for information on how the core works.
At the time of this writing, Neill Corlett's page is at:
        http://www4.ncsu.edu/~nscorlet/


-- 11. SMD ROM Format --

The SMD ROM format consists of a 512 byte header and the actual ROM image in
16KB chunks with the odd bytes at the beginning, and the even bytes at the 
end.

	Header offsets:
	0x00:	Number of 16KB blocks. This number is often incorrect and it 
		is wise to calculate it manually: (sizeof(file) - 512) / 16384
	0x02:	If 0, the ROM is standalone or the last part of a series. 
		Otherwise it is part of a split ROM set.
	0x08:	0xaa
	0x09:	0xbb

Note: Most ROMs have 0xaa and 0xbb at 0x08 and 0x09 but a very small percentage 
(about 1.28% by my calculations) do not conform to this. Those offsets are 
useful for checking wether a ROM is in SMD format but be aware that they are 
not always correct. An example of a ROM image floating around that would not
pass this check would be the US version of Contra Hard Corps (contraus.smd at
www.emux.com.) It also has 0 at header offset 0x00, but it _is_ a working SMD
ROM.

	Decoding a 16KB SMD blocke: (thanks to XnaK and Kuwanger)
        1. If the byte offset in the block is less than 8192, copy the byte
           from the SMD block to the first unused odd offset in the decode
           buffer.
        2. Otherwise, put it in the first unused even offset in the decode
           buffer.

	Example block-decoding C function: (from my own GROM v0.75)
	
	void smd_bin(unsigned char *bin_block, unsigned char *smd_block)
	{
		int	i, o = 1, e = 0;
		
		/* convert 16KB of SMD to BIN */
		for (i = 0; i < 8192; i++)
		{
			bin_block[o] = smd_block[i];
			bin_block[e] = smd_block[i + 8192];
			o += 2;
			e += 2;
		}
	}	
		
Get GROM at: 
	http://www.powernet.net/~trzy
	http://www.zophar.net
	http://eidolon.psp.net		
	http://www.vintagegaming.com
		...or...
	If all else fails, email me.


-- 12. Useful Genesis Routines --

This section is meant to serve as a collection of useful Genesis routines for
beginning Genesis developers. Anyone is encouraged to send in their own code
(in 68K assembly or C) to trzy@powernet.net!
        All of these routines have been used successfully with Charles Doty's
Genesis assembler, ASM. I'm not very proficient with 68K yet, so excuse my
sloppy, register-devouring code.

* RegisterSet: a1 = control port, d0 = register #, d1 = data to write
RegisterSet:
        asl     #8, d0        
        or      d1, d0
        ori     #$8000, d0              ; write1
        move.w  d0, (a1)
        rts

* WriteColor: a0 = data port, a1 = control port, d0 = color #, d1 = data
* Note: data must be in CRAM format already: 0000BBB0GGG0RRR0
WriteColor:
        asl     #1, d0                  ; * 2 for CRAM address
        ori     #$c000, d0              ; cd1 = 1, cd0 = 1: CRAM write
        move.w  d0, (a1)                ; send address
        move.w  #0, (a1)
        move.w  d1, (a0)                ; send data
        rts

* VRAMWriteAddr: a0 = data port, a1 = control port, d0 = VRAM address,
* d1 = scratch (used internally, contents destroyed)
VRAMWriteAddr:
        move.l  d0, d1                  ; copy here
        andi    #$c000, d1              ; isolate a15, a14
        asr     #8, d1
        asr     #8, d1                  ; get them into lowest 2 bits
        andi    #$3fff, d0              ; replace a15, a14 here with...
        ori     #$4000, d0              ; VRAM write
        move.w  d0, (a1)                ; request address...
        move.w  d1, (a1)
        rts

* WriteTile: a0 = data port, a1 = control port, a2 = address of tile,
* d0 = VRAM address (0 - $bfff), d1 = scratch, d2 = scratch
* Assumes that the auto-increment (register #15) is set to 2.
WriteTile:
        move.l  d0, d1                  ; copy here
        andi    #$c000, d1              ; isolate a15, a14
        asr     #8, d1
        asr     #8, d1                  ; get them into lowest 2 bits
        andi    #$3fff, d0              ; replace a15, a14 here with...
        ori     #$4000, d0              ; VRAM write
        move.w  d0, (a1)                ; request address...
        move.w  d1, (a1)

        move.l  #8, d2
WriteTile_loop:
        move.l  (a2), d1
        move.l  d1, (a0)
        add     #4, a2

        sub     #1, d2
        bne     WriteTile_loop

        rts
