chiark / gitweb /
serialtxbuf_init
[trains.git] / detpic / program.asm
1 ;======================================================================
2 ; MAIN PROGRAM AND GLUE
3
4  include common.inc
5  code
6
7 ;======================================================================
8 ; COMMON TO MASTER AND SLAVE
9
10 ;----------------------------------------
11 vector_reset
12
13  set_f qqTRISA
14  set_f qqTRISB
15  set_f qqTRISC
16  set_f qqTRISD
17  set_f qqTRISE
18  mov_lw 0xaa
19  mov_wf qqLATA
20  mov_wf qqLATB
21  mov_wf qqLATC
22  mov_wf qqLATD
23  mov_wf qqLATE
24
25         clr_f   INTCON
26         bs_f    RCON, IPEN      ; interrupt priorities
27
28         call    idlocs_init
29
30         bt_f_if0 idloc1,idloc1_master
31         call    slave_init
32
33         bt_f_if1 idloc1,idloc1_master
34         call    master_init
35
36         bs_f    INTCON, GIEH
37         bs_f    INTCON, GIEL
38
39         bt_f_if1 idloc1,idloc1_boarddet
40         goto    backgroundloop_detectors
41         goto    backgroundloop_reversers
42
43 ;----------
44 common_local_init
45 ; called by {master,slave}_init immediately after they've init'd i2c.
46         clr_f   PCLATU
47         call    bitnum2bit_init
48
49         call    points_local_init
50         call    polarity_local_init
51         call    detect_local_init
52         return
53
54 ;----------------------------------------
55 panic_kill_hook
56
57 ;----------
58 intrl_handled_routine
59         mov_ff  isr_low_save_stkptr, STKPTR
60         intrl_handled_core
61
62 ;======================================================================
63 ; MASTER
64
65 ;----------------------------------------
66 master_init
67 ; Master-specific initialisation.
68         call    i2cm_init
69         call    serialtxbuf_init
70         call    polarity_master_init
71         call    common_local_init
72         call    serial_init
73         call    nmra_init
74         call    serialtxfc_init
75         call    mascan_init
76         return
77
78 ;----------------------------------------
79 master_interrupt_low
80         enter_interrupt_low
81         call    ticker_intrl
82         call    nmra_serialrx_intrl
83         call    serialtx_intrl
84         call    points_local_intrl
85         call    i2cm_intrl
86         panic   morse_IL
87
88 ;----------
89 master_interrupt_high_notnmra
90         panic   morse_IH
91
92 ;----------------------------------------------------------------------
93 serialrx_table_section  code    0x2000
94
95 ;--------------------
96 serialrx_generalmsg
97 ;command_<something>  has same calling convention:
98 ;
99 ;  FSR0 ->      start of message                undefined
100 ;  *FSR0        message                         undefined
101 ;
102 ;                               ; INDF0=MM ww ww ww ww ii ii ii
103         rlc_fw  INDF0           ; W =   ww ww ww ww ii ii ii ??  C=MM Z=00
104         bra_c   serialrx_if_multibyte
105         bra_z   command_crashed
106         ; single-byte non-0 command, has to be ON or OFF
107         ;       ie supposedly   ; INDF0=zz zz II zz zz zz zz PP  C=zz
108         rrc_fw  INDF0           ; W =   zz zz zz II zz zz zz zz  C=PP
109         xor_lw  0x10            ; W =   zz zz zz zz zz zz zz zz  C=PP Z=OK
110         bra_n   serialrx_bad
111         goto    command_power
112
113 ;-----
114 serialrx_if_multibyte
115         rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
116         and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
117         bt_f_if0 PCL, 0 ; always true, but reads PCL
118         add_wff PCL
119         ; <--- here is zero
120
121         goto    serialrx_bad            ; 1 0000 xxx
122         goto    command_ping            ; 1 0001 xxx
123         goto    command_polarity        ; 1 0010 xxx
124         goto    serialrx_bad            ; 1 0011 xxx
125         goto    command_point           ; 1 0100 xxx
126         goto    serialrx_bad            ; 1 0101 xxx
127         goto    serialrx_bad            ; 1 0110 xxx
128         goto    serialrx_bad            ; 1 0111 xxx
129         goto    serialrx_bad            ; 1 1000 xxx
130         goto    serialrx_bad            ; 1 1001 xxx
131         goto    serialrx_bad            ; 1 1010 xxx
132         goto    serialrx_bad            ; 1 1011 xxx
133         goto    serialrx_bad            ; 1 1100 xxx
134         goto    serialrx_bad            ; 1 1101 xxx
135         goto    serialrx_bad            ; 1 1110 xxx
136         goto    serialrx_bad            ; 1 1111 xxx
137
138         goto    serialrx_bad    ; 0 0000 xxx
139         goto    serialrx_bad    ; 0 0001 xxx
140         goto    serialrx_bad    ; 0 0000 xxx
141
142 ;-----
143 serialrx_bad
144         mov_ff  INDF0, t
145         panic   morse_HX
146
147 ;       bra_z   crashed_master_do
148 ;       mov_fw  INDF0
149 ;       bra_z   crashed_master_do
150
151 ;       serialrx_check 0x00, 0x21, command_power_on
152 ;       serialrx_check 0x21, 0x20, command_power_off
153
154 ;       and_lw  0xf8            ; W^0x20 =      MM ww ww ww  ww zz zz zz
155
156 ;       serialrx_check 0x00, 0xa0, point_master_do
157 ;       serialrx_check 0xa0, 0x88, ping_master_do
158 ;       serialrx_check 0x88, 0x90, polarity_master_do
159
160 ;       serialrx_check 0x90, 0x90, polarity_master_do
161
162 ;       mov_wf  t               ; t =           MM ww ww ww  ww zz zz zz
163
164 ;       xor_lw  0x
165
166 ;       and_lw
167
168 ;--------------------
169 command_ping
170 command_point
171 command_crashed
172         panic   morse_UC
173
174 ;======================================================================
175 ; SLAVE
176
177 ;----------------------------------------
178 slave_init
179 ; Slave-specific initialisation.
180         mov_fw  picno
181         call    i2cs_init
182
183         bc_f    T0CON, TMR0ON
184
185         call    common_local_init
186         call    detect_slave_init
187         return
188
189 ;----------------------------------------
190 slave_interrupt_low
191         enter_interrupt_low
192         call    points_local_intrl
193         panic   morse_IL
194
195 ;----------------------------------------
196 i2csu_write_begin
197         return
198
199 near_local_do code
200 ;----------------------------------------
201 i2csu_write_data
202         call    led_green
203
204         bt_w_if0 7
205         bra     i2csu_write_if_special
206         bt_w_if1 6
207         bra     polarity_local_do
208         bt_w_if0 5
209         bra     point_local_do
210 i2csu_write_if_bad
211         mov_wf  t
212         panic   morse_DX
213
214 i2csu_write_if_special
215         tst_w_ifnz
216         bra     i2csu_write_if_bad
217         goto    panic_crashread_commanded
218
219 code2 code
220 ;======================================================================
221 ; MASTER/SLAVE deviations
222
223 ;----------
224 message_for_master
225 ; Either transmits the message to the master, or if we are the master,
226 ; handles it as an incoming message from the notional `slave 0'.
227 ;  W            message         unchanged
228 ;  GIEH         set             set
229         bt_f_if0 idloc1,idloc1_master
230         goto    slave_add_short_message
231         goto    loopback_read_byte
232
233 ;======================================================================
234  include final.inc