forked from totalspectrum/spin2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2_code.spin
More file actions
729 lines (652 loc) · 11.9 KB
/
Copy pathp2_code.spin
File metadata and controls
729 lines (652 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
''
'' P2 specific system calls
''
con
_rxpin = 63
_txpin = 62
pri _getcnt : r = +long
asm
getct r
endasm
return r
pri _getcnth : r = +long
asm
getct r wc
endasm
return r
pri _getcnthl : rl = +long, rh = +long
asm
getct rh wc
getct rl
endasm
return rl, rh
pri waitcnt(x = long)
asm
addct1 x, #0
waitct1
endasm
pri _waitcnt(x = long)
asm
addct1 x, #0
waitct1
endasm
pri _cogid : rval = long
asm
cogid rval
endasm
return rval
pri _cogstop(id = long)
asm
cogstop id
endasm
return 0
pri _lockclr(id = long) : rval | mask
mask := -1
asm
lockrel id wc
muxc rval,mask
endasm
return rval
pri _lockset(id = long) : rval | mask
mask := -1
asm
locktry id wc
muxnc rval,mask ' NOTE: C bit is opposite in P2
endasm
return rval
' locktry is like lockset but with opposite return value
pri _locktry(id = long) : rval | mask
mask := -1
asm
locktry id wc
muxc rval,mask
endasm
return rval
pri _locknew : rval = long
asm
locknew rval
endasm
return rval
pri _lockret(id = long)
asm
lockret id
endasm
return 0
pri _reboot
_clkset(0, 0)
asm
hubset ##%0001 << 28
endasm
pri _hubset(x)
asm
hubset x
endasm
pri _clkset(mode, freq) | oldmode, xsel
xsel := mode & 3
if xsel == 0 and mode > 1
xsel := 3
oldmode := __clkmode_var & !3 ' remove low bits, if any
__clkfreq_var := freq
__clkmode_var := mode
mode := mode & !3
asm
wrpin #%00_00001_0,#_rxpin
dirh #_rxpin
wxpin freq,#_rxpin
fltl #_rxpin
hubset oldmode ' go to RCFAST using known prior mode
hubset mode ' setup for new mode, still RCFAST
waitx ##20_000_000/100 ' wait ~10ms
or mode, xsel
hubset mode ' activate new mode
endasm
__clkfreq_ms := freq / 1000
__clkfreq_us := freq / 1000000
_bitcycles := 0
pri _sqrt(a) | r
if (a =< 0)
return 0
asm
qsqrt a, #0
getqx r
endasm
return r
pri _sqrt64(lo, hi) | r
asm
qsqrt lo, hi
getqx r
endasm
return r
pri _coginit(id, code, param)
asm
setq param
coginit id, code wc
if_c neg id, #1
endasm
return id
pri _cogchk(id = long) : r
r := -1
asm
cogid id wc
if_nc mov r, #0
endasm
return r
''
'' serial send/receive code
'' this code uses smarpins, so doesn't require a COG. The down side is that
'' the smartpin has only a single character buffer.
''
dat
orgh
_bitcycles long 0
con
_txmode = %0000_0000_000_0000000000000_01_11110_0 'async tx mode, output enabled for smart output
_rxmode = %0000_0000_000_0000000000000_00_11111_0 'async rx mode, input enabled for smart input
' utility function: check for tx finished
pri _txwait | w, c
repeat
w,c := _rdpinx(_txpin)
while c <> 0
pri _setbaud(baudrate) | bitperiod, bit_mode
bitperiod := (__clkfreq_var / baudrate)
_txwait()
_dirl(_txpin)
_dirl(_rxpin)
_bitcycles := bitperiod
bit_mode := 7 + (bitperiod << 16)
_wrpin(_txpin, _txmode)
_wxpin(_txpin, bit_mode)
_wrpin(_rxpin, _rxmode)
_wxpin(_rxpin, bit_mode)
_dirh(_txpin)
_dirh(_rxpin)
pri _txraw(c) | z
_txwait()
if _bitcycles == 0
_setbaud(__default_baud__) ' set up in common.c
_drvl(_txpin) ' make sure smartpin is enabled (debug() disables it)
_wypin(_txpin, c)
return 1
' timeout is approximately in milliseconds (actually in 1024ths of a second)
pri _rxraw(timeout = 0) : rxbyte = long | z, endtime, temp2, rxpin
if _bitcycles == 0
_setbaud(__default_baud__)
if timeout
endtime := _getcnt() + timeout * (__clkfreq_var >> 10)
else
endtime := 0 ' just to make compiler happy
rxbyte := -1
rxpin := _rxpin
z := 0
repeat
z := _pinr(rxpin)
if z
rxbyte := _rdpin(rxpin)>>24
quit
if timeout
if endtime - _getcnt() < 0
quit
' returns -1 immediately if no pending character
pri _rxpoll() : rxbyte = long | z, rxpin
if _bitcycles == 0
_setbaud(__default_baud__)
rxbyte := -1
rxpin := _rxpin
z := _pinr(rxpin)
if z
rxbyte := _rdpin(rxpin)>>24
pri _dirl(pin = long)
asm
dirl pin
endasm
pri _dirh(pin = long)
asm
dirh pin
endasm
pri _dirnot(pin = long)
asm
dirnot pin
endasm
pri _dirrnd(pin = long)
asm
dirrnd pin
endasm
pri _dirw(pin = long, c = long)
asm
test c,#1 wc
dirc pin
endasm
pri _drvl(pin = long)
asm
drvl pin
endasm
pri _drvh(pin = long)
asm
drvh pin
endasm
pri _drvnot(pin = long)
asm
drvnot pin
endasm
pri _drvrnd(pin = long)
asm
drvrnd pin
endasm
pri _drvw(pin = long, c = long)
asm
test c,#1 wc
drvc pin
endasm
pri _outl(pin = long)
asm
outl pin
endasm
pri _outh(pin = long)
asm
outh pin
endasm
pri _outnot(pin = long)
asm
outnot pin
endasm
pri _outrnd(pin = long)
asm
outnot pin
endasm
pri _out(pin = long, c = long)
asm
test c,#1 wc
outc pin
endasm
pri _fltl(pin = long)
asm
fltl pin
endasm
pri _flth(pin = long)
asm
flth pin
endasm
pri _fltnot(pin = long)
asm
fltnot pin
endasm
pri _fltrnd(pin = long)
asm
fltnot pin
endasm
pri _fltw(pin = long, val = long)
asm
test val,#1 wc
fltc pin
endasm
' special case of _pinread where "pin" is a single pin
pri _pinr(pin = long) : val
asm
testp pin wc
wrc val
endasm
pri _waitx(tim = long)
asm
waitx tim
endasm
pri _cogatn(mask = +long)
asm
cogatn mask
endasm
pri _pollatn() : flag
flag := 0
asm
pollatn wc
subx flag, #0 ' turns c bit into -1
endasm
pri _waitatn()
asm
waitatn
endasm
pri _rdpin(pin = long) : r
asm
rdpin r, pin
endasm
pri _rdpinx(pin = long) : r, c
c := 0
asm
rdpin r, pin wc
if_c neg c, #1
endasm
pri _rqpin(pin = long) : r
asm
rqpin r, pin
endasm
pri _akpin(pin = long)
asm
akpin pin
endasm
pri _rnd : r = +long
asm
getrnd r
endasm
pri _getrnd : r = +long
asm
getrnd r
endasm
pri _getcrc(ptr, poly, cnt) : x | y, w
if cnt =< 0
return 0
org
encod x, poly
bmask x
.loop
rdbyte w, ptr
add ptr, #1
shl w, #24
setq w
crcnib x, poly
crcnib x, poly
djnz cnt, #.loop
end
return x
pri _rotxy(x, y, angle)
asm
setq y
qrotate x, angle
getqx x
getqy y
endasm
return x,y
pri _polxy(d, angle) : x, y
asm
qrotate d, angle
getqx x
getqy y
endasm
' divide (ahi, alo) by b to produce 32 bit quotient q and remainder r
' unsigned version
pri _div64(ahi, alo, b) : q,r
asm
setq ahi
qdiv alo, b
getqx q
getqy r
endasm
' signed version
pri _sdiv64(ahi, alo, b) : q,r | shi, slo, sign
sign := 0
if ahi < 0
shi := 0
slo := 0
asm
sub slo, alo wc
subx shi, ahi
endasm
sign := 1
else
shi, slo := ahi, alo
if b < 0
sign ^= 1
b := -b
q,r := _div64(shi, slo, b)
if sign
q := -q
' various
pri _xypol(x, y) : d, angle
asm
qvector x, y
getqx d
getqy angle
endasm
pri _qsin(len, angle, twopi) : y | x
if twopi
if angle > twopi __orelse__ angle < -twopi
angle := angle // twopi
angle, x := _sdiv64(angle, 0, twopi)
x, y := _rotxy(len, 0, angle)
pri _qcos(len, angle, twopi) : x | y
if twopi
if angle > twopi __orelse__ angle < -twopi
angle := angle // twopi
angle, x := _sdiv64(angle, 0, twopi)
x, y := _rotxy(len, 0, angle)
' synthetic smartpin instruction for setting up smartpin parameters
pri _pinsetup(pin, mode, xval, yval)
_dirl(pin)
_wrpin(pin, mode)
_wxpin(pin, xval)
_wypin(pin, yval)
pri _pinstart(pin, mode, xval, yval)
_pinsetup(pin, mode, xval, yval)
_dirh(pin)
pri _pinclear(pin)
asm
fltl pin
wrpin #0, pin
endasm
' NOTE: the order of parameters is reversed from the instruction
pri _wrpin(pin, val)
asm
wrpin val, pin
endasm
pri _wxpin(pin, val)
asm
wxpin val, pin
endasm
pri _wypin(pin, val)
asm
wypin val, pin
endasm
pri _muldiv64(mult1, mult2, divisor) : r
asm
qmul mult1, mult2
getqy mult1
getqx mult2
setq mult1
qdiv mult2, divisor
getqx r
endasm
pri _call(hubaddr)
asm
call hubaddr
endasm
pri _ones(v = +long) : r=+long
asm
ones r, v
endasm
pri __builtin_parity(v = +long) : r = +long
asm
or v, #0 wc
wrc r
endasm
pri __builtin_bswap16(v = +long) : r = +long
r := v
asm
movbyts r, #%%3201
zerox r, #15
endasm
pri __builtin_bswap32(v = +long) : r = +long
r := v
asm
movbyts r, #%%0123
endasm
pri __builtin_movbyts(v = +long, m = +long) : r = +long
r := v
asm
movbyts r, m
endasm
pri __builtin_mergew(v = +long) : r = +long
r := v
asm
mergew r
endasm
pri __builtin_splitw(v = +long) : r = +long
r := v
asm
splitw r
endasm
pri __builtin_mergeb(v = +long) : r = +long
r := v
asm
mergeb r
endasm
pri __builtin_splitb(v = +long) : r = +long
r := v
asm
splitb r
endasm
pri __builtin_seussf(v = +long) : r = +long
r := v
asm
seussf r
endasm
pri __builtin_seussr(v = +long) : r = +long
r := v
asm
seussr r
endasm
pri _qexp(v) : r
asm
qexp v
getqx r
endasm
pri _qlog(v) : r
asm
qlog v
getqx r
endasm
pri _getsec() : freq = +long | hi, lo
freq := __clkfreq_var
asm
getct hi wc
getct lo
setq hi
qdiv lo, freq
getqx lo
endasm
return lo
pri _getms() : freq = +long | lo, hi
lo,hi := _getcnthl()
freq := __clkfreq_ms
if freq == 0
__clkfreq_ms := freq := __clkfreq_var +/ 1000
asm
setq hi
qdiv lo, freq
getqx freq
endasm
return freq
pri _getus() : freq = +long | lo, hi
lo,hi := _getcnthl()
freq := __clkfreq_us
if freq == 0
__clkfreq_us := freq := __clkfreq_var +/ 1000000
asm
qdiv hi, freq
getqy hi
setq hi
qdiv lo, freq
getqx freq
endasm
return freq
''
'' memset/memmove are here (in processor specific code)
'' because on P2 we can optimize them (long operations do
'' not have to be aligned)
''
pri {++specialfunc(memset)} __builtin_memset(ptr, val, count) : r | lval
r := ptr
asm
call #\builtin_bytefill_
endasm
pri __builtin_longset(ptr, val, count) : r | lval
r := ptr
asm
call #\builtin_longfill_
endasm
pri __builtin_memmove(dst, src, count) : origdst
origdst := dst
if dst < src __orelse__ dst => src+count
repeat count >> 2
long[dst] := long[src]
dst += 4
src += 4
if count & 2
word[dst] := word[src]
dst += 2
src += 2
if count & 1
byte[dst] := byte[src]
else
dst += count
src += count
repeat count
dst -= 1
src -= 1
byte[dst] := byte[src]
''
'' bytefill etc.
''
pri bytefill(ptr, val, count)
asm
call #\builtin_bytefill_
endasm
pri wordfill(ptr, val, count)
asm
call #\builtin_wordfill_
endasm
pri longfill(ptr, val, count)
asm
call #\builtin_longfill_
endasm
'' create a method pointer
pri _make_methodptr(o, func) : ptr
ptr := o | (func << 20)
return ptr
'
' create a class interface from a definite object o and
' a skeleton list of functions skel containing n entries
'
pub _make_interfaceptrs(o, skel, n) : r | siz, i, p, f
siz := n * 4
p := _gc_alloc_managed(siz)
if p == 0
return p
r := p
repeat while n-- > 0
f := long[skel]
if f & 1
long[p] := r | (f & -2)
else
long[p] := o | f
p += 4
skel += 4
''
'' 64 bit operations
''
pri _int64_add(alo, ahi, blo, bhi) : rlo, rhi
rlo := alo
rhi := ahi
asm
add rlo, blo wc
addx rhi, bhi
endasm
pri _int64_sub(alo, ahi, blo, bhi) : rlo, rhi
rlo := alo
rhi := ahi
asm
sub rlo, blo wc
subx rhi, bhi
endasm
' compare unsigned alo, ahi, return -1, 0, or +1
pri _int64_cmpu(alo, ahi, blo, bhi) : r
asm
cmp alo, blo wc,wz
cmpx ahi, bhi wc,wz
if_z mov r, #0
if_nz negc r, #1
endasm
' compare signed alo, ahi, return -1, 0, or +1
pri _int64_cmps(alo, ahi, blo, bhi) : r
asm
cmp alo, blo wc,wz
cmpsx ahi, bhi wc,wz
if_z mov r, #0
if_nz negc r, #1
endasm