rbpic 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/rbpic-compile +26 -0
  2. data/lib/rbpic/gen.rb +282 -0
  3. metadata +79 -0
data/bin/rbpic-compile ADDED
@@ -0,0 +1,26 @@
1
+ #!ruby
2
+
3
+ require 'trollop'
4
+
5
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'rbpic/gen'
7
+
8
+ opts = Trollop::options do
9
+ version "rbpic-compile #{RbPIC::VERSION} (c) 2006-2012 Patrick J. Franz"
10
+
11
+ banner <<-EOS
12
+ Usage:
13
+ rbpic-compile [options] <script>
14
+
15
+ options:
16
+ EOS
17
+
18
+ opt :assemble_only, "Output assembly only; don't link.", :short => 'S'
19
+ end
20
+
21
+ asm = RbPIC::RSM.load ARGV[0]
22
+
23
+ asm_name = ARGV[0].chomp(File.extname(ARGV[0])) + '.asm'
24
+ File.open(asm_name, 'w') { |f| asm.each { |line| f.puts line } }
25
+
26
+ `mpasmx /pPIC10F202 /t2 #{asm_name}` unless opts[:assemble_only]
data/lib/rbpic/gen.rb ADDED
@@ -0,0 +1,282 @@
1
+ #
2
+ # Code generator for rbPIC.
3
+ #
4
+
5
+ module RbPIC
6
+
7
+ VERSION = '1.0.0'
8
+
9
+ class RSMConstant
10
+
11
+ def initialize(code)
12
+ @code = code
13
+ @code << "\tcblock 0x08"
14
+ end
15
+
16
+
17
+ def method_missing(sym, *args)
18
+ @code << "\t\t#{sym.to_s}"
19
+ end
20
+
21
+ end
22
+
23
+
24
+ class RSM
25
+
26
+ attr_reader :code
27
+
28
+
29
+ def self.load(rsm=nil, &blk)
30
+ c = RSM.new
31
+
32
+ if block_given?
33
+ c.instance_eval &blk
34
+ else
35
+ c.instance_eval IO.read(rsm), rsm
36
+ end
37
+
38
+ c.end_code
39
+ c.code
40
+ end
41
+
42
+
43
+ def initialize
44
+ @code = []
45
+
46
+ _emit "\t\#include \"p10f202.inc\""
47
+ _emit "\t__config _WDT_OFF & _CP_OFF & _MCLRE_OFF"
48
+ _emit ''
49
+ _emit "\torg\t0x00"
50
+ # Skip over the delay routine
51
+ _emit "\tgoto\t$+2"
52
+ _emit ''
53
+ # Create our default 4 microsecond delay routine
54
+ subr(:four_microsecond_delay) { done 0 }
55
+ _emit ''
56
+ end
57
+
58
+
59
+ def subr(label, &b)
60
+ _emit "#{label.to_s.capitalize}"
61
+ self.instance_eval(&b)
62
+ _emit ''
63
+ end
64
+
65
+
66
+ def done(w)
67
+ _emit "\tretlw\t0x#{w.to_s(16)}"
68
+ _emit ''
69
+ end
70
+
71
+
72
+ def constants(&b)
73
+ c = RSMConstant.new @code
74
+ c.instance_eval &b
75
+
76
+ _emit "\tendc"
77
+ _emit ''
78
+ end
79
+
80
+ # The internal 4 MHz oscillator has a calibration value stored in
81
+ # the last memory location. On device reset it is loaded into the W
82
+ # register. init_clock moves the calibration value into the OSCCAL
83
+ # register and configures the OPTION register as:
84
+ # ~GPWU: disabled
85
+ # ~GPPU: enabled
86
+ # T0CS: transition on internal instruction cycle clock
87
+ # T0SE: increment on high-to-low transition
88
+ # PSA: prescalar assigned to WDT
89
+ # PS<2:0>: WDT rate 1:1
90
+ def init_clock
91
+ _emit "\tmovwf\tOSCCAL"
92
+ _emit "\tmovlw\tb'10011000'"
93
+ _emit "\toption"
94
+ _emit ''
95
+ end
96
+
97
+
98
+ def config_io(pins)
99
+ tris = 0
100
+
101
+ pins.each do |pin, state|
102
+ case pin
103
+ when :gp0
104
+ state == :out ? tris &= ~0x1 : tris |= 0x1
105
+
106
+ when :gp1
107
+ state == :out ? tris &= ~0x2 : tris |= 0x2
108
+
109
+ when :gp2
110
+ state == :out ? tris &= ~0x4 : tris |= 0x4
111
+
112
+ when :gp3
113
+ state == :out ? tris &= ~0x8 : tris |= 0x8
114
+ end
115
+ end
116
+
117
+ _emit "\tmovlw\tb'#{tris.to_s(2).rjust(8, '0')}'"
118
+ _emit "\ttris\tGPIO"
119
+ _emit ''
120
+ end
121
+
122
+
123
+ def set(sym, val)
124
+ _emit "\tmovlw\t0x#{val.to_s(16)}"
125
+ _emit "\tmovwf\t#{sym.to_s}"
126
+ _emit ''
127
+ end
128
+
129
+
130
+ def loop(label, &blk)
131
+ block label, &blk
132
+ _emit "\tgoto\t#{label.to_s.capitalize}"
133
+ _emit ''
134
+ end
135
+
136
+
137
+ def block(label, &blk)
138
+ _emit "#{label.to_s.capitalize}"
139
+ self.instance_eval &blk
140
+ _emit ''
141
+ end
142
+
143
+
144
+ def set_io(ports)
145
+ ports.each do |p, state|
146
+ case p
147
+ when :gp0
148
+ state == :hi ? _emit("\tbsf\tGPIO, 0") : _emit("\tbcf\tGPIO, 0")
149
+ when :gp1
150
+ state == :hi ? _emit("\tbsf\tGPIO, 1") : _emit("\tbcf\tGPIO, 1")
151
+ when :gp2
152
+ state == :hi ? _emit("\tbsf\tGPIO, 2") : _emit("\tbcf\tGPIO, 2")
153
+ else
154
+ abort "Invalid port #{p.to_s} specified"
155
+ end
156
+ end
157
+ _emit ''
158
+ end
159
+
160
+
161
+ def set_bit(var, bit)
162
+ _emit "\tbsf\t#{var.to_s}, 0x#{bit.to_s(16)}"
163
+ _emit ''
164
+ end
165
+
166
+
167
+ def clear_bit(var, bit)
168
+ _emit "\tbcf\t#{var.to_s}, 0x#{bit.to_s(16)}"
169
+ _emit ''
170
+ end
171
+
172
+
173
+ def test(var, bit, target)
174
+ _emit "\tbtfss\t#{var.to_s}, 0x#{bit.to_s(16)}"
175
+ _emit "\tgoto\t#{target.to_s.capitalize}"
176
+ _emit ''
177
+ end
178
+
179
+
180
+ def decrement_by(val, sym)
181
+ _emit "\tmovlw\t0x#{val.to_s(16)}"
182
+ _emit "\tsubwf\t#{sym.to_s}, f"
183
+ _emit ''
184
+ end
185
+
186
+
187
+ def increment_by(val, sym)
188
+ _emit "\tmovlw\t0x#{val.to_s(16)}"
189
+ _emit "\taddwf\t#{sym.to_s}, f"
190
+ _emit ''
191
+ end
192
+
193
+
194
+ def copy(from, to)
195
+ _emit "\tmovf\t#{from.to_s}, w"
196
+ _emit "\tmovwf\t#{to.to_s}"
197
+ _emit ''
198
+ end
199
+
200
+
201
+ def test_carry(label)
202
+ _emit "\tbtfss\tSTATUS, 0"
203
+ _emit "\tgoto\t#{label.to_s.capitalize}"
204
+ _emit ''
205
+ end
206
+
207
+
208
+ def add(val, to)
209
+ _emit "\tmovlw\t0x#{val.to_s(16)}"
210
+ _emit "\taddwf\t#{to.to_s}, w"
211
+ _emit ''
212
+ end
213
+
214
+
215
+ def subtract(val, from)
216
+ _emit "\tmovlw\t0x#{val.to_s(16)}"
217
+ _emit "\tsubwf\t#{from.to_s}, w"
218
+ _emit ''
219
+ end
220
+
221
+
222
+ def subtract_and_set(sym, from)
223
+ _emit "\tmovf\t#{sym.to_s}, w"
224
+ _emit "\tsubwf\t#{from.to_s}, f"
225
+ end
226
+
227
+
228
+ def delay(secs)
229
+ # Number of 4 microsecond delays to do
230
+ n = secs / 4.0e-6
231
+ n.ceil.to_i.times do
232
+ _emit "\tcall\tFour_microsecond_delay"
233
+ end
234
+ _emit ''
235
+ end
236
+
237
+
238
+ def decrement_and_test(var, *ops)
239
+ _emit "\tdecfsz\t#{var.to_s}, f"
240
+ self.send ops[0], ops[1]
241
+ _emit ''
242
+ end
243
+
244
+
245
+ def jump(label)
246
+ _emit "\tgoto\t#{label.to_s.capitalize}"
247
+ _emit ''
248
+ end
249
+
250
+
251
+ def increment(var)
252
+ _emit "\tincf\t#{var.to_s}, f"
253
+ _emit ''
254
+ end
255
+
256
+
257
+ def decrement(var)
258
+ _emit "\tdecf\t#{var.to_s}, f"
259
+ _emit ''
260
+ end
261
+
262
+
263
+ def end_code
264
+ _emit "\tend"
265
+ _emit ''
266
+ end
267
+
268
+
269
+ def method_missing(sym)
270
+ _emit "\tcall\t#{sym.to_s.capitalize}"
271
+ end
272
+
273
+
274
+ private
275
+
276
+
277
+ def _emit(s)
278
+ @code << s
279
+ end
280
+ end
281
+
282
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbpic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Patrick J. Franz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: trollop
16
+ requirement: &27193284 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *27193284
25
+ - !ruby/object:Gem::Dependency
26
+ name: wrong
27
+ requirement: &27193008 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *27193008
36
+ description: ! 'rbpic
37
+
38
+ =====
39
+
40
+
41
+ A high-level assembly dialect (RSM) that compiles into PIC assembly that
42
+
43
+ targets the PIC10 series of microprocessors by Microchip.
44
+
45
+ '
46
+ email:
47
+ - patrick@z-dyne.com
48
+ executables:
49
+ - rbpic-compile
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/rbpic/gen.rb
54
+ - bin/rbpic-compile
55
+ homepage: http://github.com/zdyne/rbpic
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 1.9.2
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.17
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: High-level assembly language for the PIC10.
79
+ test_files: []