1
0
mirror of https://github.com/mre/mos6502.git synced 2024-06-02 20:41:33 +00:00
mre-mos6502/README.md
Matthias Endler 3830ccca27 Add comments
2021-04-07 14:01:45 +02:00

1.7 KiB

mos6502

docs.rs

An emulator for the MOS 6502 CPU written in Rust.
It builds on stable Rust and supports #[no_std] targets.

Usage example

use mos6502::address::Address;
use mos6502::cpu;

fn main() {
    // Calculate the greatest common divisor of 56 and 49
    // using Euclid's algorithm.
    let zero_page_data = [56, 49];

    let program = [
        // (F)irst | (S)econd
        // .algo
        0xa5, 0x00,       // Load from F to A
        // .algo_
        0x38,             // Set carry flag
        0xe5, 0x01,       // Substract S from number in A (from F)
        0xf0, 0x07,       // Jump to .end if diff is zero
        0x30, 0x08,       // Jump to .swap if diff is negative
        0x85, 0x00,       // Load A to F
        0x4c, 0x12, 0x00, // Jump to .algo_
        // .end
        0xa5, 0x00,       // Load from S to A
        0xff,
        // .swap
        0xa6, 0x00,       // load F to X
        0xa4, 0x01,       // load S to Y
        0x86, 0x01,       // Store X to F
        0x84, 0x00,       // Store Y to S
        0x4c, 0x10, 0x00, // Jump to .algo
    ];

    let mut cpu = cpu::CPU::new();

    cpu.memory.set_bytes(Address(0x00), &zero_page_data);
    cpu.memory.set_bytes(Address(0x10), &program);
    cpu.registers.program_counter = Address(0x10);

    cpu.run();

    // The expected GCD is 7
    assert_eq!(7, cpu.registers.accumulator);
}

Credits

This started off as a fork of amw-zero/6502-rs, which seems to be unmaintained at this point.