Class: RPicSim::Storage::Register

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mplab_register, memory, width) ⇒ Register

Returns a new instance of Register

Parameters:

  • mplab_register (Mplab::MplabRegister)
  • memory

    An optional parameter that enables memory_value.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rpicsim/storage/register.rb', line 10

def initialize(mplab_register, memory, width)
  @mplab_register = mplab_register
  @name = mplab_register.name.to_sym

  @size = case width
          when 8 then 1
          when 16 then 2
          when 24 then 3
          when 32 then 4
          else raise "Unsupported register width: #{name} is #{width}-bit."
          end

  var_type = case size
             when 1 then MemoryUInt8
             when 2 then MemoryUInt16
             when 3 then MemoryUInt24
             when 4 then MemoryUInt32
             end

  @var = var_type.new(name, address).bind(memory)
end

Instance Attribute Details

#nameObject (readonly)



3
4
5
# File 'lib/rpicsim/storage/register.rb', line 3

def name
  @name
end

#sizeObject (readonly)

The size of the register in bytes.



6
7
8
# File 'lib/rpicsim/storage/register.rb', line 6

def size
  @size
end

Instance Method Details

#addressInteger

Gets the address of the register.

Returns:

  • (Integer)


60
61
62
# File 'lib/rpicsim/storage/register.rb', line 60

def address
  @mplab_register.address
end

#addressesRange

Gets the range of addresses occupied.

Returns:

  • (Range)

    A range of integers.



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

def addresses
  address...(address + size)
end

#inspectObject



74
75
76
# File 'lib/rpicsim/storage/register.rb', line 74

def inspect
  '<%s %s 0x%x>' % [self.class, name, address]
end

#memory_valueObject

Reads the value directly from the memory object backing the register.



54
55
56
# File 'lib/rpicsim/storage/register.rb', line 54

def memory_value
  @var.value
end

#memory_value=(value) ⇒ Object

For some registers, like STATUS, you cannot read and write the full range of possible values using #value= because some bits are not writable by the CPU. This setter gets around that by writing directly to the memory object that backs the register.



49
50
51
# File 'lib/rpicsim/storage/register.rb', line 49

def memory_value=(value)
  @var.value = value
end

#to_sObject



70
71
72
# File 'lib/rpicsim/storage/register.rb', line 70

def to_s
  name.to_s
end

#valueInteger

Reads the value of the register.

Returns:

  • (Integer)


40
41
42
# File 'lib/rpicsim/storage/register.rb', line 40

def value
  @mplab_register.read
end

#value=(val) ⇒ Object

Sets the value of the register.

Parameters:

  • val (Integer)


34
35
36
# File 'lib/rpicsim/storage/register.rb', line 34

def value=(val)
  @mplab_register.write val
end