Module: RPicSim::Sim::ClassDefinitionMethods

Included in:
RPicSim::Sim
Defined in:
lib/rpicsim/sim.rb

Overview

These methods should be called while defining a subclass of RPicSim::Sim.

Instance Method Summary collapse

Instance Method Details

#def_pin(our_name, datasheet_name) ⇒ Object

Define a pin alias.

Parameters:

  • our_name (Symbol)

    Specifies what you would like to call the pin. A method with this name will be added to your class's Shortcuts module so it is available as a method on instances of your class and also in your RSpec tests.

  • datasheet_name (Symbol)

    A symbol like :RB3 that specifies what pin it is.



72
73
74
75
76
77
# File 'lib/rpicsim/sim.rb', line 72

def def_pin(our_name, datasheet_name)
  our_name = our_name.to_sym
  @pin_aliases[our_name] = datasheet_name.to_sym

  self::Shortcuts.send(:define_method, our_name) { pin our_name }
end

#def_symbol(name, address, memory_type = nil) ⇒ Object

Define a symbol. Normally symbols are loaded by #use_file or #import_symbols, but this method allows for adding additional symbols one at a time.

See ProgramFile#def_symbol for details about the parameters this method takes.



61
62
63
# File 'lib/rpicsim/sim.rb', line 61

def def_symbol(name, address, memory_type = nil)
  program_file.def_symbol name, address, memory_type
end

#def_var(name, type, opts = {}) ⇒ Object

Define a variable.

Parameters:

  • name (Symbol)

    Specifies what you would like to call the variable. A method with this name will be added to your class's Shortcuts module so it is available as a method on instances of your class and also in your RSpec tests. The method will return a Variable object that you can use to read or write the value of the actual variable in the simulation.

  • type (Symbol)

    Specifies how to interpret the data in the variable and its size. For integers, it should be one of :uint8, :int8, :uint16, :int16, :uint24, :int24, :uint32, :int32, or :word. The s stands for signed and the u stands for unsigned, and the number stands for the number of bits. All multi-byte integers are considered to be little Endian.

  • opts (Hash) (defaults to: {})

    Specifies additional options. The options are:

    • :memory: Specifies the memory that the variable lives in. Valid values are :ram (default), :eeprom, and :program_memory.

    • :symbol: By default, we look for a symbol with the same name as the variable and use that as the location of the variable. This option lets you specify a different symbol to look for in the firmware, so you could call the variable one thing in your firmware and call it a different thing in your tests. This option is ignored if +:address is specified.

    • :address: An integer to use as the address of the variable.



98
99
100
101
102
103
104
105
106
# File 'lib/rpicsim/sim.rb', line 98

def def_var(name, type, opts = {})
  if @variable_set.nil?
    raise 'The device and filename need to be specified before defining variables.'
  end

  @variable_set.def_var(name, type, opts)

  self::Shortcuts.send(:define_method, name) { var name }
end

#import_symbols(symbol_source) ⇒ Object

Specifies additional symbols to use in the simulation. Symbols might be loaded from the program file when you call #use_file, but if those symbols are not sufficient then you can call this method to incorporate another source of symbols.

See ProgramFile#import_symbols for details on what the parameter should be.



51
52
53
# File 'lib/rpicsim/sim.rb', line 51

def import_symbols(symbol_source)
  program_file.import_symbols(symbol_source)
end

#use_device(device) ⇒ Object

Specifies what exact device the firmware runs on.

Parameters:

  • device (String)

    The device name, for example “PIC10F322”.



29
30
31
32
# File 'lib/rpicsim/sim.rb', line 29

def use_device(device)
  @device = device
  @assembly = Mplab::MplabAssembly.new(device)
end

#use_file(filename) ⇒ Object

Specifies the path to the firmware file. The file can be a HEX or COF file, but COF is recommended so that you can access symbol/label addresses and other debugging information. You must call #use_device before calling this.



38
39
40
41
42
# File 'lib/rpicsim/sim.rb', line 38

def use_file(filename)
  raise "The device needs to be specified before filename (e.g. 'use_device \"PIC10F322\"')" unless @device
  @filename = filename.to_s
  load_program_file
end