Skip to content

Commit da4f587

Browse files
committed
Improved timing statistics consistency, now it always starts at the top of the loop for each application (that has a loop)
1 parent 34e2e43 commit da4f587

File tree

15 files changed

+177
-178
lines changed

15 files changed

+177
-178
lines changed

gbsp/main/main.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ void app_main(void)
173173
while (true)
174174
{
175175
// RG_TIMER_INIT();
176+
const int64_t startTime = rg_system_timer();
176177
uint32_t joystick = rg_input_read_gamepad();
177178

178179
if (joystick & (RG_KEY_MENU | RG_KEY_OPTION))
@@ -182,13 +183,11 @@ void app_main(void)
182183
else
183184
rg_gui_options_menu();
184185
memset(&mixbuffer, 0, sizeof(mixbuffer));
186+
continue;
185187
}
186188

187-
int64_t start_time = rg_system_timer();
188-
189189
update_input();
190190
rumble_frame_reset();
191-
192191
clear_gamepak_stickybits();
193192
execute_arm(execute_cycles);
194193
// RG_TIMER_LAP("execute_arm");
@@ -199,7 +198,7 @@ void app_main(void)
199198
size_t frames_count = sound_read_samples((s16 *)mixbuffer, AUDIO_BUFFER_LENGTH);
200199
// RG_TIMER_LAP("sound_read_samples");
201200

202-
rg_system_tick(rg_system_timer() - start_time);
201+
rg_system_tick(rg_system_timer() - startTime);
203202

204203
rg_audio_submit(mixbuffer, frames_count);
205204
// RG_TIMER_LAP("rg_audio_submit");

gwenesis/main/main.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,24 +323,28 @@ void app_main(void)
323323
extern int hint_pending;
324324

325325
uint32_t keymap[8] = {RG_KEY_UP, RG_KEY_DOWN, RG_KEY_LEFT, RG_KEY_RIGHT, RG_KEY_A, RG_KEY_B, RG_KEY_SELECT, RG_KEY_START};
326-
uint32_t joystick = 0, joystick_old;
326+
uint32_t joystick_old;
327327

328328
int skipFrames = 0;
329329

330-
RG_LOGI("emulation loop\n");
330+
RG_LOGI("emulation loop");
331331
while (true)
332332
{
333-
joystick_old = joystick;
334-
joystick = rg_input_read_gamepad();
333+
const int64_t startTime = rg_system_timer();
334+
uint32_t joystick = rg_input_read_gamepad();
335+
bool drawFrame = skipFrames == 0;
336+
bool slowFrame = false;
335337

336338
if (joystick & (RG_KEY_MENU | RG_KEY_OPTION))
337339
{
338340
if (joystick & RG_KEY_MENU)
339341
rg_gui_game_menu();
340342
else
341343
rg_gui_options_menu();
344+
continue;
342345
}
343-
else if (joystick != joystick_old)
346+
347+
if (joystick != joystick_old)
344348
{
345349
for (int i = 0; i < 8; i++)
346350
{
@@ -349,12 +353,9 @@ void app_main(void)
349353
else
350354
gwenesis_io_pad_release_button(0, i);
351355
}
356+
joystick_old = joystick;
352357
}
353358

354-
int64_t startTime = rg_system_timer();
355-
bool drawFrame = skipFrames == 0;
356-
bool slowFrame = false;
357-
358359
int lines_per_frame = REG1_PAL ? LINES_PER_FRAME_PAL : LINES_PER_FRAME_NTSC;
359360
int hint_counter = gwenesis_vdp_regs[10];
360361

retro-core/components/pce-go/gfx.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@ gfx_latch_context(int force)
311311
static inline void
312312
render_lines(int min_line, int max_line)
313313
{
314-
gfx_context.latched = 0;
315-
316314
uint8_t *screen_buffer = osd_gfx_framebuffer(PCE.VDC.screen_width, PCE.VDC.screen_height);
317315
if (!screen_buffer) {
318316
return;
@@ -399,7 +397,7 @@ gfx_irq(int type)
399397
Process one scanline
400398
*/
401399
void
402-
gfx_run(void)
400+
gfx_run(bool draw)
403401
{
404402
int scanline = PCE.Scanline;
405403

@@ -431,8 +429,10 @@ gfx_run(void)
431429

432430
if (scanline >= IO_VDC_MINLINE && scanline <= IO_VDC_MAXLINE) {
433431
if (gfx_context.latched) {
434-
render_lines(last_line_counter, line_counter);
432+
if (draw)
433+
render_lines(last_line_counter, line_counter);
435434
last_line_counter = line_counter;
435+
gfx_context.latched = 0;
436436
}
437437
line_counter++;
438438
}
@@ -442,7 +442,9 @@ gfx_run(void)
442442

443443
// Draw any lines left in the context
444444
gfx_latch_context(0);
445-
render_lines(last_line_counter, line_counter);
445+
if (draw)
446+
render_lines(last_line_counter, line_counter);
447+
gfx_context.latched = 0;
446448

447449
// Trigger interrupts
448450
if (SpHitON && sprite_hit_check()) {

retro-core/components/pce-go/gfx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <stdbool.h>
44

55
int gfx_init(void);
6-
void gfx_run(void);
6+
void gfx_run(bool draw);
77
void gfx_term(void);
88
void gfx_irq(int type);
99
void gfx_reset(bool hard);

retro-core/components/pce-go/pce-go.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ static const struct {
9393
{0x00000000, "Unknown", 0},
9494
};
9595

96-
static bool running = false;
97-
9896
/**
9997
* Load card into memory and set its memory map
10098
* NOTE: This function takes ownership of `data`
@@ -320,19 +318,21 @@ PalettePCE(int bitdepth)
320318
* Start the emulation
321319
*/
322320
void
323-
RunPCE(void)
321+
RunPCE(bool draw)
324322
{
325-
running = true;
326-
327-
while (running)
328-
{
329-
osd_input_read(PCE.Joypad.regs);
330-
pce_run();
331-
osd_vsync();
332-
}
323+
pce_run(draw);
333324
}
334325

335326

327+
/**
328+
* Set joystick status
329+
*/
330+
void
331+
InputPCE(int port, uint32_t value)
332+
{
333+
PCE.Joypad.regs[port & 0x7] = value & 0xFF;
334+
}
335+
336336
/**
337337
* Load saved state
338338
*/
@@ -441,7 +441,7 @@ SaveState(const char *name)
441441
* Cleanup and quit (not used in retro-go)
442442
*/
443443
void
444-
ShutdownPCE()
444+
ShutdownPCE(void)
445445
{
446446
gfx_term();
447447
psg_term();

retro-core/components/pce-go/pce-go.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@
7070
#define XBUF_WIDTH (352 + 16)
7171
#define XBUF_HEIGHT (242)
7272

73+
int InitPCE(int samplerate, bool stereo);
74+
void ResetPCE(bool hard);
75+
void InputPCE(int port, uint32_t value);
76+
void RunPCE(bool draw);
77+
void ShutdownPCE(void);
78+
7379
int LoadState(const char *name);
7480
int SaveState(const char *name);
75-
void ResetPCE(bool);
76-
void RunPCE(void);
77-
void ShutdownPCE();
78-
int InitPCE(int samplerate, bool stereo);
7981
int LoadCard(uint8_t *data, size_t size);
8082
int LoadFile(const char *name);
8183
void *PalettePCE(int bitdepth);
8284

8385
extern uint8_t *osd_gfx_framebuffer(int width, int height);
84-
extern void osd_input_read(uint8_t joypads[8]);
85-
extern void osd_vsync(void);

retro-core/components/pce-go/pce.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pce_term(void)
122122
* Run emulation for one frame
123123
**/
124124
void
125-
pce_run(void)
125+
pce_run(bool draw)
126126
{
127127
// Handle pending video mode changes
128128
if (PCE.VDC.mode_chg) {
@@ -139,7 +139,7 @@ pce_run(void)
139139
PCE.MaxCycles -= PCE.Cycles;
140140
PCE.Cycles = 0;
141141
}
142-
gfx_run();
142+
gfx_run(draw);
143143
}
144144
}
145145

retro-core/components/pce-go/pce.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ extern uint8_t *PageW[8];
266266
int pce_init(void);
267267
void pce_reset(bool hard);
268268
void pce_term(void);
269-
void pce_run(void);
270-
void pce_pause(void);
269+
void pce_run(bool draw);
271270
void pce_writeIO(uint16_t A, uint8_t V);
272271
uint8_t pce_readIO(uint16_t A);
273272

retro-core/main/main_gbc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ void gbc_main(void)
313313
// Ready!
314314

315315
uint32_t joystick_old = -1;
316-
uint32_t joystick = 0;
317316

318317
while (true)
319318
{
320-
joystick = rg_input_read_gamepad();
319+
const int64_t startTime = rg_system_timer();
320+
uint32_t joystick = rg_input_read_gamepad();
321+
bool drawFrame = !skipFrames;
321322

322323
if (joystick & (RG_KEY_MENU|RG_KEY_OPTION))
323324
{
@@ -329,8 +330,10 @@ void gbc_main(void)
329330
}
330331
else
331332
rg_gui_options_menu();
333+
continue;
332334
}
333-
else if (joystick != joystick_old)
335+
336+
if (joystick != joystick_old)
334337
{
335338
int pad = 0;
336339
if (joystick & RG_KEY_UP) pad |= GB_PAD_UP;
@@ -345,9 +348,6 @@ void gbc_main(void)
345348
joystick_old = joystick;
346349
}
347350

348-
int64_t startTime = rg_system_timer();
349-
bool drawFrame = !skipFrames;
350-
351351
video_time = audio_time = 0;
352352

353353
if (drawFrame)

retro-core/main/main_gw.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,24 @@ void gw_main(void)
229229

230230
while (true)
231231
{
232+
const int64_t startTime = rg_system_timer();
233+
uint32_t joystick = rg_input_read_gamepad();
234+
bool drawFrame = true;
235+
232236
/* refresh internal G&W timer on emulated CPU state transition */
233237
if (previous_m_halt != m_halt)
234238
gw_check_time();
235239

236240
previous_m_halt = m_halt;
237241

238-
// hardware keys
239-
uint32_t joystick = rg_input_read_gamepad();
240-
241-
if (joystick & RG_KEY_MENU)
242-
rg_gui_game_menu();
243-
else if (joystick & RG_KEY_OPTION)
244-
rg_gui_options_menu();
242+
if (joystick & (RG_KEY_MENU|RG_KEY_OPTION))
243+
{
244+
if (joystick & RG_KEY_MENU)
245+
rg_gui_game_menu();
246+
else if (joystick & RG_KEY_OPTION)
247+
rg_gui_options_menu();
248+
continue;
249+
}
245250

246251
// soft keys emulation
247252
if (softkey_duration > 0)
@@ -253,14 +258,21 @@ void gw_main(void)
253258
softkey_alarm_pressed = 0;
254259
}
255260

256-
int64_t startTime = rg_system_timer();
257-
bool drawFrame = true;
258261

259262
/* Emulate and Blit */
260263
// Call the emulator function with number of clock cycles
261264
// to execute on the emulated device
262265
gw_system_run(GW_SYSTEM_CYCLES);
263266

267+
/* copy audio samples for DMA */
268+
rg_audio_sample_t mixbuffer[GW_AUDIO_BUFFER_LENGTH];
269+
for (size_t i = 0; i < GW_AUDIO_BUFFER_LENGTH; i++)
270+
{
271+
mixbuffer[i].left = gw_audio_buffer[i] << 13;
272+
mixbuffer[i].right = gw_audio_buffer[i] << 13;
273+
}
274+
gw_audio_buffer_copied = true;
275+
264276
// Our refresh rate is 128Hz, which is way too fast for our display
265277
// so make sure the previous frame is done sending before queuing a new one
266278
if (rg_display_sync(false) && drawFrame)
@@ -272,15 +284,6 @@ void gw_main(void)
272284

273285
// Tick before submitting audio/syncing
274286
rg_system_tick(rg_system_timer() - startTime);
275-
276-
/* copy audio samples for DMA */
277-
rg_audio_sample_t mixbuffer[GW_AUDIO_BUFFER_LENGTH];
278-
for (size_t i = 0; i < GW_AUDIO_BUFFER_LENGTH; i++)
279-
{
280-
mixbuffer[i].left = gw_audio_buffer[i] << 13;
281-
mixbuffer[i].right = gw_audio_buffer[i] << 13;
282-
}
283287
rg_audio_submit(mixbuffer, GW_AUDIO_BUFFER_LENGTH);
284-
gw_audio_buffer_copied = true;
285288
} // end of loop
286289
}

0 commit comments

Comments
 (0)