chiark / gitweb /
new D macro for using debugbyte
[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 D macro v
126         mov_lw  v
127         call    debugbyte
128         endm
129
130 ;----------------------------------------
131 ; For entering and leaving Low ISR, saving and restoring STATUS and W
132 ; See above under <something>_intrl, and {master,slave}_interrupt_low
133
134 enter_interrupt_low macro
135         mov_ff  STATUS, isr_low_save_status
136         mov_wf  isr_low_save_w
137         mov_ff  STKPTR, isr_low_save_stkptr
138         endm
139
140 intrh_fsr0_save macro
141         mov_ff  FSR0L, isr_high_save_fsr0
142         mov_ff  FSR0H, isr_high_save_fsr0+1
143         endm
144
145 intrh_fsr0_restore macro
146         mov_ff  isr_high_save_fsr0,   FSR0L
147         mov_ff  isr_high_save_fsr0+1, FSR0H
148         endm
149
150 intrl_handled_core macro ; for internal use only
151         mov_fw  isr_low_save_w
152         mov_ff  isr_low_save_status, STATUS
153         retfie
154         endm
155
156 intrl_handled_nostack macro
157         pop     ; undo the `call' from the original ISR
158         intrl_handled_core
159         endm
160
161 intrl_handled macro
162         goto    intrl_handled_routine
163         endm
164
165 ;----------------------------------------
166 ; For disabling all interrupts, to make a critical section:
167 ; (for use from main program and Low ISR only)
168 ;
169 ;  GIEH                 modified appropriately
170 ;  everything else      preserved
171
172 intrh_mask macro
173         bc_f    INTCON,GIEH
174         endm
175
176 intrh_unmask macro
177         bs_f    INTCON,GIEH
178         endm
179
180 ;----------------------------------------
181 ; For the fix specified in the silicon errata:
182 ; silicon revision B4 issue 4
183 ;
184 ;                       Before          After
185 ;  TABLAT               any             data from flash
186 ;  TBLPTR*              correct         incremented/decremented
187 ;  everything else      any             preserved
188
189 tblrd_postinc_fixup macro
190         tblrd   *+
191         dw      0xffff
192         endm
193
194 tblrd_postdec_fixup macro
195         tblrd   *-
196         dw      0xffff
197         endm
198
199 ;----------------------------------------
200 ; For setting up TBLPTR
201
202 load_tblptr macro value
203 ;
204 ;                       Before          After
205 ;  TBLPTR*              any             set
206 ;  W, STATUS            any             undefined
207 ;
208         mov_lw  value & 0xff
209         mov_wf  TBLPTRL
210
211         mov_lw  value >> 8
212         mov_wf  TBLPTRH
213
214   if value > 0xffff
215         mov_lw  value >> 16
216         mov_wf  TBLPTRU
217   else
218         clr_f   TBLPTRU
219   endif
220         endm
221
222 load_perpic_tblptr macro flash_map_base, perpic_entry_size
223 ;
224 ;                       Before          After
225 ;  TBLPTR*              any             set
226 ;  W, STATUS, PROD*     any             undefined
227 ;  everything else      any             preserved
228 ;
229         mov_lw  perpic_entry_size
230         mul_wf  picno
231
232         mov_lw  flash_map_base & 0xff
233         add_wfw PRODL
234         mov_wf  TBLPTRL
235
236         mov_lw  flash_map_base >> 8
237         addc_wfw PRODH
238         mov_wf  TBLPTRH
239
240         clr_f   TBLPTRU         ; TBLPTR* -> our point data
241         endm
242
243 ;----------------------------------------------------------------------
244 ; PINSPECS stuff
245 ;
246 ; A PINSPEC is a constant 0x<bit><port> where <port> is a b c d e
247 ; and <port> is 0 1 2 3 4 5 6 7.  Generally p<picno>_<subsystem>_<pin>
248 ; are equ'd for this.
249
250   radix hex
251 p0_cdu_enable           equ     5b
252 p0_rs232_fcin           equ     4b
253 p0_booster_shutdown     equ     2b
254 p0_booster_overload     equ     1b
255 p0_booster_userfault    equ     0b
256 p0_spare2               equ     6d
257 p0_spare1               equ     5d
258 p0_rs232_fcout          equ     5c
259 pall_perpicled          equ     2d
260 pall_pt0reverse         equ     7b
261 p0_spare0               equ     0a
262 p0_booster_dirn         equ     0c
263 p0_booster_pwm          equ     1c
264   radix dec
265
266 ;                       
267 ;  LAT*                 may be subject to read-modify-write, see below
268 ;  TRIS*                may be subject to read-modify-write, see below
269 ;  PORT*                may be read, see below
270 ;  everything else      untouched
271 ;
272 ;                       LAT*<bit>       TRIS*<bit>      PORT*
273 ;  pin_z                untouched       set             untouched
274 ;  pin_h                set             cleared         untouched
275 ;  pin_l                cleared         cleared         untouched
276 ;  pin_nz               untouched       cleared         untouched
277 ;  pin_vh               set             untouched       untouched
278 ;  pin_vl               cleared         untouched       untouched
279 ;  pin_ifh              untouched       untouched       read
280 ;  pin_ifl              untouched       untouched       read
281
282 pin_z   macro   pinspec
283         bs_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
284         endm
285
286 pin_nz  macro   pinspec
287         bc_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
288         endm
289
290 pin_znz macro   pinspec
291         btg_f   TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
292         endm
293
294 pin_vh  macro   pinspec
295         bs_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
296         endm
297
298 pin_vl  macro   pinspec
299         bc_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
300         endm
301
302 pin_vhl macro   pinspec
303         btg_f   LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
304         endm
305
306 pin_h   macro   pinspec
307         pin_vh  pinspec
308         pin_nz  pinspec
309         endm
310
311 pin_l   macro   pinspec
312         pin_vl  pinspec
313         pin_nz  pinspec
314         endm
315
316 pin_ifh macro   pinspec
317         bt_f_if1 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
318         endm
319
320 pin_ifl macro   pinspec
321         bt_f_if0 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
322         endm
323
324 pinlat_ifh macro        pinspec
325         bt_f_if1 LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
326         endm
327
328 pinlat_ifl      macro   pinspec
329         bt_f_if0 LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
330         endm
331
332 ;----------------------------------------------------------------------