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 due to lack of buffer space:
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 ;======================================================================
25 ; HANDLING OF I2C EVENTS
27 near_getwritebyteyes code
28 ;----------------------------------------
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
35 ;----------------------------------------
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:
45 ;----------------------------------------
47 ; handles i2c interrupt using i2cm_interrupt[_definite],
48 ; according to the rules for <something>_intrl.
51 call i2cm_interrupt_definite
54 ;======================================================================
55 ; PROCESSING OF INCOMING BYTES - CORE AND DETECTION
57 ;----------------------------------------
60 ; State Reading Reading-Wait
63 ; See detect.asm head comment for protocol info, in particular
64 ; the meaning of these bytes.
66 mov_wf b ; W = b = received byte
68 mov_lfsr slavetable, 1
69 mov_ff cslot, FSR1L ; FSR1 -> slave's flags
73 ; this is a first (head) byte:
75 bt_f_if0 POSTINC1, stf_detect ; FSR1 -> detbasel
76 bra read_got_first_reversers
77 read_got_first_detectors ; b = MdBBdddd
78 and_lw 0xb0 ; W = M0BB0000
79 mov_wf cbyte ; cbyte = M0BB0000
80 mov_lw 0x4f ; W = 01001111
81 and_wff b ; b = 0d00dddd
82 mov_fw POSTINC1 ; W = detbasel; FSR1 -> lastd0
83 rcall read_got_detectbyte_prep ; b = 0d00dddd
85 bt_f_if1 b, 6 ; b bit .d......
86 bs_f b, 4 ; b = 0d0ddddd
87 ; ^ ^ copies of same bit
88 ; u = (still) 0C00CCCC
89 ; or, using detection segment bit numbers:
95 read_got_first_reversers
97 bra read_got_bad_first_reversers
98 and_lw 0x80 ; W = M0000000
99 mov_wf cbyte ; cbyte = M0000000
100 bc_f b, 7 ; b = 00dddddd
101 mov_fw POSTINC1 ; W = detbasel; FSR1 -> lastd0
102 rcall read_got_detectbyte_prep
106 read_got_bad_first_reversers panic morse_MR
109 read_got_detectors_b1 ; b = dddddddd
110 bc_f cbyte, 4 ; cbyte = M0B00000
111 mov_fw POSTINC1 ; W = detbasel; FSR1 -> lastd0
112 inc_f FSR1L ; FSR1 -> lastd1
113 add_lw 5 ; W = detbasel+8
114 rcall read_got_detectbyte_prep
118 read_got_detectors_b2 ; b = dddddddd
119 bc_f cbyte,5 ; cbyte = M0000000
120 mov_fw POSTDEC1 ; W = detbasel; FSR1 -> flags
121 bs_f FSR1L, 2 ; FSR1L -> lastd2
122 add_lw 13 ; W = detbasel+16
123 rcall read_got_detectbyte_prep
127 read_got_detectbyte_prep_ifsomething
129 ; This is a branch of read_got_detectbyte_prep, called if we're doing
130 ; `return' rather than `pop+return'. For conditions on return, see
131 ; read_got_detectbyte_prep; these are supposed to be (basically) the
132 ; same as the entry conditions for addmsgs_<kind>.
143 xor_wfw INDF1 ; lastd<n> = [d0]*
145 ior_wff FSR1L ; FSR1L -> detmsgh
146 return ; to addmsgs_<something>, very shortly
148 ;----------------------------------------
151 bra read_got_detectors_b1
153 bra read_got_detectors_b2
154 ; it must be an extra byte
156 bt_f_if0 b, 7 ; any more ?
159 call process_got_extra
160 bra i2c_arrange_next_byte
163 read_got_detectbyte_prep
165 ; Sees if anything has changed. If no changes pops one return address
166 ; and branches to i2c_arrange_next_byte; if some changes, returns to
169 ; So, caller should be i2cmu_read_got_byte, and next stuff
170 ; should basically be a call to addmsg_<something> (perhaps preceded
171 ; by a bit of fiddling of b). addmsg_<something> will finish
172 ; by branching to i2c_arrange_next_byte.
174 ; call return pop+return
175 ; W adjdetbasel preserved undefined
176 ; b [d0]* preserved preserved
177 ; FSR1 -> lastd<n> detmsgh preserved
178 ; cbyte set for next read etc. preserved preserved
179 ; u undefined [C0]* preserved
180 ; lastd<n> [o0]* [d0]* preserved = [d0]*
182 ; TOS -> (optionally, fiddle b, and then:) goto addmsgs_<something>
183 ; NOS return address for i2cmu_read_got_byte
185 mov_wf t ; t = adjdetbasel
187 xor_wfw INDF1 ; W = [C0]*, Z iff same
188 ; where C set iff change to that detection segment
189 bra_nz read_got_detectbyte_prep_ifsomething
190 ; there's nothing to do
193 ;----------------------------------------
194 i2c_arrange_next_byte
196 goto i2cm_read_another
198 ;======================================================================
199 ; DECIDING WHICH SLAVE TO ADDRESS
201 i2c_arrange_something
202 ; figure out what to do next - which pic to address, etc.
203 bs_f cbyte, cbyte_halted
205 tst_f_ifnz wslave ; anyone asked to write ?
208 ; no writing needed, we consider reading:
209 bt_f_if1 flags, flags_polarising
210 return ; do not scan while booster PWM is off while polarising
211 ; to avoid all trains disappearing and reappearing
215 and_lw outbuf_size - 1
216 cmp_fw_ifle outmsg_targetlen
217 return ; target len < actual len, do not add anything
218 ; ok, there's space, go ahead:
219 bc_f cbyte, cbyte_halted
222 add_wff cslot ; cslot -> next ste_flags
223 mov_lfsr slavetable, 1 ; FSR1H -> slavetable
224 mov_ff cslot, FSR1L ; FSR1 -> new ste_flags
226 bt_f_if1 POSTDEC1, stf_sentinel ; FSR1 -> ste_slave
227 bra nextslave_looparound
228 ; Ok, we have a slave:
230 mov_fw INDF1 ; W = new slave number
234 ; now we do our own detection
235 mov_lw (slavetable + ste_flags) & 0xff ; select our own slot
237 call read_detection_head_master
238 goto i2cmu_read_got_byte
240 ;----------------------------------------
242 ; So we've done one write.
243 bs_f cbyte, cbyte_halted
245 tst_f_ifnz wslave ; anyone asked in the meantime ?
248 ; Anyone else to write to ?
249 ; add calls to needwrite_<foo> here:
250 call polarity_needwrite
251 call points_needwrite
252 ; end of list of calls to needwrite_<foo>
254 ; no, if we're here, no-one wants to write
255 bra nextslave_nowrite
257 ;----------------------------------------
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
264 bt_f_if0 cbyte, cbyte_halted
265 return ; we're currently doing something
269 ; wslave slave to write to
270 bc_f cbyte, cbyte_halted
274 goto i2cm_write_start
276 ;----------------------------------------
277 i2c_consider_restartread
278 bt_f_if0 cbyte, cbyte_halted
280 bra nextslave_nowrite
282 ;======================================================================
286 mov_lw (slavetable + ste_flags + ste_size) & 0xff
287 ; start with 1st actual slave
290 bs_f cbyte, cbyte_halted ; serial output of `hello' will start us up
294 mov_lfsr slavetable, 0 ; FSR0 -> slavetable
295 load_tblptr pic2detinfo ; TBLPTR* -> pic2detinfo
296 clr_f t ; t = loop counter
298 tblrd_postinc_fixup ; TABLAT = EOOOOSSS
299 mov_fw TABLAT ; W = EOOOOSSS, N = E
301 tblrd_postinc_fixup ; TABLAT = DSSSSSSS, N (still) = E
302 bra_nn mascan_init_ifabsent
304 mov_ff t, POSTINC0 ; ste_slave = slave
305 mov_wf u ; u = 1OOOOSSS
307 bra_nz mascan_bad_detinfo0
310 mov_fw TABLAT ; W = DSSSSSSS
311 and_lw 0x80 ; W = D0000000
312 mov_wf POSTINC0 ; ste_flags = D0000000
314 mov_fw TABLAT ; W = DSSSSSSS
315 bc_w 7 ; W = 0SSSSSSS = first
316 add_lw -0xf8 ; W = first - 0xf8 = detbasel
317 mov_wf POSTINC0 ; detbasel
319 clr_f POSTINC0 ; lastd0
320 clr_f POSTINC0 ; lastd1
321 clr_f POSTINC0 ; lastd2
322 set_f POSTINC0 ; unused
324 mov_fw u ; W = 10000SSS
325 xor_lw b'10011000' ^ 0x80 ; W = detmsgh
326 mov_wf POSTINC0 ; detmsgh
330 bt_f_if0 t, maxpics_ln2
332 ; we've read the whole flash table
334 if slavetable == 0x400
336 bra mascan_bad_toomany
339 clr_f POSTINC0 ; ste_slave
340 mov_lw (1<<stf_detect)|(1<<stf_sentinel)
341 mov_wf POSTINC0 ; ste_flags
342 ; rest of final entry, and rest of table, is undefined
344 ; at 9600, it's about 1ms per char.
345 ; we allow 1ms to scan 2 pics via i2c.
346 ; so our target len is (no of pics)/2
347 ; plus 2 bytes of slop
350 mov_wf outmsg_targetlen
354 mascan_bad_detinfo0 panic morse_DF
355 mascan_bad_toomany panic morse_DG
357 ;======================================================================
358 ; PROCESSING OF INCOMING BYTES - EXTRA (NON-DETECTION)
361 ;----------------------------------------
373 ;======================================================================
374 ; GENERATION OF DETECTION MESSAGES FOR HOST - MAD BT_F_IF1 TABLES
376 addmsg_testbit macro bit
381 addmsg_return macro dummy_bit
382 goto i2c_arrange_next_byte
385 addmsg_ignore macro dummy_bit
390 addmsg_padding macro dummy_bit
395 ;----------------------------------------
398 ; on entry after first addmsg_one, or when done
399 ; W, STATUS, v, FSR0 undefined trashed
400 ; t adjdetbasel not modified by addmsgs_<kind> or _one
401 ; u [C0]* not modified by addmsgs_<kind> or _one
402 ; b [d0]* not modified by addmsgs_<kind> or _one
403 ; lastd<n> [d0]* (new) not modified by addmsgs_<kind> or _one
404 ; FSR1 -> detmsgh not modified by addmsgs_<kind> or _one
405 ; outbuf, outmsg_* not full, updated appropriately
406 ; all others any not interfered with
408 ; (this is all set up by read_prep_detectbyte and
409 ; read_got_detectbyte_prep_ifsomething)
410 ; when done, branches to i2c_arrange_next_byte, rather than returning
412 addmsgs_section code ( 7 )*4 + 0x2100
415 ; |- PCL bbb value after macro - number in this
416 ; V column should increment 1 each line
418 addmsgs_revhead addmsg_testbit 0
426 addmsgs_all addmsg_testbit 7
436 addmsgs_dethead addmsg_testbit 0
440 addmsg_testbit 6 ; bit 6 was copied to 4 but only in b, not u
445 ; TOS - 4 -> bt_f_if1 w, b'bbb'
447 ; other conditions on entry and exit as for entry to addmsgs_<kind>, above
448 rr_fw TOSL ; W = 0???bbb0
450 ior_wfw 0xf8 ; W = 11111bbb
451 mov_wf FSR0L ; FSR0L = 11111bbb
452 clr_f FSR0H ; FSR0 -> bitnum2bit[bbb]
453 add_wfw t ; W = adjdetbasel + 11111bbb
454 ; ie = 0 SSSSSSS (det msg low byte)
455 mov_wf v ; v = 0 SSSSSSS (det msg low byte)
458 and_wfw INDF0 ; train: W = 0x00, Z=1; none: W = 0*d0*, Z=0
460 mov_lw 0x08 ; train: W = 0 000 0 000; none: W = 0 000 1 000
461 xor_wfw INDF1 ; W = 1 001 Y SSS (det msg high byte)
464 mov_fw v ; W = 0 SSSSSSS (det msg low byte)
465 goto serial_addbyte_another
467 ;======================================================================