chiark / gitweb /
isr register use fixes (particularly for <foo>_local_do, which have strange calling...
[trains.git] / detpic / common.inc
1 ;======================================================================
2 ; common.inc
3 ; common macros & equs etc.
4 ; generally include this at the top of each file.
5
6 ;----------------------------------------------------------------------
7 ; COMMON INCLUDES and BOILERPLATE
8         include         /usr/share/gputils/header/p18f458.inc
9         radix           dec 
10         include         panic.inc
11         include         morse+auto.inc
12         include         ../iwjpictest/insn-aliases.inc
13 clock equ -1
14         include         ../iwjpictest/clockvaries.inc
15         include         variables+vars.inc
16         include         pindata.inc
17         include         program+clocks.inc
18
19 ;----------------------------------------------------------------------
20 ; Common conventions for function register notation:
21
22 ;                       Master                  Slave
23 ; Registers etc.
24 ;   W                   Trashed                 Trashed
25 ;   STATUS              Trashed                 Trashed
26 ;   BSR                 Not used                Not used
27 ;   t                   Low ISR                 Low ISR
28 ;   TBLPTR*,TABLAT      Low ISR                 Low ISR
29 ;   PROD*               Low ISR                 Low ISR
30 ;   FSR0                Low ISR                 Low ISR
31 ;   PCLATU              Always set to 0         Not used
32 ;   PCLATH              Low ISR                 Not used
33 ;   t_dolocal           Low ISR                 High ISR
34 ;   FSR1                Low ISR                 High ISR (detect[1])
35 ;   FSR2                Low ISR                 High ISR (detect[1])
36 ;
37 ; Trashed       May be trashed by any routine anywhere.  Saved
38 ;               during every ISR entry/exit.
39 ;
40 ; Low ISR       May be used/traashed by any routine run in low-priority
41 ;               interrupt, or any routine run during initialisation.
42 ;               May therefore not be used in background loop with
43 ;               interrupts enabled.  May not be used by high-priority
44 ;               ISR (unless explicitly saved, eg isrh_fsr0_{save,restore}).
45 ;
46 ; High ISR      May be used/trashed by any routine run in high-priority
47 ;               interrupt, or any routine run during initialisation.
48 ;               May therefore not be used elsewhere with interrupts
49 ;               enabled.
50 ;
51 ;               Only the routines specially noted as intended to
52 ;               be called from the High ISR are safe.
53 ;
54 ; ... (subsystem)
55 ;               Register is reserved for use by this subsystem, which
56 ;               is allowed to expect the value to be preserved.
57 ;               Anything else which uses it must save and restore (and
58 ;               may also need to disable interrupts, depending on its
59 ;               relative status).
60 ;
61 ; Not High      May be used by any routine not running in high-priority
62 ;               interrupt.  Not saved by high-priority interrupt
63 ;               entry/exit, so any high-priority interrupt routine which
64 ;               uses this register must save and restore it.
65 ;
66 ; A routine which is allowed to trash a register may document that it
67 ; saves that register for the benefit of its callers.
68 ;
69 ;  [1]  FSR1 and FSR2 on slave pics are reserved exclusively for the
70 ;       I2C response and detection code (detect.asm), after det_slave_init.
71 ;
72 ; General-purpose hardware allocation:
73 ;
74 ;                       Master                  Slave
75 ;  Timer 0              nmra                    Disabled
76 ;  Timer 2              -                       -
77 ;  Timer 1              (1ms tick, int. low)    (1ms tick, int. low)
78 ;  CCP1                 (1ms tick, int. low)    (1ms tick, int. low)
79 ;  Timer 3              point fire timer        point fire timer
80 ;  ECCP                 -                       -
81 ;
82 ;   (...) indicates that this is a projected use, NYI
83
84 ;----------------------------------------------------------------------
85 ; Conventional routine names:
86 ;
87 ; <periph>_local_do     Process a master-to-slave command to activate
88 ;                       a local peripheral, in High ISR.  Also called
89 ;                       on master in Low ISR to activate its own
90 ;                       local peripherals.  NB strange calling convention!
91 ;
92 ; <periph>_local_init   Initialises RAM tables for local peripheral
93 ;                       and arranges for pins to be set in appropriate
94 ;                       quiescent state.  Configures pic built-in
95 ;                       peripherals.
96 ;
97 ; <periph>_local_intrl  Low ISR service routine for peripheral (see below).
98 ;                               
99 ; command_<periph>      Called when an appropriate message has been
100 ;                       received from the host.
101 ;
102 ; <something>_intrl     Low ISR service routine.
103 ;                       Checks for any relevant interrupt.
104 ;                       If not, just returns.
105 ;                       If found, services it and then does either
106 ;                        intrl_handled or intrl_handled_nostack
107 ;                        neither of which return; the latter is
108 ;                        faster but implies a promise 
109 ;
110 ;----------------------------------------------------------------------
111 ; MACROS
112
113 ;----------------------------------------
114 ; For entering and leaving Low ISR, saving and restoring STATUS and W
115 ; See above under <something>_intrl, and {master,slave}_interrupt_low
116
117 enter_interrupt_low macro
118         mov_ff  STATUS, isr_low_save_status
119         mov_wf  isr_low_save_w
120         mov_ff  STKPTR, isr_low_save_stkptr
121         endm
122
123 intrh_fsr0_save macro
124         mov_ff  FSR0L, isr_high_save_fsr0
125         mov_ff  FSR0H, isr_high_save_fsr0+1
126         endm
127
128 intrh_fsr0_restore macro
129         mov_ff  isr_high_save_fsr0,   FSR0L
130         mov_ff  isr_high_save_fsr0+1, FSR0H
131         endm
132
133 intrl_handled_core macro ; for internal use only
134         mov_fw  isr_low_save_w
135         mov_ff  isr_low_save_status, STATUS
136         retfie
137         endm
138
139 intrl_handled_nostack macro
140         pop     ; undo the `call' from the original ISR
141         intrl_handled_core
142         endm
143
144 intrl_handled macro
145         goto    intrl_handled_routine
146         endm
147
148 ;----------------------------------------
149 ; For disabling all interrupts, to make a critical section:
150 ; (for use from main program and Low ISR only)
151 ;
152 ;  GIEH                 modified appropriately
153 ;  everything else      preserved
154
155 intrh_mask macro
156         bc_f    INTCON,GIEH
157         endm
158
159 intrh_unmask macro
160         bs_f    INTCON,GIEH
161         endm
162
163 ;----------------------------------------
164 ; For the fix specified in the silicon errata:
165 ; silicon revision B4 issue 4
166 ;
167 ;                       Before          After
168 ;  TABLAT               any             data from flash
169 ;  TBLPTR*              correct         incremented/decremented
170 ;  everything else      any             preserved
171
172 tblrd_postinc_fixup macro
173         tblrd   *+
174         dw      0xffff
175         endm
176
177 tblrd_postdec_fixup macro
178         tblrd   *-
179         dw      0xffff
180         endm
181
182 ;----------------------------------------
183 ; For setting up TBLPTR according to the picno
184
185 load_perpic_tblptr macro flash_map_base, perpic_entry_size
186 ;
187 ;                       Before          After
188 ;  TBLPTR*              any             set
189 ;  W, STATUS, PROD*     any             undefined
190 ;  everything else      any             preserved
191 ;
192         mov_lw  perpic_entry_size
193         mul_wf  picno
194
195         mov_lw  flash_map_base & 0xff
196         add_wfw PRODL
197         mov_wf  TBLPTRL
198
199         mov_lw  flash_map_base >> 8
200         addc_wfw PRODH
201         mov_wf  TBLPTRH
202
203         clr_f   TBLPTRU         ; TBLPTR* -> our point data
204         endm
205
206 ;----------------------------------------------------------------------
207 ; PINSPECS stuff
208 ;
209 ; A PINSPEC is a constant 0x<bit><port> where <port> is a b c d e
210 ; and <port> is 0 1 2 3 4 5 6 7.  Generally p<picno>_<subsystem>_<pin>
211 ; are equ'd for this.
212
213  radix hex
214 p0_cdu_enable           equ     5b
215 p0_rs232_fcin           equ     4b
216 p0_booster_shutdown     equ     2b
217 p0_booster_overload     equ     1b
218 p0_booster_userfault    equ     0b
219 p0_spare2               equ     6d
220 p0_spare1               equ     5d
221 p0_rs232_fcout          equ     5c
222 pall_perpicled          equ     2d
223 pall_pt0reverse         equ     7b
224 p0_spare0               equ     0a
225 p0_booster_dirn         equ     0c
226 p0_booster_pwm          equ     1c
227  radix dec
228
229 ;                       
230 ;  LAT*                 may be subject to read-modify-write, see below
231 ;  TRIS*                may be subject to read-modify-write, see below
232 ;  PORT*                may be read, see below
233 ;  everything else      untouched
234 ;
235 ;                       LAT*<bit>       TRIS*<bit>      PORT*
236 ;  pin_z                untouched       set             untouched
237 ;  pin_h                set             cleared         untouched
238 ;  pin_l                cleared         cleared         untouched
239 ;  pin_nz               untouched       cleared         untouched
240 ;  pin_vh               set             untouched       untouched
241 ;  pin_vl               cleared         untouched       untouched
242 ;  pin_ifh              untouched       untouched       read
243 ;  pin_ifl              untouched       untouched       read
244
245 pin_z   macro   pinspec
246         bs_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
247         endm
248
249 pin_nz  macro   pinspec
250         bc_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
251         endm
252
253 pin_vh  macro   pinspec
254         bs_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
255         endm
256
257 pin_vl  macro   pinspec
258         bc_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
259         endm
260
261 pin_h   macro   pinspec
262         pin_vh  pinspec
263         pin_nz  pinspec
264         endm
265
266 pin_l   macro   pinspec
267         pin_vl  pinspec
268         pin_nz  pinspec
269         endm
270
271 pin_ifh macro   pinspec
272         bt_f_if1 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
273         endm
274
275 pin_ifl macro   pinspec
276         bt_f_if0 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
277         endm
278
279 ;----------------------------------------------------------------------