-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_assembler_test.py
More file actions
446 lines (387 loc) · 19.8 KB
/
Copy pathbase_assembler_test.py
File metadata and controls
446 lines (387 loc) · 19.8 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
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
"""
Tests for the CSSE232 Risc-V Assembler
Written by: R Williamson, 2024
"""
import assembler
import unittest
#This is only used by the autograder
#from gradescope_utils.autograder_utils.decorators import weight
def split_inst(inst):
#helper to make writing tests cleaner
inst = inst.replace(",","")
sinst = inst.split()
return(sinst[0], sinst[1:])
def Assembler_method_testing_helper(myassert, func, inst, machine):
#helper to make writing individual assembler test cases easier
#checks the output matches expectation
cmd, args = split_inst(inst)
result = func(cmd, args, 0)
myassert(machine, result)
def Assembler_method_error_testing_helper(myassert, error, func, inst):
#helper to make writing individual assembler test cases easier
#checks that an appropriate exception is raised
cmd, args = split_inst(inst)
with myassert(error):
func(cmd, args, 0)
class TestRType(unittest.TestCase):
#@weight(1)
def test_R_types_add(self):
inst = "add x1, x1, x1"
machine = "0000 0000 0001 0000 1000 0000 1011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
inst = "add t0, s0, sp"
machine = "0000 0000 0010 0100 0000 0010 1011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_sub(self):
inst = "sub x5, x16, x31"
machine = "0100 0001 1111 1000 0000 0010 1011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
inst = "sub s0, zero, a0"
machine = "0100 0000 1010 0000 0000 0100 0011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_xor(self):
inst = "xor x3, x0, at"
machine = "0000 0001 1111 0000 0100 0001 1011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_or(self):
inst = "or x4, s1, s2"
machine = "0000 0001 0010 0100 1110 0010 0011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_and(self):
inst = "and t5, t5, s11"
machine = "0000 0001 1011 1111 0111 1111 0011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_sll(self):
inst = "sll x7, a1, a2"
machine = "0000 0000 1100 0101 1001 0011 1011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_sra(self):
inst = "sra a7, s9, x31 "
machine = "0100 0001 1111 1100 1101 1000 1011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_slt(self):
inst = "slt a4, a5, a6 "
machine = "0000 0001 0000 0111 1010 0111 0011 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_R_Type, inst, machine)
#@weight(1)
def test_R_types_arguments(self):
#too many arguments
inst = "add x1, x1, x1, x2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_R_Type, inst)
#too few arguments
inst = "add x1, x1"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_R_Type, inst)
#wrong argument format
inst = "add x1, x1(x1)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_R_Type, inst)
#@weight(1)
def test_R_types_registers(self):
#testing bad register names
inst = "add z1, x1, x1"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_R_Type, inst)
inst = "or x1, sid, x1"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_R_Type, inst)
inst = "sll x1, x1, t22"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_R_Type, inst)
#testing accidental immediate
inst = "add x1, x1, 4"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_R_Type, inst)
inst = "add x1, x1, LABEL"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_R_Type, inst)
class TestIType(unittest.TestCase):
#@weight(1)
def test_I_types_addi(self):
inst = "addi t5, s0, 2"
machine = "0000 0000 0010 0100 0000 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
inst = "addi t5, s0, -2"
machine = "1111 1111 1110 0100 0000 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
inst = "addi t5, s0, 256"
machine = "0001 0000 0000 0100 0000 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
inst = "addi t5, s0, -256"
machine = "1111 0000 0000 0100 0000 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
#@weight(1)
def test_I_types_xori(self):
inst = "xori t5, s0, 2"
machine = "0000 0000 0010 0100 0100 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
inst = "xori t5, s0, -2"
machine = "1111 1111 1110 0100 0100 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
#@weight(1)
def test_I_types_ori(self):
inst = "ori t5, s0, 2"
machine = "0000 0000 0010 0100 0110 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
inst = "ori t5, s0, -2"
machine = "1111 1111 1110 0100 0110 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
#@weight(1)
def test_I_types_andi(self):
inst = "andi t5, s0, 2"
machine = "0000 0000 0010 0100 0111 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
inst = "andi t5, s0, -2"
machine = "1111 1111 1110 0100 0111 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
#@weight(1)
def test_I_types_slli(self):
inst = "slli t5, s0, 2"
machine = "0000 0000 0010 0100 0001 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_shift, inst, machine)
inst = "slli t5, s0, -2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_shift, inst)
#@weight(1)
def test_I_types_srli(self):
inst = "srli t5, s0, 2"
machine = "0000 0000 0010 0100 0101 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_shift, inst, machine)
inst = "srli t5, s0, -2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_shift, inst)
#@weight(1)
def test_I_types_srai(self):
inst = "srai t5, s0, 2"
machine = "0100 0000 0010 0100 0101 1111 0001 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_shift, inst, machine)
inst = "srai t5, s0, -2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_shift, inst)
#@weight(1)
def test_I_types_arguments(self):
#testing bad number of args
inst = "addi t5, s0"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_I_Type, inst)
inst = "addi t5, 4"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_I_Type, inst)
inst = "addi t5, s0, 4, t0"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_I_Type, inst)
inst = "addi t5, s0, 4, 4"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_I_Type, inst)
inst = "addi t5, s0, t4, t0"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadOperands, assembler.Assemble_I_Type, inst)
#testing bad register names
inst = "addi z1, x1, 2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_I_Type, inst)
inst = "ori x1, sid, 2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_I_Type, inst)
inst = "slli x1, a10, 2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_I_Type_shift, inst)
#@weight(1)
def test_I_types_immediates(self):
#testing bad immediates
inst = "addi t5, s0, 4097"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type, inst)
inst = "slli t5, s0, 33"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_shift, inst)
#@weight(1)
def test_I_types_bad_instructions(self):
#testing for instructions that shouldnt exist
inst = "subi t5, s0, 2"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadInstruction, assembler.Assemble_I_Type, inst)
class TestBaseOffsetInstructions(unittest.TestCase):
#@weight(1)
def test_lw(self):
inst = "lw a0, 0(sp)"
machine = "0000 0000 0000 0001 0010 0101 0000 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_base_offset, inst, machine)
inst = "lw a0, -4(sp)"
machine = "1111 1111 1100 0001 0010 0101 0000 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_base_offset, inst, machine)
#@weight(1)
def test_sw(self):
inst = "sw a0, 0(sp)"
machine = "0000 0000 1010 0001 0010 0000 0010 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_S_Type, inst, machine)
inst = "sw a0, -4(sp)"
machine = "1111 1110 1010 0001 0010 1110 0010 0011"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_S_Type, inst, machine)
#@weight(1)
def test_jalr(self):
inst = "jalr ra, 4(a0)"
machine = "0000 0000 0100 0101 0000 0000 1110 0111"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_base_offset, inst, machine)
inst = "jalr ra, 4 (a0)"
machine = "0000 0000 0100 0101 0000 0000 1110 0111"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type_base_offset, inst, machine)
inst = "jalr ra, a0, 4"
machine = "0000 0000 0100 0101 0000 0000 1110 0111"
Assembler_method_testing_helper(self.assertEqual, assembler.Assemble_I_Type, inst, machine)
#@weight(1)
def test_I_types_bad_instructions(self):
#testing lw for errors
inst = "lw a0, a0(sp)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_base_offset, inst)
inst = "lw a0, 4(4)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_I_Type_base_offset, inst)
inst = "lw at, 4097(t5)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_base_offset, inst)
#testing jalr for errors
inst = "jalr a0, a0(sp)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_base_offset, inst)
inst = "jalr a0, 4(4)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_I_Type_base_offset, inst)
inst = "jalr at, 4097(t5)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_I_Type_base_offset, inst)
#testing sw for errors
inst = "sw a0, a0(sp)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_S_Type, inst)
inst = "sw a0, 4(4)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadRegister, assembler.Assemble_S_Type, inst)
inst = "sw at, 4097(t5)"
Assembler_method_error_testing_helper(self.assertRaises, assembler.BadImmediate, assembler.Assemble_S_Type, inst)
class TestFileAssembly(unittest.TestCase):
#@weight(1)
def test_remove_comments(self):
instructions = """add x1, x1, x1 ; end of line comment
;comment line
add t0, s0, sp
sub x5, x16, x31"""
instructions = instructions.split("\n")
cleaned = """add x1, x1, x1
add t0, s0, sp
sub x5, x16, x31"""
#clean up the whitespace and make it a list
cleaned = [x.lstrip().rstrip() for x in cleaned.split("\n")]
result = assembler.comments_pass(instructions)
#check number of lines is right
self.assertEqual(len(cleaned), len(result))
#check each line is right
for m,a in zip(cleaned, result):
self.assertEqual(m.rstrip(), a.rstrip())
#@weight(2)
def test1(self):
#Mix of R types
instructions = """add x1, x1, x1
add t0, s0, sp
sub x5, x16, x31
sub s0, zero, a0
xor x3, x0, at
or x4, s1, s2"""
instructions = instructions.split("\n")
machine = """0000 0000 0001 0000 1000 0000 1011 0011
0000 0000 0010 0100 0000 0010 1011 0011
0100 0001 1111 1000 0000 0010 1011 0011
0100 0000 1010 0000 0000 0100 0011 0011
0000 0001 1111 0000 0100 0001 1011 0011
0000 0001 0010 0100 1110 0010 0011 0011"""
#clean up the whitespace and make it a list
machine = [x.lstrip().rstrip() for x in machine.split("\n")]
assembled = assembler.assemble_asm(instructions)
#check number of instructions is right
self.assertEqual(len(machine), len(assembled))
#check each instruction is right
for m,a in zip(machine, assembled):
self.assertEqual(m, a)
#@weight(2)
def test2(self):
#Mix of I types
instructions = """addi t5, s0, 279
andi t5, s0, 2
addi t5, s0, -7
xori t5, s0, 200
ori t5, s0, -289"""
instructions = instructions.split("\n")
machine = """0001 0001 0111 0100 0000 1111 0001 0011
0000 0000 0010 0100 0111 1111 0001 0011
1111 1111 1001 0100 0000 1111 0001 0011
0000 1100 1000 0100 0100 1111 0001 0011
1110 1101 1111 0100 0110 1111 0001 0011"""
#clean up the whitespace and make it a list
machine = [x.lstrip().rstrip() for x in machine.split("\n")]
assembled = assembler.assemble_asm(instructions)
#check number of instructions is right
self.assertEqual(len(machine), len(assembled))
#check each instruction is right
for m,a in zip(machine, assembled):
self.assertEqual(m, a)
#@weight(2)
def test3(self):
#Mix of R and I types
instructions = """xor x3, x0, at
addi t5, s0, 27
sub x5, x16, x31
andi t5, s0, 2
addi t5, s0, -7
add t0, s0, sp
xori t5, s0, 2004"""
instructions = instructions.split("\n")
machine = """0000 0001 1111 0000 0100 0001 1011 0011
0000 0001 1011 0100 0000 1111 0001 0011
0100 0001 1111 1000 0000 0010 1011 0011
0000 0000 0010 0100 0111 1111 0001 0011
1111 1111 1001 0100 0000 1111 0001 0011
0000 0000 0010 0100 0000 0010 1011 0011
0111 1101 0100 0100 0100 1111 0001 0011"""
#clean up the whitespace and make it a list
machine = [x.lstrip().rstrip() for x in machine.split("\n")]
assembled = assembler.assemble_asm(instructions)
#check number of instructions is right
self.assertEqual(len(machine), len(assembled))
#check each instruction is right
for m,a in zip(machine, assembled):
self.assertEqual(m, a)
#@weight(2)
def test4(self):
#Mix of base off-set
instructions = """lw a0, 0(sp)
sw a0, 0(sp)
lw a0, -4(sp)
jalr ra, 4(a0)
sw a0, -4(sp)
jalr ra, a0, 4"""
instructions = instructions.split("\n")
machine = """0000 0000 0000 0001 0010 0101 0000 0011
0000 0000 1010 0001 0010 0000 0010 0011
1111 1111 1100 0001 0010 0101 0000 0011
0000 0000 0100 0101 0000 0000 1110 0111
1111 1110 1010 0001 0010 1110 0010 0011
0000 0000 0100 0101 0000 0000 1110 0111"""
#clean up the whitespace and make it a list
machine = [x.lstrip().rstrip() for x in machine.split("\n")]
assembled = assembler.assemble_asm(instructions)
#check number of instructions is right
self.assertEqual(len(machine), len(assembled))
#check each instruction is right
for m,a in zip(machine, assembled):
self.assertEqual(m, a)
#@weight(2)
def test5(self):
#Mix of R, I, and base-offset
instructions = """xor x3, x0, at
addi t5, s0, 27
sub x5, x16, x3
lw a0, -4(sp)
andi t5, s0, 2
addi t5, s0, -7
add t0, s0, sp
xori t5, s0, 2004
jalr ra, a0, 4
sw a0, 0(sp)"""
instructions = instructions.split("\n")
machine = """0000 0001 1111 0000 0100 0001 1011 0011
0000 0001 1011 0100 0000 1111 0001 0011
0100 0000 0011 1000 0000 0010 1011 0011
1111 1111 1100 0001 0010 0101 0000 0011
0000 0000 0010 0100 0111 1111 0001 0011
1111 1111 1001 0100 0000 1111 0001 0011
0000 0000 0010 0100 0000 0010 1011 0011
0111 1101 0100 0100 0100 1111 0001 0011
0000 0000 0100 0101 0000 0000 1110 0111
0000 0000 1010 0001 0010 0000 0010 0011"""
#clean up the whitespace and make it a list
machine = [x.lstrip().rstrip() for x in machine.split("\n")]
assembled = assembler.assemble_asm(instructions)
#check number of instructions is right
self.assertEqual(len(machine), len(assembled))
#check each instruction is right
for m,a in zip(machine, assembled):
self.assertEqual(m, a)