From 8c70df142c7bb5c68e8880033130e973d16b37ef Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 00:07:53 +0300 Subject: [PATCH] new example --- examples/euclidean_algo.rs | 47 ++++++++++++++++++++++++++++++++ {src/bin => examples}/mos6502.rs | 41 ++-------------------------- 2 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 examples/euclidean_algo.rs rename {src/bin => examples}/mos6502.rs (60%) diff --git a/examples/euclidean_algo.rs b/examples/euclidean_algo.rs new file mode 100644 index 0000000..330a542 --- /dev/null +++ b/examples/euclidean_algo.rs @@ -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::().unwrap()) + .collect::>(); + 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); +} diff --git a/src/bin/mos6502.rs b/examples/mos6502.rs similarity index 60% rename from src/bin/mos6502.rs rename to examples/mos6502.rs index f8c4612..e41531e 100644 --- a/src/bin/mos6502.rs +++ b/examples/mos6502.rs @@ -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); -} +} \ No newline at end of file