From 3ecac1636f30050c35ac5637398a91faab12d891 Mon Sep 17 00:00:00 2001 From: Joaquin Date: Fri, 16 Dec 2022 00:01:48 +0200 Subject: [PATCH] [ppu] Make struct pack bits for proper union If not for the attribute flag, the proccesor would automatically makes padding between the elements of the struct since they jump over the byte boundary for efficiency purposes, making all elements easier to acces for the cpu. This however destroys our entire union, since its made with no paddin in mind, the attribute flag removes the padding +Makefile cleaning +*Reg.bits renamed to more appropriate .field --- .gitignore | 4 +++- Makefile | 9 +++++---- bus.c | 7 +++++++ ppu.c | 16 ++++++++-------- ppu.h | 18 ++++++++++++------ 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index aa88328..75a2089 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ media exec +nesem *log* *cache* *.patch *.out -*dump* \ No newline at end of file +*dump* +*.json diff --git a/Makefile b/Makefile index cdcb4c8..f0b9489 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,17 @@ CC = cc -CFLAGS = -g -DDEBUG -static +CFLAGS = -static -Ofast +CDEBUGFLAGS = -g -DDEBUG -static RM = rm -rf -OUTFILE = exec +OUTFILE = nesem default: all all: - $(CC) bus.c cpu.c cartridge.c ppu.c -Ofast -o $(OUTFILE) + $(CC) $(CFLAGS) bus.c cpu.c cartridge.c ppu.c -o $(OUTFILE) clean: $(RM) $(OUTFILE) debug: - $(CC) $(CFLAGS) bus.c cpu.c ppu.c cartridge.c -o $(OUTFILE) + $(CC) $(CDEBUGFLAGS) bus.c cpu.c ppu.c cartridge.c -o $(OUTFILE) diff --git a/bus.c b/bus.c index 7452151..3d67808 100644 --- a/bus.c +++ b/bus.c @@ -112,6 +112,13 @@ void activateCpuNmi(){ int main(int argc, char * argv[]){ + #ifdef DEBUG + locationRegister testbit; + testbit.data = 0; + testbit.field.fineY = 0xFF; + printf("%016x", testbit.data); + #endif + activateCpuNmiBool = false; CPU * cpu = (CPU*)malloc(sizeof(CPU)); //create new CPU diff --git a/ppu.c b/ppu.c index 95c5a6d..17adb64 100644 --- a/ppu.c +++ b/ppu.c @@ -28,12 +28,12 @@ fixed word resolveAttributeTableAddress(word regData){ locationRegister n; n.data = regData; - byte trimmedCoarseX = n.bits.coarseX >> 2; //trim the lowest 2 bits of coarse X - byte trimmedCoarseY = n.bits.coarseY >> 2; //trim the lowest 2 bits of coarse Y + byte trimmedCoarseX = n.field.coarseX >> 2; //trim the lowest 2 bits of coarse X + byte trimmedCoarseY = n.field.coarseY >> 2; //trim the lowest 2 bits of coarse Y word ret = trimmedCoarseX | (trimmedCoarseY << 3); //YYYXXX ret = 0b1111 >> 6 | ret; //1111YYYXXX - ret = n.bits.nameTableID << 10 | ret; //NN1111YYYXXX + ret = n.field.nameTableID << 10 | ret; //NN1111YYYXXX ret = 0b10 << 12 | ret; //10NN1111YYYXXX return ret; @@ -106,7 +106,7 @@ void ppuRegWrite(word address, byte data){ case 0: //ppuctrl ppu.control.full = data; - ppu.tReg.bits.nameTableID = ppu.control.nameTableID; + ppu.tReg.field.nameTableID = ppu.control.nameTableID; break; @@ -134,12 +134,12 @@ void ppuRegWrite(word address, byte data){ if(ppu.expectingLsb){ //second write - ppu.tReg.bits.fineY = data & 0b00000111; //set fineY - ppu.tReg.bits.coarseY = data >> 3; //set coarse Y + ppu.tReg.field.fineY = data & 0b00000111; //set fineY + ppu.tReg.field.coarseY = data >> 3; //set coarse Y }else{ //first write ppu.xReg = data & 0b00000111; //set fineX - ppu.tReg.bits.coarseX = data >> 3; //set coarse X + ppu.tReg.field.coarseX = data >> 3; //set coarse X } ppu.expectingLsb = !ppu.expectingLsb; @@ -162,7 +162,7 @@ void ppuRegWrite(word address, byte data){ buf = buf | (ppu.tReg.data & 0xFF); ppu.tReg.data = buf; - ppu.tReg.bits.fineY = ppu.tReg.bits.fineY & 0b011; //clear highest bit of fine Y + ppu.tReg.field.fineY = ppu.tReg.field.fineY & 0b011; //clear highest bit of fine Y } ppu.expectingLsb = !ppu.expectingLsb; diff --git a/ppu.h b/ppu.h index 3b08023..a6fae80 100644 --- a/ppu.h +++ b/ppu.h @@ -5,14 +5,20 @@ typedef struct{ union{ - struct{ - byte unused : 1; - byte fineY : 3; - byte nameTableID : 2; - byte coarseY : 5; + struct __attribute__((packed, aligned(1))){ byte coarseX : 5; + byte coarseY : 5; + byte nameTableID : 2; + byte fineY : 3; + byte unused: 1; + + }field; + + struct{ + byte lsb; + byte msb; - }bits; + }bytes; word data; };