"""Observe original CPU experiments; no claim of an implemented BIOS ABI.""" import hashlib import json import subprocess from diagnostics import MPR, build_probe_images # API callees implemented by this firmware, with the contract measured against the # private reference BIOS. Only slot $1E (EX_GETVER) currently; everything else is a # nonreturning diagnostic stub. Values are what the guest must observe on return. # # P register: only a partial bitmask is asserted, never the whole byte. The emulator seeds the # initial CPU state from a PRNG, so P (in particular carry, overflow and decimal) differs between # runs and environments - the caller's carry was 1 in one cold run and 0 in another. The asserted # bits are Z=1 and N=0 (LDY #0 is the last flag-setting instruction) and I=1 (the caller's SEI, # which the callee does not clear). Carry is not claimed to hold a value; instead carry-in vs # carry-out equality across the JSR/RTS is verified on the real core. See FLAG_* below. IMPLEMENTED_SLOTS = {0x1E: {'a': 0x11, 'x': 3, 'y': 0, 'sp': 255}} FLAG_CARRY, FLAG_ZERO, FLAG_INTERRUPT, FLAG_NEGATIVE = 0x01, 0x02, 0x04, 0x80 def check_implemented_slot(records, symbols, slot, expected): """Assert the measured partial contract of an implemented API slot on real-core records. Registers A/X/Y/SP are compared to `expected` (IMPLEMENTED_SLOTS). P is checked as a bitmask only: Z=1, N=0, I=1, plus carry preserved from the caller's P at the same JSR checkpoint. V and D are not asserted, and the absolute P byte is never fixed. Returns nothing; raises ValueError on any deviation (used by the negative tests). """ if len(records['OBS']) < 4: # loaded/cold/nonreturning/warm; otherwise the loop below is vacuous raise ValueError('execution records are incomplete; the contract cannot be established') for row in records['OBS'][1:]: if (row['pc'], row['mpr']) != (symbols['api_call_return']+3, MPR): raise ValueError('implemented API did not return to the caller continuation') observed = {r: row[r] for r in expected} if observed != expected: raise ValueError(f'API ${slot:02X} returned {observed}, measured {expected}') if not row['p'] & FLAG_ZERO: raise ValueError(f'API ${slot:02X} returned with Z clear; the measured contract requires Z=1') if row['p'] & FLAG_NEGATIVE: raise ValueError(f'API ${slot:02X} returned with N set; the measured contract requires N=0') if not row['p'] & FLAG_INTERRUPT: raise ValueError(f'API ${slot:02X} returned with I clear; the measured contract requires I=1') caller_p = trace(records, row['cycle'], 'api_jsr')['p'] if (row['p'] ^ caller_p) & FLAG_CARRY: raise ValueError(f'API ${slot:02X} did not preserve carry across the call') if bytes.fromhex(row['work_ram_0200_hex'])[0x11] != expected['a']: raise ValueError('implemented API did not preserve the caller accumulator in RAM') for cycle in ('cold', 'warm'): if not any(r['cycle'] == cycle and r['phase'] == 'api_call_return' for r in records['TRACE']): raise ValueError('implemented API did not return to the caller') def execute(probe, rom, cue, labels, irq='none', media=True, steps=512): """cue '-' + media=False runs the no-media branch (#142): CD hardware on, no disc.""" result = subprocess.run([str(probe), str(rom), str(cue), str(steps), str(labels), irq], check=True, capture_output=True, text=True, timeout=30) records = {} for prefix in ('OBS', 'TRACE', 'CAP', 'BANK'): records[prefix] = [json.loads(line[len(prefix)+1:]) for line in result.stdout.splitlines() if line.startswith(prefix+' ')] if len(records['CAP']) != 1 or records['CAP'][0].get('instruction_step') is not True: raise ValueError('real instruction stepping capability missing') if records['CAP'][0].get('cdrom_hardware') is not True or records['CAP'][0].get('cdrom_media') is not media: raise ValueError('contract probe did not use System Card/CD path') if [r['phase'] for r in records['OBS']] != ['loaded', 'cold', 'nonreturning', 'warm']: raise ValueError('missing contract execution observations') records['rom_sha256'] = hashlib.sha256(rom.read_bytes()).hexdigest() return records def trace(records, cycle, phase): rows = [r for r in records['TRACE'] if r['cycle'] == cycle and r['phase'] == phase] if len(rows) != 1: raise ValueError(f'missing/duplicate actual {cycle} {phase} checkpoint') return rows[0] def check_terminal(records, pc, expected_ram, mpr=MPR): for row in records['OBS'][1:]: if row['pc'] != pc or row['mpr'] != mpr or row['sp'] != 255: raise ValueError('terminal PC/explicit MPR/stack mismatch') if bytes.fromhex(row['work_ram_0200_hex'])[:len(expected_ram)] != expected_ram: raise ValueError('real RAM checkpoint mismatch') def check_call(records, symbols): check_terminal(records, symbols['call_halt'], bytes((0x11, 0x22, 0x33, 0xff, 0x33))) for cycle in ('cold', 'warm'): before = trace(records, cycle, 'call_before') entry = trace(records, cycle, 'call_entry') after = trace(records, cycle, 'call_return') restore = trace(records, cycle, 'restore_mpr') if (before['a'], before['x'], before['y'], before['sp']) != (0x11,0x22,0x33,255): raise ValueError('explicit caller register inputs missing') if entry['sp'] != 253 or after['sp'] != 255: raise ValueError('JSR/RTS stack is not balanced') for register in ('a','x','y','p'): if before[register] != after[register]: raise ValueError(f'original procedure did not restore {register}') if before['mpr'] != after['mpr'] or after['mpr'] != MPR: raise ValueError('original procedure did not restore MPR') if restore['mpr'][2] != 3 or restore['a'] != 1: raise ValueError('temporary guest bank switch not observed') pushed = bytes.fromhex(entry['stack_01f8_hex']) if int.from_bytes(pushed[6:8], 'little') != symbols['call_before']+2: raise ValueError('actual JSR return address bytes mismatch') # source: (IRR bit, handler label, guest RAM marker, pre-interrupt trace phase at irq_wait) # The timer phase is 'timer_armed' because the harness injects nothing for it. IRQ_SOURCES = {'irq2': (1, 'irq_entry', 0x77, 'irq_asserted'), 'irq1': (2, 'irq_entry', 0x77, 'irq_asserted'), 'timer': (4, 'timer_entry', 0x7a, 'timer_armed')} def check_irq(records, symbols, irq): bit, handler, marker, armed = IRQ_SOURCES[irq] for cycle in ('cold','warm'): if any(r['cycle'] == cycle and r['phase'] == 'irq_unexpected' for r in records['TRACE']): raise ValueError('interrupt dispatched through an unexpected vector') for row in records['OBS'][1:]: if row['pc'] != symbols['irq_wait'] or row['sp'] != 255 or row['mpr'] != MPR: raise ValueError('IRQ fixture did not resume with balanced stack/mapping') if bytes.fromhex(row['work_ram_0200_hex'])[16] != marker: raise ValueError('IRQ guest RAM marker not observed') for cycle in ('cold','warm'): before = trace(records, cycle, armed) entry = trace(records, cycle, handler) after = trace(records, cycle, 'irq_resumed') if before['p'] & 4 or not entry['p'] & 4 or entry['sp'] != 252 or after['sp'] != 255: raise ValueError('IRQ/RTI P/SP transitions missing') if entry['irr'] & bit == 0: raise ValueError('requested actual interrupt source not observed') for register in ('a','x','y','p','mpr'): if before[register] != after[register]: raise ValueError(f'original IRQ procedure did not restore {register}') stack = bytes.fromhex(entry['stack_01f8_hex']) if stack[5] != before['p'] or int.from_bytes(stack[6:8], 'little') != symbols['irq_wait']: raise ValueError('actual interrupt stacked P/PC mismatch') def run_contracts(probe, base, output, cue): folder = output / 'contract-probes' images, symbols, labels = build_probe_images(base, folder) evidence = {'scope': 'original CPU/memory procedures plus the single implemented slot $1E callee; not a general BIOS ABI proof', 'cases': {}, 'negative_cases': [], 'loader_cases': [], 'conditions': {'requested_checkpoint_instructions':512, 'capability_instruction_before_cold':1, 'total_instructions_to_cold':513, 'total_instructions_to_warm':512, 'extra_instructions':32, 'irq_injection':'public AssertIRQ1/AssertIRQ2 at original irq_wait, deassert on handler entry', 'timer_source':'guest writes $0C00=0/$0C01=1; handler stops timer and acks $1403', 'unselected_vectors':'IRQ2/IRQ1/timer/NMI not under test point to nonreturning irq_unexpected', 'ram':'physical working RAM offsets 0200..023F and stack 01F8..01FF', 'explicit_guest_mpr':MPR}, 'nmi': {'status':'NOT_TESTED', 'reason':'pinned Geargrafx HuC6280 HandleIRQ dispatches only $FFFA/$FFF8/$FFF6; ' 'no NMI input or $FFFC dispatch exists, so NMI cannot be raised'}, 'symbols':symbols} for name, rom in images.items(): selected_labels = labels if name.startswith('api_jsr'): slot = int(name[-2:],16) selected_labels = folder/(name+'.trace-labels') selected_labels.write_text(labels.read_text()+f'api_slot_entry {0xe000+3*slot:04x}\n') records = execute(probe, rom, cue, selected_labels, name if name in IRQ_SOURCES else 'none') if name == 'bank': expected = bytes.fromhex('11 1f 22 2f a1 af 33 3f 5a a5 22 11 4c') + bytes([symbols['bank_probe'] >> 8]) check_terminal(records, symbols['bank_halt'], expected) for cycle in ('cold','warm'): if trace(records,cycle,'mirror_read')['mpr'][2] != 32: raise ValueError('physical bank20 mirror not observed') elif name.startswith('api_jsr'): slot = int(name[-2:],16) for cycle in ('cold','warm'): before = trace(records,cycle,'api_jsr'); entry = trace(records,cycle,'api_slot_entry') if (before['a'],before['x'],before['y'],before['sp']) != (0x11,0x22,0x33,255): raise ValueError('JSR slot caller register inputs missing') if (entry['a'],entry['x'],entry['y'],entry['sp']) != (0x11,0x22,0x33,253): raise ValueError('actual API ingress registers/stack mismatch') if slot in IMPLEMENTED_SLOTS: # Implemented callee: assert the partial contract measured against the private # reference BIOS (docs/abi-contracts.md). One slot, not a general ABI proof. check_implemented_slot(records, symbols, slot, IMPLEMENTED_SLOTS[slot]) else: address = int.from_bytes(base[3*slot+1:3*slot+3],'little') halt = int.from_bytes(base[address-0xe000+3:address-0xe000+5],'little') for row in records['OBS'][1:]: if (row['pc'],row['x'],row['sp']) != (halt,slot,253) or row['mpr'] != MPR: raise ValueError('unimplemented JSR slot returned or lost its diagnostic state') for cycle in ('cold','warm'): if any(r['cycle']==cycle and r['phase']=='api_call_return' for r in records['TRACE']): raise ValueError('unimplemented API falsely returned') elif name == 'ram': if records['CAP'][0]['card_ram_bytes'] != 196608: raise ValueError('Super CD RAM configuration not available') check_terminal(records, symbols['ram_halt'], bytes.fromhex('81 8f 91 9f 61 6f 71 7f'), [0xff,0xf8,0x80,0x87,0x68,0x7f,4,0]) elif name == 'call': check_call(records, symbols) else: check_irq(records, symbols, name) evidence['cases'][name] = records # Actual loader observations. Acceptance is explicitly not firmware validity. for name, data, expected in ( ('raw_payload', base, True), ('prefix_512', bytes(512)+base, True), ('all_ff_payload_not_firmware', b'\xff'*len(base), True), ('empty', b'', False), ('128k', base[:131072], False), ('one_byte_short', base[:-1], False), ('one_byte_extra', base+b'\xff', False), ('prefix_511', bytes(511)+base, False), ('512k', base+base, False)): path = folder / (name+'.loader-input'); path.write_bytes(data) result = subprocess.run([str(probe),str(path),'--load-only'], check=True, capture_output=True, text=True, timeout=30) rows = [json.loads(line[5:]) for line in result.stdout.splitlines() if line.startswith('LOAD ')] if len(rows) != 1 or rows[0] != {'accepted':expected, 'input_bytes':len(data), 'execution':'NOT_RUN'}: raise ValueError(f'actual loader result mismatch: {name}') evidence['loader_cases'].append({'case':name, 'sha256':hashlib.sha256(data).hexdigest(), **rows[0]}) path.unlink() # Negative original-procedure variants, never replacement BIOS APIs. for name, address, before, after in ( ('missing_rts', symbols['call_rts'], b'\x60', b'\x80\xfe'), ('wrong_mpr_restore', symbols['restore_mpr'], b'\x53\x04', b'\x53\x08')): data = bytearray(images['call'].read_bytes()); offset = address-0xe000 if data[offset:offset+len(before)] != before: raise ValueError('negative diagnostic instruction contract mismatch') data[offset:offset+len(after)] = after path = folder/(name+'-not-bios.pce'); path.write_bytes(data) records = execute(probe,path,cue,labels) if name == 'missing_rts': if any(r['pc'] != symbols['call_rts'] or r['sp'] != 253 for r in records['OBS'][1:]): raise ValueError('missing RTS negative did not reach its expected nonreturning state') else: for cycle in ('cold','warm'): row = trace(records,cycle,'call_return') if row['sp'] != 255 or row['mpr'][2:4] != [3,1]: raise ValueError('wrong MPR negative did not execute expected bad restore') try: check_call(records,symbols) except ValueError: evidence['negative_cases'].append({'case':name,'observations':records}) else: raise ValueError(f'negative CPU procedure unexpectedly passed: {name}') # Interrupt negatives: a wrong vector or a wrong return must fail the same check_irq. unexpected = symbols['irq_unexpected'].to_bytes(2, 'little') entry = symbols['irq_entry'].to_bytes(2, 'little') for name, irq, edits in ( ('irq1_vector_swapped_with_irq2', 'irq1', ((0x1ff8, entry, unexpected), (0x1ff6, unexpected, entry))), ('irq1_rts_instead_of_rti', 'irq1', ((symbols['irq_exit']-0xe000, b'\x40', b'\x60'),)), ('timer_rts_instead_of_rti', 'timer', ((symbols['timer_exit']-0xe000, b'\x40', b'\x60'),))): data = bytearray(images[irq].read_bytes()) for offset, before, after in edits: if data[offset:offset+len(before)] != before: raise ValueError(f'{name}: interrupt negative contract mismatch') data[offset:offset+len(after)] = after path = folder/(name+'-not-bios.pce'); path.write_bytes(data) records = execute(probe, path, cue, labels, irq) if name.startswith('irq1_vector') and any( (r['pc'], r['sp']) != (symbols['irq_unexpected'], 252) for r in records['OBS'][1:]): raise ValueError('swapped vector negative did not dispatch to irq_unexpected') # RTS pops stacked P as a return-address byte, runs into zero RAM and BRKs (pushed P has B=$10) # into the shared IRQ2/BRK trap with SP $FB. if name.endswith('rts_instead_of_rti') and any( (r['pc'], r['sp']) != (symbols['irq_unexpected'], 0xfb) or not bytes.fromhex(r['stack_01f8_hex'])[4] & 0x10 for r in records['OBS'][1:]): raise ValueError('RTS negative did not reach the trap through BRK with SP $FB') try: check_irq(records, symbols, irq) except ValueError as error: evidence['negative_cases'].append({'case':name, 'rejected_by':str(error), 'observations':records}) else: raise ValueError(f'interrupt negative unexpectedly passed: {name}') return evidence