Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add performance tests for 6502 implementations #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions bin/bin2js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node

const { exit } = require('process');

const readFile = require('fs').promises.readFile;
const argv = require('yargs').argv;

const fileName = argv._[0];
const name = argv.n || argv.name;
const start = argv.s !== undefined ? argv.s : argv.start;

function toHex(v, n) {
if (!n) {
n = 2
}
let r = v.toString(16);
r = r.padStart(n, '0');
return r;
}

function usage(message) {
if (message) {
console.error(message);
}
console.error("bin2js -n name -s start binfile");
}

if (argv.h || argv.help) {
usage();
process.exit(0);
}
if (!name || !fileName || (typeof name != "string")) {
usage("must specify a file name for binfile");
process.exit(1);
}

if (start === "" || start === undefined) {
usage("must specify a start page");
process.exit(2);
}

if (typeof start === "string") {
if (start.startsWith("0x")) {
start = parseInt(start.slice(2), 16);
} else {
start = parseInt(start, 10);
}
}

readFile(fileName, { flag: 'r' }).then((fileData => {
if (fileData.length % 256 != 0) {
console.error(`${filename} length is not a multiple of 256`);
process.exit(2);
}
let end = start + fileData.length / 256 - 1;
console.log('const MEMORY = [');
const step = 0x08;
for (let i = 0; i < fileData.length; i += step) {
let line = ' ';
for (let j = i; j < fileData.length && j < i + step; j++) {
line += '0x' + toHex(fileData[j]) + ', ';
}
line += '// ' + toHex(i, 4);
console.log(line);
}
console.log('];');
console.log();
console.log(`export default function ${name}() {`);
console.log(' let mem = [...MEMORY];');
console.log(' return {');
console.log(' start: function() {');
console.log(` return 0x${toHex(start)};`);
console.log(' },');
console.log(' end: function() {');
console.log(` return 0x${toHex(end)};`);
console.log(' },');
console.log(' read: function(page, off) {');
console.log(` return mem[(page - 0x${toHex(start)}) << 8 | off];`);
console.log(' },');
console.log(' write: function(page, off, val) {');
console.log(` mem[(page - 0x${toHex(start)}) << 8 | off] = val;`);
console.log(' },');
console.log(' reset: function() {');
console.log(' mem = [...MEMORY];');
console.log(' },');
console.log(' };');
console.log('}');
})).catch((reason) => {
console.error('Unable to read binary file:', reason);
process.exit(1);
});
80 changes: 80 additions & 0 deletions test/perf/cpu_benchmark.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html>

<head>
<title>Benchmark for 6502</title>
<script async="false" type="module" src="/dist/cpu_benchmark.js"></script>
<script>
window.addEventListener('load', async (e) => {
await window.benchmark();
console.log('done');
});
</script>
<style>
table {
border-collapse: collapse;
empty-cells: hide;
}

tr.bottom-header {
border-bottom: 2px solid black;
}

th.right-header {
border-right: 2px solid black;
border-top: 1px solid gray;
}

td {
text-align: right;
border-top: 1px solid gray;
padding: 4px 10px;
}

</style>
</head>

<body>
<h1>Benchmark for 6502</h1>

<table id="benchmarks">
<thead>
<tr id="impl">
<th></th>
<th colspan="2">cpu6502.js</th>
</tr>
<tr class="bottom-header" id="emul">
<th></th>
<th>6502</th>
<th>65C02</th>
</tr>
</thead>
<tr id="min">
<th class="right-header">min</th>
<td id="min_js_6502"></td>
<td id="min_js_65C02"></td>
</tr>
<tr id="max">
<th class="right-header">max</th>
<td id="max_js_6502"></td>
<td id="max_js_65C02"></td>
</tr>
<tr id="mean">
<th class="right-header">mean</th>
<td id="mean_js_6502"></td>
<td id="mean_js_65C02"></td>
</tr>
<tr id="median">
<th class="right-header">median</th>
<td id="median_js_6502"></td>
<td id="median_js_65C02"></td>
</tr>
<tr id="runs">
<th class="right-header">runs</th>
<td id="runs_js_6502"></td>
<td id="runs_js_65C02"></td>
</tr>
</table>
</body>

</html>
Loading