2018-10-25 13:02:27 +00:00
|
|
|
# mos6502
|
|
|
|
|
2019-10-22 00:17:15 +00:00
|
|
|
[![Build Status](https://travis-ci.org/mre/mos6502.svg?branch=master)](https://travis-ci.org/mre/mos6502)
|
|
|
|
![](https://github.com/mre/mos6502/workflows/test/badge.svg)
|
2017-10-08 15:31:20 +00:00
|
|
|
|
2018-11-04 20:39:15 +00:00
|
|
|
An emulator for the [MOS 6502 CPU](https://en.wikipedia.org/wiki/MOS_Technology_6502) written in Rust.
|
2018-11-04 20:54:09 +00:00
|
|
|
This started off as a fork of [amw-zero/6502-rs](https://github.com/amw-zero/6502-rs),
|
2017-10-08 15:31:20 +00:00
|
|
|
which seems to be unmaintained at this point.
|
|
|
|
|
2018-11-04 19:40:00 +00:00
|
|
|
It builds with the latest stable Rust and supports `#[no_std]` targets.
|
2018-11-04 19:26:51 +00:00
|
|
|
|
|
|
|
## Usage example
|
|
|
|
|
|
|
|
```rust
|
|
|
|
extern crate mos6502;
|
|
|
|
use mos6502::address::Address;
|
2019-10-22 12:15:26 +00:00
|
|
|
use mos6502::cpu;
|
2018-11-04 19:26:51 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
2019-10-22 15:53:13 +00:00
|
|
|
let zero_page_data = [56, 49];
|
2018-11-04 19:26:51 +00:00
|
|
|
|
|
|
|
let program = [
|
2019-10-22 12:15:26 +00:00
|
|
|
// (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
|
2018-11-04 19:26:51 +00:00
|
|
|
];
|
|
|
|
|
2019-10-22 12:15:26 +00:00
|
|
|
let mut cpu = cpu::CPU::new();
|
2018-11-04 19:26:51 +00:00
|
|
|
|
2019-10-22 12:15:26 +00:00
|
|
|
cpu.memory.set_bytes(Address(0x00), &zero_page_data);
|
|
|
|
cpu.memory.set_bytes(Address(0x10), &program);
|
|
|
|
cpu.registers.program_counter = Address(0x10);
|
2018-11-04 19:26:51 +00:00
|
|
|
|
|
|
|
cpu.run();
|
2019-10-22 12:15:26 +00:00
|
|
|
|
2019-10-22 15:53:13 +00:00
|
|
|
assert_eq!(7, cpu.registers.accumulator);
|
|
|
|
|
2018-11-04 19:26:51 +00:00
|
|
|
}
|
|
|
|
```
|