# The API The whole public surface is the names in `ym2612_python.__all__`. megadrive-python uses it. ```python from ym2612_python import YM2612, YM2612_DISCRETE, YM3438_ASIC, FMVariant, CLOCKS_PER_SAMPLE fm = YM2612(YM2612_DISCRETE) # or YM3438_ASIC; the default is the discrete chip fm.write(port, value) # ports 0/2: address for part I/II; 1/3: data status = fm.read(port) # the status byte, or None where the part leaves the bus undriven fm.run(clocks) # advance by clocks of the chip's clock pin frames = fm.take_samples() # [(left, right), ...], one per 144 clocks fm.irq # the /IRQ pin: True while either timer flag is set fm.reset_line(level) # the /IC pin: 0 holds the chip in reset, 1 releases it fm.enabled = False # the board's switch: silent frames, timers still run fm.channel_values() # the six signed 9-bit values of the last sample (validation) ``` ## Time - `CLOCKS_PER_SAMPLE = 144`: clocks of the chip's pin per output sample (spec 4.2). On a Mega Drive the pin runs at the master clock divided by 7. - `run(a); run(b)` equals `run(a + b)`: the remainder is kept. - A write changes the register at once and is heard from the next sample (PROVISIONAL, T9; when inside a sample the chip takes a write is unknown, U1). - `WAIT_AFTER_ADDRESS` (17), `WAIT_AFTER_OPERATOR_DATA` (83), `WAIT_AFTER_CHANNEL_DATA` (47): Yamaha's waits in pin clocks. Not enforced; there for a board that wants to warn. - `BUSY_CLOCKS` (192): how long status reads BUSY after a data write. ## Output `take_samples()` returns, per sample, each side as the sum of the six channel slots in steps of the 9-bit converter, after the variant's output stage: within -1,554 to +1,554 (T11). On `YM2612_DISCRETE` the output stage adds the "ladder" offsets (PROVISIONAL constants, T10); on `YM3438_ASIC` it adds nothing. The board scales, filters and mixes with the PSG. ## Variants `YM2612_DISCRETE` and `YM3438_ASIC` are frozen `FMVariant` records of plain values: whether the output stage has the ladder and its three constants, and which addresses return status (T3). No logic asks which variant it is running. A new one is `dataclasses.replace(...)`. ## State All state is plain attributes (integers, lists, small records), no record with 80 or more attributes, nothing callable: a board's save-state walker can copy it out and back. `tests/test_contract.py` checks this. ## Speed About 9 times real time on PyPy 3.11 with six channels sounding. ## For a board - `run()` takes clocks of the chip's pin, which on a Mega Drive is the master clock divided by 7. The board keeps its own clock; the core keeps only the remainder. - Where `read()` returns `None` the board supplies its own open-bus value. - The output is integer converter steps with the ladder applied on the discrete chip. The FM-to-PSG balance is the board's choice (spec 15; community test `test_psg_balance_is_the_boards_business`). - Writes are taken at sample boundaries (T9). A program that writes faster than Yamaha allows is heard up to a sample differently from the chip (U1). - The discrete chip's ladder constants are provisional until a measurement exists (validation.md, rung 7). The withdrawn core under this name had clock-level timing entries (`IC_SLOT_LAG`, `IC_WRITE_SLOT_LAG`, `TIMER_CONTROL_DELAY_CYCLES`, `mclk`, `control_delay`, `busy_on_all_ports`). This core has none of them: there is no timing inside a sample, and which addresses return status is the variant's `status_ports`.