-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyCHIP8.py
More file actions
64 lines (48 loc) · 1.73 KB
/
PyCHIP8.py
File metadata and controls
64 lines (48 loc) · 1.73 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import argparse
import logging
from pathlib import Path
import pygame
from PyCHIP8.conf import Config
from PyCHIP8.cpu import CPU
from PyCHIP8.screen import Screen
logging.basicConfig(level=logging.WARNING)
class PyCHIP8:
"""
Main class of the emulator
"""
def __init__(self, path: str):
"""
PyCHIP8 class constructor, its only purpose is to initialize CPU and Screen objects
:param path: Path to file containing CHIP8 game or program
"""
self.screen = Screen()
self.cpu = CPU(self.screen)
self.cpu.reset()
self.rom_path = Path(path)
def run(self):
"""
Main method of CHIP-8 emulator
"""
single_instruction_interval = (1000 // Config.CPU_CLOCK_SPEED)
pygame.time.set_timer(pygame.USEREVENT, Config.TIMER_DELAY)
pygame.display.set_caption("PyCHIP8 by Piotr Kramek")
try:
self.cpu.load_rom(self.rom_path)
except FileNotFoundError:
print("\nFile does not exist\n")
else:
while self.cpu.running:
pygame.time.wait(single_instruction_interval)
self.cpu.execute_opcode()
self.screen.refresh()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.cpu.exit()
if event.type == pygame.USEREVENT:
self.cpu.decrement_values_in_timers()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='CHIP-8 emulator')
parser.add_argument('--rom', help='Path to ROM file containing CHIP-8 game or program')
args = parser.parse_args()
emulator = PyCHIP8(args.rom)
emulator.run()