From 0293320bb85b9435e3ae8475c0ac286b57ab8a1c Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 23 May 2018 21:35:22 +0300 Subject: [PATCH] use a constant for the binary code offset where the instructions start --- lib/parfait/binary_code.rb | 14 ++++++++------ lib/risc/interpreter.rb | 14 ++++++++------ lib/risc/machine.rb | 2 +- test/risc/test_interpreter.rb | 29 ++++++++++++++++------------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/parfait/binary_code.rb b/lib/parfait/binary_code.rb index 0767c802..ea314f07 100644 --- a/lib/parfait/binary_code.rb +++ b/lib/parfait/binary_code.rb @@ -8,20 +8,22 @@ module Parfait # class BinaryCode < Data16 attr_reader :next + def self.offset + 2 * 4 # size of type (2, type+next) * word_size (4) + end def initialize(total_size) super() - extend_to(total_size) + extend_to(total_size ) #puts "Init with #{total_size} for #{object_id}" (0 ..(data_length)).each{ |index| set_word(index , 0) } end def extend_to(total_size) - if total_size > self.data_length - extend_one unless @next - @next.extend_to(total_size - data_length) - end + return unless total_size > self.data_length + extend_one() unless @next + @next.extend_to(total_size - data_length) end - def extend_one + def extend_one() @next = BinaryCode.new(1) #puts "extending #{total_size - data_length} in #{self}" Risc::Position.reset(self) if Risc::Position.set?(self) diff --git a/lib/risc/interpreter.rb b/lib/risc/interpreter.rb index b021a6fe..066869bd 100644 --- a/lib/risc/interpreter.rb +++ b/lib/risc/interpreter.rb @@ -14,7 +14,7 @@ module Risc # fire events for changed pc and register contents include Util::Eventable include Util::Logging - log_level :info + log_level :debug attr_reader :instruction , :clock , :pc # current instruction and pc attr_reader :registers # the registers, 16 (a hash, sym -> contents) @@ -48,9 +48,11 @@ module Risc def set_pc( pos ) raise "Not int #{pos}" unless pos.is_a? Numeric position = Position.at(pos) - log.debug "Setting Position #{position}" if position.is_a?(Position::CodePosition) - return set_pc(position.at + 12) + log.debug "Setting Position #{clock}-#{position}, #{position.method}" + return set_pc(position.at + Parfait::BinaryCode.offset) + else + log.debug "Setting Position #{clock}-#{position}, #{position.binary}" end raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(Position::InstructionPosition) set_instruction( position.instruction ) @@ -60,7 +62,7 @@ module Risc def set_instruction( instruction ) raise "set to same instruction #{instruction}:#{instruction.class}" if @instruction == instruction - log.debug "Setting Instruction #{instruction}" + log.debug "Setting Instruction #{instruction.class}" old = @instruction @instruction = instruction trigger(:instruction_changed, old , instruction) @@ -97,7 +99,7 @@ module Risc return @clock end name = @instruction.class.name.split("::").last - log.debug "#{@pc.to_s}:#{@clock.to_s(16)}: #{@instruction.to_s}" + log.debug "#{@pc.to_s(16)}:#{@clock}: #{@instruction.to_s}" fetch = send "execute_#{name}" log.debug register_dump if fetch @@ -214,7 +216,7 @@ module Risc meth = @instruction.method at = Position.get(meth.binary).at log.debug "Call to #{meth.name} at:#{at}" - set_pc(at + 12) + set_pc(at + BinaryCode.offset) #set_instruction @instruction.method.risc_instructions false end diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index 7829156b..b709f26e 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -108,7 +108,7 @@ module Risc first_method = Parfait.object_space.types.values.first.methods before = at Position.set( first_method.binary , at , first_method) - Position.set( first_method.cpu_instructions, at + 12 , first_method.binary) + Position.set( first_method.cpu_instructions, at + Parfait::BinaryCode.offset , first_method.binary) log.debug "Method #{first_method.name}:#{before.to_s(16)} len: #{(at - before).to_s(16)}" log.debug "Instructions #{first_method.cpu_instructions.object_id.to_s(16)}:#{(before+12).to_s(16)}" at diff --git a/test/risc/test_interpreter.rb b/test/risc/test_interpreter.rb index 8782fcac..d09a035f 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 test_class + def pest_class assert_equal Risc::Interpreter , Interpreter.new.class end - def test_starts_stopped + def pest_starts_stopped assert_equal :stopped , Interpreter.new.state end - def test_has_regs + def pest_has_regs assert_equal 12 , Interpreter.new.registers.length end - def test_has_r0 + def pest_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 test_starts + def pest_starts assert_equal 0 , @interpreter.start_machine end - def test_started + def pest_started @interpreter.start_machine assert_equal :running , @interpreter.state end - def test_pos + def pest_pos @interpreter.start_machine assert_equal 1 , @interpreter.clock end @@ -43,30 +43,33 @@ module Risc @interpreter = Interpreter.new @interpreter.start_machine end - def test_tick1 + def pest_tick1 assert_equal 2 , @interpreter.tick end - def test_clock1 + def pest_clock1 @interpreter.tick assert_equal 2 , @interpreter.clock end - def test_pc1 + def pest_pc1 @interpreter.tick assert_equal 19484 , @interpreter.pc end - def test_tick2 + def pest_tick2 @interpreter.tick assert_equal 3 , @interpreter.tick end - def test_clock2 + def pest_clock2 @interpreter.tick @interpreter.tick assert_equal 3 , @interpreter.clock end - def test_pc2 + def pest_pc2 @interpreter.tick @interpreter.tick assert_equal 19488 , @interpreter.pc end + def test_tick_15 + 15.times {@interpreter.tick} + end end end