chiark / gitweb /
start scanning with board 1, not 2. trying to debug it still ...
[trains.git] / detpic / mascan.asm
1 ;======================================================================
2 ; MASTER - SCANNING ETC.
3
4   include common.inc
5
6 ;----------------------------------------------------------------------
7   udata_acs
8
9 ; for reading and detection:
10 b       res     1       ; byte just read
11 cslot   res     1       ; current slave in slave table, points to flags byte
12 cbyte   res     1
13         ; one of the following:
14         ;       0000 0000       we're expecting the first byte
15         ;       M0B1 0000       we're expecting more detection byte 1
16         ;       M010 0000       we're expecting more detection byte 2
17         ;       1000 0000       we're expecting an extra byte
18         ;       ???? ???1       reading halted due to lack of buffer space:
19 cbyte_halted equ 0 ; also set briefly while we decide what to do next
20
21 wslave                  res 1 ; slave we need to write to
22 outmsg_targetlen        res 1 ; 
23
24 ;======================================================================
25 ; HANDLING OF I2C EVENTS
26
27 near_getwritebyteyes code
28 ;----------------------------------------
29 i2c_getwritebyte_yes @
30         pop             ; we don't care where we were in i2cmu_write_next_byte
31         bc_f    STATUS, Z       ; yes, we want to write this byte
32         return          ; from i2cmu_write_next_byte
33         
34   code
35 ;----------------------------------------
36 i2cmu_write_next_byte
37         ; add calls to getwritebyte_<foo> here:
38         call    polarity_getwritebyte
39         call    points_getwritebyte
40         ; end of list of calls to getwritebyte_<foo>
41         ; so, no-one wants this slave:
42         bs_f    STATUS, Z
43         return
44
45 ;----------------------------------------
46 i2cm_intrl @
47 ; handles i2c interrupt using i2cm_interrupt[_definite],
48 ; according to the rules for <something>_intrl.
49         bt_f_if0 PIR1, SSPIF
50         return
51         call    i2cm_interrupt_definite
52         intrl_handled_nostack
53
54 ;======================================================================
55 ; PROCESSING OF INCOMING BYTES - CORE AND DETECTION
56
57 ;----------------------------------------
58 i2cmu_read_got_byte
59 ;               Beforehand      At call
60 ;   State       Reading         Reading-Wait
61 ;   W                           data from slave
62 ;
63 ; See detect.asm head comment for protocol info, in particular
64 ; the meaning of these bytes.
65                                 ; W =           received byte
66         mov_wf  b               ; W = b =       received byte
67
68  panic morse_TI4
69         mov_lfsr slavetable, 1
70         mov_ff  cslot, FSR1L    ; FSR1 ->       slave's flags
71
72         tst_f_ifnz cbyte
73         bra     read_got_notfirst
74         ; this is a first (head) byte:
75
76         bt_f_if0 POSTINC1, stf_detect ; FSR1 -> detbasel
77         bra     read_got_first_reversers
78 read_got_first_detectors        ; b =           MdBBdddd
79         and_lw  0xb0            ; W =           M0BB0000
80         mov_wf  cbyte           ; cbyte =       M0BB0000
81         mov_lw  0x4f            ; W =           01001111
82         and_wff b               ; b =           0d00dddd
83         mov_fw  POSTINC1        ; W = detbasel; FSR1 -> lastd0
84         rcall   read_got_detectbyte_prep ; b =  0d00dddd
85                                          ; u =  0C00CCCC
86         bt_f_if1 b, 6           ; b bit         .d......
87         bs_f    b, 4            ; b =           0d0ddddd
88                                 ;                ^ ^ copies of same bit
89                                 ; u = (still)   0C00CCCC
90                                 ; or, using detection segment bit numbers:
91                                 ; b =           z4z43210
92                                 ; u =           z4zz3210
93         goto    addmsgs_dethead
94
95 ;----------
96 read_got_first_reversers
97         bt_f_if1 b, 6
98         bra     read_got_bad_first_reversers
99         and_lw  0x80            ; W =           M0000000
100         mov_wf  cbyte           ; cbyte =       M0000000
101         bc_f    b, 7            ; b =           00dddddd
102         mov_fw  POSTINC1        ; W = detbasel; FSR1 -> lastd0
103         rcall   read_got_detectbyte_prep
104         goto    addmsgs_revhead
105
106 ;-----
107 read_got_bad_first_reversers panic morse_MR
108
109 ;----------
110 read_got_detectors_b1           ; b =           dddddddd
111         bc_f    cbyte, 4        ; cbyte =       M0B00000
112         mov_fw  POSTINC1        ; W = detbasel; FSR1 -> lastd0
113         inc_f   FSR1L           ; FSR1 -> lastd1
114         add_lw  5               ; W = detbasel+8
115         rcall   read_got_detectbyte_prep
116         goto    addmsgs_all
117
118 ;----------
119 read_got_detectors_b2           ; b =           dddddddd
120         bc_f    cbyte,5         ; cbyte =       M0000000
121         mov_fw  POSTDEC1        ; W = detbasel; FSR1 -> flags
122         bs_f    FSR1L, 2        ; FSR1L ->      lastd2
123         add_lw  13              ; W = detbasel+16
124         rcall   read_got_detectbyte_prep
125         goto    addmsgs_all
126
127 ;----------
128 read_got_detectbyte_prep_ifsomething
129 ;
130 ; This is a branch of read_got_detectbyte_prep, called if we're doing
131 ; `return' rather than `pop+return'.  For conditions on return, see
132 ; read_got_detectbyte_prep; these are supposed to be (basically) the
133 ; same as the entry conditions for addmsgs_<kind>.
134 ;
135 ;               on entry
136 ;  W            [C0]*
137 ;  t            adjdetbasel
138 ;  u            undefined
139 ;  b            [d0]*
140 ;  lastd<n>     [o0]*
141 ;  FSR1 ->      lastd<n>
142 ;
143         mov_wf  u               ; u =           [C0]*
144         xor_wfw INDF1           ; lastd<n> =    [d0]*
145         mov_lw  0x07
146         ior_wff FSR1L           ; FSR1L ->      detmsgh
147         return ; to addmsgs_<something>, very shortly
148
149 ;----------------------------------------
150 read_got_notfirst
151         bt_f_if1 cbyte, 4
152         bra     read_got_detectors_b1
153         bt_f_if1 cbyte, 5
154         bra     read_got_detectors_b2
155         ; it must be an extra byte
156
157         bt_f_if0 b, 7           ; any more ?
158         bc_f    cbyte, 7
159         bc_f    b, 7
160         call    process_got_extra
161         bra     i2c_arrange_next_byte
162
163 ;----------
164 read_got_detectbyte_prep
165 ;
166 ; Sees if anything has changed.  If no changes pops one return address
167 ; and branches to i2c_arrange_next_byte; if some changes, returns to
168 ; calling point.
169 ;
170 ; So, caller should be i2cmu_read_got_byte, and next stuff
171 ; should basically be a call to addmsg_<something> (perhaps preceded
172 ; by a bit of fiddling of b).  addmsg_<something> will finish
173 ; by branching to i2c_arrange_next_byte.
174 ;
175 ;               call                    return          pop+return
176 ;  W            adjdetbasel             preserved       undefined
177 ;  b            [d0]*                   preserved       preserved
178 ;  FSR1 ->      lastd<n>                detmsgh         preserved
179 ;  cbyte        set for next read etc.  preserved       preserved
180 ;  u            undefined               [C0]*           preserved
181 ;  lastd<n>     [o0]*                   [d0]*           preserved = [d0]*
182 ;
183 ;  TOS ->       (optionally, fiddle b, and then:) goto addmsgs_<something>
184 ;  NOS          return address for i2cmu_read_got_byte
185 ;
186         mov_wf  t               ; t =           adjdetbasel
187         mov_fw  b               ; W =           [d0]*
188         xor_wfw INDF1           ; W =           [C0]*, Z iff same
189                         ;  where C set iff change to that detection segment
190         bra_nz  read_got_detectbyte_prep_ifsomething
191         ; there's nothing to do
192         pop
193 ;...
194 ;----------------------------------------
195 i2c_arrange_next_byte
196         tst_f_ifnz cbyte
197         goto    i2cm_read_another
198 ;...
199 ;======================================================================
200 ; DECIDING WHICH SLAVE TO ADDRESS
201 ;...
202 i2c_arrange_something
203 i2cmu_done
204 ; figure out what to do next - which pic to address, etc.
205         bs_f    cbyte, cbyte_halted
206
207         tst_f_ifnz wslave       ; anyone asked to write ?
208         bra     arrange_write
209
210         ; Anyone else to write to ?
211         ; add calls to needwrite_<foo> here:
212         call    polarity_needwrite
213         call    points_needwrite
214         ; end of list of calls to needwrite_<foo>
215
216         ; no, if we're here, no-one wants to write:
217
218 nextslave_nowrite
219         ; no writing needed, we consider reading:
220         bt_f_if1 flags, flags_polarising
221         return  ; do not scan while booster PWM is off while polarising
222                 ; to avoid all trains disappearing and reappearing
223
224         neg_fw  outmsg_begin
225         add_wfw outmsg_end
226         and_lw  outbuf_size - 1
227         cmp_fw_ifle outmsg_targetlen
228         return ; target len < actual len, do not add anything
229         ; ok, there's space, go ahead:
230         bc_f    cbyte, cbyte_halted
231
232         mov_lw  ste_size
233         add_wff cslot           ; cslot -> next ste_flags
234         mov_lfsr slavetable, 1  ; FSR1H -> slavetable
235         mov_ff  cslot, FSR1L    ; FSR1 -> new ste_flags
236
237  panic morse_TI3
238         bt_f_if1 POSTDEC1, stf_sentinel ; FSR1 -> ste_slave
239         bra     nextslave_looparound
240         ; Ok, we have a slave:
241
242         mov_fw  INDF1           ; W = new slave number
243         goto    i2cm_read_start
244
245 nextslave_looparound
246         ; now we do our own detection
247         mov_lw  (slavetable + ste_flags) & 0xff ; select our own slot
248         mov_wf  cslot
249         call    read_detection_head_master
250         goto    i2cmu_read_got_byte
251
252
253 ;----------------------------------------
254 i2c_needwrite @
255 ; Informs mascan that we need to write to some slave.
256 ; Some time after this, mascan will call getwritebyte_<everything>
257 ; and this must yield at least one byte to write.
258 ;  W            slave that we must write to
259         mov_wf  wslave
260         bt_f_if0 cbyte, cbyte_halted
261         return ; we're currently doing something
262 ;...    
263 ;----------
264 arrange_write
265 ;  wslave       slave to write to
266         bc_f    cbyte, cbyte_halted
267         mov_fw  wslave
268         mov_wf  cwslave
269         clr_f   wslave
270         goto    i2cm_write_start
271
272 ;----------------------------------------
273 i2c_consider_restartread @
274         bt_f_if0 cbyte, cbyte_halted
275         return
276         bra     nextslave_nowrite
277
278 ;======================================================================
279 ; INITIALISATION
280
281 mascan_init @
282         mov_lw  (slavetable + ste_flags) & 0xff
283                  ; pretend we've just done us, to start with 1st actual slave
284         mov_wf  cslot
285         clr_f   cbyte
286         bs_f    cbyte, cbyte_halted ; serial output of `hello' will start us up
287         clr_f   wslave
288
289         mov_lw  2
290         mov_lfsr slavetable, 0          ; FSR0 -> slavetable
291         load_tblptr pic2detinfo         ; TBLPTR* -> pic2detinfo
292         clr_f   t                       ; t = loop counter
293 mascan_init_loop
294         tblrd_postinc_fixup             ; TABLAT = EOOOOSSS
295         mov_fw  TABLAT                  ; W = EOOOOSSS, N = E
296
297         tblrd_postinc_fixup             ; TABLAT = DSSSSSSS, N (still) = E
298         bra_nn  mascan_init_ifabsent
299
300         mov_ff  t, POSTINC0             ; ste_slave = slave
301         mov_wf  u                       ; u = 1OOOOSSS
302         and_lw  0x78
303         bra_nz  mascan_bad_detinfo0
304         ; ok                            ; u = 10000SSS
305
306         mov_fw  TABLAT                  ; W = DSSSSSSS
307         and_lw  0x80                    ; W = D0000000
308         mov_wf  POSTINC0                ; ste_flags = D0000000
309
310         mov_fw  TABLAT                  ; W = DSSSSSSS
311         bc_w    7                       ; W = 0SSSSSSS = first
312         add_lw  -0xf8                   ; W = first - 0xf8 = detbasel
313         mov_wf  POSTINC0                ; detbasel
314
315         clr_f   POSTINC0                ; lastd0
316         clr_f   POSTINC0                ; lastd1
317         clr_f   POSTINC0                ; lastd2
318         set_f   POSTINC0                ; unused
319
320         mov_fw  u                       ; W = 10000SSS
321         xor_lw  b'10011000' ^ 0x80      ; W = detmsgh
322         mov_wf  POSTINC0                ; detmsgh
323
324 mascan_init_ifabsent
325         inc_f   t                       ; next slave
326         bt_f_if0 t, maxpics_ln2
327         bra     mascan_init_loop
328         ; we've read the whole flash table
329
330   if slavetable == 0x400
331         bt_f_if1 FSR0H, 0
332         bra     mascan_bad_toomany
333   endif
334
335         clr_f   POSTINC0                ; ste_slave
336         mov_lw  (1<<stf_detect)|(1<<stf_sentinel)
337         mov_wf  POSTINC0                ; ste_flags
338         ; rest of final entry, and rest of table, is undefined
339
340         ; at 9600, it's about 1ms per char.
341         ; we allow 1ms to scan 2 pics via i2c.
342         ; so our target len is (no of pics)/2
343         ; plus 2 bytes of slop
344         rl_fw   t
345         add_lw  2
346         mov_wf  outmsg_targetlen
347
348         return
349
350 mascan_bad_detinfo0 panic morse_DF
351 mascan_bad_toomany panic morse_DG
352
353 ;======================================================================
354 ; PROCESSING OF INCOMING BYTES - EXTRA (NON-DETECTION)
355
356 near_gots code
357 ;----------------------------------------
358 process_got_extra @
359         mov_fw  b
360 loopback_read_byte @
361 ;...
362 ;  W            message
363         xor_lw  0x00 ^ 0x20
364         bra_z   got_pointed
365         xor_lw  0x20 ^ 0xb0
366         bra_z   got_aargh
367         panic   morse_MX
368
369 ;======================================================================
370 ; GENERATION OF DETECTION MESSAGES FOR HOST - MAD BT_F_IF1 TABLES
371
372 addmsg_testbit macro bit
373         bt_f_if1 u, bit
374         rcall   addmsg_one
375         endm
376
377 addmsg_return macro dummy_bit
378         goto    i2c_arrange_next_byte
379         endm
380
381 addmsg_ignore macro dummy_bit
382         nop
383         nop
384         endm
385
386 addmsg_padding macro dummy_bit
387         nop
388         mov_lw  dummy_bit
389         endm
390
391 ;----------------------------------------
392 ;addmsgs_<kind>
393 ;
394 ;                       on entry        after first addmsg_one, or when done
395 ;  W, STATUS, v, FSR0   undefined       trashed
396 ;  t                    adjdetbasel     not modified by addmsgs_<kind> or _one
397 ;  u                    [C0]*           not modified by addmsgs_<kind> or _one
398 ;  b                    [d0]*           not modified by addmsgs_<kind> or _one
399 ;  lastd<n>             [d0]* (new)     not modified by addmsgs_<kind> or _one
400 ;  FSR1 ->              detmsgh         not modified by addmsgs_<kind> or _one
401 ;  outbuf, outmsg_*     not full, updated appropriately
402 ;  all others           any             not interfered with
403 ;  
404 ; (this is all set up by read_prep_detectbyte and
405 ;  read_got_detectbyte_prep_ifsomething)
406 ; when done, branches to i2c_arrange_next_byte, rather than returning
407
408 addmsgs_section code (          7       )*4 + 0x2100
409 ;
410 ;                               A
411 ;                               |- PCL bbb value after macro - number in this
412 ;                               V       column should increment 1 each line
413 ;
414 addmsgs_revhead addmsg_testbit  0
415                 addmsg_testbit  1
416                 addmsg_testbit  2
417                 addmsg_testbit  3
418                 addmsg_testbit  4
419                 addmsg_testbit  5
420                 addmsg_return   6
421
422 addmsgs_all     addmsg_testbit  7
423                 addmsg_testbit  0
424                 addmsg_testbit  1
425                 addmsg_testbit  2
426                 addmsg_testbit  3
427                 addmsg_testbit  4
428                 addmsg_testbit  5
429                 addmsg_testbit  6
430                 addmsg_return   7
431
432 addmsgs_dethead addmsg_testbit  0
433                 addmsg_testbit  1
434                 addmsg_testbit  2
435                 addmsg_testbit  3
436                 addmsg_testbit  6 ; bit 6 was copied to 4 but only in b, not u
437                 addmsg_return   5
438
439 ;----------
440 addmsg_one
441 ;  TOS - 4 ->   bt_f_if1 w, b'bbb'
442 ;  TOSL         ???bbb00
443 ; other conditions on entry and exit as for entry to addmsgs_<kind>, above
444  panic morse_TI5
445         rr_fw   TOSL            ; W =           0???bbb0
446         rr_w                    ; W =           00???bbb
447         ior_wfw 0xf8            ; W =           11111bbb
448         mov_wf  FSR0L           ; FSR0L =       11111bbb
449         clr_f   FSR0H           ; FSR0 ->       bitnum2bit[bbb]
450         add_wfw t               ; W =           adjdetbasel + 11111bbb
451                                 ;  ie =         0 SSSSSSS (det msg low byte)
452         mov_wf  v               ; v =           0 SSSSSSS (det msg low byte)
453
454         mov_fw  b               ; W =           [d0]*
455         and_wfw INDF0           ; train: W = 0x00, Z=1; none: W = 0*d0*, Z=0
456         bt_f_if0 STATUS, Z
457         mov_lw  0x08            ; train: W = 0 000 0 000; none: W = 0 000 1 000
458         xor_wfw INDF1           ; W =           1 001 Y SSS (det msg high byte)
459         call    serial_addbyte
460
461         mov_fw  v               ; W =           0 SSSSSSS (det msg low byte)
462         goto    serial_addbyte_another
463
464 ;======================================================================
465   include final.inc