home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / sim / z8k / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  10.0 KB  |  533 lines

  1. /* support routines for interpreted instructions
  2.    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of Z8KSIM
  5.  
  6. Z8KSIM is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Z8KSIM 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.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Z8KSIM; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <ansidecl.h>
  21. #include "sysdep.h"
  22. #include "tm.h"
  23. #include "sim.h"
  24. #include "mem.h"
  25. #include <sys/times.h>
  26. #include <sys/param.h>
  27.  
  28. #include "syscall.h"
  29.  
  30. static int get_now PARAMS((void)); 
  31. static int now_persec PARAMS((void)); 
  32. static int put_long PARAMS((sim_state_type *context, int ptr, int value)); 
  33. static int put_short PARAMS((sim_state_type *context, int ptr, int value)); 
  34.  
  35. int sim_z8001_mode;
  36.  
  37. static int
  38. get_now ()
  39. {
  40.   struct tms b;
  41.  
  42.   times (&b);
  43.   return b.tms_utime + b.tms_stime;
  44. }
  45.  
  46. static int
  47. now_persec ()
  48. {
  49.   return HZ;
  50. }
  51.  
  52.  
  53. /* #define LOG /* define this to print instruction use counts */
  54.  
  55. #ifdef __GNUC__
  56. #define INLINE  __inline__
  57. #include "inlines.h"
  58. #else
  59. #include "inlines.h"
  60. #endif
  61.  
  62. /* This holds the entire cpu context */
  63. static sim_state_type the_state;
  64.  
  65. int
  66. fail (context, dummy)
  67.      sim_state_type *context;
  68.      int dummy;
  69. {
  70.   context->exception = SIM_BAD_INST;
  71.   return 1;
  72. }
  73.  
  74. void
  75. sfop_bad1 (context)
  76.      sim_state_type *context;
  77. {
  78.   context->exception = SIM_BAD_INST;
  79. }
  80.  
  81. void
  82. bfop_bad1 (context)
  83.      sim_state_type *context;
  84. {
  85.   context->exception = SIM_BAD_INST;
  86. }
  87.  
  88. void
  89. fop_bad (context)
  90.      sim_state_type *context;
  91. {
  92.   context->exception = SIM_BAD_INST;
  93. }
  94.  
  95. /* Table of bit counts for all byte values */
  96. char the_parity[256] =
  97. {
  98.   0, 1, 1, 2, 1, 2, 2, 3,
  99.   1, 2, 2, 3, 2, 3, 3, 4,
  100.   1, 2, 2, 3, 2, 3, 3, 4,
  101.   2, 3, 3, 4, 3, 4, 4, 5,
  102.   1, 2, 2, 3, 2, 3, 3, 4,
  103.   2, 3, 3, 4, 3, 4, 4, 5,
  104.   2, 3, 3, 4, 3, 4, 4, 5,
  105.   3, 4, 4, 5, 4, 5, 5, 6,
  106.   1, 2, 2, 3, 2, 3, 3, 4,
  107.   2, 3, 3, 4, 3, 4, 4, 5,
  108.   2, 3, 3, 4, 3, 4, 4, 5,
  109.   3, 4, 4, 5, 4, 5, 5, 6,
  110.   2, 3, 3, 4, 3, 4, 4, 5,
  111.   3, 4, 4, 5, 4, 5, 5, 6,
  112.   3, 4, 4, 5, 4, 5, 5, 6,
  113.   4, 5, 5, 6, 5, 6, 6, 7,
  114.   1, 2, 2, 3, 2, 3, 3, 4,
  115.   2, 3, 3, 4, 3, 4, 4, 5,
  116.   2, 3, 3, 4, 3, 4, 4, 5,
  117.   3, 4, 4, 5, 4, 5, 5, 6,
  118.   2, 3, 3, 4, 3, 4, 4, 5,
  119.   3, 4, 4, 5, 4, 5, 5, 6,
  120.   3, 4, 4, 5, 4, 5, 5, 6,
  121.   4, 5, 5, 6, 5, 6, 6, 7,
  122.   2, 3, 3, 4, 3, 4, 4, 5,
  123.   3, 4, 4, 5, 4, 5, 5, 6,
  124.   3, 4, 4, 5, 4, 5, 5, 6,
  125.   4, 5, 5, 6, 5, 6, 6, 7,
  126.   3, 4, 4, 5, 4, 5, 5, 6,
  127.   4, 5, 5, 6, 5, 6, 6, 7,
  128.   4, 5, 5, 6, 5, 6, 6, 7,
  129.   5, 6, 6, 7, 6, 7, 7, 8};
  130.  
  131.  
  132. int read ();
  133. int write ();
  134. int open ();
  135. int close ();
  136. int open ();
  137. int close ();
  138. int creat ();
  139. int link ();
  140. int fstat ();
  141.  
  142. static int
  143. put_short (context, ptr, value)
  144. sim_state_type *context;
  145.      int ptr;
  146.      int value;
  147. {
  148.   put_word_mem_da (context, ptr, value);
  149.   return ptr + 2;
  150. }
  151.  
  152. static int
  153. put_long (context, ptr, value)
  154.      sim_state_type *context;
  155.      int ptr;
  156.      int value;
  157. {
  158.   put_long_mem_da (context, ptr, value);
  159.   return ptr + 4;
  160. }
  161.  
  162. #define aptr(x) ((sitoptr(x)) + (char *)(context->memory))
  163. static int args[3];
  164. static int arg_index;
  165. /* Translate a z8k system call into a host system call */
  166. void
  167. support_call (context, sc)
  168.      sim_state_type *context;
  169.      int sc;
  170. {
  171.   extern int errno;
  172.   int ret;
  173.   int retnext = 0;
  174.   int fd;
  175.  
  176.   int olderrno = errno;
  177.   
  178.   errno = 0;
  179.   switch (sc)
  180.   {
  181.    case SYS_ARG:
  182.     args[arg_index++] = context->regs[0].word << 16  | context->regs[1].word;
  183.     break;
  184.    case SYS_exit:
  185.     context->exception = SIM_DONE;
  186.     arg_index = 0;
  187.     break;
  188.    case SYS_close:
  189.     ret = close ((int)(args[0]));
  190.     arg_index = 0;
  191.     break;
  192.    case SYS_creat:
  193.     ret = creat (aptr(args[0]), args[1]);
  194.     arg_index = 0;
  195.     break;
  196.    case SYS_isatty:
  197.     ret = isatty (args[0]);
  198.     arg_index = 0;
  199.     break;
  200.    case SYS_open:
  201.     ret = open (aptr(args[0]), args[1], args[2]);
  202.     arg_index = 0;
  203.     break;
  204.    case SYS_lseek:
  205.     ret = lseek (args[0], (off_t)args[1], args[2]);
  206.     arg_index = 0;
  207.     break;
  208.    case SYS_read:
  209.     ret = read (args[0],aptr(args[1]), args[2]);
  210.     arg_index = 0;
  211.     break;
  212.    case SYS_write:
  213.     ret = write (args[0], aptr(args[1]), args[2]);
  214.     arg_index = 0;
  215.     break;
  216.    case SYS_time:
  217.    {
  218.      int dst = args[0];
  219.  
  220.      ret = time (0);
  221.      if (dst)
  222.      {
  223.        put_long_mem_da (context, sitoptr(dst), ret);
  224.      }
  225.      retnext = ret;
  226.      ret = retnext >> 16;
  227.      arg_index = 0;
  228.    }
  229.     break;
  230.    case SYS_fstat:
  231.    {
  232.      int buf;
  233.      struct stat host_stat;
  234.      fd = args[0];
  235.      buf = ptrtosi(args[1]);
  236.      ret = fstat (fd, &host_stat);
  237.      buf = put_short (context, buf, host_stat.st_dev);
  238.      buf = put_long (context, buf, host_stat.st_ino);
  239.      buf = put_short (context, buf, host_stat.st_mode);
  240.      buf = put_short (context, buf, host_stat.st_nlink);
  241.      buf = put_short (context, buf, host_stat.st_uid);
  242.      buf = put_short (context, buf, host_stat.st_uid);
  243.      buf = put_short (context, buf, host_stat.st_rdev);
  244.      buf = put_long (context, buf, host_stat.st_size);
  245.      buf = put_long (context, buf, host_stat.st_atime);
  246.      arg_index = 0;
  247.    }
  248.     break;
  249.    default:
  250.    case SYS_link:
  251.     context->exception = SIM_BAD_SYSCALL;
  252.     arg_index = 0;
  253.     break;
  254.   }
  255.   context->regs[2].word = ret;
  256.   context->regs[3].word = retnext;
  257.   context->regs[5].word = errno;
  258.   errno = olderrno;
  259. }
  260.  
  261. #undef get_word_mem_da
  262.  
  263. int
  264. get_word_mem_da (context, addr)
  265.      sim_state_type *context;
  266.      int addr;
  267. {
  268.   return ((context)->memory[(addr) >> 1]);
  269. }
  270.  
  271. #undef get_word_reg
  272. int
  273. get_word_reg (context, reg)
  274.      sim_state_type *context;
  275.      int reg;
  276. {
  277.   return context->regs[reg].word;
  278. }
  279.  
  280. #ifdef LOG
  281. int log[64 * 1024];
  282.  
  283. #endif
  284.  
  285. void
  286. tm_store_register (regno, value)
  287.      int regno;
  288.      int value;
  289. {
  290.   switch (regno)
  291.     {
  292.     case REG_PC:
  293.       the_state.sometimes_pc = value;
  294.       break;
  295.  
  296.     default:
  297.       put_word_reg (&the_state, regno, value);
  298.     }
  299. }
  300.  
  301. void
  302. swap_long (buf, val)
  303.      char *buf;
  304.      int val;
  305. {
  306.   buf[0] = val >> 24;
  307.   buf[1] = val >> 16;
  308.   buf[2] = val >> 8;
  309.   buf[3] = val >> 0;
  310. }
  311.  
  312. void
  313. swap_word (buf, val)
  314.      char *buf;
  315.      int val;
  316. {
  317.   buf[0] = val >> 8;
  318.   buf[1] = val >> 0;
  319. }
  320.  
  321. void
  322. tm_fetch_register (regno, buf)
  323.      int regno;
  324.      char *buf;
  325. {
  326.   switch (regno)
  327.   {
  328.    case REG_CYCLES:
  329.     swap_long (buf, the_state.cycles);
  330.     break;
  331.    case REG_INSTS:
  332.     swap_long (buf, the_state.insts);
  333.     break;
  334.    case REG_TIME:
  335.     swap_long (buf, the_state.ticks);
  336.     break;
  337.    case REG_PC:
  338.     swap_long (buf, the_state.sometimes_pc);
  339.     break;
  340.    case REG_SP:
  341.    {
  342.      if (sim_z8001_mode) {
  343.        swap_long(buf, get_long_reg (&the_state, 14));
  344.      }
  345.      else {
  346.        swap_long(buf, get_word_reg(&the_state,15));
  347.      }
  348.    }
  349.     break;
  350.    case REG_FP:
  351.    {
  352.      if (sim_z8001_mode) {
  353.        swap_long(buf, get_long_reg (&the_state, 10));
  354.      }
  355.      else {
  356.        swap_long(buf, get_word_reg(&the_state,10));
  357.      }
  358.    }
  359.     break;
  360.    default:
  361.    {
  362.      swap_word (buf, get_word_reg (&the_state, regno));
  363.    }
  364.   }
  365. }
  366.  
  367. void
  368. tm_resume (step)
  369.      int step;
  370. {
  371.   int now = get_now ();
  372.   struct op_info *p;
  373.   int word;
  374.   int pc;
  375.   extern int (*(sfop_table[]))();
  376.   extern int (*(bfop_table[]))();
  377.   int (*((*table)))();
  378.   sim_state_type *context = &the_state;
  379.  
  380.   if (step)
  381.   {
  382.     context->exception = SIM_SINGLE_STEP;
  383.   }
  384.   else
  385.   {
  386.     context->exception = 0;
  387.   }
  388.  
  389.   pc = context->sometimes_pc;    
  390.   if (sim_z8001_mode) 
  391.   {
  392.     table = bfop_table;
  393.     pc = MAP_PHYSICAL_TO_LOGICAL(pc);
  394.   }
  395.   else 
  396.   {
  397.     table = sfop_table;
  398.   }
  399.  
  400.  
  401.   do
  402.   {
  403.     word = get_word_mem_da (context, pc);
  404.     p = op_info_table + word;
  405.  
  406. #ifdef LOG
  407.     log[word]++;
  408. #endif
  409.     pc = table[p->exec] (context, pc, word);
  410.     context->insts++;
  411.  
  412.   }
  413.   while (!context->exception);
  414.  
  415.  
  416.  
  417.   context->sometimes_pc = MAP_LOGICAL_TO_PHYSICAL(pc);
  418.   context->ticks += get_now () - now;
  419. }
  420.  
  421. int
  422. tm_signal ()
  423. {
  424.   return the_state.exception;
  425. }
  426.  
  427. void
  428. tm_info_print (x)
  429.      sim_state_type *x;
  430. {
  431.   double timetaken = (double) x->ticks / (double) now_persec ();
  432.   double virttime = x->cycles / 4.0e6;
  433.  
  434.   printf ("instructions executed          : %9d\n", x->insts);
  435.   printf ("cycles counted                 : %9d\n", x->cycles);
  436.   printf ("cycles/inst                    : %9.1f\n",
  437.       (double) x->cycles / (double) x->insts);
  438.   printf ("virtual time taked (at 4Mhz)   : %9.1f\n", virttime);
  439.   printf ("real time taken                : %9.1f\n", timetaken);
  440.  
  441.   printf ("virtual instructions per second: %9.1f\n",
  442.       x->insts / timetaken);
  443.   printf ("emulation speed                : %9.1f%%\n", virttime / timetaken * 100.0);
  444. #ifdef LOG
  445.   {
  446.     extern int quick[];
  447.  
  448.     for (i = 0; quick[i]; i++)
  449.       {
  450.     log[quick[i]] += 100000;
  451.       }
  452.   }
  453.  
  454.   for (i = 0; i < 64 * 1024; i++)
  455.     {
  456.       if (log[i])
  457.     {
  458.       printf ("            /*%7d*/ 0x%x,\n", log[i], i);
  459.     }
  460.     }
  461. #endif
  462.  
  463. }
  464.  
  465. int
  466. sim_trace ()
  467. {
  468.   int i;
  469.   char buffer[10];
  470.   int r;
  471.  
  472.   printf ("\n");
  473.   for (r = 0; r < 16; r++)
  474.     {
  475.       int m;
  476.  
  477.       printf ("r%2d", r);
  478.       printf ("=%04x  ", get_word_reg (&the_state, r));
  479.       for (m = -4; m < 8; m++)
  480.     {
  481.       if (m == 0)
  482.         printf (">");
  483.       printf ("%04x ",
  484.           get_word_mem_da
  485.            (&the_state,( 0xfffe & get_word_reg (&the_state, r)) + m * 2));
  486.     }
  487.       printf ("\n");
  488.     }
  489.  
  490.   printf ("\n");
  491.   printf ("%9d %9d %08x ", the_state.cycles, the_state.insts, the_state.sometimes_pc);
  492.  
  493.   for (i = 0; i < 6; i++)
  494.     {
  495.       buffer[i] = get_byte_mem_da (&the_state, the_state.sometimes_pc + i);
  496.     }
  497.  
  498.   print_insn_z8001 (the_state.sometimes_pc, buffer, stdout);
  499.   printf ("\n");
  500.   tm_resume (1);
  501.   if (the_state.exception != SIM_SINGLE_STEP)
  502.     return 1;
  503.   return 0;
  504. }
  505.  
  506. void
  507. tm_state (x)
  508.      sim_state_type *x;
  509. {
  510.   *x = the_state;
  511. }
  512.  
  513. void
  514. tm_exception (x)
  515. int x;
  516. {
  517.   the_state.exception = x;
  518. }
  519.  
  520. int
  521. tm_read_byte (x)
  522. int x;
  523. {
  524.   return sim_read_byte (&the_state, x);
  525. }
  526.  
  527. void
  528. tm_write_byte (x, y)
  529. int x,y;
  530. {
  531.   sim_write_byte (&the_state, x, y);
  532. }
  533.