2017-01-19 08:02:29 +01:00
|
|
|
module Risc
|
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
|
2019-07-25 20:23:55 +02:00
|
|
|
# We collect the stdout (as a hack not to interpret the OS) in a string. It can also be passed
|
|
|
|
# in to the init, as an IO
|
2016-12-06 10:38:09 +01:00
|
|
|
#
|
2015-07-30 18:18:12 +02:00
|
|
|
class Interpreter
|
|
|
|
# fire events for changed pc and register contents
|
2018-03-26 19:05:30 +02:00
|
|
|
include Util::Eventable
|
2018-05-20 13:45:48 +02:00
|
|
|
include Util::Logging
|
2018-06-09 21:13:43 +02:00
|
|
|
log_level :info
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2018-05-23 17:05:22 +02:00
|
|
|
attr_reader :instruction , :clock , :pc # current instruction and pc
|
2015-10-19 13:46:12 +02:00
|
|
|
attr_reader :registers # the registers, 16 (a hash, sym -> contents)
|
2019-07-25 20:23:55 +02:00
|
|
|
attr_reader :stdout, :state , :flags # somewhat like the flags on a cpu, hash sym => bool (zero .. . )
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2019-07-25 20:23:55 +02:00
|
|
|
# start in state :stopped and set registers to unknown
|
|
|
|
# Linker gives the state of the program
|
|
|
|
# Passing a stdout in (an IO, only << called) can be used to get output immediately.
|
|
|
|
def initialize( linker , stdout = "")
|
|
|
|
@stdout , @clock , @pc , @state = stdout, 0 , 0 , :stopped
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers = {}
|
2015-11-04 19:22:03 +01:00
|
|
|
@flags = { :zero => false , :plus => false ,
|
|
|
|
:minus => false , :overflow => false }
|
2018-04-03 14:23:19 +02:00
|
|
|
(0...12).each do |reg|
|
|
|
|
set_register "r#{reg}".to_sym , "r#{reg}:unknown"
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
2018-07-02 15:19:01 +02:00
|
|
|
@linker = linker
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
|
2019-07-25 20:23:55 +02:00
|
|
|
def start_program()
|
2018-07-02 15:19:01 +02:00
|
|
|
init = @linker.cpu_init
|
2015-10-21 13:07:29 +02:00
|
|
|
set_state(:running)
|
2018-05-17 19:14:59 +02:00
|
|
|
set_pc( Position.get(init).at )
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
|
2018-03-23 17:56:38 +01:00
|
|
|
def set_state( state )
|
2015-10-21 13:07:29 +02:00
|
|
|
old = @state
|
|
|
|
return if state == old
|
|
|
|
@state = state
|
|
|
|
trigger(:state_changed , old , state )
|
|
|
|
end
|
|
|
|
|
2018-05-17 19:14:59 +02:00
|
|
|
def set_pc( pos )
|
|
|
|
raise "Not int #{pos}" unless pos.is_a? Numeric
|
|
|
|
position = Position.at(pos)
|
2018-06-05 17:11:25 +02:00
|
|
|
raise "No position at 0x#{pos.to_s(16)}" unless position
|
2018-06-02 22:02:59 +02:00
|
|
|
log.debug "Setting Position #{clock}-#{position}, "
|
|
|
|
set_instruction( position.object )
|
2018-05-23 17:05:22 +02:00
|
|
|
@clock += 1
|
|
|
|
@pc = position.at
|
2018-05-17 19:14:59 +02:00
|
|
|
end
|
|
|
|
|
2018-04-03 14:23:19 +02:00
|
|
|
def set_instruction( instruction )
|
2018-07-02 16:29:26 +02:00
|
|
|
raise "set to same instruction #{instruction}:#{instruction.class} at #{clock}" if @instruction == instruction
|
2018-05-23 20:35:22 +02:00
|
|
|
log.debug "Setting Instruction #{instruction.class}"
|
2015-07-30 18:18:12 +02:00
|
|
|
old = @instruction
|
2018-04-03 14:23:19 +02:00
|
|
|
@instruction = instruction
|
|
|
|
trigger(:instruction_changed, old , instruction)
|
2018-05-17 19:14:59 +02:00
|
|
|
set_state( :exited ) unless instruction
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_register( reg )
|
2018-06-29 10:39:07 +02:00
|
|
|
reg = reg.symbol if reg.is_a? Risc::RegisterValue
|
|
|
|
raise "Not a register #{reg}" unless Risc::RegisterValue.look_like_reg(reg)
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers[reg]
|
|
|
|
end
|
|
|
|
|
2018-03-23 17:56:38 +01:00
|
|
|
def set_register( reg , val )
|
2015-07-30 18:18:12 +02:00
|
|
|
old = get_register( reg ) # also ensures format
|
2019-02-07 17:24:35 +01:00
|
|
|
if val.is_a? ::Integer
|
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
|
2018-06-29 10:39:07 +02:00
|
|
|
reg = reg.symbol if reg.is_a? Risc::RegisterValue
|
2018-04-03 14:23:19 +02:00
|
|
|
val = Parfait.object_space.nil_object if val.nil? #because that's what real code has
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers[reg] = val
|
|
|
|
trigger(:register_changed, reg , old , val)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tick
|
2018-05-17 19:14:59 +02:00
|
|
|
unless @instruction
|
|
|
|
log.debug "No Instruction , No Tick"
|
|
|
|
return @clock
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
name = @instruction.class.name.split("::").last
|
2018-05-23 20:35:22 +02:00
|
|
|
log.debug "#{@pc.to_s(16)}:#{@clock}: #{@instruction.to_s}"
|
2015-07-30 18:18:12 +02:00
|
|
|
fetch = send "execute_#{name}"
|
2018-03-23 17:56:38 +01:00
|
|
|
log.debug register_dump
|
2018-05-17 19:14:59 +02:00
|
|
|
if fetch
|
2018-05-23 17:05:22 +02:00
|
|
|
pc = @pc + @instruction.byte_length
|
|
|
|
set_pc(pc)
|
2018-05-17 19:14:59 +02:00
|
|
|
else
|
|
|
|
log.debug "No Fetch"
|
|
|
|
end
|
|
|
|
@clock
|
2015-10-23 20:27:36 +02:00
|
|
|
end
|
2018-04-08 22:45:23 +02:00
|
|
|
|
2015-07-30 18:18:12 +02:00
|
|
|
# Instruction interpretation starts here
|
2018-04-02 15:36:43 +02:00
|
|
|
def execute_DynamicJump
|
2018-04-08 22:45:23 +02:00
|
|
|
method = get_register(@instruction.register)
|
2018-07-30 09:26:47 +02:00
|
|
|
pos = Position.get(method.binary)
|
|
|
|
log.debug "Jump to binary at: #{pos} #{method.name}:#{method.binary.class}"
|
|
|
|
raise "Invalid position for #{method.name}" unless pos.valid?
|
|
|
|
pos = pos + Parfait::BinaryCode.byte_offset
|
2018-07-03 18:15:36 +02:00
|
|
|
set_pc( pos )
|
2018-05-17 19:14:59 +02:00
|
|
|
false
|
2018-04-02 15:36:43 +02:00
|
|
|
end
|
2015-10-19 15:08:00 +02:00
|
|
|
def execute_Branch
|
2015-10-23 20:27:36 +02:00
|
|
|
label = @instruction.label
|
2018-05-25 18:16:13 +02:00
|
|
|
pos = Position.get(label).at
|
2018-05-28 14:09:59 +02:00
|
|
|
pos += Parfait::BinaryCode.byte_offset if label.is_a?(Parfait::BinaryCode)
|
2018-07-03 18:15:36 +02:00
|
|
|
set_pc( pos )
|
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
|
2018-03-24 16:53:27 +01:00
|
|
|
def execute_IsNotZero
|
2015-10-19 15:22:24 +02:00
|
|
|
@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
|
2018-05-30 23:07:58 +02:00
|
|
|
value = value.address if value.is_a?(Label)
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register( to , value )
|
|
|
|
true
|
|
|
|
end
|
2018-03-31 11:38:30 +02:00
|
|
|
alias :execute_LoadData :execute_LoadConstant
|
2015-07-30 18:18:12 +02:00
|
|
|
|
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
|
2018-03-23 17:56:38 +01:00
|
|
|
case object
|
|
|
|
when Symbol
|
2019-09-08 14:31:03 +02:00
|
|
|
if(index == 0)
|
|
|
|
value = object.get_type
|
|
|
|
elsif(index==1)
|
|
|
|
value = object.to_s.length
|
|
|
|
else
|
|
|
|
raise "Must convert symbol to word:#{object}:#{index}"
|
|
|
|
end
|
2018-03-23 17:56:38 +01:00
|
|
|
when nil
|
|
|
|
raise "error #{@instruction} retrieves nil"
|
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
|
2018-07-04 07:28:29 +02:00
|
|
|
log.debug "#{@instruction} == #{object}(#{Position.get(object)}) (#{value}|#{index})"
|
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 )
|
2019-02-07 17:24:35 +01:00
|
|
|
#value = value.object_id unless value.is_a? ::Integer
|
2015-11-19 09:09:55 +01:00
|
|
|
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
|
|
|
|
|
2018-03-21 11:18:04 +01:00
|
|
|
def execute_Transfer
|
2015-07-30 18:18:12 +02:00
|
|
|
value = get_register @instruction.from
|
|
|
|
set_register @instruction.to , value
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_FunctionCall
|
2018-05-19 11:21:20 +02:00
|
|
|
meth = @instruction.method
|
2018-07-04 07:28:29 +02:00
|
|
|
at = Position.get(meth.binary)
|
2018-05-19 11:21:20 +02:00
|
|
|
log.debug "Call to #{meth.name} at:#{at}"
|
2018-05-28 14:09:59 +02:00
|
|
|
set_pc(at + Parfait::BinaryCode.byte_offset)
|
2015-07-30 18:18:12 +02:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-10-17 18:36:00 +02:00
|
|
|
def execute_FunctionReturn
|
2018-03-23 17:56:38 +01:00
|
|
|
link = get_register( @instruction.register )
|
2018-07-04 07:28:29 +02:00
|
|
|
log.debug "Return to #{link.to_s(16)}"
|
2018-05-30 11:54:40 +02:00
|
|
|
set_pc link
|
2018-05-24 18:20:06 +02:00
|
|
|
false
|
2015-10-17 18:36:00 +02:00
|
|
|
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
|
2019-07-25 20:23:55 +02:00
|
|
|
@stdout << str.to_s
|
|
|
|
@stdout.flush if @stdout.respond_to?(:flush)
|
2016-12-11 13:19:24 +01:00
|
|
|
return str.to_s.length
|
|
|
|
when Parfait::Word
|
2019-07-25 20:23:55 +02:00
|
|
|
@stdout << str.to_string
|
|
|
|
@stdout.flush if @stdout.respond_to?(:flush)
|
2016-12-11 13:19:24 +01:00
|
|
|
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
|
|
|
|
|
2018-04-08 00:26:29 +02:00
|
|
|
def make_op_arg(arg)
|
|
|
|
case arg
|
|
|
|
when Integer
|
|
|
|
arg
|
|
|
|
when Parfait::Word
|
|
|
|
arg.to_string.to_sym.object_id
|
|
|
|
when String
|
|
|
|
arg.to_sym.object_id
|
|
|
|
when Symbol
|
|
|
|
arg.object_id
|
|
|
|
when Parfait::Object
|
|
|
|
arg.object_id
|
|
|
|
else
|
|
|
|
raise "Op arg #{arg}:#{arg.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-11 13:19:24 +01:00
|
|
|
def handle_operator(left, right)
|
2018-04-08 00:26:29 +02:00
|
|
|
left = make_op_arg(left)
|
|
|
|
right = make_op_arg(right)
|
2018-03-24 16:53:27 +01:00
|
|
|
case @instruction.operator
|
|
|
|
when :+
|
2018-04-03 14:23:19 +02:00
|
|
|
left + right
|
2018-03-24 16:53:27 +01:00
|
|
|
when :-
|
2018-04-07 21:35:48 +02:00
|
|
|
if( left.is_a?(String) or right.is_a?(String))
|
|
|
|
left == right ? 0 : 1 #for opal, and exception
|
|
|
|
else
|
|
|
|
left - right
|
|
|
|
end
|
2018-03-24 16:53:27 +01:00
|
|
|
when :>>
|
2018-04-03 14:23:19 +02:00
|
|
|
left / (2**right)
|
2018-03-24 16:53:27 +01:00
|
|
|
when :<<
|
2018-04-03 14:23:19 +02:00
|
|
|
left * (2**right)
|
2018-03-24 16:53:27 +01:00
|
|
|
when :*
|
2018-04-03 14:23:19 +02:00
|
|
|
left * right
|
2018-03-24 16:53:27 +01:00
|
|
|
when :&
|
2018-04-03 14:23:19 +02:00
|
|
|
left & right
|
2018-03-24 16:53:27 +01:00
|
|
|
when :|
|
2018-04-03 14:23:19 +02:00
|
|
|
left | right
|
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
|
2018-03-23 17:56:38 +01:00
|
|
|
|
|
|
|
def register_dump
|
|
|
|
(0..7).collect do |reg|
|
|
|
|
value = @registers["r#{reg}".to_sym]
|
2018-04-03 14:23:19 +02:00
|
|
|
"#{reg}-" +
|
2018-03-23 17:56:38 +01:00
|
|
|
case value
|
|
|
|
when String
|
|
|
|
value[0..10]
|
|
|
|
else
|
|
|
|
value.class.name.split("::").last
|
|
|
|
end
|
|
|
|
end.join("|")
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
end
|