Pronounced "Mossy"
A cycle-accurate 6502 CPU emulator written in JavaScript.
Originally developed as the CPU core for the HoneyCrisp Emulator, MOSe is now available for general-purpose use in building 6502-based system emulators.
// Create a CPU instance with 64KB RAM (default)
const cpu = new mose(64);
// Load some code
cpu.write(0x0600, 0xA9); // LDA #$42
cpu.write(0x0601, 0x42);
// Set program counter and execute
cpu.PC = 0x0600;
cpu.step();
console.log(cpu.A); // 0x42const cpu = new mose(ramSizeKB = 64)Creates a new 6502 CPU instance with the specified RAM size in kilobytes.
Supported RAM sizes: 4, 8, 16, 32, 48, 64 (default: 64KB)
const cpu = new mose(16); // 16KB RAM system
const cpu = new mose(64); // 64KB RAM system (default)step()- Execute one instruction, returns cycles takenrunCycles(target)- Execute instructions for approximatelytargetcyclesreset()- Reset CPU to initial state, load PC from reset vector ($FFFC-$FFFD)triggerBreak()- Set break flag for next instruction cycletriggerNMI()- Trigger a Non-Maskable Interrupt (processed on next step)
read(addr)- Read byte from memory addresswrite(addr, value)- Write byte to memory address
push(value)- Push byte onto stackpop()- Pop byte from stack
getStatus()- Get status register as bytesetStatus(value)- Set status register from bytesetZN(value)- Set Zero and Negative flags based on value
runTest(test)- Run a single test case, returns result objectrunTestSuite(tests)- Run multiple tests, returns summaryrunFunctionalTest(romData)- Run Klaus2m5's 6502 functional test ROM
Direct access to CPU registers:
A- AccumulatorX- X registerY- Y registerPC- Program CounterS- Stack Pointer
C- Carry flagZ- Zero flagI- Interrupt Disable flagD- Decimal Mode flagB- Break flagV- Overflow flagN- Negative flag
cycles- Cycles taken by last instructiontotalCycles- Total cycles executed since resetopcode- Last executed opcoderamSize- Configured RAM size in KB
emulateIndirectJMPBug- Enable/disable 6502 JMP ($xxFF) hardware bug (default: true)
const cpu = new mose(64);
// Load a program that adds two numbers
const program = [
0xA9, 0x05, // LDA #$05
0x69, 0x03, // ADC #$03
0x8D, 0x00, 0x02, // STA $0200
0x00 // BRK
];
// Load program at $0600
program.forEach((byte, i) => cpu.write(0x0600 + i, byte));
// Set reset vector to point to the program
cpu.write(0xFFFC, 0x00);
cpu.write(0xFFFD, 0x06);
// Reset and run
cpu.reset();
while (cpu.opcode !== 0x00) { // Run until BRK
cpu.step();
}
console.log(`Result: ${cpu.read(0x0200)}`); // Should print 8
console.log(`Total cycles: ${cpu.totalCycles}`); // Cycle countconst cpu = new mose(64);
// Load a BCD addition program
const program = [
0xF8, // SED ; Set decimal mode
0xA9, 0x09, // LDA #$09 ; Load 9 (BCD)
0x69, 0x01, // ADC #$01 ; Add 1 (BCD)
0x8D, 0x00, 0x02, // STA $0200 ; Store result
0xD8, // CLD ; Clear decimal mode
0x00 // BRK
];
program.forEach((byte, i) => cpu.write(0x0600 + i, byte));
cpu.write(0xFFFC, 0x00);
cpu.write(0xFFFD, 0x06);
cpu.reset();
while (cpu.opcode !== 0x00) {
cpu.step();
}
console.log(`BCD Result: ${cpu.read(0x0200).toString(16)}`); // 0x10 (BCD: 10)const cpu = new mose(64);
// Set up NMI handler
const nmiHandler = [
0xA9, 0xFF, // LDA #$FF
0x8D, 0x00, 0x02, // STA $0200
0x40 // RTI
];
// Load NMI handler at $8000
nmiHandler.forEach((byte, i) => cpu.write(0x8000 + i, byte));
// Set NMI vector
cpu.write(0xFFFA, 0x00);
cpu.write(0xFFFB, 0x80);
// Main program
const program = [
0xA9, 0x00, // LDA #$00
0x8D, 0x00, 0x02, // STA $0200
0xEA, // NOP
0xEA, // NOP
0x00 // BRK
];
program.forEach((byte, i) => cpu.write(0x0600 + i, byte));
cpu.write(0xFFFC, 0x00);
cpu.write(0xFFFD, 0x06);
cpu.reset();
cpu.step(); // Execute LDA
cpu.step(); // Execute STA
// Trigger NMI
cpu.triggerNMI();
cpu.step(); // NMI is processed here
console.log(`Result: ${cpu.read(0x0200)}`); // 0xFF (set by NMI handler)MOSe is designed as an emulation core. It handles CPU operations, but you'll need to add system-specific hardware to create a complete emulator.
Examples of systems you can build:
- Apple-1 - Proven with HoneyCrisp Emulator (add 6820 PIA, terminal I/O)
- NES - Add PPU, APU, and memory mappers
- Apple II - Add video, keyboard, and disk I/O
- Commodore 64 - Add VIC-II, SID, and CIA chips
- Atari 2600 - Add TIA and RIOT chips
class MySystem {
constructor() {
this.cpu = new mose(64);
this.ioPort = 0x00;
// Override read/write for memory-mapped I/O
const originalRead = this.cpu.read.bind(this.cpu);
const originalWrite = this.cpu.write.bind(this.cpu);
this.cpu.read = (addr) => {
if (addr === 0xD000) return this.ioPort; // I/O port
return originalRead(addr);
};
this.cpu.write = (addr, val) => {
if (addr === 0xD000) {
this.ioPort = val;
console.log(`I/O Port: ${val}`);
return;
}
originalWrite(addr, val);
};
}
}- All 151 official opcodes
- Common undocumented/illegal opcodes (LAX, SAX, DCP, ISC, SLO, RLA, SRE, RRA, etc.)
- All 13 addressing modes
- Cycle-accurate timing with page-cross penalties
- Decimal mode (BCD) arithmetic support
- NMI (Non-Maskable Interrupt) support
- Configurable hardware bug emulation (JMP indirect)
- Proper flag behavior matching hardware
- Built-in test framework
- Compatible with Klaus2m5's functional test suite
- Individual instruction testing support
# Clone the repository
git clone https://github.com/landonjsmith/MOSe.git
# Or download the latest release
# https://github.com/landonjsmith/MOSe/releases- Browsers - Works in all modern browsers (ES6+)
- Node.js - Compatible with Node.js 12+ (CommonJS export included)
- Bundlers - Works with Webpack, Rollup, Vite, etc.
MIT License - See LICENSE file for details
Developed by Landon J. Smith
Special thanks to Klaus Dormann for the 6502 functional test suite.