Subversion Repositories pentevo

Rev

Rev 798 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "std.h"
  2.  
  3. #include "emul.h"
  4. #include "vars.h"
  5. #include "debug.h"
  6. #include "dbgpaint.h"
  7. #include "dbgcmd.h"
  8. #include "dbgmem.h"
  9. #include "wd93crc.h"
  10. #include "util.h"
  11.  
  12. static TRKCACHE edited_track;
  13.  
  14. static unsigned sector_offset, sector;
  15.  
  16. static void findsector(unsigned addr)
  17. {
  18.    for (sector_offset = sector = 0; sector < edited_track.s; sector++, sector_offset += edited_track.hdr[sector].datlen)
  19.       if (addr >= sector_offset && addr < sector_offset + edited_track.hdr[sector].datlen)
  20.          return;
  21.    errexit("internal diskeditor error");
  22. }
  23.  
  24. unsigned char *editam(unsigned addr)
  25. {
  26.    Z80 &cpu = CpuMgr.Cpu();
  27.    if (editor == ED_CMOS) return &cmos[addr & (sizeof(cmos)-1)];
  28.    if (editor == ED_NVRAM) return &nvram[addr & (sizeof(nvram)-1)];
  29.    if(editor == ED_COMP_PAL) return &comp.comp_pal[addr & (sizeof(comp.comp_pal) - 1)];
  30.    if (editor == ED_MEM) return cpu.DirectMem(addr);
  31.    if (!edited_track.trkd) return nullptr;
  32.    if (editor == ED_PHYS) return edited_track.trkd + addr;
  33.    // editor == ED_LOG
  34.    findsector(addr); return edited_track.hdr[sector].data + addr - sector_offset;
  35. }
  36.  
  37. static void editwm(unsigned addr, unsigned char byte)
  38. {
  39.    if (editrm(addr) == byte) return;
  40.    unsigned char *ptr = editam(addr);
  41.    if (!ptr) return; *ptr = byte;
  42.    if(editor == ED_MEM || editor == ED_CMOS || editor == ED_NVRAM || editor == ED_COMP_PAL)
  43.    {
  44.        if(editor == ED_COMP_PAL)
  45.        {
  46.            temp.comp_pal_changed = 1;
  47.        }
  48.        return;
  49.    }
  50.    if (editor == ED_PHYS) { comp.fdd[mem_disk].optype |= 2; return; }
  51.    comp.fdd[mem_disk].optype |= 1;
  52.    // recalc sector checksum
  53.    findsector(addr);
  54.    *(unsigned short*)(edited_track.hdr[sector].data + edited_track.hdr[sector].datlen) =
  55.       u16(wd93_crc(edited_track.hdr[sector].data - 1, edited_track.hdr[sector].datlen + 1));
  56. }
  57.  
  58. unsigned memadr(unsigned addr)
  59. {
  60.    if (editor == ED_MEM) return (addr & 0xFFFF);
  61.    if (editor == ED_CMOS) return (addr & (sizeof(cmos)-1));
  62.    if (editor == ED_NVRAM) return (addr & (sizeof(nvram)-1));
  63.    if (editor == ED_COMP_PAL) return (addr & (sizeof(comp.comp_pal) - 1));
  64.    // else if (editor == ED_PHYS || editor == ED_LOG)
  65.    if (!mem_max) return 0;
  66.    while ((int)addr < 0) addr += mem_max;
  67.    while (addr >= mem_max) addr -= mem_max;
  68.    return addr;
  69. }
  70.  
  71. void showmem()
  72. {
  73.    Z80 &cpu = CpuMgr.Cpu();
  74.    char line[80]; unsigned ii;
  75.    unsigned cursor_found = 0;
  76.    if(mem_dump)
  77.    {
  78.        mem_ascii = 1;
  79.        mem_sz = 32;
  80.    }
  81.    else mem_sz = 8;
  82.  
  83.    if (editor == ED_LOG || editor == ED_PHYS)
  84.    {
  85.       edited_track.clear();
  86.       edited_track.seek(comp.fdd+mem_disk, mem_track/2, mem_track & 1, (editor == ED_LOG)? LOAD_SECTORS : JUST_SEEK);
  87.       if (!edited_track.trkd) { // no track
  88.          for (ii = 0; ii < mem_size; ii++) {
  89.             sprintf(line, (ii == mem_size/2)?
  90.                "          track not found            ":
  91.                "                                     ");
  92.             tprint(mem_x, mem_y+ii, line, (activedbg == WNDMEM) ? W_SEL : W_NORM);
  93.          }
  94.          mem_max = 0;
  95.          goto title;
  96.       }
  97.       mem_max = edited_track.trklen;
  98.       if (editor == ED_LOG)
  99.          for (mem_max = ii = 0; ii < edited_track.s; ii++)
  100.             mem_max += edited_track.hdr[ii].datlen;
  101.    }
  102.    else if(editor == ED_MEM)
  103.        mem_max = 0x10000;
  104.    else if(editor == ED_CMOS)
  105.        mem_max = sizeof(cmos);
  106.    else if(editor == ED_NVRAM)
  107.        mem_max = sizeof(nvram);
  108.    else if(editor == ED_COMP_PAL)
  109.        mem_max = sizeof(comp.comp_pal);
  110.  
  111.    unsigned div;
  112.    div = mem_dump ? 32 : 8;
  113.    unsigned dx;
  114.    dx = (mem_max + div - 1) / div;
  115.    unsigned mem_lines;
  116.    mem_lines = min(dx, unsigned(mem_size));
  117. redraw:
  118.    cpu.mem_curs = memadr(cpu.mem_curs);
  119.    cpu.mem_top = memadr(cpu.mem_top);
  120.    for (ii = 0; ii < mem_lines; ii++)
  121.    {
  122.       unsigned ptr = memadr(cpu.mem_top + ii*mem_sz);
  123.       sprintf(line, "%04X ", ptr);
  124.       unsigned cx = 0;
  125.       if (mem_dump)
  126.       {  // 0000 0123456789ABCDEF0123456789ABCDEF
  127.          for (unsigned dx = 0; dx < 32; dx++)
  128.          {
  129.             if (ptr == cpu.mem_curs) cx = dx+5;
  130.             unsigned char c = editrm(ptr); ptr = memadr(ptr+1);
  131.             line[dx+5] = char(c ? c : '.');
  132.          }
  133.       }
  134.       else
  135.       {  // 0000 11 22 33 44 55 66 77 88 abcdefgh
  136.          for (unsigned dx = 0; dx < 8; dx++)
  137.          {
  138.             if (ptr == cpu.mem_curs) cx = (mem_ascii) ? dx+29 : dx*3 + 5 + cpu.mem_second;
  139.             unsigned char c = editrm(ptr); ptr = memadr(ptr+1);
  140.             sprintf(line+5+3*dx, "%02X", c); line[7+3*dx] = ' ';
  141.             line[29+dx] = char(c ? c : '.');
  142.          }
  143.       }
  144.       line[37] = 0;
  145.       tprint(mem_x, mem_y+ii, line, (activedbg == WNDMEM) ? W_SEL : W_NORM);
  146.       cursor_found |= cx;
  147.       if (cx && (activedbg == WNDMEM))
  148.          txtscr[(mem_y+ii)*80+mem_x+cx+80*30] = W_CURS;
  149.    }
  150.    if (!cursor_found) { cursor_found=1; cpu.mem_top=cpu.mem_curs & ~(mem_sz-1); goto redraw; }
  151. title:
  152.    const char *MemName = nullptr;
  153.    if (editor == ED_MEM)
  154.        MemName = "memory";
  155.    else if (editor == ED_CMOS)
  156.        MemName = "cmos";
  157.    else if (editor == ED_NVRAM)
  158.        MemName = "nvram";
  159.    else if(editor == ED_COMP_PAL)
  160.        MemName = "comppal";
  161.  
  162.    if(editor == ED_MEM || editor == ED_CMOS || editor == ED_NVRAM || editor == ED_COMP_PAL)
  163.    {
  164.        sprintf(line, "%s: %04X gsdma: %06X", MemName, cpu.mem_curs & 0xFFFF, temp.gsdmaaddr);
  165.    }
  166.    else if (editor == ED_PHYS)
  167.        sprintf(line, "disk %c, trk %02X, offs %04X", int(mem_disk+'A'), mem_track, cpu.mem_curs);
  168.    else
  169.    { // ED_LOG
  170.       if (mem_max)
  171.           findsector(cpu.mem_curs);
  172.       sprintf(line, "disk %c, trk %02X, sec %02X[%02X], offs %04X",
  173.         int(mem_disk+'A'), mem_track, sector, edited_track.hdr[sector].n,
  174.         cpu.mem_curs-sector_offset);
  175.    }
  176.    tprint(mem_x, mem_y-1, line, W_TITLE);
  177.    frame(mem_x,mem_y,37,mem_size,FRAME);
  178. }
  179.  
  180.       /* ------------------------------------------------------------- */
  181. void mstl()
  182. {
  183.     Z80 &cpu = CpuMgr.Cpu();
  184.     if(mem_max)
  185.     {
  186.         cpu.mem_curs &= ~(mem_sz - 1);
  187.         cpu.mem_second = 0;
  188.     }
  189. }
  190. void mendl()
  191. {
  192.     Z80 &cpu = CpuMgr.Cpu();
  193.     if(mem_max)
  194.     {
  195.         cpu.mem_curs |= (mem_sz - 1);
  196.         cpu.mem_second = 1;
  197.     }
  198. }
  199. void mup()
  200. {
  201.     Z80 &cpu = CpuMgr.Cpu();
  202.     if (mem_max) cpu.mem_curs -= mem_sz;
  203. }
  204. void mpgdn()
  205. {
  206.     Z80 &cpu = CpuMgr.Cpu();
  207.     if(mem_max)
  208.     {
  209.         cpu.mem_curs += mem_size * mem_sz;
  210.         cpu.mem_top += mem_size * mem_sz;
  211.     }
  212. }
  213.  
  214. void mpgup()
  215. {
  216.     Z80 &cpu = CpuMgr.Cpu();
  217.     if(mem_max)
  218.     {
  219.         cpu.mem_curs -= mem_size * mem_sz;
  220.         cpu.mem_top -= mem_size * mem_sz;
  221.     }
  222. }
  223.  
  224. void mdown()
  225. {
  226.    if (!mem_max) return;
  227.  
  228.    Z80 &cpu = CpuMgr.Cpu();
  229.    cpu.mem_curs += mem_sz;
  230.    if (((cpu.mem_curs - cpu.mem_top + mem_max) % mem_max) / mem_sz >= mem_size) cpu.mem_top += mem_sz;
  231. }
  232.  
  233. void mleft()
  234. {
  235.    if (!mem_max) return;
  236.    Z80 &cpu = CpuMgr.Cpu();
  237.    if (mem_ascii || !cpu.mem_second) cpu.mem_curs--;
  238.    if (!mem_ascii) cpu.mem_second ^= 1;
  239. }
  240.  
  241. void mright()
  242. {
  243.    Z80 &cpu = CpuMgr.Cpu();
  244.    if (!mem_max) return;
  245.    if (mem_ascii || cpu.mem_second) cpu.mem_curs++;
  246.    if (!mem_ascii) cpu.mem_second ^= 1;
  247.    if (((cpu.mem_curs - cpu.mem_top + mem_max) % mem_max) / mem_sz >= mem_size) cpu.mem_top += mem_sz;
  248. }
  249.  
  250. char dispatch_mem()
  251. {
  252.    Z80 &cpu = CpuMgr.Cpu();
  253.    if (!mem_max)
  254.        return 0;
  255.  
  256.    u8 Kbd[256];
  257.    GetKeyboardState(Kbd);
  258.    unsigned short k = 0;
  259.    if (ToAscii(input.lastkey,0,Kbd,&k,0) != 1)
  260.        return 0;
  261.  
  262.    if (mem_ascii)
  263.    {
  264.       k &= 0xFF;
  265.       if (k < 0x20 || k >= 0x80)
  266.           return 0;
  267.       editwm(cpu.mem_curs, (unsigned char)k);
  268.       mright();
  269.       return 1;
  270.    }
  271.    else
  272.    {
  273.       u8 u = u8(toupper(k));
  274.       if ((u >= '0' && u <= '9') || (u >= 'A' && u <= 'F'))
  275.       {
  276.          unsigned char k = (u >= 'A') ? u - 'A'+10 : u - '0';
  277.          unsigned char c = editrm(cpu.mem_curs);
  278.          if (cpu.mem_second) editwm(cpu.mem_curs, (c & 0xF0) | k);
  279.          else editwm(cpu.mem_curs, u8(c & 0x0F) | u8(k << 4));
  280.          mright();
  281.          return 1;
  282.       }
  283.    }
  284.    return 0;
  285. }
  286.  
  287. void mtext()
  288. {
  289.    Z80 &cpu = CpuMgr.Cpu();
  290.    int rs = find1dlg(cpu.mem_curs);
  291.    if (rs != -1) cpu.mem_curs = unsigned(rs);
  292. }
  293.  
  294. void mcode()
  295. {
  296.    Z80 &cpu = CpuMgr.Cpu();
  297.    int rs = find2dlg(cpu.mem_curs);
  298.    if (rs != -1) cpu.mem_curs = unsigned(rs);
  299. }
  300.  
  301. void mgoto()
  302. {
  303.    Z80 &cpu = CpuMgr.Cpu();
  304.    int v = input4(mem_x, mem_y, cpu.mem_top);
  305.    if(v != -1)
  306.    {
  307.        cpu.mem_top = (unsigned(v) & ~(mem_sz - 1));
  308.        cpu.mem_curs = unsigned(v);
  309.    }
  310. }
  311.  
  312. void mswitch() { mem_ascii ^= 1; }
  313.  
  314. void msp()
  315. {
  316.     Z80 &cpu = CpuMgr.Cpu();
  317.     cpu.mem_curs = cpu.sp;
  318. }
  319. void mpc()
  320. {
  321.     Z80 &cpu = CpuMgr.Cpu();
  322.     cpu.mem_curs = cpu.pc;
  323. }
  324.  
  325. void mbc()
  326. {
  327.     Z80 &cpu = CpuMgr.Cpu();
  328.     cpu.mem_curs = cpu.bc;
  329. }
  330.  
  331. void mde()
  332. {
  333.     Z80 &cpu = CpuMgr.Cpu();
  334.     cpu.mem_curs = cpu.de;
  335. }
  336.  
  337. void mhl()
  338. {
  339.     Z80 &cpu = CpuMgr.Cpu();
  340.     cpu.mem_curs = cpu.hl;
  341. }
  342.  
  343. void mix()
  344. {
  345.     Z80 &cpu = CpuMgr.Cpu();
  346.     cpu.mem_curs = cpu.ix;
  347. }
  348.  
  349. void miy()
  350. {
  351.     Z80 &cpu = CpuMgr.Cpu();
  352.     cpu.mem_curs = cpu.iy;
  353. }
  354.  
  355. void mmodemem() { editor = ED_MEM; }
  356. void mmodephys() { editor = ED_PHYS; }
  357. void mmodelog() { editor = ED_LOG; }
  358.  
  359. void mdiskgo()
  360. {
  361.    Z80 &cpu = CpuMgr.Cpu();
  362.    if (editor == ED_MEM) return;
  363.    for (;;) {
  364.       *(unsigned*)str = mem_disk + 'A';
  365.       if (!inputhex(mem_x+5, mem_y-1, 1, true)) return;
  366.       if (*str >= 'A' && *str <= 'D') break;
  367.    }
  368.    mem_disk = unsigned(*str-'A'); showmem();
  369.    int x = input2(mem_x+12, mem_y-1, mem_track);
  370.    if (x == -1) return;
  371.    mem_track = unsigned(x);
  372.    if (editor == ED_PHYS) return;
  373.    showmem();
  374.    // enter sector
  375.    for (;;) {
  376.       findsector(cpu.mem_curs); x = input2(mem_x+20, mem_y-1, sector);
  377.       if (x == -1) return; if (unsigned(x) < edited_track.s) break;
  378.    }
  379.    for (cpu.mem_curs = 0; x; x--) cpu.mem_curs += edited_track.hdr[x-1].datlen;
  380. }
  381.