Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
on:
workflow_dispatch:
schedule:
- cron: '0 10 * * 2'
pull_request:
Expand Down Expand Up @@ -32,5 +33,12 @@ jobs:

- name: moon build
run: |
moon build --target wasm-gc
moon build --target js
moon build --target wasm-gc --release
moon build --target js --release

- uses: actions/setup-node@v4
with:
node-version: 22

- name: Verify CPU state trace
run: node scripts/check-nestest.mjs
3 changes: 0 additions & 3 deletions lib/uint.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
///|
type UInt8 = Byte

///|
type UInt32 = UInt

///|
trait U8 {
u8(Self) -> UInt8
Expand Down
4 changes: 2 additions & 2 deletions main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ document.addEventListener('keydown', (event) => { nes_keyboard(buttonDown, event
document.addEventListener('keyup', (event) => { nes_keyboard(buttonUp, event) });

if (backend == "wasm-gc" || backend == "wasm") {
const wasm_binary = `target/${backend}/release/build/lib/lib.wasm`;
const wasm_binary = `_build/${backend}/release/build/lib/lib.wasm`;
const module = await WebAssembly.compileStreaming(fetch(wasm_binary));

if (do_nestest) {
Expand Down Expand Up @@ -304,7 +304,7 @@ if (backend == "wasm-gc" || backend == "wasm") {
});
}
} else {
const { nestest, load_rom, run, buttonDown, buttonUp, run_with_logging, set_image_data, set_callback } = await import("./target/js/release/build/lib/lib.js");
const { nestest, load_rom, run, buttonDown, buttonUp, run_with_logging, set_image_data, set_callback } = await import("./_build/js/release/build/lib/lib.js");
image = new ImageData(new Uint8ClampedArray(SCREEN_WIDTH * SCREEN_HEIGHT * 4), SCREEN_WIDTH, SCREEN_HEIGHT);
set_image_data(image.data)
set_callback(nes_write_audio_sample)
Expand Down
13 changes: 13 additions & 0 deletions moon.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "moonbit-community/MoonbitNES"

version = "0.1.0"

readme = "README.md"

repository = "https://github.com/moonbit-community/MoonbitNES"

license = "MIT"

keywords = [ "Game" ]

description = "A NES emulator"
9 changes: 0 additions & 9 deletions moon.mod.json

This file was deleted.

60 changes: 60 additions & 0 deletions scripts/check-nestest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { nestest } from "../_build/js/release/build/lib/lib.js";

// Compare every instruction's address, CPU registers and PPU/CPU cycle counters.
// Disassembly text is intentionally separate: the emulator calls ISB "ISC",
// and its existing APU register reads differ in the final instructions.
const trace = [];
const log = console.log;
try {
console.log = (line) => {
if (/^[0-9A-F]{4} /.test(line)) trace.push(line);
};
nestest(Array.from(readFileSync(new URL("../nestest/nestest.nes", import.meta.url))));
} finally {
console.log = log;
}
const expected = readFileSync(new URL("../nestest/nestest.log", import.meta.url), "utf8")
.trimEnd().split(/\r?\n/);
assert.equal(trace.length, 8991);
assert.equal(trace.length, expected.length);
const state = (line) => {
const match = line.match(/^([0-9A-F]{4}) .*?(A:[0-9A-F]{2} X:.*)$/);
assert.ok(match, `Invalid trace line: ${line}`);
return `${match[1]} ${match[2]}`;
};
for (let i = 0; i < expected.length; i++) {
assert.equal(state(trace[i]), state(expected[i]), `CPU state at instruction ${i + 1}`);
}
console.log(`Verified ${trace.length} CPU states and cycle counters against nestest.log.`);

// Exercise the same exported API and linear-memory transfer as the browser UI.
const wasmTrace = [];
let line = "";
const wasm = new WebAssembly.Module(readFileSync(
new URL("../_build/wasm-gc/release/build/lib/lib.wasm", import.meta.url),
));
const instance = new WebAssembly.Instance(wasm, {
nes: { write_audio_sample() {} },
spectest: { print_char(ch) {
if (ch === 10) {
if (/^[0-9A-F]{4} /.test(line)) wasmTrace.push(line);
line = "";
} else if (ch !== 13) {
line += String.fromCharCode(ch);
}
} },
});
const rom = readFileSync(new URL("../nestest/nestest.nes", import.meta.url));
const memory = instance.exports["moonbit.memory"];
if (memory.buffer.byteLength < rom.length) {
memory.grow(Math.ceil((rom.length - memory.buffer.byteLength) / 65536));
}
new Uint8Array(memory.buffer, 0, rom.length).set(rom);
instance.exports.nestest(rom.length);
assert.equal(wasmTrace.length, expected.length);
for (let i = 0; i < expected.length; i++) {
assert.equal(state(wasmTrace[i]), state(expected[i]), `Wasm GC CPU state at instruction ${i + 1}`);
}
console.log(`Verified ${wasmTrace.length} Wasm GC CPU states and cycle counters.`);
Loading