Class: MemoryChip

Inherits:
Object
  • Object
show all
Defined in:
lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb

Overview

Simulation of the CPU memory: filled with "." for debug purpose.

Instance Method Summary collapse

Constructor Details

#initialize(size, width = 8, memory_map = {}) ⇒ MemoryChip

Returns a new instance of MemoryChip.



194
195
196
197
198
199
200
201
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 194

def initialize(size,width = 8, memory_map = {})
  @sync  = Mutex.new
  @width = width
  @data_mask = (2 << @width) - 1
  @addr_mask = (2 << size) - 1
  @cells = [0] * size
  @memory_map = memory_map
end

Instance Method Details

#[](addr) ⇒ Object

Read the memory.



208
209
210
211
212
213
214
215
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 208

def [](addr)
  addr = addr.to_i & @addr_mask
  # Is it a memory mapped address?
  loc = @memory_map[addr]
  return loc.call if loc # Yes.
  # No, normal access.
  return @cells[addr]
end

#[]=(addr, val) ⇒ Object

Write the memory.



218
219
220
221
222
223
224
225
226
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 218

def []=(addr,val)
  addr = addr.to_i & @addr_mask
  val  = val.to_i  & @data_mask
  # Is it a memory mapped address?
  loc = @memory_map[addr]
  return loc.call(val) if loc
  # No, normal access.
  @cells[addr] = val
end

#synchronize(&ruby_block) ⇒ Object



203
204
205
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 203

def synchronize(&ruby_block)
  @sync.synchronize(&ruby_block)
end