1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::{NesError, NesResult};
use anyhow::anyhow;
use enum_dispatch::enum_dispatch;
use serde::{Deserialize, Serialize};
use std::fmt::Write;
#[cfg(not(target_arch = "wasm32"))]
use std::path::{Path, PathBuf};

pub const CONFIG_DIR: &str = ".config/tetanes";
pub const SAVE_DIR: &str = "save";
pub const SRAM_DIR: &str = "sram";

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[must_use]
pub enum NesRegion {
    #[default]
    Ntsc,
    Pal,
    Dendy,
}

impl NesRegion {
    pub const fn as_slice() -> &'static [Self] {
        &[NesRegion::Ntsc, NesRegion::Pal, NesRegion::Dendy]
    }
}

impl AsRef<str> for NesRegion {
    fn as_ref(&self) -> &str {
        match self {
            Self::Ntsc => "NTSC",
            Self::Pal => "PAL",
            Self::Dendy => "Dendy",
        }
    }
}

impl TryFrom<&str> for NesRegion {
    type Error = NesError;

    fn try_from(value: &str) -> NesResult<Self> {
        match value {
            "NTSC" => Ok(Self::Ntsc),
            "PAL" => Ok(Self::Pal),
            "Dendy" => Ok(Self::Dendy),
            _ => Err(anyhow!("invalid nes region")),
        }
    }
}

impl From<usize> for NesRegion {
    fn from(value: usize) -> Self {
        match value {
            1 => Self::Pal,
            2 => Self::Dendy,
            _ => Self::Ntsc,
        }
    }
}

#[enum_dispatch(Mapper)]
pub trait Regional {
    fn region(&self) -> NesRegion {
        NesRegion::default()
    }
    fn set_region(&mut self, _region: NesRegion) {}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[must_use]
pub enum Kind {
    Soft,
    Hard,
}

#[enum_dispatch(Mapper)]
pub trait Reset {
    fn reset(&mut self, _kind: Kind) {}
}

#[enum_dispatch(Mapper)]
pub trait Clock {
    fn clock(&mut self) -> usize {
        0
    }
    fn clock_to(&mut self, _clocks: u64) {}
}

#[macro_export]
macro_rules! hashmap {
    { $($key:expr => $value:expr),* $(,)? } => {{
        let mut m = ::std::collections::HashMap::new();
        $(
            m.insert($key, $value);
        )*
        m
    }};
    ($hm:ident, { $($key:expr => $value:expr),* $(,)? } ) => ({
        $(
            $hm.insert($key, $value);
        )*
    });
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn config_dir() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("./"))
        .join(CONFIG_DIR)
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn config_path<P: AsRef<Path>>(path: P) -> PathBuf {
    config_dir().join(path)
}

/// Prints a hex dump of a given byte array starting at `addr_offset`.
#[must_use]
pub fn hexdump(data: &[u8], addr_offset: usize) -> Vec<String> {
    use std::cmp;

    let mut addr = 0;
    let len = data.len();
    let mut last_line_same = false;
    let mut output = Vec::new();

    let mut last_line = String::with_capacity(80);
    while addr <= len {
        let end = cmp::min(addr + 16, len);
        let line_data = &data[addr..end];
        let line_len = line_data.len();

        let mut line = String::with_capacity(80);
        for byte in line_data.iter() {
            let _ = write!(line, " {byte:02X}");
        }

        if line_len % 16 > 0 {
            let words_left = (16 - line_len) / 2;
            for _ in 0..3 * words_left {
                line.push(' ');
            }
        }

        if line_len > 0 {
            line.push_str("  |");
            for c in line_data {
                if (*c as char).is_ascii() && !(*c as char).is_control() {
                    let _ = write!(line, "{}", (*c as char));
                } else {
                    line.push('.');
                }
            }
            line.push('|');
        }
        if last_line == line {
            if !last_line_same {
                last_line_same = true;
                output.push("*".to_string());
            }
        } else {
            last_line_same = false;
            output.push(format!("{:08x} {}", addr + addr_offset, line));
        }
        last_line = line;

        addr += 16;
    }
    output
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::{
        control_deck::ControlDeck,
        input::Slot,
        mapper::{Mapper, MapperRevision},
        nes::event::{Action, NesState, Setting},
        ppu::Ppu,
        video::VideoFilter,
    };
    use anyhow::Context;
    use once_cell::sync::Lazy;
    use pix_engine::prelude::{Image, PixelFormat};
    use serde::{Deserialize, Serialize};
    use std::fmt::Write;
    use std::{
        collections::hash_map::DefaultHasher,
        env,
        fs::{self, File},
        hash::{Hash, Hasher},
        io::{BufReader, BufWriter},
        path::{Path, PathBuf},
    };

    pub(crate) const RESULT_DIR: &str = "test_results";

    static INIT_TESTS: Lazy<bool> = Lazy::new(|| {
        let result_dir = PathBuf::from(RESULT_DIR);
        if result_dir.exists() {
            fs::remove_dir_all(result_dir).expect("cleared test results dir");
        }
        true
    });
    static PASS_DIR: Lazy<PathBuf> = Lazy::new(|| {
        let directory = PathBuf::from(RESULT_DIR).join("pass");
        fs::create_dir_all(&directory).expect("created pass test results dir");
        directory
    });
    static FAIL_DIR: Lazy<PathBuf> = Lazy::new(|| {
        let directory = PathBuf::from(RESULT_DIR).join("fail");
        fs::create_dir_all(&directory).expect("created fail test results dir");
        directory
    });

    #[macro_export]
    macro_rules! test_roms {
        ($directory:expr, $( $(#[ignore = $reason:expr])? $test:ident ),* $(,)?) => {$(
            $(#[ignore = $reason])?
            #[test]
            fn $test() {
                $crate::common::tests::test_rom($directory, stringify!($test));
            }
        )*};
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[must_use]
    struct TestFrame {
        number: u32,
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        hash: Option<u64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        slot: Option<Slot>,
        #[serde(skip_serializing_if = "Option::is_none")]
        action: Option<Action>,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[must_use]
    struct RomTest {
        name: String,
        frames: Vec<TestFrame>,
    }

    fn get_rom_tests(directory: &str) -> (PathBuf, Vec<RomTest>) {
        let file = PathBuf::from(directory)
            .join("tests")
            .with_extension("json");
        let tests = File::open(&file)
            .and_then(|file| {
                Ok(serde_json::from_reader::<_, Vec<RomTest>>(BufReader::new(
                    file,
                ))?)
            })
            .expect("valid rom test data");
        (file, tests)
    }

    fn load_control_deck<P: AsRef<Path>>(path: P) -> ControlDeck {
        let path = path.as_ref();
        let mut rom = BufReader::new(File::open(path).expect("failed to open path"));
        let mut deck = ControlDeck::default();
        deck.load_rom(&path.to_string_lossy(), &mut rom)
            .expect("failed to load rom");
        deck.set_filter(VideoFilter::Pixellate);
        deck.set_region(NesRegion::Ntsc);
        deck
    }

    fn handle_frame_action(test_frame: &TestFrame, deck: &mut ControlDeck) {
        if let Some(action) = test_frame.action {
            log::debug!("{:?}", action);
            match action {
                Action::Nes(state) => match state {
                    NesState::SoftReset => deck.reset(Kind::Soft),
                    NesState::HardReset => deck.reset(Kind::Hard),
                    NesState::MapperRevision(board) => match board {
                        MapperRevision::Mmc3(revision) => {
                            if let Mapper::Txrom(ref mut mapper) = deck.mapper_mut() {
                                mapper.set_revision(revision);
                            }
                        }
                        _ => panic!("unhandled MapperRevision {board:?}"),
                    },
                    _ => panic!("unhandled Nes state: {state:?}"),
                },
                Action::Setting(setting) => match setting {
                    Setting::SetVideoFilter(filter) => deck.set_filter(filter),
                    Setting::SetNesFormat(format) => deck.set_region(format),
                    _ => panic!("unhandled Setting: {setting:?}"),
                },
                Action::Joypad(button) => {
                    let slot = test_frame.slot.unwrap_or(Slot::One);
                    let joypad = deck.joypad_mut(slot);
                    joypad.set_button(button.into(), true);
                }
                _ => (),
            }
        }
    }

    fn handle_snapshot(
        test: &str,
        test_frame: &TestFrame,
        deck: &mut ControlDeck,
        count: usize,
    ) -> Option<(u64, u64, u32, PathBuf)> {
        test_frame.hash.map(|expected| {
            let mut hasher = DefaultHasher::new();
            let frame = deck.frame_buffer();
            frame.hash(&mut hasher);
            let actual = hasher.finish();
            log::debug!(
                "frame : {}, matched: {}",
                test_frame.number,
                expected == actual
            );

            let result_dir = if env::var("UPDATE_SNAPSHOT").is_ok() || expected == actual {
                &*PASS_DIR
            } else {
                &*FAIL_DIR
            };
            let mut filename = test.to_owned();
            if let Some(ref name) = test_frame.name {
                let _ = write!(filename, "_{name}");
            } else if count > 0 {
                let _ = write!(filename, "_{}", count + 1);
            }
            let screenshot = result_dir
                .join(PathBuf::from(filename))
                .with_extension("png");

            Image::from_bytes(Ppu::WIDTH, Ppu::HEIGHT, frame, PixelFormat::Rgba)
                .expect("valid frame")
                .save(&screenshot)
                .expect("result screenshot");

            (expected, actual, test_frame.number, screenshot)
        })
    }

    pub(crate) fn test_rom(directory: &str, test_name: &str) {
        if !&*INIT_TESTS {
            log::debug!("Initialized tests");
        }

        let (test_file, mut tests) = get_rom_tests(directory);
        let mut test = tests.iter_mut().find(|test| test.name.eq(test_name));
        assert!(test.is_some(), "No test found matching {test_name:?}");
        let test = test.as_mut().expect("definitely has a test");

        let rom = PathBuf::from(directory)
            .join(PathBuf::from(&test.name))
            .with_extension("nes");
        assert!(rom.exists(), "No test rom found for {rom:?}");

        let mut deck = load_control_deck(&rom);
        if env::var("RUST_LOG").is_ok() {
            let _ = pretty_env_logger::try_init();
        }

        let mut results = Vec::new();
        for test_frame in test.frames.iter() {
            log::debug!("{} - {:?}", test_frame.number, deck.joypad_mut(Slot::One));

            while deck.frame_number() < test_frame.number {
                deck.clock_frame().expect("valid frame clock");
                deck.clear_audio_samples();
                deck.joypad_mut(Slot::One).reset(Kind::Soft);
                deck.joypad_mut(Slot::Two).reset(Kind::Soft);
            }

            handle_frame_action(test_frame, &mut deck);
            if let Some(result) = handle_snapshot(&test.name, test_frame, &mut deck, results.len())
            {
                results.push(result);
            }
        }
        let mut update_required = false;
        for (mut expected, actual, frame_number, screenshot) in results {
            if env::var("UPDATE_SNAPSHOT").is_ok() && expected != actual {
                expected = actual;
                update_required = true;
                if let Some(ref mut frame) = test
                    .frames
                    .iter_mut()
                    .find(|frame| frame.number == frame_number)
                {
                    frame.hash = Some(actual);
                }
            }
            assert_eq!(
                expected, actual,
                "mismatched snapshot for {rom:?} -> {screenshot:?}",
            );
        }
        if update_required {
            File::create(test_file)
                .context("failed to open rom test file")
                .and_then(|file| {
                    serde_json::to_writer_pretty(BufWriter::new(file), &tests)
                        .context("failed to serialize rom data")
                })
                .expect("failed to update snapshot");
        }
    }
}