# The contract with boards Everything a board needs, and nothing else. The names exported by `tms9918_python` are the whole public surface. ```python from tms9918_python import TMS9918, TMS9928A, TMS9929A, TMS9918A, VARIANTS, Frame vdp = TMS9918(TMS9928A, vram_size=16384) # 4096, 8192 or 16384: the DRAM fitted vdp.run(master_clocks) # advance; call it up to each port access vdp.write(mode, value) # MODE pin: 0 data, 1 control value = vdp.read(mode) # 0 data (the read-ahead byte), 1 status vdp.int_line # True while /INT is asserted (F and IE) vdp.line, vdp.dot # beam position, read-only frame = vdp.take_frame() # a Frame once per finished frame, else None vdp.reset() # /RESET ``` ## Time - One unit: the **master clock** (XTAL, 10.738635 MHz). A ColecoVision, SG-1000 or MSX1 Z80 runs at XTAL/3, so a board on the CPU's clock calls `run(cpu_cycles * 3)`. The pixel clock is XTAL/2, a memory cycle XTAL/4, a line 684 master clocks, a frame 262 lines (TMS9918A, TMS9928A) or 313 (TMS9929A). - `run(n)` then `run(m)` is the same as `run(n + m)` for any split. - Ports act at the core's current time: bring the core to the access's master clock, then call `read` or `write`. The CPU side (latch, read-ahead, flip-flop, status clear) happens at once; the VRAM side waits for an access window (`windows=True`, the default). - `line` is 0 for the first active line (0-191 active); `dot` counts pixel clocks 0-341 from the line's first name fetch (pixel x of the 256-pixel modes is shown at dot x + 10). - `clock` counts master clocks since power-on, to the start of the current memory cycle. ## Interrupt `int_line` is a level: True while F and IE are both 1. Setting IE while F is 1 raises it at once; a status read drops it. A board whose CPU wants an edge (the ColecoVision's NMI) takes the rising edge of `int_line`; a board with a level-sensitive IRQ (SG-1000, MSX) wires it as is. Poll it after `run` and after every port access (register 1 writes and status reads change it). ## Frames `take_frame()` returns the frame that finished most recently, once (it is finished when line 0 of the next frame begins). `Frame.pixels` is 192 `bytes` rows of 256 colour indices, each resolved through the planes (sprite, else pattern colour, else backdrop; index 0 only where every plane is transparent and the backdrop is 0). Text mode's 240 pixels sit 6 pixels in. `Frame.border` is the backdrop index of each line (262 or 313 entries, line 0 first), sampled as the line began. `Frame.number` counts frames from power-on. RGB is the board's: TI's levels are in `LEVELS_9918A` and `LEVELS_9928A` (table 2-3) as information. ## Construction keywords | Keyword | Default | What | | --- | --- | --- | | `windows` | True | CPU accesses wait for access windows; False serves them at once (MAME's model) | | `lost_access` | `"newer"` | what a second access does while one waits (U3): `"newer"`, `"restart"`, `"older"` | | `roll_over_4k` | True | the address counter wraps at 4K in 4K mode (C11) | | `mux_4k` | True | 4K mode's DRAM row/column mapping (C11) | | `offscreen_collisions` | True | sprite pixels outside columns 0-255 collide (C7) | | `flag_dot` | `(191, 266)` | where F sets (C10); MAME's point is `(193, 10)` | | `status_race` | False | the status-read race (U8), window `race_window` master clocks | | `power_on_vram` | zeros | the DRAM at power-on (U4) | | `power_on_status` | 0 | the status register at power-on (U4) | Defaults and their evidence: [claims.md](claims.md). ## Variants `VDPVariant` is a frozen dataclass; the rows are `TMS9918A`, `TMS9928A`, `TMS9929A`, and `VARIANTS` maps names to them. A board can build another row with `dataclasses.replace` (for example a TMS9128: `has_4k_select=False, colour_mask_masks_patterns=False`). Fields: `name`, `layout` (a `VerticalLayout`), `graphics2`, `has_4k_select`, `colour_mask_masks_patterns`, `output`, `levels`, `pin38`, `access_delay`. ## Inspection (not part of the contract, but stable for tests and tools) `vram` (bytearray, indexed by DRAM cell; in 16K mode the cell is the address), `regs` (list of 8), `status`, `addr`, `read_ahead`, `lost_accesses` (CPU accesses whose memory cycle never happened), `frame_count`, `lines`. ## For colecovision-python Rows `TMS9928A` (NTSC) and `TMS9929A` (PAL); `run(cycles * 3)`; the NMI is the rising edge of `int_line`; the core inserts no wait states (the chip has none; the M1 and READY waits are the board's). The BIOS writes VRAM before it sets R1 bit 7, i.e. in 4K mode: with the defaults (C11) those writes land in MacDonald's 4K-mode cells and the counter rolls over at 4K. Set `roll_over_4k=False, mux_4k=False` to reproduce MAME.