[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
main
Joaquin 3 years ago
parent e00b5d4844
commit 3ecac1636f
Signed by: puly
GPG Key ID: 9E9299CD96C65EC6
  1. 2
      .gitignore
  2. 9
      Makefile
  3. 7
      bus.c
  4. 16
      ppu.c
  5. 18
      ppu.h

2
.gitignore vendored

@ -1,7 +1,9 @@
media media
exec exec
nesem
*log* *log*
*cache* *cache*
*.patch *.patch
*.out *.out
*dump* *dump*
*.json

@ -1,16 +1,17 @@
CC = cc CC = cc
CFLAGS = -g -DDEBUG -static CFLAGS = -static -Ofast
CDEBUGFLAGS = -g -DDEBUG -static
RM = rm -rf RM = rm -rf
OUTFILE = exec OUTFILE = nesem
default: all default: all
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: clean:
$(RM) $(OUTFILE) $(RM) $(OUTFILE)
debug: 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)

@ -112,6 +112,13 @@ void activateCpuNmi(){
int main(int argc, char * argv[]){ int main(int argc, char * argv[]){
#ifdef DEBUG
locationRegister testbit;
testbit.data = 0;
testbit.field.fineY = 0xFF;
printf("%016x", testbit.data);
#endif
activateCpuNmiBool = false; activateCpuNmiBool = false;
CPU * cpu = (CPU*)malloc(sizeof(CPU)); //create new CPU CPU * cpu = (CPU*)malloc(sizeof(CPU)); //create new CPU

16
ppu.c

@ -28,12 +28,12 @@ fixed
word resolveAttributeTableAddress(word regData){ word resolveAttributeTableAddress(word regData){
locationRegister n; locationRegister n;
n.data = regData; n.data = regData;
byte trimmedCoarseX = n.bits.coarseX >> 2; //trim the lowest 2 bits of coarse X byte trimmedCoarseX = n.field.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 trimmedCoarseY = n.field.coarseY >> 2; //trim the lowest 2 bits of coarse Y
word ret = trimmedCoarseX | (trimmedCoarseY << 3); //YYYXXX word ret = trimmedCoarseX | (trimmedCoarseY << 3); //YYYXXX
ret = 0b1111 >> 6 | ret; //1111YYYXXX ret = 0b1111 >> 6 | ret; //1111YYYXXX
ret = n.bits.nameTableID << 10 | ret; //NN1111YYYXXX ret = n.field.nameTableID << 10 | ret; //NN1111YYYXXX
ret = 0b10 << 12 | ret; //10NN1111YYYXXX ret = 0b10 << 12 | ret; //10NN1111YYYXXX
return ret; return ret;
@ -106,7 +106,7 @@ void ppuRegWrite(word address, byte data){
case 0: //ppuctrl case 0: //ppuctrl
ppu.control.full = data; ppu.control.full = data;
ppu.tReg.bits.nameTableID = ppu.control.nameTableID; ppu.tReg.field.nameTableID = ppu.control.nameTableID;
break; break;
@ -134,12 +134,12 @@ void ppuRegWrite(word address, byte data){
if(ppu.expectingLsb){ if(ppu.expectingLsb){
//second write //second write
ppu.tReg.bits.fineY = data & 0b00000111; //set fineY ppu.tReg.field.fineY = data & 0b00000111; //set fineY
ppu.tReg.bits.coarseY = data >> 3; //set coarse Y ppu.tReg.field.coarseY = data >> 3; //set coarse Y
}else{ }else{
//first write //first write
ppu.xReg = data & 0b00000111; //set fineX 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; ppu.expectingLsb = !ppu.expectingLsb;
@ -162,7 +162,7 @@ void ppuRegWrite(word address, byte data){
buf = buf | (ppu.tReg.data & 0xFF); buf = buf | (ppu.tReg.data & 0xFF);
ppu.tReg.data = buf; 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; ppu.expectingLsb = !ppu.expectingLsb;

18
ppu.h

@ -5,14 +5,20 @@
typedef struct{ typedef struct{
union{ union{
struct{ struct __attribute__((packed, aligned(1))){
byte unused : 1;
byte fineY : 3;
byte nameTableID : 2;
byte coarseY : 5;
byte coarseX : 5; 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; word data;
}; };

Loading…
Cancel
Save