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