chiark / gitweb /
giant diffs of clock speed change
[trains.git] / detpic / panic.asm
1 ;======================================================================
2 ; panic.asm
3 ;
4 ; This file implements panic_routine, which is called by the
5 ; `panic' macro in panic.inc.  See panic.inc for the functionality
6 ; of `panic'.
7
8         include common.inc
9
10 ;---------------------------------------------------------------------------
11 ; reserved access bank locations
12
13         udata_acs
14
15 ; used in panic routine for temporary storage:
16
17 flash_pattern           res     1
18 morse_counter           res     1
19 register_counter        res     1
20 bit_counter             res     1
21
22 panic_address   res     1       ; condensed form of message start addr.
23 panic_morse     res     1       ; # bytes of morse msg in panic readout
24 panic_regs      res     1       ; # registers in panic readout
25
26
27 ;****************************************************************************
28
29         code
30
31 ;****************************************************************************
32
33 panic_routine
34 ; switch off interrupts and power
35 ; reconfigure timer0 for writing diagnostic msg to the LED
36
37         clr_f    INTCON        ; disable all interrupts EVER
38         bc_f     PORTC,1       ; switch off booster
39
40 ; now we have time to copy the panic message address out of WREG
41 ; (turning off interrupts is urgent (we might get interrupted while
42 ;  panicing which would be bad because we might forget to panic).
43
44         mov_wf   panic_address
45         clr_f    STKPTR         ; avoids stack overruns
46
47 ; re-initialise timer0 config
48         mov_lw  (1<<TMR0ON) | morse_t0scale ; Enable, 16-bit, timer, prescaler
49         mov_wf  T0CON
50
51 ; get # bytes of morse msg, # registers in panic readout, message start addr.
52 ; back from condensed message start addr. stored in panic_address
53
54 panic_loop
55         mov_lw  4 ; size of each message's details
56         mul_wf  panic_address
57         mov_ff  PRODL,TBLPTRL           
58         mov_ff  PRODH,WREG
59         add_lw  (morse_messages_start)/256
60         mov_wf  TBLPTRH
61         clr_f   TBLPTRU
62
63         tblrd   *+              ; read 1st byte of error message
64                                 ; (gives # bytes morse, # bytes registers)
65         dw      0xffff ; silicon errata: B4 issue 4
66
67         mov_ff  TABLAT,panic_morse
68         mov_lw  00001111b
69         and_wff panic_morse     ; panic_morse now contains # bytes of morse msgs
70
71         mov_ff  TABLAT,panic_regs
72         mov_lw  01110000b
73         and_wff panic_regs
74         swap_f  panic_regs      ; panic_regs now contains # registers to read
75
76         call    led_black
77         rcall   waiting16
78         rcall   morsemsg        ; transmit morse in red
79         call    led_black
80         rcall   waiting8
81         rcall   waiting4
82         rcall   registermsg     ; transmit contents of registers in 
83                                 ; red(=low) and blue(=high)
84         rcall   waiting16
85         bra     panic_loop
86
87 ;****************************************************************************
88 ; PANIC SUBROUTINES
89
90 morsemsg
91 ; wrapper round morse_readout to flash the per-pic led red for a morse msg
92
93 morse_msg_start
94         clr_f           morse_counter           ; clear loop counter
95
96 morse_loop
97         mov_fw          panic_morse
98         cmp_fw_ifge     morse_counter           ; if loop counter >=panic_morse
99         return                                  ; return to panic
100
101         tblrd           *+
102         mov_ff          TABLAT,flash_pattern
103         rcall           morse_readout
104         inc_f           morse_counter
105         bra             morse_loop
106
107
108 ;--------------------------
109 morse_readout
110
111 ; Flashes the per-pic led and black in a specified pattern.
112 ;
113 ; The pattern is specified as the state for 8 identically-long time
114 ; periods each as long as a morse `dot', encoded into a byte with
115 ; most significant bit first.
116 ;                       On entry                On exit
117 ; W                     any                     undefined
118 ; flash_pattern         flash pattern           preserved
119 ; bit_counter           any                     undefined
120
121         mov_lw          9
122         mov_wf          bit_counter
123         rr_f            flash_pattern
124
125 morse_readout_loop
126         dec_f_ifz       bit_counter             ; done all the bits yet ?
127         return
128         ; No:
129
130         rl_f            flash_pattern           ; top bit goes into N, 
131                                                 ;ie Negative if 1
132         bra_n           morse_readout_if_led_1
133
134 morse_readout_if_led_0
135         call            led_black
136         bra             morse_readout_endif_led
137
138 morse_readout_if_led_1
139         call            led_red
140
141 morse_readout_endif_led
142         rcall           waiting
143         bra             morse_readout_loop
144
145 ;--------------------------
146 ;--------------------------
147 registermsg
148
149 register_msg_start
150         clr_f           register_counter        ; clear loop counter
151
152 register_loop
153         mov_fw          panic_regs
154         cmp_fw_ifge     register_counter        ; if loop counter >=panic_regs
155         return                                  ; return to panic
156
157         tblrd           *+
158
159         mov_fw          TABLAT          ; TABLAT has the 8-bit version
160         mov_wf          FSR0L           ; of the address.  So, 8 bits
161                                         ; go straight into FSR0L.
162
163         mov_lw          0x0f            ; For FSR0H, we see if the
164         mov_fw          FSR0H           ; address XX is >=0x60.
165                                         ; If it is then we meant 0xfXX;
166         mov_lw          0x5f            ; if not then we meant 0x0XX.
167         cmp_fw_ifle     FSR0L           ; (This is just like PIC does
168         clr_f           FSR0H           ; for insns using Access Bank)
169
170         mov_ff          INDF0,flash_pattern
171         rcall           register_readout
172
173         inc_f           register_counter        ;increment loop counter
174
175         rcall           waiting8
176         bra             register_loop
177
178 ;--------------------------
179
180 register_readout
181
182 ; Flashes the per-pic led red(0) and green(1) in a specified pattern.
183 ; (black gap between each bit)
184 ;
185 ; The pattern is specified as the state for 8 identically-long time
186 ; periods each as long as a morse `dot', encoded into a byte with
187 ; most significant bit first.
188 ;                       On entry                On exit
189 ; W                     any                     undefined
190 ; flash_pattern         flash pattern           preserved
191 ; bit_counter           any                     undefined
192
193         clr_f   bit_counter             ; clear loop counter
194         rr_f    flash_pattern
195
196
197 register_readout_loop
198         mov_lw          8
199         cmp_fw_ifge     bit_counter             ; if loop counter >=8 (register 
200                                                 ; length), return
201         return
202
203         mov_lw          4
204         cmp_fw_ifne     bit_counter     ; if loop counter !=4 (nybble length), 
205                                         ; skip insertion of extra black space
206         bra             not_nybble_boundary
207         rcall           waiting4
208
209 not_nybble_boundary
210         rl_f            flash_pattern           ; top bit goes into N flag, 
211                                                 ; ie Negative if 1
212         bra_n           register_readout_if_led_1
213
214 register_readout_if_led_0
215         call            led_red
216         bra             register_readout_endif_led
217
218 register_readout_if_led_1
219         call            led_green
220
221 register_readout_endif_led
222         inc_f           bit_counter       ; increment loop counter
223         rcall           waiting
224         call            led_black
225         rcall           waiting
226         bra             register_readout_loop
227
228
229 ;****************************************************************************
230 ; GENERAL SUBROUTINES
231
232 ;----------------------------------------
233 waiting16       rcall   waiting8
234 waiting8        rcall   waiting4
235 waiting4        rcall   waiting2
236 waiting2        rcall   waiting
237 waiting
238 ; waits for a fixed interval, depending on the configuration of TMR0
239
240         bc_f    INTCON,2        ; clear timer0 interrupt bit (p109)
241 ; Interrupt happens on overflow.  So start at 65535-morse_t0cycles:
242         mov_lw  (65535-morse_t0cycles) / 256
243         mov_wf  TMR0H           ; p107 set high byte of timer0 (buffered,
244                                 ; only actually set when write to tmr0l occurs)
245         mov_lw  (65535-morse_t0cycles) & 0xff
246         mov_wf  TMR0L           ; set timer0 low byte - timer now set
247 waiting_loop
248         bt_f_if0 INTCON,TMR0IF          
249         bra     waiting_loop    ; wait for timer0 interrupt
250         return
251
252
253 ;****************************************************************************
254
255         include final.inc