Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / ds400 / ds400rom.c
1 // Interface to the DS80C400 ROM functions. Largely based on code released
2 // by Dallas, hence the following copyright.
3
4 // ---------------------------------------------------------------------------
5 //  Copyright (C) 2003 Dallas Semiconductor Corporation, All Rights Reserved.
6 // 
7 //  Permission is hereby granted, free of charge, to any person obtaining a
8 //  copy of this software and associated documentation files (the "Software"),
9 //  to deal in the Software without restriction, including without limitation
10 //  the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 //  and/or sell copies of the Software, and to permit persons to whom the
12 //  Software is furnished to do so, subject to the following conditions:
13 //
14 //  The above copyright notice and this permission notice shall be included
15 //  in all copies or substantial portions of the Software.
16 //
17 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 //  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 //  MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 //  IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
21 //  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 //  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 //  OTHER DEALINGS IN THE SOFTWARE.
24 //
25 //  Except as contained in this notice, the name of Dallas Semiconductor
26 //  shall not be used except as stated in the Dallas Semiconductor
27 //  Branding Policy.
28 // ---------------------------------------------------------------------------
29
30 #include <stdio.h>
31 #include <ds400rom.h>
32
33 // ROM globals, taken from startup400.a51
34 __data unsigned char __at (0x68) DSS_wos_crit_count;
35 __data unsigned int  __at (0x6b) DSS_timerReload;
36 __data unsigned char __at (0x6d) DSS_curr_pc[3];
37 __data unsigned char __at (0x72) DSS_sched[3];
38 __data unsigned char __at (0x74) DSS_ms_count[5];
39 __data unsigned char __at (0x7b) DSS_hb_chandle[5];
40     
41 // Register bank 3 equates.
42 #define R0_B3     0x18
43 #define R1_B3     0x19
44 #define R2_B3     0x1A
45 #define R3_B3     0x1B
46 #define R4_B3     0x1C
47 #define R5_B3     0x1D
48 #define R6_B3     0x1E
49 #define R7_B3     0x1F
50
51
52 // The top of the redirect function table in RAM.
53 #define CALL_TABLE_TOP  256
54     
55 // The bank the ROM is stored in.  Should be 0FFh for production
56 // 400's.  Change this value when running with a debug ROM.
57
58 #define ROM_BANK        0xFF
59
60 // The address of the ROM export table is stored
61 // at (ROM_BANK << 16) | ROM_EXPORTTABLE_OFFS
62
63 #define ROM_EXPORTTABLE_OFFS    2
64
65 //
66 // Each entry in the ROM export table is 3 bytes.
67 //
68 #define ROMXT_ENTRYSIZE      3
69
70 //
71 // The number of functions in the ROM export table is stored
72 // first in the export table.
73 //
74 #define ROMXT_NUMFUNCTIONS     (0 * ROMXT_ENTRYSIZE)
75
76 //
77 // ROM EXPORT TABLE FUNCTIONS (denoted with ROMXT)
78 //
79
80 // UTILITY functions 
81 #define ROMXT_CRC16                     (1 * ROMXT_ENTRYSIZE) //
82 #define ROMXT_MEM_CLEAR_16              (2 * ROMXT_ENTRYSIZE) //
83 #define ROMXT_MEM_COPY_16               (3 * ROMXT_ENTRYSIZE) //
84 #define ROMXT_MEM_COMPARE               (4 * ROMXT_ENTRYSIZE) //
85 #define ROMXT_ADD_DPTR1_16              (5 * ROMXT_ENTRYSIZE) // not implemented for C compiler
86 #define ROMXT_ADD_DPTR2_16              (6 * ROMXT_ENTRYSIZE) // not implemented for C compiler
87 #define ROMXT_SUB_DPTR1_16              (7 * ROMXT_ENTRYSIZE) // not implemented for C compiler
88 #define ROMXT_SUB_DPTR2_16              (8 * ROMXT_ENTRYSIZE) // not implemented for C compiler
89 #define ROMXT_GETPSEUDORANDOM           (9 * ROMXT_ENTRYSIZE) //
90
91 // MEMORY MGR
92 #define ROMXT_KERNELMALLOC              (10 * ROMXT_ENTRYSIZE) // not exposed
93 #define ROMXT_KERNELFREE                (11 * ROMXT_ENTRYSIZE) // not exposed
94 #define ROMXT_MM_MALLOC                 (12 * ROMXT_ENTRYSIZE) // exposed as redirected function
95 #define ROMXT_MM_MALLOC_DIRTY           (13 * ROMXT_ENTRYSIZE) // exposed as redirected function
96 #define ROMXT_MM_FREE                   (14 * ROMXT_ENTRYSIZE) // exposed as redirected function
97 #define ROMXT_MM_DEREF                  (15 * ROMXT_ENTRYSIZE) // exposed as redirected function
98 #define ROMXT_GETFREERAM                (16 * ROMXT_ENTRYSIZE) // exposed as redirected function
99
100 // SOCKET functions
101 #define ROMXT_ROM_SOCKET                (17 * ROMXT_ENTRYSIZE) //
102 #define ROMXT_ROM_CLOSESOCKET           (18 * ROMXT_ENTRYSIZE) //
103 #define ROMXT_ROM_SENDTO                (19 * ROMXT_ENTRYSIZE) //
104 #define ROMXT_ROM_RECVFROM              (20 * ROMXT_ENTRYSIZE) //
105 #define ROMXT_ROM_CONNECT               (21 * ROMXT_ENTRYSIZE) //
106 #define ROMXT_ROM_BIND                  (22 * ROMXT_ENTRYSIZE) //
107 #define ROMXT_ROM_LISTEN                (23 * ROMXT_ENTRYSIZE) //
108 #define ROMXT_ROM_ACCEPT                (24 * ROMXT_ENTRYSIZE) //
109 #define ROMXT_ROM_RECV                  (25 * ROMXT_ENTRYSIZE) //
110 #define ROMXT_ROM_SEND                  (26 * ROMXT_ENTRYSIZE) //
111 #define ROMXT_ROM_GETSOCKOPT            (27 * ROMXT_ENTRYSIZE) //
112 #define ROMXT_ROM_SETSOCKOPT            (28 * ROMXT_ENTRYSIZE) //
113 #define ROMXT_ROM_GETSOCKNAME           (29 * ROMXT_ENTRYSIZE) //
114 #define ROMXT_ROM_GETPEERNAME           (30 * ROMXT_ENTRYSIZE) //
115 #define ROMXT_ROM_CLEANUP               (31 * ROMXT_ENTRYSIZE) //
116 #define ROMXT_ROM_AVAIL                 (32 * ROMXT_ENTRYSIZE) //
117 #define ROMXT_ROM_JOIN                  (33 * ROMXT_ENTRYSIZE) //
118 #define ROMXT_ROM_LEAVE                 (34 * ROMXT_ENTRYSIZE) //
119 #define ROMXT_ROM_PING                  (35 * ROMXT_ENTRYSIZE) //
120 #define ROMXT_ROM_GETNETWORKPARAMS      (36 * ROMXT_ENTRYSIZE) //
121 #define ROMXT_ROM_SETNETWORKPARAMS      (37 * ROMXT_ENTRYSIZE) //
122 #define ROMXT_ROM_GETIPV6PARAMS         (38 * ROMXT_ENTRYSIZE) //
123 #define ROMXT_ROM_GETETHERNETSTATUS     (39 * ROMXT_ENTRYSIZE) //
124 #define ROMXT_ROM_GETTFTPSERVER         (40 * ROMXT_ENTRYSIZE) //
125 #define ROMXT_ROM_SETTFTPSERVER         (41 * ROMXT_ENTRYSIZE) //
126 #define ROMXT_ETH_PROCESSINTERRUPT      (42 * ROMXT_ENTRYSIZE) // not implemented for C compiler
127 #define ROMXT_ARP_GENERATEREQUEST       (43 * ROMXT_ENTRYSIZE) // not implemented for C compiler
128 #define ROMXT_NET_ETH0_MAC_ID           (44 * ROMXT_ENTRYSIZE) //
129
130 // DHCP functions
131 #define ROMXT_DHCP_INIT                 (45 * ROMXT_ENTRYSIZE) //
132 #define ROMXT_DHCP_SETUP                (46 * ROMXT_ENTRYSIZE) // not implemented for C compiler
133 #define ROMXT_DHCP_STARTUP              (47 * ROMXT_ENTRYSIZE) // not implemented for C compiler
134 #define ROMXT_DHCP_RUN                  (48 * ROMXT_ENTRYSIZE) // not implemented for C compiler
135 #define ROMXT_DHCP_STATUS               (49 * ROMXT_ENTRYSIZE) //
136 #define ROMXT_DHCP_STOP                 (50 * ROMXT_ENTRYSIZE) //
137 #define ROMXT_DHCPNOTIFY                (51 * ROMXT_ENTRYSIZE) // empty redirect stub, not implemented
138
139 // TFTP functions
140 #define ROMXT_TFTP_INIT                 (52 * ROMXT_ENTRYSIZE) //
141 #define ROMXT_TFTP_FIRST                (53 * ROMXT_ENTRYSIZE) //
142 #define ROMXT_TFTP_NEXT                 (54 * ROMXT_ENTRYSIZE) //
143 #define ROMXT_TFTP_MSG                  (55 * ROMXT_ENTRYSIZE) //
144
145 // SCHEDULER functions
146 #define ROMXT_TASK_GENESIS              (56 * ROMXT_ENTRYSIZE) //
147 #define ROMXT_TASK_GETCURRENT           (57 * ROMXT_ENTRYSIZE) //
148 #define ROMXT_TASK_GETPRIORITY          (58 * ROMXT_ENTRYSIZE) //
149 #define ROMXT_TASK_SETPRIORITY          (59 * ROMXT_ENTRYSIZE) //
150 #define ROMXT_TASK_FORK                 (60 * ROMXT_ENTRYSIZE) //
151 #define ROMXT_TASK_KILL                 (61 * ROMXT_ENTRYSIZE) //
152 #define ROMXT_TASK_SUSPEND              (62 * ROMXT_ENTRYSIZE) //
153 #define ROMXT_TASK_SLEEP                (63 * ROMXT_ENTRYSIZE) //
154 #define ROMXT_TASK_SIGNAL               (64 * ROMXT_ENTRYSIZE) //
155 #define ROMXT_ROM_TASK_SWITCH_IN        (65 * ROMXT_ENTRYSIZE) // empty redirect stub, not implemented
156 #define ROMXT_ROM_TASK_SWITCH_OUT       (66 * ROMXT_ENTRYSIZE) // empty redirect stub, not implemented
157 #define ROMXT_ENTERCRITSECTION          (67 * ROMXT_ENTRYSIZE) //
158 #define ROMXT_LEAVECRITSECTION          (68 * ROMXT_ENTRYSIZE) //
159
160 // INIT functions
161 #define ROMXT_ROM_INIT                  (69 * ROMXT_ENTRYSIZE) //
162 #define ROMXT_ROM_COPYIVT               (70 * ROMXT_ENTRYSIZE) //
163 #define ROMXT_ROM_REDIRECT_INIT         (71 * ROMXT_ENTRYSIZE) //
164 #define ROMXT_MM_INIT                   (72 * ROMXT_ENTRYSIZE) //
165 #define ROMXT_KM_INIT                   (73 * ROMXT_ENTRYSIZE) //
166 #define ROMXT_OW_INIT                   (74 * ROMXT_ENTRYSIZE) //
167 #define ROMXT_NETWORK_INIT              (75 * ROMXT_ENTRYSIZE) //
168 #define ROMXT_ETH_INIT                  (76 * ROMXT_ENTRYSIZE) //
169 #define ROMXT_INIT_SOCKETS              (77 * ROMXT_ENTRYSIZE) //
170 #define ROMXT_TICK_INIT                 (78 * ROMXT_ENTRYSIZE) //
171
172 // Timer Interrupt vectors
173 #define ROMXT_WOS_TICK                  (79 * ROMXT_ENTRYSIZE) // not implemented for C compiler
174 #define ROMXT_BLOB                      (80 * ROMXT_ENTRYSIZE) // not implemented for C compiler
175
176 // Maintenance functions
177 #define ROMXT_WOS_IOPOLL                (81 * ROMXT_ENTRYSIZE) // not implemented for C compiler
178 #define ROMXT_IP_PROCESSRECEIVEQUEUES   (82 * ROMXT_ENTRYSIZE) // not implemented for C compiler
179 #define ROMXT_IP_PROCESSOUTPUT          (83 * ROMXT_ENTRYSIZE) // not implemented for C compiler
180 #define ROMXT_TCP_RETRYTOP              (84 * ROMXT_ENTRYSIZE) // not implemented for C compiler
181 #define ROMXT_ETH_PROCESSOUTPUT         (85 * ROMXT_ENTRYSIZE) // not implemented for C compiler
182 #define ROMXT_IGMP_GROUPMAINTAINENCE    (86 * ROMXT_ENTRYSIZE) // not implemented for C compiler
183 #define ROMXT_IP6_PROCESSRECEIVEQUEUES  (87 * ROMXT_ENTRYSIZE) // not implemented for C compiler
184 #define ROMXT_IP6_PROCESSOUTPUT         (88 * ROMXT_ENTRYSIZE) // not implemented for C compiler
185 #define ROMXT_PARAMBUFFER               (89 * ROMXT_ENTRYSIZE) // not implemented for C compiler
186 #define ROMXT_RAM_TOP                   (90 * ROMXT_ENTRYSIZE) // not implemented for C compiler
187 #define ROMXT_BOOT_MEMBEGIN             (91 * ROMXT_ENTRYSIZE) // not implemented for C compiler
188 #define ROMXT_BOOT_MEMEND               (92 * ROMXT_ENTRYSIZE) // not implemented for C compiler
189
190 // 1-Wire
191 #define ROMXT_OWM_FIRST                 (93 * ROMXT_ENTRYSIZE) //
192 #define ROMXT_OWM_NEXT                  (94 * ROMXT_ENTRYSIZE) //
193 #define ROMXT_OWM_RESET                 (95 * ROMXT_ENTRYSIZE) //
194 #define ROMXT_OWM_BYTE                  (96 * ROMXT_ENTRYSIZE) //
195 #define ROMXT_OWM_SEARCH                (97 * ROMXT_ENTRYSIZE) // not implemented for C compiler
196 #define ROMXT_OW_ROMID                  (98 * ROMXT_ENTRYSIZE) //
197
198 // Misc, extras, late additions
199 #define ROMXT_AUTOBAUD                  (99 * ROMXT_ENTRYSIZE)
200 #define ROMXT_TFTP_CLOSE                (100 * ROMXT_ENTRYSIZE)
201
202
203 #define ROMRT_ENTRYSIZE  3
204
205 //
206 // ROM REDIRECT TABLE FUNCTIONS (denoted with ROMRT)
207 //
208 #define ROMRT_KERNELMALLOC                  ( 1 * ROMRT_ENTRYSIZE)
209 #define ROMRT_KERNELFREE                    ( 2 * ROMRT_ENTRYSIZE)
210 #define ROMRT_MALLOC                        ( 3 * ROMRT_ENTRYSIZE)
211 #define ROMRT_FREE                          ( 4 * ROMRT_ENTRYSIZE)
212 #define ROMRT_MALLOCDIRTY                   ( 5 * ROMRT_ENTRYSIZE)
213 #define ROMRT_DEREF                         ( 6 * ROMRT_ENTRYSIZE)
214 #define ROMRT_GETFREERAM                    ( 7 * ROMRT_ENTRYSIZE)
215 #define ROMRT_GETTIMEMILLIS                 ( 8 * ROMRT_ENTRYSIZE)
216 #define ROMRT_GETTHREADID                   ( 9 * ROMRT_ENTRYSIZE)
217 #define ROMRT_THREADRESUME                  (10 * ROMRT_ENTRYSIZE)
218 #define ROMRT_THREADIOSLEEP                 (11 * ROMRT_ENTRYSIZE)
219 #define ROMRT_THREADIOSLEEPNC               (12 * ROMRT_ENTRYSIZE)
220 #define ROMRT_THREADSAVE                    (13 * ROMRT_ENTRYSIZE)
221 #define ROMRT_THREADRESTORE                 (14 * ROMRT_ENTRYSIZE)
222 #define ROMRT_SLEEP                         (15 * ROMRT_ENTRYSIZE)
223 #define ROMRT_GETTASKID                     (16 * ROMRT_ENTRYSIZE)
224 #define ROMRT_INFOSENDCHAR                  (17 * ROMRT_ENTRYSIZE)
225 #define ROMRT_IP_COMPUTECHECKSUM_SOFTWARE   (18 * ROMRT_ENTRYSIZE)
226 #define ROMRT_0                             (19 * ROMRT_ENTRYSIZE) // undefined
227 #define ROMRT_DHCPNOTIFY                    (20 * ROMRT_ENTRYSIZE)
228 #define ROMRT_ROM_TASK_CREATE               (21 * ROMRT_ENTRYSIZE)
229 #define ROMRT_ROM_TASK_DUPLICATE            (22 * ROMRT_ENTRYSIZE)
230 #define ROMRT_ROM_TASK_DESTROY              (23 * ROMRT_ENTRYSIZE)
231 #define ROMRT_ROM_TASK_SWITCH_IN            (24 * ROMRT_ENTRYSIZE)
232 #define ROMRT_ROM_TASK_SWITCH_OUT           (25 * ROMRT_ENTRYSIZE)
233 #define ROMRT_OWIP_READCONFIG               (26 * ROMRT_ENTRYSIZE)
234 #define ROMRT_SETMACID                      (27 * ROMRT_ENTRYSIZE)
235 #define ROMRT_UNDEREF                       (28 * ROMRT_ENTRYSIZE)
236
237
238 #define GETC            \
239     clr  a              \
240     movc a, @a+dptr
241     
242
243 // expects function number in R6_B3 (low byte) & R7_B3 (high byte)
244 void _romcall(void) __naked
245 {
246 __asm    
247       push  dpx                               ; dptr0 preserved here
248       push  dph
249       push  dpl
250
251       ; point to the address of the table               
252       mov   dptr, #(ROM_BANK << 16 | ROM_EXPORTTABLE_OFFS)
253
254       push  acc                               ; acc preserved here
255       push  b                                 ; b preserved here
256       inc   dptr
257       GETC                                    ; get the address of the table
258       push  acc
259       inc   dptr
260       GETC
261       add   a, R6_B3                          ; add function offset to the table
262       mov   dpl, a
263       pop   acc
264       addc  a, R7_B3
265       mov   dph, a
266
267       ;
268       ; dpx is the same, all in the same bank
269       ;
270       inc   dptr                              ; get the target address of the function we want
271       GETC
272       mov   b, a
273       inc   dptr
274       GETC
275       mov   R3_B3, a
276       mov   R4_B3, b
277       mov   R5_B3, dpx                        ; high byte does not change
278       pop   b                                 ; b restored here
279       pop   acc                               ; acc restored here
280       pop   dpl                               ; dptr0 preserved here
281       pop   dph
282       pop   dpx
283       push  R3_B3                             ; push the target address
284       push  R4_B3
285       push  R5_B3
286       ret                                     ; this is not a ret, it is a call!
287         
288       ; the called function ends with a ret which will return to our original caller.
289 __endasm ;
290 }
291
292 // expects function number in R6_B3 (low byte) & R7_B3 (high byte)
293 void _romredirect(void) __naked
294 {
295 __asm
296       push  dpx
297       push  dph
298       push  dpl
299       push  acc
300       ; dptr = CALL_TABLE_TOP + function offset.
301       mov   a, #(CALL_TABLE_TOP & 0xff)
302       add   a, R6_B3                          ; add function offset to the table
303       mov   dpl, a
304       mov   a, #((CALL_TABLE_TOP >> 8) & 0xff)
305       addc  a, R7_B3
306       mov   dph, a
307       mov   dpx, #((CALL_TABLE_TOP >> 16) & 0xff)
308       movx  a, @dptr                      ; read high byte
309       mov   R5_B3, a
310       inc   dptr
311       movx  a, @dptr                      ; read mid byte
312       mov   R4_B3, a
313       inc   dptr
314       movx  a, @dptr                      ; read low byte
315       mov   R3_B3, a
316       pop   acc                 ; restore acc and dptr
317       pop   dpl
318       pop   dph
319       pop   dpx
320       push  R3_B3               ; push low byte of target address
321       push  R4_B3
322       push  R5_B3               ; push high byte of target address
323       ret                       ; this is not a ret, it is a call!
324
325       ; the called function ends with a ret which will return to our original caller.
326 __endasm;       
327 }
328
329
330 // This macro is invalid for the standard C preprocessor, since it
331 // includes a hash character in the expansion, hence the SDCC specific
332 // pragma.
333 #pragma sdcc_hash +
334 #define ROMCALL(x) \
335         mov     R6_B3, #(x & 0xff)              \
336         mov     R7_B3, #((x >> 8) & 0xff)       \
337         lcall   __romcall
338
339 #define ROMREDIRECT(x) \
340         mov     R6_B3, #(x & 0xff)              \
341         mov     R7_B3, #((x >> 8) & 0xff)       \
342         lcall   __romredirect
343
344
345 // init_rom: the ds400 ROM_INIT ROM function.
346 unsigned char init_rom(void __xdata *loMem,
347                        void __xdata *hiMem) __naked
348 {    
349     // shut compiler up about unused parameters.
350     loMem;
351     hiMem;
352     
353 __asm
354         ; load params.
355         ; loMem is already in DPTR.
356         mov     r2, dpx
357         mov     r1, dph
358         mov     r0, dpl
359         ; hiMem is in _init_rom_PARM_2
360         mov     dptr, #_init_rom_PARM_2
361         mov     r5, dpx
362         mov     r4, dph
363         mov     r3, dpl
364
365         ROMCALL(ROMXT_ROM_INIT)
366
367         ; result is in acc, move to dpl for C convention.
368         mov     dpl, a
369         ret
370 __endasm        ;
371 }
372
373 // DSS_gettimemillis: note that the ROM actually returns 5 bytes of time,
374 // we're discarding the high byte here.
375 unsigned long task_gettimemillis_long(void) __naked
376 {
377 __asm    
378     ; no parameters to load. 
379     ROMREDIRECT(ROMRT_GETTIMEMILLIS)
380    ; results in r4 - r0, return in DPTR/B
381    mov dpl, r0
382    mov dph, r1
383    mov dpx, r2
384    mov b, r3
385    ret
386 __endasm;
387 }
388
389 unsigned char task_getthreadID(void) __naked
390 {
391 __asm    
392     ; no parameters to load. 
393     ROMREDIRECT(ROMRT_GETTHREADID)
394    ; results in acc, return in dpl
395    mov dpl, a
396    ret
397 __endasm;    
398 }
399
400 unsigned int task_gettickreload(void)
401 {
402     return DSS_timerReload;
403 }
404
405 void task_settickreload(unsigned int rl)
406 {
407     DSS_timerReload = rl;
408 }
409