# Start here: the three machines, as a board author sees them Every fact below names its source file under `reference/` (fetch it with `scripts/fetch_references.py`). Where two sources disagree, both are given and the disagreement is listed in [undocumented-behavior.md](undocumented-behavior.md). The tier of each source is in [validation.md](validation.md). ## The shared skeleton All three machines are one Z80, one video chip (the VDP), one SN76489 sound generator, some RAM, a cartridge slot and two joypad ports, clocked from one crystal. z80-python supplies the CPU through its callables contract: ```python cpu = Z80CPU(read_byte, write_byte, read_port=read_port, write_port=write_port) cycles = cpu.step() # one instruction; the board advances the VDP and PSG by `cycles` ``` The board decodes the 16-bit memory bus into ROM, mapper registers and RAM, and the 8-bit I/O bus into the VDP, the PSG and the peripheral ports. It raises the Z80's INT line from the VDP (the Z80 is in IM 1 on all three machines, so the data bus value is never used), and NMI from the pause button. ### Clocks `reference/smspower-wiki/ClockRate.pmwiki`: | Region | Crystal | ÷15 = CPU and PSG clock | Lines per frame | Frames per second | | --- | --- | --- | --- | --- | | NTSC | 53.693175 MHz | 3,579,545 Hz | 262 | 59.92 | | PAL | 53.203424 MHz | 3,546,893 Hz | 313 | 49.70 | The VDP runs its dot clock at crystal ÷10 (342 dots per line). The CPU runs at crystal ÷15, so one line is 342 × 10 / 15 = **228 CPU cycles**, and one frame is 262 × 228 = 59,736 cycles (NTSC) or 313 × 228 = 71,364 (PAL). FluBBa's VDP test assumes exactly 228 cycles per line (`VDPTest-ReadMe.txt`). The SG-1000 drives its TMS9918A from a separate 10.738635 MHz crystal (3 × the CPU clock; MAME `src/mame/sega/sg1000.cpp` PCB notes), which gives the same 228 cycles per line. ## SG-1000 (1983), SC-3000, SG-1000 II Sources: MAME `src/mame/sega/sg1000.cpp` (PCB layout and address maps), `reference/smspower/sg1000.txt` (a 1999 draft: its "8 KB RAM" is wrong, see below), `reference/smspower-wiki/IOPortMapSG.pmwiki`. - **CPU**: NEC D780C-1 or Zilog Z8400A, NMOS Z80 at 3.579545 MHz. - **Video**: TI TMS9918ANL with 16 KB of VRAM (eight MB8118 16K×1 DRAMs). - **Sound**: TI SN76489AN, discrete. Its noise shift register is 15 bits with taps 0 and 1 ($0003); see [SN76489](#the-sn76489) below. - **RAM**: 1 KB (TMM2009) at $C000–$C3FF, mirrored through $FFFF. MAME: `map(0xc000, 0xc3ff).mirror(0x3c00).ram()`. The SC-3000 has 2 KB (`0xc000–0xc7ff` mirrored), and carts may add their own RAM. - **Cartridge**: $0000–$BFFF, no mapper on standard carts. MAME's `hash/sg1000.xml` lists 214 sets; 23 need a special board (Dahjee RAM expansions, Othello, BASIC Level III, Terebi Oekaki, Castle, Card Catcher). - **I/O** (MAME's map, mirrors included): $40–$7F write → PSG; $80–$BF even → VDP data, odd → VDP control/status; $C0–$FF → joypads (`$DC`/`$DD` by convention; 0 = pressed). The SC-3000 routes the keyboard through an 8255 PPI. - **Interrupts**: the VDP's INT output drives the Z80's /INT (IM 1 → $0038). The HOLD (pause) switch drives /NMI (→ $0066). - **No BIOS.** The cartridge is at $0000 at reset. The **SC-3000** is the same machine as a computer: 2 KB of RAM, a keyboard read through an 8255, and ports decoded by A7, A6 and A5 alone. The **SF-7000** plugs into its cartridge slot and replaces the memory map with 64 KB of RAM and an IPL ROM, and adds a uPD765A disk controller and a second 8255 at ports $E0-$FF. Both are written up in [undocumented-behavior.md](undocumented-behavior.md) ("SC-3000", "SF-7000"), each rule with its source: Charles MacDonald's SC-3000H notes (`reference/smspower/sc3000h-20040729.txt`) for the computer; MAME's driver, NEC's data sheet and the IPL ROM's own code for the disk unit. ## Mark III (1985), Master System (SMS1), Master System II (SMS2) Sources: Sega's *Software Reference Manual for the Sega Mark III Console* (`reference/smspower-attach/sms-officialdocs-*.zip`, `reference/smspower-wiki/SMSOfficialDocs.pmwiki`), Charles MacDonald's `reference/smspower/smstech-20021112.txt` and `msvdp-20021112.txt`, and the SMS Power wiki pages `MemoryMap`, `Mappers`, `IOPortMap`, `Port3E`, `PeripheralPorts`, `SMS1SMS2VDPs`, `BIOS`, `BIOSes`, `RegionDetection`. ### Memory | Range | Contents | | --- | --- | | $0000–$03FF | First 1 KB of ROM, never paged (it holds the reset and interrupt vectors) | | $0400–$3FFF | Slot 0 (16 KB page; register $FFFD) | | $4000–$7FFF | Slot 1 (register $FFFE) | | $8000–$BFFF | Slot 2 (register $FFFF), or cartridge RAM when $FFFC bit 3 is set | | $C000–$DFFF | 8 KB system RAM | | $E000–$FFFF | Mirror of system RAM; writes to $FFFC–$FFFF also go to the mapper | The **Sega mapper** (315-5208, 315-5235, 315-5365, 315-5912; pinouts in `reference/smspower-wiki/MapperPinouts.pmwiki`) has four registers: $FFFC (RAM enable, RAM bank, bank shift), $FFFD–$FFFF (slot banks). The 315-5235 resets to $FFFC=00, $FFFD=00, $FFFE=01, $FFFF=02; the 315-5208's reset state is thought to be undefined (`Mappers.pmwiki`). Third-party boards: **Codemasters** (bank registers at $0000, $4000, $8000; no protected first 1 KB), several **Korean** mappers, **4 Pak**, **Janggun**, **Nemesis/MSX**. MAME's `hash/sms.xml` lists 653 sets; the non-Sega boards are hicom 9, zemina 8, korean 6, korean_nb 5, codemasters 5, and one each of seojin, nemesis, korean_188in1, janggun and 4pak. ### I/O ports Decoded by A7, A6 and A0 only, so every port has mirrors (`msvdp-20021112.txt` section 2; `IOPortMap.pmwiki` has the table for SMS1, SMS2 and Mark III): | Port (canonical) | Read | Write | | --- | --- | --- | | $3E (even $00–$3F) | open bus (SMS1), $FF (SMS2) | Memory control: enable or disable BIOS, cartridge, card, expansion, RAM, I/O chip | | $3F (odd $00–$3F) | as above | I/O control: TH and TR pin direction and output level for both ports | | $7E (even $40–$7F) | V counter | SN76489 | | $7F (odd $40–$7F) | H counter (latched on a TH edge) | SN76489 | | $BE (even $80–$BF) | VDP data | VDP data | | $BF (odd $80–$BF) | VDP status (clears the flags and the latch) | VDP control | | $DC (even $C0–$FF) | Joypad 1 bits 0–5, joypad 2 up/down | — | | $DD (odd $C0–$FF) | Joypad 2, reset button, TH inputs | — | | $F0–$F2 (Japanese SMS, Mark III + FM unit) | $F2 FM detect | YM2413 address, data, enable | **Region detection** uses port $3F: an export console reflects the TH output levels written to $3F back on port $DD bits 6–7, a Japanese console does not (`RegionDetection.pmwiki`). The Japanese console also lacks the $3E BIOS switching the export BIOS uses. ### BIOS The export SMS1 and SMS2 have a BIOS mapped at $0000 at reset. It checks the cartridge header ("TMR SEGA" at $7FF0, `ROMHeader.pmwiki`), then writes $3E to map the cartridge in and jumps to $0000. With no valid header it runs its own built-in game (Hang On, Alex Kidd, Sonic, etc.; `BIOSes.pmwiki` lists every known dump). Two consequences for the board: - A cartridge without the header (Sega's own **Color & Switch Test** is one) never boots on an export BIOS. Measured on MAME 0.285 `sms`: the BIOS ran Alex Kidd instead. It runs on `smsj`. - Booting through the BIOS takes time: on MAME 0.285 `sms` (mpr-12808) the first cartridge I/O came at CPU cycle 3,408,512, about 57 frames. The board does both: given a BIOS image it boots through it, and without one it starts at the cartridge with the cartridge slot enabled (what most emulators do by default). ### Interrupts - **INT** (IM 1 → $0038) from the VDP: the frame interrupt, when enabled by register 1 bit 5, and the line interrupt, enabled by register 0 bit 4. The line fires when a down-counter reloaded from register 10 underflows. The line stays asserted while a pending flag is set and its enable is on; reading the control port clears both flags (`msvdp-20021112.txt` section 12). - **NMI** (→ $0066) from the pause button, edge-triggered (`PauseButton.pmwiki`, `JoypadPauseButton.pmwiki`). The Game Gear has no pause NMI; its Start button is read from port $00 bit 7. ## The VDP ### Programming model (all three machines) From `msvdp-20021112.txt` sections 3 and 4, checked by FluBBa's VDP test on real hardware (`VDPTest-ReadMe.txt`): - A 14-bit address register and a 2-bit code register, set by a two-byte command word written to the control port. A one-bit latch tracks first or second byte. The first byte sets the low 8 address bits *immediately*; the test "1byte wr sets rd adr" checks that. - Code 0: prefetch VRAM[address] into the read buffer, increment. Code 1: VRAM write. Code 2: register write (register = second byte bits 0–3, value = first byte; registers 11–15 are ignored). Code 3: CRAM write (TMS9918 has no CRAM). - A data-port read returns the buffer, then refills it from VRAM[address++]. A data-port write stores the byte *and also loads the buffer with it* ("VRAM wr set VDPbuffer", "CRAM wr set VDPbuffer"). - Reading the control port, reading the data port and writing the data port all reset the latch. - Status: bit 7 frame interrupt pending, bit 6 sprite overflow, bit 5 sprite collision. The low 5 bits are the fifth-sprite number on the TMS9918A and garbage on the SMS VDPs. ### Modes `reference/smspower-wiki/Modes.pmwiki` has the full 16-combination table of M1–M4. In short: - **Graphics I, Graphics II, Text, Multicolor** (TMS9918A modes 1, 2, 0, 3): 1-bit patterns, a fixed 15-colour palette, 32 one-colour sprites with at most four per line, a fifth-sprite flag. Specification: `reference/manuals/TMS9918A_TMS9928A_TMS9929A_VDP_Data_Manual_Nov82.pdf`. Undocumented mixed modes: Sean Young's `reference/smspower/tms9918a.txt`. - **Mode 4** (Sega): 4-bit tiles (448 in practice), a 32×28 name table with per-tile flip, priority and palette select, two 16-colour palettes in 32 bytes of CRAM (6-bit RGB on the SMS, 12-bit on the Game Gear), 64 sprites with at most eight per line, per-line X scroll, per-frame Y scroll, scroll locks, a left-column mask, and the line interrupt. - **Extra-height Mode 4** (224 and 240 lines): SMS2 and Game Gear only. Only a few Codemasters games use 224; 240 does not work on NTSC. ### What differs between VDP revisions The SMS1's 315-5124 ANDs some legacy register bits into the VRAM address (register 2 bit 0 into the name-table fetch, register 3 into pattern fetches, register 5 bit 0 into the sprite-table fetch, register 6 bits 0–1 into sprite patterns), so games that set those bits wrongly show glitches on SMS1 only. The SMS2's 315-5246 ignores them and adds the extra-height modes. On the SMS1, register 1 bit 0 (zoomed sprites) doubles only the first *N* − 4 sprites on a line horizontally. The details are in `VDPRegisters.pmwiki` and `SMS1SMS2VDPs.pmwiki`; [undocumented-behavior.md](undocumented-behavior.md) lists them. ## The SN76489 `reference/smspower-wiki/SN76489.pmwiki` (Maxim, 2005, with noise data by Charles MacDonald and John Kortink), and the TI manuals in `reference/manuals/`: - Three square-wave tone channels and one noise channel. Each has a 10-bit counter that reloads from its register; the input clock is divided by 16. Four-bit attenuation in 2 dB steps; 15 is silent. - A latch/data byte protocol on one write-only port. - Noise: a linear-feedback shift register. **Two variants matter here:** - SMS1, SMS2, Game Gear and Mega Drive (the Sega-integrated PSG): 16 bits, taps bits 0 and 3 ($0009), fed back into bit 15. - SG-1000, SC-3000H, Othello Multivision (the discrete TI part): 15 bits, taps bits 0 and 1 ($0003), fed back into bit 14. The wiki's two downloadable "white noise" bit patterns are illustrations, not recordings, and the Sega one contradicts the $0009 description ([undocumented-behavior.md](undocumented-behavior.md), open item 6). - The Sega-integrated versions power up with zeroes in the tone and noise registers and ones in the volume registers (silence). - The SMS2 clips at high volumes where the SMS1 and Mega Drive keep the full 2 dB steps (an analogue design difference; forum thread 17121). - The Game Gear adds a stereo control register at port $06: one enable bit per channel per side (`GameGearStereo`, included in `SN76489.pmwiki`). - On the SG-1000 the PSG's /READY output can stall the Z80 for 32 clocks after a write. MAME lists "connect PSG /READY signal to Z80 WAIT" as a TODO in `sg1000.cpp`, so MAME does *not* model it. ## Game Gear (1990) Sources: Sega's Game Gear hardware manual (`reference/smspower-wiki/GGOfficialDocs.pmwiki` and the page scans in `gg-officialdocs-png.zip`), `reference/smspower/ggdoc.txt`, `msvdp-20021112.txt` section 14, `GGVDP.pmwiki`, `IOPortMapGG.pmwiki`. - The Z80, VDP, PSG and I/O are in one ASIC. The Z80 behaves as a **CMOS** part: undocumented `OUT (C),0` drives $FF, not $00 (forum thread 17116; the SMS Test Suite uses this to tell a GG from an SMS). z80-python 0.4.0, the release this board runs on, models NMOS only; its main branch has the option (`cmos_out_c_zero`), and the board already sets it for the Game Gear, so it takes effect with the next release. Until then the Game Gear here drives $00. - The VDP shows a 160×144 window of the 256×192 Mode 4 picture. CRAM is 64 bytes (12-bit colour, written as a byte pair). The write latches on the odd byte. - Extra ports $00–$06: Start button, Gear-to-Gear serial, stereo. In "SMS mode" (a Master System cartridge in a converter) the VDP scales the picture and behaves as an SMS VDP. - Sega's manual notes that at power-on the CPU can leave reset while the VDP is still in reset, and tells programs to wait for V counter = $B0 first. ## Module shape (as built) One concern per module, as in z80-python: `board.py` (the machines as data, the memory map and the port decode), `system.py` (the Z80 on a board: interrupts and where in an instruction a port access lands), `vdp.py` (TMS9918A modes 0-3 and Mode 4 in one class, the chip revisions as data), `cartridge.py` (the boards, and board detection from MAME's `hash/*.xml`), `io.py` (the controller ports, the Game Gear's ports, the 8255 and the SC-3000's keyboard), `sf7000.py` (the disk unit), `audio.py` (resampling; the PSG and FM chips are their own cores), `state.py` (save states and the rewind buffer built on them) and `frontend.py`, the only module that needs a library beyond the cores. A Mega Drive with a Power Base Converter is one more machine row: the 315-5313's Mode 4 differences are in undocumented-behavior.md and [timing.md](timing.md).