2018-05-17 19:14:59 +02:00
|
|
|
require_relative "helper"
|
|
|
|
|
|
|
|
module Risc
|
|
|
|
class TestInterpreterBasics < MiniTest::Test
|
|
|
|
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_class
|
2018-05-17 19:14:59 +02:00
|
|
|
assert_equal Risc::Interpreter , Interpreter.new.class
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_starts_stopped
|
2018-05-17 19:14:59 +02:00
|
|
|
assert_equal :stopped , Interpreter.new.state
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_has_regs
|
2018-05-17 19:14:59 +02:00
|
|
|
assert_equal 12 , Interpreter.new.registers.length
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_has_r0
|
2018-05-17 19:14:59 +02:00
|
|
|
assert_equal :r0 , Interpreter.new.registers.keys.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class TestInterpreterStarts < MiniTest::Test
|
|
|
|
def setup
|
|
|
|
@machine = Risc.machine.boot
|
|
|
|
@machine.translate(:interpreter)
|
|
|
|
@machine.position_all
|
|
|
|
@interpreter = Interpreter.new
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_starts
|
2018-05-17 19:14:59 +02:00
|
|
|
assert_equal 0 , @interpreter.start_machine
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_started
|
2018-05-17 19:14:59 +02:00
|
|
|
@interpreter.start_machine
|
|
|
|
assert_equal :running , @interpreter.state
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_pos
|
2018-05-17 19:14:59 +02:00
|
|
|
@interpreter.start_machine
|
2018-05-23 17:05:22 +02:00
|
|
|
assert_equal 1 , @interpreter.clock
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
class TestInterpreterTicks < MiniTest::Test
|
|
|
|
def setup
|
|
|
|
@machine = Risc.machine.boot
|
|
|
|
@machine.translate(:interpreter)
|
|
|
|
@machine.position_all
|
|
|
|
@interpreter = Interpreter.new
|
|
|
|
@interpreter.start_machine
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_tick1
|
2018-05-23 17:05:22 +02:00
|
|
|
assert_equal 2 , @interpreter.tick
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_clock1
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
assert_equal 2 , @interpreter.clock
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_pc1
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
2018-05-29 19:52:58 +02:00
|
|
|
assert_equal 17952 , @interpreter.pc
|
2018-05-23 17:05:22 +02:00
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_tick2
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
assert_equal 3 , @interpreter.tick
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_clock2
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
@interpreter.tick
|
|
|
|
assert_equal 3 , @interpreter.clock
|
|
|
|
end
|
2018-05-24 15:26:56 +02:00
|
|
|
def test_pc2
|
2018-05-23 17:05:22 +02:00
|
|
|
@interpreter.tick
|
|
|
|
@interpreter.tick
|
2018-05-29 19:52:58 +02:00
|
|
|
assert_equal 17956 , @interpreter.pc
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
2018-05-25 18:16:13 +02:00
|
|
|
def test_tick_14_jump
|
|
|
|
14.times {@interpreter.tick}
|
|
|
|
assert_equal Branch , @interpreter.instruction.class
|
|
|
|
end
|
|
|
|
def test_tick_14_bin
|
|
|
|
13.times {@interpreter.tick}
|
|
|
|
binary_pos = binary_position
|
|
|
|
@interpreter.tick
|
|
|
|
assert_equal binary_pos , binary_position , "#{binary_pos.to_s(16)}!=#{binary_position.to_s(16)}"
|
|
|
|
end
|
|
|
|
def binary_position
|
|
|
|
Position.get(Position.get(@interpreter.instruction).binary).at
|
|
|
|
end
|
|
|
|
def test_tick_15 #more than a binary code worth
|
2018-05-23 20:35:22 +02:00
|
|
|
15.times {@interpreter.tick}
|
|
|
|
end
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
|
|
|
end
|