/* SPDX-License-Identifier: MIT */ /* First published under MIT by alewman in ym2151-python (validation/opm_ref.c); the MIT permission is kept * (notices/MIT-emulator-family.txt). Built with Nuked-OPM, LGPL-2.1-or-later, the binary is a * combined work under that licence; this repository is GPL-3.0-or-later. * Regrouped into tools/nuked on 2026-11-23 (README.md there). */ /* A headless harness around Nuked-OPM (reference/src/Nuked-OPM, LGPL-2.1, pinned at * f209e6e), built by validation/build_referee.sh and run as a referee. Nothing of * Nuked-OPM is copied into ym2151-python: this file only calls the public * functions declared in opm.h. The model's .c is compiled, never opened. * * opm_ref [stride] * * With `stride` (default 1), only the record after every stride-th cycle is * written (cycles stride-1, 2*stride-1, ...): stride 32 gives one record per * output sample, for long measurements (scripts/referee_freq.py). * * events.bin: little-endian records of 10 bytes, sorted by time: * u64 cycle; u8 kind; u8 data; * `cycle` counts calls of OPM_Clock since the end of OPM_Reset. One call is one * internal cycle, which is two input clocks (the chip halves its clock); 32 cycles * are one output sample. An event whose cycle equals the next call's index is * applied before that call. * kind 0: write `data` with A0 = 0 (the address) * kind 1: write `data` with A0 = 1 (the data) * kind 2: drive /IC: data 1 asserts it (pin low), data 0 releases it * * out.bin: one record of 6 bytes per cycle: * u8 pins; bit 0 SO, bit 1 SH1, bit 2 SH2, bit 3 IRQ (1 = asserted), * bit 4 CT1, bit 5 CT2 (as opm.h's OPM_ReadCT1/2 name them) * u8 status; what a read with A0 = 1 returns after this cycle (bit 7 BUSY, * bit 1 timer B, bit 0 timer A); the read has no side effect * i16 left; Nuked-OPM's own model of the YM3012 after this cycle * i16 right; * SO, SH1 and SH2 are the chip's pins and the contract. `left` and `right` are a * decoded convenience: mantissa and exponent turned back into a number. */ #include #include #include #include #include "opm.h" static uint64_t le64(const unsigned char *p) { uint64_t v = 0; for (int b = 7; b >= 0; b--) v = (v << 8) | p[b]; return v; } int main(int argc, char **argv) { if (argc != 5 && argc != 6) { fprintf(stderr, "usage: %s events.bin out.bin cycles ym2151|ym2164 [stride]\n", argv[0]); return 2; } uint64_t cycles = strtoull(argv[3], NULL, 10); uint64_t stride = argc == 6 ? strtoull(argv[5], NULL, 10) : 1; if (stride == 0) stride = 1; uint32_t flags = strcmp(argv[4], "ym2164") == 0 ? opm_flags_ym2164 : opm_flags_none; FILE *in = fopen(argv[1], "rb"); if (!in) { perror(argv[1]); return 1; } FILE *out = fopen(argv[2], "wb"); if (!out) { perror(argv[2]); return 1; } setvbuf(out, NULL, _IOFBF, 1 << 20); static opm_t chip; OPM_Reset(&chip, flags); unsigned char rec[10]; int have = fread(rec, 1, 10, in) == 10; for (uint64_t cycle = 0; cycle < cycles; cycle++) { while (have && le64(rec) <= cycle) { if (rec[8] == 2) OPM_SetIC(&chip, rec[9]); else OPM_Write(&chip, rec[8], rec[9]); have = fread(rec, 1, 10, in) == 10; } int32_t dac[2]; uint8_t sh1, sh2, so; OPM_Clock(&chip, dac, &sh1, &sh2, &so); unsigned char o[6]; o[0] = (unsigned char)(so | (sh1 << 1) | (sh2 << 2) | (OPM_ReadIRQ(&chip) << 3) | (OPM_ReadCT1(&chip) << 4) | (OPM_ReadCT2(&chip) << 5)); o[1] = OPM_Read(&chip, 1); int16_t l = (int16_t)dac[0], r = (int16_t)dac[1]; o[2] = (unsigned char)(l & 255); o[3] = (unsigned char)((l >> 8) & 255); o[4] = (unsigned char)(r & 255); o[5] = (unsigned char)((r >> 8) & 255); if ((cycle + 1) % stride == 0) fwrite(o, 1, 6, out); } fclose(out); fclose(in); return 0; }