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