home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / CROSSASM / 68ASMSIM.ZIP / simsrc / sim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-09  |  6.4 KB  |  282 lines

  1.  
  2. /***************************** 68000 SIMULATOR ****************************
  3.  
  4. File Name: SIM.C
  5. Version: 1.0
  6.  
  7. This file contains the function 'main()' and various screen management
  8.     routines.
  9.  
  10. ***************************************************************************/
  11.  
  12.  
  13.  
  14.  
  15. #include <stdio.h>
  16. #include "var.h"         /* include declarations for global variables */
  17.  
  18.  
  19. main()            /* main routine gives command prompt, */
  20. {                        /* scans instructions, and directs user interface */
  21. int count, refresh;
  22.  
  23. printf ("68000 Simulator\n");
  24. printf ("Version 1.0\n");
  25. init();
  26. while (!same("exit",wordptr[0]))    {
  27.     refresh = TRUE;
  28.     windowLine();
  29.     printf("> ");            /* give prompt */
  30.     if (gets(lbuf,80) == NULL) 
  31.         exit(0);
  32.     scrollWindow();
  33.     count = scan(lbuf,wordptr,10);          /* scan command and operands */
  34.     wcount = count;
  35.     if (count == 0)
  36.         refresh = FALSE;                    /* if no command entered do nothing */
  37.     else if (same("trace",wordptr[0])) trace = TRUE;
  38.     else if (same("troff",wordptr[0])) trace = FALSE;
  39.     else if (same("ex_on",wordptr[0])) {
  40.         exceptions = TRUE;
  41.         refresh = FALSE;
  42.         }
  43.      else if (same("ex_off",wordptr[0])) {
  44.          exceptions = FALSE;
  45.          refresh = FALSE;
  46.          }
  47.      else if (same("version",wordptr[0])) {
  48.          printf ("68000 Simulator Version %d.%d",
  49.              MAJOR_VERSION, MINOR_VERSION);
  50.          windowLine();
  51.          refresh = FALSE;
  52.          }
  53.      else if (same("sstep",wordptr[0])) sstep = TRUE;
  54.      else if (same("ssoff",wordptr[0])) sstep = FALSE;
  55.      else if (same("s_on",wordptr[0])) SR |= sbit;
  56.      else if (same("t_on",wordptr[0])) SR |= tbit;
  57.      else if (same("x_on",wordptr[0])) SR |= xbit;
  58.      else if (same("n_on",wordptr[0])) SR |= nbit;
  59.      else if (same("z_on",wordptr[0])) SR |= zbit;
  60.      else if (same("v_on",wordptr[0])) SR |= vbit;
  61.      else if (same("c_on",wordptr[0])) SR |= cbit;
  62.      else if (same("s_off",wordptr[0])) SR &= ~sbit;
  63.      else if (same("t_off",wordptr[0])) SR &= ~tbit;
  64.      else if (same("x_off",wordptr[0])) SR &= ~xbit;
  65.      else if (same("n_off",wordptr[0])) SR &= ~nbit;
  66.      else if (same("z_off",wordptr[0])) SR &= ~zbit;
  67.      else if (same("v_off",wordptr[0])) SR &= ~vbit;
  68.      else if (same("c_off",wordptr[0])) SR &= ~cbit;
  69.      else if (same("bp",wordptr[0])) {
  70.          selbp();
  71.          refresh = FALSE;
  72.          }
  73.      else if (same("help",wordptr[0])) {
  74.             gethelp();
  75.             clrscr();
  76.             home();
  77.             setdis();
  78.             scrshow();
  79.             at (8,1);
  80.             refresh = FALSE;
  81.             }
  82.      else if (same("clear",wordptr[0])) clear();
  83.      else if (same("ch",wordptr[0])) alter();
  84.      else if (same("exit",wordptr[0])) finish();
  85.      else if (same("refresh",wordptr[0])) { }
  86.      else if (same("dec",wordptr[0])) refresh = FALSE;
  87.      else if (same("hex",wordptr[0])) refresh = FALSE;
  88.      else if (same("go",wordptr[0])) {}
  89.      else if (same("md",wordptr[0])) {}
  90.      else if (same("ld",wordptr[0])) {}
  91.      else {
  92.          errmess();
  93.          refresh = FALSE;
  94.          }
  95.     if (same("md",wordptr[0])) mdis();
  96.     else if (same("ld",wordptr[0])) mfill();
  97.     else if (same("dec",wordptr[0])) hex_to_dec();
  98.     else if (same("hex",wordptr[0])) dec_to_hex();
  99.     else if (same("go",wordptr[0])) runprog();
  100.     if (refresh) {
  101.         save_cursor();
  102.         home();
  103.         setdis();
  104.         scrshow();
  105.         restore_cursor();
  106.         }
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. init()                     /* initialization for the simulator */
  113. {
  114.     int    i;
  115.  
  116.     exceptions = FALSE;
  117.     lbuf[0] = '\0';        /* initialize to prevent memory access violations */
  118.     wordptr[0] = lbuf;
  119.     for (i = 0; i <= 7; i++)
  120.         A[i] = D[i] = OLD_A[i] = OLD_D[i] = 0;
  121.     OLD_A[8] = cycles = PC = 0;
  122.     SR = 0x2000;
  123.     A[8] = 0xf00;
  124.     OLD_PC = old_cycles = OLD_SR = -1; /* set different from 'PC' and 'cycles' */
  125.     trace = sstep = FALSE;
  126.     old_trace = old_sstep = TRUE;      /* set different from 'trace' and 'sstep' */
  127.     clrscr();
  128.     home();
  129.     setdis();
  130.     scrshow();
  131.     at (8,1);
  132.  
  133. }
  134.  
  135.  
  136. finish()               /* normal simulator exit */
  137. {
  138.  
  139.     at (24,1);
  140.     printf("end 68000 simulator . . .");
  141.     exit(0);
  142.  
  143. }
  144.  
  145.  
  146.  
  147. errmess()        /* error message for invalid input */
  148. {
  149.  
  150.     printf("invalid input...");
  151.     windowLine();
  152.  
  153. }
  154.  
  155.  
  156.  
  157. cmderr()                    /* error message for an invalid command */
  158. {
  159.  
  160.     printf ("invalid command");
  161.     windowLine();
  162.     printf ("type help for list of commands");
  163.     windowLine();
  164.  
  165. }
  166.  
  167.  
  168.  
  169. setdis()                                                        /* shows register display */
  170. {
  171.  
  172.     printf("<D0>  = %08lx  <D4>  = %08lx  <A0>  = %08lx  <A4> = %08lx\n",D[0],
  173.         D[4],A[0],A[4]);
  174.     printf("<D1>  = %08lx  <D5>  = %08lx  <A1>  = %08lx  <A5> = %08lx\n",D[1],
  175.         D[5],A[1],A[5]);
  176.     printf("<D2>  = %08lx  <D6>  = %08lx  <A2>  = %08lx  <A6> = %08lx\n",D[2],
  177.         D[6],A[2],A[6]);
  178.     printf("<D3>  = %08lx  <D7>  = %08lx  <A3>  = %08lx  <A7> = %08lx\n",D[3],
  179.         D[7],A[3],A[7]);
  180.     printf("trace:            sstep:            cycles: %6u    <A7'>= %08lx\n"
  181.         ,cycles, A[8]);
  182.     printf("         cn  tr  st  rc           T S  INT   XNZVC    <PC> = %08lx\n"
  183.         ,PC);
  184.     printf("  port1  %02x  %02x  %02x  %02x      SR =\n",
  185.            port1[0],port1[1],port1[2],port1[3]);
  186.     printf("───────────────────────────────────────────────────────");
  187.     printf("────────────────────────\n");
  188.     old_sstep = ~sstep;
  189.     old_trace = ~trace;
  190.     OLD_SR = ~SR;
  191.  
  192. }
  193.  
  194.  
  195. scrshow()                    /* display processor registers and cycle counter */
  196. {
  197.     int    j,k;
  198.  
  199.     k = 9;
  200.     for (j=0;j<=7;j++)        /* update D registers */
  201.         {
  202.         if (D[j] != OLD_D[j])
  203.             {
  204.             OLD_D[j] = D[j];
  205.             if (j >= 4) k = 27;         /* select the proper column for display */
  206.             at(j%4+1,k);
  207.             printf("%08lx",D[j]);
  208.             }
  209.         }
  210.  
  211.     k = 45;
  212.     for (j=0;j<=8;j++)        /* update A registers */
  213.         {
  214.         if (A[j] != OLD_A[j])
  215.             {
  216.             OLD_A[j] = A[j];
  217.             if (j >= 4) k = 62;         /* select the proper column for display */
  218.             at(j%4+1,k);
  219.             if (j == 8) at (5,k);
  220.             printf("%08lx",A[j]);
  221.             }
  222.         }
  223.     
  224.     /* if port 1 has been written to then update its display */
  225.     if (p1dif)
  226.         {
  227.         at(7,10);
  228.         printf("%02x  %02x  %02x  %02x\n",port1[0],port1[1],port1[2],port1[3]);
  229.         p1dif = FALSE;
  230.         }
  231.  
  232.     /* update the status register's value */
  233.     if (SR != OLD_SR)
  234.         {
  235.         OLD_SR = SR;
  236.         at(7,35);
  237.         for (j=15;j>=0;j--)
  238.             {
  239.             if ((0x1 << j) & SR)    /* print each bit of SR */
  240.                 printf ("1");
  241.             else    printf ("0");
  242.             }
  243.         }
  244.  
  245.     if (OLD_PC ^ PC)
  246.         {
  247.         OLD_PC = PC;
  248.         at(6, 62);
  249.         printf ("%08lx",PC);
  250.         }
  251.  
  252.     if (sstep ^ old_sstep)
  253.         {
  254.         old_sstep = sstep;
  255.         at (5, 26);
  256.         if (sstep)
  257.             printf ("on ");
  258.         else
  259.             printf ("off");
  260.         }
  261.  
  262.     if (trace ^ old_trace)
  263.         {
  264.         old_trace = trace;
  265.         at(5,8);
  266.         if (trace)
  267.             printf ("on ");
  268.         else
  269.             printf ("off");
  270.         }
  271.  
  272.     if (old_cycles ^ cycles)
  273.         {
  274.         old_cycles = cycles;
  275.         at(5,45);
  276.         printf("%6u",cycles);
  277.         }
  278.  
  279. }
  280.  
  281.  
  282.