from collections import defaultdict from typing import Dict from gbso.regs import R16_HI, R16_LO, R8, R16 class CPU: carry: int cycles: int reg8: Dict[R8, int] memory: bytearray def __init__(self) -> None: self.carry = 0 self.cycles = 0 self.reg8 = defaultdict(lambda: 0) self.memory = bytearray(0xFFFF + 1) def get_reg8(self, r: R8) -> int: return self.reg8[r] def get_reg16(self, rr: R16) -> int: return (self.reg8[R16_HI[rr]] << 8) | self.reg8[R16_LO[rr]] def set_reg8(self, r: R8, n: int) -> None: self.reg8[r] = n & 0xFF def set_reg16(self, rr: R16, nn: int) -> None: self.reg8[R16_HI[rr]] = (nn >> 8) & 0xFF self.reg8[R16_LO[rr]] = nn & 0xFF def get_mem8(self, nn: int) -> int: return self.memory[nn & 0xFFFF] def get_mem16(self, nn: int) -> int: return (self.memory[(nn + 1) & 0xFF] << 8) | self.memory[nn & 0xFFFF] def set_mem8(self, nn: int, n: int) -> None: self.memory[nn & 0xFFFF] = n & 0xFF def set_mem16(self, nn: int, nn1: int) -> None: self.memory[nn & 0xFFFF] = nn1 & 0xFF self.memory[(nn + 1) & 0xFFFF] = (nn1 >> 8) & 0xFF def deref_hl(self) -> int: return self.get_mem8(self.get_reg16(R16.HL)) def deref_hl_set(self, n: int) -> None: self.set_mem8(self.get_reg16(R16.HL), n)