commit de65bbc

zzo38  ·  2022-11-28 23:49:47 +0000 UTC
parent b0fb2c8
Add instruction debugging with breakpoints and single step possibility. (It might be improved in future)
1 files changed,  +87, -6
M main.c
M main.c
+87, -6
  1@@ -226,6 +226,9 @@ static Sint32*audio_option;
  2 static SDL_AudioSpec audiospec;
  3 
  4 static Uint8 debugger=0;
  5+static Uint16 breakpoint=0;
  6+static Uint16 break_pc=0x0100;
  7+static Uint8 debug_op=0;
  8 
  9 static void debug(int i) {
 10   int j;
 11@@ -266,7 +269,7 @@ static void run(Uint16 pc) {
 12   if(!pc) return;
 13   start:
 14   while(v=mem[pc++]) {
 15-    // if(use_debug) printf("! pc=%04X v=%02X ds=%02X rs=%02X\n",pc-1,v,ds.p,rs.p);
 16+    if(pc==breakpoint && use_screen) goto stop;
 17     s=(v&0x40?&rs:&ds);
 18     k=s->p;
 19     switch(v&0x1F) {
 20@@ -331,6 +334,11 @@ static void run(Uint16 pc) {
 21       if(v&0x20) pc=x; else pc+=(Sint8)x;
 22     }
 23   }
 24+  if(pc==breakpoint && use_screen) {
 25+    stop:
 26+    paused=debugger=picture_changed=1;
 27+    break_pc=pc-1;
 28+  }
 29   fflush(stdout);
 30   if(bicycle) {
 31     if(k=device[0].d[2]) {
 32@@ -917,8 +925,13 @@ static void debug_screen(Uint8*s,Uint8 k) {
 33   static Uint8 din;
 34   static Uint32 addr=0;
 35   static Uint32 scrol=0;
 36+  static const char opname[]=
 37+    "?IPNSRDOENGLJJJSLSLSLSDDASMDAOES"
 38+    "?NOIWOUVQETTMCSTDTDTDTEEDUUINROF"
 39+    "?CPPPTPRUQHHPNRHZZRRAAIODBLVDART"
 40+  ;
 41   char buf[81];
 42-  int i;
 43+  int i,j;
 44   Stack*z;
 45   FILE*f;
 46   UxnFile*u;
 47@@ -940,7 +953,7 @@ static void debug_screen(Uint8*s,Uint8 k) {
 48         memcpy(s+80*10,buf,snprintf(buf,81,"<PAUSE> Resume"));
 49         memcpy(s+80*13,buf,snprintf(buf,81,"Pages: CTRL+"));
 50         memcpy(s+80*14,buf,snprintf(buf,81," A:Stack   B:Return  C:Device  D:Memory  E:Files "));
 51-        memcpy(s+80*15,buf,snprintf(buf,81," F:        G:        N:        O:        P:      "));
 52+        memcpy(s+80*15,buf,snprintf(buf,81," F:Code    G:        N:        O:        P:      "));
 53         memcpy(s+80*16,buf,snprintf(buf,81," Q:        R:        S:        T:        U:      "));
 54         memcpy(s+80*17,buf,snprintf(buf,81," V:        W:        X:        Y:        Z:Main  "));
 55         break;
 56@@ -977,8 +990,29 @@ static void debug_screen(Uint8*s,Uint8 k) {
 57           memcpy(s+80*(7*i+6),buf,snprintf(buf,81," <%c> Close  <%c> Rewind","cC"[i],"rR"[i]));
 58         }
 59         break;
 60+      case 6:
 61+        j=break_pc;
 62+        for(i=0;i<16 && j<0x10000;i++) {
 63+          memcpy(s+80*i,buf,snprintf(buf,81,"%04x: %c%c %c%c%c%c%c%c",j,breakpoint==j+1?'*':' ',i?' ':'>',
 64+           opname[mem[j]&0x1F],opname[(mem[j]&0x1F)+0x20],opname[(mem[j]&0x1F)+0x40]
 65+           ,mem[j]&0x20?'2':'_',mem[j]&0x40?'r':'_',mem[j]&0x80?'k':'_' ));
 66+          if(mem[j]==0x80 || mem[j]==0xC0) {
 67+            memcpy(s+80*i+9,buf,snprintf(buf,81,"LIT_%c  %02x",mem[j]&0x40?'r':'_',mem[j+1]));
 68+            j+=2;
 69+          } else if(mem[j]==0xA0 || mem[j]==0xE0) {
 70+            memcpy(s+80*i+9,buf,snprintf(buf,81,"LIT2%c  %02x%02x",mem[j]&0x40?'r':'_',mem[j+1],mem[j+2]));
 71+            j+=3;
 72+          } else {
 73+            if(!mem[j]) memcpy(s+80*i+9,"BRK",3);
 74+            j++;
 75+          }
 76+        }
 77+        memcpy(s+80*17,buf,snprintf(buf,81,"ds:%02x rs:%02x",ds.p,rs.p));
 78+        memcpy(s+80*19,buf,snprintf(buf,81,"<SPACE> Step  <RET> Continue  <x> Execute"));
 79+        memcpy(s+80*20,buf,snprintf(buf,81,"<,> Set Breakpoint  <.> Clear Breakpoint"));
 80+        break;
 81       default:
 82-        if(page<0) page=5; else page=0;
 83+        if(page<0) page=6; else page=0;
 84         goto show;
 85     }
 86   } else {
 87@@ -1062,6 +1096,53 @@ static void debug_screen(Uint8*s,Uint8 k) {
 88         if(k=='r') files_seek(&uxnfile0,8,0);
 89         if(k=='R') files_seek(&uxnfile1,8,0);
 90         break;
 91+      case 6:
 92+        if(k==8 || k==11) --break_pc;
 93+        if(k==12 || k==10) ++break_pc;
 94+        if(k>='0' && k<='9') break_pc=(break_pc<<4)+(k-'0');
 95+        if(k>='a' && k<='f') break_pc=(break_pc<<4)+(k+10-'a');
 96+        if(k==',') breakpoint=break_pc+1;
 97+        if(k=='.') breakpoint=0;
 98+        if(k=='x') run(break_pc);
 99+        if(k==' ') {
100+          j=breakpoint;
101+          breakpoint=break_pc+2;
102+          i=mem[break_pc];
103+          if(i==0x80 || i==0xC0) {
104+            breakpoint=break_pc+3;
105+          } else if(i==0xA0 || i==0xE0) {
106+            breakpoint=break_pc+4;
107+          } else if((i&0x1F)==0x0C || (i&0x1F)==0x0D || (i&0x1F)==0x0E) {
108+            if((i&0x1F)==0x0D) {
109+              i&=0x60;
110+              if(i==0x00 && ds.p>1 && !ds.d[ds.p-2]) goto no_jump;
111+              if(i==0x20 && ds.p>2 && !ds.d[ds.p-3]) goto no_jump;
112+              if(i==0x40 && rs.p>1 && !rs.d[rs.p-2]) goto no_jump;
113+              if(i==0x60 && rs.p>2 && !rs.d[rs.p-3]) goto no_jump;
114+            }
115+            i&=0x60;
116+            if(i==0x00 && ds.p) breakpoint=break_pc+((Sint8)ds.d[ds.p-1])+2;
117+            else if(i==0x20 && ds.p>1) breakpoint=GET16(ds.d+ds.p-2)+1;
118+            else if(i==0x40 && rs.p) breakpoint=break_pc+((Sint8)rs.d[rs.p-1])+2;
119+            else if(i==0x60 && rs.p>1) breakpoint=GET16(rs.d+rs.p-2)+1;
120+            else goto no_run;
121+          } else if(i) {
122+            no_jump:
123+            breakpoint=break_pc+2;
124+          } else {
125+            goto no_run;
126+          }
127+          run(break_pc);
128+          no_run:
129+          breakpoint=j;
130+          break;
131+        }
132+        if(k==13) {
133+          paused=debugger=0;
134+          run(break_pc);
135+          return;
136+        }
137+        break;
138     }
139   }
140 }
141@@ -1171,14 +1252,14 @@ static int run_screen(void) {
142         } else if(i=e.key.keysym.unicode) {
143           device[8].d[3]=i;
144           run(GET16(device[8].d));
145-          device[8].d[3]=0;
146+          if(!debugger) device[8].d[3]=0; else debug_op=8;
147         }
148         break;
149       scroll:
150         if(paused || !i) break;
151         PUT16(device[9].d+12,i);
152         run(GET16(device[9].d));
153-        PUT16(device[9].d+12,0);
154+        if(!debugger) PUT16(device[9].d+12,0); else debug_op=9;
155         break;
156       case SDL_KEYUP:
157         if(paused) break;