Skip to content

Commit 64b14f1

Browse files
committed
do some cycle counting
1 parent d2aafc0 commit 64b14f1

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

tests/src/cycle_count.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::{util, score, labels};
2+
3+
pub fn count_cycles() {
4+
let mut emu = util::emulator(None);
5+
6+
let completed_lines = labels::get("completedLines") as usize;
7+
let add_points = labels::get("addPointsRaw");
8+
let level_number = labels::get("levelNumber") as usize;
9+
10+
let mut score = move |score: u32, lines: u8, level: u8| {
11+
score::set(&mut emu, score);
12+
emu.registers.pc = add_points;
13+
emu.memory.iram_raw[completed_lines] = lines;
14+
emu.memory.iram_raw[level_number] = level;
15+
util::cycles_to_return(&mut emu)
16+
};
17+
18+
let mut highest = 0;
19+
20+
// check every linecount on every level
21+
for level in 0..=255 {
22+
for lines in 0..=4 {
23+
let count = score(999999, lines, level);
24+
25+
if count > highest {
26+
highest = count;
27+
}
28+
}
29+
}
30+
31+
println!("scoring routine most cycles: {}", highest);
32+
}

tests/src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod block;
2+
mod cycle_count;
23
mod drought;
34
mod input;
45
mod labels;
@@ -20,6 +21,8 @@ struct TestOptions {
2021
help: bool,
2122
#[options(help = "run tests")]
2223
test: bool,
24+
#[options(help = "count cycles")]
25+
cycles: bool,
2326
#[options(help = "set SPS seed", parse(try_from_str = "parse_hex"))]
2427
sps_seed: u32,
2528
#[options(help = "print SPS pieces")]
@@ -48,6 +51,11 @@ fn main() {
4851
println!("sps is the same!");
4952
}
5053

54+
// count cycles
55+
if options.cycles {
56+
cycle_count::count_cycles();
57+
}
58+
5159
// print SPS sequences
5260
if options.sps_qty > 0 {
5361
let mut blocks = sps::SPS::new();

tests/src/score.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn test() {
5151
}
5252

5353
// check tetris value from lots of scores
54-
for initial_score in 999000..=1001000 {
54+
for initial_score in 999499..=1000501 {
5555
assert_eq!(score(initial_score, 4, 18), initial_score + score_impl(4, 18));
5656
}
5757
}

tests/src/util.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ pub fn run_to_return(emu: &mut NesState, print: bool) {
3131
opcodes::pop(emu);
3232
}
3333

34+
pub fn cycles_to_return(emu: &mut NesState) -> u32 {
35+
opcodes::push(emu, 0);
36+
opcodes::push(emu, 0);
37+
38+
let mut cycles = 0;
39+
40+
loop {
41+
cycles += 1;
42+
emu.cycle();
43+
44+
if emu.registers.pc < 3 {
45+
break;
46+
}
47+
}
48+
49+
opcodes::pop(emu);
50+
opcodes::pop(emu);
51+
52+
cycles
53+
}
54+
3455
pub fn print_step(emu: &mut NesState) {
3556
if let Some(label) = labels::from_addr(emu.registers.pc) {
3657
println!("{}:", label);
@@ -64,3 +85,20 @@ pub const fn _ppu_addr_to_xy(ppu_addr: u16) -> (u8, u8) {
6485

6586
(x as _, y as _)
6687
}
88+
89+
pub const fn _xy_to_ppu_addr(x: u16, y: u16) -> u16 {
90+
const SCREEN_WIDTH: u16 = 256 / 8;
91+
const SCREEN_HEIGHT: u16 = 240 / 8;
92+
93+
let offset = (y * 32) + x;
94+
95+
let base_address = match (x / SCREEN_WIDTH, y / SCREEN_HEIGHT) {
96+
(0, 0) => 0x2000,
97+
(1, 0) => 0x2400,
98+
(0, 1) => 0x2800,
99+
(1, 1) => 0x2C00,
100+
_ => panic!("Invalid (x, y) position"),
101+
};
102+
103+
base_address + offset
104+
}

0 commit comments

Comments
 (0)