move all position setting into position

Position and subclasses handle the logic, external to
the classes, so it can be swapped out later
(at runtime positions can’t change)
This commit is contained in:
Torsten Ruger
2018-05-07 22:30:43 +03:00
parent 68fb9b1bdc
commit ce3cc72f9e
10 changed files with 52 additions and 123 deletions

View File

@ -35,6 +35,9 @@ module Risc
def to_s
"0x#{@at.to_s(16)}"
end
# just a callback after creation AND insertion
def init
end
def reset_to(pos)
return false if pos == at
if((at - pos).abs > 1000)
@ -72,7 +75,10 @@ module Risc
old.reset_to(pos)
return old
end
self.positions[object] = for_at( object , pos , extra)
position = for_at( object , pos , extra)
self.positions[object] = position
position.init
position
end
def self.for_at(object , at , extra)
@ -96,9 +102,26 @@ module Risc
@binary = binary
end
def set_position( position , binary = nil)
raise "invalid position #{position}" if position > 15
binary = Risc::Position.get(self).binary unless binary
Risc::Position.set(self, position , binary)
position += byte_length / 4 #assumes 4 byte instructions, as does the whole setup
if self.next
if( 3 == position % 15) # 12 is the amount of instructions that fit into a BinaryCode
position = 3
binary = binary.next
end
self.next.set_position( position , binary)
else
position
end
end
def reset_to(pos)
changed = super(pos)
puts "Reset (#{changed}) #{instruction}"
#puts "Reset (#{changed}) #{instruction}"
return unless changed
return unless instruction.next
instruction.next.set_position( pos + instruction.byte_length , 0)