diff --git a/lib/register/instruction.rb b/lib/register/instruction.rb index 13501cde..a362a32f 100644 --- a/lib/register/instruction.rb +++ b/lib/register/instruction.rb @@ -118,6 +118,7 @@ module Register end require_relative "instructions/setter" +require_relative "instructions/getter" require_relative "instructions/set_slot" require_relative "instructions/get_slot" require_relative "instructions/set_byte" diff --git a/lib/register/instructions/get_byte.rb b/lib/register/instructions/get_byte.rb index ef446762..e33bb412 100644 --- a/lib/register/instructions/get_byte.rb +++ b/lib/register/instructions/get_byte.rb @@ -5,23 +5,16 @@ module Register # indexes are 1 based (as for slots) , which means we sacrifice a byte of every word # for our sanity - class GetByte < Instruction + class GetByte < Getter # If you had a c array (of int8) and index offset # the instruction would do register = array[index] # The arguments are in the order that makes sense for the Instruction name # So GetSlot means the slot (array and index) moves to the register (last argument) - def initialize source , array , index , register - super(source) - @array = array - @index = index - @register = register - raise "index 0 " if index == 0 - raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) - raise "Not register #{register}" unless RegisterValue.look_like_reg(register) - raise "Not register #{array}" unless RegisterValue.look_like_reg(array) - end - attr_accessor :array , :index , :register + # def initialize source , array , index , register + # super + # end + # attr_accessor :array , :index , :register def to_s "GetByte: #{array}[#{index}] -> #{register}" diff --git a/lib/register/instructions/get_slot.rb b/lib/register/instructions/get_slot.rb index 0426b598..004e4c23 100644 --- a/lib/register/instructions/get_slot.rb +++ b/lib/register/instructions/get_slot.rb @@ -4,31 +4,21 @@ module Register # SetSlot moves data into memory from a register. # Both use a base memory (a register) - # While the virtual machine has only one instruction (Set) to move data between slots, - # the register has two, namely GetSlot and SetSlot - # # This is because that is what cpu's can do. In programming terms this would be accessing # an element in an array, in the case of GetSlot setting the value in the array. # btw: to move data between registers, use RegisterTransfer - class GetSlot < Instruction + class GetSlot < Getter # If you had a c array and index offset # the instruction would do register = array[index] # The arguments are in the order that makes sense for the Instruction name # So GetSlot means the slot (array and index) moves to the register (last argument) - def initialize source , array , index , register - super(source) - @array = array - @index = index - @register = register - raise "index 0 " if index == 0 - raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) - raise "Not register #{register}" unless RegisterValue.look_like_reg(register) - raise "Not register #{array}" unless RegisterValue.look_like_reg(array) - end - attr_accessor :array , :index , :register + # def initialize source , array , index , register + # super + # end + # attr_accessor :array , :index , :register def to_s "GetSlot: #{array}[#{index}] -> #{register}" diff --git a/lib/register/instructions/getter.rb b/lib/register/instructions/getter.rb new file mode 100644 index 00000000..3921fd04 --- /dev/null +++ b/lib/register/instructions/getter.rb @@ -0,0 +1,34 @@ +module Register + + # Getter is a base class for get instructions (GetSlot and GetByte , possibly more coming) + # + # The instruction that is modelled is loading data from an array into a register + # + # Getter has a + # - an array where the data comes from + # - and (array) index + # - Register that the data is moved to + + # Getter and Setter api follow the pattern from -> to + + class Getter < Instruction + + # If you had a c array and index offset + # the instruction would do register = array[index] + # The arguments are in the order that makes sense for the Instruction name + # So GetSlot means the slot (array and index) moves to the register (last argument) + def initialize source , array , index , register + super(source) + @array = array + @index = index + @register = register + raise "index 0 " if index == 0 + raise "Not integer or reg #{index}" unless index.is_a?(Numeric) or RegisterValue.look_like_reg(index) + raise "Not register #{register}" unless RegisterValue.look_like_reg(register) + raise "Not register #{array}" unless RegisterValue.look_like_reg(array) + end + attr_accessor :array , :index , :register + + end + +end diff --git a/lib/register/instructions/setter.rb b/lib/register/instructions/setter.rb index 1d72109f..ee366372 100644 --- a/lib/register/instructions/setter.rb +++ b/lib/register/instructions/setter.rb @@ -1,12 +1,15 @@ module Register # Setter is a base class for set instructions (SetSlot and SetByte , possibly more coming) # - # The instruction that is modelled is loading data from an array into a register + # The instruction that is modelled is loading data from a register into an array # # Setter has a - # - Register that the data is moved to - # - an array where the data comes from + # - Register that the data is comes from + # - an array where the data goes # - and (array) index + + # Getter and Setter api follow the pattern from -> to + class Setter < Instruction # If you had a c array and index offset