mirror of
https://github.com/mre/mos6502.git
synced 2024-11-30 20:51:21 +00:00
new example
This commit is contained in:
parent
587fa91bae
commit
8c70df142c
47
examples/euclidean_algo.rs
Normal file
47
examples/euclidean_algo.rs
Normal file
@ -0,0 +1,47 @@
|
||||
extern crate mos6502;
|
||||
use mos6502::address::Address;
|
||||
use mos6502::cpu;
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).unwrap();
|
||||
let nums = input
|
||||
.trim()
|
||||
.split(' ')
|
||||
.map(|s| s.parse::<u8>().unwrap())
|
||||
.collect::<Vec<u8>>();
|
||||
let (first, second) = (nums[0], nums[1]);
|
||||
|
||||
let zero_page_data = [first, second];
|
||||
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();
|
||||
|
||||
println!("GCD is {:?}", cpu.registers.accumulator);
|
||||
}
|
@ -1,45 +1,11 @@
|
||||
// Copyright (C) 2014 The 6502-rs Developers
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// 3. Neither the names of the copyright holders nor the names of any
|
||||
// contributors may be used to endorse or promote products derived from this
|
||||
// software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
// POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
extern crate mos6502;
|
||||
|
||||
#[cfg(not(test))]
|
||||
use mos6502::cpu;
|
||||
|
||||
#[cfg(not(test))]
|
||||
use mos6502::address::Address;
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn main() {
|
||||
let mut cpu = cpu::CPU::new();
|
||||
|
||||
// "Load" a program
|
||||
|
||||
|
||||
let zero_page_data = [
|
||||
// ZeroPage data start
|
||||
0x00,
|
||||
@ -98,7 +64,7 @@ fn main() {
|
||||
0xFF, // Something invalid -- the end!
|
||||
];
|
||||
|
||||
let data = [
|
||||
let data: [u8; 25] = [
|
||||
0x00,
|
||||
0x09, // ADC Absolute target
|
||||
0x00,
|
||||
@ -126,6 +92,7 @@ fn main() {
|
||||
0x06, // ADC IndirectIndexedY target
|
||||
];
|
||||
|
||||
// "Load" a program
|
||||
cpu.memory.set_bytes(Address(0x0000), &zero_page_data);
|
||||
cpu.memory.set_bytes(Address(0x4000), &program);
|
||||
cpu.memory.set_bytes(Address(0x8000), &data);
|
||||
@ -133,6 +100,4 @@ fn main() {
|
||||
cpu.registers.program_counter = Address(0x4000);
|
||||
|
||||
cpu.run();
|
||||
|
||||
println!("{:?}", cpu);
|
||||
}
|
Loading…
Reference in New Issue
Block a user