More refactoring

This commit is contained in:
Sergio Martin 2024-01-13 14:33:35 +01:00
parent 1389a87ad6
commit d023e639c3
13 changed files with 75 additions and 90 deletions

View File

@ -41,7 +41,7 @@ quickerNESCoreSrc = [
# quickerNES Core Configuration
quickerNESCoreDependency = declare_dependency(
compile_args : [ '-Wfatal-errors', '-Wall', '-Wno-multichar' ],
compile_args : [ '-Wfatal-errors', '-Wall', '-Werror', '-Wno-multichar', '-DBLARGG_NONPORTABLE'],
include_directories : include_directories(['source', 'source/core', 'extern']),
sources : [ quickerNESCoreSrc, 'extern/metrohash128/metrohash128.cpp' ]
)

View File

@ -1,7 +1,7 @@
// File_Extractor 1.0.0. http://www.slack.net/~ant/
#include <algorithm>
#include "Data_Reader.h"
#include "blargg_endian.h"
#include <errno.h>
@ -38,30 +38,12 @@ const char * Data_Reader::read( void* p, size_t n )
return err;
}
const char * Data_Reader::read_avail( void* p, size_t* n_ )
{
int n = min( (uint64_t)(*n_), remain() );
*n_ = 0;
if ( n <= 0 )
return 0;
const char * err = read_v( p, n );
if ( !err )
{
remain_ -= n;
*n_ = n;
}
return err;
}
const char * Data_Reader::skip_v( int count )
{
char buf [512];
while ( count )
{
int n = min( count, (int) sizeof buf );
int n = std::min( count, (int) sizeof buf );
count -= n;
RETURN_ERR( read_v( buf, n ) );
}

View File

@ -4,8 +4,10 @@
#ifndef DATA_READER_H
#define DATA_READER_H
#include <stdint.h>
#include <algorithm>
#include <cstdint>
#include "blargg_common.h"
#include "blargg_source.h"
/* Some functions accept a long instead of int for convenience where caller has
a long due to some other interface, and would otherwise have to get a warning,
@ -22,7 +24,23 @@ public:
// Reads min(*n,remain()) bytes and sets *n to this number, thus trying to read more
// tham remain() bytes doesn't result in error, just *n being set to remain().
const char * read_avail( void* p, size_t* n );
const char * read_avail( void* p, size_t* count )
{
int n = std::min(*count, remain());
*count = 0;
if ( n <= 0 )
return 0;
const char * err = read_v( p, n );
if ( !err )
{
remain_ -= n;
*count = n;
}
return err;
}
// Reads exactly n bytes, or returns error if they couldn't ALL be read.
// Reading past end of file results in blargg_err_file_eof.

View File

@ -1,9 +1,9 @@
// Nes_Emu 0.7.0. http://www.slack.net/~ant/
#include <algorithm>
#include <cstring>
#include "Nes_Core.h"
#include <string.h>
#include "Nes_Mapper.h"
#include "Nes_State.h"
@ -167,7 +167,7 @@ void Nes_Core::load_state( Nes_State_ const& in )
if ( in.sram_size )
{
sram_present = true;
memcpy( impl->sram, in.sram, min( (int) in.sram_size, (int) sizeof impl->sram ) );
memcpy( impl->sram, in.sram, std::min( (int) in.sram_size, (int) sizeof impl->sram ) );
enable_sram( true ); // mapper can override (read-only, unmapped, etc.)
}
@ -370,7 +370,7 @@ void Nes_Core::vector_interrupt( nes_addr_t vector )
inline nes_time_t Nes_Core::earliest_irq( nes_time_t present )
{
return min( impl->apu.earliest_irq( present ), mapper->next_irq( present ) );
return std::min( impl->apu.earliest_irq( present ), mapper->next_irq( present ) );
}
void Nes_Core::irq_changed()
@ -395,13 +395,13 @@ inline nes_time_t Nes_Core::earliest_event( nes_time_t present )
// DMC
if ( wait_states_enabled )
t = min( t, impl->apu.next_dmc_read_time() + 1 );
t = std::min( t, impl->apu.next_dmc_read_time() + 1 );
// NMI
t = min( t, ppu.nmi_time() );
t = std::min( t, ppu.nmi_time() );
if ( single_instruction_mode )
t = min( t, present + 1 );
t = std::min( t, present + 1 );
return t;
}

View File

@ -1,14 +1,6 @@
// Nes_Emu 0.7.0. http://www.slack.net/~ant/nes-emu/
// TODO: remove
#if !defined (NDEBUG) && 0
#pragma peephole on
#pragma global_optimizer on
#pragma optimization_level 4
#pragma scheduling 604
#undef BLARGG_ENABLE_OPTIMIZER
#endif
#include "Nes_Cpu.h"
@ -32,10 +24,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "blargg_source.h"
#ifdef BLARGG_ENABLE_OPTIMIZER
#include BLARGG_ENABLE_OPTIMIZER
#endif
inline void Nes_Cpu::set_code_page( int i, uint8_t const* p )
{
code_map [i] = p - (unsigned) i * page_size;

View File

@ -19,9 +19,9 @@ public:
// Map code memory (memory accessed via the program counter). Start and size
// must be multiple of page_size.
enum { page_bits = 11 };
enum { page_count = 0x10000 >> page_bits };
enum { page_size = 1L << page_bits };
static const uint8_t page_bits = 11;
static const uint16_t page_count = 0x10000 >> page_bits;
static const uint16_t page_size = 1L << page_bits;
void map_code( nes_addr_t start, unsigned size, void const* code );
// Access memory as the emulated CPU does.
@ -60,10 +60,10 @@ public:
unsigned long error_count() const { return error_count_; }
// If PC exceeds 0xFFFF and encounters page_wrap_opcode, it will be silently wrapped.
enum { page_wrap_opcode = 0xF2 };
static const uint8_t page_wrap_opcode = 0xF2;
// One of the many opcodes that are undefined and stop CPU emulation.
enum { bad_opcode = 0xD2 };
static const uint8_t bad_opcode = 0xD2;
uint8_t const* code_map [page_count + 1];
nes_time_t clock_limit;
@ -72,7 +72,7 @@ public:
nes_time_t end_time_;
unsigned long error_count_;
enum { irq_inhibit = 0x04 };
static const uint8_t irq_inhibit = 0x04;
void set_code_page( int, uint8_t const* );
void update_clock_limit();

View File

@ -62,7 +62,7 @@ public:
struct frame_t
{
static const uint8_t left = 8;
int joypad_read_count; // number of times joypads were strobed (read)
int burst_phase; // NTSC burst phase for frame (0, 1, or 2)
@ -162,10 +162,10 @@ public:
// Graphics
// Number of frames generated per second
enum { frame_rate = 60 };
static const uint16_t frame_rate = 60;
// Size of fixed NES color table (including the 8 color emphasis modes)
enum { color_table_size = 8 * 64 };
static const uint16_t color_table_size = 8 * 64;
// NES color lookup table based on standard NTSC TV decoder. Use nes_ntsc.h to
// generate a palette with custom parameters.
@ -183,7 +183,7 @@ public:
// Set range of host palette entries to use in graphics buffer; default uses
// all of them. Begin will be rounded up to next multiple of palette_alignment.
// Use frame().palette_begin to find the adjusted beginning entry used.
enum { palette_alignment = 64 };
static const uint8_t palette_alignment = 64;
void set_palette_range( int begin, int end = 256 );
// Access to emulated memory, for viewer/cheater/debugger
@ -198,11 +198,11 @@ public:
long nametable_size() const { return 0x1000; }
// Built-in 2K memory
enum { low_mem_size = 0x800 };
static const uint16_t low_mem_size = 0x800;
uint8_t* low_mem() { return emu.low_mem; }
// Optional 8K memory
enum { high_mem_size = 0x2000 };
static const uint16_t high_mem_size = 0x2000;
uint8_t* high_mem() { return emu.impl->sram; }
// End of public interface

View File

@ -3,9 +3,9 @@
// Nes_Emu 0.7.0. http://www.slack.net/~ant/
#include <algorithm>
#include <cstring>
#include "Nes_Ppu.h"
#include <string.h>
#include "Nes_State.h"
#include "Nes_Mapper.h"
#include "Nes_Core.h"
@ -272,7 +272,7 @@ void Nes_Ppu::update_sprite_hit( nes_time_t cpu_time )
if ( count_needed > 240 )
count_needed = 240;
while ( scanline_count < count_needed )
render_bg_until( max( cpu_time, next_bg_time + 1 ) );
render_bg_until( std::max( cpu_time, next_bg_time + 1 ) );
if ( sprite_hit_found < 0 )
return; // sprite won't hit

View File

@ -3,8 +3,9 @@
#include "Nes_Ppu_Rendering.h"
#include <string.h>
#include <stddef.h>
#include <algorithm>
#include <cstring>
#include <cstddef>
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
@ -509,8 +510,8 @@ void Nes_Ppu_Rendering::draw_background( int start, int count )
{
// not rendering, but still handle sprite hit using mini graphics buffer
int y = spr_ram [0] + 1;
int skip = min( count, max( y - start, 0 ) );
int visible = min( count - skip, sprite_height() );
int skip = std::min( count, std::max( y - start, 0 ) );
int visible = std::min( count - skip, sprite_height() );
if ( visible > 0 )
{

View File

@ -20,7 +20,7 @@ public:
void volume( double );
void treble_eq( blip_eq_t const& );
void output( Blip_Buffer* );
enum { osc_count = 6 };
static const uint8_t osc_count = 6;
void osc_output( int index, Blip_Buffer* );
void end_frame( nes_time_t );
void save_snapshot(vrc7_snapshot_t*);

View File

@ -32,9 +32,4 @@ otherwise continues normally. */
return "Out of memory";\
} while ( 0 )
/* The usual min/max functions for built-in types. */
template<typename T> T min( T x, T y ) { return x < y ? x : y; }
template<typename T> T max( T x, T y ) { return x > y ? x : y; }
#endif

View File

@ -5,6 +5,7 @@
#define NES_NTSC_H
#include "nes_ntsc_config.h"
#include <cstdint>
#ifdef __cplusplus
extern "C" {
@ -46,9 +47,9 @@ extern nes_ntsc_setup_t const nes_ntsc_rgb; /* crisp image */
extern nes_ntsc_setup_t const nes_ntsc_monochrome;/* desaturated + artifacts */
#ifdef NES_NTSC_EMPHASIS
enum { nes_ntsc_palette_size = 64 * 8 };
static const uint16_t nes_ntsc_palette_size = 64 * 8;
#else
enum { nes_ntsc_palette_size = 64 };
static const uint16_t { nes_ntsc_palette_size = 64 };
#endif
/* Initializes and adjusts parameters. Can be called multiple times on the same
@ -79,10 +80,10 @@ value. */
/* Interface for user-defined custom blitters */
enum { nes_ntsc_in_chunk = 3 }; /* number of input pixels read per chunk */
enum { nes_ntsc_out_chunk = 7 }; /* number of output pixels generated per chunk */
enum { nes_ntsc_black = 15 }; /* palette index for black */
enum { nes_ntsc_burst_count = 3 }; /* burst phase cycles through 0, 1, and 2 */
const static uint8_t nes_ntsc_in_chunk = 3; /* number of input pixels read per chunk */
const static uint8_t nes_ntsc_out_chunk = 7 ; /* number of output pixels generated per chunk */
const static uint8_t nes_ntsc_black = 15; /* palette index for black */
const static uint8_t nes_ntsc_burst_count = 3 ; /* burst phase cycles through 0, 1, and 2 */
/* Begins outputting row and starts three pixels. First pixel will be cut off a bit.
Use nes_ntsc_black for unused pixels. Declares variables, so must be before first
@ -106,13 +107,13 @@ statement in a block (unless you're using C++). */
/* private */
enum { nes_ntsc_entry_size = 128 };
const static uint8_t nes_ntsc_entry_size = 128;
typedef unsigned long nes_ntsc_rgb_t;
struct nes_ntsc_t {
nes_ntsc_rgb_t table [nes_ntsc_palette_size] [nes_ntsc_entry_size];
};
enum { nes_ntsc_burst_size = nes_ntsc_entry_size / nes_ntsc_burst_count };
const static uint8_t nes_ntsc_burst_size = nes_ntsc_entry_size / nes_ntsc_burst_count;
#define NES_NTSC_ENTRY_( ktable, n ) \
(nes_ntsc_rgb_t const*) (ktable + (n) * (nes_ntsc_entry_size * sizeof (nes_ntsc_rgb_t)))
@ -123,18 +124,18 @@ enum { nes_ntsc_burst_size = nes_ntsc_entry_size / nes_ntsc_burst_count };
#define NES_NTSC_RGB15_OUT( x, out ) NES_NTSC_RGB_OUT( x, out, 15 )
#define NES_NTSC_RAW_OUT( x, out ) NES_NTSC_RGB_OUT( x, out, 0 )
enum { nes_ntsc_min_in_width = 256 };
enum { nes_ntsc_min_out_width = NES_NTSC_OUT_WIDTH( nes_ntsc_min_in_width ) };
const static uint16_t nes_ntsc_min_in_width = 256;
const static uint16_t nes_ntsc_min_out_width = NES_NTSC_OUT_WIDTH( nes_ntsc_min_in_width );
enum { nes_ntsc_640_in_width = 271 };
enum { nes_ntsc_640_out_width = NES_NTSC_OUT_WIDTH( nes_ntsc_640_in_width ) };
enum { nes_ntsc_640_overscan_left = 8 };
enum { nes_ntsc_640_overscan_right = nes_ntsc_640_in_width - 256 - nes_ntsc_640_overscan_left };
const static uint16_t nes_ntsc_640_in_width = 271;
const static uint16_t nes_ntsc_640_out_width = NES_NTSC_OUT_WIDTH( nes_ntsc_640_in_width );
const static uint16_t nes_ntsc_640_overscan_left = 8;
const static uint16_t nes_ntsc_640_overscan_right = nes_ntsc_640_in_width - 256 - nes_ntsc_640_overscan_left;
enum { nes_ntsc_full_in_width = 283 };
enum { nes_ntsc_full_out_width = NES_NTSC_OUT_WIDTH( nes_ntsc_full_in_width ) };
enum { nes_ntsc_full_overscan_left = 16 };
enum { nes_ntsc_full_overscan_right = nes_ntsc_full_in_width - 256 - nes_ntsc_full_overscan_left };
const static uint16_t nes_ntsc_full_in_width = 283;
const static uint16_t nes_ntsc_full_out_width = NES_NTSC_OUT_WIDTH( nes_ntsc_full_in_width );
const static uint16_t nes_ntsc_full_overscan_left = 16;
const static uint16_t nes_ntsc_full_overscan_right = nes_ntsc_full_in_width - 256 - nes_ntsc_full_overscan_left;
/* common 3->7 ntsc macros */
#define NES_NTSC_BEGIN_ROW_6_( pixel0, pixel1, pixel2, ENTRY, table ) \

View File

@ -44,9 +44,9 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#define rgb_unit (1 << rgb_bits)
#define rgb_offset (rgb_unit * 2 + 0.5f)
enum { burst_size = nes_ntsc_entry_size / burst_count };
enum { kernel_half = 16 };
enum { kernel_size = kernel_half * 2 + 1 };
const static uint16_t burst_size = nes_ntsc_entry_size / burst_count;
const static uint8_t kernel_half = 16;
const static uint8_t kernel_size = kernel_half * 2 + 1;
typedef struct init_t
{
@ -284,8 +284,8 @@ static void init( init_t* impl, nes_ntsc_setup_t const* setup )
#define PACK_RGB( r, g, b ) ((r) << 21 | (g) << 11 | (b) << 1)
enum { rgb_kernel_size = burst_size / alignment_count };
enum { rgb_bias = rgb_unit * 2 * nes_ntsc_rgb_builder };
const static uint16_t rgb_kernel_size = burst_size / alignment_count;
const static uint32_t rgb_bias = rgb_unit * 2 * nes_ntsc_rgb_builder;
typedef struct pixel_info_t
{