Skip to content

Commit 8353bf4

Browse files
authored
Merge pull request #61 from camillobruni/2025-03-26_gbemu_hash
gbemu: Add basic canvas pixel hashing Make sure we leak a hash of all "drawn" pixels from the canvas. Currently there isn't a proper side-effect and the whole canvas code could be DCE'd which is rather unrealistic.
2 parents ed6dfc1 + 7a1eab1 commit 8353bf4

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Octane/gbemu-part1.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ function runGameboy() {
3939
GameBoyAudioNode.run();
4040
}
4141

42+
var resultHash = gameboy.canvas.resultHash;
43+
4244
resetGlobalVariables();
45+
46+
return resultHash;
4347
}
4448

4549
function tearDownGameboy() {
@@ -56,7 +60,7 @@ var expectedGameboyStateStr =
5660

5761
var GameBoyWindow = { };
5862

59-
function GameBoyContext() {
63+
function GameBoyContext(canvas) {
6064
this.createBuffer = function() {
6165
return new Buffer();
6266
}
@@ -69,20 +73,21 @@ function GameBoyContext() {
6973
this.putImageData = function (buffer, x, y) {
7074
var sum = 0;
7175
for (var i = 0; i < buffer.data.length; i++) {
72-
sum += i * buffer.data[i];
73-
sum = sum % 1000;
76+
sum ^= (i * buffer.data[i]) | 0;
7477
}
78+
canvas.resultHash ^= sum;
7579
}
7680
this.drawImage = function () { }
7781
};
7882

7983
function GameBoyCanvas() {
8084
this.getContext = function() {
81-
return new GameBoyContext();
85+
return new GameBoyContext(this);
8286
}
8387
this.width = 160;
8488
this.height = 144;
8589
this.style = { visibility: "visibile" };
90+
this.resultHash = 0x1a2b3c4f;
8691
}
8792

8893
function cout(message, colorIndex) {

Octane/gbemu-part2.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9782,7 +9782,15 @@ var gameboy_rom='r+BPyZiEZwA+AeBPySAobeEq6gAgKlYj5WJv6SRmZjjhKuXqACDJ///////////
97829782
setupGameboy();
97839783

97849784
class Benchmark {
9785+
EXPECTED_RESULT_HASH = 439041103;
9786+
97859787
runIteration() {
9786-
runGameboy();
9788+
this.result = runGameboy();
9789+
}
9790+
9791+
validate() {
9792+
if (this.result != this.EXPECTED_RESULT_HASH)
9793+
throw new Error(`Got unexpected result hash ${this.result} instead of ${this.EXPECTED_RESULT_HASH}`)
9794+
97879795
}
97889796
}

0 commit comments

Comments
 (0)