Simplifying

This commit is contained in:
Sergio Martin 2024-01-13 14:06:33 +01:00
parent 8059551339
commit 1389a87ad6
11 changed files with 86 additions and 116 deletions

View File

@ -20,7 +20,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
// Data_Reader // Data_Reader
const char * Data_Reader::read( void* p, int n ) const char * Data_Reader::read( void* p, size_t n )
{ {
if ( n < 0 ) if ( n < 0 )
return "Internal usage bug"; return "Internal usage bug";
@ -38,14 +38,11 @@ const char * Data_Reader::read( void* p, int n )
return err; return err;
} }
const char * Data_Reader::read_avail( void* p, int* n_ ) const char * Data_Reader::read_avail( void* p, size_t* n_ )
{ {
int n = min( (uint64_t)(*n_), remain() ); int n = min( (uint64_t)(*n_), remain() );
*n_ = 0; *n_ = 0;
if ( n < 0 )
return "Internal usage bug";
if ( n <= 0 ) if ( n <= 0 )
return 0; return 0;
@ -59,14 +56,6 @@ const char * Data_Reader::read_avail( void* p, int* n_ )
return err; return err;
} }
const char * Data_Reader::read_avail( void* p, long* n )
{
int i = STATIC_CAST(int, *n);
const char * err = read_avail( p, &i );
*n = i;
return err;
}
const char * Data_Reader::skip_v( int count ) const char * Data_Reader::skip_v( int count )
{ {
char buf [512]; char buf [512];
@ -79,11 +68,8 @@ const char * Data_Reader::skip_v( int count )
return 0; return 0;
} }
const char * Data_Reader::skip( int n ) const char * Data_Reader::skip( size_t n )
{ {
if ( n < 0 )
return "Internal usage bug";
if ( n <= 0 ) if ( n <= 0 )
return 0; return 0;

View File

@ -22,18 +22,17 @@ public:
// Reads min(*n,remain()) bytes and sets *n to this number, thus trying to read more // 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(). // tham remain() bytes doesn't result in error, just *n being set to remain().
const char * read_avail( void* p, int* n ); const char * read_avail( void* p, size_t* n );
const char * read_avail( void* p, long* n );
// Reads exactly n bytes, or returns error if they couldn't ALL be read. // 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. // Reading past end of file results in blargg_err_file_eof.
const char * read( void* p, int n ); const char * read( void* p, size_t n );
// Number of bytes remaining until end of file // Number of bytes remaining until end of file
uint64_t remain() const { return remain_; } size_t remain() const { return remain_; }
// Reads and discards n bytes. Skipping past end of file results in blargg_err_file_eof. // Reads and discards n bytes. Skipping past end of file results in blargg_err_file_eof.
const char * skip( int n ); const char * skip( size_t n );
virtual ~Data_Reader() { } virtual ~Data_Reader() { }
@ -47,7 +46,7 @@ protected:
Data_Reader() : remain_( 0 ) { } Data_Reader() : remain_( 0 ) { }
// Sets remain // Sets remain
void set_remain( uint64_t n ) { remain_ = n; } void set_remain( size_t n ) { remain_ = n; }
// Do same as read(). Guaranteed that 0 < n <= remain(). Value of remain() is updated // Do same as read(). Guaranteed that 0 < n <= remain(). Value of remain() is updated
// AFTER this call succeeds, not before. set_remain() should NOT be called from this. // AFTER this call succeeds, not before. set_remain() should NOT be called from this.

View File

@ -163,7 +163,7 @@ Nes_Nonlinearizer::Nes_Nonlinearizer()
float const gain = 0x7fff * 1.3f; float const gain = 0x7fff * 1.3f;
// don't use entire range, so any overflow will stay within table // don't use entire range, so any overflow will stay within table
int const range = (int) (table_size * Nes_Apu::nonlinear_tnd_gain()); int const range = (int) (table_size * Nes_Apu::nonlinear_tnd_gain());
for ( int i = 0; i < table_size; i++ ) for (uint32_t i = 0; i < table_size; i++ )
{ {
int const offset = table_size - range; int const offset = table_size - range;
int j = i - offset; int j = i - offset;

View File

@ -11,8 +11,8 @@ class Nes_Apu;
class Nes_Nonlinearizer { class Nes_Nonlinearizer {
private: private:
enum { table_bits = 11 }; static const uint8_t table_bits = 11;
enum { table_size = 1 << table_bits }; static const uint32_t table_size = 1 << table_bits;
int16_t table [table_size]; int16_t table [table_size];
Nes_Apu* apu; Nes_Apu* apu;
long accum; long accum;

View File

@ -207,9 +207,6 @@ void Nes_Core::enable_sram( bool b, bool read_only )
// Unmapped memory // Unmapped memory
#ifndef NDEBUG
static nes_addr_t last_unmapped_addr;
#endif
void Nes_Core::log_unmapped( nes_addr_t addr, int data ) void Nes_Core::log_unmapped( nes_addr_t addr, int data )
{ {

View File

@ -60,7 +60,7 @@ private:
} oscs [osc_count]; } oscs [osc_count];
blip_time_t last_time; blip_time_t last_time;
enum { amp_range = 192 }; // can be any value; this gives best error/quality tradeoff static const uint8_t amp_range = 192; // can be any value; this gives best error/quality tradeoff
Blip_Synth<blip_good_quality,1> synth; Blip_Synth<blip_good_quality,1> synth;
void run_until( blip_time_t ); void run_until( blip_time_t );

View File

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

View File

@ -1,68 +0,0 @@
while ( true )
{
while ( count-- )
{
int attrib = attr_table [addr >> 2 & 0x07];
attrib >>= (addr >> 4 & 4) | (addr & 2);
unsigned long offset = (attrib & 3) * attrib_factor + this->palette_offset;
// draw one tile
cache_t const* lines = this->get_bg_tile( nametable [addr] + bg_bank );
uint8_t* p = pixels;
addr++;
pixels += 8; // next tile
if ( !clipped )
{
// optimal case: no clipping
for ( int n = 4; n--; )
{
unsigned long line = *lines++;
((unaligned_uint32_t*) p) [0].val = (line >> 4 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line & mask) + offset;
p += row_bytes;
((unaligned_uint32_t*) p) [0].val = (line >> 6 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line >> 2 & mask) + offset;
p += row_bytes;
}
}
else
{
lines += fine_y >> 1;
if ( fine_y & 1 )
{
unsigned long line = *lines++;
((unaligned_uint32_t*) p) [0].val = (line >> 6 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line >> 2 & mask) + offset;
p += row_bytes;
}
for ( int n = height >> 1; n--; )
{
unsigned long line = *lines++;
((unaligned_uint32_t*) p) [0].val = (line >> 4 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line & mask) + offset;
p += row_bytes;
((unaligned_uint32_t*) p) [0].val = (line >> 6 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line >> 2 & mask) + offset;
p += row_bytes;
}
if ( height & 1 )
{
unsigned long line = *lines;
((unaligned_uint32_t*) p) [0].val = (line >> 4 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line & mask) + offset;
}
}
}
count = count2;
count2 = 0;
addr -= 32;
attr_table = attr_table - nametable + nametable2;
nametable = nametable2;
if ( !count )
break;
}

View File

@ -190,23 +190,80 @@ void Nes_Ppu_Rendering::draw_background_( int remain )
unsigned long const mask = 0x03030303 + zero; unsigned long const mask = 0x03030303 + zero;
unsigned long const attrib_factor = 0x04040404 + zero; unsigned long const attrib_factor = 0x04040404 + zero;
if ( height == 8 ) const int fine_y = (height == 8) ? 0 : addr >> 12;
const int clipped = (height == 8) ? false : true;
addr &= 0x03ff;
if (height == 8) height -= fine_y & 1;
while ( true )
{ {
// unclipped while ( count-- )
addr &= 0x03ff; {
int const fine_y = 0; int attrib = attr_table [addr >> 2 & 0x07];
int const clipped = false; attrib >>= (addr >> 4 & 4) | (addr & 2);
#include "Nes_Ppu_Bg.h" unsigned long offset = (attrib & 3) * attrib_factor + this->palette_offset;
}
else // draw one tile
{ cache_t const* lines = this->get_bg_tile( nametable [addr] + bg_bank );
// clipped uint8_t* p = pixels;
int const fine_y = addr >> 12; addr++;
addr &= 0x03ff; pixels += 8; // next tile
height -= fine_y & 1;
int const clipped = true; if ( !clipped )
#include "Nes_Ppu_Bg.h" {
// optimal case: no clipping
for ( int n = 4; n--; )
{
unsigned long line = *lines++;
((unaligned_uint32_t*) p) [0].val = (line >> 4 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line & mask) + offset;
p += row_bytes;
((unaligned_uint32_t*) p) [0].val = (line >> 6 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line >> 2 & mask) + offset;
p += row_bytes;
}
}
else
{
lines += fine_y >> 1;
if ( fine_y & 1 )
{
unsigned long line = *lines++;
((unaligned_uint32_t*) p) [0].val = (line >> 6 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line >> 2 & mask) + offset;
p += row_bytes;
}
for ( int n = height >> 1; n--; )
{
unsigned long line = *lines++;
((unaligned_uint32_t*) p) [0].val = (line >> 4 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line & mask) + offset;
p += row_bytes;
((unaligned_uint32_t*) p) [0].val = (line >> 6 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line >> 2 & mask) + offset;
p += row_bytes;
}
if ( height & 1 )
{
unsigned long line = *lines;
((unaligned_uint32_t*) p) [0].val = (line >> 4 & mask) + offset;
((unaligned_uint32_t*) p) [1].val = (line & mask) + offset;
}
}
}
count = count2;
count2 = 0;
addr -= 32;
attr_table = attr_table - nametable + nametable2;
nametable = nametable2;
if ( !count )
break;
} }
} }
while ( remain ); while ( remain );
} }

View File

@ -1,8 +1,6 @@
#include "abstract_file.h" #include "abstract_file.h"
#include "blargg_config.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -111,6 +111,7 @@ typedef unsigned long nes_ntsc_rgb_t;
struct nes_ntsc_t { struct nes_ntsc_t {
nes_ntsc_rgb_t table [nes_ntsc_palette_size] [nes_ntsc_entry_size]; 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 }; enum { nes_ntsc_burst_size = nes_ntsc_entry_size / nes_ntsc_burst_count };
#define NES_NTSC_ENTRY_( ktable, n ) \ #define NES_NTSC_ENTRY_( ktable, n ) \