From 8c70df142c7bb5c68e8880033130e973d16b37ef Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 00:07:53 +0300 Subject: [PATCH 1/6] 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 From 71ac36b0837b8cbedd8a427120700d8603cb1109 Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 00:14:55 +0300 Subject: [PATCH 2/6] return comment --- examples/mos6502.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/examples/mos6502.rs b/examples/mos6502.rs index e41531e..5c7ddc1 100644 --- a/examples/mos6502.rs +++ b/examples/mos6502.rs @@ -1,3 +1,30 @@ +// 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; use mos6502::cpu; From c837d7c9869005afddbf98f43b478df75c354a11 Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 00:18:07 +0300 Subject: [PATCH 3/6] return comments --- examples/mos6502.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/mos6502.rs b/examples/mos6502.rs index 5c7ddc1..f6d6d99 100644 --- a/examples/mos6502.rs +++ b/examples/mos6502.rs @@ -27,12 +27,19 @@ 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, @@ -91,7 +98,7 @@ fn main() { 0xFF, // Something invalid -- the end! ]; - let data: [u8; 25] = [ + let data = [ 0x00, 0x09, // ADC Absolute target 0x00, @@ -119,7 +126,6 @@ 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); @@ -127,4 +133,6 @@ fn main() { cpu.registers.program_counter = Address(0x4000); cpu.run(); + + println!("{:?}", cpu); } \ No newline at end of file From 0e112a8a44db394af6cd45080ea5ff8558477099 Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 00:19:09 +0300 Subject: [PATCH 4/6] return newline --- examples/mos6502.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/mos6502.rs b/examples/mos6502.rs index f6d6d99..f8c4612 100644 --- a/examples/mos6502.rs +++ b/examples/mos6502.rs @@ -135,4 +135,4 @@ fn main() { cpu.run(); println!("{:?}", cpu); -} \ No newline at end of file +} From 7f7b9d87a2afad51918fd37d418e83d3aa230574 Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 00:34:19 +0300 Subject: [PATCH 5/6] update toml --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 9879428..d4c9ddc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ license = "BSD-3-Clause" version = "0.1.0" authors = ["The 6502-rs Developers"] build = "build.rs" +exclude = ["examples/**"] [lib] # This will look in src/lib.rs From 6a0163138abfdfe303044e0434ab8a31c811dc71 Mon Sep 17 00:00:00 2001 From: GooseDB Date: Sun, 13 Oct 2019 01:22:57 +0300 Subject: [PATCH 6/6] refactoring --- examples/euclidean_algo.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/euclidean_algo.rs b/examples/euclidean_algo.rs index 330a542..2344ccd 100644 --- a/examples/euclidean_algo.rs +++ b/examples/euclidean_algo.rs @@ -3,16 +3,16 @@ use mos6502::address::Address; use mos6502::cpu; fn main() { + println!("Enter two numbers (< 128) to know their GCD:"); let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); - let nums = input + + let zero_page_data = 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