-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (51 loc) · 1.51 KB
/
main.py
File metadata and controls
70 lines (51 loc) · 1.51 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
from cpu import CPU
'''
CPU COMMAND KEY
INSTRUCTION
| Argument - Purpose
CACHE
| 0 - Turn cache off
| 1 - Turn cache on
| 2 - Flush cache
JMP
| # - Jump to specified position
'''
# Reference input files
DATA_INPUT_FILE = "Inputs/data_input.txt"
INSTRUCTION_INPUT_FILE = "Inputs/instruction_input.txt"
# Read data from file and strip whitespace
def read_strip_data(filepath):
# Open file in read mode
file = open(filepath, 'r')
# Read lines from file
data = file.readlines()
# Strip whitespace using anonymous lambda function
data = list(
map(
lambda s: s.strip(), data
)
)
return data
def initialize_memory_bus(cpu):
data_input = read_strip_data(DATA_INPUT_FILE)
for data in data_input:
data_parsed = data.split(",")
address, value = data_parsed[0], data_parsed[1]
cpu.write_memory_bus(address, value)
def send_cpu_instructions(cpu):
instructions_input = read_strip_data(INSTRUCTION_INPUT_FILE)
for instruction in instructions_input:
cpu.parse_instructions(instruction)
# Run CPU Simulator
print("------------------------")
print(" CPU Simulator ")
print("------------------------")
print("\nInstantiating CPU...")
cpu = CPU()
print("\nInitializing memory bus...")
initialize_memory_bus(cpu)
print("\nSending instructions to CPU...\n")
print("CPU Counter:" + str(cpu.get_cpu_counter_value()))
cpu.parse_instructions("JMP, 4")
print("CPU Counter: " + str(cpu.get_cpu_counter_value()))
send_cpu_instructions(cpu)