-
Notifications
You must be signed in to change notification settings - Fork 1
/
microcode.py
379 lines (311 loc) · 10.3 KB
/
microcode.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/python3
# Generates CPU microcode binary
# CPU constants
INSTR_COUNT = 64
INSTR_STATES = 8
STATE_SIZE = 3
# Addressing modes
ADDR_PC = 0
ADDR_SP = 1
ADDR_HL = 2
ADDR_CACHE = 3
# Data destinations
WRITE_A = 0
WRITE_REG = 1
WRITE_H = 2
WRITE_L = 3
WRITE_C0 = 4
WRITE_C1 = 5
WRITE_MEM = 6
WRITE_PORT = 7
# Data sources
READ_A = 0
READ_REG = 1
READ_H = 2
READ_L = 3
READ_C0 = 4
READ_C1 = 5
READ_MEM = 6
READ_PC_LOW = 7
READ_PC_HIGH = 8
READ_PORT = 9
# Data operations
OP_ALU = 0
OP_PASS = 1
OP_INC = 2
OP_DEC = 3
# PC write modes
PC_HL = 0
PC_CACHE = 1
PC_ADD = 2
# ALU modes
ALU_ADD = 0
ALU_ADC = 1
ALU_SUB = 2
ALU_SBC = 3
ALU_AND = 4
ALU_OR = 5
ALU_XOR = 6
ALU_CMP = 7
ALU_ASL = 8
ALU_LSR = 9
ALU_ASR = 10
# Instruction end conditions
COND_FLAG = 0
COND_NOT_FLAG = 1
COND_NOT_ZERO = 2
COND_NOT_MAX = 3
# Microcode generation state
states = [0 for i in range(INSTR_COUNT * INSTR_STATES)]
instruction = 0
instruction_state = 0
# Specify the current instruction
def instr(opcode):
global instruction, instruction_state
instruction = opcode
instruction_state = 0
# Add a state to the current instruction
def state(addr_mode=0, write_op=0, read_sel=0, write_sel=0, mode=0, sp_en=False, sp_inc=False, write=False, f_write=False,
condition=False, halt=False, pc_inc=False, pc_write=False, instr_done=False):
global instruction_state
value = 0
value |= addr_mode & 0x3
value |= (write_op & 0x3) << 2
value |= (read_sel & 0xF) << 4
value |= (write_sel & 0x7) << 8
value |= (mode & 0xF) << 11
value |= sp_en << 15
value |= sp_inc << 16
value |= write << 17
value |= f_write << 18
value |= condition << 19
value |= halt << 20
value |= pc_inc << 21
value |= pc_write << 22
value |= instr_done << 23
states[instruction * INSTR_STATES + instruction_state] = value
instruction_state += 1
def alu_imm_states(mode, write=True):
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state(write=write, write_op=OP_ALU, write_sel=WRITE_A, read_sel=READ_C0, mode=mode, f_write=True, instr_done=True)
def alu_reg_states(mode, write=True):
state(write=write, write_op=OP_ALU, write_sel=WRITE_A, read_sel=READ_REG, mode=mode, f_write=True, instr_done=True)
def main():
# NOP
instr(0b000000)
state(instr_done=True)
# LDR imm,reg
instr(0b000001)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_REG, read_sel=READ_MEM, pc_inc=True, f_write=True, instr_done=True)
# LDR [addr],reg
instr(0b000010)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C1, read_sel=READ_MEM, pc_inc=True)
state(addr_mode=ADDR_CACHE)
state(write=True, write_op=OP_PASS, write_sel=WRITE_REG, read_sel=READ_MEM, addr_mode=ADDR_CACHE, f_write=True, instr_done=True)
# LDR [HL],reg
instr(0b000011)
state()
state(addr_mode=ADDR_HL)
state(write=True, write_op=OP_PASS, write_sel=WRITE_REG, read_sel=READ_MEM, addr_mode=ADDR_HL, f_write=True, instr_done=True)
# STR [addr],reg
instr(0b000100)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C1, read_sel=READ_MEM, pc_inc=True)
state(write=True, write_op=OP_PASS, write_sel=WRITE_MEM, read_sel=READ_REG, addr_mode=ADDR_CACHE, instr_done=True)
# STR [HL],reg
instr(0b000101)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_MEM, read_sel=READ_REG, addr_mode=ADDR_HL, instr_done=True)
# LDA addr
instr(0b000110)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_H, read_sel=READ_MEM, pc_inc=True)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_L, read_sel=READ_MEM, pc_inc=True, instr_done=True)
# IN imm,reg
instr(0b000111)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state(write=True, write_op=OP_PASS, write_sel=WRITE_REG, read_sel=READ_PORT, f_write=True, instr_done=True)
# OUT imm,reg
instr(0b001000)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state(write=True, write_op=OP_PASS, write_sel=WRITE_PORT, read_sel=READ_REG, instr_done=True)
# INC reg
instr(0b001001)
state(write=True, write_op=OP_INC, write_sel=WRITE_REG, read_sel=READ_REG, f_write=True, instr_done=True)
# DEC reg
instr(0b001010)
state(write=True, write_op=OP_DEC, write_sel=WRITE_REG, read_sel=READ_REG, f_write=True, instr_done=True)
# INA
instr(0b001011)
state(write=True, write_op=OP_INC, write_sel=WRITE_L, read_sel=READ_L, f_write=True)
state(read_sel=READ_L, write_op=OP_PASS, condition=True, mode=COND_NOT_ZERO)
state(write=True, write_op=OP_INC, write_sel=WRITE_H, read_sel=READ_H, f_write=True, instr_done=True)
# DEA
instr(0b001100)
state(write=True, write_op=OP_DEC, write_sel=WRITE_L, read_sel=READ_L, f_write=True)
state(read_sel=READ_L, write_op=OP_PASS, condition=True, mode=COND_NOT_MAX)
state(write=True, write_op=OP_DEC, write_sel=WRITE_H, read_sel=READ_H, f_write=True, instr_done=True)
# ADD imm
instr(0b001101)
alu_imm_states(ALU_ADD)
# ADD reg
instr(0b001110)
alu_reg_states(ALU_ADD)
# ADC imm
instr(0b001111)
alu_imm_states(ALU_ADC)
# ADC reg
instr(0b010000)
alu_reg_states(ALU_ADC)
# SUB imm
instr(0b010001)
alu_imm_states(ALU_SUB)
# SUB reg
instr(0b010010)
alu_reg_states(ALU_SUB)
# SBC imm
instr(0b010011)
alu_imm_states(ALU_SBC)
# SBC reg
instr(0b010100)
alu_reg_states(ALU_SBC)
# AND imm
instr(0b010101)
alu_imm_states(ALU_AND)
# AND reg
instr(0b010110)
alu_reg_states(ALU_AND)
# OR imm
instr(0b010111)
alu_imm_states(ALU_OR)
# OR reg
instr(0b011000)
alu_reg_states(ALU_OR)
# XOR imm
instr(0b011001)
alu_imm_states(ALU_XOR)
# XOR reg
instr(0b011010)
alu_reg_states(ALU_XOR)
# CMP imm
instr(0b011011)
alu_imm_states(ALU_CMP, False)
# CMP reg
instr(0b011100)
alu_reg_states(ALU_CMP, False)
# JMP addr
instr(0b011101)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C1, read_sel=READ_MEM, pc_inc=True)
state(pc_write=True, mode=PC_CACHE, instr_done=True)
# JMP HL
instr(0b011110)
state(pc_write=True, mode=PC_HL, instr_done=True)
# JR imm
instr(0b011111)
state()
state()
state(read_sel=READ_MEM, write_op=OP_PASS, pc_write=True, mode=PC_ADD)
state(pc_inc=True, instr_done=True)
# JR imm,cc
instr(0b100000)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state(condition=True, mode=COND_NOT_FLAG)
state(read_sel=READ_C0, write_op=OP_PASS, pc_write=True, mode=PC_ADD, instr_done=True)
# JR imm,nn
instr(0b100001)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state(condition=True, mode=COND_FLAG)
state(read_sel=READ_C0, write_op=OP_PASS, pc_write=True, mode=PC_ADD, instr_done=True)
# IN reg
instr(0b100010)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_A)
state(write=True, write_op=OP_PASS, write_sel=WRITE_REG, read_sel=READ_PORT, f_write=True, instr_done=True)
# OUT reg
instr(0b100011)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_A)
state(write=True, write_op=OP_PASS, write_sel=WRITE_PORT, read_sel=READ_REG, instr_done=True)
# PUSH reg
instr(0b100100)
state(sp_en=True)
state(write=True, write_op=OP_PASS, write_sel=WRITE_MEM, read_sel=READ_REG, addr_mode=ADDR_SP, instr_done=True)
# POP reg
instr(0b100101)
state()
state(addr_mode=ADDR_SP)
state(write=True, write_op=OP_PASS, write_sel=WRITE_REG, read_sel=READ_MEM, addr_mode=ADDR_SP, sp_en=True, sp_inc=True, instr_done=True)
# JSR addr
instr(0b100110)
state()
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, pc_inc=True)
state()
state(write=True, write_op=OP_PASS, write_sel=WRITE_C1, read_sel=READ_MEM, pc_inc=True, sp_en=True)
state(write=True, write_op=OP_PASS, write_sel=WRITE_MEM, read_sel=READ_PC_LOW, addr_mode=ADDR_SP, sp_en=True)
state(write=True, write_op=OP_PASS, write_sel=WRITE_MEM, read_sel=READ_PC_HIGH, addr_mode=ADDR_SP)
state(pc_write=True, mode=PC_CACHE, instr_done=True)
# RET
instr(0b100111)
state()
state(addr_mode=ADDR_SP)
state(write=True, write_op=OP_PASS, write_sel=WRITE_C0, read_sel=READ_MEM, addr_mode=ADDR_SP, sp_en=True, sp_inc=True)
state(addr_mode=ADDR_SP)
state(write=True, write_op=OP_PASS, write_sel=WRITE_C1, read_sel=READ_MEM, addr_mode=ADDR_SP, sp_en=True, sp_inc=True)
state(pc_write=True, mode=PC_CACHE, instr_done=True)
# HALT
instr(0b101000)
state()
state(halt=True)
# LSL
instr(0b101001)
state(write=True, write_op=OP_ALU, write_sel=WRITE_A, read_sel=READ_REG, mode=ALU_ASL, f_write=True, instr_done=True)
# LSR
instr(0b101010)
state(write=True, write_op=OP_ALU, write_sel=WRITE_A, read_sel=READ_REG, mode=ALU_LSR, f_write=True, instr_done=True)
# ASR
instr(0b101011)
state(write=True, write_op=OP_ALU, write_sel=WRITE_A, read_sel=READ_REG, mode=ALU_ASR, f_write=True, instr_done=True)
output = bytearray(len(states) * STATE_SIZE)
for i in range(len(states)):
output[i * 3] = states[i] >> 16
output[i * 3 + 1] = (states[i] & 0xFF00) >> 8
output[i * 3 + 2] = states[i] & 0xFF
f = open('microcode.bin', 'wb')
f.write(output)
f.close()
output = bytearray(len(states) * STATE_SIZE)
for i in range(len(states)):
output[i * 3 + 2] = states[i] >> 16
output[i * 3 + 1] = (states[i] & 0xFF00) >> 8
output[i * 3] = states[i] & 0xFF
f = open('microcode-LE.bin', 'wb')
f.write(output)
f.close()
if __name__ == '__main__':
main()