minor cleanup

This commit is contained in:
Will Scullin 2020-11-15 18:51:05 -08:00
parent 14b8e13f79
commit e1b807ba9e
No known key found for this signature in database
GPG Key ID: 9092A5C0A673416B
2 changed files with 33 additions and 32 deletions

View File

@ -30,7 +30,12 @@
} }
], ],
"no-unused-vars": "off", "no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"], "@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
],
"no-redeclare": "off", "no-redeclare": "off",
"@typescript-eslint/no-redeclare": ["error"], "@typescript-eslint/no-redeclare": ["error"],
"no-console": [ "no-console": [

View File

@ -138,20 +138,24 @@ type WriteFn = (val: byte) => void;
type ReadAddrFn = (opts?: Opts) => word; type ReadAddrFn = (opts?: Opts) => word;
type ImpliedFn = () => void type ImpliedFn = () => void
interface Op<T> { interface Instruction<T = any> {
name: string name: string
mode: keyof Modes mode: Mode
op: (fn: T) => void op: (fn: T) => void
modeFn: T modeFn: T
} }
type Instruction = Op<ReadFn | WriteFn | ReadAddrFn | ImpliedFn | number> type StrictInstruction =
Instruction<ReadFn> |
Instruction<WriteFn> |
Instruction<ReadAddrFn> |
Instruction<ImpliedFn> |
Instruction<flag> |
Instruction<byte>
interface Instructions { type Instructions = Record<byte, StrictInstruction>
[key: number]: Instruction;
}
type callback = (cpu: any) => void; // TODO(flan): Hack until there is better typing. type callback = (cpu: CPU6502) => void;
export default class CPU6502 { export default class CPU6502 {
private readonly is65C02; private readonly is65C02;
@ -164,33 +168,29 @@ export default class CPU6502 {
private yr = 0; // Y Register private yr = 0; // Y Register
private sp = 0xff; // Stack Pointer private sp = 0xff; // Stack Pointer
private readPages: ReadablePage[] = []; private readPages: ReadablePage[] = new Array(0x100);
private writePages: WriteablePage[] = []; private writePages: WriteablePage[] = new Array(0x100);
private resetHandlers: ResettablePageHandler[] = []; private resetHandlers: ResettablePageHandler[] = [];
private cycles = 0; private cycles = 0;
private sync = false; private sync = false;
private readonly ops: Instructions;
private readonly opary: Instruction[]; private readonly opary: Instruction[];
constructor(options: CpuOptions = {}) { constructor(options: CpuOptions = {}) {
this.is65C02 = options['65C02'] ? true : false; this.is65C02 = options['65C02'] ? true : false;
for (let idx = 0; idx < 0x100; idx++) { this.readPages.fill(BLANK_PAGE);
this.readPages[idx] = BLANK_PAGE; this.writePages.fill(BLANK_PAGE);
this.writePages[idx] = BLANK_PAGE;
}
// Create this CPU's instruction table // Create this CPU's instruction table
let ops: Instructions = { ...this.OPS_6502 }; let ops = { ...this.OPS_6502 };
if (this.is65C02) { if (this.is65C02) {
ops = { ...ops, ...this.OPS_65C02 }; ops = { ...ops, ...this.OPS_65C02 };
} }
this.ops = ops;
// Certain browsers benefit from using arrays over maps // Certain browsers benefit from using arrays over maps
const opary: Instruction[] = []; const opary: Instruction[] = new Array(0x100);
for (let idx = 0; idx < 0x100; idx++) { for (let idx = 0; idx < 0x100; idx++) {
opary[idx] = ops[idx] || this.unknown(idx); opary[idx] = ops[idx] || this.unknown(idx);
@ -226,9 +226,6 @@ export default class CPU6502 {
* `a - b`. The status register is updated according to the result. * `a - b`. The status register is updated according to the result.
*/ */
private add(a: byte, b: byte, sub: boolean) { private add(a: byte, b: byte, sub: boolean) {
if (sub)
b ^= 0xff;
// KEGS // KEGS
let c, v; let c, v;
if ((this.sr & flags.D) !== 0) { if ((this.sr & flags.D) !== 0) {
@ -377,12 +374,12 @@ export default class CPU6502 {
} }
// $00 // $00
readZeroPage= (): byte => { readZeroPage = (): byte => {
return this.readByte(this.readBytePC()); return this.readByte(this.readBytePC());
} }
// $0000,X // $0000,X
readAbsoluteX= (): byte => { readAbsoluteX = (): byte => {
let addr = this.readWordPC(); let addr = this.readWordPC();
const oldPage = addr >> 8; const oldPage = addr >> 8;
addr = (addr + this.xr) & 0xffff; addr = (addr + this.xr) & 0xffff;
@ -519,7 +516,7 @@ export default class CPU6502 {
} }
// $00 // $00
readAddrZeroPage = (): byte => { readAddrZeroPage = () => {
return this.readBytePC(); return this.readBytePC();
} }
@ -625,7 +622,7 @@ export default class CPU6502 {
/* Subtract with Carry */ /* Subtract with Carry */
sbc = (readFn: ReadFn) => { sbc = (readFn: ReadFn) => {
this.ar = this.add(this.ar, readFn(), /* sub= */ true); this.ar = this.add(this.ar, readFn() ^ 0xff, /* sub= */ true);
} }
/* Increment Memory */ /* Increment Memory */
@ -981,13 +978,13 @@ export default class CPU6502 {
} }
/* No-Op */ /* No-Op */
nop = (readAddrFn: ReadAddrFn) => { nop = (impliedFn: ImpliedFn) => {
this.readByte(this.pc); this.readByte(this.pc);
readAddrFn(); impliedFn();
} }
private unknown(b: byte) { private unknown(b: byte) {
let unk: Instruction; let unk: StrictInstruction;
if (this.is65C02) { if (this.is65C02) {
unk = { unk = {
@ -1000,7 +997,7 @@ export default class CPU6502 {
const cpu = this; const cpu = this;
unk = { unk = {
name: '???', name: '???',
op: function() { op: function(_impliedFn: ImpliedFn) {
debug('Unknown OpCode: ' + toHex(b) + debug('Unknown OpCode: ' + toHex(b) +
' at ' + toHex(cpu.pc - 1, 4)); ' at ' + toHex(cpu.pc - 1, 4));
}, },
@ -1008,7 +1005,6 @@ export default class CPU6502 {
mode: 'implied' mode: 'implied'
}; };
} }
this.ops[b] = unk;
return unk; return unk;
} }
@ -1202,7 +1198,7 @@ export default class CPU6502 {
pc = this.pc; pc = this.pc;
} }
const b = this.readByte(pc), const b = this.readByte(pc),
op = this.ops[b], op = this.opary[b],
size = sizes[op.mode]; size = sizes[op.mode];
let result = toHex(pc, 4) + '- '; let result = toHex(pc, 4) + '- ';
@ -1267,7 +1263,7 @@ export default class CPU6502 {
} }
const results = []; const results = [];
for (let jdx = 0; jdx < 20; jdx++) { for (let jdx = 0; jdx < 20; jdx++) {
const b = this.readByte(_pc), op = this.ops[b]; const b = this.readByte(_pc), op = this.opary[b];
results.push(this.dumpPC(_pc, symbols)); results.push(this.dumpPC(_pc, symbols));
_pc += sizes[op.mode]; _pc += sizes[op.mode];
} }