Skip to content

Commit 91e45bc

Browse files
committed
read addresses from debug info
1 parent b9a086e commit 91e45bc

File tree

6 files changed

+128
-26
lines changed

6 files changed

+128
-26
lines changed

tests/Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
gumdrop = "0.8.1"
78
rusticnes-core = { git = "https://github.com/zeta0134/rusticnes-core.git", version = "0.2.0" }

tests/src/labels.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
use std::collections::HashMap;
22
use std::sync::OnceLock;
33

4+
fn parse_debug_line(line: &str) -> (String, u16) {
5+
let pairs: Vec<&str> = line.split(',').collect();
6+
7+
let mut name = String::new();
8+
let mut val = 0;
9+
10+
for pair in pairs {
11+
let key_val: Vec<&str> = pair.split('=').collect();
12+
if key_val.len() == 2 {
13+
let key = key_val[0].trim();
14+
let value = key_val[1].trim();
15+
16+
match key {
17+
"name" => name = value.trim_matches('"').to_string(),
18+
"val" => {
19+
if let Ok(hex_value) = u16::from_str_radix(value.trim_start_matches("0x"), 16) {
20+
val = hex_value;
21+
}
22+
}
23+
_ => {}
24+
}
25+
}
26+
}
27+
28+
(name, val)
29+
}
30+
431
fn labels() -> &'static HashMap<String, u16> {
532
static LABELS: OnceLock<HashMap<String, u16>> = OnceLock::new();
633
LABELS.get_or_init(|| {
7-
let text = include_str!("../../tetris.lbl");
34+
let text = include_str!("../../tetris.dbg");
835
let mut labels: HashMap<String, u16> = HashMap::new();
936

1037
for line in text.lines() {
11-
let parts: Vec<&str> = line.split_whitespace().collect();
12-
if parts.len() >= 2 {
13-
if let Ok(hex_value) = u16::from_str_radix(parts[1], 16) {
14-
labels.insert(parts[2].to_string(), hex_value);
15-
}
38+
let (name, val) = parse_debug_line(line);
39+
40+
if !name.is_empty() {
41+
labels.insert(name, val);
1642
}
1743
}
1844

@@ -23,15 +49,14 @@ fn labels() -> &'static HashMap<String, u16> {
2349
fn addrs() -> &'static HashMap<u16, String> {
2450
static LABELS: OnceLock<HashMap<u16, String>> = OnceLock::new();
2551
LABELS.get_or_init(|| {
26-
let text = include_str!("../../tetris.lbl");
52+
let text = include_str!("../../tetris.dbg");
2753
let mut labels: HashMap<u16, String> = HashMap::new();
2854

2955
for line in text.lines() {
30-
let parts: Vec<&str> = line.split_whitespace().collect();
31-
if parts.len() >= 2 {
32-
if let Ok(hex_value) = u16::from_str_radix(parts[1], 16) {
33-
labels.insert(hex_value, parts[2].to_string());
34-
}
56+
let (name, val) = parse_debug_line(line);
57+
58+
if !name.is_empty() {
59+
labels.insert(val, name);
3560
}
3661
}
3762

tests/src/main.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
mod labels;
22
mod util;
3-
4-
// tests
53
mod pushdown;
64

5+
use gumdrop::Options;
6+
7+
#[derive(Debug, Options)]
8+
struct TestOptions {
9+
help: bool,
10+
#[options(help="run tests")]
11+
test: bool,
12+
}
13+
714
fn main() {
8-
pushdown::test();
15+
let options = TestOptions::parse_args_default_or_exit();
16+
17+
if options.test {
18+
pushdown::test();
19+
println!("pushdown works!");
20+
}
21+
22+
let mut emu = util::emulator(None);
23+
24+
emu.memory.iram_raw[labels::get("practiseType") as usize] = labels::get("MODE_SEED") as u8;
25+
926
}

tests/src/pushdown.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ pub fn test() {
44
let mut emu = util::emulator(None);
55

66
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;
7+
[0..1000, 24500..25500].into_iter().for_each(|range| {
8+
for score in range {
9+
util::set_score(&mut emu, score);
1010

11-
util::set_score(&mut emu, score);
12-
util::run_to_return(&mut emu, false);
11+
emu.registers.pc = labels::get("addPushDownPoints");
12+
emu.memory.iram_raw[labels::get("holdDownPoints") as usize] = pushdown;
1313

14-
let reference = pushdown_impl(pushdown, score as u16) as u32;
14+
util::run_to_return(&mut emu, false);
1515

16-
assert_eq!(reference, util::get_score(&mut emu) - score);
17-
}
16+
let reference = pushdown_impl(pushdown, score as u16) as u32;
17+
18+
assert_eq!(reference, util::get_score(&mut emu) - score);
19+
}
20+
});
1821
}
1922
}
2023

tests/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub fn print_step(emu: &mut NesState) {
4141
}
4242

4343
pub fn set_score(emu: &mut NesState, score: u32) {
44-
let score_addr = labels::get(".score");
45-
let binscore_addr = labels::get(".binScore");
44+
let score_addr = labels::get("score");
45+
let binscore_addr = labels::get("binScore");
4646
let bcd_str = format!("{:06}", score);
4747
let bcd_a = i64::from_str_radix(&bcd_str[0..2], 16).unwrap();
4848
let bcd_b = i64::from_str_radix(&bcd_str[2..4], 16).unwrap();
@@ -57,7 +57,7 @@ pub fn set_score(emu: &mut NesState, score: u32) {
5757
}
5858

5959
pub fn get_score(emu: &mut NesState) -> u32 {
60-
let binscore_addr = labels::get(".binScore");
60+
let binscore_addr = labels::get("binScore");
6161
emu.memory.iram_raw[binscore_addr as usize] as u32
6262
+ ((emu.memory.iram_raw[(binscore_addr + 1) as usize] as u32) << 8)
6363
+ ((emu.memory.iram_raw[(binscore_addr + 2) as usize] as u32) << 16)

0 commit comments

Comments
 (0)