prepare for info-window disassembly

This commit is contained in:
corey 2024-05-17 18:53:08 -05:00
parent 17c4fb77ba
commit 2ef5944a76
4 changed files with 139 additions and 113 deletions

235
cpu.c
View File

@ -5,7 +5,24 @@
#include"cpu.h"
#include"ppu.h"
#define qprintf(...) do{if(!quiet)printf(__VA_ARGS__);}while(0)
/* static char log_buffer[10*128]={0}; */
/* static size_t log_buffer_line=0; */
static char log_buffer[10*128]={0};
/* #define qprintf(...) do{\ */
/* if(!quiet){\ */
/* if(log_buffer_line<10)\ */
/* sprintf(log_buffer+(log_buffer_line++),__VA_ARGS__);\ */
/* }}while(0) */
//#define qprintf(...) do{size_t __i=0;if(!quiet){__i=sprintf(log_buffer,"%#x:",(cpu)->pc);sprintf(log_buffer+__i,__VA_ARGS__);}}while(0)
// This causes memory problems, why?
#define qprintf(...) do{if(!quiet){printf("%#x:",(cpu)->pc);printf(__VA_ARGS__);}}while(0)
char*cpu_log(void)
{
return log_buffer;
}
void cpu_fetch8(Cpu*cpu,uint8_t*rom,uint8_t*u8,uint8_t*ram)
{
@ -1813,13 +1830,13 @@ void cpu_decexec(Cpu*cpu,struct Ppu*ppu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool
}
// CB Prefix instructions
void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
void cpu_decexecCB(uint8_t*cpua,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
{
static const uint8_t cbreg[]={2,3,4,5,6,7,0xff,0}; //(op&0xf)%8
static const char*cbrnm[]={"b","c","d","e","h","l","(hl)","a"};
uint16_t tmp;
uint8_t tmp8;
Cpu*stp=(Cpu*)cpu;
Cpu*cpu=(Cpu*)cpua;
//08h - 0fh RRC
if(*op>=0x08 && *op<=0x0f)
@ -1828,39 +1845,39 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
if(tmp8==6)
{
if(ram[stp->h<<8|stp->l]&0x1)stp->f|=F_C;//C
else stp->f&=~F_C;
if(ram[cpu->h<<8|cpu->l]&0x1)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=ram[stp->h<<8|stp->l];
tmp=ram[cpu->h<<8|cpu->l];
if(stp->f&F_C)asm(" stc");
if(cpu->f&F_C)asm(" stc");
else asm(" clc");
asm("mov %0,%%ax":"=m"(tmp));
asm("rcr %al");
asm("mov %%ax,%0":"=m"(tmp));
ram[stp->h<<8|stp->l]=(uint8_t)tmp;
ram[cpu->h<<8|cpu->l]=(uint8_t)tmp;
}
else
{
if(cpu[cbreg[tmp8]]&0x1)stp->f|=F_C;//C
else stp->f&=~F_C;
if(cpua[cbreg[tmp8]]&0x1)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=cpu[cbreg[tmp8]];
tmp=cpua[cbreg[tmp8]];
if(stp->f&F_C)asm(" stc");
if(cpu->f&F_C)asm(" stc");
else asm(" clc");
asm("mov %0,%%ax":"=m"(tmp));
asm("rcr %al");
asm("mov %%ax,%0":"=m"(tmp));
cpu[cbreg[tmp8]]=(uint8_t)tmp;
cpua[cbreg[tmp8]]=(uint8_t)tmp;
}
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!(uint8_t)tmp)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!(uint8_t)tmp)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("rrc %s",cbrnm[(*op&0xf)%8]);
}
@ -1873,39 +1890,39 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
if(tmp8==6)
{
if(ram[stp->h<<8|stp->l]&0x80)stp->f|=F_C;//C
else stp->f&=~F_C;
if(ram[cpu->h<<8|cpu->l]&0x80)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=ram[stp->h<<8|stp->l];
tmp=ram[cpu->h<<8|cpu->l];
if(stp->f&F_C)asm(" stc");
if(cpu->f&F_C)asm(" stc");
else asm("clc");
asm("mov %0,%%ax":"=m"(tmp));
asm("rcl %al");
asm("mov %%ax,%0":"=m"(tmp));
ram[stp->h<<8|stp->l]=(uint8_t)tmp;
ram[cpu->h<<8|cpu->l]=(uint8_t)tmp;
}
else
{
if(cpu[cbreg[tmp8]]&0x80)stp->f|=F_C;//C
else stp->f&=~F_C;
if(cpua[cbreg[tmp8]]&0x80)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=cpu[cbreg[tmp8]];
tmp=cpua[cbreg[tmp8]];
if(stp->f&F_C)asm(" stc");
if(cpu->f&F_C)asm(" stc");
else asm(" clc");
asm("mov %0,%%ax":"=m"(tmp));
asm("rcl %al");
asm("mov %%ax,%0":"=m"(tmp));
cpu[cbreg[tmp8]]=(uint8_t)tmp;
cpua[cbreg[tmp8]]=(uint8_t)tmp;
}
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!(uint8_t)tmp)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!(uint8_t)tmp)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("rlc %s",cbrnm[(*op&0xf)%8]);
}
@ -1917,11 +1934,11 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
if(tmp8==6)
{
uint8_t cf=stp->f&F_C;
if(ram[stp->h<<8|stp->l]&0x1)stp->f|=F_C;//C
else stp->f&=~F_C;
uint8_t cf=cpu->f&F_C;
if(ram[cpu->h<<8|cpu->l]&0x1)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=ram[stp->h<<8|stp->l];
tmp=ram[cpu->h<<8|cpu->l];
if(cf)asm("stc");
@ -1930,15 +1947,15 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
asm("rcr %al");
asm("mov %%ax,%0":"=m"(tmp));
ram[stp->h<<8|stp->l]=(uint8_t)tmp;
ram[cpu->h<<8|cpu->l]=(uint8_t)tmp;
}
else
{
uint8_t cf=stp->f&F_C;
if(cpu[cbreg[tmp8]]&0x1)stp->f|=F_C;//C
else stp->f&=~F_C;
uint8_t cf=cpu->f&F_C;
if(cpua[cbreg[tmp8]]&0x1)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=cpu[cbreg[tmp8]];
tmp=cpua[cbreg[tmp8]];
if(cf)asm(" stc");
else asm(" clc");
@ -1946,13 +1963,13 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
asm("rcr %al");
asm("mov %%al,%0":"=m"(tmp));
cpu[cbreg[tmp8]]=(uint8_t)tmp;
cpua[cbreg[tmp8]]=(uint8_t)tmp;
}
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!(uint8_t)tmp)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!(uint8_t)tmp)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("rr %s",cbrnm[(*op&0xf)%8]);
}
@ -1964,11 +1981,11 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
if(tmp8==6)
{
uint8_t cf=stp->f&F_C;
if(ram[stp->h<<8|stp->l]&0x80)stp->f|=F_C;//C
else stp->f&=~F_C;
uint8_t cf=cpu->f&F_C;
if(ram[cpu->h<<8|cpu->l]&0x80)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=ram[stp->h<<8|stp->l];
tmp=ram[cpu->h<<8|cpu->l];
if(cf)asm(" stc");
@ -1977,15 +1994,15 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
asm("rcl %al");
asm("mov %%al,%0":"=m"(tmp));
ram[stp->h<<8|stp->l]=(uint8_t)tmp;
ram[cpu->h<<8|cpu->l]=(uint8_t)tmp;
}
else
{
uint8_t cf=stp->f&F_C;
if(cpu[cbreg[tmp8]]&0x80)stp->f|=F_C;//C
else stp->f&=~F_C;
uint8_t cf=cpu->f&F_C;
if(cpua[cbreg[tmp8]]&0x80)cpu->f|=F_C;//C
else cpu->f&=~F_C;
tmp=cpu[cbreg[tmp8]];
tmp=cpua[cbreg[tmp8]];
if(cf)asm("stc");
@ -1994,13 +2011,13 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
asm("rcl %al");
asm("mov %%al,%0":"=m"(tmp));
cpu[cbreg[tmp8]]=(uint8_t)tmp;
cpua[cbreg[tmp8]]=(uint8_t)tmp;
}
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!(uint8_t)tmp)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!(uint8_t)tmp)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("rl %s",cbrnm[(*op&0xf)%8]);
}
@ -2013,21 +2030,21 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
{
//NOTE: We are depending on arithmetic shift here,
// sra is a 'signed' operation
tmp=ram[stp->h<<8|stp->l]&0x1;
tmp8=ram[stp->h<<8|stp->l]>>=1;
tmp=ram[cpu->h<<8|cpu->l]&0x1;
tmp8=ram[cpu->h<<8|cpu->l]>>=1;
}
else
{
tmp=cpu[cbreg[tmp8]]&0x1;
tmp8=cpu[cbreg[tmp8]]>>=1;
tmp=cpua[cbreg[tmp8]]&0x1;
tmp8=cpua[cbreg[tmp8]]>>=1;
}
if(tmp)stp->f|=F_C;//C
else stp->f&=~F_C;
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!tmp8)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
if(tmp)cpu->f|=F_C;//C
else cpu->f&=~F_C;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!tmp8)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("sra %s",cbrnm[(*op&0xf)%8]);
}
@ -2038,22 +2055,22 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
tmp8=(*op&0xf)%8;
if(tmp8==6)
{
tmp=ram[stp->h<<8|stp->l]&0x80;
tmp8=ram[stp->h<<8|stp->l]<<=1;
tmp=ram[cpu->h<<8|cpu->l]&0x80;
tmp8=ram[cpu->h<<8|cpu->l]<<=1;
}
else
{
tmp=cpu[cbreg[tmp8]]&0x80;
tmp8=cpu[cbreg[tmp8]]<<=1;
tmp=cpua[cbreg[tmp8]]&0x80;
tmp8=cpua[cbreg[tmp8]]<<=1;
}
if(tmp)stp->f|=F_C;//C
else stp->f&=~F_C;
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!tmp8)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
if(tmp)cpu->f|=F_C;//C
else cpu->f&=~F_C;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!tmp8)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("sla %s",cbrnm[(*op&0xf)%8]);
}
@ -2066,22 +2083,22 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
{
//NOTE: We are depending on logical shift here,
// srl is an 'unsigned' operation
tmp=ram[stp->h<<8|stp->l]&0x1;
tmp8=ram[stp->h<<8|stp->l]>>=1;
tmp=ram[cpu->h<<8|cpu->l]&0x1;
tmp8=ram[cpu->h<<8|cpu->l]>>=1;
}
else
{
tmp=cpu[cbreg[tmp8]]&0x1;
tmp8=cpu[cbreg[tmp8]]>>=1;
tmp=cpua[cbreg[tmp8]]&0x1;
tmp8=cpua[cbreg[tmp8]]>>=1;
}
if(tmp)stp->f|=F_C;//C
else stp->f&=~F_C;
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!tmp8)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
if(tmp)cpu->f|=F_C;//C
else cpu->f&=~F_C;
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!tmp8)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("srl %s",cbrnm[(*op&0xf)%8]);
}
@ -2090,22 +2107,22 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
if(*op>=0x30 && *op<=0x37)
{
tmp8=(*op&0xf)%8;
if(tmp8==6)tmp=ram[stp->h<<8|stp->l];
else tmp=cpu[cbreg[tmp8]];//reg
if(tmp8==6)tmp=ram[cpu->h<<8|cpu->l];
else tmp=cpua[cbreg[tmp8]];//reg
asm("mov %0,%%al":"=m"(tmp));
ror(tmp,4);
//asm("rorb %0,%%al");
asm("mov %%al,%0":"=m"(tmp));
if(tmp8==6)ram[stp->h<<8|stp->l]=(uint8_t)tmp;
else cpu[cbreg[tmp8]]=(uint8_t)tmp;//reg
if(tmp8==6)ram[cpu->h<<8|cpu->l]=(uint8_t)tmp;
else cpua[cbreg[tmp8]]=(uint8_t)tmp;//reg
stp->f&=~F_C;//C
stp->f&=~F_N;//N
stp->f&=~F_H;//H
if(!(uint8_t)tmp)stp->f|=F_Z;//Z
else stp->f&=~F_Z;
cpu->f&=~F_C;//C
cpu->f&=~F_N;//N
cpu->f&=~F_H;//H
if(!(uint8_t)tmp)cpu->f|=F_Z;//Z
else cpu->f&=~F_Z;
qprintf("swap %s ; %.2xh",cbrnm[(*op&0xf)%8],(uint8_t)tmp);
}
@ -2115,13 +2132,13 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
{
tmp=((*op&0xf0)>>4)%4*2+((*op&0x0f)>=8);//bit
tmp8=(*op&0xf)%8;
if(tmp8==6)tmp8=ram[stp->h<<8|stp->l];
else tmp8=cpu[cbreg[tmp8]];//reg
if(tmp8&0x1<<tmp)stp->f&=~F_Z;//Z
else stp->f|=F_Z;
stp->f|=F_H;//H
stp->f&=~F_N;//N
qprintf("bit %u,%s ;%x (%x)",tmp,cbrnm[(*op&0xf)%8],stp->f&F_Z,tmp8&0x1<<tmp);
if(tmp8==6)tmp8=ram[cpu->h<<8|cpu->l];
else tmp8=cpua[cbreg[tmp8]];//reg
if(tmp8&0x1<<tmp)cpu->f&=~F_Z;//Z
else cpu->f|=F_Z;
cpu->f|=F_H;//H
cpu->f&=~F_N;//N
qprintf("bit %u,%s ;%x (%x)",tmp,cbrnm[(*op&0xf)%8],cpu->f&F_Z,tmp8&0x1<<tmp);
return;
}
@ -2131,8 +2148,8 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
tmp=((*op&0xf0)>>4)%4*2+((*op&0x0f)>=8);//bit
tmp8=(*op&0xf)%8;
if(tmp8==6)ram[stp->h<<8|stp->l]&=~(1<<tmp);
else cpu[cbreg[tmp8]]&=~(1<<tmp);//reg
if(tmp8==6)ram[cpu->h<<8|cpu->l]&=~(1<<tmp);
else cpua[cbreg[tmp8]]&=~(1<<tmp);//reg
qprintf("res %u,%s",tmp,cbrnm[(*op&0xf)%8]);
return;
@ -2144,8 +2161,8 @@ void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet)
tmp=((*op&0xf0)>>4)%4*2+((*op&0x0f)>=8);//bit
tmp8=(*op&0xf)%8;
if(tmp8==6)ram[stp->h<<8|stp->l]|=(1<<tmp);
else cpu[cbreg[tmp8]]|=(1<<tmp);//reg
if(tmp8==6)ram[cpu->h<<8|cpu->l]|=(1<<tmp);
else cpua[cbreg[tmp8]]|=(1<<tmp);//reg
qprintf("set %u,%s",tmp,cbrnm[(*op&0xf)%8]);
return;

1
cpu.h
View File

@ -24,6 +24,7 @@ typedef struct Cpu
uint8_t ime;
} Cpu;
char*cpu_log(void);
void cpu_decexec(Cpu*cpu,struct Ppu*ppu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet);
void cpu_decexecCB(uint8_t*cpu,uint8_t*rom,uint8_t*op,uint8_t*ram,bool quiet);
void cpu_fetch16(Cpu*cpu,uint8_t*rom,uint16_t*u16,uint8_t*ram);

5
main.c
View File

@ -22,6 +22,7 @@ int main(int argc,char **argv)
// Allocate Gb state
gb=gb_new();
if(!gb.cpu||!gb.ppu||!gb.ram)goto quit;
//gb.log=true;
// Parse arguments
for(size_t i=1;i<argc;++i)
@ -111,8 +112,8 @@ int main(int argc,char **argv)
//gb.cpu->pc=0x0040;
//}
if(gb.log)
printf("\n[%#10.8x]%#5.2x: ",gb.cpu->pc,gb.rom[gb.cpu->pc]);
/* if(gb.log) */
/* printf("\n[%#10.8x]%#5.2x: ",gb.cpu->pc,gb.rom[gb.cpu->pc]); */
// Fetch, decode, execute instruction
cpu_fetch8(gb.cpu,gb.rom,&gb.op,gb.ram);

11
ppu.c
View File

@ -57,8 +57,12 @@ uint32_t ppu_updatewindow(struct Gb*gb,uint8_t*ram)
delay++;
if(delay%400!=0)return 0;
if(!tigrClosed(gb->ppu->screen) && !tigrKeyHeld(gb->ppu->screen,TK_ESCAPE) && !tigrClosed(gb->ppu->info) && !tigrKeyHeld(gb->ppu->info,TK_ESCAPE))
if(!tigrClosed(gb->ppu->screen) && !tigrClosed(gb->ppu->info))
{
if(tigrKeyHeld(gb->ppu->screen,TK_ESCAPE)||tigrKeyHeld(gb->ppu->info,TK_ESCAPE))
return -1;
if(tigrKeyHeld(gb->ppu->screen,'Q')||tigrKeyHeld(gb->ppu->info,'Q'))
return -1;
// Draw graphical/main screen
{
@ -117,16 +121,19 @@ uint32_t ppu_updatewindow(struct Gb*gb,uint8_t*ram)
break;
}
// Can we capture from cpu_decexec at certain intervals?
// (with a buffer)
case M_DASM:
{
static char str[128];
strncpy(str,cpu_log(),128);
print(0,12,str);
break;
}
}
}
tigrUpdate(gb->ppu->info);
tigrUpdate(gb->ppu->screen);
}