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