Skip to content

Commit a788393

Browse files
authored
Merge pull request #40 from danleh/8bitbench-wasm-scoring
Move 8bitbench to first, average, worst scoring
2 parents 4352dab + fbf7c58 commit a788393

File tree

13 files changed

+165
-87
lines changed

13 files changed

+165
-87
lines changed

8bitbench/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/8bitbench/
File renamed without changes.

8bitbench/benchmark.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Simplified from `8bitbench/cli.mjs` and `8bitbench/js/bench.js`.
6+
// See also `8bitbench/rust/src/main.rs` for the native version.
7+
function log(str) { print(str); }
8+
function getInput() { return "t"; }
9+
globalThis.updateVideo = function(vec) { }
10+
11+
const frameWidth = 256;
12+
const frameHeight = 240;
13+
const channels = 4;
14+
15+
// For debugging only: Dump a rendered frame (as a linear vector of pixels) to
16+
// to an ASCII-encoded PPM image file.
17+
function dumpFrame(vec) {
18+
const maxValue = 255;
19+
let ppm = `P3\n${frameWidth} ${frameHeight}\n${maxValue}\n`
20+
for (let y = 0; y < frameHeight; ++y) {
21+
for (let x = 0; x < frameWidth; ++x) {
22+
const r = vec[x*channels + y*frameWidth*channels];
23+
const g = vec[1 + x*channels + y*frameWidth*channels];
24+
const b = vec[2 + x*channels + y*frameWidth*channels];
25+
const alpha = vec[3 + x*channels + y*frameWidth*channels];
26+
ppm += `${r} ${g} ${b}\n`
27+
if (alpha !== maxValue) {
28+
throw new Error("Unexpected alpha channel value: " + alpha);
29+
}
30+
}
31+
}
32+
writeFile('last_frame.ppm', ppm);
33+
}
34+
35+
class Benchmark {
36+
isInstantiated = false;
37+
38+
async runIteration() {
39+
if (!this.isInstantiated) {
40+
await wasm_bindgen(Module.wasmBinary);
41+
this.isInstantiated = true;
42+
}
43+
44+
wasm_bindgen.loadRom(Module.romBinary);
45+
46+
const frameCount = 2 * 60;
47+
for (let i = 0; i < frameCount; ++i) {
48+
wasm_bindgen.js_tick();
49+
}
50+
}
51+
52+
validate() {
53+
globalThis.updateVideo = function(vec) {
54+
if (vec.length != frameWidth * frameHeight * channels) {
55+
throw new Error("Wrong video vec length");
56+
}
57+
// dumpFrame(vec);
58+
59+
let videoSum = 0;
60+
for (let i = 0; i < vec.length; ++i) {
61+
videoSum += vec[i]
62+
}
63+
if (videoSum != 40792276) {
64+
throw new Error("Wrong video sum, the picture is wrong: " + videoSum);
65+
}
66+
}
67+
68+
wasm_bindgen.js_tick();
69+
}
70+
}

8bitbench/build.log

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Built on 2025-01-28T13:57:51Z
2+
Toolchain versions
3+
rustc 1.81.0 (eeb90cda1 2024-09-04)
4+
cargo 1.81.0 (2dbb1af80 2024-08-20)
5+
wasm-pack 0.13.1
6+
Getting sources...
7+
Cloning into '8bitbench'...
8+
Updating files: 98% (4618/4667)Updating files: 99% (4621/4667)Updating files: 100% (4667/4667)Updating files: 100% (4667/4667), done.
9+
Building...
10+
Copying files from 8bitbench/ into build/
11+
Build success

8bitbench/build.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
set -eo pipefail
4+
5+
# Cleanup old files.
6+
rm -rf 8bitbench/
7+
rm -rf build/
8+
9+
touch build.log
10+
BUILD_LOG="$(realpath build.log)"
11+
12+
echo -e "Built on $(date -u '+%Y-%m-%dT%H:%M:%SZ')" | tee "$BUILD_LOG"
13+
14+
echo "Toolchain versions" | tee -a "$BUILD_LOG"
15+
rustc --version | tee -a "$BUILD_LOG"
16+
cargo --version | tee -a "$BUILD_LOG"
17+
wasm-pack --version | tee -a "$BUILD_LOG"
18+
19+
echo -e "Getting sources..." | tee -a "$BUILD_LOG"
20+
git clone https://github.com/justinmichaud/8bitbench.git |& tee -a "$BUILD_LOG"
21+
22+
echo "Building..." | tee -a "$BUILD_LOG"
23+
pushd 8bitbench/
24+
pushd rust/
25+
# Emulator itself.
26+
# NOTE: The 8bitbench README uses `--target web`, but that produces an ES6
27+
# module, so we use the `no-modules` here (which produces almost the same code).
28+
wasm-pack build --target no-modules --release
29+
popd
30+
pushd assets/
31+
pushd cc65/
32+
# Compiler, assembler, linker etc. for the program ROM.
33+
make all
34+
popd
35+
pushd tutorial
36+
# Program ROM to run on the emulator.
37+
make
38+
popd
39+
popd
40+
popd
41+
42+
echo "Copying files from 8bitbench/ into build/" | tee -a "$BUILD_LOG"
43+
mkdir -p build/{lib/fast-text-encoding-1.0.3/,rust/pkg/,assets/}
44+
cp 8bitbench/lib/fast-text-encoding-1.0.3/text.js build/lib/fast-text-encoding-1.0.3/
45+
cp 8bitbench/rust/pkg/{emu_bench.js,emu_bench_bg.wasm} build/rust/pkg
46+
cp 8bitbench/assets/tutorial/full_game.bin build/assets/program.bin
47+
48+
echo "Build success" | tee -a "$BUILD_LOG"
File renamed without changes.

8bitbench/rust/pkg/emu_bench.js renamed to 8bitbench/build/rust/pkg/emu_bench.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
let wasm_bindgen;
2+
(function() {
3+
const __exports = {};
4+
let wasm;
15

2-
let wasm;
6+
const heap = new Array(32).fill(undefined);
37

4-
const heap = new Array(32).fill(undefined);
5-
6-
heap.push(undefined, null, true, false);
8+
heap.push(undefined, null, true, false);
79

810
function getObject(idx) { return heap[idx]; }
911

@@ -38,22 +40,6 @@ function getStringFromWasm0(ptr, len) {
3840
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
3941
}
4042

41-
function logError(f, args) {
42-
try {
43-
return f.apply(this, args);
44-
} catch (e) {
45-
let error = (function () {
46-
try {
47-
return e instanceof Error ? `${e.message}\n\nStack:\n${e.stack}` : e.toString();
48-
} catch(_) {
49-
return "<failed to stringify thrown value>";
50-
}
51-
}());
52-
console.error("wasm-bindgen: imported JS function that was not marked as `catch` threw an error:", error);
53-
throw e;
54-
}
55-
}
56-
5743
function addHeapObject(obj) {
5844
if (heap_next === heap.length) heap.push(heap.length + 1);
5945
const idx = heap_next;
@@ -132,9 +118,9 @@ function getArrayU8FromWasm0(ptr, len) {
132118
}
133119
/**
134120
*/
135-
function main() {
121+
__exports.main = function() {
136122
wasm.main();
137-
}
123+
};
138124

139125
function passArray8ToWasm0(arg, malloc) {
140126
const ptr = malloc(arg.length * 1);
@@ -145,17 +131,17 @@ function passArray8ToWasm0(arg, malloc) {
145131
/**
146132
* @param {Uint8Array} file
147133
*/
148-
function loadRom(file) {
134+
__exports.loadRom = function(file) {
149135
const ptr0 = passArray8ToWasm0(file, wasm.__wbindgen_malloc);
150136
const len0 = WASM_VECTOR_LEN;
151137
wasm.loadRom(ptr0, len0);
152-
}
138+
};
153139

154140
/**
155141
*/
156-
function js_tick() {
142+
__exports.js_tick = function() {
157143
wasm.js_tick();
158-
}
144+
};
159145

160146
async function load(module, imports) {
161147
if (typeof Response === 'function' && module instanceof Response) {
@@ -228,14 +214,12 @@ function getImports() {
228214
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
229215
throw new Error(getStringFromWasm0(arg0, arg1));
230216
};
231-
imports.wbg.__wbindgen_rethrow = function(arg0) {
232-
throw takeObject(arg0);
233-
};
234217

235218
return imports;
236219
}
237220

238221
function initMemory(imports, maybe_memory) {
222+
239223
}
240224

241225
function finalizeInit(instance, module) {
@@ -263,6 +247,15 @@ function initSync(module) {
263247
}
264248

265249
async function init(input) {
250+
if (typeof input === 'undefined') {
251+
let src;
252+
if (typeof document === 'undefined') {
253+
src = location.href;
254+
} else {
255+
src = document.currentScript.src;
256+
}
257+
input = src.replace(/\.js$/, '_bg.wasm');
258+
}
266259
const imports = getImports();
267260

268261
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
@@ -274,4 +267,8 @@ async function init(input) {
274267
const { instance, module } = await load(await input, imports);
275268

276269
return finalizeInit(instance, module);
277-
}
270+
}
271+
272+
wasm_bindgen = Object.assign(init, { initSync }, __exports);
273+
274+
})();
60.4 KB
Binary file not shown.

8bitbench/js3harness.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)