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