#!/usr/bin/env python3 # SPDX-License-Identifier: MIT # Copyright (c) 2026 Miasmark and contributors # Writes sim/tests/cpu/branches.asm (committed) to stdout: # python3 sim/tests/cpu/gen_branches.py > sim/tests/cpu/branches.asm # Truth table written from S1 p.54 (and wiki Branch): # B always, BC C, BOV OV, BPL !S, BEQ Z, BLT S^OV, BLE Z|(S^OV), BUSC S^C; # codes 8-15 are the negations (NOPP never, BNC, BNOV, BMI, BNEQ, BGE, BGT, BESC). import sys names = ["B", "BC", "BOV", "BPL", "BEQ", "BLT", "BLE", "BUSC", "NOPP", "BNC", "BNOV", "BMI", "BNEQ", "BGE", "BGT", "BESC"] def true_(c, S, Z, O, C): base = [True, C, O, not S, Z, S != O, Z or (S != O), S != C][c & 7] return bool(base) != bool(c & 8) out = ["; Branch conditions: every condition code against every S Z O C value.", "; Truth table from S1 p.54 (and wiki Branch). Generated by", "; gen_branches.py > branches.asm. A wrong decision ends at 'fail' with", "; the case number in R1. Even cases branch forward, odd ones backward.", "; BEXT reads the external conditions set by ';! bext'.", ";! bext 5 10", ";! expect end: R0=0 R1=1000", " ORG $5000", "start: CLRR R0"] n = 0 for c in range(16): for f in range(16): S, Z, O, C = (f >> 3) & 1, (f >> 2) & 1, (f >> 1) & 1, f & 1 t = true_(c, S, Z, O, C) nm = names[c] out.append("; case %d: %s with SZOC=%d%d%d%d -> %s" % (n, nm, S, Z, O, C, "taken" if t else "not taken")) if nm == "NOPP": # NOPP never branches; its displacement word is ignored out += [" MVII #%d, R1" % n, " MVII #$%02X, R3" % (f << 4), " RSWD R3", " DECLE $%03X, 0" % (0x208 | (0x20 if n & 1 else 0)), " B k%d" % n, " B fail"] out.append("k%d:" % n) elif not t: out += [" MVII #%d, R1" % n, " MVII #$%02X, R3" % (f << 4), " RSWD R3", " %-7s fail" % nm] elif n % 2 == 0: out += [" MVII #%d, R1" % n, " MVII #$%02X, R3" % (f << 4), " RSWD R3", " %-7s k%d" % (nm, n), " B fail", "k%d:" % n] else: out += [" B f%d" % n, "b%d: B k%d" % (n, n), "f%d: MVII #%d, R1" % (n, n), " MVII #$%02X, R3" % (f << 4), " RSWD R3", " %-7s b%d" % (nm, n), " B fail", "k%d:" % n] n += 1 out += ["; BEXT: conditions 5 and 10 read true, 4 reads false", " MVII #1000, R1", " BEXT e1, 5", " B fail", "e1: BEXT fail, 4", " B e3", "e2: B done", "e3: BEXT e2, 10", "fail: MVII #$DEAD, R0", "done: NOP"] sys.stdout.write("\n".join(out) + "\n")