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

  1.  
  2. /***************************** 68000 SIMULATOR ****************************
  3.  
  4. File Name: SIMOPS2.C
  5. Version: 1.0
  6.  
  7. This file contains various routines for simulator operation
  8.  
  9. The routines are :  
  10.  
  11.     alter(), hex_to_dec(), dec_to_hex(), intmod(), portmod(), pcmod(),
  12.     changemem(), mmod(), regmod(), mfill(), clear()
  13.  
  14.  
  15. ***************************************************************************/
  16.  
  17.  
  18.  
  19. #include <stdio.h>
  20. #include "extern.h"
  21.  
  22.  
  23. #define    MODIFY_DATA        0
  24. #define    MODIFY_MEMORY    1
  25.  
  26.  
  27. int alter()
  28. {
  29.     char    *choice;
  30.  
  31.     choice = gettext(2,"D<num>, A<num>, PC, MEMory, or IOport ? ");
  32.     if (same("io",choice)) portmod();
  33.     else if (same("pc",choice)) pcmod();
  34.     else if (same("mem",choice)) mmod();
  35.     else if (same("int",choice)) intmod();
  36.     else if (same("d",choice)) regmod(choice + 1, MODIFY_DATA);
  37.     else if (same("a",choice)) regmod(choice + 1, MODIFY_MEMORY);
  38.     else cmderr();
  39.     return SUCCESS;
  40.  
  41. }
  42.  
  43.  
  44.  
  45. int hex_to_dec()
  46. {
  47.     char    *num_string;
  48.     long    number;
  49.  
  50.     num_string = gettext(2,"Hex number ? ");
  51.     if (sscanf (num_string,"%08lx", &number) != 1)
  52.         errflg = TRUE;
  53.  
  54.     printf ("Decimal value = %ld", number);
  55.     windowLine();
  56.     return SUCCESS;
  57.  
  58. }
  59.  
  60.  
  61. int dec_to_hex()
  62. {
  63.     char    *num_string;
  64.     long    number;
  65.  
  66.     num_string = gettext(2,"Decimal number ? ");
  67.  
  68.     if (sscanf (num_string,"%ld", &number) != 1)
  69.         errflg = TRUE;
  70.  
  71.     printf ("Hex value = %08lx", number);
  72.     windowLine();
  73.     return SUCCESS;
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. int intmod()
  81. {
  82.     long    count, newvalue;
  83.     char    *inp;
  84.  
  85.     inp = gettext(3,"New Interrupt Mask? ");
  86.     eval2(inp, &newvalue);
  87.     if (errflg)
  88.         errmess();
  89.     else
  90.         SR = (SR & ~intmsk) | ((newvalue & 0x7) << 8);
  91.     return SUCCESS;
  92.  
  93. }
  94.  
  95.  
  96.  
  97. int portmod()
  98. {
  99.  
  100.     printf("control       = %04x  ? ",port1[0]);
  101.     port1[0] = (pchange(port1[0]) & BYTE);
  102.     if (!same(".",wordptr[0]))
  103.         {
  104.         printf("transmit data = %04x  ? ",port1[1]);
  105.         port1[1] = (pchange(port1[1]) & BYTE);
  106.         if (!same(".",wordptr[0]))
  107.             {
  108.             printf("status        = %04x  ? ",port1[2]);
  109.             port1[2] = (pchange(port1[2]) & BYTE);
  110.             if (!same(".",wordptr[0]))
  111.                 {
  112.                 printf("receive data  = %04x  ? ",port1[3]);
  113.                 port1[3] = (pchange(port1[3]) & BYTE);
  114.                 }
  115.             }
  116.         }
  117.     p1dif = TRUE;
  118.     return SUCCESS;
  119.  
  120. }
  121.  
  122.  
  123.  
  124. int pcmod()
  125. {
  126.     long    count, newvalue;
  127.     char    *inp;
  128.  
  129.     inp = gettext(3,"Location? ");
  130.     eval2(inp, &newvalue);
  131.     if (errflg)
  132.         errmess();
  133.     else
  134.         PC = newvalue;
  135.     return SUCCESS;
  136.  
  137. }
  138.  
  139.  
  140.  
  141. int changemem(oldval, result)          /* gets new value for memory modify */
  142. long oldval, *result;
  143. {                
  144.     long    i, count;
  145.  
  146.     errflg = 0;
  147.     if (gets(lbuf,80) == NULL) exit(0);
  148.     scrollWindow();
  149.     count = scan(lbuf,wordptr,1);          /* scan 1 string of input */
  150.     i = oldval;
  151.     if ((count > 0) && (!same(".",wordptr[0])))
  152.         {
  153.         eval2(wordptr[0], &i);     /* get input */
  154.         if (errflg)
  155.             i = oldval;
  156.         }
  157.     *result = i;
  158.     return -1;
  159.  
  160. }
  161.  
  162.  
  163.  
  164.  
  165. int mmod()                               /* modify memory */
  166. {
  167. int    aloc;
  168. long    value, newval;
  169.  
  170. aloc = getval(3,"Location? ");          /* get location to start modifying */
  171. while (!same(".",wordptr[0]))      /* modify memory until a "." is entered */
  172.     {
  173.     value = memread(aloc, BYTE);            /* prompt location and contents */
  174.     printf("<%04x> = %02x   ? ", WORD & aloc, value);
  175.     changemem(value, &newval);
  176.     memwrite(aloc, newval);
  177.     if (errflg)
  178.         errmess();
  179.     else
  180.         {
  181.         ++aloc;
  182.         if ((newval & ~BYTE) != 0)
  183.             {
  184.             if ((newval & 0xffff0000) != 0)
  185.                 aloc += 3;
  186.             else
  187.                 ++aloc;
  188.             }
  189.         }
  190.     }
  191. windowLine();
  192.  
  193. }
  194.  
  195.  
  196. int regmod (regpntr, data_or_mem)            /* modify processor registers */
  197. char    *regpntr;
  198. int    data_or_mem;
  199. {
  200.     long    regnum, i, value;
  201.  
  202.     errflg = FALSE;
  203.     /* pick up register number */
  204.     if (sscanf(regpntr,"%d",®num) != 1)    {
  205.         cmderr();
  206.         errflg = TRUE;
  207.         return;
  208.         }
  209.  
  210.     if (wcount >= 3) {
  211.         if (!same(".",wordptr[2]))
  212.             /* value is on line and modify only one register */
  213.             {
  214.             eval2(wordptr[2], &value);
  215.             if (errflg) 
  216.                 errmess();    
  217.             else
  218.                 if (data_or_mem == MODIFY_DATA)
  219.                     D[regnum] = value;
  220.                 else    
  221.                     A[regnum] = value;
  222.             }
  223.         }
  224.     else
  225.         /* prompt user for each value and */
  226.         /* modify all following registers */
  227.         for (i=regnum;i<=7;i++)
  228.             {
  229.             if (data_or_mem == MODIFY_DATA)    /* we're entering D regs */
  230.                 {
  231.                 printf("<D%01d> = %lx ? ", i, D[i]);
  232.                 D[i] = changemem(D[i]);
  233.                 }
  234.             else
  235.                 {
  236.                 printf("<A%01d> = %lx ? ", i, A[i]);
  237.                 A[i] = changemem(A[i]);
  238.                 }
  239.  
  240.             /* if error allow retry */
  241.             if (errflg)
  242.                 {
  243.                 errmess();
  244.                 i--;
  245.                 }
  246.             else
  247.                 if (same(".",wordptr[0]))
  248.                     break;
  249.             }
  250.     return (0);
  251. }
  252.  
  253.  
  254.  
  255. int mfill()                  /* load memory with contents of s_record file */
  256. {
  257.     FILE     *fp;
  258.     int     checksum, line, loc, end__of__file;
  259.     char     byte, s_type, nambuf[40], *name;
  260.     char     *mkfname(), *gettext(), *bufptr, *bufend;
  261.  
  262.     name = gettext(2,"Filename? ");
  263.  
  264.     fp = fopen(name, "r");
  265.     if (fp == NULL)
  266.         {                         /* if file cannot be opened, print message */
  267.         printf("error:  cannot open file %s", name);
  268.         windowLine();
  269.         return FAILURE;
  270.         }
  271.  
  272.     line = 0;
  273.     errflg = FALSE;
  274.     end__of__file = FALSE;
  275.     s_type = 0;
  276.  
  277.     fgets(lbuf, 80, fp);
  278.     sscanf (lbuf,"S%c",&s_type);
  279.     if (s_type != '0')                     /* if first record is not a 'S0' */
  280.         errflg = TRUE;                  /* record then incorrect file format */
  281.     line++;
  282.     if (~errflg)
  283.         while (fgets(lbuf, 80, fp) != NULL)           /* read file until end */
  284.         {
  285.         bufptr = lbuf;
  286.         for (bufend = bufptr; *bufend != '\0'; bufend++);
  287.         line++;
  288.         sscanf (lbuf, "S%c%2x", &s_type, &checksum);
  289.         bufptr += 4;
  290.         switch (s_type)
  291.             {
  292.             case '1' : if (sscanf(bufptr,"%04x", &loc) != 1)
  293.                     errflg = TRUE;
  294.                    else bufptr += 4;
  295.                    break;
  296.             case '2' : if (sscanf(bufptr,"%02x", &loc) != 1)
  297.                     errflg = TRUE;
  298.                    else bufptr += 2;
  299.                    break;
  300.             case '3' : if (sscanf(bufptr,"%08x", &loc) != 1)
  301.                     errflg = TRUE;
  302.                    else bufptr += 8;
  303.                    break;
  304.             case '9' : end__of__file = TRUE;
  305.                    break;
  306.             default :  errflg = TRUE;
  307.             }
  308.         if (end__of__file) break;
  309.         if (errflg) break;
  310.         while (sscanf(bufptr,"%02x",&byte))
  311.             {
  312.             bufptr += 2;
  313.             for (bufend = bufptr; *bufend != '\0'; bufend++);
  314.             if ((bufptr + 2) >= bufend) break;
  315.             if ((loc < 0) || (loc > (MEMSIZE - 1)))
  316.                 {
  317.                 printf("\nInvalid Address on line %d...",line);
  318.                 windowLine();
  319.                 errflg = TRUE;
  320.                 break;
  321.                 }
  322.             else
  323.                 memory[loc++] = byte;
  324.             }
  325.         if (errflg) break;
  326.         }
  327.  
  328.     if (errflg)                     /* if error reading file, print message */
  329.         {
  330.         windowLine();
  331.         printf("Invalid data on line %d of file...",line);
  332.         windowLine();
  333.         printf ("%d: %s", line, line, lbuf);
  334.         windowLine();
  335.          printf("Remainder of load aborted...");
  336.         windowLine();
  337.         }
  338.     fclose(fp);            /* close file specified */
  339.     return SUCCESS;
  340.  
  341. }
  342.  
  343.  
  344.  
  345. int clear()                   /* clear memory, registers, port, and cycles */
  346. {
  347.     char    *resp, *gettext();
  348.     int    all, validr, flag, i;
  349.     long    loop_counter;
  350.  
  351.     flag = 0;
  352.     validr = FALSE;
  353.     all = FALSE;
  354.     resp = gettext(2,"clear MEM, REG, PORT, INT, CYcles, or ALL ? ");
  355.  
  356.     if (same("all",resp)) {
  357.         validr = TRUE;
  358.         all = TRUE;
  359.         }
  360.  
  361.     if ((same("reg",resp)) || all)
  362.         /* clear registers */
  363.         {
  364.         flag = 1;
  365.         for(i=0; i<=7; i++) 
  366.             {
  367.             A[i] = 0;
  368.             D[i] = 0;
  369.             }
  370.         validr = TRUE;
  371.         }
  372.  
  373.     if ((same("mem",resp)) || all)
  374.         /* if user wants to clear all clear mem */
  375.         {
  376.         for (loop_counter = 0; loop_counter < MEMSIZE; loop_counter++)
  377.             memory[loop_counter] = 0;
  378.          bpoints = 0;
  379.         validr = TRUE;
  380.         }
  381.  
  382.     if ((same("port",resp)) || all)
  383.         {
  384.         p1dif = TRUE;
  385.         flag = 1;
  386.         port1[0] = 0;
  387.         port1[1] = 0;
  388.         port1[2] = 0x82;
  389.         port1[3] = 0;
  390.         validr = TRUE;
  391.         }
  392.  
  393.     if ((same("int",resp)) || all)
  394.         {
  395.         port1[0] &= 0x1f;
  396.         port1[2] |= 0x80;
  397.         validr = TRUE;
  398.         }
  399.  
  400.     if ((same("cy",resp)) || all)
  401.         {
  402.         flag = 1;
  403.         cycles = 00L;
  404.         validr = TRUE;
  405.         }
  406.  
  407.     if (!validr)
  408.         errmess();
  409.  
  410.     return flag;
  411.  
  412. }
  413.  
  414.  
  415.