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