|
|
1.1.1.3 root 1: /*
1.1 root 2: * UAE - The Un*x Amiga Emulator
1.1.1.3 root 3: *
1.1 root 4: * AutoConfig devices
5: *
1.1.1.2 root 6: * Copyright 1995, 1996 Bernd Schmidt
7: * Copyright 1996 Ed Hanway
1.1 root 8: */
9:
10: #include "sysconfig.h"
11: #include "sysdeps.h"
12:
13: #include "config.h"
14: #include "options.h"
1.1.1.3 root 15: #include "uae.h"
1.1 root 16: #include "memory.h"
17: #include "custom.h"
18: #include "newcpu.h"
1.1.1.3 root 19: #include "compiler.h"
1.1 root 20: #include "autoconf.h"
1.1.1.3 root 21: #include "osdep/exectasks.h"
1.1 root 22:
1.1.1.2 root 23: /* We'll need a lot of these. */
24: #define MAX_TRAPS 4096
25: static TrapFunction traps[MAX_TRAPS];
1.1.1.3 root 26: static int trapmode[MAX_TRAPS];
1.1.1.2 root 27: static const char *trapstr[MAX_TRAPS];
1.1.1.3 root 28: static uaecptr trapoldfunc[MAX_TRAPS];
1.1.1.2 root 29:
30: static int max_trap = 0;
31: int lasttrap;
32:
1.1.1.3 root 33: /* Stack management */
34:
35: /* The mechanism for doing m68k calls from native code is as follows:
36: *
37: * m68k code executes, stack is main
38: * calltrap to execute_fn_on_extra_stack. new stack is allocated
39: * do_stack_magic is called for the first time
40: * current context is saved with setjmp [0]
41: * transfer_control is done
42: * native code executes on new stack
43: * native code calls call_m68k
44: * setjmp saves current context [1]
45: * longjmp back to execute_fn_on_extra_stack [0]
46: * pointer to new stack is saved on m68k stack. m68k return address set
47: * to 0xF0FF00. m68k PC set to called function
48: * m68k function executes, stack is main
49: * m68k function returns to 0xF0FF00
50: * calltrap to m68k_mode_return
51: * do_stack_magic is called again
52: * current context is saved again with setjmp [0]
53: * this time, transfer_control is _not_ done, instead a longjmp[1]
54: * to the previously saved context
55: * native code executes again on temp stack
56: * native function returns to stack_stub
57: * longjmp[0] back to old context
58: * back again!
59: *
60: * A bearded man enters the room, carrying a bowl of spaghetti.
61: */
62:
63: /* This _shouldn't_ crash with a stack size of 4096, but it does...
64: * might be a bug */
65: #ifndef EXTRA_STACK_SIZE
66: #define EXTRA_STACK_SIZE 65536
67: #endif
68:
69: static void *extra_stack_list = NULL;
70:
1.1.1.4 root 71: static void *get_extra_stack (void)
1.1.1.3 root 72: {
73: void *s = extra_stack_list;
74: if (s)
75: extra_stack_list = *(void **)s;
76: if (!s)
77: s = xmalloc (EXTRA_STACK_SIZE);
78: return s;
1.1 root 79: }
80:
1.1.1.3 root 81: static void free_extra_stack (void *s)
1.1 root 82: {
1.1.1.3 root 83: *(void **)s = extra_stack_list;
84: extra_stack_list = s;
1.1 root 85: }
86:
1.1.1.3 root 87: static void stack_stub (void *s, TrapFunction f, uae_u32 *retval)
1.1 root 88: {
1.1.1.3 root 89: #ifdef CAN_DO_STACK_MAGIC
90: /*printf("in stack_stub: %p %p %p %x\n", s, f, retval, (int)*retval);*/
1.1.1.4 root 91: *retval = f ();
1.1.1.3 root 92: /*write_log ("returning from stack_stub\n");*/
1.1.1.4 root 93: longjmp (((jmp_buf *)s)[0], 1);
1.1.1.3 root 94: #endif
95: }
1.1 root 96:
1.1.1.3 root 97: static void *current_extra_stack = NULL;
98: static uaecptr m68k_calladdr;
99:
1.1.1.4 root 100: static void do_stack_magic (TrapFunction f, void *s, int has_retval)
1.1.1.3 root 101: {
102: #ifdef CAN_DO_STACK_MAGIC
103: uaecptr a7;
104: jmp_buf *j = (jmp_buf *)s;
1.1.1.4 root 105: switch (setjmp (j[0])) {
1.1.1.3 root 106: case 0:
107: /* Returning directly */
108: current_extra_stack = s;
109: if (has_retval == -1) {
110: /*write_log ("finishing m68k mode return\n");*/
1.1.1.4 root 111: longjmp (j[1], 1);
1.1.1.3 root 112: }
113: /*write_log ("calling native function\n");*/
114: transfer_control (s, EXTRA_STACK_SIZE, stack_stub, f, has_retval);
115: /* not reached */
1.1.1.4 root 116: abort ();
1.1.1.3 root 117:
118: case 1:
119: /*write_log ("native function complete\n");*/
120: /* Returning normally. */
121: if (stack_has_retval (s, EXTRA_STACK_SIZE))
122: m68k_dreg (regs, 0) = get_retval_from_stack (s, EXTRA_STACK_SIZE);
123: free_extra_stack (s);
124: break;
125:
126: case 2:
127: /* Returning to do a m68k call. We're now back on the main stack. */
128: a7 = m68k_areg(regs, 7) -= (sizeof (void *) + 7) & ~3;
129: /* Save stack to restore */
130: *((void **)get_real_address (a7 + 4)) = s;
131: /* Save special return address: this address contains a
132: * calltrap that will longjmp to the right stack. */
133: put_long (m68k_areg (regs, 7), 0xF0FF00);
134: m68k_setpc (m68k_calladdr);
135: fill_prefetch_0 ();
136: /*write_log ("native function calls m68k\n");*/
137: break;
1.1 root 138: }
1.1.1.3 root 139: current_extra_stack = 0;
140: #endif
1.1 root 141: }
142:
1.1.1.4 root 143: static uae_u32 execute_fn_on_extra_stack (TrapFunction f, int has_retval)
1.1 root 144: {
1.1.1.3 root 145: #ifdef CAN_DO_STACK_MAGIC
1.1.1.4 root 146: void *s = get_extra_stack ();
1.1.1.3 root 147: do_stack_magic (f, s, has_retval);
148: #endif
149: return 0;
150: }
1.1.1.2 root 151:
1.1.1.4 root 152: static uae_u32 m68k_mode_return (void)
1.1.1.3 root 153: {
154: #ifdef CAN_DO_STACK_MAGIC
155: uaecptr a7 = m68k_areg(regs, 7);
156: void *s = *(void **)get_real_address(a7);
157: m68k_areg(regs, 7) += (sizeof (void *) + 3) & ~3;
158: /*write_log ("doing m68k mode return\n");*/
159: do_stack_magic (NULL, s, -1);
160: #endif
161: return 0;
162: }
1.1.1.2 root 163:
1.1.1.4 root 164: static uae_u32 call_m68k (uaecptr addr, int saveregs)
1.1.1.3 root 165: {
166: volatile uae_u32 retval = 0;
167: volatile int do_save = saveregs;
168: if (current_extra_stack == NULL)
169: abort();
170: #ifdef CAN_DO_STACK_MAGIC
171: {
172: volatile struct regstruct saved_regs;
173: jmp_buf *j = (jmp_buf *)current_extra_stack;
174:
175: if (do_save)
176: saved_regs = regs;
177: m68k_calladdr = addr;
178: switch (setjmp(j[1])) {
179: case 0:
180: /*write_log ("doing call\n");*/
181: /* Returning directly: now switch to main stack and do the call */
182: longjmp (j[0], 2);
183: case 1:
184: /*write_log ("returning from call\n");*/
185: retval = m68k_dreg (regs, 0);
186: if (do_save)
187: regs = saved_regs;
188: /* Returning after the call. */
1.1 root 189: break;
1.1.1.3 root 190: }
1.1 root 191: }
1.1.1.3 root 192: #endif
1.1 root 193: return retval;
194: }
195:
1.1.1.4 root 196: uae_u32 CallLib (uaecptr base, uae_s16 offset)
1.1 root 197: {
1.1.1.3 root 198: int i;
199: uaecptr olda6 = m68k_areg(regs, 6);
200: uae_u32 retval;
201: #if 0
202: for (i = 0; i < n_libpatches; i++) {
203: if (libpatches[i].libbase == base && libpatches[i].functions[-offset/6] != NULL)
204: return (*libpatches[i].functions[-offset/6])();
205: }
206: #endif
207: m68k_areg(regs, 6) = base;
208: retval = call_m68k(base + offset, 1);
209: m68k_areg(regs, 6) = olda6;
210: return retval;
1.1 root 211: }
212:
213: /* Commonly used autoconfig strings */
214:
1.1.1.3 root 215: uaecptr EXPANSION_explibname, EXPANSION_doslibname, EXPANSION_uaeversion;
216: uaecptr EXPANSION_uaedevname, EXPANSION_explibbase = 0, EXPANSION_haveV36;
217: uaecptr EXPANSION_bootcode, EXPANSION_nullfunc;
1.1 root 218:
219: static int current_deviceno = 0;
220:
1.1.1.4 root 221: void reset_uaedevices (void)
222: {
223: current_deviceno = 0;
224: }
225:
226: int get_new_device (char **devname, uaecptr *devname_amiga)
1.1 root 227: {
228: char buffer[80];
1.1.1.3 root 229:
1.1.1.5 root 230: sprintf (buffer, "DH%d", current_deviceno);
1.1 root 231:
1.1.1.4 root 232: *devname_amiga = ds (*devname = my_strdup (buffer));
1.1 root 233: return current_deviceno++;
234: }
235:
236: /* ROM tag area memory access */
237:
1.1.1.3 root 238: static uae_u8 rtarea[65536];
1.1 root 239:
1.1.1.4 root 240: static uae_u32 rtarea_lget (uaecptr) REGPARAM;
241: static uae_u32 rtarea_wget (uaecptr) REGPARAM;
242: static uae_u32 rtarea_bget (uaecptr) REGPARAM;
243: static void rtarea_lput (uaecptr, uae_u32) REGPARAM;
244: static void rtarea_wput (uaecptr, uae_u32) REGPARAM;
245: static void rtarea_bput (uaecptr, uae_u32) REGPARAM;
246: static uae_u8 *rtarea_xlate (uaecptr) REGPARAM;
1.1 root 247:
248: addrbank rtarea_bank = {
249: rtarea_lget, rtarea_wget, rtarea_bget,
250: rtarea_lput, rtarea_wput, rtarea_bput,
251: rtarea_xlate, default_check
252: };
253:
1.1.1.4 root 254: uae_u8 REGPARAM2 *rtarea_xlate (uaecptr addr)
1.1 root 255: {
256: addr &= 0xFFFF;
257: return rtarea + addr;
258: }
259:
1.1.1.4 root 260: uae_u32 REGPARAM2 rtarea_lget (uaecptr addr)
1.1 root 261: {
1.1.1.7 ! root 262: special_mem |= S_READ;
1.1 root 263: addr &= 0xFFFF;
1.1.1.4 root 264: return (uae_u32)(rtarea_wget (addr) << 16) + rtarea_wget (addr+2);
1.1 root 265: }
266:
1.1.1.4 root 267: uae_u32 REGPARAM2 rtarea_wget (uaecptr addr)
1.1 root 268: {
1.1.1.7 ! root 269: special_mem |= S_READ;
1.1 root 270: addr &= 0xFFFF;
271: return (rtarea[addr]<<8) + rtarea[addr+1];
272: }
273:
1.1.1.4 root 274: uae_u32 REGPARAM2 rtarea_bget (uaecptr addr)
1.1 root 275: {
1.1.1.7 ! root 276: special_mem |= S_READ;
1.1 root 277: addr &= 0xFFFF;
278: return rtarea[addr];
279: }
280:
1.1.1.7 ! root 281: void REGPARAM2 rtarea_lput (uaecptr addr, uae_u32 value)
! 282: {
! 283: special_mem |= S_WRITE;
! 284: }
! 285:
! 286: void REGPARAM2 rtarea_wput (uaecptr addr, uae_u32 value)
! 287: {
! 288: special_mem |= S_WRITE;
! 289: }
! 290:
! 291: void REGPARAM2 rtarea_bput (uaecptr addr, uae_u32 value)
! 292: {
! 293: special_mem |= S_WRITE;
! 294: }
1.1 root 295:
1.1.1.3 root 296: static int trace_traps = 1;
1.1 root 297:
1.1.1.3 root 298: void REGPARAM2 call_calltrap(int func)
1.1 root 299: {
1.1.1.3 root 300: uae_u32 retval = 0;
301: int has_retval = (trapmode[func] & TRAPFLAG_NO_RETVAL) == 0;
302: int implicit_rts = (trapmode[func] & TRAPFLAG_DORET) != 0;
1.1 root 303:
1.1.1.4 root 304: if (*trapstr[func] != 0 && trace_traps)
305: write_log ("TRAP: %s\n", trapstr[func]);
1.1.1.2 root 306:
307: /* For monitoring only? */
1.1.1.3 root 308: if (traps[func] == NULL) {
309: m68k_setpc(trapoldfunc[func]);
310: fill_prefetch_0 ();
1.1.1.2 root 311: return;
312: }
1.1.1.3 root 313:
314: if (func < max_trap) {
315: if (trapmode[func] & TRAPFLAG_EXTRA_STACK) {
316: execute_fn_on_extra_stack(traps[func], has_retval);
317: return;
318: }
319: retval = (*traps[func])();
1.1.1.4 root 320: } else
321: write_log ("illegal emulator trap\n");
322:
1.1.1.3 root 323: if (has_retval)
1.1.1.2 root 324: m68k_dreg(regs, 0) = retval;
1.1.1.3 root 325: if (implicit_rts) {
326: m68k_do_rts ();
327: fill_prefetch_0 ();
1.1.1.2 root 328: }
1.1 root 329: }
330:
1.1.1.3 root 331: /* @$%&� compiler bugs */
332: static volatile int four = 4;
333:
1.1.1.4 root 334: uaecptr libemu_InstallFunctionFlags (TrapFunction f, uaecptr libbase, int offset,
335: int flags, const char *tracename)
1.1 root 336: {
1.1.1.3 root 337: int i;
338: uaecptr retval;
1.1.1.4 root 339: uaecptr execbase = get_long (four);
1.1.1.3 root 340: int trnum;
341: uaecptr addr = here();
1.1.1.4 root 342: calltrap (trnum = deftrap2 (f, flags, tracename));
343: dw (RTS);
1.1.1.3 root 344:
345: m68k_areg(regs, 1) = libbase;
346: m68k_areg(regs, 0) = offset;
347: m68k_dreg(regs, 0) = addr;
1.1.1.4 root 348: retval = CallLib (execbase, -420);
1.1.1.3 root 349:
350: trapoldfunc[trnum] = retval;
351: #if 0
352: for (i = 0; i < n_libpatches; i++) {
353: if (libpatches[i].libbase == libbase)
354: break;
1.1 root 355: }
1.1.1.3 root 356: if (i == n_libpatches) {
357: int j;
358: libpatches[i].libbase = libbase;
359: for (j = 0; j < 300; j++)
360: libpatches[i].functions[j] = NULL;
361: n_libpatches++;
1.1 root 362: }
1.1.1.3 root 363: libpatches[i].functions[-offset/6] = f;
364: #endif
365: return retval;
1.1 root 366: }
367:
368: /* some quick & dirty code to fill in the rt area and save me a lot of
369: * scratch paper
370: */
371:
372: static int rt_addr = 0;
373: static int rt_straddr = 0xFF00 - 2;
374:
1.1.1.4 root 375: uae_u32 addr (int ptr)
1.1 root 376: {
1.1.1.3 root 377: return (uae_u32)ptr + 0x00F00000;
1.1 root 378: }
379:
1.1.1.4 root 380: void db (uae_u8 data)
1.1.1.3 root 381: {
382: rtarea[rt_addr++] = data;
383: }
384:
1.1.1.4 root 385: void dw (uae_u16 data)
1.1 root 386: {
387: rtarea[rt_addr++] = data >> 8;
388: rtarea[rt_addr++] = data;
389: }
390:
1.1.1.4 root 391: void dl (uae_u32 data)
1.1 root 392: {
393: rtarea[rt_addr++] = data >> 24;
394: rtarea[rt_addr++] = data >> 16;
395: rtarea[rt_addr++] = data >> 8;
396: rtarea[rt_addr++] = data;
397: }
398:
399: /* store strings starting at the end of the rt area and working
400: * backward. store pointer at current address
401: */
402:
1.1.1.4 root 403: uae_u32 ds (char *str)
1.1 root 404: {
1.1.1.4 root 405: int len = strlen (str) + 1;
1.1 root 406:
407: rt_straddr -= len;
1.1.1.4 root 408: strcpy ((char *)rtarea + rt_straddr, str);
1.1.1.3 root 409:
1.1.1.4 root 410: return addr (rt_straddr);
1.1 root 411: }
412:
1.1.1.4 root 413: void calltrap (uae_u32 n)
1.1 root 414: {
1.1.1.4 root 415: dw (0xA000 + n);
1.1 root 416: }
417:
1.1.1.4 root 418: void org (uae_u32 a)
1.1 root 419: {
420: rt_addr = a - 0x00F00000;
421: }
422:
1.1.1.4 root 423: uae_u32 here (void)
1.1 root 424: {
1.1.1.4 root 425: return addr (rt_addr);
1.1 root 426: }
427:
1.1.1.4 root 428: int deftrap2 (TrapFunction func, int mode, const char *str)
1.1 root 429: {
430: int num = max_trap++;
431: traps[num] = func;
1.1.1.3 root 432: trapstr[num] = str;
433: trapmode[num] = mode;
1.1 root 434: return num;
435: }
436:
1.1.1.4 root 437: int deftrap (TrapFunction func)
1.1 root 438: {
1.1.1.4 root 439: return deftrap2 (func, 0, "");
1.1 root 440: }
441:
1.1.1.4 root 442: void align (int b)
1.1 root 443: {
1.1.1.4 root 444: rt_addr = (rt_addr + b - 1) & ~(b - 1);
1.1 root 445: }
446:
1.1.1.3 root 447: static uae_u32 nullfunc(void)
1.1 root 448: {
1.1.1.4 root 449: fprintf (stderr, "Null function called\n");
1.1.1.3 root 450: return 0;
1.1.1.2 root 451: }
452:
1.1.1.3 root 453: static uae_u32 getchipmemsize (void)
1.1.1.2 root 454: {
1.1.1.4 root 455: return allocated_chipmem;
1.1 root 456: }
457:
1.1.1.4 root 458: void rtarea_init (void)
1.1 root 459: {
1.1.1.3 root 460: uae_u32 a;
1.1 root 461: char uaever[100];
1.1.1.4 root 462: sprintf (uaever, "uae-%d.%d.%d", UAEMAJOR, UAEMINOR, UAESUBREV);
1.1 root 463:
1.1.1.4 root 464: EXPANSION_uaeversion = ds (uaever);
465: EXPANSION_explibname = ds ("expansion.library");
466: EXPANSION_doslibname = ds ("dos.library");
467: EXPANSION_uaedevname = ds ("uae.device");
1.1 root 468:
1.1.1.4 root 469: deftrap (NULL); /* Generic emulator trap */
1.1 root 470: lasttrap = 0;
471:
1.1.1.4 root 472: EXPANSION_nullfunc = here ();
473: calltrap (deftrap (nullfunc));
474: dw (RTS);
1.1.1.3 root 475:
1.1.1.2 root 476: a = here();
1.1 root 477: /* Standard "return from 68k mode" trap */
1.1.1.4 root 478: org (0xF0FF00);
1.1.1.3 root 479: calltrap (deftrap2 (m68k_mode_return, TRAPFLAG_NO_RETVAL, ""));
480:
481: org (0xF0FF80);
482: calltrap (deftrap2 (getchipmemsize, TRAPFLAG_DORET, ""));
1.1.1.4 root 483: org (a);
1.1.1.2 root 484:
1.1.1.3 root 485: filesys_install_code ();
486: }
1.1.1.2 root 487:
1.1.1.3 root 488: volatile int uae_int_requested = 0;
1.1.1.2 root 489:
1.1.1.3 root 490: void set_uae_int_flag (void)
491: {
492: rtarea[0xFFFB] = uae_int_requested;
1.1.1.2 root 493: }
494:
495: void rtarea_setup(void)
496: {
497: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.