From 0a6b9c561174c6e0140394ccfd9be04d35de7011 Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 02:25:37 -0400 Subject: [PATCH 1/7] Add .gitignore file -- this is probably overkill --- .gitignore | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..718e797 --- /dev/null +++ b/.gitignore @@ -0,0 +1,67 @@ +*.a +*.aux +*.bc +*.boot +*.bz2 +*.cmi +*.cmo +*.cmx +*.cp +*.cps +*.d +*.dSYM +*.def +*.diff +*.dll +*.dylib +*.elc +*.epub +*.exe +*.fn +*.html +*.ky +*.ll +*.llvm +*.log +*.o +*.orig +*.out +*.patch +*.pdf +*.pg +*.pot +*.pyc +*.rej +*.rlib +*.rustc +*.so +*.swo +*.swp +*.tmp +*.toc +*.tp +*.vr +*.x86 +*~ +.#* +.DS_Store +.cproject +.hg/ +.hgignore +.project +.settings/ +.valgrindrc +/*-*-*-*/ +/*-*-*/ +/Makefile +/doc +/target/ +/test/ +/tmp/ +TAGS +TAGS.emacs +TAGS.vi +\#* +\#*\# +src/.DS_Store +tmp.*.rs From 9a81a48502944ba2983d6ca677143e7809e74df1 Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 02:26:26 -0400 Subject: [PATCH 2/7] Add copyright, authorship, and license information --- AUTHORS.txt | 5 +++++ COPYRIGHT | 19 +++++++++++++++++++ LICENSE | 27 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 AUTHORS.txt create mode 100644 COPYRIGHT create mode 100644 LICENSE diff --git a/AUTHORS.txt b/AUTHORS.txt new file mode 100644 index 0000000..b4c91ce --- /dev/null +++ b/AUTHORS.txt @@ -0,0 +1,5 @@ +6502-rs was written by: + +Alex Weisberger +Andrew Keeton +Johannes Muenzel diff --git a/COPYRIGHT b/COPYRIGHT new file mode 100644 index 0000000..c672577 --- /dev/null +++ b/COPYRIGHT @@ -0,0 +1,19 @@ +6502-rs is copyright (C) 2014 The 6502-rs Developers (listed in the file +AUTHORS.txt). + +Licensed under the license reproduced in the file LICENSE at the root of this +repository. All files in the project carrying such notice may not be copied, +modified, or distributed except according to those terms. + + +Additional copyright may be retained by contributors other than The 6502-rs +Developers or the parties enumerated in this file. Such copyright can be +determined on a case-by-case basis by examining the author of each portion of a +file in the revision-control commit records of the project, or by consulting +representative comments claiming copyright ownership for a file. + +In all such cases, the absence of explicit licensing text indicates that the +contributor chose to license their work for distribution under identical terms +to those The 6502-rs Developers have chosen for the collective work, enumerated +at the top of this file. The only difference is the retention of copyright +itself, held by the contributor. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3a0213b --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +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. + From b22b36bc2a3445f1047ae2342436ac5597c627f0 Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 02:29:27 -0400 Subject: [PATCH 3/7] Get some basic definitions and module structure going. Not sure whether Cargo.lock should be in .gitignore --- .gitignore | 1 + Cargo.toml | 40 +++++++++ src/defs6502/Cargo.toml | 36 +++++++++ src/defs6502/lib.rs | 39 +++++++++ src/defs6502/machine/mod.rs | 156 ++++++++++++++++++++++++++++++++++++ src/exe/main.rs | 37 +++++++++ 6 files changed, 309 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/defs6502/Cargo.toml create mode 100644 src/defs6502/lib.rs create mode 100644 src/defs6502/machine/mod.rs create mode 100644 src/exe/main.rs diff --git a/.gitignore b/.gitignore index 718e797..a50055b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +Cargo.lock *.a *.aux *.bc diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3a54a43 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,40 @@ +# 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. + +[package] + +name = "6502-rs" +version = "0.0.1" +authors = ["The 6502-rs Developers"] + +[[bin]] +name = "6502-rs" +path = "src/exe/main.rs" + +[dependencies.defs6502] +path="src/defs6502" + diff --git a/src/defs6502/Cargo.toml b/src/defs6502/Cargo.toml new file mode 100644 index 0000000..e3d8ff4 --- /dev/null +++ b/src/defs6502/Cargo.toml @@ -0,0 +1,36 @@ +# 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. + +[package] +name = "defs6502" +version = "0.0.1" +authors = [ "The 6502-rs Developers" ] + +[lib] +name = "defs6502" +path = "lib.rs" + diff --git a/src/defs6502/lib.rs b/src/defs6502/lib.rs new file mode 100644 index 0000000..df841ba --- /dev/null +++ b/src/defs6502/lib.rs @@ -0,0 +1,39 @@ +// 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. + +pub use machine::ProcessorStatus; +pub use machine::Machine; +pub use machine::StackPointer; +pub use machine::STACK_POINTER_IN_MEMORY_LO; +pub use machine::STACK_POINTER_IN_MEMORY_HI; +pub use machine::MemoryAddr; +pub use machine::stack_pointer_to_addr; +pub use machine::Instruction; +pub use machine::AddressingMode; + +mod machine; + diff --git a/src/defs6502/machine/mod.rs b/src/defs6502/machine/mod.rs new file mode 100644 index 0000000..a91681c --- /dev/null +++ b/src/defs6502/machine/mod.rs @@ -0,0 +1,156 @@ +// 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. + +// Abbreviations +// +// Registers +// +// A accumulator +// X general purpose register +// Y general purpose register +// SP stack pointer +// NV-BDIZC processor status flags -- see ProcessorStatus bitflags +// PC program counter +// +// M stands for `Memory location` + +pub bitflags! { + flags ProcessorStatus: u8 { + static N = 0b10000000, // Negative + static V = 0b01000000, // oVerflow + static B = 0b00010000, // Brk + static D = 0b00001000, // Decimal mode active? + static I = 0b00000100, // Irq disabled? + static Z = 0b00000010, // Zero + static C = 0b00000001, // Carry + } +} + +pub struct Machine { + A : u8, + X : u8, + Y : u8, + SP : u8, + P : ProcessorStatus, + PC : u16, +} + +impl Machine { + pub fn new() -> Machine { + Machine { A: 0, X: 0, Y: 0, SP: 0, P: ProcessorStatus::empty(), PC: 0 } + } +} + +pub struct StackPointer(u8); + +pub static STACK_POINTER_IN_MEMORY_LO : u16 = 0x0100; +pub static STACK_POINTER_IN_MEMORY_HI : u16 = 0x01FF; + +pub struct MemoryAddr(u16); + +pub fn stack_pointer_to_addr(StackPointer(x) : StackPointer) -> MemoryAddr +{ + MemoryAddr(x as u16 + STACK_POINTER_IN_MEMORY_HI) +} + +#[deriving(Show)] +pub enum Instruction + // out in +{ ADC // ADd with Carry................ A,Z,C,N = A + M + C +, AND // logical AND................... A,Z,N = A && M +, ASL // Arithmetic Shift Left......... A,Z,C,N = M << 1 +, BCC // Branch if Carry Clear......... _ <- +, BCS // Branch if Carry Set........... +, BEQ // Branch if Equal............... +, BIT // BIT test...................... a & +, BMI // Branch if Minus............... +, BNE // Branch if Not Equal........... +, BPL // Branch if Positive............ +, BRK // BReaK......................... +, BVC // Branch if oVerflow Clear...... +, BVS // Branch if oVerflow Set........ +, CLC // CLear Carry flag.............. +, CLD // Clear Decimal Mode............ +, CLI // Clear Interrupt Disable....... +, CLV // Clear oVerflow flag........... +, CMP // Compare....................... +, CPX // Compare X register............ +, CPY // Compare Y register............ +, DEC // DECrement memory.............. +, DEX // DEcrement X register.......... +, DEY // DEcrement Y register.......... +, EOR // Exclusive OR.................. +, INC // INCrement memory.............. +, INX // INcrement X register.......... +, INY // INcrement Y register.......... +, JMP // JuMP.......................... +, JSR // Jump to SubRoutine............ +, LDA // LoaD Accumulator.............. +, LDX // LoaD X register............... +, LDY // LoaD Y register............... +, LSR // Logical Shift Right........... +, NOP // No OPeration.................. +, ORA // inclusive OR.................. +, PHA // PusH Accumulator.............. +, PHP // PusH Processor status......... +, PLA // PuLl Accumulator.............. +, PLP // PuLl Processor status......... +, ROL // ROtate Left................... +, ROR // ROtate Right.................. +, RTI // ReTurn from Interrupt......... +, RTS // ReTurn from Subroutine........ +, SBC // SuBtract with Carry........... +, SEC // SEt Carry flag................ +, SED // SEt Decimal flag.............. +, SEI // SEt Interrupt disable......... +, STA // STore Accumulator............. +, STX // STore X register.............. +, STY // STore Y register.............. +, TAX // Transfer Accumulator to X..... +, TAY // Transfer Accumulator to Y..... +, TSX // Transfer Stack pointer to X... +, TXA // Transfer X to Accumulator..... +, TXS // Transfer X to Stack pointer... +, TYA // Transfer Y to Accumulator..... +} + +pub enum AddressingMode { + Immediate, + Absolute, + ZeroPage, + Implied, + IndirectAbsolute, + AbsoluteIndexedX, + AbsoluteIndexedY, + ZeroPageIndexedX, + ZeroageIndexedY, + IndexedIndirect, + IndirectIndexed, + Relative, + Accumulator, +} + diff --git a/src/exe/main.rs b/src/exe/main.rs new file mode 100644 index 0000000..fde0727 --- /dev/null +++ b/src/exe/main.rs @@ -0,0 +1,37 @@ +// 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 defs6502; + +use defs6502::Machine; + +fn main() { + let _q = Machine::new(); + + println!("Hello, 6502?"); +} + From 0b2c805a96d5f7f93df076bec55b73c589f4f49b Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 02:59:00 -0400 Subject: [PATCH 4/7] Shuffle things around a little. For now a library called machine6502 will be the main code base. 6502-rs is inconvenient to use in a rust context since it starts with a number. The test_exe is no longer the main cargo build target --- .gitignore | 2 +- Cargo.toml | 11 ++++------ src/{defs6502 => machine6502}/lib.rs | 12 +---------- src/{defs6502 => machine6502}/machine/mod.rs | 22 +++++++++++++------- src/{defs6502 => test_exe}/Cargo.toml | 11 ++++++---- src/{exe => test_exe}/main.rs | 4 ++-- 6 files changed, 30 insertions(+), 32 deletions(-) rename src/{defs6502 => machine6502}/lib.rs (82%) rename src/{defs6502 => machine6502}/machine/mod.rs (94%) rename src/{defs6502 => test_exe}/Cargo.toml (94%) rename src/{exe => test_exe}/main.rs (96%) diff --git a/.gitignore b/.gitignore index a50055b..b8dff35 100644 --- a/.gitignore +++ b/.gitignore @@ -56,7 +56,7 @@ Cargo.lock /*-*-*/ /Makefile /doc -/target/ +target/ /test/ /tmp/ TAGS diff --git a/Cargo.toml b/Cargo.toml index 3a54a43..612f4d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,14 +27,11 @@ [package] -name = "6502-rs" +name = "machine6502" version = "0.0.1" authors = ["The 6502-rs Developers"] -[[bin]] -name = "6502-rs" -path = "src/exe/main.rs" - -[dependencies.defs6502] -path="src/defs6502" +[lib] +name = "machine6502" +path = "src/machine6502/lib.rs" diff --git a/src/defs6502/lib.rs b/src/machine6502/lib.rs similarity index 82% rename from src/defs6502/lib.rs rename to src/machine6502/lib.rs index df841ba..754e212 100644 --- a/src/defs6502/lib.rs +++ b/src/machine6502/lib.rs @@ -25,15 +25,5 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -pub use machine::ProcessorStatus; -pub use machine::Machine; -pub use machine::StackPointer; -pub use machine::STACK_POINTER_IN_MEMORY_LO; -pub use machine::STACK_POINTER_IN_MEMORY_HI; -pub use machine::MemoryAddr; -pub use machine::stack_pointer_to_addr; -pub use machine::Instruction; -pub use machine::AddressingMode; - -mod machine; +pub mod machine; diff --git a/src/defs6502/machine/mod.rs b/src/machine6502/machine/mod.rs similarity index 94% rename from src/defs6502/machine/mod.rs rename to src/machine6502/machine/mod.rs index a91681c..c1a5f7d 100644 --- a/src/defs6502/machine/mod.rs +++ b/src/machine6502/machine/mod.rs @@ -50,18 +50,26 @@ pub bitflags! { } } +#[allow(non_snake_case)] pub struct Machine { - A : u8, - X : u8, - Y : u8, - SP : u8, - P : ProcessorStatus, - PC : u16, + pub A : u8, + pub X : u8, + pub Y : u8, + pub SP : u8, + pub P : ProcessorStatus, + pub PC : u16, } impl Machine { pub fn new() -> Machine { - Machine { A: 0, X: 0, Y: 0, SP: 0, P: ProcessorStatus::empty(), PC: 0 } + Machine { + A: 0, + X: 0, + Y: 0, + SP: 0, + P: ProcessorStatus::empty(), + PC: 0 + } } } diff --git a/src/defs6502/Cargo.toml b/src/test_exe/Cargo.toml similarity index 94% rename from src/defs6502/Cargo.toml rename to src/test_exe/Cargo.toml index e3d8ff4..43e1196 100644 --- a/src/defs6502/Cargo.toml +++ b/src/test_exe/Cargo.toml @@ -26,11 +26,14 @@ # POSSIBILITY OF SUCH DAMAGE. [package] -name = "defs6502" +name = "test_exe" version = "0.0.1" authors = [ "The 6502-rs Developers" ] -[lib] -name = "defs6502" -path = "lib.rs" +[[bin]] +name = "test_exe" +path = "main.rs" + +[dependencies.machine6502] +path="../../" diff --git a/src/exe/main.rs b/src/test_exe/main.rs similarity index 96% rename from src/exe/main.rs rename to src/test_exe/main.rs index fde0727..d27be44 100644 --- a/src/exe/main.rs +++ b/src/test_exe/main.rs @@ -25,9 +25,9 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -extern crate defs6502; +extern crate machine6502; -use defs6502::Machine; +use machine6502::machine::Machine; fn main() { let _q = Machine::new(); From e4c998ddfd9b6faaaac393e3b8a13d81ef49e8bc Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 03:37:13 -0400 Subject: [PATCH 5/7] Add just a bit of extra annotation to the Instruction chart --- src/machine6502/defs/mod.rs | 170 +++++++++++++++++++++++++++++++++ src/machine6502/lib.rs | 2 +- src/machine6502/machine/mod.rs | 164 ------------------------------- src/test_exe/main.rs | 2 +- 4 files changed, 172 insertions(+), 166 deletions(-) create mode 100644 src/machine6502/defs/mod.rs delete mode 100644 src/machine6502/machine/mod.rs diff --git a/src/machine6502/defs/mod.rs b/src/machine6502/defs/mod.rs new file mode 100644 index 0000000..849cedf --- /dev/null +++ b/src/machine6502/defs/mod.rs @@ -0,0 +1,170 @@ +// 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. + +// Abbreviations +// +// General +// +// M | `Memory location` +// +// Registers +// +// A | accumulator +// X | general purpose register +// Y | general purpose register +// NV-BDIZC | processor status flags -- see ProcessorStatus bitflags +// SP | stack pointer +// PC | program counter +// + +pub bitflags! { + flags ProcessorStatus: u8 { + static N = 0b10000000, // Negative -- sometimes called S for "sign" + static V = 0b01000000, // oVerflow + static B = 0b00010000, // Brk + static D = 0b00001000, // Decimal mode active? + static I = 0b00000100, // Irq disabled? + static Z = 0b00000010, // Zero + static C = 0b00000001, // Carry + } +} + +#[allow(non_snake_case)] +pub struct Machine { + pub A : u8, + pub X : u8, + pub Y : u8, + pub P : ProcessorStatus, + pub SP : u8, + pub PC : u16, +} + +impl Machine { + pub fn new() -> Machine { + Machine { + A: 0, + X: 0, + Y: 0, + SP: 0, + P: ProcessorStatus::empty(), + PC: 0 + } + } +} + +pub struct StackPointer(u8); + +pub static STACK_POINTER_IN_MEMORY_LO : u16 = 0x0100; +pub static STACK_POINTER_IN_MEMORY_HI : u16 = 0x01FF; + +pub struct MemoryAddr(u16); + +pub fn stack_pointer_to_addr(StackPointer(x) : StackPointer) -> MemoryAddr +{ + MemoryAddr(x as u16 + STACK_POINTER_IN_MEMORY_HI) +} + +#[deriving(Show)] +pub enum Instruction + // i/o vars should be listed as follows: + // NV BDIZC A X Y SP PC M + // + // | outputs | inputs +{ ADC // ADd with Carry................ | NV ...ZC A = A + M + C +, AND // logical AND (bitwise)......... | N. ...Z. A = A && M +, ASL // Arithmetic Shift Left......... | N. ...ZC A = M << 1 +, BCC // Branch if Carry Clear......... | .. ..... PC = !C +, BCS // Branch if Carry Set........... | .. ..... PC = C +, BEQ // Branch if Equal (to zero?).... | .. ..... PC = Z +, BIT // BIT test...................... | NV ...Z. = A & M +, BMI // Branch if Minus............... | .. ..... PC = N +, BNE // Branch if Not Equal........... | .. ..... PC = !Z +, BPL // Branch if Positive............ | .. ..... PC = Z +, BRK // BReaK......................... | .. ..... +, BVC // Branch if oVerflow Clear...... | .. ..... +, BVS // Branch if oVerflow Set........ | .. ..... +, CLC // CLear Carry flag.............. | .. ..... +, CLD // Clear Decimal Mode............ | .. ..... +, CLI // Clear Interrupt Disable....... | .. ..... +, CLV // Clear oVerflow flag........... | .. ..... +, CMP // Compare....................... | .. ..... +, CPX // Compare X register............ | .. ..... +, CPY // Compare Y register............ | .. ..... +, DEC // DECrement memory.............. | .. ..... +, DEX // DEcrement X register.......... | .. ..... +, DEY // DEcrement Y register.......... | .. ..... +, EOR // Exclusive OR.................. | .. ..... +, INC // INCrement memory.............. | .. ..... +, INX // INcrement X register.......... | .. ..... +, INY // INcrement Y register.......... | .. ..... +, JMP // JuMP.......................... | .. ..... +, JSR // Jump to SubRoutine............ | .. ..... +, LDA // LoaD Accumulator.............. | .. ..... +, LDX // LoaD X register............... | .. ..... +, LDY // LoaD Y register............... | .. ..... +, LSR // Logical Shift Right........... | .. ..... +, NOP // No OPeration.................. | .. ..... +, ORA // inclusive OR.................. | .. ..... +, PHA // PusH Accumulator.............. | .. ..... +, PHP // PusH Processor status......... | .. ..... +, PLA // PuLl Accumulator.............. | .. ..... +, PLP // PuLl Processor status......... | .. ..... +, ROL // ROtate Left................... | .. ..... +, ROR // ROtate Right.................. | .. ..... +, RTI // ReTurn from Interrupt......... | .. ..... +, RTS // ReTurn from Subroutine........ | .. ..... +, SBC // SuBtract with Carry........... | .. ..... +, SEC // SEt Carry flag................ | .. ..... +, SED // SEt Decimal flag.............. | .. ..... +, SEI // SEt Interrupt disable......... | .. ..... +, STA // STore Accumulator............. | .. ..... +, STX // STore X register.............. | .. ..... +, STY // STore Y register.............. | .. ..... +, TAX // Transfer Accumulator to X..... | .. ..... +, TAY // Transfer Accumulator to Y..... | .. ..... +, TSX // Transfer Stack pointer to X... | .. ..... +, TXA // Transfer X to Accumulator..... | .. ..... +, TXS // Transfer X to Stack pointer... | .. ..... +, TYA // Transfer Y to Accumulator..... | .. ..... +} + +pub enum AddressingMode { + Immediate, + Absolute, + ZeroPage, + Implied, + IndirectAbsolute, + AbsoluteIndexedX, + AbsoluteIndexedY, + ZeroPageIndexedX, + ZeroageIndexedY, + IndexedIndirect, + IndirectIndexed, + Relative, + Accumulator, +} + diff --git a/src/machine6502/lib.rs b/src/machine6502/lib.rs index 754e212..19ebb26 100644 --- a/src/machine6502/lib.rs +++ b/src/machine6502/lib.rs @@ -25,5 +25,5 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -pub mod machine; +pub mod defs; diff --git a/src/machine6502/machine/mod.rs b/src/machine6502/machine/mod.rs deleted file mode 100644 index c1a5f7d..0000000 --- a/src/machine6502/machine/mod.rs +++ /dev/null @@ -1,164 +0,0 @@ -// 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. - -// Abbreviations -// -// Registers -// -// A accumulator -// X general purpose register -// Y general purpose register -// SP stack pointer -// NV-BDIZC processor status flags -- see ProcessorStatus bitflags -// PC program counter -// -// M stands for `Memory location` - -pub bitflags! { - flags ProcessorStatus: u8 { - static N = 0b10000000, // Negative - static V = 0b01000000, // oVerflow - static B = 0b00010000, // Brk - static D = 0b00001000, // Decimal mode active? - static I = 0b00000100, // Irq disabled? - static Z = 0b00000010, // Zero - static C = 0b00000001, // Carry - } -} - -#[allow(non_snake_case)] -pub struct Machine { - pub A : u8, - pub X : u8, - pub Y : u8, - pub SP : u8, - pub P : ProcessorStatus, - pub PC : u16, -} - -impl Machine { - pub fn new() -> Machine { - Machine { - A: 0, - X: 0, - Y: 0, - SP: 0, - P: ProcessorStatus::empty(), - PC: 0 - } - } -} - -pub struct StackPointer(u8); - -pub static STACK_POINTER_IN_MEMORY_LO : u16 = 0x0100; -pub static STACK_POINTER_IN_MEMORY_HI : u16 = 0x01FF; - -pub struct MemoryAddr(u16); - -pub fn stack_pointer_to_addr(StackPointer(x) : StackPointer) -> MemoryAddr -{ - MemoryAddr(x as u16 + STACK_POINTER_IN_MEMORY_HI) -} - -#[deriving(Show)] -pub enum Instruction - // out in -{ ADC // ADd with Carry................ A,Z,C,N = A + M + C -, AND // logical AND................... A,Z,N = A && M -, ASL // Arithmetic Shift Left......... A,Z,C,N = M << 1 -, BCC // Branch if Carry Clear......... _ <- -, BCS // Branch if Carry Set........... -, BEQ // Branch if Equal............... -, BIT // BIT test...................... a & -, BMI // Branch if Minus............... -, BNE // Branch if Not Equal........... -, BPL // Branch if Positive............ -, BRK // BReaK......................... -, BVC // Branch if oVerflow Clear...... -, BVS // Branch if oVerflow Set........ -, CLC // CLear Carry flag.............. -, CLD // Clear Decimal Mode............ -, CLI // Clear Interrupt Disable....... -, CLV // Clear oVerflow flag........... -, CMP // Compare....................... -, CPX // Compare X register............ -, CPY // Compare Y register............ -, DEC // DECrement memory.............. -, DEX // DEcrement X register.......... -, DEY // DEcrement Y register.......... -, EOR // Exclusive OR.................. -, INC // INCrement memory.............. -, INX // INcrement X register.......... -, INY // INcrement Y register.......... -, JMP // JuMP.......................... -, JSR // Jump to SubRoutine............ -, LDA // LoaD Accumulator.............. -, LDX // LoaD X register............... -, LDY // LoaD Y register............... -, LSR // Logical Shift Right........... -, NOP // No OPeration.................. -, ORA // inclusive OR.................. -, PHA // PusH Accumulator.............. -, PHP // PusH Processor status......... -, PLA // PuLl Accumulator.............. -, PLP // PuLl Processor status......... -, ROL // ROtate Left................... -, ROR // ROtate Right.................. -, RTI // ReTurn from Interrupt......... -, RTS // ReTurn from Subroutine........ -, SBC // SuBtract with Carry........... -, SEC // SEt Carry flag................ -, SED // SEt Decimal flag.............. -, SEI // SEt Interrupt disable......... -, STA // STore Accumulator............. -, STX // STore X register.............. -, STY // STore Y register.............. -, TAX // Transfer Accumulator to X..... -, TAY // Transfer Accumulator to Y..... -, TSX // Transfer Stack pointer to X... -, TXA // Transfer X to Accumulator..... -, TXS // Transfer X to Stack pointer... -, TYA // Transfer Y to Accumulator..... -} - -pub enum AddressingMode { - Immediate, - Absolute, - ZeroPage, - Implied, - IndirectAbsolute, - AbsoluteIndexedX, - AbsoluteIndexedY, - ZeroPageIndexedX, - ZeroageIndexedY, - IndexedIndirect, - IndirectIndexed, - Relative, - Accumulator, -} - diff --git a/src/test_exe/main.rs b/src/test_exe/main.rs index d27be44..2903814 100644 --- a/src/test_exe/main.rs +++ b/src/test_exe/main.rs @@ -27,7 +27,7 @@ extern crate machine6502; -use machine6502::machine::Machine; +use machine6502::defs::Machine; fn main() { let _q = Machine::new(); From 0b5bfa8d8bb9115bbac4eae177df528d6e57b1eb Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 08:07:26 -0400 Subject: [PATCH 6/7] move notes into notes folder --- 6502Arch.html => notes/6502Arch.html | 0 Notes.html => notes/Notes.html | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename 6502Arch.html => notes/6502Arch.html (100%) rename Notes.html => notes/Notes.html (100%) diff --git a/6502Arch.html b/notes/6502Arch.html similarity index 100% rename from 6502Arch.html rename to notes/6502Arch.html diff --git a/Notes.html b/notes/Notes.html similarity index 100% rename from Notes.html rename to notes/Notes.html From 3dc2647270ad6c5468e52a1e92e9954ebee7e503 Mon Sep 17 00:00:00 2001 From: Johannes Muenzel Date: Fri, 26 Sep 2014 09:25:06 -0400 Subject: [PATCH 7/7] a bit more commenting for the Instruction enum, and Addr vs AddrDiff distinction --- src/machine6502/defs/mod.rs | 82 ++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/src/machine6502/defs/mod.rs b/src/machine6502/defs/mod.rs index 849cedf..6a1af69 100644 --- a/src/machine6502/defs/mod.rs +++ b/src/machine6502/defs/mod.rs @@ -25,6 +25,8 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +extern crate std; + // Abbreviations // // General @@ -76,19 +78,49 @@ impl Machine { } } +#[deriving(PartialEq, Eq, PartialOrd, Ord)] pub struct StackPointer(u8); -pub static STACK_POINTER_IN_MEMORY_LO : u16 = 0x0100; -pub static STACK_POINTER_IN_MEMORY_HI : u16 = 0x01FF; +#[deriving(PartialEq, Eq, PartialOrd, Ord)] +pub struct Addr(u16); -pub struct MemoryAddr(u16); +#[deriving(PartialEq, Eq, PartialOrd, Ord)] +pub struct AddrDiff(u16); -pub fn stack_pointer_to_addr(StackPointer(x) : StackPointer) -> MemoryAddr -{ - MemoryAddr(x as u16 + STACK_POINTER_IN_MEMORY_HI) +// The idea here is that it doesn't make sense to add two addresses, but it +// does make sense to add an address and an "address-difference". (If this +// is too annoying to work with we should let it go.) + +impl Add for Addr { + fn add(&self, &AddrDiff(rhs): &AddrDiff) -> Addr { + let &Addr(lhs) = self; + + // We probably don't want to overflow when doing arithmetic in our own + // code. + debug_assert!({ + match lhs.checked_add(&rhs) { + None => false, + _ => true + } + }); + + return Addr(lhs + rhs); + } } -#[deriving(Show)] +pub static STACK_POINTER_IN_MEMORY_LO: Addr = Addr(0x0100); +pub static STACK_POINTER_IN_MEMORY_HI: Addr = Addr(0x01FF); + +pub fn stack_pointer_to_addr(StackPointer(x) : StackPointer) -> Addr +{ + STACK_POINTER_IN_MEMORY_LO + AddrDiff(x as u16) +} + +// We can probably come up with a better way to represent address ranges +pub static IRQ_INTERRUPT_VECTOR_LO: Addr = Addr(0xFFFE); +pub static IRQ_INTERRUPT_VECTOR_HI: Addr = Addr(0xFFFE); + +#[deriving(Show, PartialEq, Eq)] pub enum Instruction // i/o vars should be listed as follows: // NV BDIZC A X Y SP PC M @@ -104,24 +136,24 @@ pub enum Instruction , BMI // Branch if Minus............... | .. ..... PC = N , BNE // Branch if Not Equal........... | .. ..... PC = !Z , BPL // Branch if Positive............ | .. ..... PC = Z -, BRK // BReaK......................... | .. ..... -, BVC // Branch if oVerflow Clear...... | .. ..... -, BVS // Branch if oVerflow Set........ | .. ..... -, CLC // CLear Carry flag.............. | .. ..... -, CLD // Clear Decimal Mode............ | .. ..... -, CLI // Clear Interrupt Disable....... | .. ..... -, CLV // Clear oVerflow flag........... | .. ..... -, CMP // Compare....................... | .. ..... -, CPX // Compare X register............ | .. ..... -, CPY // Compare Y register............ | .. ..... -, DEC // DECrement memory.............. | .. ..... -, DEX // DEcrement X register.......... | .. ..... -, DEY // DEcrement Y register.......... | .. ..... -, EOR // Exclusive OR.................. | .. ..... -, INC // INCrement memory.............. | .. ..... -, INX // INcrement X register.......... | .. ..... -, INY // INcrement Y register.......... | .. ..... -, JMP // JuMP.......................... | .. ..... +, BRK // BReaK......................... | .. B.... SP PC = +, BVC // Branch if oVerflow Clear...... | .. ..... PC = !V +, BVS // Branch if oVerflow Set........ | .. ..... PC = V +, CLC // CLear Carry flag.............. | .. ....C = 0 +, CLD // Clear Decimal Mode............ | .. .D... = 0 +, CLI // Clear Interrupt Disable....... | .. ..I.. = 0 +, CLV // Clear oVerflow flag........... | .V ..... = 0 +, CMP // Compare....................... | N. ...ZC = A - M +, CPX // Compare X register............ | N. ...ZC = X - M +, CPY // Compare Y register............ | N. ...ZC = Y - M +, DEC // DECrement memory.............. | N. ...Z. M = M - 1 +, DEX // DEcrement X register.......... | N. ...Z. X = X - 1 +, DEY // DEcrement Y register.......... | N. ...Z. Y = Y - 1 +, EOR // Exclusive OR (bitwise)........ | N. ...Z. A = A ^ M +, INC // INCrement memory.............. | N. ...Z. M = M + 1 +, INX // INcrement X register.......... | N. ...Z. X = X + 1 +, INY // INcrement Y register.......... | N. ...Z. Y = Y + 1 +, JMP // JuMP.......................... | .. ..... PC = , JSR // Jump to SubRoutine............ | .. ..... , LDA // LoaD Accumulator.............. | .. ..... , LDX // LoaD X register............... | .. .....