Example #1
0
def test():
    state = State([], [])
    state.set_register("$t1", 1)
    state.set_register("$t2", 2)
    add = ADD("$t0", "$t1", "$t2")
    add.execute(state)
    print state.register("$t0")
Example #2
0
    def setUp(self):
        self.state = State([], [])
        self.branch_state = State(["x", "y", "z", "BLAH", "LOOP"], [])

        self.t0 = 0
        self.t1 = 434
        self.t2 = 0

        self.state.set_register("$t0", self.t0)
        self.state.set_register("$t1", self.t1)
        self.state.set_register("$t2", self.t2)

        self.branch_state.set_register("$t0", self.t0)
        self.branch_state.set_register("$t1", self.t1)
        self.branch_state.set_register("$t2", self.t2)
Example #3
0
 def setUp(self):
     self.state = State([], [])
     self.t0 = 0
     self.t1 = 10
     self.t2 = 88
     self.t3 = max_int
     self.t4 = 22
     self.state.set_register("$t0", self.t0)
     self.state.set_register("$t1", self.t1)
     self.state.set_register("$t2", self.t2)
     self.state.set_register("$t3", self.t3)
     self.state.set_register("$t4", self.t4)
Example #4
0
 def setUp(self):
     self.state = State(["a", "b", "c", "x", "y", "z"], [])
     self.state.memory.extend(range(0, 100))
Example #5
0
class TestGeneralOperations(unittest.TestCase):
    def setUp(self):
        self.state = State(["a", "b", "c", "x", "y", "z"], [])
        self.state.memory.extend(range(0, 100))

    def testJUMP(self):
        op = JUMP("x")
        op.execute(self.state)
        result = self.state.program_counter()
        self.assertEqual(result, 3)

    def testJAL(self):
        prior_val = self.state.program_counter()
        op = JAL("c")
        op.execute(self.state)
        program_counter = self.state.program_counter()
        return_register = self.state.register("$ra")
        self.assertEqual(program_counter, 2)
        self.assertEqual(return_register, prior_val)

    def testJR(self):
        self.state.set_register("$ra", 44)
        op = JR("$ra")
        op.execute(self.state)
        self.assertEqual(self.state.program_counter(), 44)

    def testMFHI(self):
        self.state.set_register("$hi", 22)
        op = MFHI("$t0")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 22)

    def testMFLO(self):
        self.state.set_register("$lo", 88)
        op = MFLO("$t0")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 88)

    def testLW(self):
        op = LW("$t0", (1, "$zero"))
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 1)

    def testSW(self):
        self.state.set_register("$t0", 22)
        op = SW("$t0", (2, "$zero"))
        op.execute(self.state)
        result = self.state.memory[2]
        self.assertEqual(result, 22)
Example #6
0
class TestMathOperations(unittest.TestCase):
    def setUp(self):
        self.state = State([], [])
        self.t0 = 0
        self.t1 = 10
        self.t2 = 88
        self.t3 = max_int
        self.t4 = 22
        self.state.set_register("$t0", self.t0)
        self.state.set_register("$t1", self.t1)
        self.state.set_register("$t2", self.t2)
        self.state.set_register("$t3", self.t3)
        self.state.set_register("$t4", self.t4)

    def testADD(self):
        op = ADD("$t0", "$t1", "$t2")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, self.t1 + self.t2)

    def testSUB(self):
        op = SUB("$t0", "$t1", "$t2")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, self.t1 - self.t2)

    def testADDU(self):
        op = ADDU("$t0", "$t3", "$t4")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, -2147483627)

    def testADDI(self):
        op = ADDI("$t0", "$t1", 22)
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, self.t1 + 22)

    def testADDIU(self):
        op = ADDIU("$t0", "$t3", 22)
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, -2147483627)

    def testSUBU(self):
        op = SUBU("$t0", "$t4", "$t3")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, -2147483625)

    def testDIV(self):
        op = DIV("$t1", "$t2")
        op.execute(self.state)
        hi = self.state.register("$hi")
        lo = self.state.register("$lo")
        self.assertEqual(hi, self.t1 % self.t2)
        self.assertEqual(lo, self.t1 / self.t2)

    def testDIVU(self):
        op = DIVU("$t1", "$t2")
        op.execute(self.state)
        hi = self.state.register("$hi")
        lo = self.state.register("$lo")
        self.assertEqual(hi, self.t1 % self.t2)
        self.assertEqual(lo, self.t1 / self.t2)

    def testMULT(self):
        op = MULT("$t1", "$t2")
        op.execute(self.state)
        hi = self.state.register("$hi")
        lo = self.state.register("$lo")
        self.assertEqual(hi, 0)
        self.assertEqual(lo, self.t1 * self.t2)
Example #7
0
class TestBoolOperations(unittest.TestCase):
    def setUp(self):
        self.state = State([], [])
        self.branch_state = State(["x", "y", "z", "BLAH", "LOOP"], [])

        self.t0 = 0
        self.t1 = 434
        self.t2 = 0

        self.state.set_register("$t0", self.t0)
        self.state.set_register("$t1", self.t1)
        self.state.set_register("$t2", self.t2)

        self.branch_state.set_register("$t0", self.t0)
        self.branch_state.set_register("$t1", self.t1)
        self.branch_state.set_register("$t2", self.t2)

    def testAND(self):
        op = AND("$t0", "$t1", "$t2")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 0)

    def testOR(self):
        op = OR("$t0", "$t1", "$t2")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 434)

    def testANDI(self):
        op = ANDI("$t0", "$t1", 434)
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 434)

    def testORI(self):
        op = ORI("$t0", "$t1", 0)
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 434)

    def testBEQ(self):
        prior_val = self.branch_state.program_counter()
        op = BEQ("$t1", "$t2", "LOOP")
        op.execute(self.branch_state)
        result = self.branch_state.program_counter()
        self.assertEqual(result, prior_val)
        self.branch_state.set_register("$pc", 0)

    def testBNE(self):
        prior_val = self.branch_state.program_counter()
        op = BNE("$t1", "$t2", "LOOP")
        op.execute(self.branch_state)
        result = self.branch_state.program_counter()
        self.assertEqual(result, 4)
        self.branch_state.set_register("$pc", 0)

    def testSLT(self):
        op = SLT("$t0", "$t1", "$t2")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 0)

    def testSLTI(self):
        op = SLTI("$t0", "$t2", 1)
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 1)

    def testSLTU(self):
        op = SLTU("$t0", "$t1", "$t2")
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 0)

    def testSLTIU(self):
        op = SLTIU("$t0", "$t1", 0)
        op.execute(self.state)
        result = self.state.register("$t0")
        self.assertEqual(result, 0)