* as/mcs51/asexpr.c (expr): disabled warning "not in .flat24 mode",
[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                 anl             _PSBANK,#0x0F                                                   ; 3
83                 orl             _PSBANK,a                                                               ; 2
84
85         clr     a                                                                               ; 1
86         movc    a,@a+dptr                                                               ; 1
87         ret                                                                                             ; 1
88     ;
89     ;   pointer to xternal data
90     ;
91  xdataptr$:
92         ; implementation for xram a16-a21 tied to P3
93                 mov             _P3,b                                                                   ; 3
94
95         movx    a,@dptr                                                                 ; 1
96         ret                                                                                             ; 1
97                                                                                                         ;===
98                                                                                                         ;43 bytes
99      _endasm ;
100 }
101
102 #elif defined DSDCC_MODEL_MEDIUM
103
104 void
105 _gptrget (char *gptr) _naked
106 {
107 /* This is the non-banked version with pointers up to 15 bits.
108    Assumes B is free to be used */
109
110     gptr; /* hush the compiler */
111
112     _asm
113     ;
114     ;   depending on the pointer type acc. to SDCCsymt.h
115     ;
116         mov             b,dph                                                                   ; 3
117         jb              _B_7,codeptr$        ; >0x80 code               ; 3
118         jnb             _B_6,xdataptr$       ; <0x40 far                ; 3
119
120         mov     b,r0   ; save r0 independant of regbank ; 2
121         mov     r0,dpl ; use only low order address             ; 2
122
123         jb              _B_5,pdataptr$       ; >0x60 pdata              ; 3
124     ;
125     ;   Pointer to data space
126     ;
127         mov     a,@r0                                                                   ; 1
128         mov     r0,b   ; restore r0                                             ; 2
129         ret                                                                                             ; 1
130     ;
131     ;   pointer to xternal stack or pdata
132     ;
133  pdataptr$:
134         movx    a,@r0                                                                   ; 1
135         mov     r0,b   ; restore r0                                             ; 2
136         ret                                                                                             ; 1
137         ;
138         ;   pointer to code area, max 15 bits
139         ;
140  codeptr$:
141         ; 0x8000 <= dptr <= 0xFFFF
142         ; no need to AND dph and restore from B if hardware wraps code memory
143         anl     dph,#0x7F                                                               ; 3
144         clr     a                                                                               ; 1
145         movc    a,@a+dptr                                                               ; 1
146         mov     dph,b                                                                   ; 3
147         ret                                                                                             ; 1
148     ;
149     ;   pointer to xternal data, max 14 bits
150     ;
151  xdataptr$:
152         ; 0 <= dptr <= 0x3FFF
153         movx    a,@dptr                                                                 ; 1
154         ret                                                                                             ; 1
155                                                                                                         ;===
156                                                                                                         ;35 bytes
157      _endasm ;
158 }
159
160 #elif 1
161
162 void
163 _gptrget (char *gptr) _naked
164 {
165 /* This is the new version with pointers up to 16 bits.
166    B cannot be trashed */
167
168     gptr; /* hush the compiler */
169
170     _asm
171     ;
172     ;   depending on the pointer type acc. to SDCCsymt.h
173     ;
174         jb              _B_7,codeptr$        ; >0x80 code               ; 3
175         jnb             _B_6,xdataptr$       ; <0x40 far                ; 3
176
177         mov     dph,r0 ; save r0 independant of regbank ; 2
178         mov     r0,dpl ; use only low order address             ; 2
179
180         jb              _B_5,pdataptr$       ; >0x60 pdata              ; 3
181     ;
182     ;   Pointer to data space
183     ;
184         mov     a,@r0                                                                   ; 1
185         mov     r0,dph ; restore r0                                             ; 2
186         mov     dph,#0 ; restore dph                                    ; 2
187         ret                                                                                             ; 1
188     ;
189     ;   pointer to xternal stack or pdata
190     ;
191  pdataptr$:
192         movx    a,@r0                                                                   ; 1
193         mov     r0,dph ; restore r0                                             ; 2
194         mov     dph,#0 ; restore dph                                    ; 2
195         ret                                                                                             ; 1
196         ;
197         ;   pointer to code area, max 16 bits
198         ;
199  codeptr$:
200         clr     a                                                                               ; 1
201         movc    a,@a+dptr                                                               ; 1
202         ret                                                                                             ; 1
203     ;
204     ;   pointer to xternal data, max 16 bits
205     ;
206  xdataptr$:
207         movx    a,@dptr                                                                 ; 1
208         ret                                                                                             ; 1
209                                                                                                         ;===
210                                                                                                         ;30 bytes
211      _endasm ;
212 }
213
214 #else
215
216 void
217 _gptrget (char *gptr) _naked
218 {
219 /* This is the old version with pointers up to 16 bits. */
220
221     gptr; /* hush the compiler */
222
223     _asm
224     ;
225     ;   depending on the pointer type acc. to SDCCsymt.h
226     ;
227         mov     a,b                                                                             ; 2
228         jz      00001$  ; 0 near                                                ; 2
229         dec     a                                                                               ; 1
230         jz      00002$  ; 1 far                                                 ; 2
231         dec     a                                                                               ; 1
232         jz      00003$  ; 2 code                                                ; 2
233         dec     a                                                                               ; 1
234         jz      00004$  ; 3 pdata                                               ; 2
235         dec     a       ; 4 skip generic pointer                ; 1
236         dec     a                                                                               ; 1
237         jz      00001$  ; 5 idata                                               ; 2
238     ;
239     ;   any other value for type
240     ;   return xFF
241         mov     a,#0xff                                                                 ; 2
242         ret                                                                                             ; 1
243     ;
244     ;   Pointer to data space
245     ;
246  00001$:
247         push    ar0                                                                             ; 2
248         ;
249         mov     r0,dpl     ; use only low order address ; 2
250         mov     a,@r0                                                                   ; 1
251         ;
252         pop     ar0                                                                             ; 2
253         ;
254         ret                                                                                             ; 1
255     ;
256     ;   pointer to xternal data
257     ;
258  00002$:
259         movx    a,@dptr                                                                 ; 1
260         ret                                                                                             ; 1
261 ;
262 ;   pointer to code area
263 ;
264  00003$:
265         ; clr     a  is already 0
266         movc    a,@a+dptr                                                               ; 1
267         ret                                                                                             ; 1
268 ;
269 ;   pointer to xternal stack or pdata
270 ;
271  00004$:
272 #if USE_PDATA_PAGING_REGISTER
273         mov     dph,__XPAGE     ; __XPAGE (usually p2) holds high byte for pdata access
274         movx    a,@dptr
275 #else
276         push    ar0                                                                             ; 2
277         mov     r0,dpl                                                                  ; 2
278         movx    a,@r0                                                                   ; 1
279         pop     ar0                                                                             ; 2
280 #endif
281         ret                                                                                             ; 1
282                                                                                                         ;===
283                                                                                                         ;40 bytes
284      _endasm ;
285 }
286 #endif
287
288 #ifdef SDCC_ds390
289 /* the  return value is expected to be in acc/_ap, and not in the standard
290  * location dpl/dph. Therefore we choose return type void here: */
291
292 #if 1
293
294 void
295 _gptrgetWord (unsigned *gptr)
296 {
297 /* This is the new version */
298     gptr; /* hush the compiler */
299
300     _asm
301     ;
302     ;   depending on the pointer type acc. to SDCCsymt.h
303     ;
304         jb              _B_7,00003$           ; >0x80 code
305         jnb             _B_6,00002$           ; <0x40 far
306
307         mov     dph,r0 ; save r0 independant of regbank
308         mov     r0,dpl ; use only low order address
309
310         jb              _B_5,00004$           ; >0x60 pdata
311     ;
312     ;   Pointer to data space
313     ;
314         mov     _ap,@r0
315         inc     r0
316         mov     a,@r0
317         inc     dpl
318         sjmp    00005$
319     ;
320     ;   pointer to xternal data
321     ;
322  00002$:
323         movx    a,@dptr
324         mov     _ap,a
325         inc     dptr
326         movx    a,@dptr
327         sjmp    00006$
328 ;
329 ;   pointer to code area
330 ;
331  00003$:
332         clr     a
333         movc    a,@a+dptr
334         mov     _ap,a
335         clr     a
336         inc     dptr
337         movc    a,@a+dptr
338         sjmp    00006$
339 ;
340 ;   pointer to xternal stack
341 ;
342  00004$:
343         movx    a,@r0
344         mov     _ap,a
345         inc     r0
346         movx    a,@r0
347         inc     dpl
348 ;
349 ;   restore and return
350 ;
351  00005$:
352         mov     r0,dph ; restore r0
353         mov     dph,#0 ; restore dph
354  00006$:
355     _endasm ;
356
357 }
358
359 #else
360
361 void
362 _gptrgetWord (unsigned *gptr)
363 {
364     gptr; /* hush the compiler */
365
366     _asm
367     ;
368     ;   depending on the pointer type acc. to SDCCsymt.h
369     ;
370         mov     a,b
371         jz      00001$  ; 0 near
372         dec     a
373         jz      00002$  ; 1 far
374         dec     a
375         jz      00003$  ; 2 code
376         dec     a
377         jz      00004$  ; 3 pdata
378         dec     a       ; 4 skip generic pointer
379         dec     a
380         jz      00001$  ; 5 idata
381     ;
382     ;   any other value for type
383     ;   return xFF
384         mov     a,#0xff
385         sjmp    00006$
386     ;
387     ;   Pointer to data space
388     ;
389  00001$:
390         push    ar0
391         mov     r0,dpl     ; use only low order address
392         mov     _ap,@r0
393         inc     r0
394         mov     a,@r0
395         inc     dpl
396         sjmp    00005$
397     ;
398     ;   pointer to xternal data
399     ;
400  00002$:
401         movx    a,@dptr
402         mov     _ap,a
403         inc     dptr
404         movx    a,@dptr
405         sjmp    00006$
406 ;
407 ;   pointer to code area
408 ;
409  00003$:
410         ; clr     a  is already 0
411         movc    a,@a+dptr
412         mov     _ap,a
413         clr     a
414         inc     dptr
415         movc    a,@a+dptr
416         sjmp    00006$
417 ;
418 ;   pointer to xternal stack
419 ;
420  00004$:
421         push    ar0
422         mov     r0,dpl
423         movx    a,@r0
424         mov     _ap,a
425         inc     r0
426         movx    a,@r0
427         inc     dpl
428 ;
429 ;   restore and return
430 ;
431 00005$:
432         pop ar0
433 00006$:
434     _endasm ;
435
436 }
437 #endif
438
439 #endif