This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_unit_test.py
154 lines (140 loc) · 5.92 KB
/
control_unit_test.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import unittest
from isa import Addressing, Instruction, Opcode
from machine import ControlUnit, DataPath
class ControlUnitTest(unittest.TestCase):
def test_program_fetch(self):
program = [Instruction(Opcode.LD, 50, Addressing.IMMEDIATE)]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.program_fetch()
assert program[0] == control_unit.program
def test_address_fetch_direct(self):
program = [Instruction(Opcode.LD, 50, Addressing.DIRECT)]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.program_fetch()
control_unit.address_fetch()
assert 50 == control_unit.data_path.address_register
def test_address_fetch_indirect(self):
program = [
Instruction(Opcode.LD, 1, Addressing.INDIRECT),
Instruction(Opcode.VAR, 512, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.program_fetch()
control_unit.address_fetch()
assert 512 == control_unit.data_path.address_register
def test_operand_fetch_immediate(self):
program = [
Instruction(Opcode.LD, 50, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.program_fetch()
control_unit.address_fetch()
control_unit.operand_fetch()
assert 50 == control_unit.data_path.mem_out.arg
def test_operand_fetch_direct(self):
program = [
Instruction(Opcode.LD, 1, Addressing.DIRECT),
Instruction(Opcode.VAR, 512, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.program_fetch()
control_unit.address_fetch()
control_unit.operand_fetch()
assert 512 == control_unit.data_path.mem_out.arg
def test_operand_fetch_indirect(self):
program = [
Instruction(Opcode.LD, 1, Addressing.INDIRECT),
Instruction(Opcode.VAR, 2, Addressing.IMMEDIATE),
Instruction(Opcode.VAR, 512, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.program_fetch()
control_unit.address_fetch()
control_unit.operand_fetch()
assert 512 == control_unit.data_path.mem_out.arg
def test_execute_load(self):
program = [
Instruction(Opcode.LD, 42, Addressing.IMMEDIATE),
Instruction(Opcode.ST, 2, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.decode_and_execute()
assert 42 == data_path.accumulator
control_unit.decode_and_execute()
assert 42 == data_path.memory[2].arg
def test_execute_add(self):
program = [
Instruction(Opcode.LD, 42, Addressing.IMMEDIATE),
Instruction(Opcode.ADD, 42, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.decode_and_execute()
assert 1 == control_unit.program_counter
control_unit.decode_and_execute()
assert 2 == control_unit.program_counter
assert 84 == data_path.accumulator
def test_execute_mod(self):
program = [
Instruction(Opcode.LD, 42, Addressing.IMMEDIATE),
Instruction(Opcode.MOD, 2, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.decode_and_execute()
assert 1 == control_unit.program_counter
control_unit.decode_and_execute()
assert 2 == control_unit.program_counter
assert 0 == data_path.accumulator
def test_execute_jmp(self):
program = [
Instruction(Opcode.JMP, 200, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.decode_and_execute()
assert 200 == control_unit.program_counter
assert 0 == data_path.accumulator
def test_jz(self):
program = [
Instruction(Opcode.JZ, 2, Addressing.IMMEDIATE), # 0
Instruction(Opcode.VAR, 2, Addressing.IMMEDIATE), # 1
Instruction(Opcode.ADD, 420, Addressing.IMMEDIATE), # 2, sets NZ to 00
Instruction(Opcode.JZ, 0, Addressing.IMMEDIATE), # 3
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.decode_and_execute()
assert 2 == control_unit.program_counter
control_unit.decode_and_execute()
assert 420 == data_path.accumulator
assert False is data_path.alu.zero
assert 3 == control_unit.program_counter
control_unit.decode_and_execute()
assert 4 == control_unit.program_counter
def test_cmp(self):
program = [
Instruction(Opcode.LD, 420, Addressing.IMMEDIATE),
Instruction(Opcode.CMP, 0, Addressing.IMMEDIATE),
Instruction(Opcode.LD, -420, Addressing.IMMEDIATE),
Instruction(Opcode.CMP, 0, Addressing.IMMEDIATE),
]
data_path = DataPath("", program)
control_unit = ControlUnit(0, data_path)
control_unit.decode_and_execute()
control_unit.decode_and_execute()
assert control_unit.data_path.alu.zero is False
assert control_unit.data_path.alu.negative is False
assert control_unit.data_path.accumulator == 420
control_unit.decode_and_execute()
control_unit.decode_and_execute()
assert control_unit.data_path.alu.zero is False
assert control_unit.data_path.alu.negative is True
assert control_unit.data_path.accumulator == -420