2015-07-30 18:18:12 +02:00
|
|
|
|
|
|
|
require_relative "eventable"
|
|
|
|
|
2015-11-18 11:00:30 +01:00
|
|
|
module Register
|
2016-12-06 10:38:09 +01:00
|
|
|
|
|
|
|
# An interpreter for the register level. As the register machine is a simple model,
|
|
|
|
# interpreting it is not so terribly difficult.
|
|
|
|
#
|
|
|
|
# There is a certain amount of basic machinery to fetch and execute the next instruction
|
2016-12-25 17:05:39 +01:00
|
|
|
# (as a cpu would), and then there is a method for each instruction. Eg an instruction SlotToReg
|
|
|
|
# will be executed by method execute_SlotToReg
|
2016-12-06 10:38:09 +01:00
|
|
|
#
|
|
|
|
# The Interpreter (a bit like a cpu) has a state flag, a current instruction and registers
|
|
|
|
# We collect the stdout (as a hack not to interpret the OS)
|
|
|
|
#
|
2015-07-30 18:18:12 +02:00
|
|
|
class Interpreter
|
|
|
|
# fire events for changed pc and register contents
|
|
|
|
include Eventable
|
2015-11-05 15:50:00 +01:00
|
|
|
include Logging
|
|
|
|
log_level :info
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2015-10-19 13:46:12 +02:00
|
|
|
attr_reader :instruction # current instruction or pc
|
|
|
|
attr_reader :clock # current instruction or pc
|
2015-11-02 19:10:48 +01:00
|
|
|
|
2015-10-19 13:46:12 +02:00
|
|
|
attr_reader :registers # the registers, 16 (a hash, sym -> contents)
|
|
|
|
attr_reader :stdout # collect the output
|
|
|
|
attr_reader :state # running etc
|
|
|
|
attr_reader :flags # somewhat like the lags on a cpu, hash sym => bool (zero .. . )
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2016-12-06 10:38:09 +01:00
|
|
|
#start in state :stopped and set registers to unknown
|
2015-07-30 18:18:12 +02:00
|
|
|
def initialize
|
2015-10-21 13:07:29 +02:00
|
|
|
@state = :stopped
|
2015-07-30 18:18:12 +02:00
|
|
|
@stdout = ""
|
|
|
|
@registers = {}
|
2015-11-04 19:22:03 +01:00
|
|
|
@flags = { :zero => false , :plus => false ,
|
|
|
|
:minus => false , :overflow => false }
|
2015-07-30 18:18:12 +02:00
|
|
|
@clock = 0
|
2015-08-08 23:52:27 +02:00
|
|
|
(0...12).each do |r|
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register "r#{r}".to_sym , "r#{r}:unknown"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
def start instruction
|
2015-11-10 18:28:31 +01:00
|
|
|
initialize
|
2015-10-21 13:07:29 +02:00
|
|
|
set_state(:running)
|
2015-10-23 20:27:36 +02:00
|
|
|
set_instruction instruction
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
|
2015-10-21 13:07:29 +02:00
|
|
|
def set_state state
|
|
|
|
old = @state
|
|
|
|
return if state == old
|
|
|
|
@state = state
|
|
|
|
trigger(:state_changed , old , state )
|
|
|
|
end
|
|
|
|
|
2015-07-30 18:18:12 +02:00
|
|
|
def set_instruction i
|
|
|
|
return if @instruction == i
|
|
|
|
old = @instruction
|
|
|
|
@instruction = i
|
|
|
|
trigger(:instruction_changed, old , i)
|
2015-10-21 13:07:29 +02:00
|
|
|
set_state( :exited) unless i
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_register( reg )
|
2015-10-10 20:38:55 +02:00
|
|
|
reg = reg.symbol if reg.is_a? Register::RegisterValue
|
|
|
|
raise "Not a register #{reg}" unless Register::RegisterValue.look_like_reg(reg)
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers[reg]
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_register reg , val
|
|
|
|
old = get_register( reg ) # also ensures format
|
2015-11-04 19:22:03 +01:00
|
|
|
if val.is_a? Fixnum
|
2015-10-19 13:46:12 +02:00
|
|
|
@flags[:zero] = (val == 0)
|
2015-11-13 23:20:03 +01:00
|
|
|
@flags[:plus] = (val >= 0)
|
2015-11-04 19:22:03 +01:00
|
|
|
@flags[:minus] = (val < 0)
|
2015-11-05 15:50:00 +01:00
|
|
|
log.debug "Set_flags #{val} :#{@flags.inspect}"
|
2015-11-04 19:22:03 +01:00
|
|
|
else
|
|
|
|
@flags[:zero] = @flags[:plus] = true
|
|
|
|
@flags[:minus] = false
|
2015-10-19 13:46:12 +02:00
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
return if old === val
|
2015-10-10 20:38:55 +02:00
|
|
|
reg = reg.symbol if reg.is_a? Register::RegisterValue
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers[reg] = val
|
|
|
|
trigger(:register_changed, reg , old , val)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tick
|
|
|
|
return unless @instruction
|
|
|
|
@clock += 1
|
|
|
|
name = @instruction.class.name.split("::").last
|
2015-11-09 22:25:34 +01:00
|
|
|
log.debug "#{@clock.to_s}: #{@instruction.to_s}"
|
2015-07-30 18:18:12 +02:00
|
|
|
fetch = send "execute_#{name}"
|
|
|
|
return unless fetch
|
2015-10-23 20:27:36 +02:00
|
|
|
set_instruction @instruction.next
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
# Label is a noop.
|
|
|
|
def execute_Label
|
|
|
|
true
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
# Instruction interpretation starts here
|
2015-10-19 15:08:00 +02:00
|
|
|
def execute_Branch
|
2015-10-23 20:27:36 +02:00
|
|
|
label = @instruction.label
|
|
|
|
set_instruction label
|
2015-07-30 18:18:12 +02:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-10-19 15:08:00 +02:00
|
|
|
def execute_IsZero
|
2015-10-19 15:22:24 +02:00
|
|
|
@flags[:zero] ? execute_Branch : true
|
|
|
|
end
|
|
|
|
def execute_IsNotzero
|
|
|
|
@flags[:zero] ? true : execute_Branch
|
|
|
|
end
|
|
|
|
def execute_IsPlus
|
|
|
|
@flags[:plus] ? execute_Branch : true
|
|
|
|
end
|
|
|
|
def execute_IsMinus
|
|
|
|
@flags[:minus] ? execute_Branch : true
|
2015-10-07 09:05:34 +02:00
|
|
|
end
|
|
|
|
|
2015-07-30 18:18:12 +02:00
|
|
|
def execute_LoadConstant
|
|
|
|
to = @instruction.register
|
2015-08-08 23:52:27 +02:00
|
|
|
value = @instruction.constant
|
2015-11-04 19:22:03 +01:00
|
|
|
#value = value.object_id unless value.is_a?(Fixnum)
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register( to , value )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-12-25 17:05:39 +01:00
|
|
|
def execute_SlotToReg
|
2015-11-07 18:38:03 +01:00
|
|
|
object = get_register( @instruction.array )
|
2015-11-07 16:34:41 +01:00
|
|
|
if( @instruction.index.is_a?(Numeric) )
|
|
|
|
index = @instruction.index
|
|
|
|
else
|
|
|
|
index = get_register(@instruction.index)
|
|
|
|
end
|
2015-11-16 17:03:29 +01:00
|
|
|
if object.is_a?(Symbol)
|
2016-12-16 00:43:54 +01:00
|
|
|
raise "Must convert symbol to word:#{object}" unless( index == 2 )
|
|
|
|
value = object.to_s.length
|
2015-11-16 17:03:29 +01:00
|
|
|
else
|
2015-11-18 14:36:43 +01:00
|
|
|
value = object.get_internal_word( index )
|
2015-11-16 17:03:29 +01:00
|
|
|
end
|
2015-11-04 19:22:03 +01:00
|
|
|
#value = value.object_id unless value.is_a? Fixnum
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register( @instruction.register , value )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-12-25 17:02:39 +01:00
|
|
|
def execute_RegToSlot
|
2015-11-07 18:38:03 +01:00
|
|
|
value = get_register( @instruction.register )
|
|
|
|
object = get_register( @instruction.array )
|
2015-11-08 16:10:36 +01:00
|
|
|
if( @instruction.index.is_a?(Numeric) )
|
2015-11-09 22:25:34 +01:00
|
|
|
index = @instruction.index
|
2015-11-08 16:10:36 +01:00
|
|
|
else
|
2015-11-09 22:25:34 +01:00
|
|
|
index = get_register(@instruction.index)
|
2015-11-08 16:10:36 +01:00
|
|
|
end
|
2015-11-18 14:36:43 +01:00
|
|
|
object.set_internal_word( index , value )
|
2015-11-09 22:25:34 +01:00
|
|
|
trigger(:object_changed, @instruction.array , index)
|
2015-07-30 18:18:12 +02:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-12-25 17:11:58 +01:00
|
|
|
def execute_ByteToReg
|
2015-11-19 09:09:55 +01:00
|
|
|
object = get_register( @instruction.array )
|
|
|
|
if( @instruction.index.is_a?(Numeric) )
|
|
|
|
index = @instruction.index
|
|
|
|
else
|
|
|
|
index = get_register(@instruction.index)
|
|
|
|
end
|
2016-12-16 00:43:54 +01:00
|
|
|
raise "Unsupported action, must convert symbol to word:#{object}" if object.is_a?(Symbol)
|
|
|
|
value = object.get_char( index )
|
2015-11-19 09:09:55 +01:00
|
|
|
#value = value.object_id unless value.is_a? Fixnum
|
|
|
|
set_register( @instruction.register , value )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-12-25 17:11:58 +01:00
|
|
|
def execute_RegToByte
|
2015-11-19 09:09:55 +01:00
|
|
|
value = get_register( @instruction.register )
|
|
|
|
object = get_register( @instruction.array )
|
|
|
|
if( @instruction.index.is_a?(Numeric) )
|
|
|
|
index = @instruction.index
|
|
|
|
else
|
|
|
|
index = get_register(@instruction.index)
|
|
|
|
end
|
2016-12-16 00:43:54 +01:00
|
|
|
object.set_char( index , value )
|
2015-11-19 09:09:55 +01:00
|
|
|
trigger(:object_changed, @instruction.array , index / 4 )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-07-30 18:18:12 +02:00
|
|
|
def execute_RegisterTransfer
|
|
|
|
value = get_register @instruction.from
|
|
|
|
set_register @instruction.to , value
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_FunctionCall
|
2015-10-28 13:33:38 +01:00
|
|
|
set_instruction @instruction.method.instructions
|
2015-07-30 18:18:12 +02:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-10-17 18:36:00 +02:00
|
|
|
def execute_FunctionReturn
|
2015-11-07 18:38:03 +01:00
|
|
|
object = get_register( @instruction.register )
|
2015-11-18 14:36:43 +01:00
|
|
|
link = object.get_internal_word( @instruction.index )
|
2015-10-23 20:27:36 +02:00
|
|
|
@instruction = link
|
2015-10-17 18:36:00 +02:00
|
|
|
# we jump back to the call instruction. so it is as if the call never happened and we continue
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-07-30 18:18:12 +02:00
|
|
|
def execute_Syscall
|
|
|
|
name = @instruction.name
|
2015-10-17 18:36:00 +02:00
|
|
|
ret_value = 0
|
2015-07-30 18:18:12 +02:00
|
|
|
case name
|
|
|
|
when :putstring
|
2016-12-11 13:19:24 +01:00
|
|
|
ret_value = handle_putstring
|
2015-07-30 18:18:12 +02:00
|
|
|
when :exit
|
|
|
|
set_instruction(nil)
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
raise "un-implemented syscall #{name}"
|
|
|
|
end
|
2015-10-17 18:36:00 +02:00
|
|
|
set_register( :r0 , ret_value ) # syscalls return into r0 , usually some int
|
2015-07-30 18:18:12 +02:00
|
|
|
true
|
|
|
|
end
|
2015-08-07 15:46:55 +02:00
|
|
|
|
2016-12-11 13:19:24 +01:00
|
|
|
def handle_putstring
|
|
|
|
str = get_register( :r1 ) # should test length, ie r2
|
|
|
|
case str
|
|
|
|
when Symbol
|
|
|
|
@stdout += str.to_s
|
|
|
|
return str.to_s.length
|
|
|
|
when Parfait::Word
|
|
|
|
@stdout += str.to_string
|
|
|
|
return str.char_length
|
|
|
|
else
|
|
|
|
raise "NO string for putstring #{str.class}:#{str.object_id}" unless str.is_a?(Symbol)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-07 15:46:55 +02:00
|
|
|
def execute_OperatorInstruction
|
2015-11-11 19:34:49 +01:00
|
|
|
left = get_register(@instruction.left) || 0
|
2015-10-07 09:05:34 +02:00
|
|
|
rr = @instruction.right
|
2015-11-11 19:34:49 +01:00
|
|
|
right = get_register(rr) || 0
|
2015-11-08 14:15:55 +01:00
|
|
|
@flags[:overflow] = false
|
2016-12-11 13:19:24 +01:00
|
|
|
result = handle_operator(left,right)
|
|
|
|
if( result > 2**32 )
|
|
|
|
@flags[:overflow] = true
|
|
|
|
result = result % 2**32
|
|
|
|
else
|
|
|
|
result = result.to_i
|
|
|
|
end
|
|
|
|
log.debug "#{@instruction} == #{result}(#{result.class}) (#{left}|#{right})"
|
|
|
|
right = set_register(@instruction.left , result)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_operator(left, right)
|
2015-10-09 17:06:00 +02:00
|
|
|
case @instruction.operator.to_s
|
2015-10-15 08:32:47 +02:00
|
|
|
when "+"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left + right
|
2015-10-07 09:05:34 +02:00
|
|
|
when "-"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left - right
|
2015-11-11 19:34:49 +01:00
|
|
|
when ">>"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left / (2**right)
|
2015-11-11 19:34:49 +01:00
|
|
|
when "<<"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left * (2**right)
|
2015-10-19 13:46:12 +02:00
|
|
|
when "*"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left * right
|
2015-11-11 19:34:49 +01:00
|
|
|
when "&"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left & right
|
2015-11-11 19:34:49 +01:00
|
|
|
when "|"
|
2016-12-11 13:19:24 +01:00
|
|
|
return left | right
|
2015-10-07 09:05:34 +02:00
|
|
|
when "=="
|
2016-12-11 13:19:24 +01:00
|
|
|
return (left == right) ? 1 : 0
|
2015-08-07 15:46:55 +02:00
|
|
|
else
|
2015-10-09 17:06:00 +02:00
|
|
|
raise "unimplemented '#{@instruction.operator}' #{@instruction}"
|
2015-08-07 15:46:55 +02:00
|
|
|
end
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
end
|