Repository navigation
Expand file tree
/
Copy pathmicrocode_create_2.py
More file actions
168 lines (142 loc) · 5.06 KB
/
Copy pathmicrocode_create_2.py
File metadata and controls
168 lines (142 loc) · 5.06 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from __future__ import annotations
from eeprom import EEPROM
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(name)s [%(levelname)s] %(filename)s:%(lineno)d: %(message)s'
)
logger = logging.getLogger(__name__)
class Options:
def __init__(self, *args):
bit_arrays = []
for option in args:
match option:
case int():
bit_arrays.append(option)
case Options():
bit_arrays.extend(option.bit_arrays)
case _:
raise TypeError(f'Invalid option: {option}')
self._bit_arrays = frozenset(bit_arrays)
def __len__(self):
return len(self._bit_arrays)
@property
def bit_arrays(self):
return self._bit_arrays
def __or__(self, other):
if isinstance(other, int):
return Options(*[bit_array | other for bit_array in self._bit_arrays])
elif isinstance(other, Options):
if len(self._bit_arrays) != len(other.bit_arrays):
raise ValueError("BitArrayArrays must be of equal length for bitwise OR")
return Options(*[b1 | b2 for b1, b2 in zip(self._bit_arrays, other.bit_arrays)])
else:
return NotImplemented
def __ror__(self, other):
return self.__or__(other)
def __and__(self, other):
if isinstance(other, int):
return Options(*[bit_array & other for bit_array in self._bit_arrays])
elif isinstance(other, Options):
if len(self._bit_arrays) != len(other.bit_arrays):
raise ValueError("BitArrayArrays must be of equal length for bitwise AND")
return Options(*[b1 & b2 for b1, b2 in zip(self._bit_arrays, other.bit_arrays)])
else:
return NotImplemented
def __rand__(self, other):
return self.__and__(other)
def __xor__(self, other):
if isinstance(other, int):
return Options(*[bit_array ^ other for bit_array in self._bit_arrays])
elif isinstance(other, Options):
if len(self._bit_arrays) != len(other.bit_arrays):
raise ValueError("BitArrayArrays must be of equal length for bitwise XOR")
return Options(*[b1 ^ b2 for b1, b2 in zip(self._bit_arrays, other.bit_arrays)])
else:
return NotImplemented
def __rxor__(self, other):
return self.__xor__(other)
def __invert__(self):
return Options(*[~bit_array for bit_array in self._bit_arrays])
def __hash__(self):
return hash(self._bit_arrays)
def __str__(self):
return f'Options({", ".join(map(lambda x: f'0b{x:011b}', self._bit_arrays))})'
def __repr__(self):
return str(self)
@staticmethod
def expand(microcode: dict[int | Options, int]) -> dict[int, int]:
expanded_microcode = {}
for key, value in microcode.items():
match key:
case Options():
for bit_array in key.bit_arrays:
expanded_microcode[bit_array] = value
case int():
expanded_microcode[key] = value
case _:
raise TypeError(f'Invalid key: {key}')
return expanded_microcode
STEP_0 = 0b000 << 4
STEP_1 = 0b001 << 4
STEP_2 = 0b010 << 4
STEP_3 = 0b011 << 4
STEP_4 = 0b100 << 4
STEP_5 = 0b101 << 4
STEP_6 = 0b110 << 4
STEP_7 = 0b111 << 4
ALL_STEPS = Options(STEP_0, STEP_1, STEP_2, STEP_3, STEP_4, STEP_5, STEP_6, STEP_7)
HT = 1 << 15 # Halt
MI = 1 << 14 # Memory Address Register In
RI = 1 << 13 # RAM In
RO = 1 << 12 # RAM Out
IO = 1 << 11 # Instruction Register Out
II = 1 << 10 # Instruction Register In
AI = 1 << 9 # A Register In
AO = 1 << 8 # A Register Out
EO = 1 << 7 # Sum Out
SU = 1 << 6 # Subtract
BI = 1 << 5 # B Register In
OI = 1 << 4 # Output Register In
CE = 1 << 3 # Counter Enable
CO = 1 << 2 # Counter Out
J = 1 << 1 # Jump
NOP = 0b0000 << 7 # No Operation
LDA = 0b0001 << 7 # Load A
STA = 0b0010 << 7 # Store A
ADD = 0b0011 << 7 #
SUB = 0b0100 << 7
JMP = 0b0101 << 7
LIA = 0b0110 << 7
LIB = 0b0111 << 7
OUT = 0b1110 << 7
HLT = 0b1111 << 7
ALL_INSTRUCTIONS = Options(NOP, LDA, STA, ADD, SUB, JMP, LIA, OUT, HLT)
#
# NO_FLAGS = 0b0000
# CARRY_FLAG = 0b0001
# ANY_CARRY_FLAG = Options(NO_FLAGS, CARRY_FLAG)
# ZERO_FLAG = 0b0010
# ANY_ZERO_FLAG = Options(NO_FLAGS, ZERO_FLAG)
# ANY_FLAGS = ANY_CARRY_FLAG | ANY_ZERO_FLAG
MICROCODE = {
ALL_INSTRUCTIONS | STEP_0: MI | CO,
ALL_INSTRUCTIONS | STEP_1: RO | II | CE,
Options(LDA, STA, ADD, SUB) | STEP_2: IO | MI,
LDA | STEP_3: RO | AI,
STA | STEP_3: AO | RI,
Options(ADD, SUB) | STEP_3: RO | BI,
ADD | STEP_4: AI | EO,
SUB | STEP_4: AI | EO | SU,
JMP | STEP_2: IO | J,
LIA | STEP_2: IO | AI,
LIB | STEP_2: IO | BI,
OUT | STEP_2: AO | OI,
HLT | STEP_2: HT,
}
print(Options.expand(MICROCODE))
#data = Options.expand(MICROCODE)
#first_data = {key: value & 0xFF for key, value in data.items()}
#with EEPROM() as eeprom:
# eeprom.write_bytes(first_data)
# eeprom.check_bytes(first_data)