Skip to content

Commit

Permalink
now supporting color palette changes through register $2001 used by s…
Browse files Browse the repository at this point in the history
…everal games
  • Loading branch information
FIX94 committed Jun 3, 2017
1 parent 069639a commit dfaace8
Show file tree
Hide file tree
Showing 31 changed files with 150 additions and 100 deletions.
5 changes: 4 additions & 1 deletion apu.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <malloc.h>
#include "apu.h"
#include "audio_fds.h"
Expand Down Expand Up @@ -152,7 +153,9 @@ void apuInitBufs()
mode4Ctr = nesPAL ? mode4CtrPal : mode4CtrNtsc;
mode5Ctr = nesPAL ? mode5CtrPal : mode5CtrNtsc;
//effective frequencies for 50.000Hz and 60.000Hz Video out
apuFrequency = nesPAL ? 831187 : 893415;
//apuFrequency = nesPAL ? 831187 : 893415;
//effective frequencies for Original PPU Video out
apuFrequency = nesPAL ? 831303 : 894886;
double dt = 1.0/((double)apuFrequency);
//LP at 22kHz
double rc = 1.0/(M_2_PI * 22000.0);
Expand Down
1 change: 1 addition & 0 deletions audio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "AL/al.h"
Expand Down
1 change: 1 addition & 0 deletions audio_fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <malloc.h>
#include "apu.h"
#include "audio_fds.h"
Expand Down
1 change: 1 addition & 0 deletions audio_mmc5.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <malloc.h>
#include "apu.h"
#include "audio_mmc5.h"
Expand Down
2 changes: 1 addition & 1 deletion audio_vrc6.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void vrc6AudioInit()
vrc6_p1Duty = 0, vrc6_p2Duty = 0, vrc6_sawAdd = 0;
vrc6_speed = 0;
vrc6_p1const = false, vrc6_p2const = false;
vrc6_p1enable = false, vrc6_p2enable = false, vrc6_sawenable;
vrc6_p1enable = false, vrc6_p2enable = false, vrc6_sawenable = false;
vrc6_halt = false;
//printf("VRC6 Audio Inited!\n");
}
Expand Down
3 changes: 2 additions & 1 deletion audio_vrc7.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "audio_vrc7.h"
Expand Down Expand Up @@ -85,7 +86,7 @@ enum
static const uint32_t vrc7MaxAtten = (1<<23);
static const double attenDb = (1<<23) / 48.0;

inline int32_t vrc7FromDb(double in)
static inline int32_t vrc7FromDb(double in)
{
return (int32_t)(in * attenDb);
}
Expand Down
2 changes: 1 addition & 1 deletion build_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

#Need to replace this with a makefile

gcc main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lglut -lopenal -lGL -lGLU -lm -Wall -Wextra -O3 -msse -mfpmath=sse -ffast-math -s -o fixNES
gcc main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lglut -lopenal -lGL -lGLU -lm -Wall -Wextra -O3 -flto -msse -mfpmath=sse -ffast-math -s -o fixNES
echo "Succesfully built fixNES"

2 changes: 2 additions & 0 deletions build_msys32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
gcc -DWINDOWS_BUILD main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lfreeglut_static -lopenal32 -lopengl32 -lglu32 -lgdi32 -lwinmm -Wall -Wextra -O3 -flto -msse -mfpmath=sse -ffast-math -s -o fixNES
2 changes: 1 addition & 1 deletion build_windows_console.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
gcc -DWINDOWS_BUILD main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lfreeglut_static -lopenal32 -lopengl32 -lglu32 -lgdi32 -lwinmm -Wall -Wextra -O3 -msse -mfpmath=sse -ffast-math -s -o fixNES
gcc -DWINDOWS_BUILD main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lfreeglut_static -lopenal32 -lopengl32 -lglu32 -lgdi32 -lwinmm -Wall -Wextra -O3 -flto -msse -mfpmath=sse -ffast-math -s -o fixNES
pause
2 changes: 1 addition & 1 deletion build_windows_noconsole.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
gcc -DWINDOWS_BUILD main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lfreeglut_static -lopenal32 -lopengl32 -lglu32 -lgdi32 -lwinmm -Wall -Wextra -O3 -msse -mfpmath=sse -ffast-math -s -o fixNES -Wl,--subsystem,windows
gcc -DWINDOWS_BUILD main.c apu.c audio.c audio_fds.c audio_mmc5.c audio_vrc6.c audio_vrc7.c alhelpers.c cpu.c ppu.c mem.c input.c mapper.c mapperList.c fm2play.c vrc_irq.c mapper/*.c -DFREEGLUT_STATIC -lfreeglut_static -lopenal32 -lopengl32 -lglu32 -lgdi32 -lwinmm -Wall -Wextra -O3 -flto -msse -mfpmath=sse -ffast-math -s -o fixNES -Wl,--subsystem,windows
pause
141 changes: 70 additions & 71 deletions cpu.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions fm2play.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include "fm2play.h"
Expand Down
1 change: 1 addition & 0 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "input.h"

//used externally by main.c
Expand Down
16 changes: 4 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <inttypes.h>
#include <GL/glut.h>
Expand All @@ -30,7 +31,7 @@
#define DEBUG_KEY 0
#define DEBUG_LOAD_INFO 1

static const char *VERSION_STRING = "fixNES Alpha v0.8.2";
static const char *VERSION_STRING = "fixNES Alpha v0.8.3";

static void nesEmuDisplayFrame(void);
static void nesEmuMainLoop(void);
Expand All @@ -47,7 +48,7 @@ static char *emuSaveName = NULL;
static uint8_t *emuPrgRAM = NULL;
static uint32_t emuPrgRAMsize = 0;
//used externally
uint8_t *textureImage = NULL;
uint32_t textureImage[0xF000];
bool nesPause = false;
bool ppuDebugPauseFrame = false;
bool doOverscan = true;
Expand Down Expand Up @@ -295,12 +296,7 @@ int main(int argc, char** argv)
emuMainFrameStart = GetTickCount();
#endif
#endif
textureImage = malloc(visibleImg);
memset(textureImage,0,visibleImg);
//make sure image is visible
uint32_t i;
for(i = 0; i < visibleImg; i+=4)
textureImage[i+3] = 0xFF;
cpuCycleTimer = nesPAL ? 16 : 12;
//do one scanline per idle loop
ppuCycleTimer = nesPAL ? 5 : 4;
Expand Down Expand Up @@ -376,9 +372,6 @@ static void nesEmuDeinit(void)
free(emuPrgRAM);
}
emuPrgRAM = NULL;
if(textureImage != NULL)
free(textureImage);
textureImage = NULL;
//printf("Bye!\n");
}

Expand Down Expand Up @@ -809,8 +802,7 @@ static void nesEmuDisplayFrame()
emuRenderFrame = false;
return;
}
if(textureImage != NULL)
glTexImage2D(GL_TEXTURE_2D, 0, 4, VISIBLE_DOTS, VISIBLE_LINES, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, textureImage);
glTexImage2D(GL_TEXTURE_2D, 0, 4, VISIBLE_DOTS, VISIBLE_LINES, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, textureImage);
emuRenderFrame = false;
}

Expand Down
1 change: 1 addition & 0 deletions mapper/fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"
#include "../cpu.h"
#include "../input.h"
Expand Down
1 change: 1 addition & 0 deletions mapper/m1.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdbool.h>
#include "../ppu.h"

Expand Down
1 change: 1 addition & 0 deletions mapper/m13.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdio.h>
#include <inttypes.h>
#include <string.h>

static uint8_t *m13_prgROM;
static uint32_t m13_prgROMsize;
Expand Down
1 change: 1 addition & 0 deletions mapper/m15.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *m15_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/m4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *m4_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/m48.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *m48_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/m7.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *m7_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/nsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"
#include "../cpu.h"
#include "../input.h"
Expand Down
1 change: 1 addition & 0 deletions mapper/p16c4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *p16c4_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/p16c8.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *p16c8_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/p32c4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *p32c4_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/p32c8.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"

static uint8_t *p32c8_prgROM;
Expand Down
1 change: 1 addition & 0 deletions mapper/vrc2_4.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"
#include "../vrc_irq.h"

Expand Down
1 change: 1 addition & 0 deletions mapper/vrc6.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"
#include "../audio_vrc6.h"
#include "../vrc_irq.h"
Expand Down
1 change: 1 addition & 0 deletions mapper/vrc7.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "../ppu.h"
#include "../audio_vrc7.h"
#include "../vrc_irq.h"
Expand Down
1 change: 1 addition & 0 deletions mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "mapper.h"
#include "ppu.h"
#include "cpu.h"
Expand Down
54 changes: 43 additions & 11 deletions ppu.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "mapper.h"
#include "ppu.h"

Expand Down Expand Up @@ -51,7 +52,7 @@
bool ppu4Screen = false;

//from main.c
extern uint8_t *textureImage;
extern uint32_t textureImage[0xF000];
extern bool nesPause;
extern bool ppuDebugPauseFrame;
extern bool doOverscan;
Expand Down Expand Up @@ -79,6 +80,7 @@ static uint8_t PPU_OAM[0x100];
static uint8_t PPU_OAM2[0x20];

static uint8_t PPU_PALRAM[0x20];
static uint32_t PPU_BGRLUT[0x200];
//internally processed, ready to draw
static uint8_t PPU_Sprites[0x20];

Expand Down Expand Up @@ -158,6 +160,36 @@ void ppuInit()
ppuCurNMIStat = false;
ppuOddFrame = false;
ppuReadReg2 = false;
//generate full BGR LUT
uint8_t rtint = nesPAL ? (1<<6) : (1<<5);
uint8_t gtint = nesPAL ? (1<<5) : (1<<6);
uint8_t btint = (1<<7);
int32_t i;
for(i = 0; i < 0x200; i++)
{
uint8_t palpos = (i&0x3F)*3;
uint8_t r = PPU_Pal[palpos];
uint8_t g = PPU_Pal[palpos+1];
uint8_t b = PPU_Pal[palpos+2];
if((i & 0xF) <= 0xD)
{
//reduce red
if((i>>1) & btint) r = (uint8_t)(((float)r)*0.75f);
if((i>>1) & gtint) r = (uint8_t)(((float)r)*0.75f);
//reduce green
if((i>>1) & rtint) g = (uint8_t)(((float)g)*0.75f);
if((i>>1) & btint) g = (uint8_t)(((float)g)*0.75f);
//reduce blue
if((i>>1) & rtint) b = (uint8_t)(((float)b)*0.75f);
if((i>>1) & gtint) b = (uint8_t)(((float)b)*0.75f);
}
//save new color into LUT
PPU_BGRLUT[i] =
(b) //Blue
| (g<<8) //Green
| (r<<16) //Red
| (0xFF<<24); //Alpha
}
}

static inline uint16_t ppuGetVramTbl(uint16_t tblStart)
Expand Down Expand Up @@ -318,20 +350,20 @@ bool ppuCycle()
if((!(PPU_Reg[1] & PPU_BG_8PX) || !(PPU_Reg[1] & PPU_SPRITE_8PX)) && (curDot < 8 || curDot > 248))
curCol = 0xFF;
/* Draw current dot on screen */
size_t drawPos = (curDot<<2)+(curLine<<10);
size_t drawPos = (curDot)+(curLine<<8);
if(curCol != 0xFF)
{
uint8_t cPalIdx = (PPU_PALRAM[curCol]&((PPU_Reg[1]&PPU_GRAY)?0x30:0x3F))*3;
textureImage[drawPos] = PPU_Pal[cPalIdx+2];
textureImage[drawPos+1] = PPU_Pal[cPalIdx+1];
textureImage[drawPos+2] = PPU_Pal[cPalIdx];
uint16_t cPalIdx;
if(pictureOutput) //use color from bg or sprite input
cPalIdx = (PPU_PALRAM[curCol&0x1F]&((PPU_Reg[1]&PPU_GRAY)?0x30:0x3F))|((PPU_Reg[1]&0xE0)<<1);
else if(ppuVramAddr >= 0x3F00 && ppuVramAddr < 0x4000) //bg and sprite disabled but address within PALRAM
cPalIdx = (PPU_PALRAM[ppuVramAddr&0x1F]&((PPU_Reg[1]&PPU_GRAY)?0x30:0x3F))|((PPU_Reg[1]&0xE0)<<1);
else //bg and sprite disabled and address not within PALRAM
cPalIdx = PPU_PALRAM[0];
textureImage[drawPos] = PPU_BGRLUT[cPalIdx];
}
else /* Draw clipped area as black */
{
textureImage[drawPos] = 0;
textureImage[drawPos+1] = 0;
textureImage[drawPos+2] = 0;
}
textureImage[drawPos] = 0xFF000000;
}

/* set to 0 during not visible dots up to post-render line */
Expand Down

0 comments on commit dfaace8

Please sign in to comment.