From 1f896fb4e943d6a3c064c5fd38e868c09ca604c6 Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Sat, 27 Sep 2014 21:32:41 -0400 Subject: [PATCH] Try to split into modules. --- 6502emu/src/address.rs | 55 ++++++++++++ 6502emu/src/machine/mod.rs | 57 ++++++++++++ 6502emu/src/main.rs | 169 ++++++----------------------------- 6502emu/src/memory.rs | 69 ++++++++++++++ 6502emu/src/registers/mod.rs | 65 ++++++++++++++ 5 files changed, 274 insertions(+), 141 deletions(-) create mode 100644 6502emu/src/address.rs create mode 100644 6502emu/src/machine/mod.rs create mode 100644 6502emu/src/memory.rs create mode 100644 6502emu/src/registers/mod.rs diff --git a/6502emu/src/address.rs b/6502emu/src/address.rs new file mode 100644 index 0000000..55b8988 --- /dev/null +++ b/6502emu/src/address.rs @@ -0,0 +1,55 @@ +// 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. + +mod address { + #[deriving(PartialEq)] + #[deriving(Eq)] + #[deriving(PartialOrd)] + #[deriving(Ord)] + pub struct Address(u16); + + impl Address { + /* TODO akeeton: Hide struct Address(u16) "constructor." + pub fn new(address_: u16) -> Address { + Address(address_) + } + */ + + pub fn to_int(&self) -> u16 { + match *self { + Address(address_) => address_ + } + } + + pub fn min() -> Address { Address(0x0100) } + pub fn max() -> Address { Address(0x01ff) } + + pub fn is_valid(&self) -> bool { + Address::min() <= *self && *self <= Address::max() + } + } +} diff --git a/6502emu/src/machine/mod.rs b/6502emu/src/machine/mod.rs new file mode 100644 index 0000000..7e714cb --- /dev/null +++ b/6502emu/src/machine/mod.rs @@ -0,0 +1,57 @@ +// 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. + +mod registers; +mod address; +mod memory; + +struct Machine { + registers: Registers, + memory: Memory +} + +impl Machine { + // TODO akeeton: Implement binary-coded decimal. + pub fn add_with_carry(&mut self, value: i8) { + let a: int = self.registers.accumulator as int; + let c: int = self.registers.status.carry as int; + + let a_new_full: int = a + c + value as int; + let a_new: i8 = a_new_full as i8; + + self.registers.accumulator = a_new; + self.registers.status.carry = if a_new_full == a_new as int { 0 } else { 1 }; + self.registers.status.zero = if a_new == 0 { 1 } else { 0 }; + self.registers.status.sign = if a_new < 0 { 1 } else { 0 }; + self.registers.status.overflow = + if (a < 0 && value < 0 && a_new >= 0) + || (a > 0 && value > 0 && a_new <= 0) + { 1 } + else + { 0 } + } +} \ No newline at end of file diff --git a/6502emu/src/main.rs b/6502emu/src/main.rs index 14158cb..7ba4dc4 100644 --- a/6502emu/src/main.rs +++ b/6502emu/src/main.rs @@ -1,145 +1,32 @@ -mod Registers { - // Each status flag should be 0 or 1. - pub struct Status { - pub carry: u8, - pub zero: u8, - pub interrupt: u8, - pub decimal_mode: u8, - pub brk: u8, - pub unused: u8, - pub overflow: u8, - pub sign: u8 - } +// 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. - impl Status { - pub fn to_byte(&self) -> u8 { - self.carry << 0 - | self.zero << 1 - | self.interrupt << 2 - | self.decimal_mode << 3 - | self.brk << 4 - | self.unused << 5 - | self.overflow << 6 - | self.sign << 7 - } - } - - pub struct Registers { - pub accumulator: i8, - pub index_x: i8, - pub index_y: i8, - pub stack_pointer: u8, - pub program_counter: u16, - pub status: Status - } - - impl Registers { - } -} - -mod Address { - #[deriving(PartialEq)] - #[deriving(Eq)] - #[deriving(PartialOrd)] - #[deriving(Ord)] - pub struct Address(u16); - - impl Address { - /* TODO akeeton: Hide struct Address(u16) "constructor." - pub fn new(address_: u16) -> Address { - Address(address_) - } - */ - - pub fn to_int(&self) -> u16 { - match *self { - Address(address_) => address_ - } - } - - pub fn min() -> Address { Address(0x0100) } - pub fn max() -> Address { Address(0x01ff) } - - pub fn is_valid(&self) -> bool { - Address::min() <= *self && *self <= Address::max() - } - } -} - -mod Memory { - use Address::Address; - - pub struct Memory { - bytes: [u8,.. 256] - } - - impl Memory { - fn address_to_byte_offset(address: &Address) -> uint { - (address.to_int() - Address::min().to_int()) as uint - } - - pub fn get_byte(&self, address: &Address) -> u8 { - if !address.is_valid() - { - fail!("Invalid address."); - } - else - { - return self.bytes[Memory::address_to_byte_offset(address)]; - } - } - - // Sets the byte at the given address to the given value and returns the - // previous value at the address. - pub fn set_byte(&mut self, address: &Address, value: u8) -> u8 { - if !address.is_valid() - { - fail!("Invalid address."); - } - else - { - let old_value = self.get_byte(address); - self.bytes[Memory::address_to_byte_offset(address)] = value; - - return old_value; - } - } - } -} - -mod Machine { - use Registers::Registers; - use Address::Address; - use Memory::Memory; - - struct Machine { - registers: Registers, - memory: Memory - } - - impl Machine { - // TODO akeeton: Implement binary-coded decimal. - pub fn add_with_carry(&mut self, value: i8) { - let a: int = self.registers.accumulator as int; - let c: int = self.registers.status.carry as int; - - let a_new_full: int = a + c + value as int; - let a_new: i8 = a_new_full as i8; - - self.registers.accumulator = a_new; - self.registers.status.carry = if a_new_full == a_new as int { 0 } else { 1 }; - self.registers.status.zero = if a_new == 0 { 1 } else { 0 }; - self.registers.status.sign = if a_new < 0 { 1 } else { 0 }; - self.registers.status.overflow = - if (a < 0 && value < 0 && a_new >= 0) - || (a > 0 && value > 0 && a_new <= 0) - { 1 } - else - { 0 } - } - } -} +mod machine; fn main() { - println!("Hello, world!") + let machine = machine::Machine::new(); } diff --git a/6502emu/src/memory.rs b/6502emu/src/memory.rs new file mode 100644 index 0000000..1686f84 --- /dev/null +++ b/6502emu/src/memory.rs @@ -0,0 +1,69 @@ +// 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. + +mod address; + +mod memory { + use address::Address; + + pub struct Memory { + bytes: [u8,.. 256] + } + + impl Memory { + fn address_to_byte_offset(address: &Address) -> uint { + (address.to_int() - Address::min().to_int()) as uint + } + + pub fn get_byte(&self, address: &Address) -> u8 { + if !address.is_valid() + { + fail!("Invalid address."); + } + else + { + return self.bytes[Memory::address_to_byte_offset(address)]; + } + } + + // Sets the byte at the given address to the given value and returns the + // previous value at the address. + pub fn set_byte(&mut self, address: &Address, value: u8) -> u8 { + if !address.is_valid() + { + fail!("Invalid address."); + } + else + { + let old_value = self.get_byte(address); + self.bytes[Memory::address_to_byte_offset(address)] = value; + + return old_value; + } + } + } +} diff --git a/6502emu/src/registers/mod.rs b/6502emu/src/registers/mod.rs new file mode 100644 index 0000000..5b89163 --- /dev/null +++ b/6502emu/src/registers/mod.rs @@ -0,0 +1,65 @@ +// 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. + +mod registers { + // Each status flag should be 0 or 1. + pub struct Status { + pub carry: u8, + pub zero: u8, + pub interrupt: u8, + pub decimal_mode: u8, + pub brk: u8, + pub unused: u8, + pub overflow: u8, + pub sign: u8 + } + + impl Status { + pub fn to_byte(&self) -> u8 { + self.carry << 0 + | self.zero << 1 + | self.interrupt << 2 + | self.decimal_mode << 3 + | self.brk << 4 + | self.unused << 5 + | self.overflow << 6 + | self.sign << 7 + } + } + + pub struct Registers { + pub accumulator: i8, + pub index_x: i8, + pub index_y: i8, + pub stack_pointer: u8, + pub program_counter: u16, + pub status: Status + } + + impl Registers { + } +} \ No newline at end of file