"""Write player32x/c1/c1_tables.h from r32x/c1.py, so the C decoder's constants are the reference decoder's. py -3.12 -m r32x.gen_tables """ from pathlib import Path from . import c1 OUT = Path(__file__).resolve().parents[1] / "player32x" / "c1" / "c1_tables.h" def arr(name: str, ctype: str, values) -> str: vals = [int(v) for v in values] body = ",\n".join(" " + ", ".join(str(v) for v in vals[i : i + 16]) for i in range(0, len(vals), 16)) return f"static {ctype} {name}[{len(vals)}] = {{\n{body}\n}};\n" def main() -> None: parts = [ "/* Generated by r32x/gen_tables.py from r32x/c1.py. Do not edit. Not const: on the SH-2 they live in .data (SDRAM), not the cartridge. */\n", "#ifndef C1_TABLES_H\n#define C1_TABLES_H\n", arr("C1_K", "int16_t", c1.K.reshape(-1)), # K[k*8 + n] arr("C1_MINTRA", "uint8_t", c1.M_INTRA.reshape(-1)), arr("C1_ZIGZAG", "uint8_t", c1.ZIGZAG), arr("C1_CL5", "uint8_t", c1.CL5), # index v + 256: clip to 0-255, then 5 bits "".join(f"#define C1_{k} ({getattr(c1, k)})\n" for k in ("K_RCR", "K_GCB", "K_GCR", "K_BCB")), arr("C1_LINE_ROW", "uint8_t", c1.LINE_ROW), "#endif\n", ] OUT.parent.mkdir(parents=True, exist_ok=True) OUT.write_text("\n".join(parts), encoding="utf-8", newline="\n") print(OUT) # The butterfly IDCT's constants (x16) for c1asm.s. K = c1.K sym = {"K0_16": K[0, 0], "KE0_16": K[2, 0], "KE1_16": K[6, 0], "KE2_16": K[2, 1], "KE3_16": K[6, 1]} for k in range(4): for c in range(4): sym[f"KO{k}{c}_16"] = K[2 * k + 1, c] asm = ["! Generated by r32x/gen_tables.py from r32x/c1.py. Do not edit."] asm += [f"\t.set\t{name}, {16 * int(v)}" for name, v in sym.items()] out_asm = OUT.with_name("c1_k16.inc") out_asm.write_text("\n".join(asm) + "\n", encoding="utf-8", newline="\n") print(out_asm) if __name__ == "__main__": main()