Class: Remian::InstructionSet
- Inherits:
-
Object
- Object
- Remian::InstructionSet
- Defined in:
- lib/remian/instruction_set.rb,
lib/remian/instruction_set/cp.rb,
lib/remian/instruction_set/mv.rb,
lib/remian/instruction_set/or.rb,
lib/remian/instruction_set/and.rb,
lib/remian/instruction_set/not.rb,
lib/remian/instruction_set/put.rb,
lib/remian/instruction_set/set.rb,
lib/remian/instruction_set/exit.rb,
lib/remian/instruction_set/jump.rb,
lib/remian/instruction_set/zero.rb,
lib/remian/instruction_set/add_i.rb,
lib/remian/instruction_set/put_a.rb,
lib/remian/instruction_set/branch.rb
Instance Attribute Summary collapse
-
#memory ⇒ Object
Returns the value of attribute memory.
-
#registers ⇒ Object
Returns the value of attribute registers.
Instance Method Summary collapse
- #add_i(addresses) ⇒ Object
- #and(addresses) ⇒ Object
- #branch(addresses) ⇒ Object
- #cp(addresses) ⇒ Object
- #exit(addresses) ⇒ Object
-
#initialize(memory, registers) ⇒ InstructionSet
constructor
A new instance of InstructionSet.
- #jump(addresses) ⇒ Object
- #mv(addresses) ⇒ Object
- #not(addresses) ⇒ Object
- #or(addresses) ⇒ Object
- #put(data) ⇒ Object
- #put_a(addresses) ⇒ Object
- #set(addresses) ⇒ Object
- #zero(addresses) ⇒ Object
Constructor Details
#initialize(memory, registers) ⇒ InstructionSet
Returns a new instance of InstructionSet.
18 19 20 21 |
# File 'lib/remian/instruction_set.rb', line 18 def initialize memory, registers @memory = memory @registers = registers end |
Instance Attribute Details
#memory ⇒ Object
Returns the value of attribute memory.
15 16 17 |
# File 'lib/remian/instruction_set.rb', line 15 def memory @memory end |
#registers ⇒ Object
Returns the value of attribute registers.
16 17 18 |
# File 'lib/remian/instruction_set.rb', line 16 def registers @registers end |
Instance Method Details
#add_i(addresses) ⇒ Object
3 4 5 6 7 |
# File 'lib/remian/instruction_set/add_i.rb', line 3 def add_i addresses operands = addresses.map {|address| @memory.read(address.to_i).to_i} sum = operands.inject :+ @memory.write addresses[0].to_i, sum end |
#and(addresses) ⇒ Object
3 4 5 6 |
# File 'lib/remian/instruction_set/and.rb', line 3 def and addresses operands = addresses.map {|address| @memory.read(address.to_i).to_i} @memory.write(addresses[0].to_i, operands.inject(:*)) end |
#branch(addresses) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/remian/instruction_set/branch.rb', line 3 def branch addresses if @memory.read(addresses[0].to_i).to_i != 0 destination = @memory.read(addresses[1].to_i).to_i Remian.say "Branching to address #{destination}.", :magenta @registers[:address] = destination end end |
#cp(addresses) ⇒ Object
3 4 5 |
# File 'lib/remian/instruction_set/cp.rb', line 3 def cp addresses @memory.write(addresses[1].to_i, @memory.read(addresses[0].to_i)) end |
#exit(addresses) ⇒ Object
3 4 5 6 |
# File 'lib/remian/instruction_set/exit.rb', line 3 def exit addresses Remian.say "Told to exit.", :magenta Process.exit end |
#jump(addresses) ⇒ Object
3 4 5 6 7 |
# File 'lib/remian/instruction_set/jump.rb', line 3 def jump addresses destination = @memory.read(addresses[0].to_i).to_i Remian.say "Jumping to address #{destination}.", :magenta @registers[:address] = destination end |
#mv(addresses) ⇒ Object
3 4 5 6 |
# File 'lib/remian/instruction_set/mv.rb', line 3 def mv addresses @memory.write(addresses[1].to_i, @memory.read(addresses[0].to_i)) @memory.write(addresses[0].to_i, "") end |
#not(addresses) ⇒ Object
3 4 5 |
# File 'lib/remian/instruction_set/not.rb', line 3 def not addresses @memory.write(addresses[0].to_i, @memory.read(addresses[0].to_i) == 0) end |
#or(addresses) ⇒ Object
3 4 5 6 |
# File 'lib/remian/instruction_set/or.rb', line 3 def or addresses operands = addresses.map {|address| @memory.read(address.to_i).to_i} @memory.write(addresses[0].to_i, operands.inject(:+) != 0) end |
#put(data) ⇒ Object
3 4 5 |
# File 'lib/remian/instruction_set/put.rb', line 3 def put data puts data end |
#put_a(addresses) ⇒ Object
3 4 5 6 |
# File 'lib/remian/instruction_set/put_a.rb', line 3 def put_a addresses operands = addresses.map {|address| @memory.read(address.to_i)} puts operands end |
#set(addresses) ⇒ Object
3 4 5 |
# File 'lib/remian/instruction_set/set.rb', line 3 def set addresses @memory.write(operand[0], operand[1]) end |
#zero(addresses) ⇒ Object
3 4 5 6 |
# File 'lib/remian/instruction_set/zero.rb', line 3 def zero addresses operands = addresses.map {|address| @memory.read(address.to_i).to_i} @memory.write(addresses[0].to_i, operands[0].to_i == 0) end |