1 ;======================================================================
2 ; MASTER - SCANNING ETC.
6 ;----------------------------------------------------------------------
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
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
21 wslave res 1 ; slave we need to write to
22 outmsg_targetlen res 1 ;
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
29 ;======================================================================
30 ; HANDLING OF I2C EVENTS
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
40 ;----------------------------------------
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:
51 ;----------------------------------------
53 ; handles i2c interrupt using i2cm_interrupt[_definite],
54 ; according to the rules for <something>_intrl.
58 call i2cm_interrupt_definite
61 ;======================================================================
62 ; PROCESSING OF INCOMING BYTES - CORE AND DETECTION
67 ; W received byte preserved
68 ; b received byte preserved
71 bs_f INDF1, stf_responding
72 dec_f_ifnz slaves_awaiting
74 ; that's the last slave, excellent
75 mov_lw b'00001001' ; W trashed
77 mov_fw b ; W = received byte (again)
80 ;----------------------------------------
83 ; State Reading Reading-Wait
86 ; See detect.asm head comment for protocol info, in particular
87 ; the meaning of these bytes.
89 mov_wf b ; W = b = received byte
94 Df b ; also restores b to W
96 mov_lfsr slavetable, 1
97 mov_ff cslot, FSR1L ; FSR1 -> slave's flags
100 bra read_got_notfirst
101 ; this is a first (head) byte:
103 bt_f_if0 INDF1, stf_responding ; seen this PIC ack already ?
104 rcall read_first_response ; no, we might have to send HELLO
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
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:
126 read_got_first_reversers
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
137 read_got_bad_first_reversers panic morse_MR
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
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
159 read_got_detectbyte_prep_ifsomething
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>.
178 Df u ; also restores u to W
180 xor_wff INDF1 ; lastd<n> = [d0]*
181 ; to force test of repeated detection notification,
182 ; comment out previous line
184 ior_wff FSR1L ; FSR1L -> detmsgh
185 return ; to addmsgs_<something>, very shortly
187 ;----------------------------------------
190 mov_fw PREINC1 ; W = detbasel; FSR1 -> detbasel
192 bra read_got_detectors_b1
194 bra read_got_detectors_b2
195 ; it must be an extra byte
197 bt_f_if0 b, 7 ; any more ?
200 call process_got_extra
201 bra i2c_arrange_next_byte
204 read_got_detectbyte_prep
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
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.
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]*
223 ; TOS -> (optionally, fiddle b, and then:) goto addmsgs_<something>
224 ; NOS return address for i2cmu_read_got_byte
226 mov_wf t ; t = adjdetbasel
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
237 ;----------------------------------------
238 i2c_arrange_next_byte
240 goto i2cm_read_another
242 ;======================================================================
243 ; DECIDING WHICH SLAVE TO ADDRESS
245 i2c_arrange_something
247 ; figure out what to do next - which pic to address, etc.
248 bs_f cbyte, cbyte_halted
250 tst_f_ifnz wslave ; anyone asked to write ?
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>
259 ; no, if we're here, no-one wants to write:
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
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
276 add_wff cslot ; cslot -> next ste_flags
277 mov_lfsr slavetable, 1 ; FSR1H -> slavetable
278 mov_ff cslot, FSR1L ; FSR1 -> new ste_flags
280 bt_f_if1 POSTDEC1, stf_sentinel ; FSR1 -> ste_slave
281 bra nextslave_looparound
282 ; Ok, we have a slave:
285 mov_fw INDF1 ; W = new slave number
289 ; now we do our own detection
290 mov_lw (slavetable + ste_flags) & 0xff ; select our own slot
293 call read_detection_head_master
294 goto i2cmu_read_got_byte
296 ;----------------------------------------
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
303 bt_f_if0 cbyte, cbyte_halted
304 return ; we're currently doing something
308 ; wslave slave to write to
309 bc_f cbyte, cbyte_halted
313 goto i2cm_write_start
315 ;----------------------------------------
316 i2c_consider_restartread @
317 bt_f_if0 cbyte, cbyte_halted
319 bra nextslave_nowrite
321 ;----------------------------------------
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
328 dec_f_ifnz slavenoack_counter
330 dec_f_ifnz slavenoack_counter+1
337 ;======================================================================
341 mov_lw (slavetable + ste_flags) & 0xff
342 ; pretend we've just done us, to start with 1st actual slave
345 bs_f cbyte, cbyte_halted
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
355 mov_lfsr slavetable, 0 ; FSR0 -> slavetable
356 load_tblptr pic2detinfo ; TBLPTR* -> pic2detinfo
357 clr_f t ; t = loop counter
359 tblrd_postinc_fixup ; TABLAT = EOOOOSSS
360 mov_fw TABLAT ; W = EOOOOSSS, N = E
362 tblrd_postinc_fixup ; TABLAT = DSSSSSSS, N (still) = E
363 bra_nn mascan_init_ifabsent ; E.......
365 inc_f slaves_awaiting
367 mov_ff t, POSTINC0 ; ste_slave = slave
368 mov_wf u ; u = 1OOOOSSS
370 bra_nz mascan_bad_detinfo0
373 mov_fw TABLAT ; W = DSSSSSSS
374 and_lw 0x80 ; W = D0000000
375 mov_wf POSTINC0 ; ste_flags = D0000000
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
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
390 mov_fw u ; W = 10000SSS
391 xor_lw b'10011000' ^ 0x80 ; W = detmsgh
392 mov_wf POSTINC0 ; detmsgh
396 bt_f_if0 t, maxpics_ln2
398 ; we've read the whole flash table
400 if slavetable == 0x400
402 bra mascan_bad_toomany
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
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
416 mov_wf outmsg_targetlen
420 mascan_bad_detinfo0 panic morse_DF
421 mascan_bad_toomany panic morse_DG
423 ;======================================================================
424 ; PROCESSING OF INCOMING BYTES - EXTRA (NON-DETECTION)
427 ;----------------------------------------
439 ;======================================================================
440 ; GENERATION OF DETECTION MESSAGES FOR HOST - MAD BT_F_IF1 TABLES
442 addmsg_testbit macro addmsg_macro_bit
443 bt_f_if1 u, addmsg_macro_bit
447 addmsg_return macro dummy_addmsg_macro_bit
448 goto i2c_arrange_next_byte
451 addmsg_ignore macro dummy_addmsg_macro_bit
456 addmsg_padding macro dummy_addmsg_macro_bit
458 mov_lw dummy_addmsg_macro_bit
461 ;----------------------------------------
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
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
478 addmsgs_section code ( 7 )*4 + 0x2100
481 ; |- PCL bbb value after macro - number in this
482 ; V column should increment 1 each line
484 addmsgs_revhead addmsg_testbit 0
492 addmsgs_all addmsg_testbit 7
502 addmsgs_dethead addmsg_testbit 0
506 addmsg_testbit 6 ; bit 6 was copied to 4 but only in b, not u
511 ; TOS - 4 -> bt_f_if1 u, b'bbb'
513 ; other conditions on entry and exit as for entry to addmsgs_<kind>, above
516 rr_fw TOSL ; W = 0???bbb0
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)
527 and_wfw INDF0 ; train: W = 0x00, Z=1; none: W = 0*d0*, Z=0
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)
534 mov_fw v ; W = 0 SSSSSSS (det msg low byte)
536 goto serial_addbyte_another
538 addmsg_bad panic morse_DJ
540 ;======================================================================