trigger on state change

also make states symbols
This commit is contained in:
Torsten Ruger 2015-10-21 14:07:29 +03:00
parent db5c37bc55
commit a44b88f570

View File

@ -18,7 +18,7 @@ module Interpreter
attr_reader :flags # somewhat like the lags on a cpu, hash sym => bool (zero .. . ) attr_reader :flags # somewhat like the lags on a cpu, hash sym => bool (zero .. . )
def initialize def initialize
@state = "runnnig" @state = :stopped
@stdout = "" @stdout = ""
@registers = {} @registers = {}
@flags = { :zero => false , :positive => false , @flags = { :zero => false , :positive => false ,
@ -27,12 +27,21 @@ module Interpreter
(0...12).each do |r| (0...12).each do |r|
set_register "r#{r}".to_sym , "r#{r}:unknown" set_register "r#{r}".to_sym , "r#{r}:unknown"
end end
@block = nil
end end
def start bl def start bl
set_state(:running)
set_block bl set_block bl
end end
def set_state state
old = @state
return if state == old
@state = state
trigger(:state_changed , old , state )
end
def set_block bl def set_block bl
return if @block == bl return if @block == bl
raise "Error, nil block" unless bl raise "Error, nil block" unless bl
@ -43,11 +52,11 @@ module Interpreter
end end
def set_instruction i def set_instruction i
@state = "exited" unless i
return if @instruction == i return if @instruction == i
old = @instruction old = @instruction
@instruction = i @instruction = i
trigger(:instruction_changed, old , i) trigger(:instruction_changed, old , i)
set_state( :exited) unless i
end end
def get_register( reg ) def get_register( reg )
@ -62,8 +71,7 @@ module Interpreter
@flags[:zero] = (val == 0) @flags[:zero] = (val == 0)
@flags[:positive] = (val > 0) @flags[:positive] = (val > 0)
@flags[:negative] = (val < 0) @flags[:negative] = (val < 0)
#puts "Set_flags #{val} :zero=#{@flags[:zero]}" #puts "Set_flags #{val} :#{@flags.inspect}"
end end
return if old === val return if old === val
reg = reg.symbol if reg.is_a? Register::RegisterValue reg = reg.symbol if reg.is_a? Register::RegisterValue
@ -210,7 +218,7 @@ module Interpreter
else else
raise "unimplemented '#{@instruction.operator}' #{@instruction}" raise "unimplemented '#{@instruction.operator}' #{@instruction}"
end end
#puts "#{@instruction} == #{result} (#{left}|#{right})" puts "#{@instruction} == #{result} (#{left}|#{right})"
right = set_register(@instruction.left , result) right = set_register(@instruction.left , result)
true true
end end