Class: Remian::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/remian/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Memory

Returns a new instance of Memory.



7
8
9
10
# File 'lib/remian/memory.rb', line 7

def initialize size
  @size = size
  @store = Array.new @size, ""
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/remian/memory.rb', line 5

def size
  @size
end

#storeObject

Returns the value of attribute store.



5
6
7
# File 'lib/remian/memory.rb', line 5

def store
  @store
end

Instance Method Details

#read(address) ⇒ Object



12
13
14
15
# File 'lib/remian/memory.rb', line 12

def read address
  memory_key = convert_address_to_memory_key address
  @store[memory_key]
end

#write(address, value) ⇒ Object



17
18
19
20
21
22
# File 'lib/remian/memory.rb', line 17

def write address, value
  memory_key = convert_address_to_memory_key address
  if memory_key >= 0 && memory_key < @store.length
    @store[memory_key] = value
  end
end

#write_data(data) ⇒ Object



24
25
26
27
28
# File 'lib/remian/memory.rb', line 24

def write_data data
  data.split("\n").each_with_index do |instruction, i|
    write i + 1, instruction
  end
end