Imported Upstream version 2.9.0
[debian/cc1111] / 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 #else
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 #endif
215
216 #ifdef SDCC_ds390
217 /* the  return value is expected to be in acc/_ap, and not in the standard
218  * location dpl/dph. Therefore we choose return type void here: */
219
220 void
221 _gptrgetWord (unsigned *gptr)
222 {
223     gptr; /* hush the compiler */
224
225     __asm
226     ;
227     ;   depending on the pointer type acc. to SDCCsymt.h
228     ;
229         jb      _B_7,00003$           ; >0x80 code
230         jnb     _B_6,00002$           ; <0x40 far
231
232         mov     dph,r0 ; save r0 independant of regbank
233         mov     r0,dpl ; use only low order address
234
235         jb      _B_5,00004$           ; >0x60 pdata
236     ;
237     ;   Pointer to data space
238     ;
239         mov     _ap,@r0
240         inc     r0
241         mov     a,@r0
242         inc     dpl
243         sjmp    00005$
244     ;
245     ;   pointer to xternal data
246     ;
247  00002$:
248         movx    a,@dptr
249         mov     _ap,a
250         inc     dptr
251         movx    a,@dptr
252         sjmp    00006$
253 ;
254 ;   pointer to code area
255 ;
256  00003$:
257         clr     a
258         movc    a,@a+dptr
259         mov     _ap,a
260         clr     a
261         inc     dptr
262         movc    a,@a+dptr
263         sjmp    00006$
264 ;
265 ;   pointer to xternal stack
266 ;
267  00004$:
268         movx    a,@r0
269         mov     _ap,a
270         inc     r0
271         movx    a,@r0
272         inc     dpl
273 ;
274 ;   restore and return
275 ;
276  00005$:
277         mov     r0,dph ; restore r0
278         mov     dph,#0 ; restore dph
279  00006$:
280         xch     a,_ap
281     __endasm;
282
283 }
284
285 #endif