~sircmpwn/hdmg

e0405e1ff99333d0bc9b9bba5ee72747e8f19138 — Drew DeVault 1 year, 3 months ago 6016958
hdmg: run CPU during update
2 files changed, 20 insertions(+), 1 deletions(-)

M cmd/hdmg/main.ha
M dmg/dmg.ha
M cmd/hdmg/main.ha => cmd/hdmg/main.ha +13 -0
@@ 3,6 3,7 @@ use dmg::cart;
use fmt;
use io;
use os;
use time;
use sdl2;
use sdl2::{
	SDL_EventType,


@@ 10,8 11,12 @@ use sdl2::{
	SDL_WindowFlags
};

def CLOCK_NANOHZ: i64 = dmg::CLOCK_HZ * 1000000;

export type state = struct {
	quit: bool,
	prev: time::instant,
	dmg: *dmg::dmg,
	window: *sdl2::SDL_Window,
	render: *sdl2::SDL_Renderer,
};


@@ 43,6 48,8 @@ export fn main() void = {

	let state = state {
		quit = false,
		dmg = &dmg,
		prev = time::now(time::clock::MONOTONIC),
		window = win,
		render = render,
	};


@@ 63,6 70,12 @@ fn update(state: *state) (void | sdl2::error) = {
	case =>
		yield;
	};

	const now = time::now(time::clock::MONOTONIC);
	const diff = time::diff(state.prev, now);
	const cycles = (CLOCK_NANOHZ / diff): uint;
	dmg::exec(state.dmg, cycles);
	state.prev = now;
};

fn draw(state: *state) (void | sdl2::error) = {

M dmg/dmg.ha => dmg/dmg.ha +7 -1
@@ 2,7 2,7 @@ use dmg::cart;
use sm83;

// Clock rate in Hz of the DMG CPU.
export def CLOCK_HZ: size = 4194304;
export def CLOCK_HZ: i64 = 4194304;

export type dmg = struct {
	cart: *cart::cart,


@@ 24,6 24,12 @@ export fn init(cart: *cart::cart) dmg = {
	};
};

// Frees state associated with a [[dmg]].
export fn finish(dmg: *dmg) void = {
	mmu_free(dmg.mmu);
};

// Executes the [[dmg]] CPU for a given number of cycles.
export fn exec(dmg: *dmg, cycles: uint) void = {
	sm83::exec(&dmg.cpu, cycles);
};