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