chiark / gitweb /
wip making main program compile
[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    common_local_init
70         call    serial_init
71         call    nmra_init
72         return
73
74 ;----------------------------------------
75 master_interrupt_low
76         enter_interrupt_low
77         ;call   ticker_intrl
78         call    nmra_serialrx_intrl
79         ;call   serialtx_intrl
80         ;call   points_local_intrl
81         ;call   i2cm_intrl
82         panic   morse_IL
83
84 ;----------
85 master_interrupt_high_notnmra
86         panic   morse_IH
87
88 ;----------------------------------------------------------------------
89 serialrx_table_section  code    0x2000
90
91 ;--------------------
92 serialrx_generalmsg
93 ;  FSR0 ->      start of message                undefined
94 ;  *FSR0        message                         undefined
95 ;
96 ;                               ; INDF0=MM ww ww ww ww ii ii ii
97         rlc_fw  INDF0           ; W =   ww ww ww ww ii ii ii ??  C=MM Z=00
98         bra_c   serialrx_if_multibyte
99         bra_z   command_crashed
100         ; single-byte non-0 command, has to be ON or OFF
101         ;       ie supposedly   ; INDF0=zz zz II zz zz zz zz PP  C=zz
102         rrc_fw  INDF0           ; W =   zz zz zz II zz zz zz zz  C=PP
103         xor_lw  0x10            ; W =   zz zz zz zz zz zz zz zz  C=PP Z=OK
104         bra_n   serialrx_bad
105         goto    command_power
106
107 ;-----
108 serialrx_if_multibyte
109         rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
110         and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
111         add_wff PCL
112         ; <--- here is zero
113
114         goto    serialrx_bad            ; 1 0000 xxx
115         goto    command_ping            ; 1 0001 xxx
116         goto    command_polarity        ; 1 0010 xxx
117         goto    serialrx_bad            ; 1 0011 xxx
118         goto    command_point           ; 1 0100 xxx
119         goto    serialrx_bad            ; 1 0101 xxx
120         goto    serialrx_bad            ; 1 0110 xxx
121         goto    serialrx_bad            ; 1 0111 xxx
122         goto    serialrx_bad            ; 1 1000 xxx
123         goto    serialrx_bad            ; 1 1001 xxx
124         goto    serialrx_bad            ; 1 1010 xxx
125         goto    serialrx_bad            ; 1 1011 xxx
126         goto    serialrx_bad            ; 1 1100 xxx
127         goto    serialrx_bad            ; 1 1101 xxx
128         goto    serialrx_bad            ; 1 1110 xxx
129         goto    serialrx_bad            ; 1 1111 xxx
130
131         goto    serialrx_bad    ; 0 0000 xxx
132         goto    serialrx_bad    ; 0 0001 xxx
133         goto    serialrx_bad    ; 0 0000 xxx
134
135 ;-----
136 serialrx_bad
137         mov_ff  INDF0, t
138         panic   morse_HX
139
140 ;       bra_z   crashed_master_do
141 ;       mov_fw  INDF0
142 ;       bra_z   crashed_master_do
143
144 ;       serialrx_check 0x00, 0x21, command_power_on
145 ;       serialrx_check 0x21, 0x20, command_power_off
146
147 ;       and_lw  0xf8            ; W^0x20 =      MM ww ww ww  ww zz zz zz
148
149 ;       serialrx_check 0x00, 0xa0, point_master_do
150 ;       serialrx_check 0xa0, 0x88, ping_master_do
151 ;       serialrx_check 0x88, 0x90, polarity_master_do
152
153 ;       serialrx_check 0x90, 0x90, polarity_master_do
154
155 ;       mov_wf  t               ; t =           MM ww ww ww  ww zz zz zz
156
157 ;       xor_lw  0x
158
159 ;       and_lw
160
161 ;--------------------
162 command_ping
163 command_power
164 command_point
165 command_crashed
166         panic   morse_UC
167
168 code2   code
169
170 ;======================================================================
171 ; SLAVE
172
173 ;----------------------------------------
174 slave_init
175 ; Slave-specific initialisation.
176         mov_fw  picno
177         call    i2cs_init
178
179         bc_f    T0CON, TMR0ON
180
181         call    common_local_init
182         call    detect_slave_init
183         return
184
185 ;----------------------------------------
186 slave_interrupt_low
187         enter_interrupt_low
188         call    points_local_intrl
189         panic   morse_IL
190
191 ;----------------------------------------
192 i2csu_write_begin
193         return
194
195 ;----------------------------------------
196 i2csu_write_data
197         call    led_green
198
199         bt_f_if1 picno,picno_panicd
200         goto    i2csu_write_panicd
201
202         bt_w_if0 7
203         bra     i2csu_write_if_special
204         bt_w_if1 6
205         bra     polarity_local_do
206         bt_w_if0 5
207         bra     point_local_do
208 i2csu_write_if_bad
209         mov_wf  t
210         panic   morse_DX
211
212 i2csu_write_if_special
213         tst_w_ifnz
214         bra     i2csu_write_if_bad
215         goto    panic_crashread_commanded
216
217 ;======================================================================
218 ; MASTER/SLAVE deviations
219
220 ;----------
221 message_for_master
222 ; Either transmits the message to the master, or if we are the master,
223 ; handles it as an incoming message from the notional `slave 0'.
224 ;  W            message         unchanged
225 ;  GIEH         set             set
226         bt_f_if1 idloc1,idloc1_master
227         goto    loopback_read_byte
228         goto    slave_add_short_message
229
230 ;======================================================================
231 ; UNIMPLEMENTED STUFF
232
233 ;----------
234 loopback_read_byte
235         panic   morse_UL
236
237  include final.inc