chiark / gitweb /
hostside: hidrawconv: declarations for hatswitches; impl'n is still no-op
[trains.git] / detpic / misc.asm
1 ;======================================================================
2
3   include common.inc
4
5   udata_acs
6 dsave_w         res     1
7 dsave_fsr0      res     2
8 debugp          res     1
9
10 debug_section udata 0x100
11 debug   res     256
12
13   code
14 ;======================================================================
15 ; GENERALLY USEFUL ROUTINES
16
17 ;----------
18 read_pic_no @
19 ; read pic no from ID locations and return it
20 ;       W               undefined               pic number from ID loc 0
21 ;       status Z        undefined               1 iff master PIC
22 ;       TBLPTR*         undefined               points to 20001
23
24         mov_lw  0x20
25         mov_wf  TBLPTRU
26         clr_f   TBLPTRH
27         clr_f   TBLPTRL
28
29         tblrd_postinc_fixup
30         mov_fw  TABLAT
31         return
32
33 ;----------
34 idlocs_init @
35 ; read id locations and store in canonical place
36 ;       W               undefined               undefined
37 ;       picno           undefined               pic number (from 20000)
38 ;       idloc1          undefined               idloc1 (from 20001)
39
40         rcall   read_pic_no
41         mov_wf  picno
42
43         tblrd_postinc_fixup
44         mov_fw  TABLAT
45         mov_wf  idloc1
46
47         return
48
49 ;----------
50 bitnum2bit_init @
51 ; populate bitnum2bit
52         mov_lw  0x80
53         mov_lfsr bitnum2bit,0
54 init_bitnum2bit_loop
55         rl_w
56         mov_wf  POSTINC0
57         bra_nn  init_bitnum2bit_loop
58         return
59
60 ;----------
61 memory_erase @
62 ;  picno                        preserved
63 ;  idloc1                       preserved
64 ;  other GPRs                   overwritten with 0xbb
65         mov_ff  picno, FSR1L
66         mov_ff  idloc1, TABLAT
67         mov_lfsr 0, 0
68 memory_erase_pageloop
69         mov_lw  0xbb
70 memory_erase_byteloop
71         mov_wf  POSTINC0
72         mov_wf  POSTINC0
73         mov_wf  POSTINC0
74         mov_wf  POSTINC0
75         mov_wf  POSTINC0
76         mov_wf  POSTINC0
77         mov_wf  POSTINC0
78         mov_wf  POSTINC0
79         tst_f_ifnz FSR0L
80         bra     memory_erase_byteloop
81         ; new page:
82
83         mov_lw  0x06
84         xor_wfw FSR0H
85         bra_nz  memory_erase_pageloop
86         ; done!
87
88         mov_ff  FSR1L, picno
89         mov_ff  TABLAT, idloc1
90         return
91
92 ;----------
93 debugbyte @
94 ; Adds a byte to debug
95 ;  W            message byte            preserved
96 ;  STATUS       any                     trashed
97 ;  all others   any                     preserved
98 ; Not for use in High ISR
99         mov_ff  FSR0L, dsave_fsr0
100         mov_ff  FSR0H, dsave_fsr0+1
101         mov_lfsr debug, 0
102         mov_ff  debugp, FSR0L
103         mov_wf  INDF0
104         inc_f   debugp
105         mov_ff  dsave_fsr0,   FSR0L
106         mov_ff  dsave_fsr0+1, FSR0H
107         return
108
109 ;------------------------------------------------------------
110 outpins_local_init_part1 @
111 ; Initialises a table of output pins
112 ;       TBLPTR          picno2<thing>map        (presence bitmap)
113 ;       FSR0            <thing>ix2latbit - 1    (start of output table)
114 ;       W               max<thing>s / 8         (table sizes)
115 ; outpins_local_init_part2 @
116 ;       TBLPTR          bk<thing>ix2portnumbitnum       (phys pins)
117 ;       FSR0            <thing>ix2latbit - 1    (start of output table)
118 ;       W               max<thing>s             (table sizes)
119 ; Caller must use both, and set variables as above on each entry.
120 ;
121 ; Input table is as made by writeasm_outpin
122 ; Result table is series of 2-byte entries,
123 ;   0th byte is  bottom byte of LAT* address
124 ;   1st byte is  single-bit mask for the relevant pin
125
126 ;  Internals:
127 ;       We do this in two stages.
128 ;       Firstly, we scan the bitmap for this pic, setting
129 ;        <thing>ix2latbit to 0xff,0x00 for used points and 0x00,0x00
130 ;        to unused ones.
131 ;       Secondly, we scan the bk<thing>ix2portnumbitnum, adjusting
132 ;        <thing>ix2latbit to have actually correct data.
133 ;       Doing it like this avoids having to constantly recompute
134 ;        individual TBLPTR*'s.
135
136 ;        On entry FSR0 -> this bit and LAT*
137 ;         points just at last thing we've filled in
138
139         mov_wf  t               ; t = byte counter
140 ;...
141 outpins_init_byte_loop
142         mov_lw  8               ; W = bit counter
143         tblrd_postinc_fixup     ; TABLAT = bitmap data being processed
144 ;...
145 outpins_init_bit_loop
146         clr_f   PREINC0         ; FSR0 -> LAT*[current] := 0
147         rrc_f   TABLAT
148         bt_f_if1 STATUS,C
149         set_f   INDF0           ; FSR0 -still-> LAT*[current] := 0xff
150
151         clr_f   PREINC0         ; FSR0 -> bit[current] := 0
152
153         dec_w_ifnz
154         bra     outpins_init_bit_loop
155         dec_f_ifnz t
156         bra     outpins_init_byte_loop
157 ;... end of loop:
158
159
160 ;       We've scanned for points used on this board;
161 ;       now find the actual pins.
162
163         return             ; caller will reset
164 outpins_local_init_part2 @ ;  TBLPTR, FSR0 and W (see above)
165
166         bt_f_if1 idloc1,idloc1_boarddet
167         add_wff TBLPTRL
168
169         set_f   FSR2H           ; FSR2 -> some SFR, will point to LAT/TRIS
170         mov_lfsr bitnum2bit+7, 1 ; FSR1 -> bitnum2bit+7
171
172         mov_wf  t               ; t = loop counter
173 ;...
174 outpins_init_portbit_loop
175         tblrd_postinc_fixup     ; TABLAT = portnum4 || bitnum4
176
177         bt_f_if0 PREINC0,7      ; zero?, FSR0 -> LAT*[this]
178         bra     outpins_init_portbit_endif_used
179 ;...
180 outpins_init_portbit_if_used
181         mov_fw  TABLAT
182         bra_n   outpins_initing_bad_object
183
184         ior_lw  0xf8            ; W -> bit value for bit
185         mov_wf  FSR1L           ; FSR1 -> bit value for bit
186
187         swap_fw TABLAT          ; W = bitnum4 || portnum4
188         and_lw  0x0f            ; W = portnum4
189         add_lw  LATA & 0xff     ; W = LAT*
190         mov_wf  POSTINC0        ; LAT*[this] := LAT, FSR0 -> bit[this]
191         mov_wf  FSR2L           ; FSR2 -> LAT*
192
193         mov_fw  INDF1           ; W = bit
194         mov_wf  POSTDEC0        ; bit[this] = bit, FSR0 -> LAT*[this]
195         com_w                   ; W = ~bit
196         and_wff INDF2           ; LAT* &= ~bit, ie pin set to L (still Z)
197
198         pin_vh  pall_pt0reverse ; but pt0 pin is backwards, set to H
199                                 ;  (still Z, unless we've done this already)
200                                 ;  (we don't care about doing this far too
201                                 ;   many times here.)
202         mov_lw  TRISA-LATA
203         add_wff FSR2L           ; FSR2 -> TRIS*
204         com_fw  INDF1           ; W = ~bit
205         and_wff INDF2           ; TRIS* &= ~bit, ie pin set to not Z
206
207         set_f   FSR1L           ; FSR1 -> bitnum2bit+7, again
208 outpins_init_portbit_endif_used
209         ; so now we move on to the next one
210         mov_fw  POSTINC0        ; FSR0 -> bit[this]
211
212         dec_f_ifnz t
213         bra     outpins_init_portbit_loop
214
215         return
216
217 ;----------
218 outpins_initing_bad_object
219         panic   morse_OF
220
221   include final.inc