@ -12,7 +12,7 @@ unsigned char bus[BUS_SIZE]; //this is the bus array
#include"cartridge.h"
#include"cartridge.h"
wordmapper_resolve_read(wordaddress){//info about what regions are mirrored can be found on the nes dev wiki
wordmapperResolveRead(wordaddress){//info about what regions are mirrored can be found on the nes dev wiki
if(address<=0x1FFF&&address>=0x0800)
if(address<=0x1FFF&&address>=0x0800)
address%=0x0800;
address%=0x0800;
@ -23,7 +23,7 @@ word mapper_resolve_read(word address){ //info about what regions are mirrored c
returnaddress;
returnaddress;
}
}
wordmapper_resolve_write(wordaddress,bytedata){//info about what regions are mirrored can be found on the nes dev wiki
wordmapperResolveWrite(wordaddress,bytedata){//info about what regions are mirrored can be found on the nes dev wiki
if(address<=0x1FFF&&address>=0x0800)
if(address<=0x1FFF&&address>=0x0800)
address%=0x0800;
address%=0x0800;
@ -34,41 +34,41 @@ word mapper_resolve_write(word address, byte data){ //info about what regions ar
returnaddress;
returnaddress;
}
}
voidbus_write8(wordaddress,worddata){
voidbusWrite8(wordaddress,worddata){
if(!mapper000_write(address,data,false)){//first thing we do is we hand the operation to the mapper to resolve any cartridge-side bank switching and mirroring, if the address we wanna write to isnt on the cartridge, we return false and we write to the bus normally
if(!mapper000_Write(address,data,false)){//first thing we do is we hand the operation to the mapper to resolve any cartridge-side bank switching and mirroring, if the address we wanna write to isnt on the cartridge, we return false and we write to the bus normally
address=mapper_resolve_write(address,data);//a lot of regions on the NES bus are mirrored/synced, this just ensures we are always writing to the parent region, not to a empty cloned one
address=mapperResolveWrite(address,data);//a lot of regions on the NES bus are mirrored/synced, this just ensures we are always writing to the parent region, not to a empty cloned one
#ifdef DEBUG
#ifdef DEBUG
printf("\nwrite-ram at 0x%04X val = 0x%04X\n; old_val 0x%04X",address,data,bus_read8(address));
printf("\nwrite-ram at 0x%04X val = 0x%04X\n; old_val 0x%04X",address,data,busRead8(address));
#endif
#endif
bus[address]=(unsignedchar)data;
bus[address]=(unsignedchar)data;
}
}
}
}
wordbus_read8(wordaddress){
wordbusRead8(wordaddress){
worddata;
worddata;
if((data=mapper000_read(address,false))>=0x100){//we first ask the mapper to read the data from the address for us in case its on the cartridge, if it returns 0x100 (0xFF + 1 aka impossible to get from reading a byte) that means the data stored at that address is not on the cartridge, but rather on the nes memory, thus we hand the job over to the bus
if((data=mapper000_Read(address,false))>=0x100){//we first ask the mapper to read the data from the address for us in case its on the cartridge, if it returns 0x100 (0xFF + 1 aka impossible to get from reading a byte) that means the data stored at that address is not on the cartridge, but rather on the nes memory, thus we hand the job over to the bus
address=mapper_resolve_read(address);//a lot of regions on the NES bus are mirrored/synced, this just ensures we are always writing to the parent region, not to a empty cloned one
address=mapperResolveRead(address);//a lot of regions on the NES bus are mirrored/synced, this just ensures we are always writing to the parent region, not to a empty cloned one
returnbus[address];
returnbus[address];
}else{
}else{
returndata;
returndata;
}
}
}
}
wordbus_read16(wordaddress){
wordbusRead16(wordaddress){
wordd=bus_read8(address+1);//read msb
wordd=busRead8(address+1);//read msb
d<<=8;//put msb in the msb section
d<<=8;//put msb in the msb section
d|=bus_read8(address);//read lsb
d|=busRead8(address);//read lsb
returnd;//return whole word
returnd;//return whole word
}
}
voidbus_write16(wordaddress,worddata){
voidbusWrite16(wordaddress,worddata){
bus_write8(address,data>>8);//write msb
busWrite8(address,data>>8);//write msb
bus_write8(address+1,data&0b00001111);//write lsb
busWrite8(address+1,data&0b00001111);//write lsb
}
}
voiddump_bus(){
voiddumpBus(){
FILE*fdump;
FILE*fdump;
fdump=fopen("dumpfile","w");
fdump=fopen("dumpfile","w");
@ -81,7 +81,7 @@ void dump_bus(){
//If the file was successfuly opened then this code will run
//If the file was successfuly opened then this code will run
for(unsignedlonglongi=0;i<=0xFFFF;i++)
for(unsignedlonglongi=0;i<=0xFFFF;i++)
fprintf(fdump,"%c",bus_read8(i));
fprintf(fdump,"%c",busRead8(i));
}
}
bytedebug_read_do_not_use_pls(wordaddress){
bytedebug_read_do_not_use_pls(wordaddress){
@ -111,7 +111,7 @@ int main(int argc, char * argv[]){
}
}
#endif
#endif
init_cpu(cpu);//put new CPU in starting mode and dock it to the bus
initCpu(cpu);//put new CPU in starting mode and dock it to the bus
cpu->PC=PROG_START_ADDR;
cpu->PC=PROG_START_ADDR;
if(argc<=1){//Check to see if a rom was given
if(argc<=1){//Check to see if a rom was given
@ -120,12 +120,12 @@ int main(int argc, char * argv[]){
}
}
//load the CHR and PRG banks from the .nes file (argv[1]), also loads the header for mapper construction
//load the CHR and PRG banks from the .nes file (argv[1]), also loads the header for mapper construction
init_banks(argv[1]);
initBanks(argv[1]);
#ifdef DEBUG
#ifdef DEBUG
//Test to see if writing to the bus is working correctly
//Test to see if writing to the bus is working correctly
init_opcodereg(cpu);//import the stuff about each microcode, stuff like bytes per instruction, cycles, adressing mode, and operation in the array, where the value in the array is the byte that triggers that action for the CPU
init_opcodereg(cpu);//import the stuff about each microcode, stuff like bytes per instruction, cycles, adressing mode, and operation in the array, where the value in the array is the byte that triggers that action for the CPU
}
}
voidcpu_clock(CPU*cpu){
voidcpuClock(CPU*cpu){
#ifdef DEBUG
#ifdef DEBUG
handle_errors(cpu);
handleErrors(cpu);
#endif
#endif
cpu->pcNeedsInc=true;
cpu->pcNeedsInc=true;
wordargs=0;
wordargs=0;
if(cpu->opcodes[bus_read8(cpu->PC)].bytes>2){//if args is 16bit shifts the first byte to the hi byte (they get reversed basically)
if(cpu->opcodes[busRead8(cpu->PC)].bytes>2){//if args is 16bit shifts the first byte to the hi byte (they get reversed basically)