ram: ram->ram rom: rom->rom

This commit is contained in:
cr0sd 2020-02-03 15:10:29 -06:00
parent 4cec81e7e9
commit f2d96eda83
7 changed files with 27 additions and 21 deletions

8
65.c
View File

@ -9,7 +9,7 @@ void cpu65_exec(cpu_t*cpu,ram_t*ram)
//printf("pc: 0x%04x\t",cpu->pc);
//printf("ac: 0x%02x",cpu->ac);
//puts("");
printw("0x%04x: 0x%02x\n",cpu->pc,ram->mem[cpu->pc]);
printw("0x%04x: 0x%02x\n",cpu->pc,ram->ram[cpu->pc]);
cpu_exec(cpu,ram);
cpu->pc+=1;
refresh();
@ -55,12 +55,12 @@ int main(int argc,char**argv)
rom_map(rom,ram,PRG_ROM_OFFSET);
refresh();
//for(int i=0;i<100;++i)
//printf("%2x",ram->mem[i]);
//printf("%2x",ram->ram[i]);
//puts("");
}
// Execute ROM
if(rom->data)
if(rom->rom)
{
cpu->pc=PRG_ROM_OFFSET;
// Read instructions for testing purposes
@ -78,7 +78,7 @@ int main(int argc,char**argv)
switch(getchar())
{
case 'i':
printw("ac: 0x%x\n",cpu->ac);
printw("a: 0x%x\n",cpu->a);
break;
case 's':
cpu65_exec(cpu,ram);

8
cpu.c
View File

@ -15,8 +15,14 @@ cpu_t*cpu_init(void)
void cpu_exec(cpu_t*cpu,ram_t*ram)
{
switch(ram->mem[cpu->pc])
// NOTE: Don't handle pc incrementing
// here
switch(ram->ram[cpu->pc])
{
case 0xa9:
cpu->pc+=1;
cpu->a=ram->ram[cpu->pc];
break;
case 0x00:
break;
case 0x0a:

2
cpu.h
View File

@ -13,7 +13,7 @@ typedef struct ram_t ram_t;
typedef struct cpu_t
{
uint16_t pc; // Program counter
uint8_t ac; // Accumulator
uint8_t a; // Accumulator
uint8_t x;
uint8_t y;

8
ram.c
View File

@ -9,8 +9,8 @@ ram_t*ram_init(void)
puterr("%s: Failed to allocate RAM\n",__func__);
return NULL;
}
ram->mem=malloc(0x10000);
if(!ram->mem)
ram->ram=malloc(0x10000);
if(!ram->ram)
{
puterr("%s: Failed to allocate memory for RAM\n",__func__);
//return ram;
@ -21,10 +21,10 @@ ram_t*ram_init(void)
// Free data belonging to ram object
void ram_del(ram_t*ram)
{
if(!ram->mem)
if(!ram->ram)
{
puterr("%s: Attempting to free NULL memory\n",__func__);
return;
}
free(ram->mem);
free(ram->ram);
}

2
ram.h
View File

@ -11,7 +11,7 @@
// for 16-bit address space
typedef struct ram_t
{
uint8_t*mem;
uint8_t*ram;
}ram_t;
ram_t*ram_init(void);

18
rom.c
View File

@ -27,8 +27,8 @@ void rom_load_file(rom_t*rom,const char*filepath)
rom->data_len=len;
// Allocate memory for ROM data
rom->data=malloc(len);
if(!rom->data)
rom->rom=malloc(len);
if(!rom->rom)
{
puterr("%s: Error allocating ROM memory for file data (\"%s\")\n",__func__,filepath);
return;
@ -54,7 +54,7 @@ void rom_load_file(rom_t*rom,const char*filepath)
//lseek(fd,16,SEEK_SET);
//printw("data_len: %d\n",rom->data_len);
read(fd,rom->data,len); // ROM data
read(fd,rom->rom,len); // ROM data
//printw("success reading rom \"%s\" (%u B)\n",filepath,len);
close(fd);
@ -87,12 +87,12 @@ void rom_print_header_info(rom_t*rom)
// offset used to determine where in RAM said ROM data will be mapped
void rom_map(rom_t*rom,ram_t*ram,size_t offset)
{
if(!rom || !rom->data)
if(!rom || !rom->rom)
{
puterr("%s: Attempting to map NULL ROM data into RAM memory\n",__func__);
return;
}
if(!ram || !ram->mem)
if(!ram || !ram->ram)
{
puterr("%s: Attempting to map ROM data into NULL RAM memory\n",__func__);
return;
@ -107,17 +107,17 @@ void rom_map(rom_t*rom,ram_t*ram,size_t offset)
// SIGSEGV crashing the program
const size_t DATA_CHUNK=2048;
for(size_t len=rom->data_len;len>0;len-=(len>DATA_CHUNK)?DATA_CHUNK:len)
memmove(ram->mem+offset,rom->data,(len>DATA_CHUNK)?DATA_CHUNK:len);
//memmove(ram->mem,rom->data,rom->data_len);
memmove(ram->ram+offset,rom->rom,(len>DATA_CHUNK)?DATA_CHUNK:len);
//memmove(ram->ram,rom->rom,rom->data_len);
}
// Free memory allocated to ROM object
void rom_del(rom_t*rom)
{
if(!rom->data)
if(!rom->rom)
{
puterr("%s: Attempting to free NULL memory\n",__func__);
return;
}
free(rom->data);
free(rom->rom);
}

2
rom.h
View File

@ -17,7 +17,7 @@ typedef struct romNES_t
const char*filepath; // File name associated with ROM file on host machine
uint8_t header[16]; // iNes header file
uint8_t trainer[1024]; // iNes (optional) trainer
uint8_t*data; // ROM loaded into (host) RAM
uint8_t*rom; // ROM loaded into (host) RAM
size_t data_len; // Size of data in buffer
}rom_t;