chiark / gitweb /
fixes from nye debugging
[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  set_f xdebug+7
63  clr_f xdebug+6
64         call    serial_init
65         call    i2cm_init
66         call    serialtxbuf_init
67         call    polarity_master_init
68         call    points_master_init
69         call    common_local_init
70         call    nmra_init
71         call    serialtxfc_init
72         call    mascan_init
73         call    tick_init
74         call    cdu_init
75         call    power_polarising_init
76         call    power_fault_init
77         call    watchdog_init
78  clr_f xdebug+0
79         return
80
81 boom3
82  panic morse_TI3
83
84 ;----------------------------------------
85 master_interrupt_low @
86         enter_interrupt_low
87  D 0x80
88  mov_lfsr 0x5f+2, 0
89  mov_fw INDF0
90  xor_lw 0x40
91  bra_nz boom3
92         call    tick_intrl
93  D 0x01
94         call    nmra_serialrx_intrl
95  D 0x02
96         call    power_fault_intrl
97  D 0x03
98         call    serialtxfc_intrl
99  D 0x04
100         call    serialtx_intrl
101  D 0x05
102         call    points_local_intrl
103  D 0x06
104         call    i2cm_intrl
105  bs_f   xdebug+0, 5
106         panic   morse_IL
107
108 ;----------------------------------------------------------------------
109 serialrx_table_section  code    0x2000
110
111 ;--------------------
112 serialrx_generalmsg @
113 ;command_<something>  has same calling convention:
114 ;
115 ;  FSR0 ->      start of message                undefined
116 ;  *FSR0        message                         undefined
117 ;
118         mov_fw  INDF0
119         bra_n   serialrx_if_multibyte
120         bra_z   command_crashed
121
122         xor_lw  0x21
123         bra_z   command_power_on
124         xor_lw  0x01
125         bra_z   command_power_off
126         bra     serialrx_bad
127
128 ;-----
129 serialrx_if_multibyte
130         rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
131         and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
132         bt_f_if0 PCL, 0 ; always true, but reads PCL
133         add_wff PCL
134         ; <--- here is zero
135
136         goto    serialrx_bad            ; 1 0000 xxx
137         goto    command_ping            ; 1 0001 xxx
138         goto    command_polarity        ; 1 0010 xxx
139         goto    command_watchdog        ; 1 0011 xxx
140         goto    command_point           ; 1 0100 xxx
141         goto    serialrx_bad            ; 1 0101 xxx
142         goto    serialrx_bad            ; 1 0110 xxx
143         goto    serialrx_bad            ; 1 0111 xxx
144         goto    serialrx_bad            ; 1 1000 xxx
145         goto    serialrx_bad            ; 1 1001 xxx
146         goto    serialrx_bad            ; 1 1010 xxx
147         goto    serialrx_bad            ; 1 1011 xxx
148         goto    serialrx_bad            ; 1 1100 xxx
149         goto    serialrx_bad            ; 1 1101 xxx
150         goto    serialrx_bad            ; 1 1110 xxx
151         goto    serialrx_bad            ; 1 1111 xxx
152
153 ;-----
154 serialrx_bad
155         mov_ff  INDF0, t
156         panic   morse_HX
157
158 code2 code
159 ;--------------------
160 command_ping
161         mov_fw  POSTINC0
162         mov_ff  INDF0, t
163         call    serial_addbyte
164         mov_fw  t
165         bra_n   command_ping_bad
166  mov_wf xdebug+6
167         goto    serial_addbyte_another
168
169 command_ping_bad panic morse_HP
170
171 ;======================================================================
172 ; SLAVE
173
174 ;----------------------------------------
175 slave_init
176 ; Slave-specific initialisation.
177         clr_f   flags
178
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