* device/lib/_gptrget.c,
[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 /* the  return value is expected to be in acc, and not in the standard
34  * location dpl. Therefore we choose return type void here: */
35 void
36 _gptrget (char *gptr)
37 {
38     gptr; /* hush the compiler */
39
40     _asm
41     ;   save values passed
42     ;
43     ;   depending on the pointer type acc. to SDCCsymt.h
44     ;
45         mov     a,b
46         jz      00001$  ; 0 near
47         dec     a
48         jz      00002$  ; 1 far
49         dec     a
50         jz      00003$  ; 2 code
51         dec     a
52         jz      00004$  ; 3 pdata
53         dec     a       ; 4 skip generic pointer
54         dec     a
55         jz      00001$  ; 5 idata
56     ;
57     ;   any other value for type
58     ;   return xFF
59         mov     a,#0xff
60         sjmp    00005$
61     ;
62     ;   Pointer to data space
63     ;
64  00001$:
65         push    ar0
66         ;
67         mov     r0,dpl     ; use only low order address
68         mov     a,@r0
69         ;
70         pop     ar0
71         ;
72         sjmp    00005$
73     ;
74     ;   pointer to xternal data
75     ;
76  00002$:
77         movx    a,@dptr
78         sjmp    00005$
79 ;
80 ;   pointer to code area
81 ;
82  00003$:
83         ; clr     a  is already 0
84         movc    a,@a+dptr
85         sjmp    00005$
86 ;
87 ;   pointer to xternal stack or pdata
88 ;
89  00004$:
90 #if USE_PDATA_PAGING_REGISTER
91         mov     dph,__XPAGE     ; __XPAGE (usually p2) holds high byte for pdata access
92         movx    a,@dptr
93 #else
94         push    ar0
95         mov     r0,dpl
96         movx    a,@r0
97         pop     ar0
98 #endif
99
100 ;
101 ;   return
102 ;
103  00005$:
104      _endasm ;
105 }
106
107 #ifdef SDCC_ds390
108 /* the  return value is expected to be in acc/_ap, and not in the standard
109  * location dpl/dph. Therefore we choose return type void here: */
110 void
111 _gptrgetWord (unsigned *gptr)
112 {
113     gptr; /* hush the compiler */
114
115     _asm
116     ;
117     ;   depending on the pointer type acc. to SDCCsymt.h
118     ;
119         mov     a,b
120         jz      00001$  ; 0 near
121         dec     a
122         jz      00002$  ; 1 far
123         dec     a
124         jz      00003$  ; 2 code
125         dec     a
126         jz      00004$  ; 3 pdata
127         dec     a       ; 4 skip generic pointer
128         dec     a
129         jz      00001$  ; 5 idata
130     ;
131     ;   any other value for type
132     ;   return xFF
133         mov     a,#0xff
134         sjmp    00006$
135     ;
136     ;   Pointer to data space
137     ;
138  00001$:
139         push    ar0
140         mov     r0,dpl     ; use only low order address
141         mov     _ap,@r0
142         inc     r0
143         mov     a,@r0
144         inc     dpl
145         sjmp    00005$
146     ;
147     ;   pointer to xternal data
148     ;
149  00002$:
150         movx    a,@dptr
151         mov     _ap,a
152         inc     dptr
153         movx    a,@dptr
154         sjmp    00006$
155 ;
156 ;   pointer to code area
157 ;
158  00003$:
159         ; clr     a  is already 0
160         movc    a,@a+dptr
161         mov     _ap,a
162         clr     a
163         inc     dptr
164         movc    a,@a+dptr
165         sjmp    00006$
166 ;
167 ;   pointer to xternal stack
168 ;
169  00004$:
170         push    ar0
171         mov     r0,dpl
172         movx    a,@r0
173         mov     _ap,a
174         inc     r0
175         movx    a,@r0
176         inc     dpl
177 ;
178 ;   restore and return
179 ;
180 00005$:
181     pop ar0
182 00006$:
183      _endasm ;
184
185 }
186 #endif