From bc1687c247affd88b36d552bd827ba340dd08ebc Mon Sep 17 00:00:00 2001 From: Andrew Keeton Date: Thu, 16 Oct 2014 19:38:00 -0400 Subject: [PATCH] Make Address take i32 so relative addresses work. --- src/address.rs | 4 ++-- src/instruction.rs | 6 +++--- src/machine.rs | 2 +- src/registers.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/address.rs b/src/address.rs index 35dae9b..e2f2bac 100644 --- a/src/address.rs +++ b/src/address.rs @@ -29,7 +29,7 @@ // does make sense to add an address and an "address-difference". (If this // is too annoying to work with we should let it go.) #[deriving(PartialEq, Eq, PartialOrd, Ord, Show)] -pub struct AddressDiff(pub u16); +pub struct AddressDiff(pub i32); #[deriving(PartialEq, Eq, PartialOrd, Ord, Show)] pub struct Address(pub u16); @@ -37,7 +37,7 @@ pub struct Address(pub u16); impl Add for Address { fn add(&self, &AddressDiff(rhs): &AddressDiff) -> Address { let &Address(lhs) = self; - Address(lhs + rhs) + Address(((lhs as i32) + rhs) as u16) } } diff --git a/src/instruction.rs b/src/instruction.rs index 9112db2..7671d2d 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -216,12 +216,12 @@ impl AddressingMode { AbsoluteX => { // Use [u8, ..2] from instruction as address, add X // (Output: a 16-bit address) - UseAddress(arr_to_addr(arr) + AddressDiff(x as u16)) + UseAddress(arr_to_addr(arr) + AddressDiff(x as i32)) }, AbsoluteY => { // Use [u8, ..2] from instruction as address, add Y // (Output: a 16-bit address) - UseAddress(arr_to_addr(arr) + AddressDiff(y as u16)) + UseAddress(arr_to_addr(arr) + AddressDiff(y as i32)) }, Indirect => { // Use [u8, ..2] from instruction as an address. Interpret the @@ -248,7 +248,7 @@ impl AddressingMode { let start = arr[0]; let slice = memory.get_slice(Address(start as u16), AddressDiff(2)); - UseAddress(arr_to_addr(slice) + AddressDiff(y as u16)) + UseAddress(arr_to_addr(slice) + AddressDiff(y as i32)) }, } } diff --git a/src/machine.rs b/src/machine.rs index df23a32..0591769 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -29,7 +29,7 @@ use log; use std; -use address::{AddressDiff}; +use address::{Address, AddressDiff}; use instruction; use instruction::{DecodedInstr}; use memory::Memory; diff --git a/src/registers.rs b/src/registers.rs index ca68215..bdbc684 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -118,7 +118,7 @@ pub struct StackPointer(pub u8); impl StackPointer { pub fn to_address(&StackPointer(sp): &StackPointer) -> Address { - STACK_ADDRESS_LO + AddressDiff(sp as u16) + STACK_ADDRESS_LO + AddressDiff(sp as i32) } }