introduce base class for get_slot and get_byte called getter
This commit is contained in:
parent
ef285a146f
commit
e479b00b29
@ -118,6 +118,7 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
require_relative "instructions/setter"
|
require_relative "instructions/setter"
|
||||||
|
require_relative "instructions/getter"
|
||||||
require_relative "instructions/set_slot"
|
require_relative "instructions/set_slot"
|
||||||
require_relative "instructions/get_slot"
|
require_relative "instructions/get_slot"
|
||||||
require_relative "instructions/set_byte"
|
require_relative "instructions/set_byte"
|
||||||
|
@ -5,23 +5,16 @@ module Register
|
|||||||
# indexes are 1 based (as for slots) , which means we sacrifice a byte of every word
|
# indexes are 1 based (as for slots) , which means we sacrifice a byte of every word
|
||||||
# for our sanity
|
# for our sanity
|
||||||
|
|
||||||
class GetByte < Instruction
|
class GetByte < Getter
|
||||||
|
|
||||||
# If you had a c array (of int8) and index offset
|
# If you had a c array (of int8) and index offset
|
||||||
# the instruction would do register = array[index]
|
# the instruction would do register = array[index]
|
||||||
# The arguments are in the order that makes sense for the Instruction name
|
# 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)
|
# So GetSlot means the slot (array and index) moves to the register (last argument)
|
||||||
def initialize source , array , index , register
|
# def initialize source , array , index , register
|
||||||
super(source)
|
# super
|
||||||
@array = array
|
# end
|
||||||
@index = index
|
# attr_accessor :array , :index , :register
|
||||||
@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 to_s
|
def to_s
|
||||||
"GetByte: #{array}[#{index}] -> #{register}"
|
"GetByte: #{array}[#{index}] -> #{register}"
|
||||||
|
@ -4,31 +4,21 @@ module Register
|
|||||||
# SetSlot moves data into memory from a register.
|
# SetSlot moves data into memory from a register.
|
||||||
# Both use a base memory (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
|
# 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.
|
# an element in an array, in the case of GetSlot setting the value in the array.
|
||||||
|
|
||||||
# btw: to move data between registers, use RegisterTransfer
|
# btw: to move data between registers, use RegisterTransfer
|
||||||
|
|
||||||
class GetSlot < Instruction
|
class GetSlot < Getter
|
||||||
|
|
||||||
# If you had a c array and index offset
|
# If you had a c array and index offset
|
||||||
# the instruction would do register = array[index]
|
# the instruction would do register = array[index]
|
||||||
# The arguments are in the order that makes sense for the Instruction name
|
# 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)
|
# So GetSlot means the slot (array and index) moves to the register (last argument)
|
||||||
def initialize source , array , index , register
|
# def initialize source , array , index , register
|
||||||
super(source)
|
# super
|
||||||
@array = array
|
# end
|
||||||
@index = index
|
# attr_accessor :array , :index , :register
|
||||||
@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 to_s
|
def to_s
|
||||||
"GetSlot: #{array}[#{index}] -> #{register}"
|
"GetSlot: #{array}[#{index}] -> #{register}"
|
||||||
|
34
lib/register/instructions/getter.rb
Normal file
34
lib/register/instructions/getter.rb
Normal file
@ -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
|
@ -1,12 +1,15 @@
|
|||||||
module Register
|
module Register
|
||||||
# Setter is a base class for set instructions (SetSlot and SetByte , possibly more coming)
|
# 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
|
# Setter has a
|
||||||
# - Register that the data is moved to
|
# - Register that the data is comes from
|
||||||
# - an array where the data comes from
|
# - an array where the data goes
|
||||||
# - and (array) index
|
# - and (array) index
|
||||||
|
|
||||||
|
# Getter and Setter api follow the pattern from -> to
|
||||||
|
|
||||||
class Setter < Instruction
|
class Setter < Instruction
|
||||||
|
|
||||||
# If you had a c array and index offset
|
# If you had a c array and index offset
|
||||||
|
Loading…
x
Reference in New Issue
Block a user