From 4b34546c113103f6ea64e4055e12794141415eda Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 28 May 2018 18:20:09 +0300 Subject: [PATCH] fixing the binary code indexes AGAIN, that third of all bugs, the off by one --- lib/parfait/binary_code.rb | 28 ++++++++++++----------- lib/risc/parfait_adapter.rb | 2 +- lib/risc/position/code_position.rb | 2 +- lib/risc/position/instruction_position.rb | 17 +++++++------- test/parfait/test_binary_code.rb | 25 +++++++++++++------- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/lib/parfait/binary_code.rb b/lib/parfait/binary_code.rb index 399f7110..5a55bda5 100644 --- a/lib/parfait/binary_code.rb +++ b/lib/parfait/binary_code.rb @@ -15,9 +15,8 @@ module Parfait def self.byte_offset self.type_length * 4 # size of type * word_size (4) end - #16 - 2 -1 , two instance variables and one for the jump def self.data_length - 13 + self.memory_size - self.type_length - 1 #one for the jump end def data_length self.class.data_length @@ -29,14 +28,15 @@ module Parfait def initialize(total_size) super() extend_to(total_size ) - #puts "Init with #{total_size} for #{object_id}" - (0 ..(data_length)).each{ |index| set_word(index , 0) } + (0 ... data_length).each{ |index| set_word(index , 0) } + set_last(0) end def extend_to(total_size) - return unless total_size > self.data_length + return if total_size < data_length extend_one() unless @next @next.extend_to(total_size - data_length) end + def extend_one() @next = BinaryCode.new(1) if Risc::Position.set?(self) @@ -57,34 +57,36 @@ module Parfait def each_word( all = true) index = 0 - length = data_length - length += 1 if all - while( index < length) + while( index < data_length) yield get_word(index) index += 1 end + yield( get_last ) if all end def set_word(index , word) raise "invalid index #{index}" if index < 0 - if index > data_length + if index >= data_length #raise "invalid index #{index}" unless @next extend_to( index ) @next.set_word( index - data_length , word) else - set_internal_word(index + 2 , word) + set_internal_word(index + BinaryCode.type_length , word) end end + def get_last() + get_internal_word(data_length + BinaryCode.type_length) + end def set_last(word) - set_word( data_length , word) + set_internal_word(data_length + BinaryCode.type_length , word) end def get_word(index) raise "invalid index #{index}" if index < 0 - if index > data_length + 1 + if index >= data_length raise "invalid index #{index}" unless @next return @next.get_word( index - data_length) end - get_internal_word(index + 2) + get_internal_word(index + BinaryCode.type_length) end def set_char(index , char) if index >= byte_length diff --git a/lib/risc/parfait_adapter.rb b/lib/risc/parfait_adapter.rb index 2e5199c9..68974994 100644 --- a/lib/risc/parfait_adapter.rb +++ b/lib/risc/parfait_adapter.rb @@ -15,7 +15,7 @@ module Parfait @memory[index] end - # 1 -based index + # 0 -based index def set_internal_word(index , value) return super if index < self.class.type_length raise "Word[#{index}] = nil" if( value.nil? and self.class != List) diff --git a/lib/risc/position/code_position.rb b/lib/risc/position/code_position.rb index 36baf04e..d3154def 100644 --- a/lib/risc/position/code_position.rb +++ b/lib/risc/position/code_position.rb @@ -33,8 +33,8 @@ module Risc end end def reset_to(pos , ignored) - init(pos , ignored) super(pos, ignored) + init(pos , ignored) Position.log.debug "ResetCode (#{pos.to_s(16)}) #{code}" end # insert a jump to the next instruction, at the last instruction diff --git a/lib/risc/position/instruction_position.rb b/lib/risc/position/instruction_position.rb index a2cf1427..2b06d7da 100644 --- a/lib/risc/position/instruction_position.rb +++ b/lib/risc/position/instruction_position.rb @@ -23,28 +23,27 @@ module Risc @binary = binary end def init(at, binary) + @binary = binary return if at == 0 and binary.nil? raise "faux pas" if at < Position.get(binary).at return unless @instruction.next - @binary = binary nekst = at + @instruction.byte_length diff = nekst - Position.get(@binary).at Position.log.debug "Diff: #{diff.to_s(16)} , next #{nekst.to_s(16)} , binary #{Position.get(@binary)}" raise "Invalid position #{diff.to_s(16)} , next #{nekst.to_s(16)} #{self}" if diff < 8 - next_binary = @binary - if( (diff % (@binary.padded_length - @instruction.byte_length)) == 0 ) - next_binary.extend_one unless next_binary.next - next_binary = next_binary.next - raise "end of line " unless next_binary - nekst = Position.get(next_binary).at + Parfait::BinaryCode.byte_offset + if( (diff % (binary.padded_length - @instruction.byte_length)) == 0 ) + binary.extend_one unless binary.next + binary = binary.next + raise "end of line " unless binary + nekst = Position.get(binary).at + Parfait::BinaryCode.byte_offset Position.log.debug "Jump to: #{nekst.to_s(16)}" end - Position.set(@instruction.next, nekst , next_binary) + Position.set(@instruction.next, nekst , binary) end def reset_to(pos , binary) - init(pos , binary) super(pos , binary) + init(pos , binary) Position.log.debug "ResetInstruction (#{pos.to_s(16)}) #{instruction}" end end diff --git a/test/parfait/test_binary_code.rb b/test/parfait/test_binary_code.rb index d4c62776..aca39fb3 100644 --- a/test/parfait/test_binary_code.rb +++ b/test/parfait/test_binary_code.rb @@ -52,8 +52,9 @@ module Parfait assert @code.set_char(56 , 120) end def test_nilled - assert_equal 0 , @code.get_word(1) - assert_equal 0 , @code.get_word(13) + assert_equal 0 , @code.get_word(0) + assert_equal 0 , @code.get_word(12) + assert_equal 0 , @code.get_last end def test_get_set_self @code.set_word(10,1) @@ -91,7 +92,7 @@ module Parfait assert_equal 14 , len end def test_each_set - (0..12).each{|i| @code.set_word(i,i)} + (0...13).each{|i| @code.set_word(i,i)} all = [] @code.each_word(false){ |w| all << w} assert_equal 0 , all.first @@ -106,12 +107,14 @@ module Parfait assert_equal 1, @code.get_word(1) end def test_get_internal_word - @code.set_word(1 , 1) - assert_equal 1, @code.get_internal_word(@code.data_start + 1) + @code.set_word(0 , 1) + assert_equal 1, @code.get_internal_word(BinaryCode.type_length) end - def test_set_13 - @code.set_word(13 , 1) + def test_set_12 + @code.set_word(12 , 12) + assert_equal 0 , @code.get_last assert_nil @code.next + assert_equal 12 , @code.get_word(12) end def test_set_last_no_extend @code.set_last(1) @@ -119,12 +122,18 @@ module Parfait end def test_set_last_and_get @code.set_last(1) - assert_equal 1, @code.get_word(BinaryCode.data_length) + assert_equal 1, @code.get_last end def test_has_each sum = 0 @code.each_block{ sum += 1} assert_equal sum , 1 end + def test_step_13 + @code.set_word(13,13) + assert @code.next + assert_equal 13, @code.get_word(13) + assert_equal 13, @code.next.get_word(0) + end end end