Class: RPicSim::MemoryWatcher
- Inherits:
-
Object
- Object
- RPicSim::MemoryWatcher
- 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
-
#var_names_ignored ⇒ Array(Symbol)
Allows you to read or write the list of variable names whose changes will be ignored.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears the record of what variables have been written to.
-
#initialize(sim, memory, vars) ⇒ MemoryWatcher
constructor
Creates a new instance.
-
#writes ⇒ Hash
Generate a nice report of what variables have been written to since the last time #clear was called.
Constructor Details
#initialize(sim, memory, vars) ⇒ MemoryWatcher
Creates a new instance.
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_ignored ⇒ Array(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.
21 22 23 |
# File 'lib/rpicsim/memory_watcher.rb', line 21 def var_names_ignored @var_names_ignored end |
Instance Method Details
#clear ⇒ Object
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 |
#writes ⇒ Hash
Generate a nice report of what variables have been written to since the last time #clear was called.
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 |