Skip to content

Commit

Permalink
implement snapshotting
Browse files Browse the repository at this point in the history
  • Loading branch information
zeim839 committed Nov 21, 2024
1 parent 1014af0 commit 3df1002
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
19 changes: 12 additions & 7 deletions src/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ struct ppu_t;

typedef struct
{
mapper_t* mapper;
uint8_t ram[RAM_SIZE];
uint8_t bus;
joypad_t joy1;
joypad_t joy2;
// Memory.
mapper_t* mapper;
uint8_t ram[RAM_SIZE];
uint8_t bus;

// Joypad controllers.
joypad_t joy1;
joypad_t joy2;

// Bus-connected modules.
struct cpu6502_t* cpu;
struct apu_t* apu;
struct ppu_t* ppu;
struct apu_t* apu;
struct ppu_t* ppu;

} bus_t;

Expand Down
25 changes: 19 additions & 6 deletions src/emulator.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "emulator.h"
#include "snapshot.h"

emulator_t* emulator_create(mapper_t* mapper)
{
Expand Down Expand Up @@ -65,20 +66,25 @@ emulator_t* emulator_create(mapper_t* mapper)

void emulator_exec(emulator_t* emu)
{
joypad_t* joy1 = &emu->bus->joy1;
joypad_t* joy2 = &emu->bus->joy2;
ppu_t* ppu = emu->ppu;
cpu6502_t* cpu = emu->cpu;
apu_t* apu = emu->apu;
gfx_t* gfx = emu->gfx;
gfx_t* gfx = emu->gfx;
timerx_t* timer = &emu->timer;
timerx_t frame_timer = timerx_create(emu->period);
snapshot_t* snapshot = snapshot_create(emu);

SDL_Event e;
timerx_mark_start(&frame_timer);

while (!emu->exit) {

// Reinitialize every loop because snapshots.
joypad_t* joy1 = &emu->bus->joy1;
joypad_t* joy2 = &emu->bus->joy2;
ppu_t* ppu = emu->ppu;
cpu6502_t* cpu = emu->cpu;
apu_t* apu = emu->apu;

timerx_mark_start(timer);

while (SDL_PollEvent(&e)) {
joypad_update(joy1, &e);
joypad_update(joy2, &e);
Expand All @@ -100,6 +106,12 @@ void emulator_exec(emulator_t* emu)
case SDLK_F5:
emulator_reset(emu);
break;
case SDLK_TAB:
snapshot_restore(snapshot, emu);
continue;
case SDLK_q:
snapshot_update(snapshot, emu);
continue;
default:
break;
}
Expand Down Expand Up @@ -162,6 +174,7 @@ void emulator_exec(emulator_t* emu)
timerx_wait(IDLE_SLEEP);
}
}
snapshot_destroy(snapshot);
timerx_mark_end(&frame_timer);
emu->time_diff = timerx_get_diff(&frame_timer);
}
Expand Down

0 comments on commit 3df1002

Please sign in to comment.