-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8_state.py
45 lines (39 loc) · 1.79 KB
/
chip8_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
FONT = (0xF0, 0x90, 0x90, 0x90, 0xF0, # 0
0x20, 0x60, 0x20, 0x20, 0x70, # 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, # 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, # 3
0x90, 0x90, 0xF0, 0x10, 0x10, # 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, # 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, # 6
0xF0, 0x10, 0x20, 0x40, 0x40, # 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, # 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, # 9
0xF0, 0x90, 0xF0, 0x90, 0x90, # A
0xE0, 0x90, 0xE0, 0x90, 0xE0, # B
0xF0, 0x80, 0x80, 0x80, 0xF0, # C
0xE0, 0x90, 0x90, 0x90, 0xE0, # D
0xF0, 0x80, 0xF0, 0x80, 0xF0, # E
0xF0, 0x80, 0xF0, 0x80, 0x80) # F
class State:
def __init__(self, program): # Creates a state object with all values empty
self.SCREEN_WIDTH = 64
self.SCREEN_HEIGHT = 32
self.register = [0x00] * 16 # 16 registers, each 8 bits. Reigisters go from V0 - VF, but VF doubles as a carry flag.
self.I = 0x0000 # The address register. 16 bits.
self.pc = 0x200 # Points to the current opcode
self.delay = 0x00 # When set by 0xFX07, counts down at 60hz
self.sound = 0x00 # When set by 0xFX18, counts down at 60hz while emitting a tone.
self.stack = []
self.keypad = [0] * 16
self.init_memory(program)
self.init_screen()
def load_data(self, data, data_start):
'''Load data into memory, starting at datastart'''
for i, byte in enumerate(data):
self.memory[data_start+i] = byte
def init_memory(self, program):
self.memory = [0x00] * 0x1000 # Default 4096 (0x1000) memory locations, each 8 bits (1 byte).
self.load_data(program, 0x200)
self.load_data(FONT, 0)
def init_screen(self):
self.screen = [0x00]*self.SCREEN_HEIGHT*self.SCREEN_WIDTH