-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.py
More file actions
94 lines (70 loc) · 3.11 KB
/
cpu.py
File metadata and controls
94 lines (70 loc) · 3.11 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
from cache import Cache
from memory import Memory
CPU_COUNTER_INIT_VALUE = 0
NUMBER_OF_REGISTERS = 9
ADD_INSTRUCTION_OPERATOR = "ADD"
ADD_I_INSTRUCTION_OPERATOR = "ADDI"
JUMP_INSTRUCTION_OPERATOR = "J"
CACHE_INSTRUCTION_OPERATOR = "CACHE"
CACHE_OFF_VALUE = 0
CACHE_ON_VALUE = 1
CACHE_FLUSH_VALUE = 2
def convert_register_to_index(value):
return int(value[1:])
class CPU:
def __init__(self):
self.cpu_counter = CPU_COUNTER_INIT_VALUE
self.registers = [0] * NUMBER_OF_REGISTERS
self.cache_flag = False
self.cache = Cache()
self.memory_bus = Memory()
def increment_cpu_counter(self):
self.cpu_counter += 1
def reset_cpu_counter(self):
self.cpu_counter = CPU_COUNTER_INIT_VALUE
def set_cpu_counter(self, value):
self.cpu_counter = value
def get_cpu_counter(self):
return self.cpu_counter
def reset_registers(self):
for i in range(len(self.registers)):
self.registers[i] = 0
def set_cache_flag(self, value):
self.cache_flag = value
def flush_cache(self):
self.cache.flush_cache()
def search_cache(self, address):
return self.cache.search_cache(address)
def write_cache(self, address, value):
self.cache.write_cache(address, value)
def search_memory_bus(self, address):
return self.memory_bus.search_memory_bus(address)
def write_memory_bus(self, address, value):
self.memory_bus.write_memory_bus(address, value)
def jump_instruction(self, target):
self.cpu_counter = int(target)
def add_instruction(self, destination, source, target):
self.registers[convert_register_to_index(destination)] = self.registers[convert_register_to_index(source)] + \
self.registers[convert_register_to_index(target)]
def add_i_instruction(self, destination, source, immediate):
self.registers[convert_register_to_index(destination)] = self.registers[convert_register_to_index(source)] + \
int(immediate)
def cache_instruction(self, value):
if value == CACHE_OFF_VALUE:
self.set_cache_flag(False)
if value == CACHE_ON_VALUE:
self.set_cache_flag(True)
if value == CACHE_FLUSH_VALUE:
self.flush_cache()
def parse_instruction(self, instruction):
instruction_parsed = instruction.split(",")
print("Reading instruction: " + instruction)
self.increment_cpu_counter()
if instruction_parsed[0] == ADD_INSTRUCTION_OPERATOR:
self.add_instruction(instruction_parsed[1], instruction_parsed[2], instruction_parsed[3])
if instruction_parsed[0] == ADD_I_INSTRUCTION_OPERATOR:
self.add_i_instruction(instruction_parsed[1], instruction_parsed[2], instruction_parsed[3])
if instruction_parsed[0] == JUMP_INSTRUCTION_OPERATOR:
self.jump_instruction(instruction_parsed[1])
if instruction_parsed[0] == CACHE_INSTRUCTION_OPERATOR:
self.cache_instruction(instruction_parsed[1])