give interpreter a clock and pc

where the pc, like in cpu’s is the memory position.
That is what the interpreter works on.
But for humans, the clock is a simpler way to count where the program
is at, no. of instructions executed
This commit is contained in:
Torsten Ruger 2018-05-23 18:05:22 +03:00
parent ef2dc932ad
commit a2e7d7c469
3 changed files with 34 additions and 15 deletions

View File

@ -46,7 +46,7 @@ module Risc
end end
def source_mini def source_mini
return "(no source)" unless source return "(no source)" unless source
return "(from: #{source[0..35]})" if source.is_a?(String) return "(from: #{source[0..50]})" if source.is_a?(String)
"(from: #{source.class.name.split("::").last})" "(from: #{source.class.name.split("::").last})"
end end
end end

View File

@ -14,15 +14,15 @@ module Risc
# fire events for changed pc and register contents # fire events for changed pc and register contents
include Util::Eventable include Util::Eventable
include Util::Logging include Util::Logging
log_level :debug log_level :info
attr_reader :instruction , :clock # current instruction or pc attr_reader :instruction , :clock , :pc # current instruction and pc
attr_reader :registers # the registers, 16 (a hash, sym -> contents) attr_reader :registers # the registers, 16 (a hash, sym -> contents)
attr_reader :stdout, :state , :flags # somewhat like the lags on a cpu, hash sym => bool (zero .. . ) attr_reader :stdout, :state , :flags # somewhat like the lags on a cpu, hash sym => bool (zero .. . )
#start in state :stopped and set registers to unknown #start in state :stopped and set registers to unknown
def initialize() def initialize()
@stdout , @clock , @state = "", 0 , :stopped @stdout , @clock , @pc , @state = "", 0 , 0 , :stopped
@registers = {} @registers = {}
@flags = { :zero => false , :plus => false , @flags = { :zero => false , :plus => false ,
:minus => false , :overflow => false } :minus => false , :overflow => false }
@ -53,8 +53,9 @@ module Risc
return set_pc(position.at + 12) return set_pc(position.at + 12)
end end
raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(Position::InstructionPosition) raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(Position::InstructionPosition)
set_instruction( position.instruction) set_instruction( position.instruction )
@clock = position.at @clock += 1
@pc = position.at
end end
def set_instruction( instruction ) def set_instruction( instruction )
@ -96,12 +97,12 @@ module Risc
return @clock return @clock
end end
name = @instruction.class.name.split("::").last name = @instruction.class.name.split("::").last
log.debug "#{@clock.to_s}: #{@instruction.to_s}" log.debug "#{@pc.to_s}:#{@clock.to_s(16)}: #{@instruction.to_s}"
fetch = send "execute_#{name}" fetch = send "execute_#{name}"
log.debug register_dump log.debug register_dump
if fetch if fetch
clock = @clock + @instruction.byte_length pc = @pc + @instruction.byte_length
set_pc(clock) set_pc(pc)
else else
log.debug "No Fetch" log.debug "No Fetch"
end end

View File

@ -32,7 +32,7 @@ module Risc
end end
def test_pos def test_pos
@interpreter.start_machine @interpreter.start_machine
assert_equal 0 , @interpreter.clock assert_equal 1 , @interpreter.clock
end end
end end
class TestInterpreterTicks < MiniTest::Test class TestInterpreterTicks < MiniTest::Test
@ -43,12 +43,30 @@ module Risc
@interpreter = Interpreter.new @interpreter = Interpreter.new
@interpreter.start_machine @interpreter.start_machine
end end
def test_tick def test_tick1
assert_equal 19484 , @interpreter.tick assert_equal 2 , @interpreter.tick
end end
def test_tick def test_clock1
assert_equal 19484 , @interpreter.tick @interpreter.tick
assert_equal 19488 , @interpreter.tick assert_equal 2 , @interpreter.clock
end
def test_pc1
@interpreter.tick
assert_equal 19484 , @interpreter.pc
end
def test_tick2
@interpreter.tick
assert_equal 3 , @interpreter.tick
end
def test_clock2
@interpreter.tick
@interpreter.tick
assert_equal 3 , @interpreter.clock
end
def test_pc2
@interpreter.tick
@interpreter.tick
assert_equal 19488 , @interpreter.pc
end end
end end
end end