2019-10-12 21:07:53 +00:00
|
|
|
extern crate mos6502;
|
2022-10-18 08:59:52 +00:00
|
|
|
|
2019-10-12 21:07:53 +00:00
|
|
|
use mos6502::cpu;
|
2023-10-31 15:45:15 +00:00
|
|
|
use mos6502::instruction::Nmos6502;
|
2023-04-03 05:15:07 +00:00
|
|
|
use mos6502::memory::Bus;
|
2023-04-03 06:31:49 +00:00
|
|
|
use mos6502::memory::Memory;
|
2019-10-12 21:07:53 +00:00
|
|
|
|
|
|
|
fn main() {
|
2023-06-20 08:32:28 +00:00
|
|
|
println!("Enter two numbers (< 128) separated by a space to know their GCD.");
|
2019-10-12 21:07:53 +00:00
|
|
|
let mut input = String::new();
|
|
|
|
std::io::stdin().read_line(&mut input).unwrap();
|
2019-10-12 22:22:57 +00:00
|
|
|
|
|
|
|
let zero_page_data = input
|
2022-06-07 18:17:44 +00:00
|
|
|
.split_whitespace()
|
2019-10-12 21:07:53 +00:00
|
|
|
.map(|s| s.parse::<u8>().unwrap())
|
|
|
|
.collect::<Vec<u8>>();
|
|
|
|
|
|
|
|
let program = [
|
2022-06-07 17:30:29 +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
|
2019-10-12 21:07:53 +00:00
|
|
|
0x4c, 0x12, 0x00, // Jump to .algo_
|
2022-06-07 17:30:29 +00:00
|
|
|
// .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
|
2019-10-12 21:07:53 +00:00
|
|
|
0x4c, 0x10, 0x00, // Jump to .algo
|
|
|
|
];
|
|
|
|
|
2023-10-31 15:45:15 +00:00
|
|
|
let mut cpu = cpu::CPU::new(Memory::new(), Nmos6502);
|
2019-10-12 21:07:53 +00:00
|
|
|
|
2022-10-18 08:59:52 +00:00
|
|
|
cpu.memory.set_bytes(0x00, &zero_page_data);
|
|
|
|
cpu.memory.set_bytes(0x10, &program);
|
|
|
|
cpu.registers.program_counter = 0x10;
|
2019-10-12 21:07:53 +00:00
|
|
|
|
|
|
|
cpu.run();
|
|
|
|
|
2022-06-07 18:17:44 +00:00
|
|
|
println!("GCD is {}", cpu.registers.accumulator);
|
2019-10-12 21:07:53 +00:00
|
|
|
}
|