-
Notifications
You must be signed in to change notification settings - Fork 4
/
changer.asm
840 lines (718 loc) · 15.6 KB
/
changer.asm
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
processor 16f716
include "p16f716.inc"
include "coff.inc"
__CONFIG _HS_OSC & _WDT_ON & _CP_OFF & _BOREN_OFF
radix dec
CONSTANT IRQ_W=0x7f
CONSTANT IRQ_STATUS=0x7e
CONSTANT COMMAND=0x7d
CONSTANT SRQ_COUNT=0x7c
CONSTANT XFER_TMP_BUF=0x30
CONSTANT RESP_BUF=0x40
CONSTANT RESP_BUF_LEN=0x4F
; 0x30 - 0x38 send/receive temp space
; 0x40 - 0x4E command response buffer
MAIN CODE
start
.sim ".frequency=20e6"
.sim "module library libgpsim_modules"
.sim "module load usart U1"
.sim "node n0"
.sim "node n1"
.sim "attach n0 portb2 U1.RXPIN"
.sim "attach n1 portb1 U1.TXPIN"
.sim "U1.txbaud = 19200"
.sim "U1.rxbaud = 19200"
org 0
goto main
nop
nop
nop
goto irq
wait_clk_low macro
btfsc PORTB, 4
goto $-1
endm
wait_clk_low_safe macro
clrwdt
btfsc PORTB, 4
goto $-2
endm
wait_clk_high macro
btfss PORTB, 4
goto $-1
endm
wait_data_low macro
btfsc PORTA, 4
goto $-1
endm
wait_data_high macro
btfss PORTA, 4
goto $-1
endm
wait_data_high_safe macro
clrwdt
btfss PORTA, 4
goto $-2
endm
wait_clk_high_safe macro
clrwdt
btfss PORTB, 4
goto $-2
endm
main:
clrf PORTA
clrf PORTB
clrf SRQ_COUNT
bsf PORTA, 4
bsf STATUS, RP0
bcf OPTION_REG^0x80, T0CS
bcf TRISB^0x80, 0
bsf TRISB^0x80, 1
bsf TRISB^0x80, 2
bsf TRISB^0x80, 4
bsf TRISB^0x80, 5
movlw 0x10
movwf TRISA^0x80
bcf STATUS, RP0
bsf INTCON, GIE
bsf INTCON, PEIE
wait_clk_high_safe
wait_data_high_safe
decode_burst:
clrf 0x20
clrf 0x21
movlw XFER_TMP_BUF
movwf FSR
clrwdt
; Bit 0 (MSB)
wait_clk_low_safe
bcf INTCON, GIE
bsf PORTB, 0
wait_clk_high
movfw PORTA
clrwdt
movwf INDF
incf FSR, F
; Bit 1
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; Bit 2
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; Bit 3
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; Bit 4
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; Bit 5
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; Bit 6
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; Bit 7 (LSB)
wait_clk_low
clrwdt
wait_clk_high
movfw PORTA
movwf INDF
incf FSR, F
; wait for HU to release data line
wait_data_high
clrwdt
; end timing-critical
bsf INTCON, GIE
; compress 8 bytes to one
movlw XFER_TMP_BUF
movwf FSR
clrf 0x20
bcf STATUS, C
decode_loop:
rlf 0x20, F
btfsc INDF, 4
bsf 0x20, 0
incf FSR, F
clrwdt
btfss FSR, 3
goto decode_loop
bcf PORTB, 0
; sets up CMD_BUF_LEN and buffer contents
movfw 0x20
call command_logic
movlw RESP_BUF
movwf FSR
send_next_byte:
movfw INDF
call send_byte
incf FSR, F
decf RESP_BUF_LEN, F
clrwdt
skpz
goto send_next_byte
call ack_byte
goto decode_burst
; end main
; must preserve FSR
send_byte:
movwf 0x20
movfw FSR
movwf 0x21
; blow out one byte to 8
movlw XFER_TMP_BUF
movwf FSR
encode_loop:
movlw 0
rlf 0x20, F
btfsc STATUS, C
iorlw 0x10
movwf INDF
incf FSR, F
clrwdt
btfss FSR, 3
goto encode_loop
call ack_byte
; switch data line to output
bsf PORTA, 4
bsf STATUS, RP0
bcf TRISA^0x80, 4
bcf STATUS, RP0
movlw XFER_TMP_BUF
movwf FSR
movfw INDF
; Bit 0 (MSB)
clrwdt
wait_clk_low
movwf PORTA
bcf INTCON, GIE
wait_clk_high
clrwdt
incf FSR, F
movfw INDF
; Bit 1
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; Bit 2
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; Bit 3
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; Bit 4
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; Bit 5
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; Bit 6
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; Bit 7 (LSB)
wait_clk_low
movwf PORTA
clrwdt
wait_clk_high
incf FSR, F
movfw INDF
; release data line
wait_clk_low
clrwdt
bsf PORTA, 4
bsf STATUS, RP0
bsf TRISA^0x80, 4
bcf STATUS, RP0
; end time critical
bsf INTCON, GIE
; restore FSR
movfw 0x21
movwf FSR
return
; end send_byte
delay macro
clrf TMR0
bcf INTCON, T0IF
clrwdt
btfss INTCON, T0IF
goto $-2
endm
ack_byte:
; switch data line to output
bsf PORTA, 4
bsf STATUS, RP0
bcf TRISA^0x80, 4
bcf STATUS, RP0
delay
delay
delay
delay
delay
delay
delay
delay
delay
delay
wait_clk_high
clrwdt
wait_clk_low
bcf PORTA, 4
clrwdt
wait_clk_high
wait_clk_low
clrwdt
wait_clk_high
wait_clk_low
clrwdt
wait_clk_high
wait_clk_low
clrwdt
wait_clk_high
wait_clk_low
clrwdt
wait_clk_high
wait_clk_low
clrwdt
bsf PORTA, 4
bsf STATUS, RP0
bsf TRISA^0x80, 4
bcf STATUS, RP0
wait_clk_high
clrwdt
return
command_logic:
clrf RESP_BUF_LEN
movwf COMMAND
movwf RESP_BUF
movlw RESP_BUF
movwf FSR
incf FSR, F
incf RESP_BUF_LEN, F
clrwdt
movlw 0x09
subwf COMMAND, W
skpnz
goto cmd_09
clrwdt
movlw 0x4c
subwf COMMAND, W
skpnz
goto cmd_4c
clrwdt
movlw 0x45
subwf COMMAND, W
skpnz
goto cmd_45
clrwdt
movlw 0x4b
subwf COMMAND, W
skpnz
goto cmd_4b
clrwdt
movlw 0x41
subwf COMMAND, W
skpnz
goto cmd_41
clrwdt
movlw 0x50
subwf COMMAND, W
skpnz
goto cmd_50
clrwdt
movlw 0x51
subwf COMMAND, W
skpnz
goto cmd_51
clrwdt
movlw 0xe2
subwf COMMAND, W
skpnz
goto cmd_e2
clrwdt
movlw 0x61
subwf COMMAND, W
skpnz
goto empty_cmd
clrwdt
movlw 0x70
subwf COMMAND, W
skpnz
goto empty_cmd
clrwdt
movlw 0x81
subwf COMMAND, W
skpnz
goto empty_cmd
clrwdt
movlw 0x5c
subwf COMMAND, W
skpnz
goto cmd_5c
clrwdt
movlw 0xe1
subwf COMMAND, W
skpnz
goto cmd_e1
clrwdt
movlw 0x00
subwf COMMAND, W
skpnz
goto cmd_00
clrwdt
movlw 0xf7
subwf COMMAND, W
skpnz
goto cmd_f7
clrwdt
movlw 0x11
subwf COMMAND, W
skpnz
goto cmd_11
clrwdt
movlw 0x62
subwf COMMAND, W
skpnz
goto empty_cmd
clrwdt
movlw 0x70
subwf COMMAND, W
skpnz
goto empty_cmd
clrwdt
movlw 0x81
subwf COMMAND, W
skpnz
goto empty_cmd
clrwdt
movlw 0x01
subwf COMMAND, W
skpnz
goto cmd_01
return
cmd_09:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x1a
call enqueue_byte
return
; end command_logic
cmd_4c:
movlw 0x02
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x00
call enqueue_byte
return
cmd_45:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x1a
call enqueue_byte
return
cmd_4b:
movlw 0x02
call enqueue_byte
movlw 0x12
call enqueue_byte
movlw 0x01
call enqueue_byte
return
cmd_41:
movlw 0x04
call enqueue_byte
movlw 0x1a
call enqueue_byte
movlw 0x55
call enqueue_byte
movlw 0x50
call enqueue_byte
movlw 0x00
call enqueue_byte
return
cmd_50:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x1a
call enqueue_byte
return
cmd_51:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x1a
call enqueue_byte
return
cmd_e2:
movlw 0x03
call enqueue_byte
movlw 0x42
call enqueue_byte
movlw 0xff
call enqueue_byte
movlw 0xff
call enqueue_byte
return
empty_cmd:
movlw 0x00
call enqueue_byte
return
cmd_5c:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x06
call enqueue_byte
return
cmd_e1:
movlw 0x03
call enqueue_byte
movlw 0x41
call enqueue_byte
movlw 0xff
call enqueue_byte
movlw 0xff
call enqueue_byte
call enable_timer
return
; cmd 0x00 is sent during interrupt servicing. rather than being
; echo'ed, we reply with 0xf7
cmd_00:
movlw 0xf7
movwf RESP_BUF
return
; cmd 0xf7 causes the interrupt line to be released
cmd_f7:
bsf STATUS, RP0
bsf TRISB, 5
bcf STATUS, RP0
return
cmd_11:
incf SRQ_COUNT, F
movlw 8
subwf SRQ_COUNT, W
skpz
call enable_timer
clrwdt
movlw 1
subwf SRQ_COUNT, W
skpnz
goto cmd_11_1
clrwdt
movlw 2
subwf SRQ_COUNT, W
skpnz
goto cmd_11_2
clrwdt
movlw 3
subwf SRQ_COUNT, W
skpnz
goto cmd_11_3
clrwdt
movlw 4
subwf SRQ_COUNT, W
skpnz
goto cmd_11_2
clrwdt
movlw 5
subwf SRQ_COUNT, W
skpnz
goto cmd_11_3
clrwdt
movlw 6
subwf SRQ_COUNT, W
skpnz
goto cmd_11_4
clrwdt
movlw 7
subwf SRQ_COUNT, W
skpnz
goto cmd_11_5
clrwdt
movlw 8
subwf SRQ_COUNT, W
skpnz
goto cmd_11_6
return
cmd_11_1:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x04
call enqueue_byte
return
cmd_11_2:
movlw 0x03
call enqueue_byte
movlw 0x41
call enqueue_byte
movlw 0x12
call enqueue_byte
movlw 0x01
call enqueue_byte
return
cmd_11_3:
movlw 0x03
call enqueue_byte
movlw 0x31
call enqueue_byte
movlw 0x43
call enqueue_byte
movlw 0x31
call enqueue_byte
return
cmd_11_4:
movlw 0x02
call enqueue_byte
movlw 0x31
call enqueue_byte
movlw 0x01
call enqueue_byte
return
cmd_11_5:
movlw 0x03
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x01
call enqueue_byte
movlw 0x01
call enqueue_byte
return
cmd_11_6:
movlw 0x03
call enqueue_byte
movlw 0x10
call enqueue_byte
movlw 0x00
call enqueue_byte
movlw 0x00
call enqueue_byte
return
cmd_01:
movlw 0x00
call enqueue_byte
; reset SRQ count for next run
clrf SRQ_COUNT
return
enqueue_byte:
movwf INDF
incf FSR, F
incf RESP_BUF_LEN, F
xorlw 0xff
movwf INDF
incf FSR, F
incf RESP_BUF_LEN, F
return
; end enqueue_byte
enable_timer:
bcf T1CON, TMR1ON
clrf TMR1L
clrf TMR1H
bcf PIR1, TMR1IF
bsf STATUS, RP0
bsf PIE1^0x80, TMR1IE
bcf STATUS, RP0
bsf T1CON, TMR1ON
return
; end enable_timer
irq:
movwf IRQ_W
swapf STATUS, W
movwf IRQ_STATUS
btfsc PIR1, TMR1IF
call timer_expired
swapf IRQ_STATUS, W
movwf STATUS
swapf IRQ_W, F
swapf IRQ_W, W
retfie
; end of irq
timer_expired:
bsf STATUS, RP0
movfw PIE1^0x80
bcf STATUS, RP0
movwf 0x20
btfss 0x20, TMR1IE
return
; clear and disable interrupt
bsf STATUS, RP0
bcf PIE1^0x80, TMR1IE
bcf STATUS, RP0
bcf PIR1, TMR1IF
; assert SRQ
bcf PORTB, 5
bsf STATUS, RP0
bcf TRISB^0x80, 5
bcf STATUS, RP0
return
; end of timer_expired
end