home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / EXTRA-ST / CPM-80-E / CPM-0.2 / CPM-0 / cpm-0.2 / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  8.9 KB  |  330 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    CP/M emulator version 0.1                         */
  5. /*                                         */
  6. /*    written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  7. /*    June-1994                                 */
  8. /*                                         */
  9. /*    This file is distributed under the GNU COPYRIGHT             */
  10. /*    see COPYRIGHT.GNU for Copyright details                     */
  11. /*                                         */
  12. /*                                         */
  13. /*****************************************************************************/
  14. #include "cpmemu.h"
  15. #include <time.h>
  16. #include <sys/time.h>
  17.  
  18. extern int slowdown, tickercnt;
  19.  
  20. static void doint(void) {
  21.     /* execute Z80-interrupt */
  22.   
  23.     unsigned char rembyte;
  24.     unsigned oldpc;
  25.     rembyte = z80mem[oldpc = --z80regs.pc];
  26.     z80mem[oldpc] = 0xff;    /* RST 38h */
  27.     singlestep();        /* perform RST */
  28.     z80mem[oldpc++] = rembyte;    /* restore old byte */
  29.     do {
  30.     singlestep();
  31.     if (z80mem[z80regs.pc] == 0x76 || z80regs.pc >= BDOS)
  32.         break;
  33.     } while (z80regs.pc != oldpc);
  34. }
  35.  
  36. void run_at_2MHz(void) {
  37.     if (slowdown) {
  38.     struct timeval tv1;
  39.     struct timezone tz;
  40.     int ctr, icnt;
  41.     ctr = icnt = 0;
  42.     while (!(z80mem[z80regs.pc] == 0x76 && z80regs.pc < BDOS)) {
  43.         z80step(1);
  44.         ++*(char *)&z80regs.ir;
  45.         if (++ctr == slowdown) {
  46.         ctr = 0;
  47.         if (tickercnt && ++icnt == tickercnt) {
  48.             /* do an interrupt */
  49.             icnt = 0;
  50.             if (z80regs.pc > 0x100 && z80regs.pc < BDOS &&
  51.             !z80regs.iff) {
  52.             doint();
  53.             }
  54.         } else
  55.             gettimeofday(&tv1, &tz);
  56.         }
  57.     }
  58.     } else
  59.     z80run();    /* full speed! */
  60. }
  61.  
  62. void commandloop(void) {
  63.     /* forever cycle in command loop or enter GO mode */
  64.     static int lastkey = 0;
  65.     long memcnt = 0, currmem = 0;
  66.     struct z80regs mem[256];
  67.     for (;;) {
  68.     int c;
  69.     unsigned num, addr;
  70.     dispregs(z80regs.pc);
  71.     nodisplay:
  72.     if (prompt)
  73.         printf("Z80> "); fflush(stdout);
  74.     while (!(c = conin()))
  75.         ;        /* c is command character */
  76.     if (prompt)
  77.         putchar('\r');
  78.     if (c == '\r' || c == '\n') {
  79.         switch (lastkey) {
  80.         case 'l': case 'u':
  81.         disassem(&addr, 16);
  82.         break;
  83.         case 'd':
  84.         memdump(addr+=0x80, 8);
  85.         break;
  86.         }
  87.     } else
  88.         lastkey = c;
  89.  
  90.         switch (c) {
  91.     case '?':
  92.         printf("Z80 debug quick help:\n"
  93.            "B (n) (loc)   set breakpoint (n) to (loc)\n"
  94.            "L (n) (loc)   set listpoint (n) to (loc)\n"
  95.            "N (n) (cnt)   set ignore count for break/listpoint (n)\n"
  96.            "S             estimate emulation speed\n"
  97.            "R             run (continue)\n"
  98.            "x             display Z80 registers\n"
  99.            "l (loc)       disassemble memory\n"
  100.            "d (loc)       dump memory\n"
  101.            "g             go (continue)\n"
  102.            "j (loc)       jump to (loc)\n"
  103.            "e (loc)       edit byte (loc)\n"
  104.            "; (loc)       run until (loc)\n"
  105.            "t             single step one statement\n"
  106.            "c             single step; but run through subroutines\n"
  107.            "q             exit cpm emulator\n"
  108.            "b             run until next BIOS call\n"
  109.            "i             info about breakpoints\n"
  110.            "w (loc)       watch memory cell (step until it changes)\n"
  111.            "r             step until SP is incremented (RET, POP)\n"
  112.            "all data is hexadecimal; pressing ENTER at (loc) input\n"
  113.            "will return current PC\n");
  114.         break;
  115.     case 'B':        /* set Breakpoint (n) */
  116.         printf("set breakpoint ");
  117.         num = getdig('1') - '1';
  118.         printf(" to address ");
  119.         breakpoint[num].where = gethex();
  120.         breakpoint[num].action = AC_BREAK;
  121.         breakpoint[num].maxcnt = 1;
  122.         breakpoint[num].curcnt = 0;
  123.         break;
  124.     case 'L':        /* set listpoint (n) */
  125.         printf("set listpoint ");
  126.         num = getdig('1') - '1';
  127.         printf(" to address ");
  128.         breakpoint[num].where = gethex();
  129.         breakpoint[num].action = AC_LIST;
  130.         breakpoint[num].maxcnt = 1;
  131.         breakpoint[num].curcnt = 0;
  132.         break;
  133.     case 'N':        /* number break- or listpoint (n) */
  134.         printf("Number break/listpoint ");
  135.         num = getdig('1') - '1';
  136.         printf(" to maxcount ");
  137.         breakpoint[num].maxcnt = getdig(0);
  138.         breakpoint[num].curcnt = 0;
  139.         if (!breakpoint[num].maxcnt)
  140.         breakpoint[num].action = AC_NONE;
  141.         break;
  142.     case 'S':        /* measure speed */
  143.         printf("Estimating speed... "); fflush(stdout);
  144.         {   struct z80regs saveregs;
  145.         clock_t t;
  146.         long cycles;
  147.         static unsigned char benchmark[24] = {
  148.             0x1e, 0x06,        /* ld e,6 */
  149.             0x01, 0x00, 0x00,    /* ld bc,0 */
  150.             0x0a,        /* 7T:    ld a,(bc) */
  151.             0x65,        /* 4T:    ld h,l */
  152.             0x54,        /* 4T:    ld d,h */
  153.             0x21, 0x33, 0x33,    /* 10T:    ld hl,3333 */
  154.             0x0b,            /* 6T:    dec bc */
  155.             0x78,        /* 4T:    ld a,b */
  156.             0xb1,        /* 4T:    or c */
  157.             0x38, 0xef,        /* 5T:    jr false */
  158.             0xc2, 0x05, 0x01,    /* 10T: jp true */
  159.             0x1d,        /* dec e */
  160.             0x20, 0xec,        /* jr nz,102 */
  161.             0x76        /* HALT */
  162.             }, savemem[24];
  163.         memcpy(savemem, z80mem+0x100, 24);
  164.         memcpy(z80mem+0x100, benchmark, 24);
  165.         saveregs = z80regs;
  166.         z80regs.pc = 0x100;
  167.         t = clock();
  168.         z80run();
  169.         t = clock() - t;
  170.         cycles = 6L * 65536L * 54L;/* should be 54T on a genuine Z80 */
  171. #if 1
  172.         z80regs = saveregs;
  173.         memcpy(z80mem+0x100, savemem, 24);
  174. #endif
  175.         printf("Your Z80 roams at %f MHz\n",
  176.                (double)cycles * 1e-6 / t * CLOCKS_PER_SEC);
  177.         }
  178.         break;
  179.     case 'R':        /* run the program */
  180.         z80run();
  181.         break;
  182.         case 'x':
  183.         /* dispregs(z80regs.pc); */
  184.         dispregs2();
  185.             break;
  186.         case 'l':
  187.         printf("list from loc: ");
  188.         addr = gethex();
  189.         disassem(&addr, 16);
  190.         goto nodisplay;
  191.         case 'd':
  192.         /* display memory */
  193.         printf("dump memory at: ");
  194.         addr = gethex();
  195.         memdump(addr, 8);
  196.         goto nodisplay;
  197.         case 'g':    /* GO */
  198.         debug = 0;
  199.         return;
  200.         case 'j':
  201.         printf("jump to: ");
  202.         z80regs.pc = gethex();
  203.         break;
  204.         case 'e':
  205.         printf("edit byte: ");    /* abort with ctrl-@ */
  206.         addr = gethex();
  207.         for (;;) {
  208.         printf("%04x %02x ", addr, z80mem[addr]);
  209.         num = gethex();
  210.         z80mem[addr++] = num;
  211.         }
  212.         /* never reaches this */
  213.         case ';':
  214.         printf("run until ");
  215.         breakpoint[NBREAKS].where = gethex();
  216.     xcall:
  217.         breakpoint[NBREAKS].action = AC_BREAK;
  218.         breakpoint[NBREAKS].maxcnt = 1;
  219.         breakpoint[NBREAKS].curcnt = 0;
  220.         z80run();
  221.         breakpoint[NBREAKS].action = AC_NONE;
  222.         break;
  223.         case 'c':    /* trace over CALLs and RSTs */
  224.         switch (z80mem[z80regs.pc]) {
  225.         case 0xcd:
  226.         case 0xc4: case 0xcc: case 0xd4: case 0xdc:
  227.         case 0xe4: case 0xec: case 0xf4: case 0xfc:
  228.         breakpoint[NBREAKS].where = z80regs.pc + 3;
  229.         goto xcall;
  230.         case 0xf7:              /* short BDOS call */
  231.         breakpoint[NBREAKS].where = z80regs.pc + 2;
  232.         goto xcall;
  233.         case 0xc7: case 0xcf: case 0xd7: case 0xdf:
  234.         case 0xe7: case 0xef: case 0xff:
  235.         breakpoint[NBREAKS].where = z80regs.pc + 1;
  236.         goto xcall;
  237.         } /* else fall thru */
  238.         case 't':
  239.         z80step(1);
  240.         break;
  241.         case 'q':
  242.         exit(0);
  243.     case 'b':
  244.         break_at_BIOS = 1;
  245.         z80run();
  246.         break;
  247.     case 'i':    /* info */
  248.         {   struct breakpoint *bp;
  249.         int i;
  250.         bp = breakpoint;
  251.         for (i = 0; i < NBREAKS; ++i, ++bp)
  252.             if (bp->action)
  253.             printf("BP %d (count %d of %d) is set at %04x, type %s\n", i, bp->curcnt, bp->maxcnt, bp->where, bp->action == AC_BREAK ? "BREAK" : "LIST");
  254.         printf("last location of 'c' breakpoint: %04x\n", bp->where);
  255.         }
  256.         break;
  257.         case 'w':
  258.         printf("watch mem cell: ");
  259.         addr = gethex();
  260.         num = z80mem[addr];
  261.         do {
  262.         unsigned oldpc;
  263.         oldpc = z80regs.pc;
  264.         z80step(1);
  265.         if (z80regs.pc == oldpc && z80mem[z80regs.pc] != 0x10)
  266.             break;    /* break, but not on djnz $ */
  267.         } while (z80mem[addr] == num);
  268.         break;
  269.         case 'r':
  270.         printf("running...\n");
  271.         addr = z80regs.sp + 2;
  272.         do {
  273.         z80step(1);
  274.         if (z80mem[z80regs.pc] == 0x76 && z80regs.pc < BDOS)
  275.             break;
  276.         } while (z80regs.sp != addr);
  277.         break;
  278.     case 'T':    /* trace... */
  279.         printf("Trace until ");
  280.         addr = gethex();
  281.         do {
  282.         dispregs(z80regs.pc);
  283.         z80step(1);
  284.         if (z80mem[z80regs.pc] == 0x76 && z80regs.pc < BDOS)
  285.             break;
  286.         } while (z80regs.pc != addr);
  287.         break;
  288.     case 'M':    /* memorize... */
  289.         printf("Memorize until: ");
  290.         addr = gethex();
  291.         memcnt = 0;
  292.         do {
  293.         mem[memcnt & 255] = z80regs;
  294.         z80step(1);
  295.         if (z80mem[z80regs.pc] == 0x76 && z80regs.pc < BDOS)
  296.             break;
  297.         ++memcnt;
  298.         } while (z80regs.pc != addr);
  299.         currmem = memcnt & 255;
  300.         break;
  301.     case 'U':    /* memorize... */
  302.         printf("Watch: ");
  303.         memcnt = 0;
  304.         do {
  305.         mem[memcnt & 255] = z80regs;
  306.         z80step(1);
  307.         if (z80mem[z80regs.pc] == 0x76 && z80regs.pc < BDOS)
  308.             break;
  309.         ++memcnt;
  310.         } while (z80mem[0x4d04] != 0xee && z80mem[0x4d04] != 0xbc);
  311.         currmem = memcnt & 255;
  312.         break;
  313.     case '+':
  314.         z80regs = mem[currmem = (currmem+1) & 255];
  315.         printf("die - %ld:\n", (memcnt-currmem+256) & 255);
  316.         break;
  317.     case '-':
  318.         z80regs = mem[currmem = (currmem+255) & 255];
  319.         printf("die - %ld:\n", (memcnt-currmem+256) & 255);
  320.         break;
  321.         case '2':    /* run at 2 MHz */
  322.         printf("running at 2 MHz\n");
  323.         run_at_2MHz();
  324.         break;
  325.     default:
  326.         goto nodisplay;
  327.     }
  328.     }
  329. }
  330.