Skip to content

Commit 7bb45a4

Browse files
authored
Merge pull request #8 from sql-hkr/refactor/type-hints
Refactor type hints to use built-in generics #7
2 parents 16e588d + 5342ad9 commit 7bb45a4

2 files changed

Lines changed: 19 additions & 21 deletions

File tree

‎src/tiny8/cpu.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
instruction handlers by defining methods named ``op_<mnemonic>`` on ``CPU``.
77
"""
88

9-
from typing import Dict, List, Optional, Tuple
9+
from typing import Optional
1010

1111
from .memory import Memory
1212

@@ -29,18 +29,18 @@ class CPU:
2929
currently-loaded program.
3030
3131
Attributes:
32-
regs (List[int]): 32 8-bit general purpose registers (R0..R31).
32+
regs (list[int]): 32 8-bit general purpose registers (R0..R31).
3333
pc (int): Program counter (index into ``program``).
3434
sp (int): Stack pointer (index into RAM in the associated
3535
:class:`tiny8.memory.Memory`).
3636
sreg (int): Status register bits stored in a single integer (I, T,
3737
H, S, V, N, Z, C).
3838
cycle (int): Instruction execution counter.
39-
reg_trace (List[Tuple[int, int, int]]): Per-cycle register change
39+
reg_trace (list[tuple[int, int, int]]): Per-cycle register change
4040
trace entries of the form ``(cycle, reg, new_value)``.
41-
mem_trace (List[Tuple[int, int, int]]): Per-cycle memory change
41+
mem_trace (list[tuple[int, int, int]]): Per-cycle memory change
4242
trace entries of the form ``(cycle, addr, new_value)``.
43-
step_trace (List[dict]): Full per-step snapshots useful for
43+
step_trace (list[dict]): Full per-step snapshots useful for
4444
visualization and debugging.
4545
4646
Note:
@@ -52,7 +52,7 @@ class CPU:
5252
def __init__(self, memory: Optional[Memory] = None):
5353
self.mem = memory or Memory()
5454
# 32 general purpose 8-bit registers R0-R31
55-
self.regs: List[int] = [0] * 32
55+
self.regs: [int] = [0] * 32
5656
# Program Counter (word-addressable for AVR) - we use byte addressing for simplicity
5757
self.pc: int = 0
5858
# Stack Pointer - point into RAM
@@ -62,16 +62,16 @@ def __init__(self, memory: Optional[Memory] = None):
6262
# Cycle counter
6363
self.cycle: int = 0
6464
# Simple interrupt vector table (addr->enabled)
65-
self.interrupts: Dict[int, bool] = {}
65+
self.interrupts: dict[int, bool] = {}
6666
# Execution trace of register/memory changes per cycle
67-
self.reg_trace: List[Tuple[int, int, int]] = [] # (cycle, reg, newval)
68-
self.mem_trace: List[Tuple[int, int, int]] = [] # (cycle, addr, newval)
67+
self.reg_trace: list[tuple[int, int, int]] = [] # (cycle, reg, newval)
68+
self.mem_trace: list[tuple[int, int, int]] = [] # (cycle, addr, newval)
6969
# Full step trace: list of dicts with cycle, pc, instr_text, regs snapshot, optional mem snapshot
70-
self.step_trace: List[dict] = []
70+
self.step_trace: list[dict] = []
7171
# Program area - list of instructions as tuples: (mnemonic, *operands)
72-
self.program: List[Tuple[str, Tuple]] = []
72+
self.program: list[tuple[str, tuple]] = []
7373
# Labels -> pc
74-
self.labels: Dict[str, int] = {}
74+
self.labels: dict[str, int] = {}
7575
# Running state
7676
self.running = False
7777

@@ -289,11 +289,11 @@ def write_ram(self, addr: int, val: int) -> None:
289289
self.mem_trace.append((self.cycle, addr, val & 0xFF))
290290

291291
# Program loading
292-
def load_program(self, program: List[Tuple[str, Tuple]], labels: Dict[str, int]):
292+
def load_program(self, program: list[tuple[str, tuple]], labels: dict[str, int]):
293293
"""Load an assembled program into the CPU.
294294
295295
Args:
296-
program: List of ``(mnemonic, operands)`` tuples returned by the
296+
program: list of ``(mnemonic, operands)`` tuples returned by the
297297
assembler.
298298
labels: Mapping of label strings to instruction indices.
299299

‎src/tiny8/memory.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Memory model for tiny8 - simple RAM and ROM with change tracking."""
22

3-
from typing import List, Tuple
4-
53

64
class Memory:
75
"""
@@ -42,8 +40,8 @@ def __init__(self, ram_size: int = 2048, rom_size: int = 2048):
4240
self.ram = [0] * ram_size
4341
self.rom = [0] * rom_size
4442
# change logs: list of (addr, old, new, cycle)
45-
self.ram_changes: List[Tuple[int, int, int, int]] = []
46-
self.rom_changes: List[Tuple[int, int, int, int]] = []
43+
self.ram_changes: list[tuple[int, int, int, int]] = []
44+
self.rom_changes: list[tuple[int, int, int, int]] = []
4745

4846
def read_ram(self, addr: int) -> int:
4947
"""Read and return the value stored in RAM at the specified address.
@@ -87,7 +85,7 @@ def write_ram(self, addr: int, value: int, cycle: int = 0) -> None:
8785
if old != self.ram[addr]:
8886
self.ram_changes.append((addr, old, self.ram[addr], cycle))
8987

90-
def load_rom(self, data: List[int]) -> None:
88+
def load_rom(self, data: list[int]) -> None:
9189
"""Load a ROM image into the emulator's ROM buffer.
9290
9391
Args:
@@ -128,7 +126,7 @@ def read_rom(self, addr: int) -> int:
128126
raise IndexError("ROM address out of range")
129127
return self.rom[addr]
130128

131-
def snapshot_ram(self) -> List[int]:
129+
def snapshot_ram(self) -> list[int]:
132130
"""Return a snapshot copy of the emulator's RAM.
133131
134132
Returns:
@@ -139,7 +137,7 @@ def snapshot_ram(self) -> List[int]:
139137
"""
140138
return list(self.ram)
141139

142-
def snapshot_rom(self) -> List[int]:
140+
def snapshot_rom(self) -> list[int]:
143141
"""Return a snapshot copy of the ROM contents.
144142
145143
Returns:

0 commit comments

Comments
 (0)