Annotation of uae/src/autoconf.c, revision 1.1.1.9

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: /* ROM tag area memory access */
                    220: 
1.1.1.9 ! root      221: static uae_u8 *rtarea;
1.1       root      222: 
1.1.1.4   root      223: static uae_u32 rtarea_lget (uaecptr) REGPARAM;
                    224: static uae_u32 rtarea_wget (uaecptr) REGPARAM;
                    225: static uae_u32 rtarea_bget (uaecptr) REGPARAM;
                    226: static void rtarea_lput (uaecptr, uae_u32) REGPARAM;
                    227: static void rtarea_wput (uaecptr, uae_u32) REGPARAM;
                    228: static void rtarea_bput (uaecptr, uae_u32) REGPARAM;
                    229: static uae_u8 *rtarea_xlate (uaecptr) REGPARAM;
1.1       root      230: 
                    231: addrbank rtarea_bank = {
                    232:     rtarea_lget, rtarea_wget, rtarea_bget,
                    233:     rtarea_lput, rtarea_wput, rtarea_bput,
1.1.1.9 ! root      234:     rtarea_xlate, default_check, NULL
1.1       root      235: };
                    236: 
1.1.1.4   root      237: uae_u8 REGPARAM2 *rtarea_xlate (uaecptr addr)
1.1       root      238: {
                    239:     addr &= 0xFFFF;
                    240:     return rtarea + addr;
                    241: }
                    242: 
1.1.1.4   root      243: uae_u32 REGPARAM2 rtarea_lget (uaecptr addr)
1.1       root      244: {
1.1.1.7   root      245:     special_mem |= S_READ;
1.1       root      246:     addr &= 0xFFFF;
1.1.1.4   root      247:     return (uae_u32)(rtarea_wget (addr) << 16) + rtarea_wget (addr+2);
1.1       root      248: }
                    249: 
1.1.1.4   root      250: uae_u32 REGPARAM2 rtarea_wget (uaecptr addr)
1.1       root      251: {
1.1.1.7   root      252:     special_mem |= S_READ;
1.1       root      253:     addr &= 0xFFFF;
                    254:     return (rtarea[addr]<<8) + rtarea[addr+1];
                    255: }
                    256: 
1.1.1.4   root      257: uae_u32 REGPARAM2 rtarea_bget (uaecptr addr)
1.1       root      258: {
1.1.1.7   root      259:     special_mem |= S_READ;
1.1       root      260:     addr &= 0xFFFF;
                    261:     return rtarea[addr];
                    262: }
                    263: 
1.1.1.7   root      264: void REGPARAM2 rtarea_lput (uaecptr addr, uae_u32 value)
                    265: {
                    266:     special_mem |= S_WRITE;
                    267: }
                    268: 
                    269: void REGPARAM2 rtarea_wput (uaecptr addr, uae_u32 value)
                    270: {
                    271:     special_mem |= S_WRITE;
                    272: }
                    273: 
                    274: void REGPARAM2 rtarea_bput (uaecptr addr, uae_u32 value)
                    275: {
                    276:     special_mem |= S_WRITE;
                    277: }
1.1       root      278: 
1.1.1.3   root      279: static int trace_traps = 1;
1.1       root      280: 
1.1.1.3   root      281: void REGPARAM2 call_calltrap(int func)
1.1       root      282: {
1.1.1.3   root      283:     uae_u32 retval = 0;
                    284:     int has_retval = (trapmode[func] & TRAPFLAG_NO_RETVAL) == 0;
                    285:     int implicit_rts = (trapmode[func] & TRAPFLAG_DORET) != 0;
1.1       root      286: 
1.1.1.4   root      287:     if (*trapstr[func] != 0 && trace_traps)
                    288:        write_log ("TRAP: %s\n", trapstr[func]);
1.1.1.2   root      289: 
                    290:     /* For monitoring only? */
1.1.1.3   root      291:     if (traps[func] == NULL) {
                    292:        m68k_setpc(trapoldfunc[func]);
                    293:        fill_prefetch_0 ();
1.1.1.2   root      294:        return;
                    295:     }
1.1.1.3   root      296: 
                    297:     if (func < max_trap) {
                    298:        if (trapmode[func] & TRAPFLAG_EXTRA_STACK) {
                    299:            execute_fn_on_extra_stack(traps[func], has_retval);
                    300:            return;
                    301:        }
                    302:        retval = (*traps[func])();
1.1.1.4   root      303:     } else
                    304:        write_log ("illegal emulator trap\n");
                    305: 
1.1.1.3   root      306:     if (has_retval)
1.1.1.2   root      307:        m68k_dreg(regs, 0) = retval;
1.1.1.3   root      308:     if (implicit_rts) {
                    309:        m68k_do_rts ();
                    310:        fill_prefetch_0 ();
1.1.1.2   root      311:     }
1.1       root      312: }
                    313: 
1.1.1.3   root      314: /* @$%&� compiler bugs */
                    315: static volatile int four = 4;
                    316: 
1.1.1.4   root      317: uaecptr libemu_InstallFunctionFlags (TrapFunction f, uaecptr libbase, int offset,
                    318:                                     int flags, const char *tracename)
1.1       root      319: {
1.1.1.3   root      320:     int i;
                    321:     uaecptr retval;
1.1.1.4   root      322:     uaecptr execbase = get_long (four);
1.1.1.3   root      323:     int trnum;
                    324:     uaecptr addr = here();
1.1.1.4   root      325:     calltrap (trnum = deftrap2 (f, flags, tracename));
                    326:     dw (RTS);
1.1.1.3   root      327: 
                    328:     m68k_areg(regs, 1) = libbase;
                    329:     m68k_areg(regs, 0) = offset;
                    330:     m68k_dreg(regs, 0) = addr;
1.1.1.4   root      331:     retval = CallLib (execbase, -420);
1.1.1.3   root      332: 
                    333:     trapoldfunc[trnum] = retval;
                    334: #if 0
                    335:     for (i = 0; i < n_libpatches; i++) {
                    336:        if (libpatches[i].libbase == libbase)
                    337:            break;
1.1       root      338:     }
1.1.1.3   root      339:     if (i == n_libpatches) {
                    340:        int j;
                    341:        libpatches[i].libbase = libbase;
                    342:        for (j = 0; j < 300; j++)
                    343:            libpatches[i].functions[j] = NULL;
                    344:        n_libpatches++;
1.1       root      345:     }
1.1.1.3   root      346:     libpatches[i].functions[-offset/6] = f;
                    347: #endif
                    348:     return retval;
1.1       root      349: }
                    350: 
                    351: /* some quick & dirty code to fill in the rt area and save me a lot of
                    352:  * scratch paper
                    353:  */
                    354: 
                    355: static int rt_addr = 0;
                    356: static int rt_straddr = 0xFF00 - 2;
                    357: 
1.1.1.4   root      358: uae_u32 addr (int ptr)
1.1       root      359: {
1.1.1.3   root      360:     return (uae_u32)ptr + 0x00F00000;
1.1       root      361: }
                    362: 
1.1.1.4   root      363: void db (uae_u8 data)
1.1.1.3   root      364: {
                    365:     rtarea[rt_addr++] = data;
                    366: }
                    367: 
1.1.1.4   root      368: void dw (uae_u16 data)
1.1       root      369: {
                    370:     rtarea[rt_addr++] = data >> 8;
                    371:     rtarea[rt_addr++] = data;
                    372: }
                    373: 
1.1.1.4   root      374: void dl (uae_u32 data)
1.1       root      375: {
                    376:     rtarea[rt_addr++] = data >> 24;
                    377:     rtarea[rt_addr++] = data >> 16;
                    378:     rtarea[rt_addr++] = data >> 8;
                    379:     rtarea[rt_addr++] = data;
                    380: }
                    381: 
                    382: /* store strings starting at the end of the rt area and working
                    383:  * backward.  store pointer at current address
                    384:  */
                    385: 
1.1.1.4   root      386: uae_u32 ds (char *str)
1.1       root      387: {
1.1.1.4   root      388:     int len = strlen (str) + 1;
1.1       root      389: 
                    390:     rt_straddr -= len;
1.1.1.4   root      391:     strcpy ((char *)rtarea + rt_straddr, str);
1.1.1.3   root      392: 
1.1.1.4   root      393:     return addr (rt_straddr);
1.1       root      394: }
                    395: 
1.1.1.4   root      396: void calltrap (uae_u32 n)
1.1       root      397: {
1.1.1.4   root      398:     dw (0xA000 + n);
1.1       root      399: }
                    400: 
1.1.1.4   root      401: void org (uae_u32 a)
1.1       root      402: {
                    403:     rt_addr = a - 0x00F00000;
                    404: }
                    405: 
1.1.1.4   root      406: uae_u32 here (void)
1.1       root      407: {
1.1.1.4   root      408:     return addr (rt_addr);
1.1       root      409: }
                    410: 
1.1.1.4   root      411: int deftrap2 (TrapFunction func, int mode, const char *str)
1.1       root      412: {
                    413:     int num = max_trap++;
                    414:     traps[num] = func;
1.1.1.3   root      415:     trapstr[num] = str;
                    416:     trapmode[num] = mode;
1.1       root      417:     return num;
                    418: }
                    419: 
1.1.1.4   root      420: int deftrap (TrapFunction func)
1.1       root      421: {
1.1.1.4   root      422:     return deftrap2 (func, 0, "");
1.1       root      423: }
                    424: 
1.1.1.4   root      425: void align (int b)
1.1       root      426: {
1.1.1.4   root      427:     rt_addr = (rt_addr + b - 1) & ~(b - 1);
1.1       root      428: }
                    429: 
1.1.1.3   root      430: static uae_u32 nullfunc(void)
1.1       root      431: {
1.1.1.4   root      432:     fprintf (stderr, "Null function called\n");
1.1.1.3   root      433:     return 0;
1.1.1.2   root      434: }
                    435: 
1.1.1.3   root      436: static uae_u32 getchipmemsize (void)
1.1.1.2   root      437: {
1.1.1.4   root      438:     return allocated_chipmem;
1.1       root      439: }
                    440: 
1.1.1.8   root      441: static uae_u32 uae_puts (void)
                    442: {
                    443:     puts (get_real_address (m68k_areg (regs, 0)));
                    444:     return 0;
                    445: }
                    446: 
1.1.1.9 ! root      447: static void rtarea_init_mem (void)
        !           448: {
        !           449:     rtarea = mapped_malloc (0x10000, "rtarea");
        !           450:     if (!rtarea) {
        !           451:        write_log ("virtual memory exhausted (rtarea)!\n");
        !           452:        abort ();
        !           453:     }
        !           454:     rtarea_bank.baseaddr = rtarea;
        !           455: }
        !           456: 
1.1.1.4   root      457: void rtarea_init (void)
1.1       root      458: {
1.1.1.3   root      459:     uae_u32 a;
1.1       root      460:     char uaever[100];
1.1.1.9 ! root      461: 
        !           462:     rtarea_init_mem ();
        !           463: 
1.1.1.4   root      464:     sprintf (uaever, "uae-%d.%d.%d", UAEMAJOR, UAEMINOR, UAESUBREV);
1.1       root      465: 
1.1.1.4   root      466:     EXPANSION_uaeversion = ds (uaever);
                    467:     EXPANSION_explibname = ds ("expansion.library");
                    468:     EXPANSION_doslibname = ds ("dos.library");
                    469:     EXPANSION_uaedevname = ds ("uae.device");
1.1       root      470: 
1.1.1.4   root      471:     deftrap (NULL); /* Generic emulator trap */
1.1       root      472:     lasttrap = 0;
                    473: 
1.1.1.4   root      474:     EXPANSION_nullfunc = here ();
                    475:     calltrap (deftrap (nullfunc));
                    476:     dw (RTS);
1.1.1.3   root      477: 
1.1.1.2   root      478:     a = here();
1.1       root      479:     /* Standard "return from 68k mode" trap */
1.1.1.4   root      480:     org (0xF0FF00);
1.1.1.3   root      481:     calltrap (deftrap2 (m68k_mode_return, TRAPFLAG_NO_RETVAL, ""));
                    482: 
                    483:     org (0xF0FF80);
                    484:     calltrap (deftrap2 (getchipmemsize, TRAPFLAG_DORET, ""));
1.1.1.8   root      485: 
                    486:     org (0xF0FF10);
                    487:     calltrap (deftrap2 (uae_puts, TRAPFLAG_NO_RETVAL, ""));
                    488:     dw (RTS);
                    489: 
1.1.1.4   root      490:     org (a);
1.1.1.2   root      491: 
1.1.1.3   root      492:     filesys_install_code ();
                    493: }
1.1.1.2   root      494: 
1.1.1.3   root      495: volatile int uae_int_requested = 0;
1.1.1.2   root      496: 
1.1.1.3   root      497: void set_uae_int_flag (void)
                    498: {
                    499:     rtarea[0xFFFB] = uae_int_requested;
1.1.1.2   root      500: }
                    501: 
                    502: void rtarea_setup(void)
                    503: {
                    504: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.