chiark / gitweb /
i2cmu_done and i2c_arrange_something initial implementation
[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
19 ;======================================================================
20 ; HANDLING OF I2C EVENTS
21
22 near_getwritebyteyes code
23 ;----------------------------------------
24 getwritebyte_yes
25         pop             ; we don't care where we were in i2cmu_write_next_byte
26         bc_f    STATUS, Z       ; yes, we want to write this byte
27         return          ; from i2cmu_write_next_byte
28         
29  code
30 ;----------------------------------------
31 i2cmu_write_next_byte
32         ; add calls to getwritebyte_<foo> here:
33         call    getwritebyte_polarity
34         call    getwritebyte_points
35         ; end of list of calls to getwritebyte_<foo>
36         ; so, no-one wants this slave:
37         bs_f    STATUS, Z
38         return
39
40         bra_nz  write_another_slave
41
42         
43
44         return
45
46 ;----------------------------------------
47 i2cm_intrl
48 ; handles i2c interrupt using i2cm_interrupt[_definite],
49 ; according to the rules for <something>_intrl.
50         bt_f_if0 PIR1, SSPIF
51         return
52         call    i2cm_interrupt_definite
53         intrl_handled_nostack
54
55 ;======================================================================
56 ; PROCESSING OF INCOMING BYTES - CORE AND DETECTION
57
58 ;----------------------------------------
59 i2cmu_read_got_byte
60 ;               Beforehand      At call
61 ;   State       Reading         Reading-Wait
62 ;   W                           data from slave
63 ;
64 ; See detect.asm head comment for protocol info, in particular
65 ; the meaning of these bytes.
66                                 ; W =           received byte
67         mov_wf  b               ; W = b =       received byte
68
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         mov_fw  needwrite_slave
206         bra_nz  arrange_write
207
208         ; add calls to needwrite_<foo> here:
209         call    needwrite_polarity
210         call    needwrite_points
211         ; end of list of calls to needwrite_<foo>
212
213         ; no writing needed, we have to read:
214         mov_lfsr slavetable, 1  ; FSR1H -> slavetable
215         mov_ff  cslot, FSR1L    ; FSR1 -> current ste_flags
216         mov_lw  ste_size        ; W = ste_size
217 nextslave_loop
218         add_wff FSR1L           ; FSR1 -> next ste_flags
219         bt_f_if0 INDF1, stf_present
220         bra     nextslave_loop
221
222 nextslave_reloop_resume
223         mov_ff  FSR1L, cslot    ; cslot -> new ste_flags
224         bt_f_if1 POSTDEC1, stf_sentinel ; FSR1 -> ste_slave
225         bra     nextslave_reloop
226
227         mov_fw  INDF1           ; W = new slave number
228         goto    i2cm_read_start
229
230 nextslave_reloop
231         mov_lfsr slavetable + ste_size + ste_flags
232         bra     nextslave_reloop_resume
233
234 fixme do not i2cm_read_start if no room in buffer
235 fixme check that we don't accidentally skip reading a slave
236
237 ;======================================================================
238 ; PROCESSING OF INCOMING BYTES - EXTRA (NON-DETECTION)
239
240 near_gots code
241 ;----------------------------------------
242 process_got_extra
243         mov_fw  b
244 loopback_read_byte
245 ;...
246 ;  W            message
247         xor_lw  0x00 ^ 0x20
248         bra_z   got_pointed
249         xor_lw  0x20 ^ 0xb0
250         bra_z   got_aargh
251         panic   morse_MX
252
253 ;======================================================================
254 ; GENERATION OF DETECTION MESSAGES FOR HOST - MAD BT_F_IF1 TABLES
255
256 addmsg_testbit macro bit
257         bt_f_if1 u, bit
258         rcall   addmsg_one
259         endm
260
261 addmsg_return macro dummy_bit
262         goto    i2c_arrange_next_byte
263         endm
264
265 addmsg_ignore macro dummy_bit
266         nop
267         nop
268         endm
269
270 addmsg_padding macro dummy_bit
271         nop
272         mov_lw  dummy_bit
273         endm
274
275 ;----------------------------------------
276 ;addmsgs_<kind>
277 ;
278 ;                       on entry        after first addmsg_one, or when done
279 ;  W, STATUS, v, FSR0   undefined       trashed
280 ;  t                    adjdetbasel     not modified by addmsgs_<kind> or _one
281 ;  u                    [C0]*           not modified by addmsgs_<kind> or _one
282 ;  b                    [d0]*           not modified by addmsgs_<kind> or _one
283 ;  lastd<n>             [d0]* (new)     not modified by addmsgs_<kind> or _one
284 ;  FSR1 ->              detmsgh         not modified by addmsgs_<kind> or _one
285 ;  outbuf, outmsg_*     not full, updated appropriately
286 ;  all others           any             not interfered with
287 ;  
288 ; (this is all set up by read_prep_detectbyte and
289 ;  read_got_detectbyte_prep_ifsomething)
290 ; when done, branches to i2c_arrange_next_byte, rather than returning
291
292 addmsgs_section code (          7       )*4 + 0x2100
293 ;
294 ;                               A
295 ;                               |- PCL bbb value after macro - number in this
296 ;                               V       column should increment 1 each line
297 ;
298 addmsgs_revhead addmsg_testbit  0
299                 addmsg_testbit  1
300                 addmsg_testbit  2
301                 addmsg_testbit  3
302                 addmsg_testbit  4
303                 addmsg_testbit  5
304                 addmsg_return   6
305
306 addmsgs_all     addmsg_testbit  7
307                 addmsg_testbit  0
308                 addmsg_testbit  1
309                 addmsg_testbit  2
310                 addmsg_testbit  3
311                 addmsg_testbit  4
312                 addmsg_testbit  5
313                 addmsg_testbit  6
314                 addmsg_return   7
315
316 addmsgs_dethead addmsg_testbit  0
317                 addmsg_testbit  1
318                 addmsg_testbit  2
319                 addmsg_testbit  3
320                 addmsg_testbit  6 ; bit 6 was copied to 4 but only in b, not u
321                 addmsg_return   5
322
323 ;----------
324 addmsg_one
325 ;  TOS - 4 ->   bt_f_if1 w, b'bbb'
326 ;  TOSL         ???bbb00
327 ; other conditions on entry and exit as for entry to addmsgs_<kind>, above
328         rr_fw   TOSL            ; W =           0???bbb0
329         rr_w                    ; W =           00???bbb
330         ior_wfw 0xf8            ; W =           11111bbb
331         mov_wf  FSR0L           ; FSR0L =       11111bbb
332         clr_f   FSR0H           ; FSR0 ->       bitnum2bit[bbb]
333         add_wfw t               ; W =           adjdetbasel + 11111bbb
334                                 ;  ie =         0 SSSSSSS (det msg low byte)
335         mov_wf  v               ; v =           0 SSSSSSS (det msg low byte)
336
337         mov_fw  b               ; W =           [d0]*
338         and_wfw INDF0           ; train: W = 0x00, Z=1; none: W = 0*d0*, Z=0
339         bt_f_if0 STATUS, Z
340         mov_lw  0x08            ; train: W = 0 000 0 000; none: W = 0 000 1 000
341         xor_wfw INDF1           ; W =           1 001 Y SSS (det msg high byte)
342         call    serial_addbyte
343
344         mov_fw  v               ; W =           0 SSSSSSS (det msg low byte)
345         goto    serial_addbyte_another
346
347 ;======================================================================
348  include final.inc