introduce some helper methods

This commit is contained in:
Torsten Ruger
2018-06-02 23:02:59 +03:00
parent c2d450f779
commit 1d1c7105b4
9 changed files with 50 additions and 25 deletions

View File

@ -49,13 +49,13 @@ module Risc
raise "Not int #{pos}" unless pos.is_a? Numeric
position = Position.at(pos)
raise "No position #{pos.to_s(16)}" unless position
if position.is_a?(CodePosition)
if position.is_a?(CodeListener)
raise "Setting Code #{clock}-#{position}, #{position.method}"
#return set_pc(position.at + Parfait::BinaryCode.byte_offset)
end
log.debug "Setting Position #{clock}-#{position}, #{position.binary}"
raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(InstructionPosition)
set_instruction( position.instruction )
log.debug "Setting Position #{clock}-#{position}, "
#raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(InstructionPosition)
set_instruction( position.object )
@clock += 1
@pc = position.at
end

View File

@ -85,7 +85,7 @@ module Risc
def position_all
raise "Not translated " unless @translated
#need the initial jump at 0 and then functions
Position.init(cpu_init , -1)
Position.new(cpu_init , 0)
code_start = position_objects( @platform.padding )
# and then everything code
position_code(code_start)
@ -101,8 +101,8 @@ module Risc
sorted.each do | objekt|
next if objekt.is_a?( Parfait::BinaryCode) or objekt.is_a?( Risc::Label )
before = at
position = Position.init(objekt,at)
previous.register_event(:position_changed , PositionListener.new(objekt)) if previous
position = Position.new(objekt , at)
previous.position_listener(objekt) if previous
previous = position
at += objekt.padded_length
log.debug "Object #{objekt.class}:#{before.to_s(16)} len: #{(at - before).to_s(16)}"
@ -123,7 +123,7 @@ module Risc
type.methods.each_method do |method|
last_code = CodeListener.init(method.binary , code_start)
InstructionListener.init(method.cpu_instructions, method.binary)
last_code.register_event(:position_changed , prev_code.object) if prev_code
last_code.position_listener( prev_code.object) if prev_code
prev_code = last_code
code_start = last_code.next_slot
end

View File

@ -48,7 +48,7 @@ module Risc
Position.set_to(position , at)
if code.next
listener = PositionListener.new(code.next)
position.register_event(:position_changed , listener)
position.position_listener( listener)
end
at += code.padded_length unless at < 0
code = code.next

View File

@ -62,7 +62,7 @@ module Risc
nekst = instruction.next
if nekst
listener = InstructionListener.new( nekst , code )
position.register_event(:position_changed , listener)
position.position_listener(listener)
end
instruction = nekst
end

View File

@ -34,6 +34,20 @@ module Risc
Position.set_to(self , pos)
end
# utility to register events of type :position_changed
# can give an object and a PositionListener will be created for it
def position_listener(listener)
unless listener.class.name.include?("Listener")
listener = PositionListener.new(listener)
end
register_event(:position_changed , listener)
end
# utility to get all registered listeners to the :position_changed event
# returns an array
def position_listeners
event_table[:position_changed]
end
#look for InstructionListener and return its code if found
def get_code
listener = event_table.find{|one| one.class == InstructionListener}