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