#!/usr/bin/env python3 """Write src/opcodes.rs from the reference's generated ``sm83_python._opcodes``. The reference generates its table from GBCTR's opcodes.toml and gb-opcodes' lengths (its scripts/gen_opcode_table.py); this transcribes that table as it stands at the pin, so the two can never drift. Run it with the reference's interpreter: external/sm83-python/.venv/bin/python scripts/gen_opcodes.py > src/opcodes.rs """ from __future__ import annotations import json from sm83_python._opcodes import CB_OPCODES, OPCODES def rows(table) -> str: return "".join( f" Opcode::new({json.dumps(e.mnemonic)}, {e.length}, {json.dumps(e.category)}),\n" for e in table ) print( f"""//! The 512 SM83 opcodes: mnemonic, byte length and GBCTR timing category (`_opcodes.py`). //! //! Generated by scripts/gen_opcodes.py from the reference's generated table (itself from //! GBCTR's opcodes.toml and gbdev/gb-opcodes' Opcodes.json, cross-checked). Do not edit; //! regenerate. The handlers are hand-written in the instruction modules. /// One opcode: GBCTR's mnemonic text, length in bytes, and timing category. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Opcode {{ pub mnemonic: &'static str, pub length: u8, pub category: &'static str, }} impl Opcode {{ const fn new(mnemonic: &'static str, length: u8, category: &'static str) -> Self {{ Opcode {{ mnemonic, length, category, }} }} }} /// Unprefixed opcodes, indexed by opcode byte; holes are `DB $XX`. pub const OPCODES: [Opcode; 256] = [ {rows(OPCODES)}]; /// Opcodes after the $CB prefix, indexed by the second byte. pub const CB_OPCODES: [Opcode; 256] = [ {rows(CB_OPCODES)}];""")