neser 0.1.1

NESER - NES Emulator in Rust - is a NES emulator written in Rust. It aims to be a high-quality, hardware-accurate emulator that is also easy to use and extend. It supports a wide range of NES games and features, including various mappers, audio processing, and input handling. NESER is designed to be modular and extensible, allowing developers to easily add new features or support for additional hardware. It can be run using one of two frontends: a native desktop application using SDL2, or a web application using WebAssembly. The desktop application provides a high-performance, feature-rich experience with support for various input devices and display options, while the web application allows users to play NES games directly in their browsers without needing to install any software in a BYOR manner (Bring Your Own Roms).
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! Mapper 020 - Famicom Disk System (FDS)
//!
//! Specifications:
//! - Main: <https://www.nesdev.org/wiki/INES_Mapper_020>
//! - FDS hardware: <https://www.nesdev.org/wiki/Family_Computer_Disk_System>
//!
//! Known Limitations:
//! - Disk I/O is not emulated (no .fds disk image loading).
//! - FDS audio channels (wave table, modulation) are stored but not mixed.
//! - BIOS ROM is taken from PRG-ROM; no user-supplied BIOS loading.

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

/// FDS work-RAM region: $6000–$DFFF (32 KiB).
const WORK_RAM_SIZE: usize = 0x8000;
const WORK_RAM_BASE: u16 = 0x6000;
const WORK_RAM_END: u16 = 0xDFFF;

const BIOS_ROM_BASE: u16 = 0xE000;

const WAVE_RAM_BASE: u16 = 0x4040;
const WAVE_RAM_END: u16 = 0x407F;
const WAVE_RAM_MASK: u8 = 0x3F;
const WAVE_RAM_LEN: usize = 64;

const AUDIO_REG_BASE: u16 = 0x4080;
const AUDIO_REG_END: u16 = 0x408F;
const AUDIO_REGS_LEN: usize = 16;

const FDS_CONTROL_MIRROR_BIT: u8 = 0x08;
const DISK_STATUS_NOT_READY: u8 = 0x80;

const IRQ_ENABLE_BIT: u8 = 0x01;
const IRQ_REPEAT_BIT: u8 = 0x02;

/// Snapshot layout: 8-byte header, then wave RAM, then audio regs.
const SNAPSHOT_HEADER_LEN: usize = 8;
const SNAPSHOT_MIN_LEN: usize = SNAPSHOT_HEADER_LEN + WAVE_RAM_LEN + AUDIO_REGS_LEN;

/// Mapper 020 — Famicom Disk System
///
/// Memory map (CPU):
/// - `$4020–$402F`: FDS disk I/O registers (write) / status (read)
/// - `$4030–$403F`: FDS drive status registers (read)
/// - `$4040–$407F`: Wave RAM (6-bit, R/W)
/// - `$4080–$408F`: Audio control registers (write)
/// - `$6000–$DFFF`: 32 KiB work RAM (R/W)
/// - `$E000$FFFF`: BIOS ROM (8 KiB, from PRG-ROM)
///
/// Mirroring: Fixed horizontal (hardwired on FDS hardware).
/// IRQ: 16-bit CPU-cycle count-down timer; fires on reach-zero; optional reload.
pub struct Mapper20 {
    base: BaseMapper,

    /// 32 KiB work RAM ($6000–$DFFF).
    work_ram: Box<[u8; WORK_RAM_SIZE]>,

    // --- IRQ timer ---
    irq_latch: u16,
    irq_counter: u16,
    irq_enabled: bool,
    irq_pending: bool,
    irq_repeat: bool,

    // --- I/O enable register ($4023) ---
    io_enable: u8,

    // --- FDS disk control register ($4025, write-only) ---
    fds_control: u8,

    // --- Audio ---
    /// Wave RAM: 64 × 6-bit entries at $4040–$407F.
    wave_ram: [u8; 64],
    /// Audio control registers $4080–$408F (write, backing store for snapshot).
    audio_regs: [u8; 16],
}

impl Mapper20 {
    pub fn new(ctx: super::mapper::MapperContext) -> Self {
        let capabilities = MapperCapabilities {
            has_irq: true,
            has_expansion_audio: true,
            has_dynamic_mirroring: false,
            ..Default::default()
        };
        let mut base = BaseMapper::new(&ctx, capabilities);
        base.set_mirroring(NametableLayout::Horizontal);

        Self {
            base,
            work_ram: Box::new([0u8; WORK_RAM_SIZE]),
            irq_latch: 0,
            irq_counter: 0,
            irq_enabled: false,
            irq_pending: false,
            irq_repeat: false,
            io_enable: 0,
            fds_control: 0,
            wave_ram: [0u8; 64],
            audio_regs: [0u8; 16],
        }
    }

    /// Read a byte from BIOS ROM at $E000$FFFF.
    fn read_bios(&self, addr: u16) -> u8 {
        let prg = self.base.prg_rom();
        if prg.is_empty() {
            return 0;
        }
        let offset = (addr as usize - BIOS_ROM_BASE as usize) % prg.len();
        prg[offset]
    }

    fn pack_irq_flags(&self) -> u8 {
        (self.irq_enabled as u8) | ((self.irq_pending as u8) << 1) | ((self.irq_repeat as u8) << 2)
    }

    fn unpack_irq_flags(&mut self, flags: u8) {
        self.irq_enabled = (flags & 0x01) != 0;
        self.irq_pending = (flags & 0x02) != 0;
        self.irq_repeat = (flags & 0x04) != 0;
    }
}

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

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

    fn read_prg(&self, addr: u16) -> u8 {
        match addr {
            0x4030 => DISK_STATUS_NOT_READY,
            0x4031 => 0, // read data register (no disk)
            0x4032 => 0, // drive status
            0x4033 => 0, // external connector
            WAVE_RAM_BASE..=WAVE_RAM_END => {
                let idx = (addr - WAVE_RAM_BASE) as usize;
                self.wave_ram[idx] & WAVE_RAM_MASK
            }
            WORK_RAM_BASE..=WORK_RAM_END => {
                let offset = (addr - WORK_RAM_BASE) as usize;
                self.work_ram[offset]
            }
            BIOS_ROM_BASE..=0xFFFF => self.read_bios(addr),
            _ => 0,
        }
    }

    fn write_prg(&mut self, addr: u16, value: u8) {
        match addr {
            0x4020 => {
                self.irq_latch = (self.irq_latch & 0xFF00) | (value as u16);
            }
            0x4021 => {
                self.irq_latch = (self.irq_latch & 0x00FF) | ((value as u16) << 8);
            }
            0x4022 => {
                // Any write acknowledges a pending IRQ.
                self.irq_pending = false;
                self.irq_enabled = (value & IRQ_ENABLE_BIT) != 0;
                self.irq_repeat = (value & IRQ_REPEAT_BIT) != 0;
                if self.irq_enabled {
                    self.irq_counter = self.irq_latch;
                } else {
                    self.irq_counter = 0;
                }
            }
            0x4023 => {
                self.io_enable = value;
            }
            0x4025 => {
                self.fds_control = value;
                // Bit 3: 0 = vertical mirroring, 1 = horizontal mirroring.
                self.base
                    .set_mirroring_hv((value & FDS_CONTROL_MIRROR_BIT) != 0);
            }
            WAVE_RAM_BASE..=WAVE_RAM_END => {
                let idx = (addr - WAVE_RAM_BASE) as usize;
                self.wave_ram[idx] = value & WAVE_RAM_MASK;
            }
            AUDIO_REG_BASE..=AUDIO_REG_END => {
                let idx = (addr - AUDIO_REG_BASE) as usize;
                self.audio_regs[idx] = value;
            }
            WORK_RAM_BASE..=WORK_RAM_END => {
                let offset = (addr - WORK_RAM_BASE) as usize;
                self.work_ram[offset] = value;
            }
            _ => {}
        }
    }

    fn cpu_cycle(&mut self) {
        if !self.irq_enabled || self.irq_counter == 0 {
            return;
        }
        self.irq_counter = self.irq_counter.wrapping_sub(1);
        if self.irq_counter == 0 {
            self.irq_pending = true;
            if self.irq_repeat {
                self.irq_counter = self.irq_latch;
            } else {
                self.irq_enabled = false;
            }
        }
    }

    fn irq_pending(&self) -> bool {
        self.irq_pending
    }

    fn get_mirroring(&self) -> NametableLayout {
        self.base.mirroring()
    }

    fn prg_ram_snapshot(&self) -> Vec<u8> {
        self.work_ram.to_vec()
    }

    fn restore_prg_ram(&mut self, data: &[u8]) {
        let len = data.len().min(WORK_RAM_SIZE);
        self.work_ram[..len].copy_from_slice(&data[..len]);
    }

    fn registers_snapshot(&self) -> Vec<u8> {
        let mirror_byte = matches!(self.base.mirroring(), NametableLayout::Horizontal) as u8;
        let mut v = vec![
            (self.irq_latch & 0xFF) as u8,
            (self.irq_latch >> 8) as u8,
            (self.irq_counter & 0xFF) as u8,
            (self.irq_counter >> 8) as u8,
            self.pack_irq_flags(),
            self.io_enable,
            self.fds_control,
            mirror_byte,
        ];
        v.extend_from_slice(&self.wave_ram);
        v.extend_from_slice(&self.audio_regs);
        v
    }

    fn restore_registers(&mut self, data: &[u8]) {
        if data.len() < SNAPSHOT_MIN_LEN {
            return;
        }
        self.irq_latch = (data[0] as u16) | ((data[1] as u16) << 8);
        self.irq_counter = (data[2] as u16) | ((data[3] as u16) << 8);
        self.unpack_irq_flags(data[4]);
        self.io_enable = data[5];
        self.fds_control = data[6];
        self.base.set_mirroring_hv(data[7] != 0);

        let wave_end = SNAPSHOT_HEADER_LEN + WAVE_RAM_LEN;
        let audio_end = wave_end + AUDIO_REGS_LEN;
        self.wave_ram
            .copy_from_slice(&data[SNAPSHOT_HEADER_LEN..wave_end]);
        self.audio_regs.copy_from_slice(&data[wave_end..audio_end]);
    }

    fn reset(&mut self) {
        self.work_ram.fill(0);
        self.irq_latch = 0;
        self.irq_counter = 0;
        self.irq_enabled = false;
        self.irq_pending = false;
        self.irq_repeat = false;
        self.io_enable = 0;
        self.fds_control = 0;
        self.wave_ram.fill(0);
        self.audio_regs.fill(0);
        self.base.set_mirroring(NametableLayout::Horizontal);
    }
}

#[cfg(test)]
mod tests {
    use super::Mapper20;
    use crate::cartridge::NametableLayout;
    use crate::cartridge::mapper::{Mapper, MapperContext, create_mapper};

    fn make_mapper() -> Mapper20 {
        Mapper20::new(MapperContext::new_for_test(
            20,
            vec![0u8; 8 * 1024], // 8 KiB BIOS placeholder
            vec![],              // CHR-RAM
            NametableLayout::Horizontal,
        ))
    }

    // --- Factory ---

    #[test]
    fn mapper_20_is_registered() {
        let result = create_mapper(MapperContext::new_for_test(
            20,
            vec![0u8; 8 * 1024],
            vec![],
            NametableLayout::Horizontal,
        ));
        assert!(
            result.is_ok(),
            "mapper 20 must be registered in the factory"
        );
    }

    // --- Mirroring ---

    #[test]
    fn mirroring_defaults_to_horizontal() {
        let mapper = make_mapper();
        assert_eq!(
            mapper.get_mirroring(),
            NametableLayout::Horizontal,
            "FDS mirroring must default to Horizontal"
        );
    }

    #[test]
    fn mirroring_controlled_by_fds_control_bit3() {
        let mut mapper = make_mapper();
        // bit 3 = 0 → Vertical
        mapper.write_prg(0x4025, 0x00);
        assert_eq!(mapper.get_mirroring(), NametableLayout::Vertical);
        // bit 3 = 1 → Horizontal
        mapper.write_prg(0x4025, 0x08);
        assert_eq!(mapper.get_mirroring(), NametableLayout::Horizontal);
    }

    // --- Work RAM ---

    #[test]
    fn work_ram_is_readable_and_writable_at_6000() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x6000, 0xAB);
        assert_eq!(
            mapper.read_prg(0x6000),
            0xAB,
            "Work RAM at $6000 must be writable and readable"
        );
    }

    #[test]
    fn work_ram_covers_full_6000_dfff_range() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x6000, 0x11);
        mapper.write_prg(0x8000, 0x22);
        mapper.write_prg(0xDFFF, 0x33);
        assert_eq!(mapper.read_prg(0x6000), 0x11);
        assert_eq!(mapper.read_prg(0x8000), 0x22);
        assert_eq!(mapper.read_prg(0xDFFF), 0x33);
    }

    // --- BIOS ROM ---

    #[test]
    fn bios_rom_readable_at_e000() {
        let mut bios = vec![0u8; 8 * 1024];
        bios[0] = 0x42;
        bios[8 * 1024 - 1] = 0x99;
        let mapper = Mapper20::new(MapperContext::new_for_test(
            20,
            bios,
            vec![],
            NametableLayout::Horizontal,
        ));
        assert_eq!(mapper.read_prg(0xE000), 0x42, "BIOS first byte at $E000");
        assert_eq!(mapper.read_prg(0xFFFF), 0x99, "BIOS last byte at $FFFF");
    }

    // --- IRQ ---

    #[test]
    fn irq_not_pending_on_power_on() {
        let mapper = make_mapper();
        assert!(!mapper.irq_pending(), "IRQ must not be pending on power-on");
    }

    #[test]
    fn irq_does_not_fire_when_disabled() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4020, 0x01);
        mapper.write_prg(0x4021, 0x00);
        // Do NOT enable via $4022
        mapper.cpu_cycle();
        assert!(!mapper.irq_pending(), "IRQ must not fire when disabled");
    }

    #[test]
    fn irq_fires_after_counter_expires() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4020, 0x01); // latch low = 1
        mapper.write_prg(0x4021, 0x00); // latch high = 0 → latch = 1
        mapper.write_prg(0x4022, 0x01); // enable, no repeat
        // Counter loaded to 1; one cycle decrements to 0 → IRQ fires
        mapper.cpu_cycle();
        assert!(
            mapper.irq_pending(),
            "IRQ must fire when 16-bit counter reaches zero"
        );
    }

    #[test]
    fn irq_does_not_fire_before_counter_expires() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4020, 0x03); // latch = 3
        mapper.write_prg(0x4021, 0x00);
        mapper.write_prg(0x4022, 0x01); // enable
        mapper.cpu_cycle();
        assert!(!mapper.irq_pending(), "IRQ must not fire on first cycle");
        mapper.cpu_cycle();
        assert!(!mapper.irq_pending(), "IRQ must not fire on second cycle");
        mapper.cpu_cycle();
        assert!(
            mapper.irq_pending(),
            "IRQ must fire on third cycle (counter 3→0)"
        );
    }

    #[test]
    fn irq_acknowledged_by_writing_4022_with_bit0_clear() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4020, 0x01);
        mapper.write_prg(0x4021, 0x00);
        mapper.write_prg(0x4022, 0x01); // enable
        mapper.cpu_cycle();
        assert!(mapper.irq_pending(), "IRQ must be pending before ack");
        mapper.write_prg(0x4022, 0x00); // disable + acknowledge
        assert!(
            !mapper.irq_pending(),
            "IRQ must clear after $4022 write with bit0=0"
        );
    }

    #[test]
    fn irq_repeats_when_repeat_flag_set() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4020, 0x02); // latch = 2
        mapper.write_prg(0x4021, 0x00);
        mapper.write_prg(0x4022, 0x03); // enable + repeat
        // Cycle 1: counter 2 → 1
        mapper.cpu_cycle();
        assert!(!mapper.irq_pending());
        // Cycle 2: counter 1 → 0 → fires, reloads to 2
        mapper.cpu_cycle();
        assert!(mapper.irq_pending(), "IRQ must fire after latch cycles");
        // Acknowledge and check the counter reloaded
        mapper.write_prg(0x4022, 0x03); // re-enable (also reloads)
        assert!(
            !mapper.irq_pending(),
            "Re-enabling must clear IRQ and reload counter"
        );
        // Should fire again after 2 more cycles
        mapper.cpu_cycle();
        assert!(!mapper.irq_pending());
        mapper.cpu_cycle();
        assert!(mapper.irq_pending(), "IRQ must repeat every latch cycles");
    }

    // --- Audio ---

    #[test]
    fn audio_wave_ram_is_readable_and_writable() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4040, 0xFF); // only 6 bits stored
        assert_eq!(
            mapper.read_prg(0x4040),
            0x3F,
            "Wave RAM must store 6-bit values"
        );
        mapper.write_prg(0x407F, 0x1A);
        assert_eq!(mapper.read_prg(0x407F), 0x1A & 0x3F);
    }

    #[test]
    fn audio_control_registers_writable() {
        let mut mapper = make_mapper();
        // Should not panic — just a write, no assertion needed beyond that
        mapper.write_prg(0x4080, 0x80);
        mapper.write_prg(0x408A, 0xFF);
    }

    // --- Snapshot / restore ---

    #[test]
    fn snapshot_restore_roundtrip() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x4020, 0x05);
        mapper.write_prg(0x4021, 0x00);
        mapper.write_prg(0x4022, 0x01); // enable IRQ, latch=5
        mapper.write_prg(0x4040, 0x3F); // wave RAM
        mapper.write_prg(0x4080, 0x7F); // audio reg

        let snap = mapper.registers_snapshot();

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

        assert_eq!(
            restored.registers_snapshot(),
            snap,
            "registers_snapshot/restore_registers roundtrip must preserve all register state"
        );
    }

    #[test]
    fn work_ram_snapshot_restore_roundtrip() {
        let mut mapper = make_mapper();
        mapper.write_prg(0x6000, 0xAB);
        mapper.write_prg(0xDFFF, 0xCD);

        let snap = mapper.prg_ram_snapshot();
        assert_eq!(snap.len(), 0x8000, "work RAM snapshot must be 32 KiB");

        let mut restored = make_mapper();
        restored.restore_prg_ram(&snap);

        assert_eq!(
            restored.read_prg(0x6000),
            0xAB,
            "Restored work RAM must match at $6000"
        );
        assert_eq!(
            restored.read_prg(0xDFFF),
            0xCD,
            "Restored work RAM must match at $DFFF"
        );
    }
}