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