diff --git a/lib/risc/position.rb b/lib/risc/position.rb index 567438be..621dbae8 100644 --- a/lib/risc/position.rb +++ b/lib/risc/position.rb @@ -29,7 +29,7 @@ module Risc @positions = {} @reverse_cache = {} end - + def self.at( int ) @reverse_cache[int] end @@ -65,9 +65,11 @@ module Risc if testing.class != position.class raise "Mismatch (at #{to.to_s(16)}) new:#{position}:#{position.class} , was #{testing}:#{testing.class}" end - @reverse_cache.delete(to) end - @reverse_cache[position.at] = position + unless position.object.is_a? Label + @reverse_cache.delete(to) + @reverse_cache[position.at] = position + end log.debug "Reset #{position} (#{to.to_s(16)}) for #{position.class}" return position end @@ -81,7 +83,7 @@ module Risc raise "Mismatch (at #{pos.to_s(16)}) was:#{position} #{position.class} #{position.object} , should #{testing}#{testing.class}" if testing and testing.class != position.class self.positions[object] = position position.init(pos) - @reverse_cache[position.at] = position + @reverse_cache[position.at] = position unless object.is_a? Label log.debug "Set #{position} (#{pos.to_s(16)}) for #{position.class}" position end diff --git a/test/risc/position/test_instruction_position.rb b/test/risc/position/test_instruction_position.rb index 4e6f4d68..6be243ff 100644 --- a/test/risc/position/test_instruction_position.rb +++ b/test/risc/position/test_instruction_position.rb @@ -35,6 +35,14 @@ module Risc assert_equal InstructionPosition , at_4.class assert_equal Branch , at_4.instruction.class end + def test_label_at_reverse + branch = Branch.new("b" , @label) + Position.set(branch , 8 , @binary) + Position.set(@label , 8 , @binary) + at_4 = Position.at(8) + assert_equal InstructionPosition , at_4.class + assert_equal Branch , at_4.instruction.class + end def test_reset_false_type assert_raises {Position.set(@label , 0 , @binary)} end diff --git a/test/risc/test_interpreter.rb b/test/risc/test_interpreter.rb index d09a035f..89d3fe80 100644 --- a/test/risc/test_interpreter.rb +++ b/test/risc/test_interpreter.rb @@ -3,16 +3,16 @@ require_relative "helper" module Risc class TestInterpreterBasics < MiniTest::Test - def pest_class + def test_class assert_equal Risc::Interpreter , Interpreter.new.class end - def pest_starts_stopped + def test_starts_stopped assert_equal :stopped , Interpreter.new.state end - def pest_has_regs + def test_has_regs assert_equal 12 , Interpreter.new.registers.length end - def pest_has_r0 + def test_has_r0 assert_equal :r0 , Interpreter.new.registers.keys.first end end @@ -23,14 +23,14 @@ module Risc @machine.position_all @interpreter = Interpreter.new end - def pest_starts + def test_starts assert_equal 0 , @interpreter.start_machine end - def pest_started + def test_started @interpreter.start_machine assert_equal :running , @interpreter.state end - def pest_pos + def test_pos @interpreter.start_machine assert_equal 1 , @interpreter.clock end @@ -43,30 +43,30 @@ module Risc @interpreter = Interpreter.new @interpreter.start_machine end - def pest_tick1 + def test_tick1 assert_equal 2 , @interpreter.tick end - def pest_clock1 + def test_clock1 @interpreter.tick assert_equal 2 , @interpreter.clock end - def pest_pc1 + def test_pc1 @interpreter.tick - assert_equal 19484 , @interpreter.pc + assert_equal 18392 , @interpreter.pc end - def pest_tick2 + def test_tick2 @interpreter.tick assert_equal 3 , @interpreter.tick end - def pest_clock2 + def test_clock2 @interpreter.tick @interpreter.tick assert_equal 3 , @interpreter.clock end - def pest_pc2 + def test_pc2 @interpreter.tick @interpreter.tick - assert_equal 19488 , @interpreter.pc + assert_equal 18396 , @interpreter.pc end def test_tick_15 15.times {@interpreter.tick}