chiark / gitweb /
fixes and changes from code review
[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
46
47 ; re-initialise timer0 config
48         bc_f     T0CON,6       ; p107 Timer0 -> 16bit mode
49         bc_f     T0CON,5       ; timer0 use internal clock
50         bc_f     T0CON,3       ; use prescaler
51         bc_f     T0CON,2       ; }
52         bs_f     T0CON,1       ; } prescale value 1:16 (13ms x 16)
53         bs_f     T0CON,0       ; }
54
55 ; get # bytes of morse msg, # registers in panic readout, message start addr.
56 ; back from condensed message start addr. stored in panic_address
57
58 panic_loop
59         mov_lw  4 ; size of each message's details
60         mul_wf  panic_address
61         mov_ff  PRODL,TBLPTRL           
62         mov_ff  PRODH,WREG
63         add_lw  (morse_messages_start)/256
64         mov_wf  TBLPTRH
65         clr_f   TBLPTRU
66
67         tblrd   *+              ; read 1st byte of error message
68                                 ; (gives # bytes morse, # bytes registers)
69
70         mov_ff  TABLAT,panic_morse
71         mov_lw  00001111b
72         and_wff panic_morse     ; panic_morse now contains # bytes of morse msgs
73
74         mov_ff  TABLAT,panic_regs
75         mov_lw  01110000b
76         and_wff panic_regs
77         swap_f  panic_regs      ; panic_regs now contains # registers to read
78
79         call    led_black
80         call    waiting
81         call    waiting
82         call    waiting
83         call    waiting
84         call    morsemsg        ; transmit morse in red
85         call    led_black
86         call    waiting
87         call    waiting
88         call    waiting
89         call    waiting
90         call    registermsg     ; transmit contents of registers in 
91                                 ; red(=low) and blue(=high)
92         goto    panic_loop
93
94 ;****************************************************************************
95 ; PANIC SUBROUTINES
96
97 morsemsg
98 ; wrapper round morse_readout to flash the per-pic led red for a morse msg
99
100 morse_msg_start
101         clr_f           morse_counter           ; clear loop counter
102
103 morse_loop
104         mov_fw          panic_morse
105         cmp_fw_ifge     morse_counter           ; if loop counter >=panic_morse
106         return                                  ; return to panic
107
108         tblrd           *+
109         mov_ff          TABLAT,flash_pattern
110         call            morse_readout
111         inc_f           morse_counter
112         goto            morse_loop
113
114
115 ;--------------------------
116 morse_readout
117
118 ; Flashes the per-pic led and black in a specified pattern.
119 ;
120 ; The pattern is specified as the state for 8 identically-long time
121 ; periods each as long as a morse `dot', encoded into a byte with
122 ; most significant bit first.
123 ;                       On entry                On exit
124 ; W                     any                     undefined
125 ; flash_pattern         flash pattern           preserved
126 ; bit_counter           any                     undefined
127
128         mov_lw          8
129         mov_wf          bit_counter
130         rr_f            flash_pattern
131
132 morse_readout_loop
133         dec_f_ifz       bit_counter             ; done all the bits yet ?
134         return
135         ; No:
136
137         rl_f            flash_pattern           ; top bit goes into N, 
138                                                 ;ie Negative if 1
139         bra_n           morse_readout_if_led_1
140
141 morse_readout_if_led_0
142         call            led_black
143         bra             morse_readout_endif_led
144
145 morse_readout_if_led_1
146         call            led_red
147
148 morse_readout_endif_led
149         bra             morse_readout_loop
150
151 ;--------------------------
152 ;--------------------------
153 registermsg
154
155 register_msg_start
156         clr_f           register_counter        ; clear loop counter
157
158 register_loop
159         mov_fw          panic_regs
160         cmp_fw_ifge     register_counter        ; if loop counter >=panic_regs
161         return                                  ; return to panic
162
163         tblrd           *+
164
165         mov_fw          TABLAT          ; TABLAT has the 8-bit version
166         mov_wf          FSR0L           ; of the address.  So, 8 bits
167                                         ; go straight into FSR0L.
168
169         mov_lw          0x0f            ; For FSR0H, we see if the
170         mov_fw          FSR0H           ; address XX is >=0x60.
171                                         ; If it is then we meant 0xfXX;
172         mov_lw          0x5f            ; if not then we meant 0x0XX.
173         cmp_fw_ifle     FSR0L           ; (This is just like PIC does
174         clr_f           FSR0H           ; for insns using Access Bank)
175
176         mov_ff          INDF0,flash_pattern
177         call            register_readout
178
179         inc_f           register_counter        ;increment loop counter
180
181         call            waiting
182         call            waiting
183         goto            register_loop
184
185 ;--------------------------
186
187 register_readout
188
189 ; Flashes the per-pic led red(0) and green(1) in a specified pattern.
190 ; (black gap between each bit)
191 ;
192 ; The pattern is specified as the state for 8 identically-long time
193 ; periods each as long as a morse `dot', encoded into a byte with
194 ; most significant bit first.
195 ;                       On entry                On exit
196 ; W                     any                     undefined
197 ; flash_pattern         flash pattern           preserved
198 ; bit_counter           any                     undefined
199
200         clr_f   bit_counter             ; clear loop counter
201         rr_f    flash_pattern
202
203
204 register_readout_loop
205         mov_lw          8
206         cmp_fw_ifge     bit_counter             ; if loop counter >=8 (register 
207                                                 ; length), return
208         return
209
210         mov_lw          4
211         cmp_fw_ifne     bit_counter     ; if loop counter !=4 (nybble length), 
212                                         ; skip insertion of extra black space
213         goto            not_nybble_boundary
214         call            waiting
215
216 not_nybble_boundary
217         rl_f            flash_pattern           ; top bit goes into N flag, 
218                                                 ; ie Negative if 1
219         bra_n           register_readout_if_led_1
220
221 register_readout_if_led_0
222         call            led_red
223         bra             register_readout_endif_led
224
225 register_readout_if_led_1
226         call            led_green
227
228 register_readout_endif_led
229         inc_f           bit_counter       ; increment loop counter
230         call            waiting
231         call            led_black
232         call            waiting
233         bra             register_readout_loop
234
235
236 ;****************************************************************************
237 ; GENERAL SUBROUTINES
238
239 ;----------------------------------------
240 waiting
241 ; waits for a fixed interval, depending on the configuration of TMR0
242
243         bc_f    INTCON,2        ; clear timer0 interrupt bit (p109)
244         clr_f   TMR0H           ; p107 set high byte of timer0 to 0 (buffered,
245                                 ; only actually set when write to tmr0l occurs)
246         clr_f   TMR0L           ; set timer0 low byte - timer now set to 0000h
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