forked from nathaliafab/Projeto_IH_RISC-V
-
Notifications
You must be signed in to change notification settings - Fork 2
/
assembler.py
544 lines (445 loc) · 11.3 KB
/
assembler.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
"""
Author: Nathalia Barbosa (@nathaliafab)
Date: 2023-06-16
This script translates instructions from a given file into a format readable by the instruction memory.
(It basically functions as an assembler.)
> The instructions to be translated are stored in a file named "instructions.txt"
> The translated instructions will be written to a file named "instruction.mif"
The instructions MUST follow the following formats, with one instruction per line:
<instruction> <register>,<register>,<register>
<instruction> <register>,<register>,<immediate>
<instruction> <register>,<offset>(<register>)
<instruction> <register>,<immediate>
~ Otherwise, it won't work. ;)
Example of a valid instruction file (content delimited by ```):
```
sub x6,x6,x1
addi x1,x0,8
lw x9,0(x0)
auipc x6,3
```
"""
import os
BITS_IN_CHUNK = 8
INSTRUCTION = {
"lui": {
"format": "U",
"opcode": "0110111",
"funct3": "",
"funct7": ""
},
"auipc": {
"format": "U",
"opcode": "0010111",
"funct3": "",
"funct7": ""
},
"jal": {
"format": "J",
"opcode": "1101111",
"funct3": "",
"funct7": ""
},
"jalr": {
"format": "I",
"opcode": "1100111",
"funct3": "000",
"funct7": ""
},
"beq": {
"format": "B",
"opcode": "1100011",
"funct3": "000",
"funct7": ""
},
"bne": {
"format": "B",
"opcode": "1100011",
"funct3": "001",
"funct7": ""
},
"blt": {
"format": "B",
"opcode": "1100011",
"funct3": "100",
"funct7": ""
},
"bge": {
"format": "B",
"opcode": "1100011",
"funct3": "101",
"funct7": ""
},
"bltu": {
"format": "B",
"opcode": "1100011",
"funct3": "110",
"funct7": ""
},
"bgeu": {
"format": "B",
"opcode": "1100011",
"funct3": "111",
"funct7": ""
},
"lb": {
"format": "I",
"opcode": "0000011",
"funct3": "000",
"funct7": ""
},
"lh": {
"format": "I",
"opcode": "0000011",
"funct3": "001",
"funct7": ""
},
"lw": {
"format": "I",
"opcode": "0000011",
"funct3": "010",
"funct7": ""
},
"lbu": {
"format": "I",
"opcode": "0000011",
"funct3": "100",
"funct7": ""
},
"lhu": {
"format": "I",
"opcode": "0000011",
"funct3": "101",
"funct7": ""
},
"sb": {
"format": "S",
"opcode": "0100011",
"funct3": "000",
"funct7": ""
},
"sh": {
"format": "S",
"opcode": "0100011",
"funct3": "001",
"funct7": ""
},
"sw": {
"format": "S",
"opcode": "0100011",
"funct3": "010",
"funct7": ""
},
"addi": {
"format": "I",
"opcode": "0010011",
"funct3": "000",
"funct7": ""
},
"slti": {
"format": "I",
"opcode": "0010011",
"funct3": "010",
"funct7": ""
},
"sltiu": {
"format": "I",
"opcode": "0010011",
"funct3": "011",
"funct7": ""
},
"xori": {
"format": "I",
"opcode": "0010011",
"funct3": "100",
"funct7": ""
},
"ori": {
"format": "I",
"opcode": "0010011",
"funct3": "110",
"funct7": ""
},
"andi": {
"format": "I",
"opcode": "0010011",
"funct3": "111",
"funct7": ""
},
"slli": {
"format": "I",
"opcode": "0010011",
"funct3": "001",
"funct7": "0000000"
},
"srli": {
"format": "I",
"opcode": "0010011",
"funct3": "101",
"funct7": "0000000"
},
"srai": {
"format": "I",
"opcode": "0010011",
"funct3": "101",
"funct7": "0100000"
},
"add": {
"format": "R",
"opcode": "0110011",
"funct3": "000",
"funct7": "0000000"
},
"sub": {
"format": "R",
"opcode": "0110011",
"funct3": "000",
"funct7": "0100000"
},
"sll": {
"format": "R",
"opcode": "0110011",
"funct3": "001",
"funct7": "0000000"
},
"slt": {
"format": "R",
"opcode": "0110011",
"funct3": "010",
"funct7": "0000000"
},
"sltu": {
"format": "R",
"opcode": "0110011",
"funct3": "011",
"funct7": "0000000"
},
"xor": {
"format": "R",
"opcode": "0110011",
"funct3": "100",
"funct7": "0000000"
},
"srl": {
"format": "R",
"opcode": "0110011",
"funct3": "101",
"funct7": "0000000"
},
"sra": {
"format": "R",
"opcode": "0110011",
"funct3": "101",
"funct7": "0100000"
},
"or": {
"format": "R",
"opcode": "0110011",
"funct3": "110",
"funct7": "0000000"
},
"and": {
"format": "R",
"opcode": "0110011",
"funct3": "111",
"funct7": "0000000"
},
}
# creates the file and writes the header
def create_file(file_name):
header = ("DEPTH = 65536; -- The size of memory in words\n"
"WIDTH = 8; -- The size of data in bits\n"
"ADDRESS_RADIX = DEC; -- The radix for address values\n"
"DATA_RADIX = BIN; -- The radix for data values\n"
"CONTENT -- Start of (address: data pairs)\n"
"BEGIN\n\n")
with open(file_name, "w") as file:
file.write(header)
# reads the instruction file and returns a list containing the instructions (its lines)
def read_file(file_name):
try:
with open(file_name, "r") as file:
instructions = file.readlines()
if not instructions:
raise Exception("Empty file or no instructions found.")
except Exception as e:
print(f"Error reading instructions from '{file_name}': {e}")
exit(1)
return instructions
# writes 8-bit chunks of the instructions to the file ({index}: {chunk} -- {instruction})
def write_instruction(file_name, index, chunk, instr):
if (int(index) % 4 != 0):
instr = ""
else:
instr = f"\t-- {instr.rstrip()}"
with open(file_name, "a") as file:
file.write(f"{index}: {chunk};{instr}\n")
if (int(index) % 4 == 3):
file.write("\n")
# appends a final "END;" string to the file
def end_file(file_name):
with open(file_name, "a") as file:
file.write("END;")
def negative_to_twos_complement(negative_binary):
abs_binary = negative_binary
ones_complement = ''.join('1' if bit == '0' else '0' for bit in abs_binary)
twos_complement = ''
carry = 1
for bit in reversed(ones_complement):
if carry == 1:
if bit == '0':
twos_complement = '1' + twos_complement
carry = 0
else:
twos_complement = '0' + twos_complement
carry = 1
else:
twos_complement = bit + twos_complement
return twos_complement
# converts a decimal value to a signed binary value (sign bit is the first bit)
def sbin(value):
binary = bin(int(value))
if (binary[0] == '-'):
return '1' + negative_to_twos_complement(binary[3:])
else:
return '0' + binary[2:]
# pads a binary value with 0s or 1s to a certain length (same as zfill() but extends the sign bit)
def sfill(value, length):
if (len(value) < length):
if (value[0] == '1'):
return (length - len(value)) * '1' + value
else:
return (length - len(value)) * '0' + value
else:
return value
def check_register(register):
if (register[0] != "x" or int(register[1:]) < 0 or int(register[1:]) > 31):
raise Exception("Invalid register.")
def check_instruction(instruction):
if (instruction not in INSTRUCTION):
raise Exception("Instruction not found.")
def check_immediate(immediate, length):
if (int(immediate) > 2**(length - 1) - 1 or int(immediate) < -2**(length - 1) + 1):
raise Exception("Immediate value out of range.")
# translates an instruction to binary (assembly to machine code)
def translate_instruction(instruction):
try:
instr = instruction.split(" ")[0]
check_instruction(instr)
opcode = INSTRUCTION[instr]["opcode"]
funct3 = INSTRUCTION[instr]["funct3"]
funct7 = INSTRUCTION[instr]["funct7"]
if (INSTRUCTION[instr]["format"] not in ["S", "B"]):
rd = instruction.split(" ")[1].split(",")[0]
check_register(rd)
rd = bin(int(rd[1:]))[2:].zfill(5)
if (INSTRUCTION[instr]["format"] == "U"):
imm = instruction.split(" ")[1].split(",")[1]
check_immediate(imm, 20)
imm = sfill(sbin(imm)[0:20], 20)
binary = imm + rd + opcode
elif (INSTRUCTION[instr]["format"] == "J"):
imm = instruction.split(" ")[1].split(",")[1]
check_immediate(imm, 20)
imm = sfill(sbin(imm)[0:20], 21)
imm = imm[::-1]
bit20 = imm[20]
bit10to1 = (imm[1:11])[::-1]
bit11 = imm[11]
bit19to12 = (imm[12:20])[::-1]
imm = sfill((bit20 + bit10to1 + bit11 + bit19to12), 20)
binary = imm + rd + opcode
elif (
INSTRUCTION[instr]["format"] == "I"
and instr not in ["lw", "lb", "lh", "lbu", "lhu", "slli", "srli", "srai"]):
rs1 = instruction.split(" ")[1].split(",")[1]
check_register(rs1)
rs1 = bin(int(rs1[1:]))[2:].zfill(5)
imm = instruction.split(" ")[1].split(",")[2]
check_immediate(imm, 12)
imm = sfill(sbin(imm)[0:12], 12)
binary = imm + rs1 + funct3 + rd + opcode
elif (INSTRUCTION[instr]["format"] == "B"):
rs1 = instruction.split(" ")[1].split(",")[0]
rs2 = instruction.split(" ")[1].split(",")[1]
check_register(rs1)
check_register(rs2)
rs1 = bin(int(rs1[1:]))[2:].zfill(5)
rs2 = bin(int(rs2[1:]))[2:].zfill(5)
imm = instruction.split(" ")[1].split(",")[2]
check_immediate(imm, 12)
imm = sfill(sbin(imm)[0:12], 13)
imm = imm[::-1]
bit12 = imm[12]
bit10to5 = (imm[5:11])[::-1]
bit4to1 = (imm[1:5])[::-1]
bit11 = imm[11]
binary = sfill((bit12 + bit10to5), 7) + rs2 + rs1 + funct3 + sfill(
(bit4to1 + bit11), 5) + opcode
elif (instr in ["lb", "lh", "lw", "lbu", "lhu"]):
rs1 = instruction.split(" ")[1].split(",")[1]
rs1 = rs1.split("(")[1].split(")")[0]
check_register(rs1)
rs1 = bin(int(rs1[1:]))[2:].zfill(5)
imm = instruction.split(" ")[1].split(",")[1]
imm = imm.split("(")[0]
check_immediate(imm, 12)
imm = sfill(sbin(imm)[0:12], 12)
binary = imm + rs1 + funct3 + rd + opcode
elif (INSTRUCTION[instr]["format"] == "S"):
rs2 = instruction.split(" ")[1].split(",")[0]
rs1 = instruction.split(" ")[1].split(",")[1]
rs1 = rs1.split("(")[1].split(")")[0]
check_register(rs1)
check_register(rs2)
rs2 = bin(int(rs2[1:]))[2:].zfill(5)
rs1 = bin(int(rs1[1:]))[2:].zfill(5)
imm = instruction.split(" ")[1].split(",")[1]
imm = imm.split("(")[0]
check_immediate(imm, 12)
imm = sfill(sbin(imm)[0:12], 12)
imm = imm[::-1]
bit11to5 = (imm[5:12])[::-1]
bit4to0 = (imm[0:5])[::-1]
binary = sfill(bit11to5, 7) + rs2 + rs1 + funct3 + sfill(bit4to0, 5) + opcode
elif (INSTRUCTION[instr]["format"] == "R"):
rs1 = instruction.split(" ")[1].split(",")[1]
rs2 = instruction.split(" ")[1].split(",")[2]
check_register(rs1)
check_register(rs2)
rs1 = bin(int(rs1[1:]))[2:].zfill(5)
rs2 = bin(int(rs2[1:]))[2:].zfill(5)
binary = funct7 + rs2 + rs1 + funct3 + rd + opcode
elif (instr in ["slli", "srli", "srai"]):
rs1 = instruction.split(" ")[1].split(",")[1]
check_register(rs1)
rs1 = bin(int(rs1[1:]))[2:].zfill(5)
shamt = instruction.split(" ")[1].split(",")[2]
shamt = sfill(sbin(shamt)[0:6], 5)
binary = funct7 + shamt + rs1 + funct3 + rd + opcode
except Exception as e:
print(f"Error translating instruction '{instruction.rstrip()}': {e}")
return None
return binary
def main():
instructions = read_file("instructions.txt")
create_file("instruction.mif")
for i, instruction in enumerate(instructions):
binary = translate_instruction(instruction)
if binary:
chunks = [
binary[j:j + BITS_IN_CHUNK] for j in range(0, len(binary), BITS_IN_CHUNK)
]
chunks = chunks[::-1]
for j, chunk in enumerate(chunks):
index = f"{i * 4 + j:03d}"
write_instruction("instruction.mif", index, chunk, instruction)
else:
line = i + 1
print(f"Translation failed on line {line}.\n")
os.remove("instruction.mif")
exit(2)
end_file("instruction.mif")
print("Assembly to machine code translation complete.\n")
if __name__ == "__main__":
main()