Skip to content

Commit

Permalink
before new UI
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRainbowPhoenix committed Aug 17, 2024
1 parent 139bd7e commit 57790ec
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if(EMSCRIPTEN)
add_executable(calcemu ${SOURCES})
# -fsanitize=undefined on both
target_compile_options(calcemu PRIVATE -Wall -Wextra -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -O3 -flto -g -sSTACK_SIZE=1048576 -Dmain=main -sEXPORTED_FUNCTIONS=_main,_runFrame,_startInterpreter,_setTrace,_free,_malloc,_runIterationsCPU,_runOneFrame,_dumpOneFrame,_updateTimers,_updateDisplayFromFramebuffer -sEXPORTED_RUNTIME_METHODS=ccall,cwrap,run,UTF8ToString -sEXTRA_EXPORTED_RUNTIME_METHODS=["FS","FS_createDataFile","FS_readFile","FS_unlink"] -sFORCE_FILESYSTEM=1)
target_link_options(calcemu PRIVATE --shell-file=../src/gui/html5/gui.html -g -sSTACK_SIZE=1048576 -s TOTAL_MEMORY=48496640 -sALLOW_MEMORY_GROWTH -sEXPORTED_FUNCTIONS=_setKeydown,_main -sEXPORTED_RUNTIME_METHODS=ccall,cwrap --preload-file=Addin.g3a --preload-file=ram8c.bin --preload-file=rom.bin --preload-file=rs.bin -sEXTRA_EXPORTED_RUNTIME_METHODS=["FS","FS_createDataFile","FS_readFile","FS_unlink"] -sFORCE_FILESYSTEM=1)
target_link_options(calcemu PRIVATE --shell-file=../src/gui/html5/gui.html -g -sSTACK_SIZE=1048576 -s TOTAL_MEMORY=48496640 -sALLOW_MEMORY_GROWTH -sEXPORTED_FUNCTIONS=_setKeydown,_main -sEXPORTED_RUNTIME_METHODS=ccall,cwrap --preload-file=Addin.g3a --preload-file=ram8c.bin --preload-file=rom.bin --preload-file=rs.bin -sNO_EXIT_RUNTIME=1 -sEXTRA_EXPORTED_RUNTIME_METHODS=["FS","FS_createDataFile","FS_readFile","FS_unlink"] -sFORCE_FILESYSTEM=1)
elseif(USE_SDL_GUI)
set(SOURCES ${SOURCES}
src/gui/sdl/gui.c
Expand Down
129 changes: 111 additions & 18 deletions ebuild/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let auto_mode = true;
let loaded_filename = false;

/* Global helpers */

Expand All @@ -19,15 +20,51 @@ function generateTemporaryName() {
return btoa(String.fromCharCode.apply(null, array)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}

function runNow() {
if (!auto_mode) {
// Call this to resume :
// Module.ccall('runFrame', null, [], []);
for (let it=0; it<10; it++) {
Module.ccall('dumpOneFrame', null, [], [])
Module.ccall('runOneFrame', null, ['number'], [1])
}
// Module.ccall('runOneFrame', null, ['number'], [1])
function do_bsod(error) {
console.error(error);

const canvas = document.getElementById('canvas');

let existingBsod = document.getElementById('bsod');
if (existingBsod) {
existingBsod.remove();
}

const bsod = document.createElement('div');
bsod.id = 'bsod';

// Set the size and position of the BSOD div to match the canvas
bsod.style.position = 'absolute';
bsod.style.width = canvas.width + 'px';
bsod.style.height = canvas.height + 'px';
bsod.style.left = canvas.offsetLeft + 'px';
bsod.style.top = canvas.offsetTop + 'px';
bsod.style.padding = '24px';
bsod.style.boxSizing = 'border-box';

bsod.style.backgroundColor = '#0000AA';
bsod.style.color = 'white';
bsod.style.fontFamily = '"Lucida Console", monospace';
bsod.style.whiteSpace = 'pre-wrap';
bsod.style.overflowWrap = 'break-word';

// Set the error message
bsod.innerText = `${error.name}\n\n${error.message}`;

// Show the BSOD div
bsod.style.display = 'block';
canvas.parentNode.insertBefore(bsod, canvas.nextSibling);
}

function debug_step() {
try {
Module.ccall('dumpOneFrame', null, [], [])
Module.ccall('runOneFrame', null, ['number'], [1])

return true;
} catch (error) {
do_bsod(error);
return false;
}
}

Expand All @@ -54,18 +91,71 @@ document.getElementById("btn_saveScreen").addEventListener("click", function() {
link.download = 'ClassPad_Screen.png';
link.click();
})

document.getElementById("btn_debug").addEventListener("click", function() {
if (!trace_mode) {
trace_mode = 1;
Module.ccall('setTrace', null, ['number'], [trace_mode]);
}

if (loaded_filename) {
let existingBsod = document.getElementById('bsod');
if (existingBsod) {
existingBsod.remove();
}

Module.ccall('startInterpreter', null, ["string"], [loaded_filename]);
loaded_filename = null;

for (let it=0; it<10; it++) {
if(!debug_step()) break;
}
}

console.log("TODO: btn_debug")
})

let trace_mode = 0;

document.getElementById("btn_dump").addEventListener("click", function() {
trace_mode = (!trace_mode & 1);

Module.ccall('setTrace', null, ['number'], [trace_mode]);
console.log("TODO: btn_dump")
})

document.getElementById("btn_run").addEventListener("click", function() {
if (loaded_filename) {
let existingBsod = document.getElementById('bsod');
if (existingBsod) {
existingBsod.remove();
}

try {
Module.ccall('startInterpreter', null, ["string"], [loaded_filename]);
} catch (error) {
do_bsod(error);
}
loaded_filename = null;
}
console.log("TODO: btn_run")
})

document.addEventListener('cpu:dump', function(event) {
console.log(`cpu:dump: INSTR ${event.detail.instr.toString(2).padStart(16, '0')} \t - PC: 0x${event.detail.pc.toString(16).padStart(8, '0')}`);
console.log(`cpu:dump: INSTR ${event.detail.instr.toString(16).padStart(4, '0')} \t - PC: 0x${(event.detail.pc >>> 0).toString(16).padStart(8, '0')}`);
console.log(event.detail.regs.map((r) => (r >>> 0).toString(16).toUpperCase()))
});

document.addEventListener('cpu:crash', function(event) {
console.log(event);

let name = event.detail.name
let address = (event.detail.address >>> 0).toString(16).padStart(8, '0')
let pc = (event.detail.pc >>> 0).toString(16).padStart(8, '0')
do_bsod({
name: name,
message: `@ 0x${address} \t\n PC:${pc}\n`
})
console.log(event.detail.regs.map((r) => (r >>> 0).toString(16).toUpperCase()))
});

Expand All @@ -76,20 +166,20 @@ document.addEventListener('emu:main', function(event) {

console.log('emu:main recv:', event.detail.state);
// TODO: if Auto mode
if (!auto_mode) {
Module.ccall('setTrace', null, ['number'], [1]);
}
// if (!auto_mode) {
// Module.ccall('setTrace', null, ['number'], [1]);
// }

return;
// TODO: this is managed by using the "pick a file" button

let addinName = 'MyAddin-fx.g3a' // "SampleAddin.g3a" / ...
loadResourceIntoFS(`/${addinName}`, addinName).then(() => {
console.log("File loaded !")
console.log("File loaded_filename !")

Module.ccall('startInterpreter', null, ["string"], [addinName]);
// TOOD: else, manually load and run
runNow();
// runNow();
})

});
Expand All @@ -112,7 +202,7 @@ function loadFileIntoFS(file, fileName, callback) {
Module.FS.unlink(fileName);
}
Module.FS.createDataFile("/", fileName, data, true, true);
console.log(`${fileName} loaded into the Emscripten filesystem.`);
console.log(`${fileName} loaded_filename into the Emscripten filesystem.`);
callback(null);
};
reader.onerror = function(error) {
Expand Down Expand Up @@ -150,9 +240,12 @@ async function loadResourceIntoFS(filePath, fileName) {
document.getElementById('fileInput').addEventListener('change', function(event) {
var file = event.target.files[0];
if (file) {
loaded_filename = null;
loadFileIntoFSPromise(file, file.name).then(() => {
Module.ccall('startInterpreter', null, ["string"], [file.name]);
runNow();
console.log(file.name)
loaded_filename = file.name;
// Module.ccall('startInterpreter', null, ["string"], [file.name]);
// runNow();
})
.catch(error => {
console.error('Error loading file:', error);
Expand Down
4 changes: 4 additions & 0 deletions src/gui/html5/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@ void runMainLoop(void (*callback)(void)) {
} else {
// if called in "run" :
emscripten_set_main_loop(callback, FPS, false);

// fall off main: either keep running or exit with code.
// emscripten_exit_with_live_runtime();
// emscripten_force_exit(0);
}
}
8 changes: 7 additions & 1 deletion src/interpreter.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
// #include <unistd.h>

#ifdef EMSCRIPTEN
Expand Down Expand Up @@ -64,7 +65,12 @@ int startInterpreter(const char* filename) {
cpu.branchDelayDone = false;
cpu.branchTarget = 0;

cpu.reg.PC = 0x00300000;
const char *ext = strrchr(filename, '.');
if (ext && strcmp(ext, ".bin") == 0) {
cpu.reg.PC = 0x8CFE6000;
} else {
cpu.reg.PC = 0x00300000;
}
// cpu.reg.PC = 0xA0000000;

// Return address
Expand Down
42 changes: 40 additions & 2 deletions src/memory/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <string.h>
#include <endian.h>

#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif

#include "../int.h"
#include "memory.h"
#include "specialAddrs.h"
Expand Down Expand Up @@ -70,9 +74,32 @@ void initMemory(const char* filename) {
// Allocate a memory area at 0x80000000 (ROM)
int romSize = (1024 * 1024 * (32 + 2));
int g3aOffset = (1024 * 1024 * 32);
int binOffset = 0x8CFF0000;
// hhk system is 0x80000000 to 0x81500000
void* rom = allocMemArea(0x80000000, 0x80000000 + romSize);
// Load g3a
loadFile(filename, rom + g3aOffset, 0x7000);
// hhk bin are often loaded at 0x8CFF0000 but doom is at 0x8CFE2000
void* hhk_ram = allocMemArea(0x8CFE2000, 0x8D000000);


const char *ext = strrchr(filename, '.');
if (ext && strcmp(ext, ".bin") == 0) {
// Load .bin file
printf("Loading bin file ...\n");

// TODO: dynamic entry point
long entry_point = 0x8cfe6000;
loadFile(filename, hhk_ram + (entry_point - 0x8CFE2000), 0);
// EntryPoint entryPoint = RunApp(g_numApps - 1);
// if (entryPoint) {
// entryPoint();
// }
} else {
// Load other file types, such as g3a
loadFile(filename, rom + g3aOffset, 0x7000);
}



// Alias at 0xa0000000 (P2)
createAlias(0x80000000, 0x80000000 + romSize, 0xa0000000);
// Set up an alias at 0x00300000 in virtual memory to the file
Expand Down Expand Up @@ -148,6 +175,17 @@ u32 readMemory(u32 address, u32 size) {
if (address % size != 0) {
printf("Unaligned memory read at %08x, PC = %08x\n", address, cpu.reg.PC);
// *((volatile u32* ) 1);

#ifdef EMSCRIPTEN
EM_ASM_({
let event = new CustomEvent('cpu:crash', {
detail: { name: 'UNALIGNED_MEMORY_READ', address: $0, pc: $1 }
});
document.dispatchEvent(event);
}, address, cpu.reg.PC);
emscripten_cancel_main_loop();
#endif

exit(1);
}

Expand Down

0 comments on commit 57790ec

Please sign in to comment.