Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues fix #18

Closed
wants to merge 2 commits into from
Closed
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
49 changes: 30 additions & 19 deletions riscv_assembler/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,25 @@ def __reg_to_bin(self,x):

#for jumps, calculates hex address of func
def calcJump(self, x,line_num):
#calc line number of func
for i in range(len(self.code)):
# search forward
skip_labels = 0
for i in range(line_num, len(self.code)):
if x+":" == self.code[i]:
jump_size = (i - line_num - skip_labels) * 4 # how many instructions to jump ahead
return jump_size
if self.code[i].endswith(':'):
skip_labels += 1

# search backward
skip_labels = 0
for i in range(line_num, -1, -1):
# substruct correct label itself
if self.code[i].endswith(':'):
skip_labels += 1
if x+":" == self.code[i]:
return (i - line_num)*4 #how many instructions to jump ahead/behind
jump_size = (i - line_num + skip_labels) * 4 # how many instructions to jump behind
return jump_size

#print("Address not found")
return -10 #if not found

Expand Down Expand Up @@ -272,29 +287,27 @@ def S_type(
])

def SB_type(
self, instr, rs1,
self, instr, rs1,
rs2, imm):

if instr not in self.SB_instr:
raise WrongInstructionType()

opcode = 0;f3 = 1;f7 = 2
opcode = 0
f3 = 1

mod_imm = (int(imm) - ((int(imm) >> 12) << 12)) >> 6 # imm[12]
mod_imm += (int(imm) - ((int(imm) >> 11) >> 11)) >> 5 # imm[12|10:5]
mod_imm_2 = (int(imm) - ((int(imm) >> 5) << 5)) # imm[4:1]
mod_imm_2 += (int(imm) - ((int(imm) >> 11) << 11)) >> 10 # imm[4:1|11]
imm = format(((1 << 13) - 1) & imm, '013b') # represent `imm` as 13-bit in two's complement form

return "".join([
#"".join([
# self.__binary(int(imm),13)[::-1][12][::-1],
# self.__binary(int(imm),13)[::-1][5:11][::-1]
#]),
self.__binary(mod_imm,7),
imm[0], imm[12-10 : 12-4],
self.__reg_to_bin(rs2),
self.__reg_to_bin(rs1),
self.instr_data[instr][f3],
self.__binary(mod_imm_2,5),
imm[12-4 : 12-0], imm[1],
#"".join([
# self.__binary(int(imm),13)[::-1][1:5][::-1],
# self.__binary(int(imm),13)[::-1][11][::-1]
Expand All @@ -320,25 +333,23 @@ def U_type(
])

def UJ_type(
self, instr,
self, instr,
imm, rd):

if instr not in self.UJ_instr:
raise WrongInstructionType()

opcode = 0;f3 = 1;f7 = 2
opcode = 0

imm = format(((1 << 21) - 1) & imm, '021b') # represent `imm` as 21-bit in two's complement form

mod_imm = ((int(imm) - ((int(imm) >> 20) << 20)) >> 19) << 19 # imm[20]
mod_imm += (int(imm) - ((int(imm) >> 10) << 10)) >> 1 # imm[20|10:1]
mod_imm += (int(imm) - ((int(imm) >> 11) << 11)) >> 10 # imm[20|10:1|11]
mod_imm += (int(imm) - ((int(imm) >> 19) << 19)) >> 12 # imm[20|10:1|11|19:12]
return "".join([
#"".join([
# self.__binary(int(imm),21)[::-1][20][::-1], self.__binary(int(imm),21)[::-1][1:11][::-1],
# self.__binary(int(imm),21)[::-1][11][::-1],
# self.__binary(int(imm),21)[::-1][12:20][::-1]
#]),
self.__binary(mod_imm,20),
#]),
imm[0], imm[20-10 : 20-0], imm[9], imm[20-19 : 20-11],
self.__reg_to_bin(rd),
self.instr_data[instr][opcode]
])
Expand Down