d7711e4319caa5e46e37cf78c3edb3aad1137846
[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  dataptrrestore$:
64         mov     r0,dph ; restore r0                     ; 2
65         mov     dph,#0 ; restore dph                    ; 2
66         ret                                             ; 1
67     ;
68     ;   pointer to external stack or pdata
69     ;
70  pdataptr$:
71         movx    a,@r0                                   ; 1
72         sjmp    dataptrrestore$                         ; 2
73     ;
74     ;   pointer to code area
75     ;
76  codeptr$:
77     ; implementation for SiLabs C8051F12x
78         mov     a,b                                     ; 2
79         anl     a,#0x03                                 ; 2
80         swap    a                                       ; 1
81         push    _PSBANK                                 ; 2
82         anl     _PSBANK,#0x0F                           ; 3
83         orl     _PSBANK,a                               ; 2
84
85         clr     a                                       ; 1
86         movc    a,@a+dptr                               ; 1
87         pop     _PSBANK                                 ; 2
88         ret                                             ; 1
89     ;
90     ;   pointer to xternal data
91     ;
92  xdataptr$:
93     ; implementation for xram a16-a21 tied to P3
94         mov     _P3,b                                   ; 3
95
96         movx    a,@dptr                                 ; 1
97         ret                                             ; 1
98                                                         ;===
99                                                         ;44 bytes
100      __endasm;
101 }
102
103 #elif defined DSDCC_MODEL_MEDIUM
104
105 void
106 _gptrget (char *gptr) __naked
107 {
108 /* This is the non-banked version with pointers up to 15 bits.
109    Assumes B is free to be used */
110
111     gptr; /* hush the compiler */
112
113     __asm
114     ;
115     ;   depending on the pointer type acc. to SDCCsymt.h
116     ;
117         mov     b,dph                                   ; 3
118         jb      _B_7,codeptr$        ; >0x80 code       ; 3
119         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
120
121         mov     b,r0   ; save r0 independant of regbank ; 2
122         mov     r0,dpl ; use only low order address     ; 2
123
124         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
125     ;
126     ;   Pointer to data space
127     ;
128         mov     a,@r0                                   ; 1
129         mov     r0,b   ; restore r0                     ; 2
130         ret                                             ; 1
131     ;
132     ;   pointer to xternal stack or pdata
133     ;
134  pdataptr$:
135         movx    a,@r0                                   ; 1
136         mov     r0,b   ; restore r0                     ; 2
137         ret                                             ; 1
138     ;
139     ;   pointer to code area, max 15 bits
140     ;
141  codeptr$:
142     ; 0x8000 <= dptr <= 0xFFFF
143     ; no need to AND dph and restore from B if hardware wraps code memory
144         anl     dph,#0x7F                               ; 3
145         clr     a                                       ; 1
146         movc    a,@a+dptr                               ; 1
147         mov     dph,b                                   ; 3
148         ret                                             ; 1
149     ;
150     ;   pointer to xternal data, max 14 bits
151     ;
152  xdataptr$:
153     ; 0 <= dptr <= 0x3FFF
154         movx    a,@dptr                                 ; 1
155         ret                                             ; 1
156                                                         ;===
157                                                         ;35 bytes
158      __endasm;
159 }
160
161 #elif 1
162
163 void
164 _gptrget (char *gptr) __naked
165 {
166 /* This is the new version with pointers up to 16 bits.
167    B cannot be trashed */
168
169     gptr; /* hush the compiler */
170
171     __asm
172     ;
173     ;   depending on the pointer type acc. to SDCCsymt.h
174     ;
175         jb      _B_7,codeptr$        ; >0x80 code       ; 3
176         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
177
178         mov     dph,r0 ; save r0 independant of regbank ; 2
179         mov     r0,dpl ; use only low order address     ; 2
180
181         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
182     ;
183     ;   Pointer to data space
184     ;
185         mov     a,@r0                                   ; 1
186  dataptrrestore$:
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         sjmp    dataptrrestore$                         ; 2
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                                                         ;27 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