# SPDX-License-Identifier: MIT """Field extraction and the field tables, shared by the core and the disassembler. Every instruction is one 24-bit word (DS section 3, "Instructions"; fullsnes "Opcode Encoding"):: 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 RT P-SEL ALU (4) ASL DPL DPHM (4) RPDCR SRC (4) DST (4) OP / RT 1 0 BRCH (9) NA (11) BANK(2) JP 1 1 ID (16) - - DST (4) LD The names are the data sheet's (DS p.18-24). """ from __future__ import annotations # Instruction types: bits 23-22 (DS p.18 "ALL INSTRUCTIONS"). OP, RT, JP, LD = 0, 1, 2, 3 def kind(word: int) -> int: """Bits 23-22: OP, RT, JP or LD.""" return word >> 22 & 3 def p_select(word: int) -> int: """OP/RT bits 21-20: the ALU's P input (DS p.18).""" return word >> 20 & 3 def alu(word: int) -> int: """OP/RT bits 19-16: the ALU operation (DS p.19).""" return word >> 16 & 0xF def asl(word: int) -> int: """OP/RT bit 15: 0 = AccA and FLAG A, 1 = AccB and FLAG B (DS p.19).""" return word >> 15 & 1 def dpl(word: int) -> int: """OP/RT bits 14-13: DPNOP, DPINC, DPDEC, DPCLR on DP bits 3-0 (DS p.19).""" return word >> 13 & 3 def dphm(word: int) -> int: """OP/RT bits 12-9: the value XORed into DP bits 7-4 (DS p.20).""" return word >> 9 & 0xF def rpdcr(word: int) -> int: """OP/RT bit 8: RPDEC (DS p.20).""" return word >> 8 & 1 def src(word: int) -> int: """OP/RT bits 7-4: the move's source (DS p.21).""" return word >> 4 & 0xF def dst(word: int) -> int: """OP/RT/LD bits 3-0: the move's destination (DS p.22).""" return word & 0xF def brch(word: int) -> int: """JP bits 21-13: the branch code (DS p.23).""" return word >> 13 & 0x1FF def na(word: int) -> int: """JP bits 12-2: the next address (DS p.24).""" return word >> 2 & 0x7FF def bank(word: int) -> int: """JP bits 1-0: reserved on the uPD7725, NA bits 12-11 on the uPD96050 (fullsnes; U11).""" return word & 3 def immediate(word: int) -> int: """LD bits 21-6: the 16-bit immediate (DS p.24).""" return word >> 6 & 0xFFFF P_SELECT = ("RAM", "IDB", "M", "N") ALU = ( "NOP", "OR", "AND", "XOR", "SUB", "ADD", "SBB", "ADC", "DEC", "INC", "CMP", "SHR1", "SHL1", "SHL2", "SHL4", "XCHG", ) ACC = ("ACCA", "ACCB") DPL = ("DPNOP", "DPINC", "DPDEC", "DPCLR") SRC = ( "NON", # DS note 1: "The contents of the TRB register are also output if NON is specified" "A", "B", "TR", "DP", "RP", "RO", "SGN", "DR", "DRNF", "SR", "SIM", "SIL", "K", "L", "MEM", ) DST = ( "@NON", "@A", "@B", "@TR", "@DP", "@RP", "@DR", "@SR", "@SOL", "@SOM", "@K", "@KLR", "@KLM", "@L", "@TRB", "@MEM", ) # The ALU codes that read P (DS p.19): OR ... ADC. DEC, INC and the shifts ignore it. BINARY_ALU = frozenset(range(1, 8)) # SRC and DST codes the core names. SRC_TRB, SRC_A, SRC_B, SRC_TR, SRC_DP, SRC_RP, SRC_RO, SRC_SGN = range(8) SRC_DR, SRC_DRNF, SRC_SR, SRC_SIM, SRC_SIL, SRC_K, SRC_L, SRC_MEM = range(8, 16) DST_NON, DST_A, DST_B, DST_TR, DST_DP, DST_RP, DST_DR, DST_SR = range(8) DST_SOL, DST_SOM, DST_K, DST_KLR, DST_KLM, DST_L, DST_TRB, DST_MEM = range(8, 16) # BRCH codes (DS p.23; the uPD96050's 000h, 101h, 141h from fullsnes "JP Instructions"). JMPSO, JMP, HJMP, CALL, HCALL = 0x000, 0x100, 0x101, 0x140, 0x141 BRCH = { 0x000: "JMPSO", 0x100: "JMP", 0x101: "HJMP", 0x140: "CALL", 0x141: "HCALL", 0x080: "JNCA", 0x082: "JCA", 0x084: "JNCB", 0x086: "JCB", 0x088: "JNZA", 0x08A: "JZA", 0x08C: "JNZB", 0x08E: "JZB", 0x090: "JNOVA0", 0x092: "JOVA0", 0x094: "JNOVB0", 0x096: "JOVB0", 0x098: "JNOVA1", 0x09A: "JOVA1", 0x09C: "JNOVB1", 0x09E: "JOVB1", 0x0A0: "JNSA0", 0x0A2: "JSA0", 0x0A4: "JNSB0", 0x0A6: "JSB0", 0x0A8: "JNSA1", 0x0AA: "JSA1", 0x0AC: "JNSB1", 0x0AE: "JSB1", 0x0B0: "JDPL0", 0x0B1: "JDPLN0", 0x0B2: "JDPLF", 0x0B3: "JDPLNF", 0x0B4: "JNSIAK", 0x0B6: "JSIAK", 0x0B8: "JNSOAK", 0x0BA: "JSOAK", 0x0BC: "JNRQM", 0x0BE: "JRQM", } UNCONDITIONAL = frozenset((JMPSO, JMP, HJMP, CALL, HCALL)) __all__ = [ "ACC", "ALU", "BINARY_ALU", "BRCH", "CALL", "DPL", "DST", "HCALL", "HJMP", "JMP", "JMPSO", "JP", "LD", "OP", "P_SELECT", "RT", "SRC", "UNCONDITIONAL", "alu", "asl", "bank", "brch", "dphm", "dpl", "dst", "immediate", "kind", "na", "p_select", "rpdcr", "src", ]