Fixes #6 - Throttle CPU speed to 1 Mhz

This commit is contained in:
Stefan Arentz 2016-12-11 11:27:42 -07:00
parent 7be7c2eff9
commit 095ca9d191
3 changed files with 26 additions and 12 deletions

2
cpu.c
View File

@ -170,7 +170,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) {
pc, bytes, trace_instruction, trace_state, trace_stack);
}
return 0;
return i->cycles;
}
/* Public API */

34
sdl.c
View File

@ -177,10 +177,11 @@ static bool sdl_poll_event(struct cpu_t *cpu, struct a2p_t *a2p, struct scr_t *s
return true;
}
static bool sdl_step_cpu(struct cpu_t *cpu, struct a2p_t *a2p, struct scr_t *scr) {
for (int i = 0; i < 5000; i++) {
static bool sdl_step_cpu(struct cpu_t *cpu, struct a2p_t *a2p, struct scr_t *scr, int cycles) {
int i = 0;
while (true) {
int ret = cpu_step(cpu);
if (ret != 0) {
if (ret < 0) {
switch (ret) {
case EWM_CPU_ERR_UNIMPLEMENTED_INSTRUCTION:
fprintf(stderr, "CPU: Exited because of unimplemented instructions 0x%.2x at 0x%.4x\n",
@ -195,6 +196,13 @@ static bool sdl_step_cpu(struct cpu_t *cpu, struct a2p_t *a2p, struct scr_t *scr
}
cpu_nmi(cpu);
}
i++;
cycles -= ret;
if (cycles <= 0) {
break;
}
}
return true;
}
@ -202,21 +210,25 @@ static bool sdl_step_cpu(struct cpu_t *cpu, struct a2p_t *a2p, struct scr_t *scr
void sdl_main(struct cpu_t *cpu, struct a2p_t *a2p, struct scr_t *scr) {
SDL_StartTextInput();
Uint32 ticks = SDL_GetTicks();
while (true) {
if (!sdl_poll_event(cpu, a2p, scr)) {
break;
}
if (!sdl_step_cpu(cpu, a2p, scr)) {
break;
}
if ((SDL_GetTicks() - ticks) >= (1000 / EWM_SDL_FPS)) {
if (!sdl_step_cpu(cpu, a2p, scr, 1000000 / EWM_SDL_FPS)) {
break;
}
SDL_Delay(10); // TODO This should really not be here. Implement proper throttling.
if (a2p->screen_dirty) {
ewm_scr_update(scr);
a2p->screen_dirty = false;
SDL_RenderPresent(scr->renderer);
}
if (a2p->screen_dirty) {
ewm_scr_update(scr);
a2p->screen_dirty = false;
SDL_RenderPresent(scr->renderer);
ticks = SDL_GetTicks();
}
}

2
sdl.h
View File

@ -25,6 +25,8 @@
#include <SDL2/SDL.h>
#define EWM_SDL_FPS (50)
extern SDL_Window *window;
extern SDL_Renderer *renderer;