npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

6502-emulator

v1.0.0

Published

Emulator for the MOS6502 CPU. Designed for simulating custom logic boards that use the 6502.

Downloads

11

Readme

CPU6502 Emulator

A javascript emulator for the MOS6502 (WDC 65c02 compatible) CPU. Designed for simulating custom logic boards that use the 6502.

All memory read/writes call out to a delegate function, allowing custom memory logic, I/O logic and address decoding.

Configuration

const cpu = new CPU6502({
  accessMemory, // function to be called when CPU reads/writes to memory
  logInstructions: false, // output each instruction to console, for advanced debugging
  logInternalState: false, // output internal processor state after each instruction for advanced debugging
  maxInstructions: 50 // automatically pause execution after 50 instructions
});

Usage

Available methods

const cpu = new CPU6502();

cpu.reset(); // trigger reset sequence
cpu.triggerIRQB(); // trigger IRQB interrupt
cpu.triggerNMIB(); // trigger NMIB interrupt

cpu.pauseClock(); // pause clock, stop executing instructions
cpu.startClock(); // start/resume clock

// accessing internal state
console.log(cpu.reg_a);
console.log(cpu.reg_x);
console.log(cpu.reg_y);
console.log(cpu.programCounter);
console.log(cpu.stackPointer);
console.log(cpu.processorStatus);

Example 1: Executing simple machine code

import { CPU6502, ReadWrite } from "6502-emulator";

// instruction opcodes
const I_NOOP = 0xea;
const I_LDA = 0xa9;
const I_STA = 0x8d;
const I_JMP = 0x4c;

// set up memory
const ram = new Uint8ClampedArray(0xffff); // 64kb ram
ram.fill(I_NOOP); // fill ram with noop instructions

// store reset vector as 0200 (little endian)
ram[0xfffc] = 0x00;
ram[0xfffd] = 0x02;

// create a basic program
ram.set(
  [
    I_LDA, 0x55, // lda 55
    I_STA, 0x00, 0x60, // 55 -> 6000 (output 55 to address 0x6000)

    I_LDA, 0xaa, // lda AA
    I_STA, 0x00, 0x60, // AA -> 6000 (output AA to address 0x6000)

    I_JMP, 0x00, 0x02  // jump back to start of program
  ],
  0x0200
);

const accessMemory = (readWrite, address, value) => {
  // capture a write to 0x6000 as a magic output address, print to console
  if (address === 0x6000 && readWrite === ReadWrite.write) {
    console.log("Output: ", value.toString(16));
    return;
  }
  
  // write value to RAM (processor is reading from [address])
  if (readWrite === ReadWrite.read) {
    return ram[address];
  }

  // store value in RAM (processor is writing [value] to [address])
  ram[address] = value;
};

const cpu = new CPU6502({ accessMemory });
// trigger a reset to start the clock & jump to the reset vector
cpu.reset();

Example 2: Load ROM image from disk

import { CPU6502, ReadWrite } from "6502-emulator";

// load image from disk
const ramImagePath = "./myROMFile";
const ramImage = fs.readFileSync(ramImagePath);
  
// set up memory
const ram = Uint8ClampedArray.from(ramImage);

const accessMemory = (readWrite, address, value) => {
  // capture a write to 0x6000 as a magic output address, print to console
  if (address === 0x6000 && readWrite === ReadWrite.write) {
    console.log("Output: ", value.toString(16));
    return;
  }

  // capture a write to 0x6005 as a magic output address, pause the clock
  if (address === 0x6005 && readWrite === ReadWrite.write) {
    console.log("Exit captured! pausing clock");
    cpu.pauseClock();
    return;
  }

  // write value to RAM (processor is reading from [address])
  if (readWrite === ReadWrite.read) {
    return ram[address];
  }

  // store value in RAM (processor is writing [value] to [address])
  ram[address] = value;
}

const cpu = new CPU6502({ accessMemory });
// trigger a reset to start the clock & jump to the reset vector
cpu.reset();