void Z80_Debug (Z80_Regs *R) { byte buf[5]; char flags[]="SZ.H.VNC"; char S[20],f[9]; int i; buf[0]=Z80_RDOP(R->PC.D); i=1; /* Figure out which bytes are opcode arguments and which are not */ switch (buf[0]) { case 0xCB: case 0xED: buf[1]=Z80_RDOP((R->PC.D+1)&0xFFFF); i=2; break; case 0xDD: case 0xFD: buf[1]=Z80_RDOP((R->PC.D+1)&0xFFFF); i=2; if (buf[1]==0xCB) { buf[2]=Z80_RDOP_ARG((R->PC.D+2)&0xFFFF); buf[3]=Z80_RDOP((R->PC.D+3)&0xFFFF); i=4; } } for (;i<5;++i) { buf[i]=Z80_RDOP_ARG((R->PC.D+i)&0xFFFF); } Z80_Dasm (buf,S,R->PC.D); for (i=strlen(S);i<19;++i) S[i]=' '; S[19]='\0'; for (i=7;i>=0;--i) { f[7-i]=(R->AF.D&(1<<i))? flags[7-i]:'.'; } f[8]='\0'; printf ("%04X: %s AT SP: %04X %04X FLAGS: %s\n",R->PC.D,S, Z80_RDMEM(R->SP.D)+Z80_RDMEM((R->SP.D+1)&0xFFFF)*256, Z80_RDMEM((R->SP.D+2)&0xFFFF)+Z80_RDMEM((R->SP.D+3)&0xFFFF)*256,f); printf ("%27sAF:%04X BC:%04X DE:%04X HL:%04X IX:%04X IY:%04X\n", "",R->AF.D,R->BC.D,R->DE.D,R->HL.D,R->IX.D,R->IY.D); }
int main (int argc,char *argv[]) { int i,j,n; char *filename=NULL,buf[20]; unsigned char *filebuf; FILE *f; unsigned begin=0,end=(unsigned)-1,offset=0,filelen,len,pc; printf ("z80dasm: Portable Z80 disassembler\n" "Copyright (C) Marcel de Kogel 1996,1997\n"); for (i=1,n=0;i<argc;++i) { if (argv[i][0]!='-') { switch (++n) { case 1: filename=argv[i]; break; default: usage(); } } else { for (j=0;Options[j];++j) if (!strcmp(argv[i]+1,Options[j])) break; switch (j) { case 0: ++i; if (i>argc) usage(); begin=strtoul(argv[i],NULL,16); break; case 1: ++i; if (i>argc) usage(); end=strtoul(argv[i],NULL,16); break; case 2: ++i; if (i>argc) usage(); offset=strtoul(argv[i],NULL,16); break; default: usage(); } } } if (!filename) { usage(); return 1; } f=fopen (filename,"rb"); if (!f) { printf ("Unable to open %s\n",filename); return 2; } fseek (f,0,SEEK_END); filelen=ftell (f); fseek (f,begin,SEEK_SET); len=(filelen>end)? (end-begin+1):(filelen-begin); filebuf=malloc(len+16); if (!filebuf) { printf ("Memory allocation error\n"); fclose (f); return 3; } memset (filebuf,0,len+16); if (fread(filebuf,1,len,f)!=len) { printf ("Read error\n"); fclose (f); free (filebuf); return 4; } fclose (f); pc=0; while (pc<len) { i=Z80_Dasm (filebuf+pc,buf,pc+offset); for (j=strlen(buf);j<19;++j) buf[j]=' '; buf[19]='\0'; printf (" %s ; %06X ",buf,pc+offset); for (j=0;j<i;++j) printf("%02X ",filebuf[pc+j]); printf ("\n"); pc+=i; } free (filebuf); return 0; }