Class: RPicSim::Memory

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

Overview

This object allows read and write access to the data currently stored in a memory space of the simulated device. Instances are usually retrieved from a Sim object by calling Sim#ram, Sim#program_memory, or Sim#eeprom.

The behavior of read_word and write_word differs depending on what kind of Memory is being used, as shown in the table below:

Memory type                Address type   Read/write chunk for word methods
RAM and EEPROM:            Byte address   1 byte (8 bits)
PIC18 program memory:      Byte address   1 word (16 bits)
Non-PIC18 program memory:  Word address   1 word (12 or 14 bits)

The read_byte and write_byte methods use the same type of address as read_word and write_word, but they can only read and write from the lower 8 bits of the word. The upper bits of the word, if there are any, are left unchanged. For RAM and EEPROM, read_byte and write_byte behave the same way as read_word and write_word.

For more information, see Memories.

Instance Method Summary collapse

Instance Method Details

#read_byte(address) ⇒ Integer

Reads the byte in memory at the specified address.

Parameters:

  • address (Integer)

Returns:

  • (Integer)


37
38
39
# File 'lib/rpicsim/memory.rb', line 37

def read_byte(address)
  @mplab_memory.read_byte(address)
end

#read_bytes(address, size) ⇒ Array(Integer)

Reads a series of bytes from the simulated memory using read_byte and returns them as an array.

Parameters:

  • address (Integer)

    The address of the first byte to read.

  • size (Integer)

    The number of bytes to read.

Returns:

  • (Array(Integer))


76
77
78
# File 'lib/rpicsim/memory.rb', line 76

def read_bytes(address, size)
  size.times.map { |i| read_byte(address + i) }
end

#read_word(address) ⇒ Integer

Reads the word in memory at the specified address.

Parameters:

  • address (Integer)

Returns:

  • (Integer)


52
53
54
# File 'lib/rpicsim/memory.rb', line 52

def read_word(address)
  @mplab_memory.read_word(address)
end

#valid_address?(address) ⇒ Boolean

Returns true if the specified address is valid.

Parameters:

  • address (Integer)

Returns:

  • (Boolean)

    true or false



66
67
68
# File 'lib/rpicsim/memory.rb', line 66

def valid_address?(address)
  @mplab_memory.valid_address?(address)
end

#write_byte(address, value) ⇒ Object

Writes the given integer to the byte in memory at the specified address.

Parameters:

  • address (Integer)
  • value (Integer)


45
46
47
# File 'lib/rpicsim/memory.rb', line 45

def write_byte(address, value)
  @mplab_memory.write_byte(address, value)
end

#write_bytes(address, bytes) ⇒ Object

Writes a series of bytes to the simulated memory.

Parameters:

  • address (Integer)
  • bytes

    Array of integers or a string.



84
85
86
87
88
# File 'lib/rpicsim/memory.rb', line 84

def write_bytes(address, bytes)
  bytes = bytes.bytes if bytes.is_a?(String)
  bytes.each_with_index { |b, i| write_byte(address + i, b) }
  nil
end

#write_word(address, value) ⇒ Object

Writes the given integer to the word in memory at the specified address.

Parameters:

  • address (Integer)
  • value (Integer)


59
60
61
# File 'lib/rpicsim/memory.rb', line 59

def write_word(address, value)
  @mplab_memory.write_word(address, value)
end