From b664e0102f343931756504f961f89a5705d45033 Mon Sep 17 00:00:00 2001 From: Persune Date: Wed, 18 Jan 2023 16:17:24 +0800 Subject: [PATCH] Keep non-LUT generation; use #elif --- crt_core.c | 4 ++-- crt_nes.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/crt_core.c b/crt_core.c index 8295b1c..9d4b139 100644 --- a/crt_core.c +++ b/crt_core.c @@ -108,11 +108,11 @@ eqf(struct EQF *f, int s) /* index : 0 1 2 3 4 5 6 */ /* weight: 1 4 7 8 7 4 1 */ return (s + h[6] + ((h[1] + h[5]) * 4) + ((h[2] + h[4]) * 7) + (h[3] * 8)) >> 5; -#elseif USE_6_SAMPLE +#elif USE_6_SAMPLE /* index : 0 1 2 3 4 5 */ /* weight: 1 3 4 4 3 1 */ return (s + h[5] + 3 * (h[1] + h[4]) + 4 * (h[2] + h[3])) >> 4; -#elseif USE_5_SAMPLE +#elif USE_5_SAMPLE /* index : 0 1 2 3 4 */ /* weight: 1 2 2 2 1 */ return (s + h[4] + ((h[1] + h[2] + h[3]) << 1)) >> 3; diff --git a/crt_nes.c b/crt_nes.c index fd9c689..21816d7 100644 --- a/crt_nes.c +++ b/crt_nes.c @@ -17,10 +17,13 @@ #include #include +#define USE_LUT 0 + +#if USE_LUT // Precalculate the low and high signal chosen for each 64 base colors // with their respective attenuated values // https://www.nesdev.org/wiki/NTSC_video#Brightness_Levels -const int8_t IRE_levels[2][2][0x40]{ +const int IRE_levels[2][2][0x40]{ // waveform low { // normal @@ -94,6 +97,50 @@ square_sample(int p, int phase) return IRE_levels[v][e][p & 0x3F]; } +#else +/* generate the square wave for a given 9-bit pixel and phase */ +static int +square_sample(int p, int phase) +{ + static int active[6] = { + 0300, 0100, + 0500, 0400, + 0600, 0200 + }; + int bri, hue, v; + + hue = (p & 0x0f); + + /* last two columns are black */ + if (hue >= 0x0e) { + return 0; + } + + bri = ((p & 0x30) >> 4) * 300; + + switch (hue) { + case 0: + v = bri + 410; + break; + case 0x0d: + v = bri - 300; + break; + default: + v = (((hue + phase) % 12) < 6) ? (bri + 410) : (bri - 300); + break; + } + + if (v > 1024) { + v = 1024; + } + /* red 0100, green 0200, blue 0400 */ + if ((p & 0700) & active[(phase >> 1) % 6]) { + return (v >> 1) + (v >> 2); + } + + return v; +} +#endif #define NES_OPTIMIZED 0