-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.py
More file actions
121 lines (94 loc) · 3.62 KB
/
cpu.py
File metadata and controls
121 lines (94 loc) · 3.62 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from cache import Cache
from memory import Memory
# Instruction operators
ADD_INSTR_OPERATOR = "ADD"
ADD_I_INSTR_OPERATOR = "ADDI"
JUMP_INSTR_OPERATOR = "JMP"
CACHE_INSTR_OPERATOR = "CACHE"
HALT_INSTR_OPERATOR = "HALT"
# Cache status values
CACHE_OFF_VALUE = 0
CACHE_ON_VALUE = 1
CACHE_FLUSH_VALUE = 2
# Initialize CPU constants
CPU_COUNTER_INIT_VALUE = 0
NUM_REGISTERS = 9
# Helper function which converts register position to integer
def register_to_index(register):
return int(register[1:])
class CPU:
def __init__(self):
self.memory_bus = Memory()
self.cache = Cache()
self.cache_flag = False
self.cpu_counter = CPU_COUNTER_INIT_VALUE
# Creates list NUM_REGISTERS long with [0, 0, ...]
self.registers = [0] * NUM_REGISTERS
# Memory bus methods
def write_memory_bus(self, address, value):
self.memory_bus.write_memory_bus(address, value)
def search_memory_bus(self, address):
self.memory_bus.search_memory_bus(address)
# Cache methods
def flush_cache(self):
self.cache.flush_cache()
def write_cache(self, address, value):
self.cache.write_cache(address, value)
def search_cache(self, address):
return self.cache.search_cache(address)
def set_cache_flag(self, value):
self.cache_flag = value
# CPU counter methods
def reset_cpu_counter(self):
self.cpu_counter = CPU_COUNTER_INIT_VALUE
print("CPU counter reset to " + str(CPU_COUNTER_INIT_VALUE))
def increment_cpu_counter(self):
self.cpu_counter += 1
def get_cpu_counter_value(self):
return self.cpu_counter
# Register methods
def reset_registers(self):
for i in range(len(self.registers)):
self.registers[i] = 0
print("Reset registers")
# Command methods
def cache_instruction(self, value):
if value == CACHE_OFF_VALUE:
self.set_cache_flag(False)
print("Turned cache off")
if value == CACHE_ON_VALUE:
self.set_cache_flag(True)
print("Turned cache on")
if value == CACHE_FLUSH_VALUE:
self.flush_cache()
def jump_instruction(self, target):
self.cpu_counter = target
print("Jumped to " + str(target))
def add_instruction(self, destination, source, target):
destination = register_to_index(destination)
source = register_to_index(source)
target = register_to_index(target)
self.registers[destination] = source + target
def add_i_instruction(self, destination, source, immediate):
destination = register_to_index(destination)
source = register_to_index(source)
self.registers[destination] = source + int(immediate)
# Instruction Parsing
def parse_instructions(self, instruction):
print("\nInstruction: " + str(instruction))
self.increment_cpu_counter()
parsed_instruction = instruction.split(",")
command = parsed_instruction[0]
if command == CACHE_INSTR_OPERATOR:
self.cache_instruction(int(parsed_instruction[1]))
elif command == JUMP_INSTR_OPERATOR:
self.jump_instruction(int(parsed_instruction[1]))
elif command == ADD_INSTR_OPERATOR:
self.add_instruction(parsed_instruction[1], parsed_instruction[2], parsed_instruction[3])
elif command == ADD_I_INSTR_OPERATOR:
self.add_i_instruction(parsed_instruction[1], parsed_instruction[2], parsed_instruction[3])
elif command == HALT_INSTR_OPERATOR:
print("CPU Halting...")
return
else:
print("Command not recognized")