chiark / gitweb /
remove some xdebugs
[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         call    tick_intrl
92         call    nmra_serialrx_intrl
93         call    power_fault_intrl
94         call    serialtxfc_intrl
95         call    serialtx_intrl
96         call    points_local_intrl
97         call    i2cm_intrl
98         panic   morse_IL
99
100 ;----------------------------------------------------------------------
101 serialrx_table_section  code    0x2000
102
103 ;--------------------
104 serialrx_generalmsg @
105 ;command_<something>  has same calling convention:
106 ;
107 ;  FSR0 ->      start of message                undefined
108 ;  *FSR0        message                         undefined
109 ;
110         mov_fw  INDF0
111         bra_n   serialrx_if_multibyte
112         bra_z   command_crashed
113
114         xor_lw  0x21
115         bra_z   command_power_on
116         xor_lw  0x01
117         bra_z   command_power_off
118         bra     serialrx_bad
119
120 ;-----
121 serialrx_if_multibyte
122         rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
123         and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
124         bt_f_if0 PCL, 0 ; always true, but reads PCL
125         add_wff PCL
126         ; <--- here is zero
127
128         goto    serialrx_bad            ; 1 0000 xxx
129         goto    command_ping            ; 1 0001 xxx
130         goto    command_polarity        ; 1 0010 xxx
131         goto    serialrx_bad            ; 1 0011 xxx
132         goto    command_point           ; 1 0100 xxx
133         goto    serialrx_bad            ; 1 0101 xxx
134         goto    serialrx_bad            ; 1 0110 xxx
135         goto    serialrx_bad            ; 1 0111 xxx
136         goto    serialrx_bad            ; 1 1000 xxx
137         goto    serialrx_bad            ; 1 1001 xxx
138         goto    serialrx_bad            ; 1 1010 xxx
139         goto    serialrx_bad            ; 1 1011 xxx
140         goto    serialrx_bad            ; 1 1100 xxx
141         goto    serialrx_bad            ; 1 1101 xxx
142         goto    serialrx_bad            ; 1 1110 xxx
143         goto    serialrx_bad            ; 1 1111 xxx
144
145 ;-----
146 serialrx_bad
147         mov_ff  INDF0, t
148         panic   morse_HX
149
150 code2 code
151 ;--------------------
152 command_ping
153         mov_fw  POSTINC0
154         mov_ff  INDF0, t
155         call    serial_addbyte
156         mov_fw  t
157         bra_n   command_ping_bad
158         goto    serial_addbyte_another
159
160 command_ping_bad panic morse_HP
161
162 ;======================================================================
163 ; SLAVE
164
165 ;----------------------------------------
166 slave_init
167 ; Slave-specific initialisation.
168         mov_fw  picno
169         call    i2cs_init
170
171         bc_f    T0CON, TMR0ON
172
173         call    common_local_init
174         call    detect_slave_init
175         return
176
177 ;----------------------------------------
178 slave_interrupt_low @
179         enter_interrupt_low
180         call    points_local_intrl
181         panic   morse_IL
182
183 ;----------------------------------------
184 i2csu_write_begin
185         return
186
187 near_local_do code
188 ;----------------------------------------
189 i2csu_write_data
190         call    led_green
191
192         bt_w_if0 7
193         bra     i2csu_write_if_special
194         bt_w_if1 6
195         bra     polarity_local_do
196         bt_w_if0 5
197         bra     point_local_do
198 i2csu_write_if_bad
199         mov_wf  t
200         panic   morse_DX
201
202 i2csu_write_if_special
203         tst_w_ifnz
204         bra     i2csu_write_if_bad
205         goto    panic_crashread_commanded
206
207 code3 code
208 ;======================================================================
209 ; MASTER/SLAVE deviations
210
211 ;----------
212 message_for_master @
213 ; Either transmits the message to the master, or if we are the master,
214 ; handles it as an incoming message from the notional `slave 0'.
215 ;  W            message         unchanged
216 ;  GIEH         set             set
217         bt_f_if0 idloc1,idloc1_master
218         goto    slave_add_short_message
219         goto    loopback_read_byte
220
221 ;======================================================================
222   include final.inc