Skip to content

Commit bb13a4a

Browse files
committed
Add test harness for running Klaus's functional test
1 parent cec1539 commit bb13a4a

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

Processor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ export class Processor {
5858
this.reset();
5959
}
6060

61+
public setPC(pc: number) {
62+
this.bigRegisters[PC] = pc;
63+
}
64+
65+
public getPC() {
66+
return this.bigRegisters[PC];
67+
}
68+
6169
public reset() {
6270
// set the PC to the reset vector
6371
this.registers = new Uint8Array(5);

test/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You will need 6502_functional_test.bin taken from (or compiled from) Klaus's test suite: https://github.com/Klaus2m5/6502_65C02_functional_tests/tree/master

test/TestRam.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Addressable } from "../Addressable";
2+
3+
export class TestRam implements Addressable {
4+
private data: Uint8Array;
5+
6+
constructor(data: Uint8Array) {
7+
this.data = data;
8+
}
9+
10+
public read(location: number): number {
11+
return this.data[location];
12+
}
13+
14+
public write(location: number, data: number) {
15+
this.data[location] = data;
16+
}
17+
}

test/test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Processor } from "../Processor";
2+
import { readFileSync } from "fs";
3+
import { TestRam } from "./TestRam";
4+
5+
const ramData = readFileSync("./6502_functional_test.bin");
6+
const ram = new TestRam(ramData);
7+
8+
const processor = new Processor(ram);
9+
processor.setPC(0x0400);
10+
11+
const startTime = Date.now();
12+
let cyclesSinceStart = 0;
13+
let hz = 1000000;
14+
const interval = setInterval(() => {
15+
const secondsElapsed = (Date.now() - startTime) / 1000;
16+
const totalExpectedCycles = secondsElapsed * hz;
17+
while (cyclesSinceStart < totalExpectedCycles) {
18+
const pcBefore = processor.getPC();
19+
cyclesSinceStart += processor.tick(true);
20+
if (processor.getPC() === pcBefore) {
21+
console.log("TRAPPED");
22+
const stack = ramData.subarray(0x100, 0x200);
23+
console.log(stack.toString("hex").match(/../g)?.join(" "));
24+
process.exit();
25+
}
26+
}
27+
}, 1);
28+
29+
process.on("beforeExit", () => {});

0 commit comments

Comments
 (0)