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