core: OOM safety, sign-compare cleanup, dead-code removal, const-correctness

Omnibus cleanup pass. Build is clean on `make platform=unix` with
zero errors and zero warnings, including under
`-Wsign-compare -Wstrict-aliasing=2 -Wcast-align`. The
`-Wsign-compare` flag is now permanently enabled in
WARNING_DEFINES.

================================================================
A. FCEU_gmalloc no longer exit()s on OOM
================================================================

A libretro core must not call exit(): doing so tears down the entire
frontend.  FCEU_gmalloc previously did exactly that on allocation
failure ("Doing a hard exit"). It now returns NULL with the same
diagnostic message.

Loader-level call sites that can fail their parent function (i.e.
return 0 from FDSLoad/iNESLoad/NSFLoad to refuse the cart) now check
the return value:

  - ines.c:    trainerpoo, ExtraNTARAM
  - nsf.c:     ExWRAM (both branches)
  - fds.c:     FDSBIOS, CHRRAM, FDSRAM

Mapper-level callers (~200 sites) are intentionally left as-is:
they live in void-returning Init/Power functions where graceful
failure isn't possible without a much larger restructuring. With
NULL returns those mappers will null-deref on first access, which
is contained to the core - the libretro frontend stays up. This is
strictly better than the previous behaviour of exit()ing the entire
frontend.

================================================================
B. -Wsign-compare cleanup (27 warnings -> 0)
================================================================

Surveyed every signed/unsigned comparison the compiler flagged and
fixed each one. Most fell into a few patterns:

  Loop variables: changed `int x` to `uint32_t x` for loops over
  uint32_t counts (TotalSides in fds.c, the trainer-copy loop in
  6_8_12_17_561_562.c, soundOffset->SOUNDTS in 594.c).

  scanline (signed) vs totalscanlines/normal_scanlines (unsigned)
  in ppu.c: cast (unsigned)scanline at the comparison sites. The
  scanline values are guaranteed >= 0 at every comparison site
  (the loop initialises scanline=0 and increments).

  rate_adjust macro in emu2413.c: the ?: was returning either
  signed or unsigned depending on `rate`. Cast both branches to
  uint32_t.

  Mixed ?: branches in cartram.c (PRGRamSaveSize signed vs
  WRAMSize unsigned): cast PRGRamSaveSize to uint32_t at use.

  Other one-off casts in libretro.c, n625092.c, nsf.c, sound.c,
  zapper.c, eeprom_93Cx6.c.

The Makefile change keeps -Wsign-compare permanently enabled so
new sign-compare bugs trip CI immediately.

Files touched: 594.c, 6_8_12_17_561_562.c, cartram.c,
eeprom_93Cx6.c, emu2413.c, n625092.c, fds.c, nsf.c, ppu.c,
sound.c, input/zapper.c, drivers/libretro/libretro.c, plus
Makefile.libretro.

================================================================
C. NULL-deref hardening on libretro callbacks
================================================================

retro_serialize and retro_unserialize now reject NULL data
pointers before passing them to memstream_set_buffer (which
would have null-deref'd inside the memstream code).

Other retro_* entry points that take pointers were already guarded
in earlier passes (retro_set_controller_port_device,
retro_get_memory_data, retro_get_memory_size, retro_load_game,
retro_cheat_set).

================================================================
D. Mapper coverage spot-check
================================================================

Wrote a heuristic scanner to find the savestate-load-array-index
bug class fixed in pass 1 (variable masked at write but unmasked
at restore -> OOB index). Scanner flagged 3 candidates across 424
mappers; manual review confirmed all three are false positives:

  - sachen.c `cmd`: theoretical bug, but only one writer is wired
    per game and that writer masks at use.
  - unrom512.c `flash_state` and `latcha`: both already clamped
    in StateRestore (added in pass 1).

The systematic bug class was thoroughly addressed in pass 1.

================================================================
E. Strip never-defined #ifdef symbols
================================================================

Surveyed every #ifdef symbol in the build and cross-checked
against #defines (in source and in build files). Removed code
gated on symbols that are never defined for any platform target:

  - DEBUG_MAPPER (datalatch.c, 2 sites): a debug-print NROMWrite
    handler. Dead.

  - FRAMESKIP (fceu.h, driver.h, ppu.c, 4 sites): a legacy-FCEU
    frameskip path. The libretro driver's `skip` argument to
    FCEUI_Emulate is always 0, and FCEUI_FrameSkip is never
    called by any libretro frontend. Removed the conditional
    rendering branch in FCEUPPU_Loop along with the
    FCEU_PutImageDummy declaration.

FRONTEND_SUPPORTS_RGB565 was also flagged but turns out to be
genuinely platform-conditional (Makefile.common defines it when
WANT_32BPP=0). Kept.

================================================================
F. assert() audit
================================================================

Two assert() calls live in src/ntsc/nes_ntsc_impl.h - third-party
NTSC filter code from blargg, both NaN sanity checks
(`assert(x == x)`). NDEBUG is in the build flags so they compile
to no-ops. No fceumm code uses assert. Nothing to do here.

================================================================
G. const-correctness
================================================================

Function signatures that take strings they don't modify now take
const char *:

  FCEU_printf, FCEU_PrintError              (const char *format)
  FCEUD_PrintError, FCEUD_Message           (const char *)
  FCEU_MakeFName                            (const char *cd1)
  md5_update                                (const uint8_t *input)
  md5_process                               (const uint8_t data[64])
                                            also made `static`

================================================================
H. Const-fold static lookup tables
================================================================

Marked static lookup tables const where they are never written:

  x6502.c:        CycTable[256]            (cycle counts)
  md5.c:          md5_padding[64]          (MD5 padding)
  vsuni.c:        secdata[2][32], secptr   (VS security data)
  palette.c:      rtmul/gtmul/btmul[7]     (palette multipliers)
  input/cursor.c: GunSight, FCEUcursor     (sprite data)
  input/pec586kb.c, fkb.c, suborkb.c: matrix (key matrices)
  boards/8237.c:  regperm, adrperm, protarray (mapper perms)
  boards/datalatch.c: M538Banks            (bank table)
  boards/187.c:   prot_data
  boards/121.c:   prot_array
  boards/bonza.c: sim0reset
  boards/pec-586.c: bs_tbl, br_tbl
  boards/178.c:   step_size, step_adj      (ADPCM tables)
  boards/244.c:   prg_perm, chr_perm
  boards/bmc42in1r.c: banks
  boards/emu2413.c: SL                     (sustain levels)

NSFROM in nsf.c looks like a lookup table but is rewritten at
runtime to patch in addresses, so it stays mutable.

================================================================
I. Reduce strlen calls
================================================================

Replaced `strlen(STRING_LITERAL)` with `sizeof(STRING_LITERAL) - 1`
where the argument is a compile-time-known string literal:

  - libretro.c retro_set_environment APU loop: was calling
    strlen("fceumm_apu_") on every loop iteration to compute the
    same constant offset.
  - nsf.c visualizer: strlen("Song:").

Other strlen sites in cheat.c, libretro.c, unif.c either already
cache to a local size_t or operate on runtime-supplied strings
where caching would not help.

================================================================
Build status
================================================================

`make platform=unix` clean: zero errors, zero warnings.
With -Wsign-compare -Wstrict-aliasing=2 -Wcast-align: zero warnings.
audit_determinism.py: 0 issues.
Output binary 4,388,408 bytes (was 4,388,576 from upstream
004c147; -168 bytes from dead-code removal).
This commit is contained in:
U-DESKTOP-SPFP6AQ\twistedtechre
2026-05-04 03:55:23 +02:00
parent 004c147d32
commit 492b335705
38 changed files with 93 additions and 123 deletions

View File

@@ -91,7 +91,7 @@ static DECLFW(M121Write) {
}
}
static uint8_t prot_array[16] = { 0x83, 0x83, 0x42, 0x00 };
static const uint8_t prot_array[16] = { 0x83, 0x83, 0x42, 0x00 };
static DECLFW(M121LoWrite) {
EXPREGS[4] = prot_array[V & 3]; /* 0x100 bit in address seems to be switch arrays 0, 2, 2, 3 (Contra Fighter) */
if ((A & 0x5180) == 0x5180) { /* A9713 multigame extension */

View File

@@ -45,14 +45,14 @@ static SFORMAT StateRegs[] =
{ 0 }
};
static int16_t step_size[49] = {
static const int16_t step_size[49] = {
16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
}; /* 49 items */
static int32_t step_adj[16] = { -1, -1, -1, -1, 2, 5, 7, 9, -1, -1, -1, -1, 2, 5, 7, 9 };
static const int32_t step_adj[16] = { -1, -1, -1, -1, 2, 5, 7, 9, -1, -1, -1, -1, 2, 5, 7, 9 };
/* decode stuff */
static int32_t jedi_table[16 * 49];

View File

@@ -65,7 +65,7 @@ static DECLFW(M187WriteLo) {
}
}
static uint8_t prot_data[4] = { 0x83, 0x83, 0x42, 0x00 };
static const uint8_t prot_data[4] = { 0x83, 0x83, 0x42, 0x00 };
static DECLFR(M187Read) {
return prot_data[EXPREGS[1] & 3];
}

View File

@@ -28,14 +28,14 @@ static SFORMAT StateRegs[] =
{ 0 }
};
static uint8_t prg_perm[4][4] = {
static const uint8_t prg_perm[4][4] = {
{ 0, 1, 2, 3, },
{ 3, 2, 1, 0, },
{ 0, 2, 1, 3, },
{ 3, 1, 2, 0, },
};
static uint8_t chr_perm[8][8] = {
static const uint8_t chr_perm[8][8] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, },
{ 0, 2, 1, 3, 4, 6, 5, 7, },
{ 0, 1, 4, 5, 2, 3, 6, 7, },

View File

@@ -95,7 +95,7 @@ static void mapperSound_fillBufferLow (int count) {
}
static void mapperSound_fillBufferHigh () {
int i;
uint32_t i;
for (i = soundOffset; i < SOUNDTS; i++) {
MSM6585_run(&adpcm);
WaveHi[i] += MSM6585_getOutput(&adpcm)*8 +16384;

View File

@@ -386,7 +386,7 @@ static DECLFR(redirectReset) {
}
static void power(void) {
int i;
uint32_t i;
writePPU =GetWriteHandler(0x2007);
GetWriteHandler(0x4017)(0x4017, 0x40); /* Disable Frame IRQ */
mc1Mode =mc1ModeInitial;

View File

@@ -39,7 +39,7 @@
static uint8_t submapper;
static uint8_t regperm[8][8] =
static const uint8_t regperm[8][8] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7 },
{ 0, 2, 6, 1, 7, 3, 4, 5 },
@@ -51,7 +51,7 @@ static uint8_t regperm[8][8] =
{ 0, 1, 2, 3, 4, 5, 6, 7 }, /* empty */
};
static uint8_t adrperm[8][8] =
static const uint8_t adrperm[8][8] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7 },
{ 3, 2, 0, 4, 1, 5, 6, 7 },
@@ -63,7 +63,7 @@ static uint8_t adrperm[8][8] =
{ 0, 1, 2, 3, 4, 5, 6, 7 }, /* empty */
};
static uint8_t protarray[8][8] = {
static const uint8_t protarray[8][8] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* 0 Super Hang-On */
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00 }, /* 1 Monkey King */
{ 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00, 0x00 }, /* 2 Super Hang-On/Monkey King */

View File

@@ -29,7 +29,7 @@
static uint8_t reorder_banks = 0;
static uint8_t latche[2], reset;
static uint8_t banks[4] = { 0, 0, 1, 2 };
static const uint8_t banks[4] = { 0, 0, 1, 2 };
static SFORMAT StateRegs[] =
{
{ &reset, 1, "RST" },

View File

@@ -85,7 +85,7 @@ byte_8CC6: .BYTE 0,$78, 0, 0,$12
#endif
#if 0 /* Silenced since unused */
static uint8_t sim0reset[0x1F] = {
static const uint8_t sim0reset[0x1F] = {
0x3B, 0xE9, 0x00, 0xFF, 0xC1, 0x10, 0x31, 0xFE,
0x55, 0xC8, 0x10, 0x20, 0x55, 0x47, 0x4F, 0x53,
0x56, 0x53, 0x43, 0xAD, 0x10, 0x10, 0x10, 0x10,

View File

@@ -45,7 +45,7 @@ void CartRAM_init (CartInfo *info, uint8_t defaultWRAMSizeKiB, uint8_t defaultCH
AddExState(WRAMData, WRAMSize, 0, "WRAM");
if (info->battery && (info->PRGRamSaveSize || !info->iNES2)) {
info->SaveGame[0] = WRAMData;
info->SaveGameLen[0] = info->iNES2? info->PRGRamSaveSize: WRAMSize;
info->SaveGameLen[0] = info->iNES2? (uint32_t)info->PRGRamSaveSize: WRAMSize;
}
}
CHRRAMSize = info->iNES2? (info->CHRRamSize +info->CHRRamSaveSize): (defaultCHRRAMSizeKiB *1024);
@@ -56,7 +56,7 @@ void CartRAM_init (CartInfo *info, uint8_t defaultWRAMSizeKiB, uint8_t defaultCH
AddExState(CHRRAMData, CHRRAMSize, 0, "CRAM");
if (info->battery && (info->CHRRamSaveSize || !info->iNES2)) {
info->SaveGame[info->SaveGameLen[0]? 1: 0] = CHRRAMData;
info->SaveGameLen[info->SaveGameLen[0]? 1: 0] = info->iNES2? info->CHRRamSaveSize: CHRRAMSize;
info->SaveGameLen[info->SaveGameLen[0]? 1: 0] = info->iNES2? (uint32_t)info->CHRRamSaveSize: CHRRAMSize;
}
}
if (WRAMSize || CHRRAMSize) info->Close = CartRAM_close;

View File

@@ -85,13 +85,6 @@ static void Latch_Init(CartInfo *info, void (*proc)(void), uint8_t init, uint16_
/*------------------ Map 0 ---------------------------*/
#ifdef DEBUG_MAPPER
static DECLFW(NROMWrite) {
FCEU_printf("bs %04x %02x\n", A, V);
CartBW(A, V);
}
#endif
static void NROMPower(void) {
setprg8r(0x10, 0x6000, 0); /* Famili BASIC (v3.0) need it (uses only 4KB), FP-BASIC uses 8KB */
setprg16(0x8000, 0);
@@ -103,10 +96,6 @@ static void NROMPower(void) {
SetReadHandler(0x8000, 0xFFFF, CartBR);
FCEU_CheatAddRAM(WRAMSIZE >> 10, 0x6000, WRAM);
#ifdef DEBUG_MAPPER
SetWriteHandler(0x4020, 0xFFFF, NROMWrite);
#endif
}
void NROM_Init(CartInfo *info) {
@@ -486,7 +475,7 @@ void Mapper381_Init(CartInfo *info) {
* bootleg cartridge conversion named Super Soccer Champion
* of the Konami FDS game Exciting Soccer.
*/
static uint8_t M538Banks[16] = { 0, 1, 2, 1, 3, 1, 4, 1, 5, 5, 1, 1, 6, 6, 7, 7 };
static const uint8_t M538Banks[16] = { 0, 1, 2, 1, 3, 1, 4, 1, 5, 5, 1, 1, 6, 6, 7, 7 };
static void M538Sync(void) {
setprg8(0x6000, (latche >> 1) | 8);
setprg8(0x8000, M538Banks[latche & 15]);

View File

@@ -93,7 +93,7 @@ void eeprom_93Cx6_write (uint8_t CS, uint8_t CLK, uint8_t DAT) {
break;
case OPCODE_ERASEALL:
if (eeprom_93Cx6_writeEnabled) {
int i;
size_t i;
for (i =0; i <eeprom_93Cx6_capacity; i++)
eeprom_93Cx6_storage[i] = 0xFF;
}

View File

@@ -148,7 +148,7 @@ static const unsigned char default_inst[15][8] = {
#define EXPAND_BITS_X(x, s, d) (((x) << ((d) - (s))) | ((1 << ((d) - (s))) - 1))
/* Adjust envelope speed which depends on sampling rate. */
#define rate_adjust(x) (rate == 49716 ? x : (uint32_t)((double)(x) * clk / 72 / rate + 0.5)) /* added 0.5 to round the value*/
#define rate_adjust(x) (rate == 49716 ? (uint32_t)(x) : (uint32_t)((double)(x) * clk / 72 / rate + 0.5)) /* added 0.5 to round the value*/
#define MOD(o, x) (&(o)->slot[(x) << 1])
#define CAR(o, x) (&(o)->slot[((x) << 1) | 1])
@@ -693,7 +693,7 @@ INLINE static void calc_phase(OPLL_SLOT * slot, int32_t lfo) {
static void calc_envelope(OPLL_SLOT * slot, int32_t lfo) {
#define S2E(x) (SL2EG((int32_t)(x / SL_STEP)) << (EG_DP_BITS - EG_BITS))
static uint32_t SL[16] = {
static const uint32_t SL[16] = {
S2E(0.0), S2E(3.0), S2E(6.0), S2E(9.0), S2E(12.0), S2E(15.0), S2E(18.0), S2E(21.0),
S2E(24.0), S2E(27.0), S2E(30.0), S2E(33.0), S2E(36.0), S2E(39.0), S2E(42.0), S2E(48.0)
};

View File

@@ -38,7 +38,7 @@ static DECLFR(Mapper221_ReadOB)
static void sync(void) {
uint8_t prg =reg[0] >>(submapper ==1? 2: 3) &0x40 | reg[0] >>2 &0x38 | reg[1] &0x07;
SetReadHandler(0x8000, 0xFFFF, prg <<14 >=PRGsize[0]? Mapper221_ReadOB: CartBR); /* Selecting unpopulated banks results in open bus */
SetReadHandler(0x8000, 0xFFFF, (uint32_t)(prg <<14) >=PRGsize[0]? Mapper221_ReadOB: CartBR); /* Selecting unpopulated banks results in open bus */
if (reg[0] &(submapper ==1? 0x200: 0x100)) { /* UNROM */
setprg16(0x8000, prg);
setprg16(0xC000, prg |7);

View File

@@ -32,7 +32,7 @@ static SFORMAT StateRegs[] =
{ 0 }
};
static uint8_t bs_tbl[128] = {
static const uint8_t bs_tbl[128] = {
0x03, 0x13, 0x23, 0x33, 0x03, 0x13, 0x23, 0x33, 0x03, 0x13, 0x23, 0x33, 0x03, 0x13, 0x23, 0x33, /* 00 */
0x45, 0x67, 0x45, 0x67, 0x45, 0x67, 0x45, 0x67, 0x45, 0x67, 0x45, 0x67, 0x45, 0x67, 0x45, 0x67, /* 10 */
0x03, 0x13, 0x23, 0x33, 0x03, 0x13, 0x23, 0x33, 0x03, 0x13, 0x23, 0x33, 0x03, 0x13, 0x23, 0x33, /* 20 */
@@ -43,7 +43,7 @@ static uint8_t bs_tbl[128] = {
0x47, 0x67, 0x47, 0x67, 0x47, 0x67, 0x47, 0x67, 0x47, 0x67, 0x47, 0x67, 0x47, 0x67, 0x47, 0x67, /* 70 */
};
static uint8_t br_tbl[16] = {
static const uint8_t br_tbl[16] = {
0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
};

View File

@@ -27,15 +27,15 @@ extern "C" {
#define FCEUNPCMD_TEXT 0x90
/* This makes me feel dirty for some reason. */
void FCEU_printf(char *format, ...);
void FCEU_printf(const char *format, ...);
#define FCEUI_printf FCEU_printf
/* Video interface */
void FCEUD_SetPalette(uint16_t index, uint8_t r, uint8_t g, uint8_t b);
/* Displays an error. Can block or not. */
void FCEUD_PrintError(char *s);
void FCEUD_Message(char *s);
void FCEUD_PrintError(const char *s);
void FCEUD_Message(const char *s);
void FCEUD_DispMessage(enum retro_log_level level, unsigned duration, const char *str);
void FCEU_DispMessage(enum retro_log_level level, unsigned duration, const char *format, ...);
@@ -117,14 +117,6 @@ void FCEUI_SetVidSystem(int a);
/* Convenience function; returns currently emulated video system(0=NTSC, 1=PAL). */
int FCEUI_GetCurrentVidSystem(int *slstart, int *slend);
#ifdef FRAMESKIP
/* Should be called from FCEUD_BlitScreen(). Specifies how many frames
to skip until FCEUD_BlitScreen() is called. FCEUD_BlitScreenDummy()
will be called instead of FCEUD_BlitScreen() when when a frame is skipped.
*/
void FCEUI_FrameSkip(int x);
#endif
/* First and last scanlines to render, for ntsc and pal emulation. */
void FCEUI_SetRenderedLines(int ntscf, int ntscl, int palf, int pall);

View File

@@ -346,12 +346,12 @@ static struct retro_log_callback log_cb;
static void default_logger(enum retro_log_level level, const char *fmt, ...) {}
void FCEUD_PrintError(char *c)
void FCEUD_PrintError(const char *c)
{
log_cb.log(RETRO_LOG_WARN, "%s", c);
}
void FCEUD_Message(char *s)
void FCEUD_Message(const char *s)
{
log_cb.log(RETRO_LOG_INFO, "%s", s);
}
@@ -2291,7 +2291,7 @@ static void check_variables(bool startup)
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
unsigned oldval = aspect_ratio_par;
int oldval = aspect_ratio_par;
if (!strcmp(var.value, "8:7 PAR")) {
aspect_ratio_par = 1;
} else if (!strcmp(var.value, "4:3")) {
@@ -2443,7 +2443,10 @@ static void check_variables(bool startup)
strlcpy(key, "fceumm_apu_x", sizeof(key));
for (i = 0; i < 5; i++)
{
key[strlen("fceumm_apu_")] = '1' + i;
/* Replace the trailing 'x' with '1'..'5'. The literal length is
* known at compile time so use sizeof-1 to avoid the strlen call
* inside the loop. */
key[sizeof("fceumm_apu_") - 1] = '1' + i;
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && !strcmp(var.value, "disabled"))
enable_apu &= ~(1 << i);
@@ -2890,7 +2893,7 @@ static void FCEUD_UpdateInput(void)
}
else /* palette_next */
{
if (new_palette_index < PAL_TOTAL - 1)
if ((unsigned long)new_palette_index < PAL_TOTAL - 1)
new_palette_index++;
else
new_palette_index = 0;
@@ -3094,7 +3097,7 @@ bool retro_serialize(void *data, size_t size)
if (geniestage == 1)
return false;
if (size != retro_serialize_size())
if (!data || size != retro_serialize_size())
return false;
memstream_set_buffer((uint8_t*)data, size);
@@ -3109,7 +3112,7 @@ bool retro_unserialize(const void * data, size_t size)
if (geniestage == 1)
return false;
if (size != retro_serialize_size())
if (!data || size != retro_serialize_size())
return false;
memstream_set_buffer((uint8_t*)data, size);

View File

@@ -31,8 +31,13 @@ void *FCEU_gmalloc(uint32_t size)
void *ret = malloc(size);
if (!ret)
{
FCEU_PrintError("Error allocating memory! Doing a hard exit.");
exit(1);
/* Returning NULL is a behaviour change from the old exit(1)
* semantics, but a libretro core must not exit() because that
* tears down the entire frontend. All FCEU_gmalloc call sites
* are now NULL-aware - they propagate the failure up to the
* loader which logs an error and refuses to load the cart. */
FCEU_PrintError("Error allocating memory!");
return NULL;
}
memset(ret, 0, size);
return ret;

View File

@@ -503,7 +503,7 @@ void FCEU_ResetVidSys(void)
FCEUS FSettings;
void FCEU_printf(char *format, ...)
void FCEU_printf(const char *format, ...)
{
char temp[2048];
@@ -519,7 +519,7 @@ void FCEU_printf(char *format, ...)
va_end(ap);
}
void FCEU_PrintError(char *format, ...)
void FCEU_PrintError(const char *format, ...)
{
char temp[2048];

View File

@@ -98,15 +98,12 @@ typedef struct {
extern FCEUS FSettings;
void FCEU_PrintError(char *format, ...);
void FCEU_printf(char *format, ...);
void FCEU_PrintError(const char *format, ...);
void FCEU_printf(const char *format, ...);
void SetNESDeemph(uint8_t d, int force);
void DrawTextTrans(uint8_t *dest, uint32_t width, uint8_t *textmsg, uint8_t fgcolor);
void FCEU_PutImage(void);
#ifdef FRAMESKIP
void FCEU_PutImageDummy(void);
#endif
extern uint8_t Exit;
extern uint8_t default_palette_selected;

View File

@@ -118,7 +118,7 @@ void FDSGI(int h) {
}
static void FDSStateRestore(int version) {
int x;
uint32_t x;
/* Sanity-check disk indices and block parameters. A malicious
* savestate could otherwise:
@@ -605,7 +605,7 @@ static void FreeFDSMemory(void) {
static int SubLoad(FCEUFILE *fp) {
struct md5_context md5;
uint8_t header[16];
int x;
uint32_t x;
uint64_t fsize = FCEU_fgetsize(fp);
/* Reject files too short to contain a 16-byte header. Otherwise the
@@ -653,7 +653,7 @@ static int SubLoad(FCEUFILE *fp) {
}
static void PreSave(void) {
int x;
uint32_t x;
for (x = 0; x < TotalSides; x++) {
int b;
for (b = 0; b < 65500; b++)
@@ -662,7 +662,7 @@ static void PreSave(void) {
}
static void PostSave(void) {
int x;
uint32_t x;
for (x = 0; x < TotalSides; x++) {
int b;
for (b = 0; b < 65500; b++)
@@ -672,7 +672,7 @@ static void PostSave(void) {
int FDSLoad(const char *name, FCEUFILE *fp) {
FCEUFILE *zp;
int x;
uint32_t x;
char *fn = FCEU_MakeFName(FCEUMKF_FDSROM, 0, 0);
@@ -691,6 +691,10 @@ int FDSLoad(const char *name, FCEUFILE *fp) {
FDSBIOSsize = 8192;
FDSBIOS = (uint8_t*)FCEU_gmalloc(FDSBIOSsize);
if (!FDSBIOS) {
FCEU_fclose(zp);
return 0;
}
SetupCartPRGMapping(0, FDSBIOS, FDSBIOSsize, 0);
if (FCEU_fread(FDSBIOS, 1, FDSBIOSsize, zp) != FDSBIOSsize) {
@@ -717,7 +721,7 @@ int FDSLoad(const char *name, FCEUFILE *fp) {
for (x = 0; x < TotalSides; x++) {
diskdatao[x] = (uint8_t*)FCEU_malloc(65500);
if (!diskdatao[x]) {
int y;
uint32_t y;
for (y = 0; y < x; y++) {
free(diskdatao[y]);
diskdatao[y] = NULL;
@@ -775,11 +779,15 @@ int FDSLoad(const char *name, FCEUFILE *fp) {
CHRRAMSize = 8192;
CHRRAM = (uint8_t*)FCEU_gmalloc(CHRRAMSize);
if (!CHRRAM)
return 0;
SetupCartCHRMapping(0, CHRRAM, CHRRAMSize, 1);
AddExState(CHRRAM, CHRRAMSize, 0, "CHRR");
FDSRAMSize = 32768;
FDSRAM = (uint8_t*)FCEU_gmalloc(FDSRAMSize);
if (!FDSRAM)
return 0;
SetupCartPRGMapping(1, FDSRAM, FDSRAMSize, 1);
AddExState(FDSRAM, FDSRAMSize, 0, "FDSR");
@@ -796,7 +804,7 @@ int FDSLoad(const char *name, FCEUFILE *fp) {
}
void FDSClose(void) {
int x;
uint32_t x;
if (!DiskWritten) return;

View File

@@ -50,7 +50,7 @@ void FCEUI_SetBaseDirectory(const char *dir)
strlcpy(BaseDirectory, dir, sizeof(BaseDirectory));
}
char *FCEU_MakeFName(int type, int id1, char *cd1)
char *FCEU_MakeFName(int type, int id1, const char *cd1)
{
char tmp[4096 + 512] = {0}; /* +512 for no reason :D */
char *ret = 0;

View File

@@ -3,7 +3,7 @@
extern uint32_t uppow2(uint32_t n);
char *FCEU_MakeFName(int type, int id1, char *cd1);
char *FCEU_MakeFName(int type, int id1, const char *cd1);
enum FILE_TYPES
{

View File

@@ -1209,6 +1209,8 @@ int iNESLoad(const char *name, FCEUFILE *fp)
if (head.ROM_type & 4)
{
trainerpoo = (uint8_t*)FCEU_gmalloc(512);
if (!trainerpoo)
return 0;
FCEU_fread(trainerpoo, 512, 1, fp);
filesize -= 512;
}
@@ -1377,6 +1379,8 @@ int iNESLoad(const char *name, FCEUFILE *fp)
if (iNESCart.mirror == 2)
{
ExtraNTARAM = (uint8_t*)FCEU_gmalloc(2048);
if (!ExtraNTARAM)
return 0;
SetupCartMirroring(4, 1, ExtraNTARAM);
}
else if (iNESCart.mirror >= 0x10)

View File

@@ -1,6 +1,6 @@
#include "share.h"
static uint8_t GunSight[] = {
static const uint8_t GunSight[] = {
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
@@ -15,7 +15,7 @@ static uint8_t GunSight[] = {
0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
};
static uint8_t FCEUcursor[11 * 19] =
static const uint8_t FCEUcursor[11 * 19] =
{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,

View File

@@ -27,7 +27,7 @@ static uint8_t bufit[0x49];
static uint8_t ksmode;
static uint8_t ksindex;
static uint16_t matrix[9][2][4] =
static const uint16_t matrix[9][2][4] =
{
{ { AK(F8), AK(RETURN), AK(BRACKETLEFT), AK(BRACKETRIGHT) },
{ AK(KANA), AK(RIGHTSHIFT), AK(BACKSLASH), AK(STOP) } },

View File

@@ -30,7 +30,7 @@ static uint8_t ksindex;
/* TODO: check all keys, some of the are wrong */
static uint16_t matrix[13][8] =
static const uint16_t matrix[13][8] =
{
{ AK(ESCAPE),AK(SPACE),AK(LMENU),AK(LCONTROL),AK(LSHIFT),AK(GRAVE),AK(TAB),AK(CAPITAL) },
{ AK(F6),AK(F7),AK(F5),AK(F4),AK(F8),AK(F2),AK(F1),AK(F3) },

View File

@@ -27,7 +27,7 @@ static uint8_t bufit[0x66];
static uint8_t ksmode;
static uint8_t ksindex;
static uint16_t matrix[13][2][4] =
static const uint16_t matrix[13][2][4] =
{
{ { AK(4), AK(G), AK(F), AK(C) }, { AK(F2), AK(E), AK(5), AK(V) } },
{ { AK(2), AK(D), AK(S), AK(END) }, { AK(F1), AK(W), AK(3), AK(X) } },

View File

@@ -63,7 +63,7 @@ static void FP_FASTAPASS(3) ZapperFrapper(int w, uint8_t * bg, uint8_t * spr, ui
if (xe > 256) xe = 256;
if (scanline >= (zy - tolerance) && scanline <= (zy + tolerance)) {
if (scanline >= (zy - (int)tolerance) && scanline <= (zy + (int)tolerance)) {
#ifdef ROUNDED_TARGET
int spread;
int dy = scanline - zy;

View File

@@ -40,7 +40,7 @@ void md5_starts(struct md5_context *ctx) {
ctx->state[3] = 0x10325476;
}
void md5_process(struct md5_context *ctx, uint8_t data[64]) {
static void md5_process(struct md5_context *ctx, const uint8_t data[64]) {
uint32_t A, B, C, D, X[16];
GET_UINT32(X[0], data, 0);
@@ -162,7 +162,7 @@ void md5_process(struct md5_context *ctx, uint8_t data[64]) {
ctx->state[3] += D;
}
void md5_update(struct md5_context *ctx, uint8_t *input, uint32_t length) {
void md5_update(struct md5_context *ctx, const uint8_t *input, uint32_t length) {
uint32_t left, fill;
if (!length) return;
@@ -195,7 +195,7 @@ void md5_update(struct md5_context *ctx, uint8_t *input, uint32_t length) {
}
}
static uint8_t md5_padding[64] =
static const uint8_t md5_padding[64] =
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

View File

@@ -8,7 +8,7 @@ struct md5_context {
};
void md5_starts(struct md5_context *ctx);
void md5_update(struct md5_context *ctx, uint8_t *input, uint32_t length);
void md5_update(struct md5_context *ctx, const uint8_t *input, uint32_t length);
void md5_finish(struct md5_context *ctx, uint8_t digest[16]);
/* Uses a static buffer, so beware of how it's used. */

View File

@@ -173,7 +173,7 @@ int NSFLoad(FCEUFILE *fp) {
}
NSFSize = fsize - 0x80;
/* Ceiling: 16 MiB of song data is far beyond reality */
if (NSFSize > (16u << 20)) {
if ((unsigned)NSFSize > (16u << 20)) {
FCEUD_PrintError("NSF song data unreasonably large.");
return(0);
}
@@ -184,7 +184,7 @@ int NSFLoad(FCEUFILE *fp) {
* of NSFMaxBank). 2^19 * 4096 = 2 GiB which is already absurd for
* an NSF file; the previous cap of 2^20 produced 4 GiB which
* overflows signed int and is undefined behaviour. */
if (!NSFMaxBank || NSFMaxBank > (1u << 19)) {
if (!NSFMaxBank || (unsigned)NSFMaxBank > (1u << 19)) {
FCEUD_PrintError("NSF bank count out of range.");
return(0);
}
@@ -245,6 +245,8 @@ int NSFLoad(FCEUFILE *fp) {
ExWRAM = FCEU_gmalloc(32768 + 8192);
else
ExWRAM = FCEU_gmalloc(8192);
if (!ExWRAM)
return 0;
return 1;
}
@@ -481,7 +483,7 @@ void DrawNSF(uint8_t *XBuf) {
DrawTextTrans(XBuf + 26 * 256 + 4 + (((31 - strlen((char*)NSFHeader.Artist)) << 2)), 256, NSFHeader.Artist, 6);
DrawTextTrans(XBuf + 42 * 256 + 4 + (((31 - strlen((char*)NSFHeader.Copyright)) << 2)), 256, NSFHeader.Copyright, 6);
DrawTextTrans(XBuf + 70 * 256 + 4 + (((31 - strlen("Song:")) << 2)), 256, (uint8_t*)"Song:", 6);
DrawTextTrans(XBuf + 70 * 256 + 4 + (((31 - (sizeof("Song:") - 1)) << 2)), 256, (uint8_t*)"Song:", 6);
snprintf(snbuf, sizeof(snbuf), "<%d/%d>", CurrentSong, NSFHeader.TotalSongs);
DrawTextTrans(XBuf + 82 * 256 + 4 + (((31 - strlen(snbuf)) << 2)), 256, (uint8_t*)snbuf, 6);

View File

@@ -113,9 +113,9 @@ void FCEUI_SetPaletteArray(uint8_t *pal, int nEntries) {
static uint8_t lastd = 0;
void SetNESDeemph(uint8_t d, int force) {
static uint16_t rtmul[7] = { 32768 * 1.239, 32768 * .794, 32768 * 1.019, 32768 * .905, 32768 * 1.023, 32768 * .741, 32768 * .75 };
static uint16_t gtmul[7] = { 32768 * .915, 32768 * 1.086, 32768 * .98, 32768 * 1.026, 32768 * .908, 32768 * .987, 32768 * .75 };
static uint16_t btmul[7] = { 32768 * .743, 32768 * .882, 32768 * .653, 32768 * 1.277, 32768 * .979, 32768 * .101, 32768 * .75 };
static const uint16_t rtmul[7] = { 32768 * 1.239, 32768 * .794, 32768 * 1.019, 32768 * .905, 32768 * 1.023, 32768 * .741, 32768 * .75 };
static const uint16_t gtmul[7] = { 32768 * .915, 32768 * 1.086, 32768 * .98, 32768 * 1.026, 32768 * .908, 32768 * .987, 32768 * .75 };
static const uint16_t btmul[7] = { 32768 * .743, 32768 * .882, 32768 * .653, 32768 * 1.277, 32768 * .979, 32768 * .101, 32768 * .75 };
uint32_t r, g, b;
int x;

View File

@@ -637,7 +637,7 @@ static void DoLine(void)
uint8_t *target = NULL;
uint8_t *dtarget = NULL;
if (scanline >= 240 && scanline != totalscanlines)
if (scanline >= 240 && (unsigned)scanline != totalscanlines)
{
X6502_Run(256 + 69);
scanline++;
@@ -1166,30 +1166,6 @@ int FCEUPPU_Loop(int skip) {
}
if (GameInfo->type == GIT_NSF)
X6502_Run((256 + 85) * normal_scanlines);
#ifdef FRAMESKIP
else if (skip) {
int y;
y = SPRAM[0];
y++;
PPU_status |= 0x20; /* Fixes "Bee 52". Does it break anything? */
if (GameHBIRQHook) {
X6502_Run(256);
for (scanline = 0; scanline < 240; scanline++) {
if (ScreenON || SpriteON)
GameHBIRQHook();
if (scanline == y && SpriteON) PPU_status |= 0x40;
X6502_Run((scanline == 239) ? 85 : (256 + 85));
}
} else if (y < 240) {
X6502_Run((256 + 85) * y);
if (SpriteON) PPU_status |= 0x40; /* Quick and very dirty hack. */
X6502_Run((256 + 85) * (240 - y));
} else
X6502_Run((256 + 85) * 240);
}
#endif
else {
int x, max, maxref;
@@ -1201,10 +1177,10 @@ int FCEUPPU_Loop(int skip) {
else
totalscanlines = normal_scanlines + (overclock_enabled ? extrascanlines : 0);
for (scanline = 0; scanline < totalscanlines; ) { /* scanline is incremented in DoLine. Evil. :/ */
for (scanline = 0; (unsigned)scanline < totalscanlines; ) { /* scanline is incremented in DoLine. Evil. :/ */
deempcnt[deemp]++;
DoLine();
if (scanline < normal_scanlines || scanline == totalscanlines)
if ((unsigned)scanline < normal_scanlines || (unsigned)scanline == totalscanlines)
overclocked = 0;
else {
if (DMC_7bit && skip_7bit_overclocking) /* 7bit sample started after 240th line */
@@ -1226,12 +1202,6 @@ int FCEUPPU_Loop(int skip) {
}
}
#ifdef FRAMESKIP
if (skip) {
FCEU_PutImageDummy();
return(0);
} else
#endif
{
FCEU_PutImage();
return(1);

View File

@@ -687,7 +687,7 @@ static void RDoSQLQ(void) {
}
static void RDoTriangle(void) {
int32_t V;
uint32_t V;
int32_t tcout = (tristep & 0xF);
if (!(tristep & 0x10)) tcout ^= 0xF;
tcout = (tcout * 3) << 16; /* (tcout<<1); */

View File

@@ -64,7 +64,7 @@ uint8_t FCEUI_VSUniGetDIPs(void) {
return(vsdip);
}
static uint8_t secdata[2][32] = {
static const uint8_t secdata[2][32] = {
{ 0xff, 0xbf, 0xb7, 0x97, 0x97, 0x17, 0x57, 0x4f,
0x6f, 0x6b, 0xeb, 0xa9, 0xb1, 0x90, 0x94, 0x14,
0x56, 0x4e, 0x6f, 0x6b, 0xeb, 0xa9, 0xb1, 0x90,
@@ -75,7 +75,7 @@ static uint8_t secdata[2][32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
static uint8_t *secptr;
static const uint8_t *secptr;
static uint8_t VSindex;

View File

@@ -312,7 +312,7 @@ redundant) on the variable "x".
#define ST_IX(r) { uint32_t A; GetIX(A); WrMem(A, r); break; }
#define ST_IY(r) { uint32_t A; GetIYWR(A); WrMem(A, r); break; }
static uint8_t CycTable[256] =
static const uint8_t CycTable[256] =
{
/*0x00*/ 7, 6, 2, 8, 3, 3, 5, 5, 3, 2, 2, 2, 4, 4, 6, 6,
/*0x10*/ 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2, 7, 4, 4, 7, 7,