implemented instructions beq, bne, blt, bge, bltu, bgeu, improved unsigned number handling

float_support
Anton Lydike 4 years ago
parent 88c0b77a16
commit 0aa42d0d1c

@ -178,10 +178,20 @@ class CPU:
INS_NOT_IMPLEMENTED(ins) INS_NOT_IMPLEMENTED(ins)
def instruction_beq(self, ins: 'LoadedInstruction'): def instruction_beq(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) ASSERT_LEN(ins.args, 3)
reg1 = ins.get_reg(0)
reg2 = ins.get_reg(1)
dest = ins.get_imm(2)
if self.regs.get(reg1) == self.regs.get(reg2):
self.pc = dest
def instruction_bne(self, ins: 'LoadedInstruction'): def instruction_bne(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) ASSERT_LEN(ins.args, 3)
reg1 = ins.get_reg(0)
reg2 = ins.get_reg(1)
dest = ins.get_imm(2)
if self.regs.get(reg1) != self.regs.get(reg2):
self.pc = dest
def instruction_blt(self, ins: 'LoadedInstruction'): def instruction_blt(self, ins: 'LoadedInstruction'):
ASSERT_LEN(ins.args, 3) ASSERT_LEN(ins.args, 3)
@ -192,13 +202,28 @@ class CPU:
self.pc = dest self.pc = dest
def instruction_bge(self, ins: 'LoadedInstruction'): def instruction_bge(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) ASSERT_LEN(ins.args, 3)
reg1 = ins.get_reg(0)
reg2 = ins.get_reg(1)
dest = ins.get_imm(2)
if self.regs.get(reg1) >= self.regs.get(reg2):
self.pc = dest
def instruction_bltu(self, ins: 'LoadedInstruction'): def instruction_bltu(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) ASSERT_LEN(ins.args, 3)
reg1 = to_unsigned(ins.get_reg(0))
reg2 = to_unsigned(ins.get_reg(1))
dest = ins.get_imm(2)
if self.regs.get(reg1) < self.regs.get(reg2):
self.pc = dest
def instruction_bgeu(self, ins: 'LoadedInstruction'): def instruction_bgeu(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) ASSERT_LEN(ins.args, 3)
reg1 = to_unsigned(ins.get_reg(0))
reg2 = to_unsigned(ins.get_reg(1))
dest = ins.get_imm(2)
if self.regs.get(reg1) >= self.regs.get(reg2):
self.pc = dest
def instruction_j(self, ins: 'LoadedInstruction'): def instruction_j(self, ins: 'LoadedInstruction'):
INS_NOT_IMPLEMENTED(ins) INS_NOT_IMPLEMENTED(ins)

@ -35,11 +35,22 @@ def int_from_bytes(bytes, unsigned=False):
for b in bytes: for b in bytes:
num = num << 8 num = num << 8
num += b num += b
sign = num >> (len(bytes) * 8 - 1)
if sign and not unsigned: if unsigned:
return num - 2 ** (8 * len(bytes))
return num return num
return to_signed(num)
def to_unsigned(num: int, bytes=4):
if num < 0:
return 2**(bytes * 8) - num
def to_signed(num: int, bytes=4):
if num >> (bytes * 8 - 1):
return num - 2 ** (8 * bytes)
# Colors # Colors

Loading…
Cancel
Save