-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROM.asm
1531 lines (1270 loc) · 44.4 KB
/
ROM.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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Implantable Stimulator-Transponder (IST) Program
; ------------------------------------------------
; This code runs in the OSR8V3 microprocessor of the A3041A.
; Configuration Constants.
const version 21 ; The firmwarwe version.
const identifier_hi 0x27 ; 0-255, no restrictions
const identifier_lo 0x9A ; 0-255, low nibble cannot be 0x0 or 0xF
const frequency_low 5 ; Radio frequency calibration.
; CPU Address Map Boundary Constants
const mmu_vmem 0x0000 ; Base of Main Program Variable Memory
const mmu_sba 0x0100 ; Stack Base Address
const mmu_umem 0x0200 ; Base of User Program Variable Memory
const mmu_ctrl 0x0400 ; Base of Control Space
const mmu_prog 0x0800 ; Base of user program memory window
; Address Map Locations
const mmu_sdb 0x0400 ; Sensor Data Byte (Write)
const mmu_scr 0x0401 ; Sensor Control Register (Write)
const mmu_irqb 0x0402 ; Interrupt Request Bits (Read)
const mmu_imsk 0x0403 ; Interrupt Mask Bits (Read/Write)
const mmu_irst 0x0404 ; Interrupt Reset Bits (Write)
const mmu_dva 0x0405 ; Device Active (Write)
const mmu_stc 0x0406 ; Stimulus Current (Write)
const mmu_rst 0x0407 ; System Reset (Write)
const mmu_xhb 0x0408 ; Transmit HI Byte (Write)
const mmu_xlb 0x0409 ; Transmit LO Byte (Write)
const mmu_xch 0x040A ; Transmit Channel Number (Write)
const mmu_xcr 0x040B ; Transmit Control Register (Write)
const mmu_rfc 0x040C ; Radio Frequency Calibration (Write)
const mmu_etc 0x040D ; Enable Transmit Clock (Write)
const mmu_tcf 0x040E ; Transmit Clock Frequency (Write)
const mmu_tcd 0x040F ; Transmit Clock Divider (Write)
const mmu_bcc 0x0410 ; Boost CPU Clock (Write)
const mmu_dfr 0x0411 ; Diagnostic Flag Register (Read/Write)
const mmu_sr 0x0412 ; Status Register (Read)
const mmu_cmp 0x0413 ; Command Memory Portal (Read)
const mmu_cpr 0x0415 ; Command Processor Reset (Write)
const mmu_i1ph 0x0416 ; Interrupt Timer One Period HI (Write)
const mmu_i1pl 0x0417 ; Interrupt Timer One Period LO (Write)
const mmu_i2ph 0x0418 ; Interrupt Timer Two Period HI (Write)
const mmu_i2pl 0x0419 ; Interrupt Timer Two Period LO (Write)
const mmu_i3p 0x041A ; Interrupt Timer Three Period (Write)
const mmu_i4p 0x041B ; Interrupt Timer Four Period (Write)
; Status Bit Masks, for use with status register
const sr_cmdrdy 0x01 ; Command Ready Flag
const sr_entck 0x02 ; Transmit Clock Enabled
const sr_saa 0x04 ; Sensor Access Active Flag
const sr_txa 0x08 ; Transmit Active Flag
const sr_cpa 0x10 ; Command Processor Active
const sr_boost 0x20 ; Boost Flag
const sr_cme 0x40 ; Command Memory Empty
; Transmit Control Masks, for use with tansmit control register
const tx_txi 0x01 ; Assert transmit initiate
const tx_txwp 0x02 ; Assert transmit warm-up
; Auxiliary message types.
const at_id 1 ; Identification
const at_ack 2 ; Acknowledgements
const at_batt 3 ; Battery Measurement
const at_conf 4 ; Confirmation
const at_ver 5 ; Version
; Bit Masks
const bit0_mask 0x01 ; Bit Zero Mask
const bit1_mask 0x02 ; Bit One Mask
const bit2_mask 0x04 ; Bit Two Mask
const bit3_mask 0x08 ; Bit Three Mask
const bit7_mask 0x80 ; Bit Seven Mask
const bit0_clr 0xFE ; Bit Zero Clear
const bit1_clr 0xFD ; Bit One Clear
const bit2_clr 0xFB ; Bit Two Clear
const bit3_clr 0xF7 ; Bit Three Clear
; Timing Constants.
const min_tcf 72 ; Minimum TCK periods per half RCK period
const tx_delay 50 ; Wait time for sample transmission, TCK periods
const sa_delay 30 ; Wait time for sensor access, TCK periods
const wp_delay 255 ; Warm-up delay for auxiliary messages
const num_vars 64 ; Number of vars to clear at start
const initial_tcd 15 ; Max possible value of TCK divisor
const uprog_tick 163 ; User program interrupt period
const id_delay 33 ; To pad id delay to 50 TCK periods
const min_int_p 25 ; Minimum transmit period
const shdn_rst 250 ; Shutdown counter reset value.
; Stimulus Control Variables
const Scurrent 0x0000 ; Stimulus Current
const Spulse1 0x0001 ; Pulse Length, HI
const Spulse0 0x0002 ; Pulse Length, LO
const Sint1 0x0003 ; Interval Length, HI
const Sint0 0x0004 ; Interval Length, LO
const Slen1 0x0005 ; Stimulus Length, HI
const Slen0 0x0006 ; Stimulus Length, LO
const Srand 0x0007 ; Random pulse timing
const Srun 0x0008 ; Run stimulus
const Spulse 0x0009 ; Stimulus Pulse Run Flag
const Sack_key 0x000A ; Acknowledgement key
const Sdly1 0x000B ; Stimulus Delay Byte One
const Sdly0 0x000C ; Stimulus Delay Byte Zero
const Sdelay 0x000D ; Stimulus Delay Run Flag
const Smaxdly1 0x000E ; Max Delay, HI
const Smaxdly0 0x000F ; Max Delay, LO
; Command Decode Variables
const ccmdb 0x0016 ; Copy of Command Byte
; Shutdown counter.
const shdncnt1 0x0019 ; Counter Byte One
const shdncnt0 0x001A ; Counter Byte Zero
; Random Number Variabls
const rand_1 0x0020 ; Random Number Byte One
const rand_0 0x0021 ; Random Number Byte Zero
; User Program Control Variables
const UPrun 0x0022 ; Running
const UPinit 0x0023 ; Initialize
; Transmission Control Variables
const xmit_p 0x0028 ; Transmit Period
const xmit_ch 0x0029 ; Telemetry Channel Number
; User Program Constants
const prog_usr 0x0800 ; User program location
const ret_code 0x0A ; Return from subrouting instruction
; Operation Codes
const op_stop 0 ; 0 operands
const op_start 1 ; 8 operands
const op_xon 2 ; 2 operand
const op_xoff 3 ; 0 operands
const op_batt 4 ; 0 operands
const op_id 5 ; 0 operands
const op_pgld 6 ; 1 operand, variable data
const op_pgon 7 ; 0 operands
const op_pgoff 8 ; 0 operands
const op_pgrst 9 ; 0 operands
const op_shdn 10 ; 0 operands
const op_ver 11 ; 0 operands
; Synchronization.
const synch_nostim 32 ;
const synch_stim 96 ;
; Random Number Generator.
const rand_taps 0xB4 ; Determines which taps to XOR.
; ------------------------------------------------------------
; The CPU reserves two locations 0x0000 for the start of program
; execution, and 0x0003 for interrupt execution. We put jumps at
; both locations. A jump takes exactly three bytes.
start:
jp main
jp interrupt
; ---------------------------------------------------------------
; Eight-bit multiplier. Load two eight-bit operands into B and C
; and the sixteen-bit result will be returned in B (HI) and C (LO).
; Takes 200 to 300 clock cycles depending upon the operand C, an
; average of 250 (50 us at 5 MHz or 7.6 ms at 32.768 kHz).
multiply:
; Save registers and flags on the stack.
push F
push A
push D
push H
push L
; We use D to count down from eight to zero.
ld A,8
push A
pop D
; Clear HL.
ld A,0
push A
pop H
push A
pop L
; Shift C left and check the bit that comes out the top end, now in our
; carry bit. If carry is not set, jump forward to shift HL.
mult_start:
push C
pop A
sla A
push A
pop C
jp nc,mult_check_done
; Carry bit set means we add B to HL.
push L
pop A
add A,B
push A
pop L
push H
pop A
adc A,0
push A
pop H
; Decrement D. If zero, we have added eight times and
; there is no need to shift HL again, we are done.
mult_check_done:
dec D
jp z,mult_done
; Shift HL to the left, filling in bit zero with a zero. We are
; going repeat our addition loop.
push L
pop A
sla A
push A
pop L
push H
pop A
rl A
push A
pop H
jp mult_start
; Multiplication is complete and the result is in HL. Move the
; result to BC so that this routine affects only BC.
mult_done:
push H
pop B
push L
pop C
; Recover registers and flags.
pop L
pop H
pop D
pop A
pop F
ret
; ------------------------------------------------------------
; The random number generator updates rand_0 and rand_1 with a
; sixteen-bit linear feedback shift register.
rand:
push F
push A
ld A,(rand_1) ; Rotate rand_1 to the right,
srl A ; filling top bit with zero,
ld (rand_1),A ; and placing bottom bit in carry.
ld A,(rand_0) ; Rotate rand_0 to the right,
rr A ; filling top bit with carry,
ld (rand_0),A ; and placing bottom bit in carry.
ld A,(rand_1) ; Load A with rand_1 again.
jp nc,rand_tz ; If carry is set, perform the XOR
xor A,rand_taps ; operation on tap bits and
ld (rand_1),A ; save to memory.
rand_tz:
pop A
pop F
ret
; ------------------------------------------------------------
; Generate a number that we can use to delay a pulse within a
; stimulus interval. The minimum delay is zero and the maximum
; is the interval length minus the pulse length. We write the
; delay bytes to Sdly0 and Sdly1.
;
rand_dly:
push F
push A
push B
push C
push D
push E
push H
push L
; Copy the stimulus interval length into the scratch pad
; and subtract the pulse length. The result is our maximum
; delay for randomized pulses. If this value is negative,
; set to zero.
ld A,(Spulse0) ; Load pulse length byte
push A ; zero into B
pop B ; and interval length
ld A,(Sint0) ; byte zero into A.
sub A,B ; Subtract to get
ld (Smaxdly0),A ; max delay byte zero.
ld A,(Spulse1) ; Load pulse length
push A ; byte one into
pop B ; B and
ld A,(Sint1) ; interval lengt byte one
sbc A,B ; into A and subtract with
ld (Smaxdly1),A ; carry to get max delay byte one.
jp nc,rand_delay_ds ; If not negative, move on.
ld A,0 ; Otherwise
ld (Sdly1),A ; set delay to
ld (Sdly0),A ; zero.
jp rand_delay_done
rand_delay_ds:
; Get a random number and place it in D. This is one of our
; product terms. The other is the maximum delay for randomized
; pulses, which is currently in the scratch pad.
call rand ; Update the random number.
ld A,(rand_0) ; Load the random number
push A ; and move to B
pop B ; for multiplication.
push A ; Also store in D for
pop D ; later.
; Multiply the maximum delay by the random number and store the
; top two bytes of the twenty-four bit product in memory.
ld A,(Smaxdly0) ; Load LO byte zero of max delay
push A ; and place in C for
pop C ; multiplication.
call multiply ; Let BC := B * C.
push B ; Store HI byte in E for later.
pop E ; Won't be using LO byte.
push D ; Bring back our random
pop B ; number.
ld A,(Smaxdly1) ; Put byte one of max delay
push A ; in C
pop C ; for multiplication
call multiply ; Let BC := B * C.
push B ; The HI byte is our random
pop A ; delay byte one, so store
ld (Sdly1),A ; now in timer byte one.
push C ; Move LO byte from
pop B ; C to B.
push E ; Move HI byte of first product
pop A ; from E to A.
add A,B ; Add bytes, never generates carry.
ld (Sdly0),A ; Put delay byte zero in timer.
rand_delay_done:
pop L
pop H
pop E
pop D
pop C
pop B
pop A
pop F
ret
; ------------------------------------------------------------
; Calibrate the transmit clock frequency. We take the CPU out
; of boost, turn off the transmit clock, and repeat a cycle of
; setting the transmit clock divisor and running the transmit
; clock to measure its frequency. Eventually we get a diviso
; that provides a transmit period in the range 195-215 ns. We
; leave the transmit clock off at the end.
calibrate_tck:
; Push flags and registers, disable interrupts.
push F
push A
push B
ld A,0x00 ; Clear bit zero of A
ld (mmu_bcc),A ; Disable CPU Clock Boost
ld (mmu_etc),A ; Disable Transmit Clock
ld A,initial_tcd ; The initial value of transmit clock divisor
push A ; Push divisor onto the stack
pop B ; Store divisor in B
cal_tck_1:
dec B ; Decrement the divisor.
push B ; Push divisor onto stack.
pop A ; Pop divisor into A.
ld (mmu_tcd),A ; Write divisor to transmit clock generator.
ld A,0x01 ; Set bit zero of A.
ld (mmu_etc),A ; Enable the transmit clock.
ld A,(mmu_tcf) ; Read the transmit clock frequency.
sub A,min_tcf ; Subtract the minimum frequency.
ld A,0x00 ; Clear bit zero of A.
ld (mmu_etc),A ; Disable Transmit Clock.
jp np,cal_tck_1 ; Try smaller divisor.
; Pop registers and return.
pop B
pop A
pop F
ret
; ------------------------------------------------------------
; The interrupt handler. Assumes that it interrupts a program
; running off the slow clock. Boosts as quickly as possible to
; fast clock, executes, then restores the clock. We handle the
; user program interrupt, then stimulus interrupt, and finally
; the transmit interrupt. By this means, when the interrupts
; are coincident, the user program can affect the stimulus and
; the stimulus can affect the transmission. The synchronizing
; signal, for example, will reflect the most recent state of
; the stimulus.
interrupt:
; Push A onto the stack, boost CPU, push F. We push A before
; F because we want to move into boost as quickly as possible.
; Each instruction at 33 kHz takes 150 times longer than at 5 MHz.
push A ; Save A on stack
ld A,0x01 ; Set bit zero to one.
ld (mmu_etc),A ; Enable the transmit clock, TCK.
ld (mmu_bcc),A ; Boost the CPU clock to TCK.
push F ; Save the flags onto the stack.
; Drive TP1 high.
ld A,(mmu_dfr) ; Load the diagnostic flag register.
or A,bit0_mask ; set bit zero and
ld (mmu_dfr),A ; write to diagnostic flag register.
; Push all the registers, even if we don't use them in the interrupt
; code. We want to protect the calling process from the user program.
push B
push C
push D
push E
push H
push L
push IX
push IY
; Handle the user program interrupt, in which we call the user program
; and allow it to execute and return. Because we just pushed all the
; registers, the user program can do what it likes with all the registers
; and flags, with the exception of the interrupt flag, which it must
; handle with care. Right now, the interrupt flag is set, and interrupts
; are disabled. Clearing the interrupt flag could cause the user program
; to be interrupted to execute itself in a recursion that overflows the
; stack.
int_uprog:
ld A,(mmu_irqb) ; Read the interrupt request bits
and A,bit2_mask ; and test bit one,
jp z,int_uprog_done ; skip if uprog interrupt.
ld A,bit2_mask ; Reset this interrupt
ld (mmu_irst),A ; with the bit two mask.
call prog_usr ; Call the user program.
int_uprog_done:
; Handle the stimulus interval interrupt. We decrement the stimulus
; interval counter. We start a delay or a pulse using interrupt timer
; two. We set and clear the stimulus delay, pulse, and run flags. The
; first interval of a stimulus should start immediately after reception
; of command, so the initial value of the interrupt period is one. We
; correct this value in the handler.
int_sii:
ld A,(mmu_irqb) ; Read the interrupt request bits
and A,bit0_mask ; and test bit zero.
jp z,int_sii_done ; If not set, skip this interrupt.
ld A,(Srun) ; Check the Srun flag.
add A,0 ; If it's been cleared,
jp z,int_sii_done ; we do nothing.
ld A,(Sint1) ; Set the stimulus interval delay to
ld (mmu_i1ph),A ; the value specified by the most
ld A,(Sint0) ; recent command, or adapt to value written
ld (mmu_i1pl),A ; by user program to interval length locations.
ld A,(Slen0) ; Load LO byte of stimulus length counter.
sub A,1 ; decrement
ld (Slen0),A ; save,
ld A,(Slen1) ; Load HI byte,
sbc A,0 ; apply carry bit
ld (Slen1),A ; and save.
jp nc,int_sii_do ; If >=0, start a delay or a pulse.
ld A,0 ; If <0,
ld (mmu_stc),A ; turn off the stimulus current.
ld (Srun),A ; Clear the Srun,
ld (Spulse),A ; Spulse,
ld (Sdelay),A ; and Sdelay flags.
ld (mmu_i1ph),A ; Disable the interrupt
ld (mmu_i1pl),A ; timer.
ld A,(mmu_imsk) ; Mask the the
and A,bit0_clr ; timer
ld (mmu_imsk),A ; interrupt.
ld A,op_stop ; Transmit a stimulus stop
ld (Sack_key),A ; acknowledgement
call annc_ack ; to mark stimulus end.
jp int_sii_rst
int_sii_do:
ld A,(Srand) ; Check the random flag
add A,0 ; and if zero, start pulse
jp z,int_sii_p ; otherwise start delay.
int_sii_r:
call rand_dly ; Generate random delay.
ld A,(Sdly0) ; If the delay
add A,0 ; is zero
jp nz,int_sii_rd ; we skip the
ld A,(Sdly1) ; delay and
add A,0 ; start our pulse
jp z,int_sii_p ; immediately.
int_sii_rd:
ld A,(Sdly0) ; Copy random
ld (mmu_i2pl),A ; delay to the
ld A,(Sdly1) ; Timer Two
ld (mmu_i2ph),A ; period.
ld A,1 ; Set the
ld (Sdelay),A ; delay flag.
ld A,0 ; Clear the
ld (Spulse),A ; pulse flag.
ld (mmu_stc),A ; Turn off stimulu
ld A,(mmu_imsk) ; Unmask
or A,bit1_mask ; Timer Two
ld (mmu_imsk),A ; interrupt.
ld A,bit1_mask ; Reset
ld (mmu_irst),A ; Timer Two.
jp int_sii_rst
int_sii_p:
ld A,1 ; Set the
ld (Spulse),A ; pulse flag.
ld A,0 ; Clear the delay
ld (Sdelay),A ; flag.
ld A,(Scurrent) ; Load stimulus current and
ld (mmu_stc),A ; turn on the stimulus.
ld A,(Spulse1) ; Set interrupt timer
ld (mmu_i2ph),A ; two period to the
ld A,(Spulse0) ; pulse
ld (mmu_i2pl),A ; length.
ld A,(mmu_imsk) ; Unmask
or A,bit1_mask ; Timer Two
ld (mmu_imsk),A ; interrupt.
ld A,bit1_mask ; Reset
ld (mmu_irst),A ; Timer Two
jp int_sii_rst
int_sii_rst:
ld A,bit0_mask ; Reset interrupt timer one, which clears
ld (mmu_irst),A ; its interrupt bit and reloads its timer.
int_sii_done:
; Handle the delay and pulse interrupt, which is generated by
; Timer Two. The Sdelay flag tells us if the interrupt marks
; the end of a delay or the end of a pulse. We either start
; a pulse or end a pulse.
int_sdp:
ld A,(mmu_irqb) ; Read the interrupt request bits
and A,bit1_mask ; and test bit one,
jp z,int_sdp_done ; skip if not delay and pulse interrupt.
ld A,(Sdelay) ; Check delay flag
add A,0 ; and if set, end delay and start pulse
jp z,int_sdp_pulse ; otherwise end pulse.
int_sdp_delay:
ld A,(Scurrent) ; Turn on the
ld (mmu_stc),A ; stimulus current.
ld A,(Spulse1) ; Load timer two
ld (mmu_i2ph),A ; with the
ld A,(Spulse0) ; pulse
ld (mmu_i2pl),A ; length.
ld A,0 ; Clear the
ld (Sdelay),A ; delay flag.
ld A,1 ; Set the
ld (Spulse),A ; pulse flag.
jp int_sdp_rst
int_sdp_pulse:
ld A,0 ; Stop the
ld (mmu_stc),A ; stimulus pulse.
ld (Spulse),A ; Clear pulse flag.
ld (Sdelay),A ; And the delay flag.
ld (mmu_i2ph),A ; Disable the timer two
ld (mmu_i2pl),A ; interrupt.
ld A,(mmu_imsk) ; Mask
and A,bit1_clr ; interrupt
ld (mmu_imsk),A ; two.
int_sdp_rst:
ld A,bit1_mask ; Reset Timer Two, which loads the
ld (mmu_irst),A ; the current period into its counter.
int_sdp_done:
; Handle the transmit interrupt, if it exists. We transmit a synchronizing signal.
; We won't wait for the transmission to complete because we are certain to follow
; our transmission with at least one RCK period when we move out of boost.
int_xmit:
ld A,(mmu_irqb) ; Read the interrupt request bits
and A,bit3_mask ; and test bit three,
jp z,int_xmit_done ; skip transmit if not set.
ld A,bit3_mask ; Reset this interrupt
ld (mmu_irst),A ; with the bit three mask.
ld A,(xmit_ch) ; Load A with telemetry channel number
ld (mmu_xch),A ; and write the transmit channel register.
; If a not Srun, we will transmit synch_nostim. If Srun but not Spulse,
; we transmit synch_stim. If Srun we transmit synch_stim + 8*Scurrent.
; Regardless, the lower byte we transmit will be zero.
ld A,0 ; Load A with zero
ld (mmu_xlb),A ; write to transmit LO register.
ld A,(Srun) ; Load A with Srun
add A,0 ; check value
jp nz,int_xmit_stim ; jump if set.
ld A,synch_nostim ; Load A with synch_nostim and
ld (mmu_xhb),A ; write to transmit HI register.
jp int_xmit_rdy
int_xmit_stim:
ld A,(Spulse) ; Load A with Spulse
add A,0 ; check value, jump if set.
jp nz,int_xmit_pulse
ld A,synch_stim ; Load A with synch_stim and
ld (mmu_xhb),A ; write to transmit HI register.
jp int_xmit_rdy
int_xmit_pulse:
ld A,(Scurrent) ; Load A with Scurrent and
sla A ; shift left
sla A ; three times to
sla A ; multiply by eight
add A,synch_stim ; then add synch_stim.
ld (mmu_xhb),A ; Write to transmit HI register.
int_xmit_rdy:
ld A,tx_txi ; Load transmit initiate bit
ld (mmu_xcr),A ; and write to transmit control register.
int_xmit_done:
; Turn off the transmit clock, move out of boost, restore registers and return
; from interrupt.
int_done:
pop IY
pop IX
pop L
pop H
pop E
pop D
pop C
pop B
ld A,(mmu_dfr) ; Load the diagnostic flag register.
and A,bit0_clr ; Clear bit zero and
ld (mmu_dfr),A ; write to diagnostic flag register.
ld A,0x00 ; Clear bit zero and use it to
ld (mmu_bcc),A ; move CPU back to slow RCK
ld (mmu_etc),A ; and stop the transmit clock.
pop F ; Restore the flags.
pop A ; Restore A.
rti ; Return from interrupt.
; ------------------------------------------------------------
; Transmit an annoucement, which consists of two auxiliary
; messages: one with data and another a confirmation immediately
; following. The two messages allow us to receive the annoucement
; and identify the device, as well as eliminate noise announcements.
; We pass the auxiliary type in register A and the auxiliary data
; in register B. The routine assumes we are running in boost with
; the interrupts disabled.
xmit_annc:
push F
push A
; Prepare the VCO for message transmission. We must warm it up or
; else its frequency will be wrong at tranmission time.
ld A,tx_txwp ; Turn on the VCO by writing the
ld (mmu_xcr),A ; warm-up bit to the transmit control register.
ld A,wp_delay ; Wait for a number of TCK periods while
dly A ; the VCO warms up.
ld A,0 ; Turn off the VCO and
ld (mmu_xcr),A ; let the battery recover
ld A,wp_delay ; before we
dly A ; transmit.
; Prepare the auxiliary message. For the channel number, we must
; set the bottom nibble to 0xF in order to identify this message
; as auxiliary. The top nibble is the top niblle of the device
; identifier's low byte. Within the auxiliary message, we begin with
; the bottom nibble of the device identifier's low byte. We follow
; with a nibble containing the auxiliary type, which has been
; passed in A. The second byte consists of the data in register B.
ld A,identifier_lo ; Load LO byte of identifier into A,
or A,0x0F ; set lower four bits to one
ld (mmu_xch),A ; and write the transmit channel register.
push B ; Transfer the data byte from
pop A ; B into A
ld (mmu_xlb),A ; and write to transmit LO register.
pop B ; Pop auxiliary type into B.
ld A,identifier_lo ; Load LO byte of identifier again.
sla A ; Shift A
sla A ; left
sla A ; four
sla A ; times.
or A,B ; Set the auxiliary type to acknowledgement.
ld (mmu_xhb),A ; Write to transmit HI register.
; Transmit the message.
ld A,tx_txi ; Initiate transmission with another write to
ld (mmu_xcr),A ; control register, which also turns off the warm-up.
ld A,tx_delay ; Wait for a number of TCK periods while
dly A ; the transmit completes.
; Transmit a confirmation to complete the announcement. The auxilliary
; type of a confirmation is always at_conf and the data byte is always
; the high byte of the device identifier.
ld A,identifier_hi ; Load HI byte id identifier into A and
ld (mmu_xlb),A ; write to transmit LO register.
ld A,identifier_lo ; Load LO byte of identifier into A,
or A,0x0F ; set lower four bits to one and
ld (mmu_xch),A ; write to the transmit channel register.
ld A,identifier_lo ; Load LO byte into A again,
sla A ; shift A
sla A ; left
sla A ; four
sla A ; times.
or A,at_conf ; The confirmation type code.
ld (mmu_xhb),A ; Write to transmit HI register.
ld A,tx_txi ; Initiate transmission with a write to the transmit
ld (mmu_xcr),A ; control register.
ld A,tx_delay ; Wait for confirmation
dly A ; transmition to complete.
pop F
ret
; ------------------------------------------------------------
; Transmit an acknowledgement. We put the auxiliary type in
; A and the acknowledgement key in B, then call our auxiliary
; message routine.
annc_ack:
push F
push A
push B
ld A,(Sack_key)
push A
pop B
ld A,at_ack
call xmit_annc
pop B
pop A
pop F
ret
; ------------------------------------------------------------
; Transmit a battery measurement. The battery measurement is
; inversely proportional to the battery voltage. We have:
; VBAT = 1.2 V * 256 / batt_meas. We must access twice to acquire
; and convert.
annc_batt:
push F
push A
push B
ld (mmu_scr),A ; Initiate conversion of battery voltage.
ld A,sa_delay ; Load sensor delay,
dly A ; Wait,
ld (mmu_scr),A ; convert again,
ld A,sa_delay ; wait
dly A ; again
ld A,(mmu_sdb) ; and get battery measurement.
push A ; Store battery voltage
pop B ; in B.
ld A,at_batt ; The battery type code.
call xmit_annc ; Transmit announcement.
pop B
pop A
pop F
ret
; ------------------------------------------------------------
; Announce the version number. We use A and B to pass the
; version message identifier and the version number itself.
annc_ver:
push F
push A
push B
ld A,version ; Put the version number
push A ; in register
pop B ; B for procedure call.
ld A,at_ver ; Load version type in A.
call xmit_annc ; Transmit annoucement.
pop B
pop A
pop F
ret
; ------------------------------------------------------------
; Announce the device identifier. We wait for time and then
; transmit the identifier and confirmation messages that
; make up the announcement.
annc_id:
push F
push A
push B
push H
push L
; Delay for id_delay clock cycles multiplied by numeric value
; of the device id. By this means, each device transmits its
; identifying message at a different time. With id_delay set
; to 2*tx_delay, no two ISTs will collide, but we may have to
; wait 1.4 s for all our answers. Using tx_delay, two ISTs will
; collide only if their IDs are consecutive, and we will wait
; only 0.7 s.
ld A,identifier_hi
push A
pop H
ld A,identifier_lo
push A
pop L
identify_delay:
ld A,id_delay
dly A
push L
pop A
sub A,1
push A
pop L
push H
pop A
sbc A,0
push A
pop H
jp nc,identify_delay
; Prepare A and B for call to xmit_annc.
ld A,identifier_hi ; Load HI byte id identifier
push A ; into A and
pop B ; store in B.
ld A,at_id ; Load the identify type code into A.
call xmit_annc ; Transmit annoucement.
; Return.
pop L
pop H
pop B
pop A
pop F
ret
; ------------------------------------------------------------
; Read a byte out of the command memory portal and store in a
; variable location, as well as returning it in A. With gcb_dsp
; set, we produce a serial display of the byte on diagnostic flag
; one. We assume interrupts are disabled and the CPU is running
; on the boost clock.
get_cmd_byte:
const gcb_dsp 0 ; Set to one for debugging.
const gcb_dly 33 ; Bit period for display.
const gcb_nb 11 ; Total number of bits minus start bit.
push F
ld A,(mmu_cmp) ; Read from command FIFO portal.
ld (ccmdb),A ; Store byte in memory.
ld A,gcb_dsp ; Check if we should display the byte.
and A,bit0_mask ; If not, jump to end
jp z,gcb_done ; of routine.
push B ; Push the two registers we are
push C ; going to use.
ld A,(mmu_dfr) ; Display the start bit.
or A,bit1_mask ; and
ld (mmu_dfr),A ; wait for
ld A,gcb_dly ; proscribed delay.
dly A
ld A,gcb_nb ; We are going to
push A ; transmit this number of bits
pop B ; plus a stop bit.
ld A,(ccmdb) ; Make a copy of the command
push A ; byte in
pop C ; register C.
gcb_loop:
and A,bit7_mask ; Check the most significant bit
jp nz,gcb_hi ; in the remaining command bits.
ld A,(mmu_dfr) ; Transmit a zero and wait.
and A,bit1_clr
ld (mmu_dfr),A
ld A,gcb_dly
dly A
jp gcb_sl
gcb_hi:
ld A,(mmu_dfr) ; Transmit a one and wait.
or A,bit1_mask
ld (mmu_dfr),A
ld A,gcb_dly
dly A
gcb_sl:
push C ; Get the remaining bits
pop A ; and shift them to
sla A ; the left, bringing a zero
push A ; in for the least significant
pop C ; bit, copy into C.
dec B ; Check bit counter and
jp nz,gcb_loop ; repeat if still not zero.
pop C ; Recover C and
pop B ; B registers.
gcb_done:
ld A,(ccmdb) ; Load the command byte into A.
pop F
ret
; ------------------------------------------------------------
; Read out, interpret, and execute comands. Uses the global command
; count variable, stimulus and configuration locations, and starts
; and stops stimuli, transmission, battery measurement and
; acknowledgements. The routine assumes that the user program pointer
; is stored in IY upon entry, and will pass IY back after modification.
cmd_execute:
; Push the flags onto the stack and disable interrupts. Allowing interrupts
; while we are configuring a stimulus or a transmission is more challenging
; than simply turning them off and making sure everything is set up properly