Skip to content

Commit

Permalink
Minor optimizations to TotalSMS
Browse files Browse the repository at this point in the history
  • Loading branch information
hpvb committed Aug 5, 2022
1 parent 8fc0d61 commit 461b14a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 20 deletions.
12 changes: 11 additions & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ idf_component_register(
segments.lf
)

target_compile_options(${COMPONENT_LIB} PRIVATE -Ofast -fjump-tables -ftree-switch-conversion -Wno-unused-function)
target_compile_options(${COMPONENT_LIB} PRIVATE
-Ofast
-fjump-tables
-ftree-switch-conversion
-ftree-loop-im
-ftree-loop-ivcanon
-fivopts
-ftree-vectorize
-fvariable-expansion-in-unroller
-Wno-unused-function
)
16 changes: 11 additions & 5 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ static uint64_t frame_time = 0;
static uint64_t cpu_time = 0;
static uint64_t vdp_time = 0;

extern uint64_t vdp_display_enabled;
extern uint64_t vdp_display_disabled;

static xQueueHandle button_queue;
static QueueHandle_t video_queue;

Expand Down Expand Up @@ -341,7 +344,7 @@ void main_loop() {
continue;
}

for (size_t i = 0; i < SMS_CPU_CLOCK / 30; i += sms.cpu.cycles) {
for (size_t i = 0; i < SMS_CPU_CLOCK / 60; i += sms.cpu.cycles) {
//uint64_t cpu_start = esp_timer_get_time();
z80_run();
//uint64_t cpu_end = esp_timer_get_time();
Expand Down Expand Up @@ -383,13 +386,13 @@ void main_loop() {
if (cpu_mhz < 3.579545) {
set_overscan_border(0x00f0);

ESP_LOGW(TAG, "cpu_mhz: %.6f, fps: %lli, dropped: %lli, avg_frametime: %lli, avg_cputime: %lli, avg_vdptime: %lli",
cpu_mhz, frames, dropped_frames, frame_time / frames, cpu_time / frames, vdp_time / frames);
ESP_LOGW(TAG, "cpu_mhz: %.6f, fps: %lli, dropped: %lli, avg_frametime: %lli, avg_cputime: %lli, avg_vdptime: %lli, vdp_enabled: %lli, vdp_disabled: %lli",
cpu_mhz, frames, dropped_frames, frame_time / frames, cpu_time / frames, vdp_time / frames, vdp_display_enabled, vdp_display_disabled);
} else {
set_overscan_border(current_overscan_color);

ESP_LOGI(TAG, "cpu_mhz: %.6f, fps: %lli, dropped: %lli, avg_frametime: %lli, avg_cputime: %lli, avg_vdptime: %lli",
cpu_mhz, frames, dropped_frames, frame_time / frames, cpu_time / frames, vdp_time / frames);
ESP_LOGI(TAG, "cpu_mhz: %.6f, fps: %lli, dropped: %lli, avg_frametime: %lli, avg_cputime: %lli, avg_vdptime: %lli, vdp_enabled: %lli, vdp_disabled: %lli",
cpu_mhz, frames, dropped_frames, frame_time / frames, cpu_time / frames, vdp_time / frames, vdp_display_enabled, vdp_display_disabled);
}

frames = 0;
Expand All @@ -399,6 +402,9 @@ void main_loop() {
cpu_time = 0;
vdp_time = 0;

vdp_display_enabled = 0;
vdp_display_disabled = 0;

start = esp_timer_get_time();
}
}
Expand Down
19 changes: 13 additions & 6 deletions main/totalsms/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

extern struct SMS_Core sms;

Expand Down Expand Up @@ -314,6 +313,7 @@ void mapper_update()
case MAPPER_TYPE_OTHELLO:
setup_mapper_othello();
break;
default: __builtin_unreachable();
}

// map the bios is enabled (if we have it)
Expand Down Expand Up @@ -410,16 +410,17 @@ static void sega_mapper_fffx_write(const uint16_t addr, const uint8_t value)
sega_mapper_update_slot2();
}
break;
default: __builtin_unreachable();
}
}

uint8_t SMS_read8(const uint16_t addr)
uint8_t FORCE_INLINE SMS_read8(const uint16_t addr)
{
assert(sms.rmap[addr >> 10] && "NULL ptr in rmap!");
return sms.rmap[addr >> 10][addr & 0x3FF];
}

void SMS_write8(const uint16_t addr, const uint8_t value)
void FORCE_INLINE SMS_write8(const uint16_t addr, const uint8_t value)
{
// specific mapper writes
switch (sms.cart.mapper_type)
Expand Down Expand Up @@ -462,21 +463,22 @@ void SMS_write8(const uint16_t addr, const uint8_t value)
case MAPPER_TYPE_THE_CASTLE:
case MAPPER_TYPE_OTHELLO:
break;
default: __builtin_unreachable();
}

assert(sms.wmap[addr >> 10] && "NULL ptr in wmap!");
sms.wmap[addr >> 10][addr & 0x3FF] = value;
}

uint16_t SMS_read16(const uint16_t addr)
uint16_t FORCE_INLINE SMS_read16(const uint16_t addr)
{
const uint16_t lo = SMS_read8(addr + 0);
const uint16_t hi = SMS_read8(addr + 1);

return (hi << 8) | lo;
}

void SMS_write16(const uint16_t addr, const uint16_t value)
void FORCE_INLINE SMS_write16(const uint16_t addr, const uint16_t value)
{
SMS_write8(addr + 0, value & 0xFF);
SMS_write8(addr + 1, value >> 8);
Expand Down Expand Up @@ -634,6 +636,7 @@ static void IO_vdp_data_write(const uint8_t value)

sms.vdp.addr = (sms.vdp.addr + 1) & 0x3FFF;
break;
default: __builtin_unreachable();
}
}

Expand Down Expand Up @@ -665,6 +668,7 @@ static void IO_vdp_control_write(const uint8_t value)
case VDP_CODE_CRAM_WRITE:
sms.vdp.addr = sms.vdp.control_word & 0x3FFF;
break;
default: __builtin_unreachable();
}
}
else
Expand All @@ -685,6 +689,7 @@ static uint8_t IO_gamegear_read(const uint8_t addr)
case 0x3: return /* 0x00; */ sms.port.gg_regs[0x3];
case 0x4: return /* 0xFF; */ sms.port.gg_regs[0x4];
case 0x5: return /* 0x00; */ sms.port.gg_regs[0x5];
default: __builtin_unreachable();
}

UNREACHABLE(0xFF);
Expand Down Expand Up @@ -810,6 +815,7 @@ uint8_t SMS_read_io(const uint8_t addr)
case 0xF1: case 0xF3: case 0xF5: case 0xF7:
case 0xF9: case 0xFB: case 0xFD: case 0xFF:
return sms.port.b;
default: __builtin_unreachable();
}

UNREACHABLE(0xFF);
Expand Down Expand Up @@ -899,6 +905,7 @@ void SMS_write_io(const uint8_t addr, const uint8_t value)
case 0xB1: case 0xB3: case 0xB5: case 0xB7:
case 0xB9: case 0xBB: case 0xBD: case 0xBF:
IO_vdp_control_write(value);
break;
break;
default: __builtin_unreachable();
}
}
1 change: 1 addition & 0 deletions main/totalsms/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ extern "C" {
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

#if SMS_DEBUG
#include <assert.h>
#define SMS_log(...) ESP_LOGI(TAG, __VA_ARGS__)
#define SMS_log_err(...) ESP_LOGE(TAG, __VA_ARGS__)
#define SMS_log_fatal(...) do { ESP_LOGE(TAG, __VA_ARGS__); assert(0); } while(0)
Expand Down
2 changes: 1 addition & 1 deletion main/totalsms/psg.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static FORCE_INLINE void data_reg_write(const uint8_t value)
}
}

void psg_reg_write(const uint8_t value)
void FORCE_INLINE psg_reg_write(const uint8_t value)
{
psg_sync();

Expand Down
1 change: 0 additions & 1 deletion main/totalsms/sms.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "types.h"
#include "rom_database.h"

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
Expand Down
5 changes: 4 additions & 1 deletion main/totalsms/vdp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "sms.h"
#include "internal.h"
#include "types.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -1030,6 +1029,8 @@ void render_half_frame() {
}
}

uint64_t vdp_display_enabled = 0;
uint64_t vdp_display_disabled = 0;
void vdp_render_frame()
{
// only render if display is enabled
Expand All @@ -1040,8 +1041,10 @@ void vdp_render_frame()
{
vdp_parse_sprites();
}
++vdp_display_disabled;
return;
}
++vdp_display_enabled;

// exit early if we have no pixels (this will break games that need sprite overflow and collision)
if (!sms.pixels || sms.skip_frame)
Expand Down
18 changes: 13 additions & 5 deletions main/totalsms/z80.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <stdint.h>
#include <string.h>
#include <assert.h>

extern struct SMS_Core sms;

Expand Down Expand Up @@ -232,6 +231,7 @@ static FORCE_INLINE uint8_t get_r8(const uint8_t idx)
case 0x5: return REG_L;
case 0x6: return read8(REG_HL);
case 0x7: return REG_A;
default: __builtin_unreachable();
}

UNREACHABLE(0xFF);
Expand All @@ -249,6 +249,7 @@ static FORCE_INLINE void set_r8(const uint8_t value, const uint8_t idx)
case 0x5: REG_L = value; break;
case 0x6: write8(REG_HL, value); break;
case 0x7: REG_A = value; break;
default: __builtin_unreachable();
}
}

Expand All @@ -260,6 +261,7 @@ static FORCE_INLINE uint16_t get_r16(const uint8_t idx)
case 0x1: return REG_DE;
case 0x2: return REG_HL;
case 0x3: return REG_SP;
default: __builtin_unreachable();
}

UNREACHABLE(0xFF);
Expand All @@ -273,6 +275,7 @@ static FORCE_INLINE void set_r16(uint16_t value, const uint8_t idx)
case 0x1: SET_REG_DE(value); break;
case 0x2: SET_REG_HL(value); break;
case 0x3: REG_SP = value; break;
default: __builtin_unreachable();
}
}

Expand Down Expand Up @@ -1471,6 +1474,7 @@ static FORCE_INLINE bool _CB(const uint8_t opcode, const uint8_t value, uint8_t*
case 0x1C: case 0x1D: case 0x1E: case 0x1F:
*result = SET(value, 1 << ((opcode >> 3) & 0x7));
return true;
default: __builtin_unreachable();
}

UNREACHABLE(false);
Expand Down Expand Up @@ -1512,6 +1516,7 @@ static FORCE_INLINE void execute_CB_IXIY(uint16_t ixy)
case 0x5: REG_L = result; break;
case 0x6: write8(addr, result); break;
case 0x7: REG_A = result; break;
default: __builtin_unreachable();
}

sms.cpu.cycles += 23;
Expand Down Expand Up @@ -1694,14 +1699,15 @@ static FORCE_INLINE void execute_IXIY(uint8_t* ixy_hi, uint8_t* ixy_lo)
case 0xCB: execute_CB_IXIY(pair); break;

default:
SMS_log_fatal("UNK OP: 0xFD%02X", opcode);
__builtin_unreachable();
//SMS_log_fatal("UNK OP: 0xFD%02X", opcode);
break;
}

#undef DISP
}

static FORCE_INLINE void execute_ED()
__attribute__((noinline)) static void execute_ED()
{
const uint8_t opcode = read8(REG_PC++);
sms.cpu.cycles += CYC_ED[opcode];
Expand Down Expand Up @@ -1831,7 +1837,8 @@ static FORCE_INLINE void execute_ED()
case 0xBB: OTDR(); break;

default:
SMS_log_fatal("UNK OP: 0xED%02X", opcode);
__builtin_unreachable();
//SMS_log_fatal("UNK OP: 0xED%02X", opcode);
break;
}
}
Expand Down Expand Up @@ -2045,7 +2052,8 @@ static FORCE_INLINE void execute()
case 0xFD: execute_IXIY(&REG_IYH, &REG_IYL); break;

default:
SMS_log_fatal("UNK OP: 0x%02X", opcode);
__builtin_unreachable();
//SMS_log_fatal("UNK OP: 0x%02X", opcode);
break;
}
}
Expand Down

0 comments on commit 461b14a

Please sign in to comment.