chiark / gitweb /
some use of xdebug to help spurious interrupt chasing
[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         clr_f   flags
30
31         bt_f_if0 idloc1,idloc1_master
32         call    slave_init
33
34         bt_f_if1 idloc1,idloc1_master
35         call    master_init
36
37         bs_f    INTCON, GIEH
38         bs_f    INTCON, GIEL
39
40         bt_f_if1 idloc1,idloc1_master
41         goto    backgroundloop_master
42         bt_f_if1 idloc1,idloc1_boarddet
43         goto    backgroundloop_detectors
44         goto    backgroundloop_reversers
45
46 ;----------
47 common_local_init
48 ; called by {master,slave}_init immediately after they've init'd i2c.
49         clr_f   PCLATU
50         call    bitnum2bit_init
51
52         call    points_local_init
53         call    polarity_local_init
54         call    detect_local_init
55         return
56
57 ;----------------------------------------
58 panic_kill_hook
59         call    power_panichook
60         call    cdu_panichook
61         return
62
63 ;----------
64 intrl_handled_routine
65         mov_ff  isr_low_save_stkptr, STKPTR
66         intrl_handled_core
67
68 ;======================================================================
69 ; MASTER
70
71 ;----------------------------------------
72 master_init
73 ; Master-specific initialisation.
74         call    serial_init
75         call    i2cm_init
76         call    serialtxbuf_init
77         call    polarity_master_init
78         call    points_master_init
79         call    common_local_init
80         call    nmra_init
81         call    serialtxfc_init
82         call    mascan_init
83         call    tick_init
84         call    power_polarising_init
85         call    power_fault_init
86         return
87
88 ;----------------------------------------
89 master_interrupt_low
90         enter_interrupt_low
91  mov_ff INTCON, xdebug+0
92  mov_ff INTCON2, xdebug+1
93  mov_ff INTCON3, xdebug+2
94  mov_ff PIR1, xdebug+3
95  mov_ff PIR2, xdebug+4
96  mov_ff PIR3, xdebug+5
97         call    tick_intrl
98         call    nmra_serialrx_intrl
99         call    power_fault_intrl
100         call    serialtxfc_intrl
101         call    serialtx_intrl
102         call    points_local_intrl
103         call    i2cm_intrl
104         panic   morse_IL
105
106 ;----------------------------------------------------------------------
107 serialrx_table_section  code    0x2000
108
109 ;--------------------
110 serialrx_generalmsg
111 ;command_<something>  has same calling convention:
112 ;
113 ;  FSR0 ->      start of message                undefined
114 ;  *FSR0        message                         undefined
115 ;
116 ;                               ; INDF0=MM ww ww ww ww ii ii ii
117         rlc_fw  INDF0           ; W =   ww ww ww ww ii ii ii ??  C=MM Z=00
118         bra_c   serialrx_if_multibyte
119         bra_z   command_crashed
120         ; single-byte non-0 command, has to be ON or OFF
121         ;       ie supposedly   ; INDF0=zz zz II zz zz zz zz PP  C=zz
122         rrc_fw  INDF0           ; W =   zz zz zz II zz zz zz zz  C=PP
123         xor_lw  0x10            ; W =   zz zz zz zz zz zz zz zz  C=PP Z=OK
124         bra_n   serialrx_bad
125         goto    command_power
126
127 ;-----
128 serialrx_if_multibyte
129         rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
130         and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
131         bt_f_if0 PCL, 0 ; always true, but reads PCL
132         add_wff PCL
133         ; <--- here is zero
134
135         goto    serialrx_bad            ; 1 0000 xxx
136         goto    command_ping            ; 1 0001 xxx
137         goto    command_polarity        ; 1 0010 xxx
138         goto    serialrx_bad            ; 1 0011 xxx
139         goto    command_point           ; 1 0100 xxx
140         goto    serialrx_bad            ; 1 0101 xxx
141         goto    serialrx_bad            ; 1 0110 xxx
142         goto    serialrx_bad            ; 1 0111 xxx
143         goto    serialrx_bad            ; 1 1000 xxx
144         goto    serialrx_bad            ; 1 1001 xxx
145         goto    serialrx_bad            ; 1 1010 xxx
146         goto    serialrx_bad            ; 1 1011 xxx
147         goto    serialrx_bad            ; 1 1100 xxx
148         goto    serialrx_bad            ; 1 1101 xxx
149         goto    serialrx_bad            ; 1 1110 xxx
150         goto    serialrx_bad            ; 1 1111 xxx
151
152         goto    serialrx_bad    ; 0 0000 xxx
153         goto    serialrx_bad    ; 0 0001 xxx
154         goto    serialrx_bad    ; 0 0000 xxx
155
156 ;-----
157 serialrx_bad
158         mov_ff  INDF0, t
159         panic   morse_HX
160
161 ;--------------------
162 command_ping
163         mov_fw  POSTINC0
164         mov_ff  INDF0, t
165         call    serial_addbyte
166         mov_fw  t
167         bra_n   command_ping_bad
168         goto    serial_addbyte_another
169
170 command_ping_bad panic morse_HP
171
172 code2 code
173 ;======================================================================
174 ; SLAVE
175
176 ;----------------------------------------
177 slave_init
178 ; Slave-specific initialisation.
179         mov_fw  picno
180         call    i2cs_init
181
182         bc_f    T0CON, TMR0ON
183
184         call    common_local_init
185         call    detect_slave_init
186         return
187
188 ;----------------------------------------
189 slave_interrupt_low
190         enter_interrupt_low
191         call    points_local_intrl
192         panic   morse_IL
193
194 ;----------------------------------------
195 i2csu_write_begin
196         return
197
198 near_local_do code
199 ;----------------------------------------
200 i2csu_write_data
201         call    led_green
202
203         bt_w_if0 7
204         bra     i2csu_write_if_special
205         bt_w_if1 6
206         bra     polarity_local_do
207         bt_w_if0 5
208         bra     point_local_do
209 i2csu_write_if_bad
210         mov_wf  t
211         panic   morse_DX
212
213 i2csu_write_if_special
214         tst_w_ifnz
215         bra     i2csu_write_if_bad
216         goto    panic_crashread_commanded
217
218 code3 code
219 ;======================================================================
220 ; MASTER/SLAVE deviations
221
222 ;----------
223 message_for_master
224 ; Either transmits the message to the master, or if we are the master,
225 ; handles it as an incoming message from the notional `slave 0'.
226 ;  W            message         unchanged
227 ;  GIEH         set             set
228         bt_f_if0 idloc1,idloc1_master
229         goto    slave_add_short_message
230         goto    loopback_read_byte
231
232 ;======================================================================
233   include final.inc