| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
import struct |
| 32 |
|
| 33 |
MAGIC = b"X16P" |
| 34 |
VERSION = 2 |
| 35 |
|
| 36 |
CPU_65C02 = 0 |
| 37 |
CPU_65816 = 1 |
| 38 |
|
| 39 |
F_VDA, F_VPA, F_VPB, F_WRITE = 0x01, 0x02, 0x04, 0x08 |
| 40 |
F_E, F_M, F_X, F_MLB = 0x10, 0x20, 0x40, 0x80 |
| 41 |
|
| 42 |
|
| 43 |
def pack_state(st, emulation_defaults): |
| 44 |
e = st.get("e", 1 if emulation_defaults else 0) |
| 45 |
return struct.pack( |
| 46 |
"<HHHHHBBBBH", |
| 47 |
st["pc"] & 0xFFFF, |
| 48 |
st["s"] & 0xFFFF, |
| 49 |
st["a"] & 0xFFFF, |
| 50 |
st["x"] & 0xFFFF, |
| 51 |
st["y"] & 0xFFFF, |
| 52 |
st["p"] & 0xFF, |
| 53 |
e & 0xFF, |
| 54 |
st.get("dbr", 0) & 0xFF, |
| 55 |
st.get("pbr", 0) & 0xFF, |
| 56 |
st.get("d", 0) & 0xFFFF, |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def pack_ram(ram): |
| 61 |
out = struct.pack("<H", len(ram)) |
| 62 |
for addr, val in ram: |
| 63 |
out += struct.pack("<IB", (addr or 0) & 0xFFFFFF, (val or 0) & 0xFF) |
| 64 |
return out |
| 65 |
|
| 66 |
|
| 67 |
def pack_cycle_flags(word): |
| 68 |
|
| 69 |
if word in ("read", "write"): |
| 70 |
flags = F_VDA | F_VPA |
| 71 |
if word == "write": |
| 72 |
flags |= F_WRITE |
| 73 |
return flags |
| 74 |
|
| 75 |
bits = (F_VDA, F_VPA, F_VPB, 0, F_E, F_M, F_X, F_MLB) |
| 76 |
flags = 0 |
| 77 |
for i, bit in enumerate(bits): |
| 78 |
if word[i] != "-": |
| 79 |
flags |= bit |
| 80 |
if word[3] == "w": |
| 81 |
flags |= F_WRITE |
| 82 |
return flags |
| 83 |
|
| 84 |
|
| 85 |
def pack_cycles(cycles): |
| 86 |
out = struct.pack("<H", len(cycles)) |
| 87 |
for entry in cycles: |
| 88 |
addr, val, word = entry[0], entry[1], entry[2] |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
out += struct.pack("<IBB", (addr or 0) & 0xFFFFFF, (val or 0) & 0xFF, |
| 93 |
pack_cycle_flags(word)) |
| 94 |
return out |
| 95 |
|
| 96 |
|
| 97 |
def pack_case(opcode, cpu, case): |
| 98 |
emulation_defaults = (cpu == CPU_65C02) |
| 99 |
return ( |
| 100 |
struct.pack("<BB", opcode, cpu) |
| 101 |
+ pack_state(case["initial"], emulation_defaults) |
| 102 |
+ pack_ram(case["initial"]["ram"]) |
| 103 |
+ pack_state(case["final"], emulation_defaults) |
| 104 |
+ pack_ram(case["final"]["ram"]) |
| 105 |
+ pack_cycles(case.get("cycles", [])) |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
def write_fixture(path, entries): |
| 110 |
|
| 111 |
blobs = [pack_case(op, cpu, c) for op, cpu, c in entries] |
| 112 |
with open(path, "wb") as fh: |
| 113 |
fh.write(MAGIC + struct.pack("<II", VERSION, len(blobs))) |
| 114 |
for b in blobs: |
| 115 |
fh.write(b) |
| 116 |
return len(blobs) |