chiark / gitweb /
new general temporary "v"
[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 ;----------------------------------------------------------------------
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,u,v               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                High ISR (nmra)         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/trashed 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
71 ;       detect_slave_init.  Likewise FSR2 is reserved exclusively
72 ;       for the NMRA output ISR after nmra_init.
73 ;
74 ; General-purpose hardware allocation:
75 ;
76 ;                       Master                  Slave
77 ;  Timer 0              nmra                    Disabled
78 ;  Timer 2              tick: 10ms, int. low    -
79 ;  Timer 1              -                       -
80 ;  CCP1                 -                       -
81 ;  Timer 3              point fire timer        point fire timer
82 ;  ECCP                 -                       -
83 ;
84 ;   (...) indicates that this is a projected use, NYI
85
86 ;----------------------------------------------------------------------
87 ; Conventional routine names:
88 ;
89 ; <periph>_local_do     Process a master-to-slave command to activate
90 ;                       a local peripheral, in High ISR.  Also called
91 ;                       on master in Low ISR to activate its own
92 ;                       local peripherals.  NB strange calling convention!
93 ;
94 ; <periph>_local_init   Initialises RAM tables for local peripheral
95 ;                       and arranges for pins to be set in appropriate
96 ;                       quiescent state.  Configures pic built-in
97 ;                       peripherals.
98 ;
99 ; <periph>_local_intrl  Low ISR service routine for peripheral (see below).
100 ;                               
101 ; command_<periph>      Called when an appropriate message has been
102 ;                       received from the host.
103 ;
104 ; <something>_intrl     Low ISR service routine.
105 ;                       Checks for any relevant interrupt.
106 ;                       If not, just returns.
107 ;                       If found, services it and then does either
108 ;                        intrl_handled or intrl_handled_nostack
109 ;                        neither of which return; the latter is
110 ;                        faster but implies a promise 
111 ;
112 ;----------------------------------------------------------------------
113 ; MACROS
114
115 ;----------------------------------------
116 ; For entering and leaving Low ISR, saving and restoring STATUS and W
117 ; See above under <something>_intrl, and {master,slave}_interrupt_low
118
119 enter_interrupt_low macro
120         mov_ff  STATUS, isr_low_save_status
121         mov_wf  isr_low_save_w
122         mov_ff  STKPTR, isr_low_save_stkptr
123         endm
124
125 intrh_fsr0_save macro
126         mov_ff  FSR0L, isr_high_save_fsr0
127         mov_ff  FSR0H, isr_high_save_fsr0+1
128         endm
129
130 intrh_fsr0_restore macro
131         mov_ff  isr_high_save_fsr0,   FSR0L
132         mov_ff  isr_high_save_fsr0+1, FSR0H
133         endm
134
135 intrl_handled_core macro ; for internal use only
136         mov_fw  isr_low_save_w
137         mov_ff  isr_low_save_status, STATUS
138         retfie
139         endm
140
141 intrl_handled_nostack macro
142         pop     ; undo the `call' from the original ISR
143         intrl_handled_core
144         endm
145
146 intrl_handled macro
147         goto    intrl_handled_routine
148         endm
149
150 ;----------------------------------------
151 ; For disabling all interrupts, to make a critical section:
152 ; (for use from main program and Low ISR only)
153 ;
154 ;  GIEH                 modified appropriately
155 ;  everything else      preserved
156
157 intrh_mask macro
158         bc_f    INTCON,GIEH
159         endm
160
161 intrh_unmask macro
162         bs_f    INTCON,GIEH
163         endm
164
165 ;----------------------------------------
166 ; For the fix specified in the silicon errata:
167 ; silicon revision B4 issue 4
168 ;
169 ;                       Before          After
170 ;  TABLAT               any             data from flash
171 ;  TBLPTR*              correct         incremented/decremented
172 ;  everything else      any             preserved
173
174 tblrd_postinc_fixup macro
175         tblrd   *+
176         dw      0xffff
177         endm
178
179 tblrd_postdec_fixup macro
180         tblrd   *-
181         dw      0xffff
182         endm
183
184 ;----------------------------------------
185 ; For setting up TBLPTR according to the picno
186
187 load_perpic_tblptr macro flash_map_base, perpic_entry_size
188 ;
189 ;                       Before          After
190 ;  TBLPTR*              any             set
191 ;  W, STATUS, PROD*     any             undefined
192 ;  everything else      any             preserved
193 ;
194         mov_lw  perpic_entry_size
195         mul_wf  picno
196
197         mov_lw  flash_map_base & 0xff
198         add_wfw PRODL
199         mov_wf  TBLPTRL
200
201         mov_lw  flash_map_base >> 8
202         addc_wfw PRODH
203         mov_wf  TBLPTRH
204
205         clr_f   TBLPTRU         ; TBLPTR* -> our point data
206         endm
207
208 ;----------------------------------------------------------------------
209 ; PINSPECS stuff
210 ;
211 ; A PINSPEC is a constant 0x<bit><port> where <port> is a b c d e
212 ; and <port> is 0 1 2 3 4 5 6 7.  Generally p<picno>_<subsystem>_<pin>
213 ; are equ'd for this.
214
215  radix hex
216 p0_cdu_enable           equ     5b
217 p0_rs232_fcin           equ     4b
218 p0_booster_shutdown     equ     2b
219 p0_booster_overload     equ     1b
220 p0_booster_userfault    equ     0b
221 p0_spare2               equ     6d
222 p0_spare1               equ     5d
223 p0_rs232_fcout          equ     5c
224 pall_perpicled          equ     2d
225 pall_pt0reverse         equ     7b
226 p0_spare0               equ     0a
227 p0_booster_dirn         equ     0c
228 p0_booster_pwm          equ     1c
229  radix dec
230
231 ;                       
232 ;  LAT*                 may be subject to read-modify-write, see below
233 ;  TRIS*                may be subject to read-modify-write, see below
234 ;  PORT*                may be read, see below
235 ;  everything else      untouched
236 ;
237 ;                       LAT*<bit>       TRIS*<bit>      PORT*
238 ;  pin_z                untouched       set             untouched
239 ;  pin_h                set             cleared         untouched
240 ;  pin_l                cleared         cleared         untouched
241 ;  pin_nz               untouched       cleared         untouched
242 ;  pin_vh               set             untouched       untouched
243 ;  pin_vl               cleared         untouched       untouched
244 ;  pin_ifh              untouched       untouched       read
245 ;  pin_ifl              untouched       untouched       read
246
247 pin_z   macro   pinspec
248         bs_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
249         endm
250
251 pin_nz  macro   pinspec
252         bc_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
253         endm
254
255 pin_vh  macro   pinspec
256         bs_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
257         endm
258
259 pin_vl  macro   pinspec
260         bc_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
261         endm
262
263 pin_h   macro   pinspec
264         pin_vh  pinspec
265         pin_nz  pinspec
266         endm
267
268 pin_l   macro   pinspec
269         pin_vl  pinspec
270         pin_nz  pinspec
271         endm
272
273 pin_ifh macro   pinspec
274         bt_f_if1 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
275         endm
276
277 pin_ifl macro   pinspec
278         bt_f_if0 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
279         endm
280
281 ;----------------------------------------------------------------------