# SPDX-License-Identifier: MIT """A tiny assembler for the tests: each function returns the bytes of one instruction.""" from __future__ import annotations STOP = [0x00] NOP = [0x01] CACHE = [0x02] LSR = [0x03] ROL = [0x04] LOOP = [0x3C] ALT1 = [0x3D] ALT2 = [0x3E] ALT3 = [0x3F] PLOT = [0x4C] RPIX = ALT1 + [0x4C] SWAP = [0x4D] COLOR = [0x4E] CMODE = ALT1 + [0x4E] NOT = [0x4F] MERGE = [0x70] SBK = [0x90] SEX = [0x95] ASR = [0x96] DIV2 = ALT1 + [0x96] ROR = [0x97] LOB = [0x9E] FMULT = [0x9F] LMULT = ALT1 + [0x9F] HIB = [0xC0] GETC = [0xDF] RAMB = ALT2 + [0xDF] ROMB = ALT3 + [0xDF] GETB = [0xEF] GETBH = ALT1 + [0xEF] GETBL = ALT2 + [0xEF] GETBS = ALT3 + [0xEF] BRANCH = {"BRA": 5, "BGE": 6, "BLT": 7, "BNE": 8, "BEQ": 9, "BPL": 0xA, "BMI": 0xB, "BCC": 0xC, "BCS": 0xD, "BVC": 0xE, "BVS": 0xF} # fmt: skip def to(n: int) -> list[int]: return [0x10 | n] def with_(n: int) -> list[int]: return [0x20 | n] def from_(n: int) -> list[int]: return [0xB0 | n] def move(d: int, s: int) -> list[int]: return with_(s) + to(d) def moves(d: int, s: int) -> list[int]: return with_(d) + from_(s) def iwt(n: int, value: int) -> list[int]: return [0xF0 | n, value & 0xFF, value >> 8 & 0xFF] def ibt(n: int, value: int) -> list[int]: return [0xA0 | n, value & 0xFF] def lm(n: int, address: int) -> list[int]: return ALT1 + [0xF0 | n, address & 0xFF, address >> 8] def sm(address: int, n: int) -> list[int]: return ALT2 + [0xF0 | n, address & 0xFF, address >> 8] def lms(n: int, address: int) -> list[int]: return ALT1 + [0xA0 | n, address >> 1] def sms(address: int, n: int) -> list[int]: return ALT2 + [0xA0 | n, address >> 1] def stw(n: int) -> list[int]: return [0x30 | n] def stb(n: int) -> list[int]: return ALT1 + [0x30 | n] def ldw(n: int) -> list[int]: return [0x40 | n] def ldb(n: int) -> list[int]: return ALT1 + [0x40 | n] def add(n: int) -> list[int]: return [0x50 | n] def adc(n: int) -> list[int]: return ALT1 + [0x50 | n] def add_i(n: int) -> list[int]: return ALT2 + [0x50 | n] def adc_i(n: int) -> list[int]: return ALT3 + [0x50 | n] def sub(n: int) -> list[int]: return [0x60 | n] def sbc(n: int) -> list[int]: return ALT1 + [0x60 | n] def sub_i(n: int) -> list[int]: return ALT2 + [0x60 | n] def cmp(n: int) -> list[int]: return ALT3 + [0x60 | n] def and_(n: int) -> list[int]: return [0x70 | n] def bic(n: int) -> list[int]: return ALT1 + [0x70 | n] def and_i(n: int) -> list[int]: return ALT2 + [0x70 | n] def bic_i(n: int) -> list[int]: return ALT3 + [0x70 | n] def or_(n: int) -> list[int]: return [0xC0 | n] def xor(n: int) -> list[int]: return ALT1 + [0xC0 | n] def or_i(n: int) -> list[int]: return ALT2 + [0xC0 | n] def xor_i(n: int) -> list[int]: return ALT3 + [0xC0 | n] def mult(n: int) -> list[int]: return [0x80 | n] def umult(n: int) -> list[int]: return ALT1 + [0x80 | n] def mult_i(n: int) -> list[int]: return ALT2 + [0x80 | n] def umult_i(n: int) -> list[int]: return ALT3 + [0x80 | n] def inc(n: int) -> list[int]: return [0xD0 | n] def dec(n: int) -> list[int]: return [0xE0 | n] def jmp(n: int) -> list[int]: return [0x90 | n] def ljmp(n: int) -> list[int]: return ALT1 + [0x90 | n] def link(n: int) -> list[int]: return [0x90 | n] def branch(name: str, displacement: int) -> list[int]: return [BRANCH[name], displacement & 0xFF] def program(*parts: list[int]) -> list[int]: out: list[int] = [] for part in parts: out.extend(part) return out