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