video+log flags, close romfile
This commit is contained in:
parent
cff192ea1c
commit
8b4e98dac7
3
gb.c
3
gb.c
|
@ -10,6 +10,8 @@ Gb gb_new(void)
|
|||
|
||||
gb.paused=false;
|
||||
gb.nclocks=clock();
|
||||
gb.log=false;
|
||||
gb.video=true;
|
||||
|
||||
gb.cpu=malloc(sizeof(struct Cpu));
|
||||
if(!gb.cpu)
|
||||
|
@ -104,4 +106,5 @@ void gb_print_cpu_state(Gb*gb)
|
|||
printf("\nOAM Sprite 0 Tile: %.2x X: %.2x Y: %.2x",gb->ram[0xfe02],gb->ram[0xfe00],gb->ram[0xfe01]);
|
||||
printf("\nOAM Sprite 1 Tile: %.2x X: %.2x Y: %.2x",gb->ram[0xfe06],gb->ram[0xfe04],gb->ram[0xfe05]);
|
||||
printf("\nOAM Sprite 16 Tile: %.2x X: %.2x Y: %.2x",gb->ram[0xfe12],gb->ram[0xfe10],gb->ram[0xfe11]);
|
||||
printf("\n");
|
||||
}
|
||||
|
|
2
gb.h
2
gb.h
|
@ -17,6 +17,8 @@ typedef struct Gb
|
|||
uint8_t *rom;
|
||||
uint8_t op;
|
||||
uint8_t paused;
|
||||
bool video;
|
||||
bool log;
|
||||
} Gb;
|
||||
|
||||
Gb gb_new(void);
|
||||
|
|
82
main.c
82
main.c
|
@ -9,48 +9,41 @@
|
|||
#include"ppu.h"
|
||||
#include"gb.h"
|
||||
|
||||
#define VIDEO
|
||||
//#define PRINTING
|
||||
#define _min(x,y) (((x)<(y))?(x):(y))
|
||||
#define _max(x,y) (((x)>(y))?(x):(y))
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
FILE *f;
|
||||
FILE *romfile;
|
||||
Gb gb;
|
||||
bool hexdumponly=false;
|
||||
char*fname=NULL;
|
||||
|
||||
// Allocate Gb state
|
||||
gb=gb_new();
|
||||
if(!gb.cpu||!gb.ppu||!gb.ram)goto quit;
|
||||
gb.nclocks=clock();
|
||||
gb.paused=false;
|
||||
|
||||
// Parse arguments
|
||||
for(size_t i=1;i<argc;++i)
|
||||
{
|
||||
|
||||
for(size_t i=1;i<argc;++i)
|
||||
{
|
||||
|
||||
if(strcmp(argv[i],"-d")==0)
|
||||
{
|
||||
hexdumponly=true;
|
||||
}
|
||||
|
||||
// Default action: treat as ROM file name
|
||||
else
|
||||
{
|
||||
fname=argv[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Try to open ROM file
|
||||
if(fname)
|
||||
{
|
||||
f=fopen(argv[1],"rb");
|
||||
if(!f)
|
||||
romfile=fopen(argv[1],"rb");
|
||||
if(!romfile)
|
||||
{
|
||||
fprintf(stderr,"error: failed to open file '%s'\n",argv[1]);
|
||||
goto quit;
|
||||
|
@ -63,18 +56,19 @@ int main(int argc,char **argv)
|
|||
}
|
||||
|
||||
// Allocate memory for entire ROM file
|
||||
fseek(f,0,SEEK_END);
|
||||
gb.romsize=ftell(f);
|
||||
if(0x8000>gb.romsize)gb.romsize=0x8000;
|
||||
fseek(romfile,0,SEEK_END);
|
||||
gb.romsize=ftell(romfile);
|
||||
gb.romsize=_max(gb.romsize,0x8000);
|
||||
gb.rom=malloc(gb.romsize);
|
||||
if(!gb.rom)return 2;
|
||||
|
||||
// Map ROM file into memory
|
||||
rewind(f);
|
||||
fread(gb.rom,1,gb.romsize,f);
|
||||
rewind(romfile);
|
||||
fread(gb.rom,1,gb.romsize,romfile);
|
||||
printf("Loaded ROM \'%s\' (%u B)\n",argv[1],gb.romsize);
|
||||
printf("ROM Size: %ukB\n",32<<gb.rom[0x148]);
|
||||
printf("RAM Size: %ukB\n",16<<gb.rom[0x149]*2);
|
||||
fclose(romfile);
|
||||
|
||||
// Hex dump only
|
||||
if(hexdumponly)
|
||||
|
@ -83,7 +77,7 @@ int main(int argc,char **argv)
|
|||
goto quit;
|
||||
}
|
||||
|
||||
//load program into RAM
|
||||
// Load program into RAM
|
||||
for(int i=0;i<0x8000;i++)
|
||||
{
|
||||
gb.ram[i]=gb.rom[i];
|
||||
|
@ -92,43 +86,38 @@ int main(int argc,char **argv)
|
|||
printf("Loaded 8000h (32768) bytes into RAM\n");
|
||||
|
||||
// Open window
|
||||
#ifdef VIDEO
|
||||
ppu_openwindow(gb.ppu,gb.rom+0x134);
|
||||
#endif
|
||||
if(gb.video)
|
||||
ppu_openwindow(gb.ppu,gb.rom+0x134);
|
||||
|
||||
gb.ram[0xff4f]=0;//VRAM bank
|
||||
|
||||
// for(int i=0;i<70&&((gb.cpu.pc)<gb.romsize);i++)
|
||||
// VRAM bank
|
||||
gb.ram[0xff4f]=0;
|
||||
|
||||
// Main CPU loop
|
||||
for(;;)
|
||||
{
|
||||
#ifdef VIDEO
|
||||
if(ppu_updatewindow(&gb,gb.ram))break;
|
||||
#endif
|
||||
if(gb.video)
|
||||
if(ppu_updatewindow(&gb,gb.ram))break;
|
||||
if(gb.paused)continue;
|
||||
|
||||
//Control Registers--
|
||||
gb.ram[0xff44]+=1;//CURLINE (VBLANK INT AT:144-153)
|
||||
//gb.ram[0xff05]+=1;//TIMA (TIMER INT AT OVERFLOW)
|
||||
// if(!gb.ram[0xff05])gb.ram[0xff05]=gb.ram[0xff06];
|
||||
gb.ram[0xff41]=(gb.ram[0xff41]+1)%4;//V-BLANK
|
||||
// Manage Control Registers
|
||||
gb.ram[0xff44]+=1; // CURLINE (VBLANK INT AT:144-153)
|
||||
//gb.ram[0xff05]+=1; // TIMA (TIMER INT AT OVERFLOW)
|
||||
//if(!gb.ram[0xff05])gb.ram[0xff05]=gb.ram[0xff06];
|
||||
gb.ram[0xff41]=(gb.ram[0xff41]+1)%4; // V-BLANK
|
||||
|
||||
|
||||
//Interrupts--
|
||||
//if(gb.ram[0xff44]==144 && gb.ram[0xffff] && gb.cpu.ime)//V-BLANK INT
|
||||
// Handle Interrupts
|
||||
//if(gb.ram[0xff44]==144 && gb.ram[0xffff] && gb.cpu->ime) // V-BLANK INT
|
||||
//{
|
||||
// gb.cpu.sp-=2;//push pc, jump to 0040h interrupt
|
||||
// cpu_write_ram(gb.cpu,gb.ppu,gb.cpu.sp,gb.cpu.pc&0x00ff,gb.ram);
|
||||
// cpu_write_ram(gb.cpu,gb.ppu,gb.cpu.sp+1,gb.cpu.pc>>8,gb.ram);
|
||||
// gb.cpu.pc=0x0040;
|
||||
//gb.cpu->sp-=2; // Push pc, jump to 0040h interrupt
|
||||
//cpu_write_ram(gb.cpu,gb.ppu,gb.cpu->sp,gb.cpu->pc&0x00ff,gb.ram);
|
||||
//cpu_write_ram(gb.cpu,gb.ppu,gb.cpu->sp+1,gb.cpu->pc>>8,gb.ram);
|
||||
//gb.cpu->pc=0x0040;
|
||||
//}
|
||||
|
||||
//if(gb.cpu.pc==0xff82)MessageBoxA(NULL,"FF82","-",MB_OK);
|
||||
if(gb.log)
|
||||
printf("\n[%#10.8x]%#5.2x: ",gb.cpu->pc,gb.rom[gb.cpu->pc]);
|
||||
|
||||
#ifdef PRINTING
|
||||
printf("\n[%#10.8x]%#5.2x: ",gb.cpu.pc,gb.rom[gb.cpu.pc]);
|
||||
#endif
|
||||
// Fetch, decode, execute instruction
|
||||
cpu_fetch8(gb.cpu,gb.rom,&gb.op,gb.ram);
|
||||
cpu_decexec(gb.cpu,gb.ppu,gb.rom,&gb.op,gb.ram);
|
||||
}
|
||||
|
@ -136,8 +125,7 @@ int main(int argc,char **argv)
|
|||
// Clean up, exit
|
||||
quit:
|
||||
gb_print_cpu_state(&gb);
|
||||
#ifdef VIDEO
|
||||
ppu_closewindow(gb.ppu);
|
||||
#endif
|
||||
if(gb.video)
|
||||
ppu_closewindow(gb.ppu);
|
||||
gb_free(&gb);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user