Skip to content

Commit b9a086e

Browse files
committed
add pushdown points test
1 parent 79686df commit b9a086e

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

tests/src/pushdown.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
use crate::{labels, util};
2+
3+
pub fn test() {
4+
let mut emu = util::emulator(None);
5+
6+
for pushdown in 2..15 {
7+
for score in 24500..25500 {
8+
emu.registers.pc = labels::get(".addPushDownPoints");
9+
emu.memory.iram_raw[labels::get(".holdDownPoints") as usize] = pushdown;
10+
11+
util::set_score(&mut emu, score);
12+
util::run_to_return(&mut emu, false);
13+
14+
let reference = pushdown_impl(pushdown, score as u16) as u32;
15+
16+
assert_eq!(reference, util::get_score(&mut emu) - score);
17+
}
18+
}
19+
}
20+
121
// reference implementation - tested against the original game
2-
fn pushdown(pushdown: u8, score: u16) -> u16 {
22+
// may seem weird - designed to be translated to assembly
23+
fn pushdown_impl(pushdown: u8, score: u16) -> u16 {
324
let ones = score % 10;
425
let hundredths = score % 100;
526
let mut newscore = ones as u8 + (pushdown - 1);
@@ -17,15 +38,5 @@ fn pushdown(pushdown: u8, score: u16) -> u16 {
1738
newscore = nextscore;
1839
}
1940

20-
newscore + (score-hundredths) - score
21-
}
22-
23-
use crate::{ labels, util };
24-
25-
pub fn test() {
26-
let mut emu = util::emulator(None);
27-
28-
emu.registers.pc = labels::get(".addPushDownPoints");
29-
30-
util::run_to_return(&mut emu);
41+
newscore + (score - hundredths) - score
3142
}

tests/src/util.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ pub fn emulator(rom: Option<&[u8]>) -> NesState {
1111
emu
1212
}
1313

14-
pub fn run_to_return(emu: &mut NesState) {
14+
pub fn run_to_return(emu: &mut NesState, print: bool) {
1515
opcodes::push(emu, 0);
1616
opcodes::push(emu, 0);
1717

1818
loop {
19-
print_step(emu);
19+
if print {
20+
print_step(emu);
21+
} else {
22+
emu.step();
23+
}
2024

2125
if emu.registers.pc < 3 {
2226
break;
@@ -35,3 +39,26 @@ pub fn print_step(emu: &mut NesState) {
3539

3640
println!("{}", opcode_info::disassemble_instruction(emu.cpu.opcode, 0, 0).0);
3741
}
42+
43+
pub fn set_score(emu: &mut NesState, score: u32) {
44+
let score_addr = labels::get(".score");
45+
let binscore_addr = labels::get(".binScore");
46+
let bcd_str = format!("{:06}", score);
47+
let bcd_a = i64::from_str_radix(&bcd_str[0..2], 16).unwrap();
48+
let bcd_b = i64::from_str_radix(&bcd_str[2..4], 16).unwrap();
49+
let bcd_c = i64::from_str_radix(&bcd_str[4..6], 16).unwrap();
50+
51+
emu.memory.iram_raw[(score_addr + 2) as usize] = bcd_a as u8;
52+
emu.memory.iram_raw[(score_addr + 1) as usize] = bcd_b as u8;
53+
emu.memory.iram_raw[score_addr as usize] = bcd_c as u8;
54+
emu.memory.iram_raw[binscore_addr as usize] = score as u8;
55+
emu.memory.iram_raw[(binscore_addr + 1) as usize] = (score >> 8) as u8;
56+
emu.memory.iram_raw[(binscore_addr + 2) as usize] = (score >> 16) as u8;
57+
}
58+
59+
pub fn get_score(emu: &mut NesState) -> u32 {
60+
let binscore_addr = labels::get(".binScore");
61+
emu.memory.iram_raw[binscore_addr as usize] as u32
62+
+ ((emu.memory.iram_raw[(binscore_addr + 1) as usize] as u32) << 8)
63+
+ ((emu.memory.iram_raw[(binscore_addr + 2) as usize] as u32) << 16)
64+
}

0 commit comments

Comments
 (0)