Skip to content

Commit

Permalink
Split function
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Mar 17, 2024
1 parent 6e074d1 commit a50e4a8
Showing 1 changed file with 70 additions and 67 deletions.
137 changes: 70 additions & 67 deletions src/nes/cpu/cpu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CPU: MOS 6502

import {Addressing, OpType, kInstTable} from './inst'
import {Addressing, Instruction, OpType, kInstTable} from './inst'
import {IBus} from './ibus'
import {Util} from '../../util/util'
import {Address, Byte, Word} from '../types'
Expand Down Expand Up @@ -167,82 +167,85 @@ export class Cpu {
return cycles
}

let pc = this.pc
const op = this.read8(pc++)
const op = this.read8(this.pc++)
const inst = kInstTable[op]
if (inst.opType === OpType.UNKNOWN) {
console.error(`Unknonwn OPCODE, ${Util.hex(this.pc - 1, 4)}: ${Util.hex(op, 2)}`)
this.paused = true
return 0
}

this.pc += inst.bytes
return this.execInst(inst)
}

private execInst(inst: Instruction): number {
const pc = this.pc
this.pc += inst.bytes - 1
let cycle = inst.cycle
let adr: Address // = this.getAdr(pc, inst.addressing)
{
switch (inst.addressing) {
case Addressing.IMMEDIATE:
case Addressing.RELATIVE:
adr = pc
break
case Addressing.ZEROPAGE:
adr = this.read8(pc)
break
case Addressing.ZEROPAGE_X:
adr = (this.read8(pc) + this.x) & 0xff
break
case Addressing.ZEROPAGE_Y:
adr = (this.read8(pc) + this.y) & 0xff
break
case Addressing.ABSOLUTE:
adr = this.read16(pc)
break
case Addressing.ABSOLUTE_X:
{
const base = this.read16(pc)
adr = (base + this.x) & 0xffff
if (!inst.write)
cycle += (((adr ^ base) >> 8) & 1) // 1 if page crossed or 0
}
break
case Addressing.ABSOLUTE_Y:
{
const base = this.read16(pc)
adr = (base + this.y) & 0xffff
if (!inst.write)
cycle += (((adr ^ base) >> 8) & 1) // 1 if page crossed or 0
}
break
case Addressing.INDIRECT_X:
{
const zeroPageAdr = this.read8(pc)
adr = this.read16Indirect((zeroPageAdr + this.x) & 0xff)
}
break
case Addressing.INDIRECT_Y:
{
const zeroPageAdr = this.read8(pc)
const base = this.read16Indirect(zeroPageAdr)
adr = (base + this.y) & 0xffff
if (!inst.write)
cycle += (((adr ^ base) >> 8) & 1) // 1 if page crossed or 0
}
break
case Addressing.INDIRECT:
{
const indirect = this.read16(pc)
adr = this.read16Indirect(indirect)
}
break
default:
console.error(`Illegal addressing: ${inst.addressing}, pc=${Util.hex(pc, 4)}`)
this.paused = true
// Fallthrough
case Addressing.ACCUMULATOR:
case Addressing.IMPLIED:
adr = 0 // Dummy.
break

switch (inst.addressing) {
case Addressing.IMMEDIATE:
case Addressing.RELATIVE:
adr = pc
break
case Addressing.ZEROPAGE:
adr = this.read8(pc)
break
case Addressing.ZEROPAGE_X:
adr = (this.read8(pc) + this.x) & 0xff
break
case Addressing.ZEROPAGE_Y:
adr = (this.read8(pc) + this.y) & 0xff
break
case Addressing.ABSOLUTE:
adr = this.read16(pc)
break
case Addressing.ABSOLUTE_X:
{
const base = this.read16(pc)
adr = (base + this.x) & 0xffff
if (!inst.write)
cycle += (((adr ^ base) >> 8) & 1) // 1 if page crossed or 0
}
break
case Addressing.ABSOLUTE_Y:
{
const base = this.read16(pc)
adr = (base + this.y) & 0xffff
if (!inst.write)
cycle += (((adr ^ base) >> 8) & 1) // 1 if page crossed or 0
}
break
case Addressing.INDIRECT_X:
{
const zeroPageAdr = this.read8(pc)
adr = this.read16Indirect((zeroPageAdr + this.x) & 0xff)
}
break
case Addressing.INDIRECT_Y:
{
const zeroPageAdr = this.read8(pc)
const base = this.read16Indirect(zeroPageAdr)
adr = (base + this.y) & 0xffff
if (!inst.write)
cycle += (((adr ^ base) >> 8) & 1) // 1 if page crossed or 0
}
break
case Addressing.INDIRECT:
{
const indirect = this.read16(pc)
adr = this.read16Indirect(indirect)
}
break
default:
console.error(`Illegal addressing: ${inst.addressing}, pc=${Util.hex(pc, 4)}`)
this.paused = true
// Fallthrough
case Addressing.ACCUMULATOR:
case Addressing.IMPLIED:
adr = 0 // Dummy.
break
}

// ========================================================
Expand Down

0 comments on commit a50e4a8

Please sign in to comment.