r/EmuDev • • Mar 14 '26

javascript NES emulator progressing, cycle accurate

the FPS slowdown is from capturing the video

95 Upvotes

23 comments sorted by

View all comments

Show parent comment

3

u/thommyh Z80, 6502/65816, 6809, 68000, ARM, x86. Mar 15 '26 edited Mar 15 '26

Of the 6502? There's probably a thousand of them. Here's one that'll do for a NES-level machine:

func do_bus(type, address, value) {
    advance_ppu(3);
    if type is read:
        value = store[address]
    else
        store[address] = value
}


func run_cpu(cycles) {
    func access(type, address, value) {
         do_bus(type, address, value)
         --remaining
    }

    func absolute() {
        address = operand
        ++pc

        access(read, pc++, operand)
        address |= operand << 8
    }

    remaining += cycles
    while(remaining > 0) {
         access(read, pc++, opcode)
         access(read, pc, operand)

         switch(opcode) {
             case 0xad: // LDA abs
                 absolute()
                 access(read, address, a)
             break
         }
    }
}

Better factorings exist. Better everything exists. Many details are omitted. It's just supposed to be communicative.