Exemplo n.º 1
0
    def test_op_relative_base(self):
        """
        Tests the relative base mode op code
        """
        e = ElfCPU()

        # Position
        e.load_string('9,5,204,1,99,6,7,777')
        e.interrupts = True
        # Step over relative mode op
        e.step()
        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 777)

        # Immediate
        e.reset()
        e.load_string('109,5,204,1,99,444,777')
        e.interrupts = True
        # Step over relative mode op
        e.step()
        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 777)

        # Relative
        e.reset()
        e.load_string('209,9,209,6,204,-2,99,5,333,4,6')
        e.interrupts = True
        e.debug = True
        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 333)
Exemplo n.º 2
0
    def test_op_output(self):
        """
        Tests output op code

        Use io.StringIO() to capture the output
        """
        e = ElfCPU()

        # Interrupts off
        e.load_string('4,5,104,66,99,55,5')
        e.interrupts = False

        result = None
        with patch('sys.stdout', new=io.StringIO()) as output:
            e.execute()
            result = output.getvalue()
        result = result.splitlines()

        # First is a reference to memory address 5
        self.assertEqual(result[0].strip(), '55')
        # Second is an immediate value
        self.assertEqual(result[1].strip(), '66')

        # Interrupts on

        e.load_string('4,5,104,66,99,55,5')
        e.interrupts = True

        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 55)
        # Don't clear buffer
        with self.assertRaises(OutputOverflow):
            e.execute()

        # Restart test
        e.reset()
        e.load_string('4,5,104,66,99,55,5')
        e.interrupts = True

        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 55)

        # Clear buffer
        del e.output_buffer

        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 66)

        ###############################################
        # Interrupts on RELATIVE MODE
        # Restart test
        e.reset()
        e.load_string('109,5,204,1,99,6,1234')
        e.interrupts = True

        with self.assertRaises(OutputInterrupt):
            e.execute()
        self.assertEqual(e.output_buffer, 1234)
Exemplo n.º 3
0
    def test_op_input(self):
        """
        Tests input op code

        Use unittest.mock.patch to fake the input value
        """
        e = ElfCPU()

        # Interrupts off
        e.load_string('103,3,99,-1')
        e.interrupts = False
        with patch('builtins.input', return_value='1234'):
            e.execute()
            self.assertEqual(e.peek(3), 1234)

        # Interrupts on IMMEDIATE MODE
        e.load_string('103,5,103,5,99,-1')
        e.interrupts = True
        with self.assertRaises(InputInterrupt):
            e.step()

        # Should be back at pc = 0
        self.assertEqual(e.pc, 0)

        # Load input
        e.input_buffer = 567

        # Loading again overflows
        with self.assertRaises(InputOverflow):
            e.input_buffer = 123

        # Execute the input instruction
        e.step()
        self.assertEqual(e.peek(5), 567)

        # Exec next input instruction
        with self.assertRaises(InputInterrupt):
            e.step()

        e.input_buffer = 987

        # Execute until end
        e.execute()
        self.assertEqual(e.peek(5), 987)

        ######################################################
        # Interrupts on RELATIVE MODE
        e.load_string('109,10,203,0,203,1,203,-1,99,102,100,101')
        e.interrupts = True

        # step past the relative base op code
        e.step()

        with self.assertRaises(InputInterrupt):
            e.step()

        # Should be back at pc = 2 (after relative base op code)
        self.assertEqual(e.pc, 2)

        # Load input
        e.input_buffer = 567

        # Loading again overflows
        with self.assertRaises(InputOverflow):
            e.input_buffer = 123

        # Execute the input instruction
        e.step()
        self.assertEqual(e.peek(10), 567)

        # Exec next input instruction
        with self.assertRaises(InputInterrupt):
            e.step()

        e.input_buffer = 987

        # Step to execute this input
        e.step()
        self.assertEqual(e.peek(11), 987)

        # Exec next input instruction
        with self.assertRaises(InputInterrupt):
            e.step()

        e.input_buffer = 456

        # Execute until end
        e.execute()
        self.assertEqual(e.peek(9), 456)

        ######################################################
        # Interrupts on POSITIONAL MODE
        e.load_string('3,7,3,8,3,9,99,1,3,5')
        e.interrupts = True

        with self.assertRaises(InputInterrupt):
            e.step()

        # Should be back at pc = 0
        self.assertEqual(e.pc, 0)

        # Load input
        e.input_buffer = 345

        # Loading again overflows
        with self.assertRaises(InputOverflow):
            e.input_buffer = 123

        # Execute the input instruction
        e.step()
        self.assertEqual(e.peek(7), 345)

        # Exec next input instruction
        with self.assertRaises(InputInterrupt):
            e.step()

        e.input_buffer = 765

        # Step to execute this input
        e.step()
        self.assertEqual(e.peek(8), 765)

        # Exec next input instruction
        with self.assertRaises(InputInterrupt):
            e.step()

        e.input_buffer = 555

        # Execute until end
        e.execute()
        self.assertEqual(e.peek(9), 555)