Class: RPicSim::MemoryWatcher

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

Overview

This class can attach to an MPLAB X memory class and watch for changes in it, and report those changes as a hash associating variable names to new values. This is very useful for writing tests that assert that not only were the desired variables written to, but also that no other variables were written to.

This class does not analyze the current instruction being executed; it uses the Attach method of the MPLAB X memory classes. This means that it doesn't work properly in some versions of MPLAB X. See Flaws.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sim, memory, vars) ⇒ MemoryWatcher

Creates a new instance.

Parameters:

  • sim (Sim)
  • memory (Mplab::MplabMemory)

    The memory to watch

  • vars (Array(Variable))


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rpicsim/memory_watcher.rb', line 27

def initialize(sim, memory, vars)
  # Populate the @vars_by_address instance hash
  @vars_by_address = {}
  vars.each do |var|
    var.addresses.each do |address|
      if @vars_by_address[address]
        raise 'Variable %s overlaps with %s at 0x%x' %
          [var, @vars_by_address[address], address]
      end
      @vars_by_address[address] = var
    end
  end

  @sim = sim
  @memory = memory
  @memory.on_change { |ar| handle_change(ar) }

  @vars_written = Set.new
  @var_names_ignored = default_var_names_ignored(sim.device)
end

Instance Attribute Details

#var_names_ignoredArray(Symbol)

Allows you to read or write the list of variable names whose changes will be ignored. By default, this includes registers like WREG and STATUS that change a lot.

Returns:

  • (Array(Symbol))


21
22
23
# File 'lib/rpicsim/memory_watcher.rb', line 21

def var_names_ignored
  @var_names_ignored
end

Instance Method Details

#clearObject

Clears the record of what variables have been written to.



67
68
69
# File 'lib/rpicsim/memory_watcher.rb', line 67

def clear
  @vars_written.clear
end

#writesHash

Generate a nice report of what variables have been written to since the last time #clear was called.

Returns:

  • (Hash)

    A hash where the keys are names (as symbols) of variables that were written to, and the value is the variable's current value. If an address was written to that does not correspond to a variable, the key for that will just be address as an integer.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rpicsim/memory_watcher.rb', line 54

def writes
  hash = {}
  @vars_written.each do |var|
    if var.is_a? Integer
      hash[var] = @memory.read_word(var)
    else
      hash[var.name] = var.value
    end
  end
  hash
end