1
0
mirror of https://github.com/mre/mos6502.git synced 2024-06-08 14:29:34 +00:00

Add range_incl.rs and use range_incl() in the tests.

This commit is contained in:
Andrew Keeton 2015-01-17 19:47:58 -05:00
parent 7210ee7d71
commit 6520983de4
3 changed files with 46 additions and 8 deletions

View File

@ -33,4 +33,5 @@ pub mod address;
pub mod instruction;
pub mod machine;
pub mod memory;
pub mod range_incl;
pub mod registers;

View File

@ -31,6 +31,7 @@ use address::{Address, AddressDiff};
use instruction;
use instruction::{DecodedInstr, Instruction, OpInput};
use memory::Memory;
use range_incl::range_incl;
use registers::{ Registers, StackPointer, Status, StatusArgs };
use registers::{ PS_NEGATIVE, PS_DECIMAL_MODE, PS_OVERFLOW, PS_ZERO, PS_CARRY,
PS_DISABLE_INTERRUPTS };
@ -1230,13 +1231,13 @@ fn compare_with_y_register_test() {
fn exclusive_or_test() {
let mut machine = Machine::new();
for a_before in 0us..256 {
for val in 0us..256 {
for a_before in range_incl(0u8, 255u8) {
for val in range_incl(0u8, 255u8) {
machine.execute_instruction(
(Instruction::LDA, OpInput::UseImmediate(a_before as u8))
(Instruction::LDA, OpInput::UseImmediate(a_before))
);
machine.exclusive_or(val as u8);
machine.exclusive_or(val);
let a_after = a_before ^ val;
assert_eq!(machine.registers.accumulator, a_after as i8);
@ -1260,13 +1261,13 @@ fn exclusive_or_test() {
fn inclusive_or_test() {
let mut machine = Machine::new();
for a_before in 0us..256 {
for val in 0us..256 {
for a_before in range_incl(0u8, 255u8) {
for val in range_incl(0u8, 255u8) {
machine.execute_instruction(
(Instruction::LDA, OpInput::UseImmediate(a_before as u8))
(Instruction::LDA, OpInput::UseImmediate(a_before))
);
machine.inclusive_or(val as u8);
machine.inclusive_or(val);
let a_after = a_before | val;
assert_eq!(machine.registers.accumulator, a_after as i8);

36
src/range_incl.rs Normal file
View File

@ -0,0 +1,36 @@
#![allow(unstable)]
use std::num::Int;
pub struct RangeIncl<T: Int> {
state: Option<T>,
end: T,
}
pub fn range_incl<T: Int>(begin: T, end: T) -> RangeIncl<T> {
RangeIncl { state: Some(begin), end: end }
}
trait One : Int {
fn my_one(_: Option<Self>) -> Self {
Int::one()
}
}
impl<T> One for T where T: Int {}
impl<T: Int> Iterator for RangeIncl<T> {
type Item = T;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
match self.state {
Some(current) => {
self.state =
if current == self.end { None }
else { Some(current + One::my_one(None::<T>)) };
Some(current)
},
None => {
None
}
}
}
}