* src/*.c, src/pic16/{gen.c,glue.c,main.c}: applied Vangelis
[fw/sdcc] / device / lib / _gptrget.c
1 /*-------------------------------------------------------------------------
2
3   _gptrget.c :- get value for a generic pointer
4
5              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
6
7    This library is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Library General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
25
26 /* not all devices use P2 to page pdata memory, therefore __XPAGE was
27    introduced. On some targets __XPAGE itself is a paged SFR so it is
28    not safe for all platforms to set this. Furthermore some targets
29    can be configured to behave differently for movx @dptr vs. movx @Ri
30    (don't drive high byte of address bus for movx @Ri only) */
31 #define USE_PDATA_PAGING_REGISTER 0
32
33 __sbit __at (0xF7) B_7;
34 __sbit __at (0xF6) B_6;
35 __sbit __at (0xF5) B_5;
36
37 /* the  return value is expected to be in acc, and not in the standard
38  * location dpl. Therefore we choose return type void here: */
39 #if defined DSDCC_MODEL_HUGE
40 void
41 _gptrget (char *gptr) _naked
42 {
43 /* This is the banked version with pointers up to 23 bits.
44    B cannot be trashed */
45
46     gptr; /* hush the compiler */
47
48     _asm
49     ;
50     ;   depending on the pointer type acc. to SDCCsymt.h
51     ;
52         jb      _B_7,codeptr$        ; >0x80 code       ; 3
53         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
54
55         mov     dph,r0 ; save r0 independant of regbank ; 2
56         mov     r0,dpl ; use only low order address     ; 2
57
58         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
59     ;
60     ;   Pointer to data space
61     ;
62         mov     a,@r0                                   ; 1
63         mov     r0,dph ; restore r0                     ; 2
64         mov     dph,#0 ; restore dph                    ; 2
65         ret                                             ; 1
66     ;
67     ;   pointer to external stack or pdata
68     ;
69  pdataptr$:
70         movx    a,@r0                                   ; 1
71         mov     r0,dph ; restore r0                     ; 2
72         mov     dph,#0 ; restore dph                    ; 2
73         ret                                             ; 1
74     ;
75     ;   pointer to code area
76     ;
77  codeptr$:
78     ; implementation for SiLabs C8051F12x
79         mov     a,b                                     ; 2
80         anl     a,#0x03                                 ; 2
81         swap    a                                       ; 1
82         push    _PSBANK                                 ; 2
83         anl     _PSBANK,#0x0F                           ; 3
84         orl     _PSBANK,a                               ; 2
85
86         clr     a                                       ; 1
87         movc    a,@a+dptr                               ; 1
88         pop     _PSBANK                                 ; 2
89         ret                                             ; 1
90     ;
91     ;   pointer to xternal data
92     ;
93  xdataptr$:
94     ; implementation for xram a16-a21 tied to P3
95         mov     _P3,b                                   ; 3
96
97         movx    a,@dptr                                 ; 1
98         ret                                             ; 1
99                                                         ;===
100                                                         ;47 bytes
101      _endasm ;
102 }
103
104 #elif defined DSDCC_MODEL_MEDIUM
105
106 void
107 _gptrget (char *gptr) _naked
108 {
109 /* This is the non-banked version with pointers up to 15 bits.
110    Assumes B is free to be used */
111
112     gptr; /* hush the compiler */
113
114     _asm
115     ;
116     ;   depending on the pointer type acc. to SDCCsymt.h
117     ;
118         mov     b,dph                                   ; 3
119         jb      _B_7,codeptr$        ; >0x80 code       ; 3
120         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
121
122         mov     b,r0   ; save r0 independant of regbank ; 2
123         mov     r0,dpl ; use only low order address     ; 2
124
125         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
126     ;
127     ;   Pointer to data space
128     ;
129         mov     a,@r0                                   ; 1
130         mov     r0,b   ; restore r0                     ; 2
131         ret                                             ; 1
132     ;
133     ;   pointer to xternal stack or pdata
134     ;
135  pdataptr$:
136         movx    a,@r0                                   ; 1
137         mov     r0,b   ; restore r0                     ; 2
138         ret                                             ; 1
139     ;
140     ;   pointer to code area, max 15 bits
141     ;
142  codeptr$:
143     ; 0x8000 <= dptr <= 0xFFFF
144     ; no need to AND dph and restore from B if hardware wraps code memory
145         anl     dph,#0x7F                               ; 3
146         clr     a                                       ; 1
147         movc    a,@a+dptr                               ; 1
148         mov     dph,b                                   ; 3
149         ret                                             ; 1
150     ;
151     ;   pointer to xternal data, max 14 bits
152     ;
153  xdataptr$:
154     ; 0 <= dptr <= 0x3FFF
155         movx    a,@dptr                                 ; 1
156         ret                                             ; 1
157                                                         ;===
158                                                         ;35 bytes
159      _endasm ;
160 }
161
162 #elif 1
163
164 void
165 _gptrget (char *gptr) _naked
166 {
167 /* This is the new version with pointers up to 16 bits.
168    B cannot be trashed */
169
170     gptr; /* hush the compiler */
171
172     _asm
173     ;
174     ;   depending on the pointer type acc. to SDCCsymt.h
175     ;
176         jb      _B_7,codeptr$        ; >0x80 code       ; 3
177         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
178
179         mov     dph,r0 ; save r0 independant of regbank ; 2
180         mov     r0,dpl ; use only low order address     ; 2
181
182         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
183     ;
184     ;   Pointer to data space
185     ;
186         mov     a,@r0                                   ; 1
187         mov     r0,dph ; restore r0                     ; 2
188         mov     dph,#0 ; restore dph                    ; 2
189         ret                                             ; 1
190     ;
191     ;   pointer to xternal stack or pdata
192     ;
193  pdataptr$:
194         movx    a,@r0                                   ; 1
195         mov     r0,dph ; restore r0                     ; 2
196         mov     dph,#0 ; restore dph                    ; 2
197         ret                                             ; 1
198     ;
199     ;   pointer to code area, max 16 bits
200     ;
201  codeptr$:
202         clr     a                                       ; 1
203         movc    a,@a+dptr                               ; 1
204         ret                                             ; 1
205     ;
206     ;   pointer to xternal data, max 16 bits
207     ;
208  xdataptr$:
209         movx    a,@dptr                                 ; 1
210         ret                                             ; 1
211                                                         ;===
212                                                         ;30 bytes
213      _endasm ;
214 }
215
216 #else
217
218 void
219 _gptrget (char *gptr) _naked
220 {
221 /* This is the old version with pointers up to 16 bits. */
222
223     gptr; /* hush the compiler */
224
225     _asm
226     ;
227     ;   depending on the pointer type acc. to SDCCsymt.h
228     ;
229         mov     a,b                                     ; 2
230         jz      00001$  ; 0 near                        ; 2
231         dec     a                                       ; 1
232         jz      00002$  ; 1 far                         ; 2
233         dec     a                                       ; 1
234         jz      00003$  ; 2 code                        ; 2
235         dec     a                                       ; 1
236         jz      00004$  ; 3 pdata                       ; 2
237         dec     a       ; 4 skip generic pointer        ; 1
238         dec     a                                       ; 1
239         jz      00001$  ; 5 idata                       ; 2
240     ;
241     ;   any other value for type
242     ;   return xFF
243         mov     a,#0xff                                 ; 2
244         ret                                             ; 1
245     ;
246     ;   Pointer to data space
247     ;
248  00001$:
249         push    ar0                                     ; 2
250         ;
251         mov     r0,dpl     ; use only low order address ; 2
252         mov     a,@r0                                   ; 1
253         ;
254         pop     ar0                                     ; 2
255         ;
256         ret                                             ; 1
257     ;
258     ;   pointer to xternal data
259     ;
260  00002$:
261         movx    a,@dptr                                 ; 1
262         ret                                             ; 1
263 ;
264 ;   pointer to code area
265 ;
266  00003$:
267         ; clr     a  is already 0
268         movc    a,@a+dptr                               ; 1
269         ret                                             ; 1
270 ;
271 ;   pointer to xternal stack or pdata
272 ;
273  00004$:
274 #if USE_PDATA_PAGING_REGISTER
275         mov     dph,__XPAGE     ; __XPAGE (usually p2) holds high byte for pdata access
276         movx    a,@dptr
277 #else
278         push    ar0                                     ; 2
279         mov     r0,dpl                                  ; 2
280         movx    a,@r0                                   ; 1
281         pop     ar0                                     ; 2
282 #endif
283         ret                                             ; 1
284                                                         ;===
285                                                         ;40 bytes
286      _endasm ;
287 }
288 #endif
289
290 #ifdef SDCC_ds390
291 /* the  return value is expected to be in acc/_ap, and not in the standard
292  * location dpl/dph. Therefore we choose return type void here: */
293
294 #if 1
295
296 void
297 _gptrgetWord (unsigned *gptr)
298 {
299 /* This is the new version */
300     gptr; /* hush the compiler */
301
302     _asm
303     ;
304     ;   depending on the pointer type acc. to SDCCsymt.h
305     ;
306         jb      _B_7,00003$           ; >0x80 code
307         jnb     _B_6,00002$           ; <0x40 far
308
309         mov     dph,r0 ; save r0 independant of regbank
310         mov     r0,dpl ; use only low order address
311
312         jb      _B_5,00004$           ; >0x60 pdata
313     ;
314     ;   Pointer to data space
315     ;
316         mov     _ap,@r0
317         inc     r0
318         mov     a,@r0
319         inc     dpl
320         sjmp    00005$
321     ;
322     ;   pointer to xternal data
323     ;
324  00002$:
325         movx    a,@dptr
326         mov     _ap,a
327         inc     dptr
328         movx    a,@dptr
329         sjmp    00006$
330 ;
331 ;   pointer to code area
332 ;
333  00003$:
334         clr     a
335         movc    a,@a+dptr
336         mov     _ap,a
337         clr     a
338         inc     dptr
339         movc    a,@a+dptr
340         sjmp    00006$
341 ;
342 ;   pointer to xternal stack
343 ;
344  00004$:
345         movx    a,@r0
346         mov     _ap,a
347         inc     r0
348         movx    a,@r0
349         inc     dpl
350 ;
351 ;   restore and return
352 ;
353  00005$:
354         mov     r0,dph ; restore r0
355         mov     dph,#0 ; restore dph
356  00006$:
357     _endasm ;
358
359 }
360
361 #else
362
363 void
364 _gptrgetWord (unsigned *gptr)
365 {
366     gptr; /* hush the compiler */
367
368     _asm
369     ;
370     ;   depending on the pointer type acc. to SDCCsymt.h
371     ;
372         mov     a,b
373         jz      00001$  ; 0 near
374         dec     a
375         jz      00002$  ; 1 far
376         dec     a
377         jz      00003$  ; 2 code
378         dec     a
379         jz      00004$  ; 3 pdata
380         dec     a       ; 4 skip generic pointer
381         dec     a
382         jz      00001$  ; 5 idata
383     ;
384     ;   any other value for type
385     ;   return xFF
386         mov     a,#0xff
387         sjmp    00006$
388     ;
389     ;   Pointer to data space
390     ;
391  00001$:
392         push    ar0
393         mov     r0,dpl     ; use only low order address
394         mov     _ap,@r0
395         inc     r0
396         mov     a,@r0
397         inc     dpl
398         sjmp    00005$
399     ;
400     ;   pointer to xternal data
401     ;
402  00002$:
403         movx    a,@dptr
404         mov     _ap,a
405         inc     dptr
406         movx    a,@dptr
407         sjmp    00006$
408 ;
409 ;   pointer to code area
410 ;
411  00003$:
412         ; clr     a  is already 0
413         movc    a,@a+dptr
414         mov     _ap,a
415         clr     a
416         inc     dptr
417         movc    a,@a+dptr
418         sjmp    00006$
419 ;
420 ;   pointer to xternal stack
421 ;
422  00004$:
423         push    ar0
424         mov     r0,dpl
425         movx    a,@r0
426         mov     _ap,a
427         inc     r0
428         movx    a,@r0
429         inc     dpl
430 ;
431 ;   restore and return
432 ;
433 00005$:
434         pop ar0
435 00006$:
436     _endasm ;
437
438 }
439 #endif
440
441 #endif