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