neser 1.0.0

NESER - Nintendo Emulation Systems Engine (Rust). Desktop and WebAssembly frontends.
Documentation
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
413
414
415
416
417
418
419
//! Mapper 174 – NTDEC 5-in-1 multicart
//!
//! Specifications:
//! - Primary source: NESdev Wiki <https://www.nesdev.org/wiki/INES_Mapper_174>
//! - Reference impl: Mesen2 `Core/NES/Mappers/Ntdec/Mapper174.h`
//!
//! Known Limitations:
//! - No known gameplay-blocking functional limitations are currently documented.
//!
//! ## Overview
//!
//! Mapper 174 is used by the NTDec 5-in-1 multicart (128 KiB PRG, 64 KiB CHR).
//! It is functionally similar to [Mapper 58](https://www.nesdev.org/wiki/INES_Mapper_058)
//! but with different bit placements in the address register.
//!
//! ## Register
//!
//! A write to any address in `$8000–$FFFF` latches the full address into the
//! bank register.  **The data byte is ignored; banking is determined by address
//! lines only.**
//!
//! ```text
//! A~[1... .... OPPP CCCM]
//!                         M – Nametable mirroring: 0=Vertical, 1=Horizontal
//!                    +++ – C[2:0]: 8 KiB CHR-ROM bank select (bits A3..A1)
//!               +++ – P[2:0]: PRG bank index (bits A6..A4)
//!              + – O: PRG banking mode (bit A7)
//!                      0: NROM-128 – both 16 KiB slots map to same bank P
//!                      1: BNROM-style – 32 KiB bank = P[2:1] (P[0] ignored),
//!                                       PRG A14 from CPU A14
//! ```
//!
//! ## PRG Banking
//!
//! * O=0 (16 KiB mode): both `$8000–$BFFF` and `$C000$FFFF` map to the same
//!   16 KiB PRG-ROM bank selected by P.
//! * O=1 (32 KiB mode): the two 16 KiB slots are selected by `P & 0b110` and
//!   `(P & 0b110) | 1` respectively (i.e., an aligned 32 KiB bank whose index
//!   is `P >> 1`).
//!
//! ## CHR Banking
//!
//! 8 KiB CHR-ROM bank C is mapped to `$0000–$1FFF`.
//!
//! ## Mirroring
//!
//! Bit A0 of the write address: 0 → Vertical, 1 → Horizontal.
//!
//! No PRG-RAM, no IRQ, no expansion audio.  Power-on/reset state: all bits zero.

use crate::nes::cartridge::base_mapper::BaseMapper;
use crate::nes::cartridge::mapper::{Mapper, MapperCapabilities};

const MAPPER_NUMBER: u16 = 174;
const PRG_BANK_SIZE: usize = 16 * 1024;
const CHR_BANK_SIZE: usize = 8 * 1024;

/// Mapper 174 – NTDEC 5-in-1 multicart.
///
/// See the module-level documentation for hardware details.
pub struct Mapper174 {
    base: BaseMapper,
    /// Latched address bus value (full address from last write to $8000–$FFFF).
    latch: u16,
}

impl Mapper174 {
    pub fn new(ctx: crate::nes::cartridge::mapper::MapperContext) -> Self {
        let capabilities = MapperCapabilities {
            has_chr_banking: true,
            has_dynamic_mirroring: true,
            max_prg_ram_kb: 0,
            prg_bank_size_kb: 16,
            chr_bank_size_kb: 8,
            ..Default::default()
        };
        let mut base = BaseMapper::new(&ctx, capabilities);
        base.configure_prg_banking(PRG_BANK_SIZE);
        base.configure_chr_banking(CHR_BANK_SIZE);
        let mut mapper = Self { base, latch: 0 };
        mapper.apply_latch(0);
        mapper
    }

    /// Apply the latched address bus value to the bank registers.
    fn apply_latch(&mut self, addr: u16) {
        let prg_bank = ((addr >> 4) & 0x07) as i16; // bits A6..A4
        let chr_bank = ((addr >> 1) & 0x07) as i16; // bits A3..A1
        let mirroring_h = (addr & 0x01) != 0; // bit A0
        let mode_32k = (addr & 0x80) != 0; // bit A7 = O

        if mode_32k {
            // 32 KiB mode: align to even boundary
            self.base.select_prg_page(0, prg_bank & !1);
            self.base.select_prg_page(1, prg_bank | 1);
        } else {
            // 16 KiB NROM-128 mode: both slots = same bank
            self.base.select_prg_page(0, prg_bank);
            self.base.select_prg_page(1, prg_bank);
        }

        self.base.select_chr_page(0, chr_bank);
        self.base.set_mirroring_hv(mirroring_h);
        self.latch = addr;
    }
}

impl Mapper for Mapper174 {
    fn base(&self) -> &BaseMapper {
        &self.base
    }

    fn base_mut(&mut self) -> &mut BaseMapper {
        &mut self.base
    }

    fn mapper_number(&self) -> u16 {
        MAPPER_NUMBER
    }

    fn write_prg(&mut self, addr: u16, _value: u8) {
        if addr >= 0x8000 {
            self.apply_latch(addr);
        }
    }

    fn registers_snapshot(&self) -> Vec<u8> {
        vec![(self.latch & 0xFF) as u8, ((self.latch >> 8) & 0xFF) as u8]
    }

    fn restore_registers(&mut self, data: &[u8]) {
        if data.len() >= 2 {
            let latch = (data[0] as u16) | ((data[1] as u16) << 8);
            self.apply_latch(latch);
        }
    }

    fn reset(&mut self) {
        self.apply_latch(0);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::nes::cartridge::NametableLayout;
    use crate::nes::cartridge::mapper::{MapperContext, create_mapper};
    use crate::nes::cartridge::test_helpers::banked_data;

    // Use non-power-of-two bank counts to avoid false positives from modulo wrapping.
    const PRG_BANKS: usize = 5;
    const CHR_BANKS: usize = 5;

    fn make_mapper() -> Mapper174 {
        Mapper174::new(
            MapperContext::new_for_test(
                MAPPER_NUMBER,
                banked_data(PRG_BANK_SIZE, PRG_BANKS),
                banked_data(CHR_BANK_SIZE, CHR_BANKS),
                NametableLayout::Vertical,
            )
            .with_prg_ram_banks(0),
        )
    }

    // ── Registration ──────────────────────────────────────────────────────────

    #[test]
    fn mapper_174_is_registered() {
        let result = create_mapper(
            MapperContext::new_for_test(
                MAPPER_NUMBER,
                banked_data(PRG_BANK_SIZE, PRG_BANKS),
                banked_data(CHR_BANK_SIZE, CHR_BANKS),
                NametableLayout::Vertical,
            )
            .with_prg_ram_banks(0),
        );
        assert!(
            result.is_ok(),
            "Mapper 174 must be registered in the factory"
        );
    }

    // ── Power-on state ────────────────────────────────────────────────────────

    #[test]
    fn power_on_prg_bank_is_0() {
        let mapper = make_mapper();
        // latch = 0 → O=0 (16KB mode), P=0 → both slots bank 0
        assert_eq!(
            mapper.read_prg(0x8000),
            0,
            "$8000 must map to PRG bank 0 at power-on"
        );
        assert_eq!(
            mapper.read_prg(0xC000),
            0,
            "$C000 must map to PRG bank 0 at power-on"
        );
    }

    #[test]
    fn power_on_chr_bank_is_0() {
        let mut mapper = make_mapper();
        assert_eq!(
            mapper.read_chr(0x0000),
            0,
            "CHR $0000 must map to bank 0 at power-on"
        );
    }

    #[test]
    fn power_on_mirroring_is_vertical() {
        let mapper = make_mapper();
        assert_eq!(
            mapper.get_mirroring(),
            NametableLayout::Vertical,
            "Power-on mirroring must be Vertical (M=0)"
        );
    }

    // ── PRG banking – 16 KiB mode (O=0) ─────────────────────────────────────

    #[test]
    fn nrom128_mode_both_slots_same_bank() {
        let mut mapper = make_mapper();
        // addr = 0x8010: O=0, P=1 (bits A6..A4 = 0b001), C=0, M=0
        mapper.write_prg(0x8010, 0xFF);
        assert_eq!(mapper.read_prg(0x8000), 1, "$8000 must map to bank 1");
        assert_eq!(
            mapper.read_prg(0xC000),
            1,
            "$C000 must also map to bank 1 (NROM-128 mirror)"
        );
    }

    #[test]
    fn nrom128_mode_bank_2() {
        let mut mapper = make_mapper();
        // addr = 0x8020: O=0, P=2 (bits A6..A4 = 0b010), M=0
        mapper.write_prg(0x8020, 0);
        assert_eq!(mapper.read_prg(0x8000), 2);
        assert_eq!(mapper.read_prg(0xC000), 2);
    }

    // ── PRG banking – 32 KiB mode (O=1) ─────────────────────────────────────

    #[test]
    fn bnrom_mode_32k_aligned_bank() {
        let mut mapper = make_mapper();
        // addr = 0x8080: O=1 (bit 7), P=0 → 32KB bank 0 (slots 0 and 1)
        mapper.write_prg(0x8080, 0);
        assert_eq!(
            mapper.read_prg(0x8000),
            0,
            "$8000 must be low half of 32KB bank"
        );
        assert_eq!(
            mapper.read_prg(0xC000),
            1,
            "$C000 must be high half of 32KB bank"
        );
    }

    #[test]
    fn bnrom_mode_32k_odd_bank_aligned_down() {
        let mut mapper = make_mapper();
        // addr = 0x8090: O=1, P=1 → aligned down to P=0, slots = 0,1
        mapper.write_prg(0x8090, 0);
        assert_eq!(mapper.read_prg(0x8000), 0, "Odd P aligned to P & !1 = 0");
        assert_eq!(mapper.read_prg(0xC000), 1);
    }

    #[test]
    fn bnrom_mode_32k_bank_2() {
        let mut mapper = make_mapper();
        // addr = 0x80A0: O=1, P=2 → slots = 2,3
        mapper.write_prg(0x80A0, 0);
        assert_eq!(mapper.read_prg(0x8000), 2);
        assert_eq!(mapper.read_prg(0xC000), 3);
    }

    // ── CHR banking ───────────────────────────────────────────────────────────

    #[test]
    fn chr_bank_selected_by_bits_3_to_1() {
        let mut mapper = make_mapper();
        // addr = 0x8002: C=1 (bit A1 = 1, A3:A1 = 0b001)
        mapper.write_prg(0x8002, 0);
        assert_eq!(mapper.read_chr(0x0000), 1, "CHR bank 1");
        assert_eq!(mapper.read_chr(0x1FFF), 1, "CHR bank 1 covers full 8KB");
    }

    #[test]
    fn chr_bank_3_selects_bank_3() {
        let mut mapper = make_mapper();
        // addr = 0x8006: bits A3:A1 = 0b011 = 3
        mapper.write_prg(0x8006, 0);
        assert_eq!(mapper.read_chr(0x0000), 3);
    }

    // ── Mirroring ─────────────────────────────────────────────────────────────

    #[test]
    fn bit_a0_0_selects_vertical() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x8010, 0); // A0=0
        assert_eq!(mapper.get_mirroring(), NametableLayout::Vertical);
    }

    #[test]
    fn bit_a0_1_selects_horizontal() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x8011, 0); // A0=1
        assert_eq!(mapper.get_mirroring(), NametableLayout::Horizontal);
    }

    // ── Data byte ignored ─────────────────────────────────────────────────────

    #[test]
    fn data_byte_is_ignored() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x8010, 0x00); // P=1
        assert_eq!(mapper.read_prg(0x8000), 1);
        mapper.write_prg(0x8010, 0xFF); // same address, different data
        assert_eq!(
            mapper.read_prg(0x8000),
            1,
            "Data byte must be ignored; address determines banking"
        );
    }

    // ── Writes below $8000 are ignored ───────────────────────────────────────

    #[test]
    fn write_below_8000_does_not_change_banks() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x7FFF, 0xFF);
        assert_eq!(
            mapper.read_prg(0x8000),
            0,
            "Write below $8000 must not change PRG bank"
        );
    }

    // ── Reset ─────────────────────────────────────────────────────────────────

    #[test]
    fn reset_returns_to_power_on_state() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x8030, 0); // P=3, C=1, M=0
        mapper.reset();
        assert_eq!(mapper.read_prg(0x8000), 0, "PRG bank must reset to 0");
        assert_eq!(mapper.read_prg(0xC000), 0);
        assert_eq!(mapper.read_chr(0x0000), 0, "CHR bank must reset to 0");
        assert_eq!(mapper.get_mirroring(), NametableLayout::Vertical);
    }

    // ── Snapshot / restore ────────────────────────────────────────────────────

    #[test]
    fn registers_snapshot_round_trips() {
        let mut mapper = make_mapper();
        // addr = 0x8031: O=0, P=3, C=1, M=1
        mapper.write_prg(0x8031, 0);

        let snap = mapper.registers_snapshot();
        let mut restored = make_mapper();
        restored.restore_registers(&snap);

        assert_eq!(
            restored.read_prg(0x8000),
            mapper.read_prg(0x8000),
            "Restored PRG $8000 must match"
        );
        assert_eq!(
            restored.read_prg(0xC000),
            mapper.read_prg(0xC000),
            "Restored PRG $C000 must match"
        );
        assert_eq!(
            restored.read_chr(0x0000),
            mapper.read_chr(0x0000),
            "Restored CHR must match"
        );
        assert_eq!(
            restored.get_mirroring(),
            mapper.get_mirroring(),
            "Restored mirroring must match"
        );
    }

    // ── No IRQ ────────────────────────────────────────────────────────────────

    #[test]
    fn irq_never_pending() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x8010, 0);
        assert!(!mapper.irq_pending(), "Mapper 174 must never assert IRQ");
    }

    // ── CHR-RAM fallback ──────────────────────────────────────────────────────

    #[test]
    fn chr_ram_works_when_no_chr_rom() {
        let mut mapper = Mapper174::new(
            MapperContext::new_for_test(
                MAPPER_NUMBER,
                banked_data(PRG_BANK_SIZE, PRG_BANKS),
                vec![],
                NametableLayout::Vertical,
            )
            .with_prg_ram_banks(0),
        );
        mapper.write_chr(0x0100, 0xAB);
        assert_eq!(mapper.read_chr(0x0100), 0xAB);
    }
}