Class: RbPIC::RSM

Inherits:
Object
  • Object
show all
Defined in:
lib/rbpic/gen.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRSM

Returns a new instance of RSM.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbpic/gen.rb', line 43

def initialize
  @code = []

  _emit "\t\#include \"p10f202.inc\""
  _emit "\t__config _WDT_OFF & _CP_OFF & _MCLRE_OFF"
  _emit ''
  _emit "\torg\t0x00"
  # Skip over the delay routine

  _emit "\tgoto\t$+2"
  _emit ''
  # Create our default 4 microsecond delay routine

  subr(:four_microsecond_delay) { done 0 }
  _emit ''
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym) ⇒ Object



269
270
271
# File 'lib/rbpic/gen.rb', line 269

def method_missing(sym)
  _emit "\tcall\t#{sym.to_s.capitalize}"
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



26
27
28
# File 'lib/rbpic/gen.rb', line 26

def code
  @code
end

Class Method Details

.load(rsm = nil, &blk) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbpic/gen.rb', line 29

def self.load(rsm=nil, &blk)
  c = RSM.new

  if block_given?
    c.instance_eval &blk
  else
    c.instance_eval IO.read(rsm), rsm
  end

  c.end_code
  c.code
end

Instance Method Details

#add(val, to) ⇒ Object



208
209
210
211
212
# File 'lib/rbpic/gen.rb', line 208

def add(val, to)
  _emit "\tmovlw\t0x#{val.to_s(16)}"
  _emit "\taddwf\t#{to.to_s}, w"
  _emit ''
end

#block(label, &blk) ⇒ Object



137
138
139
140
141
# File 'lib/rbpic/gen.rb', line 137

def block(label, &blk)
  _emit "#{label.to_s.capitalize}"
  self.instance_eval &blk
  _emit ''
end

#clear_bit(var, bit) ⇒ Object



167
168
169
170
# File 'lib/rbpic/gen.rb', line 167

def clear_bit(var, bit)
  _emit "\tbcf\t#{var.to_s}, 0x#{bit.to_s(16)}"
  _emit ''
end

#config_io(pins) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rbpic/gen.rb', line 98

def config_io(pins)
  tris = 0
  
  pins.each do |pin, state|
    case pin
    when :gp0
      state == :out ? tris &= ~0x1 : tris |= 0x1

    when :gp1
      state == :out ? tris &= ~0x2 : tris |= 0x2

    when :gp2
      state == :out ? tris &= ~0x4 : tris |= 0x4

    when :gp3
      state == :out ? tris &= ~0x8 : tris |= 0x8
    end
  end

  _emit "\tmovlw\tb'#{tris.to_s(2).rjust(8, '0')}'"
  _emit "\ttris\tGPIO"
  _emit ''
end

#constants(&b) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rbpic/gen.rb', line 72

def constants(&b)
  c = RSMConstant.new @code
  c.instance_eval &b
  
  _emit "\tendc"
  _emit ''
end

#copy(from, to) ⇒ Object



194
195
196
197
198
# File 'lib/rbpic/gen.rb', line 194

def copy(from, to)
  _emit "\tmovf\t#{from.to_s}, w"
  _emit "\tmovwf\t#{to.to_s}"
  _emit ''
end

#decrement(var) ⇒ Object



257
258
259
260
# File 'lib/rbpic/gen.rb', line 257

def decrement(var)
  _emit "\tdecf\t#{var.to_s}, f"
  _emit ''
end

#decrement_and_test(var, *ops) ⇒ Object



238
239
240
241
242
# File 'lib/rbpic/gen.rb', line 238

def decrement_and_test(var, *ops)
  _emit "\tdecfsz\t#{var.to_s}, f"
  self.send ops[0], ops[1]
  _emit ''
end

#decrement_by(val, sym) ⇒ Object



180
181
182
183
184
# File 'lib/rbpic/gen.rb', line 180

def decrement_by(val, sym)
  _emit "\tmovlw\t0x#{val.to_s(16)}"
  _emit "\tsubwf\t#{sym.to_s}, f"
  _emit ''
end

#delay(secs) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/rbpic/gen.rb', line 228

def delay(secs)
  # Number of 4 microsecond delays to do

  n = secs / 4.0e-6
  n.ceil.to_i.times do
    _emit "\tcall\tFour_microsecond_delay"
  end
  _emit ''
end

#done(w) ⇒ Object



66
67
68
69
# File 'lib/rbpic/gen.rb', line 66

def done(w)
  _emit "\tretlw\t0x#{w.to_s(16)}"
  _emit ''
end

#end_codeObject



263
264
265
266
# File 'lib/rbpic/gen.rb', line 263

def end_code
  _emit "\tend"
  _emit ''
end

#increment(var) ⇒ Object



251
252
253
254
# File 'lib/rbpic/gen.rb', line 251

def increment(var)
  _emit "\tincf\t#{var.to_s}, f"
  _emit ''
end

#increment_by(val, sym) ⇒ Object



187
188
189
190
191
# File 'lib/rbpic/gen.rb', line 187

def increment_by(val, sym)
  _emit "\tmovlw\t0x#{val.to_s(16)}"
  _emit "\taddwf\t#{sym.to_s}, f"
  _emit ''
end

#init_clockObject

The internal 4 MHz oscillator has a calibration value stored in the last memory location. On device reset it is loaded into the W register. init_clock moves the calibration value into the OSCCAL register and configures the OPTION register as:

~GPWU: disabled
~GPPU: enabled
 T0CS: transition on internal instruction cycle clock
 T0SE: increment on high-to-low transition
  PSA: prescalar assigned to WDT

PS<2:0>: WDT rate 1:1



90
91
92
93
94
95
# File 'lib/rbpic/gen.rb', line 90

def init_clock
  _emit "\tmovwf\tOSCCAL"
  _emit "\tmovlw\tb'10011000'"
  _emit "\toption"
  _emit ''
end

#jump(label) ⇒ Object



245
246
247
248
# File 'lib/rbpic/gen.rb', line 245

def jump(label)
  _emit "\tgoto\t#{label.to_s.capitalize}"
  _emit ''
end

#loop(label, &blk) ⇒ Object



130
131
132
133
134
# File 'lib/rbpic/gen.rb', line 130

def loop(label, &blk)
  block label, &blk
  _emit "\tgoto\t#{label.to_s.capitalize}"
  _emit ''
end

#set(sym, val) ⇒ Object



123
124
125
126
127
# File 'lib/rbpic/gen.rb', line 123

def set(sym, val)
  _emit "\tmovlw\t0x#{val.to_s(16)}"
  _emit "\tmovwf\t#{sym.to_s}"
  _emit ''
end

#set_bit(var, bit) ⇒ Object



161
162
163
164
# File 'lib/rbpic/gen.rb', line 161

def set_bit(var, bit)
  _emit "\tbsf\t#{var.to_s}, 0x#{bit.to_s(16)}"
  _emit ''
end

#set_io(ports) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rbpic/gen.rb', line 144

def set_io(ports)
  ports.each do |p, state|
    case p
    when :gp0
      state == :hi ? _emit("\tbsf\tGPIO, 0") : _emit("\tbcf\tGPIO, 0")
    when :gp1
      state == :hi ? _emit("\tbsf\tGPIO, 1") : _emit("\tbcf\tGPIO, 1")
    when :gp2
      state == :hi ? _emit("\tbsf\tGPIO, 2") : _emit("\tbcf\tGPIO, 2")
    else
      abort "Invalid port #{p.to_s} specified"
    end
  end
  _emit ''
end

#subr(label, &b) ⇒ Object



59
60
61
62
63
# File 'lib/rbpic/gen.rb', line 59

def subr(label, &b)
  _emit "#{label.to_s.capitalize}"
  self.instance_eval(&b)
  _emit ''
end

#subtract(val, from) ⇒ Object



215
216
217
218
219
# File 'lib/rbpic/gen.rb', line 215

def subtract(val, from)
  _emit "\tmovlw\t0x#{val.to_s(16)}"
  _emit "\tsubwf\t#{from.to_s}, w"
  _emit ''
end

#subtract_and_set(sym, from) ⇒ Object



222
223
224
225
# File 'lib/rbpic/gen.rb', line 222

def subtract_and_set(sym, from)
  _emit "\tmovf\t#{sym.to_s}, w"
  _emit "\tsubwf\t#{from.to_s}, f"
end

#test(var, bit, target) ⇒ Object



173
174
175
176
177
# File 'lib/rbpic/gen.rb', line 173

def test(var, bit, target)
  _emit "\tbtfss\t#{var.to_s}, 0x#{bit.to_s(16)}"
  _emit "\tgoto\t#{target.to_s.capitalize}"
  _emit ''
end

#test_carry(label) ⇒ Object



201
202
203
204
205
# File 'lib/rbpic/gen.rb', line 201

def test_carry(label)
  _emit "\tbtfss\tSTATUS, 0"
  _emit "\tgoto\t#{label.to_s.capitalize}"
  _emit ''
end