home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / MATH / C25SIM.ZIP / sample.c < prev    next >
Text File  |  1994-01-02  |  8KB  |  278 lines

  1. /* ======================================================================= */
  2. /*                                                                         */
  3. /*                                SAMPLE.C                                 */
  4. /*                                                                         */
  5. /* ======================================================================= */
  6.  
  7. #include "stdio.h"
  8. #include "stdlib.h"
  9. #include "conio.h"
  10. #include "string.h"
  11. #include "c25lib.h"
  12.  
  13. #define next_link(p)        ((char *) *((int *)(p)))
  14. #define set_next_link(p,n)  *((char **) (p)) = n
  15. #define string_part(p)      (((char *) (p)) + sizeof(char *))
  16.  
  17. char toprint[100], *first_symbol, name[40], fline[80], dummy2[40];
  18.  
  19. /* ======================================================================= */
  20. /*                                Wait States                              */
  21. /* ======================================================================= */
  22.  
  23. void __write(char *s)
  24.     {
  25.     printf("%s", s);
  26.     }
  27.  
  28. #define ui(x)  ((unsigned int) (x))
  29. #define range(x,lo,hi)  ((ui(x) >= ui(lo)) && (ui(x) <= ui(hi)))
  30.  
  31. int program_wait_states(unsigned int addr)
  32.     {
  33.     /* narrow range of very slow memory */
  34.     if (range(addr, 0x98, 0xA0)) return 10;
  35.     /* everything else is zero-wait-state */
  36.     return 0;
  37.     }
  38.  
  39. /* ======================================================================= */
  40. /*                               Symbol Table                              */
  41. /* ======================================================================= */
  42.  
  43. char *add_line(char **head, char *line)
  44.     {
  45.     char *p, *q;
  46.  
  47.     q = (char *) malloc(sizeof(char *) + strlen(line) + 1);
  48.  
  49.     if (q == NULL) return q;
  50.  
  51.     strcpy(string_part(q), line);
  52.     set_next_link(q, NULL);
  53.  
  54.     if (*head == NULL)
  55.         {
  56.         *head = q;
  57.         return q;
  58.         }
  59.  
  60.     for (p = *head; next_link(p) != NULL; p = next_link(p));
  61.     set_next_link(p, q);
  62.     return q;
  63.     }
  64.  
  65. void load_map_file(char *word)
  66.     {
  67.     int i, addr, dummy1;
  68.     FILE *f;
  69.  
  70.     for (i = 0; i < (int) strlen(word); i++) word[i] = toupper(word[i]);
  71.  
  72.     if (strchr(word, '.') == NULL) sprintf(name, "%s.map", word);
  73.     else strcpy(name, word);
  74.  
  75.     f = fopen(name, "r");
  76.     if (f == NULL)
  77.         {
  78.         printf("No map file available\n");
  79.         return;
  80.         }
  81.  
  82.     while (1)
  83.         {
  84.         fgets(fline, 80, f);
  85.         if (strcmp(fline, "GLOBAL SYMBOLS\n") == 0) break;
  86.         if (feof(f)) { fclose(f); return; }
  87.         }
  88.  
  89.     fgets(fline, 80, f);
  90.     fgets(fline, 80, f);
  91.     fgets(fline, 80, f);
  92.  
  93.     first_symbol = NULL;
  94.     while (1)
  95.         {
  96.         fgets(fline, 80, f);
  97.         if (sscanf(fline, "%x %s %x %s", &addr, name, &dummy1, dummy2) < 4)
  98.             goto fini;
  99.         sprintf(fline, "%s %04X", name, addr);
  100.         if (add_line(&first_symbol, fline) == NULL)
  101.             {
  102.             __write("Out of room for labels and global variables!\n");
  103.             goto fini;
  104.             }
  105.         }
  106. fini:
  107.     fclose(f);
  108.     return;
  109.     }
  110.  
  111. int lookup_symbol(char *name)
  112.     {
  113.     int n;
  114.     char *p, q[40];
  115.  
  116.     for (p = first_symbol; p != NULL; p = next_link(p))
  117.         {
  118.         sscanf(string_part(p), "%s %x", q, &n);
  119.         if (strcmpi(q, name) == 0)
  120.             return n;
  121.         }
  122.     printf("Couldn't find symbol \"%s\"\n", name);
  123.     return 0;
  124.     }
  125.  
  126. int print_matching_symbols(int a)
  127.     {
  128.     char *p, q[40];
  129.     int x, n = 0;
  130.  
  131.     for (p = first_symbol; p != NULL; p = next_link(p))
  132.         {
  133.         sscanf(string_part(p), "%s %x", q, &x);
  134.         if (x == a)
  135.             {
  136.             printf("%s\n", q);
  137.             n++;
  138.             }
  139.         }
  140.     return n;
  141.     }
  142.  
  143. int print_all_symbols(void)
  144.     {
  145.     char *p;
  146.     int n = 0;
  147.  
  148.     for (p = first_symbol; p != NULL; p = next_link(p))
  149.         {
  150.         printf("%s\n", string_part(p));
  151.         n++;
  152.         }
  153.     return n;
  154.     }
  155.  
  156. /* ======================================================================= */
  157. /*                                   Main                                  */
  158. /* ======================================================================= */
  159.  
  160. int cp, rstring;
  161.  
  162. void note_change(int *current, int *previous, char *name)
  163.     {
  164.     if (*previous != *current)
  165.         {
  166.         /* print the new values */
  167.         printf("%ld cycles %s=%04X\n", cycles, name, *current);
  168.  
  169.         /* and update the previous value */
  170.         *previous = *current;
  171.         }
  172.     }
  173.  
  174. char str[] = "Supercalifragilistic";
  175. int strptr = 0;
  176.  
  177. void getch2(void)
  178.     {
  179.     switch (toupper(getch()))
  180.         {
  181.         case 0: getch(); break;
  182.         case 'Q': exit(0); break;
  183.         default: break;
  184.         }
  185.     }
  186.  
  187. void main(void)
  188.     {
  189.     int i, c, mode = 0,
  190.         *k, *L, id, i1, i2, ri_counter,
  191.         k_previous, L_previous, ar4_previous, ar5_previous;
  192.     unsigned long int next_rint;
  193.  
  194.     /* First set up the simulator and load some files. */
  195.     initialize_simulator();
  196.     load_hex_files("foo");  /* C25 executable */
  197.     load_map_file("foo");   /* symbol table */
  198.  
  199.     k = data_memory_address(lookup_symbol("_k"));
  200.     L = data_memory_address(lookup_symbol("_L"));
  201.     id = lookup_symbol("_idle_here");
  202.  
  203.     i1 = lookup_symbol("tint");
  204.     i2 = lookup_symbol("rint");
  205.  
  206.     cp = lookup_symbol("cptr");
  207.     rstring = lookup_symbol("rstring");
  208.  
  209.     /* Set up wait states for slow program memory */
  210.     pwaitf = program_wait_states;
  211.  
  212.     next_rint = 600;
  213.  
  214.     while (1)
  215.         {
  216.         /* Execute one instruction */
  217.         advance();
  218.  
  219.         /* Report only when something changes */
  220.         if (mode)
  221.             {
  222.             note_change(&ar[4], &ar4_previous, "ar4");
  223.             note_change(&ar[5], &ar5_previous, "ar5");
  224.             note_change(k, &k_previous, "k");
  225.             note_change(L, &L_previous, "L");
  226.             }
  227.  
  228.         /* Footnote interrupts */
  229.         if (pc == i1)
  230.             printf("TINT Interrupt at cycle # %ld\n", cycles);
  231.         if (pc == i2)
  232.             printf("RINT Interrupt at cycle # %ld\n", cycles);
  233.         if (pc == id)
  234.             printf("Idle at cycle # %ld\n", cycles);
  235.  
  236.         if (cycles >= next_rint)
  237.             {
  238.             drr = (int) str[strptr];
  239.             strptr = (strptr + 1) % (strlen(str) + 1);
  240.             c25_interrupt(RINT);
  241.             next_rint += 1200;
  242.             }
  243.  
  244.         if (kbhit())
  245.             switch (toupper(getch()))
  246.                 {
  247.                 /* Pressing Q will quit the program */
  248.                 case 'Q':
  249.                     return;
  250.  
  251.                 /* Pressing R will get a long report */
  252.                 case 'R':
  253.                     long_report();
  254.                     break;
  255.  
  256.                 /* Pressing M will toggles k,L report mode */
  257.                 case 'M':
  258.                     mode = !mode;
  259.                     break;
  260.  
  261.                 /* Pressing S will print the received string */
  262.                 case 'S':
  263.                     i = rstring;
  264.                     while ((c = read_data_memory(i++)) != 0)
  265.                         printf("%c", (char) c);
  266.                     printf("\n");
  267.                     getch2();
  268.                     break;
  269.  
  270.                 /* Pressing any other key will get a short report */
  271.                 default:
  272.                     short_report();
  273.                     getch2();
  274.                     break;
  275.                 }
  276.         }
  277.     }
  278.