1
0
mirror of https://github.com/mre/mos6502.git synced 2024-11-24 11:31:00 +00:00
mre-mos6502/examples/euclid_bytes.rs
omarandlorraine 11499b6bc8
Starting on implementing different variants (#88)
* start on separating 6502 variants from cpu itself

* add a single variant: the NMOS one

* get examples & tests running again

* Add the Revision A variant, one that has no ROR

* disable failing lint in build-time dependencies

* Variant with no decimal mode

* Revert "disable failing lint in build-time dependencies"

This reverts commit c87975e937.

* some doc comments

* specify the variant in unit test now the API has changed

---------

Co-authored-by: Sam M W <you@example.com>
2023-10-31 16:45:15 +01:00

49 lines
1.4 KiB
Rust

extern crate mos6502;
use mos6502::cpu;
use mos6502::instruction::Nmos6502;
use mos6502::memory::Bus;
use mos6502::memory::Memory;
fn main() {
println!("Enter two numbers (< 128) separated by a space to know their GCD.");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let zero_page_data = input
.split_whitespace()
.map(|s| s.parse::<u8>().unwrap())
.collect::<Vec<u8>>();
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(Memory::new(), Nmos6502);
cpu.memory.set_bytes(0x00, &zero_page_data);
cpu.memory.set_bytes(0x10, &program);
cpu.registers.program_counter = 0x10;
cpu.run();
println!("GCD is {}", cpu.registers.accumulator);
}