Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/riscv_assembler/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ def write_to_file(output : list, file : str) -> None:
raise NotImplementedError()

def mod(self, output : list) -> list:
print('inside mod:', output)
if self.__nibble_mode:
output = AssemblyConverter.apply_nibble(output)
elif self.__hex_mode:
print('here we convert to hex', output)
output = AssemblyConverter.apply_hex(output)
return output

Expand Down
38 changes: 24 additions & 14 deletions src/riscv_assembler/instr_arr.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ def immediate(imm, n):

return mod_imm, mod_imm_2'''
mod_imm = format(((1 << 12) - 1) & int(imm), '012b')
if n == 1:
return mod_imm[0] + mod_imm[12-10 : 12-4]
return mod_imm[12-4 : 12 - 0] + mod_imm[1]
if n == 1: return mod_imm[0:7]
else: return mod_imm[7:12]


class _SB(Instruction):
Expand Down Expand Up @@ -139,7 +138,6 @@ def __str__(self):
def compute_instr(self, instr, imm, rd):
instr = super().check_instr_valid(instr, U_instr)
opcode = 0

return "".join([
_U.immediate(imm),
super().reg(rd),
Expand All @@ -148,7 +146,10 @@ def compute_instr(self, instr, imm, rd):

@staticmethod
def immediate(imm):
return format(int(imm) >> 12, '013b')
# return format(int(imm) >> 12, '013b')
high_20 = int(imm)
mod_imm = format(((1 << 20) - 1) & high_20, '020b')
return mod_imm

class _UJ(Instruction):
def __repr__(self):
Expand Down Expand Up @@ -219,13 +220,19 @@ def organize(self, tokens):
line_num, code = tokens[-2], tokens[-1]
instr, rs1, imm, rd = tokens[0], None, None, None
if instr == "jalr":
if len(tokens) == 4+2:
rs1, imm, rd = reg_map[tokens[2]], super().JUMP(tokens[3], line_num, code), reg_map[tokens[1]]
else:
rs1, imm, rd = reg_map[tokens[1]], 0, reg_map["x1"]
if tokens[3][0] =='x':
rs1, imm, rd = reg_map[tokens[3]], tokens[2], reg_map[tokens[1]] # use for jalr rd, offset(rs1)
else:
rs1, imm, rd = reg_map[tokens[2]], tokens[3], reg_map[tokens[1]] # normal jalr rs1, rs2, offset
elif instr == "lw":
rs1, imm, rd = reg_map[tokens[3]], tokens[2], reg_map[tokens[1]]
elif instr == 'ld':
elif instr == 'lh':
rs1, imm, rd = reg_map[tokens[3]], tokens[2], reg_map[tokens[1]]
elif instr == 'lb':
rs1, imm, rd = reg_map[tokens[3]], tokens[2], reg_map[tokens[1]]
elif instr == 'lbu':
rs1, imm, rd = reg_map[tokens[3]], tokens[2], reg_map[tokens[1]]
elif instr == 'lhu':
rs1, imm, rd = reg_map[tokens[3]], tokens[2], reg_map[tokens[1]]
else:
rs1, imm, rd = reg_map[tokens[2]], tokens[3], reg_map[tokens[1]]
Expand Down Expand Up @@ -266,7 +273,8 @@ def __str__(self):
return "U Parser"

def organize(self, tokens):
instr, imm, rd = tokens[0], tokens[1], reg_map[tokens[2]]
print('in U type:', tokens[0], tokens[1], tokens[2])
instr, rd, imm = tokens[0], reg_map[tokens[1]], tokens[2]
return U(instr, imm, rd)

class _UJ_parse(InstructionParser):
Expand All @@ -280,10 +288,13 @@ def __str__(self):
def organize(self, tokens):
line_num, code = tokens[-2], tokens[-1]
instr, imm, rd = tokens[0], None, None
print('see token len', len(tokens), 'and jal get:',tokens[0], tokens[1], tokens[2])
if len(tokens) == 3:
print('here')
imm, rd = super().JUMP(tokens[2], line_num, code), reg_map[tokens[1]]
else:
imm, rd = super().JUMP(tokens[1], line_num, code), reg_map["x1"]
print('there????')
imm, rd = super().JUMP(tokens[1], line_num, code), reg_map[tokens[2]]

return UJ(instr, imm, rd)

Expand Down Expand Up @@ -366,8 +377,7 @@ def instruction_map():
"remu"
]
I_instr = [
"addi", "lb", "lw",
"ld", "lbu", "lhu",
"addi", "lb", "lw", "lh", "lbu", "lhu",
"lwu", "fence", "fence.i",
"slli", "slti", "sltiu",
"xori", "slri", "srai",
Expand Down
7 changes: 5 additions & 2 deletions src/riscv_assembler/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def handle_inline_comments(x : str) -> str:

@staticmethod
def handle_specific_instr(x : list) -> list:
# for sw, lw, lb, lh, sb, sh
if len(x[0]) == 2 and (x[0] in S_instr or x[0] in I_instr):
# for sw, lw, lb, lh, sb, sh weird here. It should be rewrited clearer
if x[0]=='sw' or x[0]=='sh' or x[0]=='sb' or x[0] == 'lw' or x[0] == 'lh' or x[0] == 'lb' or x[0] == 'lhu' or x[0] == 'lbu' or (x[0]=='jalr' and len(x)==3): # op rd, offset(rs1)
y = x[-1].split('('); y[1] = y[1].replace(')','')
print('trigger special instr~~~', x[0])
return x[0:-1] + y
elif 'requires jump' == 5:
...
Expand Down Expand Up @@ -91,6 +92,7 @@ def interpret_arr(code : list) -> list:
for line_num, line in enumerate(code):
tokens = _Parser.tokenize(line, line_num, code)
int_code += [_Parser.interpret(tokens) for _ in range(1) if len(tokens) != 0]
print(int_code)

return int_code

Expand Down Expand Up @@ -124,6 +126,7 @@ def determine_type(tk : str) -> function:
parsers = [Rp, Ip, Sp, SBp, Up ,UJp, Psp]
for i in range(len(instr_sets)):
if tk in instr_sets[i]:
print('detected instr:', tk)
return parsers[i]
raise Exception("Bad Instruction Provided: " + tk + "!")

Expand Down
8 changes: 7 additions & 1 deletion tests/assembly/test3.s
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
sw s0, 0(sp)
sw s0, 0(sp)
sw x5, 100(x6)
sw x5, -300(x6)
sw x5, -1(x6)
sw x5, 2047(x6)
jalr x5, x6, 4
addi x5, x6, 4
2 changes: 1 addition & 1 deletion tests/assembly/test8.s
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ loop_end:
lw s4, 16(sp)
lw s5, 20(sp)
addi, sp, sp, 24
#ret
#ret
7 changes: 1 addition & 6 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def SUITE():
results[0] += [cnv(path)] #**
# 2.
cnv.output_mode = 'p'
print('Printing Output')
results[1] += [cnv(path)]
# 3.
cnv.output_mode = 'f'
Expand All @@ -53,7 +52,6 @@ def SUITE():
results[4] += [cnv(str_code)] #**
# 6.
cnv.output_mode = 'p'
print('Printing Output')
results[5] += [cnv(str_code)]
# 7.
cnv.output_mode = 'f'
Expand Down Expand Up @@ -82,15 +80,14 @@ def SUITE():
results[13] += ['arr{}.bin'.format(i)] #**
results[14] += [cnv.output_mode]


return results

ans = ['0xfe810113','0x00812023','0x00912223','0x01212423','0x01312623','0x01412823','0x01512a23','0x00100f13','0x01e64863','0x01e6ca63','0x01e74863','0x0140006f','0x00500593','0xfcdff06f','0x00600593','0xfc5ff06f','0x00000413','0x00000293','0x00000313','0x00000393','0x00400e13','0x03c68a33','0x03c70ab3','0x0040006f','0x02c28263','0x00052483','0x0005a903','0x01450533','0x015585b3','0x032489b3','0x01340433','0x00128293','0xfe1ff06f','0x00040513','0x00012403','0x00412483','0x00812903','0x00c12983','0x01012a03','0x01412a83','0x01810113']
ANSWERS = {
0: [['0x000000b3'], None, 'file0.txt', 'file0.bin', ['0x000000b3'], None, 'str0.txt', 'str0.bin', ['0x000000b3'], 'a', None, 'p', 'arr0.txt', 'arr0.bin', 'f'],
1: [['0x02040293'], None, 'file1.txt', 'file1.bin',['0x02040293'], None, 'str1.txt', 'str1.bin',['0x02040293'], 'a', None, 'p', 'arr1.txt', 'arr1.bin', 'f'],
2: [['0x00a00413', '0x00a00493', '0x00848263', '0xfe040493'], None, 'file2.txt', 'file2.bin', ['0x00a00413', '0x00a00493', '0x00848263', '0xfe040493'], None, 'str2.txt', 'str2.bin', ['0x00a00413', '0x00a00493', '0x00848263', '0xfe040493'], 'a', None, 'p', 'arr2.txt', 'arr2.bin', 'f'],
3: [['0x00812023'], None, 'file3.txt', 'file3.bin', ['0x00812023'], None, 'str3.txt', 'str3.bin', ['0x00812023'], 'a', None, 'p', 'arr3.txt', 'arr3.bin', 'f'],
3: [['0x00812023', '0x06532223', '0xec532a23','0xfe532fa3','0x7e532fa3','0x004302e7','0x00430293'], None, 'file3.txt', 'file3.bin', ['0x00812023', '0x06532223', '0xec532a23','0xfe532fa3','0x7e532fa3','0x004302e7','0x00430293'], None, 'str3.txt', 'str3.bin', ['0x00812023', '0x06532223', '0xec532a23','0xfe532fa3', '0x7e532fa3','0x004302e7','0x00430293'], 'a', None, 'p', 'arr3.txt', 'arr3.bin', 'f'],
4: [['0x000000b3', '0x02040293', '0x02040293','0x00812023'], None, 'file4.txt', 'file4.bin',['0x000000b3', '0x02040293', '0x02040293','0x00812023'], None, 'str4.txt', 'str4.bin',['0x000000b3', '0x02040293', '0x02040293','0x00812023'], 'a', None, 'p', 'arr4.txt', 'arr4.bin', 'f'],
5: [['0x00a00413','0x00a00493','0xfff00493','0x00048463','0xfe000ce3','0xfe040493'], None, 'file5.txt', 'file5.bin', ['0x00a00413','0x00a00493','0xfff00493','0x00048463','0xfe000ce3','0xfe040493'], None, 'str5.txt', 'str5.bin', ['0x00a00413','0x00a00493','0xfff00493','0x00048463','0xfe000ce3','0xfe040493'], 'a', None, 'p', 'arr5.txt', 'arr5.bin', 'f'],
6: [['0x00a00093','0xfec00113','0x00000663','0x00123023','0x00023083','0xfff00093','0x00123023','0x00023083','0xfe02c6e3'], None, 'file6.txt', 'file6.bin', ['0x00a00093','0xfec00113','0x00000663','0x00123023','0x00023083','0xfff00093','0x00123023','0x00023083','0xfe02c6e3'], None, 'str6.txt', 'str6.bin', ['0x00a00093','0xfec00113','0x00000663','0x00123023','0x00023083','0xfff00093','0x00123023','0x00023083','0xfe02c6e3'], 'a', None, 'p', 'arr6.txt', 'arr6.bin', 'f'],
Expand All @@ -101,12 +98,10 @@ def SUITE():

def error_label(q, test):
return "Question {q} Failed for Test {test}".format(q = q, test = test)

test_data = []
for q in range(num_questions):
for t in range(num_test_files):
test_data += [(q, t)]

@pytest.mark.parametrize("q, test", test_data)
def test_compute(q: int, test: int):
assert RESULTS[q][test] == ANSWERS[test][q], error_label(q, test)