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