fe30a84591cd9f8fa58668a50a08e809c2d8b1c1
[fw/sdcc] / device / lib / _gptrput.c
1 /*-------------------------------------------------------------------------
2
3   _gptrput.c :- put 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 #if defined DSDCC_MODEL_HUGE
38 void
39 _gptrput (char *gptr, char c) _naked
40 {
41 /* This is the banked version with pointers up to 22 bits.
42    B cannot be trashed */
43
44     gptr; c; /* hush the compiler */
45
46     _asm
47     ;
48     ;   depending on the pointer type according to SDCCsymt.h
49     ;
50         jb      _B_7,codeptr$        ; >0x80 code       ; 3
51         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
52
53         mov     dph,r0 ; save r0 independant of regbank ; 2
54         mov     r0,dpl ; use only low order address     ; 2
55
56         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
57     ;
58     ;   store into near/idata space
59     ;
60         mov     @r0,a                                   ; 1
61  dataptrrestore$:
62         mov     r0,dph ; restore r0                     ; 2
63         mov     dph,#0 ; restore dph                    ; 2
64
65  codeptr$:
66         ret                                             ; 1
67     ;
68     ;   store into external stack/pdata space
69     ;
70  pdataptr$:
71         movx    @r0,a                                   ; 1
72         sjmp    dataptrrestore$                         ; 2
73         mov     r0,dph ; restore r0                     ; 2
74         mov     dph,#0 ; restore dph                    ; 2
75         ret                                             ; 1
76     ;
77     ;   store into far space
78     ;
79  xdataptr$:
80         mov     _P3,b                                   ; 3
81
82         movx    @dptr,a                                 ; 1
83         ret                                             ; 1
84
85                                                         ;===
86                                                         ;27 bytes
87 _endasm;
88 }
89
90 #elif defined DSDCC_MODEL_MEDIUM
91
92 void
93 _gptrput (char *gptr, char c) _naked
94 {
95 /* This is the non-banked version with pointers up to 14 bits.
96    Assumes B is free to be used */
97
98     gptr; c; /* hush the compiler */
99
100     _asm
101     ;
102     ;   depending on the pointer type according to SDCCsymt.h
103     ;
104         mov     b,dph                                   ; 3
105         jb      _B_7,codeptr$        ; >0x80 code       ; 3
106         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
107
108         mov     b,r0   ; save r0 independant of regbank ; 2
109         mov     r0,dpl ; use only low order address     ; 2
110
111         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
112     ;
113     ;   store into near/idata space
114     ;
115         mov     @r0,a                                   ; 1
116  dataptrrestore$:
117         mov     r0,b   ; restore r0                     ; 2
118
119  codeptr$:
120         ret                                             ; 1
121     ;
122     ;   store into external stack/pdata space
123     ;
124  pdataptr$:
125         movx    @r0,a                                   ; 1
126         sjmp    dataptrrestore$                         ; 2
127     ;
128     ;   store into far space, max 14 bits
129     ;
130  xdataptr$:
131     ; 0 <= dptr <= 0x3FFF
132         movx    @dptr,a                                 ; 1
133         ret                                             ; 1
134                                                         ;===
135                                                         ;25 bytes
136     _endasm;
137 }
138
139 #elif 1
140
141 void
142 _gptrput (char *gptr, char c) _naked
143 {
144 /* This is the new version with pointers up to 16 bits.
145    B cannot be trashed */
146
147     gptr; c; /* hush the compiler */
148
149     _asm
150     ;
151     ;   depending on the pointer type according to SDCCsymt.h
152     ;
153         jb      _B_7,codeptr$        ; >0x80 code       ; 3
154         jnb     _B_6,xdataptr$       ; <0x40 far        ; 3
155
156         mov     dph,r0 ; save r0 independant of regbank ; 2
157         mov     r0,dpl ; use only low order address     ; 2
158
159         jb      _B_5,pdataptr$       ; >0x60 pdata      ; 3
160     ;
161     ;   store into near/idata space
162     ;
163         mov     @r0,a                                   ; 1
164  dataptrrestore$:
165         mov     r0,dph ; restore r0                     ; 2
166         mov     dph,#0 ; restore dph                    ; 2
167
168  codeptr$:
169         ret                                             ; 1
170     ;
171     ;   store into external stack/pdata space
172     ;
173  pdataptr$:
174         movx    @r0,a                                   ; 1
175         sjmp    dataptrrestore$                         ; 2
176     ;
177     ;   store into far space
178     ;
179  xdataptr$:
180         movx    @dptr,a                                 ; 1
181         ret                                             ; 1
182
183                                                         ;===
184                                                         ;24 bytes
185 _endasm;
186 }
187
188 #else
189
190 void
191 _gptrput (char *gptr, char c) _naked
192 {
193 /* This is the old version with pointers up to 16 bits. */
194
195     gptr; c; /* hush the compiler */
196
197     _asm
198         ar0 = 0x00
199         push     acc                                    ; 2
200     ;
201     ;   depending on the pointer type acc. to SDCCsymt.h
202     ;
203         mov     a,b                                     ; 2
204         jz      00001$  ; 0 near                        ; 2
205         dec     a                                       ; 1
206         jz      00002$  ; 1 far                         ; 2
207         dec     a                                       ; 1
208         jz      00003$  ; 2 code                        ; 2
209         dec     a                                       ; 1
210         jz      00004$  ; 3 pdata                       ; 2
211         dec     a       ; 4 skip generic pointer        ; 1
212         dec     a                                       ; 1
213         jz      00001$  ; 5 idata                       ; 2
214
215  00003$:
216         pop     acc    ; do nothing                     ; 2
217         ret                                             ; 1
218 ;
219 ;       store into near space
220 ;
221  00001$:
222         pop     acc                                     ; 2
223         push    ar0                                     ; 2
224         mov     r0,dpl                                  ; 2
225         mov     @r0,a                                   ; 1
226         pop     ar0                                     ; 2
227         ret                                             ; 1
228
229  00002$:
230         pop     acc                                     ; 2
231         movx    @dptr,a                                 ; 1
232         ret                                             ; 1
233
234  00004$:
235 #if USE_PDATA_PAGING_REGISTER
236         pop     acc
237         mov     dph,__XPAGE     ; __XPAGE (usually p2) holds high byte for pdata access
238         movx    @dptr,a
239 #else
240         pop     acc                                     ; 2
241         push    ar0                                     ; 2
242         mov     r0,dpl                                  ; 2
243         movx    @r0,a                                   ; 1
244         pop     ar0                                     ; 2
245 #endif
246         ret                                             ; 1
247                                                         ;===
248                                                         ;46 bytes
249 _endasm;
250 }
251 #endif
252
253 #ifdef SDCC_ds390
254
255 #if 1
256
257 void
258 _gptrputWord ()
259 {
260     _asm
261     ;
262     ;   depending on the pointer type acc. to SDCCsymt.h
263     ;
264         jb      _B_7,00013$           ; >0x80 code
265         jnb     _B_6,00012$           ; <0x40 far
266
267         mov     dph,r0 ; save r0 independant of regbank
268         mov     r0,dpl ; use only low order address
269
270         jb      _B_5,00014$           ; >0x60 pdata
271 ;
272 ;       store into near space
273 ;
274         mov     @r0,_ap
275         inc     r0
276         mov     @r0,a
277         sjmp    00015$
278 ;
279 ;       store into far space
280 ;
281  00012$:
282         xch     a,_ap
283         movx    @dptr,a
284         inc     dptr
285         xch     a,_ap
286         movx    @dptr,a
287         sjmp    00016$
288 ;
289 ;       store into code space
290 ;
291  00013$:
292         inc     dptr   ; do nothing
293         sjmp    00016$
294 ;
295 ;       store into xstack space
296 ;
297  00014$:
298         xch     a,_ap
299         movx    @r0,a
300         inc     r0
301         xch     a,_ap
302         movx    @r0, a
303  00015$:
304         mov     dpl,r0
305         mov     r0,dph ; restore r0
306         mov     dph,#0 ; restore dph
307  00016$:
308     _endasm;
309 }
310
311 #else
312
313 void
314 _gptrputWord ()
315 {
316     _asm
317         push     acc
318     ;
319     ;   depending on the pointer type acc. to SDCCsymt.h
320     ;
321         mov     a,b
322         jz      00011$  ; 0 near
323         dec     a
324         jz      00012$  ; 1 far
325         dec     a
326         jz      00013$  ; 2 code
327         dec     a
328         jz      00014$  ; 3 pdata
329         dec     a       ; 4 skip generic pointer
330         dec     a
331         jz      00011$  ; 5 idata
332         pop     acc
333         sjmp    00016$
334 ;
335 ;       store into near space
336 ;
337  00011$:
338         pop     acc
339         push    ar0
340         mov     r0,dpl
341         mov     @r0,_ap
342         inc     r0
343         mov     @r0,a
344         sjmp    00015$
345
346  00012$:
347         mov     a, _ap
348         movx    @dptr,a
349         inc     dptr
350         pop     acc
351         movx    @dptr,a
352         sjmp    00016$
353
354  00013$:
355         pop     acc    ; do nothing
356         sjmp    00016$
357
358  00014$:
359         pop     acc
360         push    ar0
361         mov     r0,dpl
362         xch     a,_ap
363         movx    @r0,a
364         inc     r0
365         xch     a,_ap
366         movx    @r0, a
367  00015$:
368         inc     dptr
369         pop     ar0
370  00016$:
371     _endasm;
372 }
373 #endif
374
375 #endif