gameboy superoptimizer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
877 B

  1. from collections import defaultdict
  2. from dataclasses import dataclass, field
  3. from typing import Dict, List, Tuple, Union
  4. from gbso.cpu.regs import R8
  5. Loc = Union[R8, int]
  6. @dataclass
  7. class CPUState:
  8. carry: int = 0
  9. sp: int = 0
  10. reg8: Dict[R8, int] = field(default_factory=lambda: defaultdict(lambda: 0))
  11. memory: Dict[int, int] = field(default_factory=lambda: defaultdict(lambda: 0))
  12. def copy(self) -> "CPUState":
  13. return CPUState(
  14. carry=self.carry,
  15. sp=self.sp,
  16. reg8=self.reg8.copy(),
  17. memory=self.memory.copy(),
  18. )
  19. def with_vals(self, *init_vals: List[Tuple[Loc, int]]) -> "CPUState":
  20. cpu = self.copy()
  21. for loc, val in init_vals:
  22. if type(loc) == R8:
  23. cpu.reg8[loc] = val
  24. else:
  25. cpu.memory[loc] = val
  26. return cpu