# The API The whole public surface is the names in `ym2612_python.__all__`. Written for megadrive-python; [adoption.md](adoption.md) lists what the board must change. ```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.