Skip to content

Commit

Permalink
Some Emscripten-based improvements
Browse files Browse the repository at this point in the history
Reports emulation speed and allows loading of savestates
  • Loading branch information
nepx committed Aug 28, 2020
1 parent 9a6e15f commit 5163d66
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
29 changes: 22 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ <h5 class="mb-0">
</div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
<div class="text-center">
<span><span id="speed">0</span> MIPS</span>
</div>
<div class="text-center">
<canvas id="canvas" height="480" width="640">Your browser does not support canvas</canvas>
</div>
Expand Down Expand Up @@ -314,7 +317,7 @@ <h5 class="modal-title">Other Settings</h5>
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

var halfix;
var halfix, savestate = null;

function pause() {
$("#btn_pause").attr("disabled", "disabled").blur();
Expand All @@ -329,7 +332,12 @@ <h5 class="modal-title">Other Settings</h5>
halfix = new Halfix(options);

halfix.init(function() {
halfix.run();
if (savestate)
halfix.loadStateXHR(savestate, function() {
halfix.run();
});
else
halfix.run();
});
} else {
halfix.pause(false);
Expand Down Expand Up @@ -362,13 +370,16 @@ <h5 class="modal-title">Other Settings</h5>
if (obj2) {
opts[str] = obj2;
}
}else{
if(getParameterByName("hd" + letters[i]) || getParameterByName("cd" + letters[i])){
} else {
if (getParameterByName("hd" + letters[i]) || getParameterByName("cd" + letters[i])) {
var str = getParameterByName("hd" + letters[i]) ? "hd" + letters[i] : "cd" + letters[i];
opts[str] = getParameterByName(str);
}
}
}

savestate = getParameterByName("savestate");

opts.canvas = document.getElementById("canvas");
}

Expand Down Expand Up @@ -458,7 +469,12 @@ <h5 class="modal-title">Other Settings</h5>
}
});

var opts = {};
var speed = document.getElementById("speed");
var opts = {
reportSpeed: function(s) {
speed.innerHTML = s;
}
};
$("#btn_start").on("click", function() {
if (opts) {
mkConfig(opts);
Expand All @@ -477,8 +493,7 @@ <h5 class="modal-title">Other Settings</h5>
});

if (getParameterByName("autostart")) {
var opts = {};
mkConfig(opts);
mkConfig(opts);
run(opts);
}
});
Expand Down
74 changes: 71 additions & 3 deletions libhalfix.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

this.fast = options["fast"] || false;

this.reportSpeed = options["reportSpeed"] || function (n) { };
console.log(options.reportSpeed);

this.paused = false;

/** @type {ImageData} */
Expand Down Expand Up @@ -288,20 +291,52 @@
document.head.appendChild(script);
};

var savestate_files = {};
function u8tostr(u) {
var str = "";
for (var i = 0; i < u.length; i = i + 1 | 0)str += String.fromCharCode(u[i]);
return str;
}
/**
* Load savestate from directory
* @param {string} statepath
* @param {function} cb
*/
Halfix.prototype["loadStateXHR"] = function (statepath, cb) {
loadFiles([
statepath + "/state.bin",
statepath + "/ram",
statepath + "/vram",
statepath + "/diskinfo.json"], function (err, data) {
if (err) throw err;
savestate_files["/state.bin"] = data[0];
savestate_files["/ram"] = data[1];
savestate_files["/vram"] = data[2];
savestate_files["/diskinfo.json"] = JSON.parse(u8tostr(data[3]));

wrap("emscripten_load_state")();

delete data[3]; // try to get this gc'ed

cb();
}, true);
};

/**
* Pause the emulator
* @param {boolean} paused
*/
Halfix.prototype["pause"] = function(paused){
Halfix.prototype["pause"] = function (paused) {
this.paused = paused;
};

/**
* Send a fullscreen request to the brower.
*/
Halfix.prototype["fullscreen"] = function(){
Halfix.prototype["fullscreen"] = function () {
Module["requestFullscreen"]();
};
var cyclebase = 0;
Halfix.prototype["run"] = function () {
if (this.paused) return;
try {
Expand All @@ -310,6 +345,8 @@
var elapsed = (temp = new Date().getTime()) - now;
if (elapsed >= 1000) {
var curcycles = cycles();
this.reportSpeed(((curcycles - cyclebase) / (elapsed) / (1000)).toFixed(2));
console.log(((curcycles - cyclebase) / (elapsed) / (1000)).toFixed(2));
//$("speed").innerHTML = ((curcycles - cyclebase) / (elapsed) / (1000)).toFixed(2);
cyclebase = curcycles;
now = temp;
Expand Down Expand Up @@ -338,7 +375,7 @@
now = new Date().getTime();
cycles = wrap("emscripten_get_cycles");
run = wrap("emscripten_run");

wrap("emscripten_set_fast")(_halfix.fast);
init_cb();
}
Expand Down Expand Up @@ -856,6 +893,37 @@
return a + b; //normalize_path(a + b);
}

// Some more savestate-related functions
/**
* Load file from file cache
* @param {number} pathstr Pointer to path string
* @param {number} addr Address to load the data
*/
window["loadFile"] = function (pathstr, addr) {
var path = readstr(pathstr);
var data = savestate_files[path];
if (!data) throw new Error("ENOENT: " + path);
memcpy(addr, data);
return addr;
};
/**
* Load file from file cache and allocate a buffer to store it in.
* It is the responsibility of the caller to free the memory.
* @param {number} pathstr Pointer to path string
* @param {number} addr Address to load the data
*/
window["loadFile2"] = function (pathstr, addr) {
var path = readstr(pathstr);
var data = savestate_files[path];
if (!data) throw new Error("ENOENT: " + path);
var len = data.length;
var addr = alloc(len);
_allocs.pop();
memcpy(addr, data);
console.log(path, data, addr);
return addr;
};

if (typeof module !== "undefined" && module["exports"])
module["exports"] = Halfix;
else
Expand Down

0 comments on commit 5163d66

Please sign in to comment.