Skip to content

Commit 2e64371

Browse files
authored
Merge pull request #34 from craigthomas/pitch-register
Add Pitch Register
2 parents dab0e5e + 1da3918 commit 2e64371

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

chip8/cpu.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def __init__(
9191
self.sp = STACK_POINTER_START
9292
self.index = 0
9393
self.rpl = [0] * NUM_REGISTERS
94+
self.pitch = 64
95+
self.playback_rate = 4000
9496

9597
self.shift_quirks = shift_quirks
9698
self.index_quirks = index_quirks
@@ -150,6 +152,7 @@ def __init__(
150152
0x29: self.load_index_with_reg_sprite, # Fs29 - LOAD I, Vs
151153
0x30: self.load_index_with_extended_reg_sprite, # Fs30 - LOAD I, Vs
152154
0x33: self.store_bcd_in_memory, # Fs33 - BCD
155+
0x3A: self.load_pitch, # Fx3A - PITCH Vx
153156
0x55: self.store_regs_in_memory, # Fs55 - STOR [I], Vs
154157
0x65: self.read_regs_from_memory, # Fs65 - LOAD Vs, [I]
155158
0x75: self.store_regs_in_rpl, # Fs75 - SRPL Vs
@@ -923,6 +926,21 @@ def store_bcd_in_memory(self):
923926
self.memory[self.index + 2] = int(bcd_value[2])
924927
self.last_op = f"BCD V{x:01X} ({bcd_value})"
925928

929+
def load_pitch(self):
930+
"""
931+
Fx3A - PITCH Vx
932+
933+
Loads the value from register x into the pitch register. The
934+
register calculation is as follows:
935+
936+
Bits: 15-12 11-8 7-4 3-0
937+
F x 3 A
938+
"""
939+
x = (self.operand & 0x0F00) >> 8
940+
self.pitch = self.v[x]
941+
self.playback_rate = 4000 * 2 ** ((self.pitch - 64) / 48)
942+
self.last_op = f"PITCH V{x:01X}"
943+
926944
def store_regs_in_memory(self):
927945
"""
928946
Fn55 - STOR [I]
@@ -1011,6 +1029,8 @@ def reset(self):
10111029
self.sp = STACK_POINTER_START
10121030
self.index = 0
10131031
self.rpl = [0] * NUM_REGISTERS
1032+
self.pitch = 64
1033+
self.playback_rate = 4000
10141034

10151035
def load_rom(self, filename, offset=PROGRAM_COUNTER_START):
10161036
"""

test/test_chip8cpu.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ def setUp(self):
3737
def test_memory_size_default_64k(self):
3838
self.assertEqual(65536, len(self.cpu.memory))
3939

40+
def test_pitch_init_64(self):
41+
self.assertEqual(64, self.cpu.pitch)
42+
self.assertEqual(4000, self.cpu.playback_rate)
43+
44+
def test_load_pitch(self):
45+
self.cpu.v[1] = 112
46+
self.cpu.operand = 0xF13A
47+
self.cpu.load_pitch()
48+
self.assertEqual(112, self.cpu.pitch)
49+
self.assertEqual(8000, self.cpu.playback_rate)
50+
51+
def test_load_pitch_integration(self):
52+
self.cpu.v[1] = 112
53+
self.cpu.memory[0x0200] = 0xF1
54+
self.cpu.memory[0x0201] = 0x3A
55+
self.cpu.execute_instruction()
56+
self.assertEqual(112, self.cpu.pitch)
57+
self.assertEqual(8000, self.cpu.playback_rate)
58+
4059
def test_return_from_subroutine(self):
4160
for address in range(0x200, 0xFFFF, 0x10):
4261
self.cpu.memory[self.cpu.sp] = address & 0x00FF
@@ -293,7 +312,6 @@ def test_right_shift_reg_quirks(self):
293312

294313
def test_right_shift_reg(self):
295314
self.cpu.shift_quirks = False
296-
y = 0x8
297315
for x in range(0xF):
298316
for y in range(0xF):
299317
for value in range(0, 0xFF, 0x10):

0 commit comments

Comments
 (0)