reznez 0.0.0

The high accuracy NES Emulator
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
#[rustfmt::skip]

use std::fmt;

use splitbits::{splitbits_named, splitbits_named_into_ux, splitbits_named_ux, combinebits, replacebits};

use crate::ppu::name_table::background_tile_index::{TileColumn, TileRow};
use crate::ppu::name_table::name_table_quadrant::NameTableQuadrant;
use crate::ppu::pattern_table::{PatternTableSide, PatternIndex};
use crate::ppu::pixel_index::{ColumnInTile, PixelColumn, PixelRow, RowInTile};
use crate::ppu::register::registers::ctrl::AddressIncrement;

/*
 * 0 123 45 6789A BCDEF
 * 0 yyy NN YYYYY XXXXX
 * | ||| || ||||| +++++-- Coarse X Scroll
 * | ||| || +++++-------- Coarse Y Scroll
 * | ||| ++-------------- Nametable Quadrant
 * | +++----------------- Fine Y Scroll
 * +--------------------- Unused, always zero
 */
#[derive(Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct PpuAddress {
    address: u16,
    fine_x_scroll: ColumnInTile,
}

impl PpuAddress {
    pub const ZERO: PpuAddress = PpuAddress {
        address: 0x0000,
        fine_x_scroll: ColumnInTile::Zero,
    };
    pub const PALETTE_TABLE_START: PpuAddress = PpuAddress {
        address: 0x3F00,
        fine_x_scroll: ColumnInTile::Zero,
    };

    pub const fn from_u16(value: u16) -> PpuAddress {
        PpuAddress {
            address: value & 0b0011_1111_1111_1111,
            fine_x_scroll: ColumnInTile::Zero,
        }
    }

    pub fn in_name_table(n: NameTableQuadrant, c: TileColumn, r: TileRow) -> PpuAddress {
        PpuAddress::from_u16(combinebits!("0010 nn rrrrr ccccc"))
    }

    pub fn in_attribute_table(n: NameTableQuadrant, c: TileColumn, r: TileRow) -> PpuAddress {
        let r = r.to_u16() / 4;
        let c = c.to_u16() / 4;
        PpuAddress::from_u16(combinebits!("0010 nn 1111r rrccc"))
    }

    pub fn in_pattern_table(s: PatternTableSide, p: PatternIndex, r: RowInTile, select_high: bool) -> PpuAddress {
        let h = select_high;
        PpuAddress::from_u16(combinebits!("000s pppp pppp hrrr"))
    }

    pub fn advance(&mut self, address_increment: AddressIncrement) {
        if address_increment == AddressIncrement::Right {
            let mut coarse_x_scroll = self.coarse_x_scroll();
            let wrapped = coarse_x_scroll.increment();
            self.set_coarse_x_scroll(coarse_x_scroll);
            if !wrapped {
                return;
            }
        }

        let mut coarse_y_scroll = self.coarse_y_scroll();
        let wrapped = coarse_y_scroll.increment();
        self.set_coarse_y_scroll(coarse_y_scroll);
        if wrapped {
            let mut name_table_quadrant = self.name_table_quadrant();
            let wrapped = name_table_quadrant.increment();
            self.set_name_table_quadrant(name_table_quadrant);
            if wrapped {
                let mut fine_y_scroll = self.fine_y_scroll();
                fine_y_scroll.increment_low_bits();
                self.set_fine_y_scroll(fine_y_scroll);
            }
        }
    }

    pub fn increment_coarse_x_scroll(&mut self) {
        let mut coarse_x_scroll = self.coarse_x_scroll();
        let wrapped = coarse_x_scroll.increment();
        self.set_coarse_x_scroll(coarse_x_scroll);
        if wrapped {
            let mut name_table_quadrant = self.name_table_quadrant();
            name_table_quadrant = name_table_quadrant.next_horizontal();
            self.set_name_table_quadrant(name_table_quadrant);
        }
    }

    pub fn increment_fine_y_scroll(&mut self) {
        let mut fine_y_scroll = self.fine_y_scroll();
        let wrapped = fine_y_scroll.increment();
        self.set_fine_y_scroll(fine_y_scroll);
        if wrapped {
            let mut coarse_y_scroll = self.coarse_y_scroll();
            let wrapped = coarse_y_scroll.increment_visible();
            self.set_coarse_y_scroll(coarse_y_scroll);
            if wrapped {
                let mut name_table_quadrant = self.name_table_quadrant();
                name_table_quadrant = name_table_quadrant.next_vertical();
                self.set_name_table_quadrant(name_table_quadrant);
            }
        }
    }

    pub fn to_pending_data_source(self) -> PpuAddress {
        let mut data_source = self;
        if data_source.to_u16() >= 0x3000 {
            data_source = PpuAddress::from_u16(data_source.to_u16() - 0x1000);
        }

        data_source
    }

    pub fn name_table_quadrant(self) -> NameTableQuadrant {
        splitbits_named_ux!(self.address, ".... nn.. .... ....").into()
    }

    pub fn name_table_location(self) -> Option<(NameTableQuadrant, u16)> {
        if self.address >= 0x2000 && self.address < 0x3F00 {
            Some(splitbits_named_into_ux!(self.address, ".... nnll llll llll"))
        } else {
            None
        }
    }

    pub fn is_in_pattern_table(self) -> bool {
        self.address < 0x2000
    }

    pub fn is_in_name_table_proper(self) -> bool {
        if self.address >= 0x2000 && self.address < 0x3F00 {
            self.address % 0x400 < 0x3C0
        } else {
            false
        }
    }

    pub fn is_in_attribute_table(self) -> bool {
        if self.address >= 0x2000 && self.address < 0x3F00 {
            self.address % 0x400 >= 0x3C0
        } else {
            false
        }
    }

    /*
     * 0123456789ABCDEF
     * -----------01234  $SCROLL#1
     * ----67----------  $CTRL
     */
    pub fn x_scroll(self) -> XScroll {
        XScroll {
            coarse: self.coarse_x_scroll(),
            fine: self.fine_x_scroll,
        }
    }

    fn coarse_x_scroll(self) -> TileColumn {
        splitbits_named_ux!(self.address, ".... .... ...x xxxx").into()
    }

    /*
     * 0123456789ABCDEF
     *  567--01234-----  $SCROLL#2
     *  ---67----------  $CTRL
     */
    pub fn y_scroll(self) -> YScroll {
        YScroll {
            coarse: self.coarse_y_scroll(),
            fine: self.fine_y_scroll(),
        }
    }

    fn coarse_y_scroll(self) -> TileRow {
        splitbits_named_into_ux!(self.address, "...... yyyyy .....")
    }

    fn fine_y_scroll(self) -> RowInTile {
        splitbits_named_into_ux!(self.address, ". yyy ............")
    }

    pub fn set_name_table_quadrant(&mut self, n: NameTableQuadrant) {
        self.address = replacebits!(self.address, "0... nn.. .... ....");
    }

    pub fn set_x_scroll(&mut self, value: u8) {
        let value = XScroll::from_u8(value);
        self.fine_x_scroll = value.fine();
        self.set_coarse_x_scroll(value.coarse());
    }

    fn set_coarse_x_scroll(&mut self, x: TileColumn) {
        self.address = replacebits!(self.address, ".... .... ...x xxxx");
    }

    pub fn set_y_scroll(&mut self, value: u8) {
        let value = YScroll::from_u8(value);
        self.set_coarse_y_scroll(value.coarse());
        self.set_fine_y_scroll(value.fine());
    }

    fn set_coarse_y_scroll(&mut self, y: TileRow) {
        self.address = replacebits!(self.address, ".... ..yy yyy. ....");
    }

    fn set_fine_y_scroll(&mut self, y: RowInTile) {
        self.address = replacebits!(self.address, "0yyy .... .... ....");
    }

    pub fn copy_x_scroll(&mut self, other: PpuAddress) {
        self.set_x_scroll(other.x_scroll().to_u8());
    }

    pub fn copy_y_scroll(&mut self, other: PpuAddress) {
        self.set_y_scroll(other.y_scroll().to_u8());
    }

    pub fn copy_name_table_quadrant(&mut self, other: PpuAddress) {
        self.set_name_table_quadrant(other.name_table_quadrant());
    }

    pub fn copy_horizontal_name_table_side(&mut self, other: PpuAddress) {
        let mut name_table_quadrant = self.name_table_quadrant();
        name_table_quadrant.copy_horizontal_side_from(other.name_table_quadrant());
        self.set_name_table_quadrant(name_table_quadrant);
    }

    pub fn set_high_byte(&mut self, h: u8) {
        // Lose the top bit of the fine y scroll.
        let h = h & 0b0011_1111;
        self.address = replacebits!(self.address, "00hh hhhh .... ....");
    }

    pub fn set_low_byte(&mut self, l: u8) {
        self.address = replacebits!(self.address, ".... .... llll llll");
    }

    pub const fn to_u16(self) -> u16 {
        // Chop off the top bit of fine y to leave a 14-bit representation.
        self.to_scroll_u16() & 0b0011_1111_1111_1111
    }

    pub fn to_usize(self) -> usize {
        usize::from(self.to_u16())
    }

    pub const fn to_scroll_u16(self) -> u16 {
        self.address
    }

    pub fn pattern_table_side(self) -> PatternTableSide {
        splitbits_named!(self.address, "...p .... .... ....").into()
    }
}

impl PartialEq for PpuAddress {
    fn eq(&self, rhs: &PpuAddress) -> bool {
        self.to_u16() == rhs.to_u16()
    }
}

impl fmt::Display for PpuAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "${:04X}", self.to_u16())
    }
}

#[derive(Clone, Copy, Debug)]
pub struct XScroll {
    coarse: TileColumn,
    fine: ColumnInTile,
}

impl XScroll {
    pub const ZERO: XScroll =
        XScroll { coarse: TileColumn::ZERO, fine: ColumnInTile::Zero };

    fn from_u8(value: u8) -> XScroll {
        let (coarse, fine) = splitbits_named_into_ux!(value, "cccccfff");
        XScroll { coarse, fine }
    }

    pub fn coarse(self) -> TileColumn {
        self.coarse
    }

    pub fn fine(self) -> ColumnInTile {
        self.fine
    }

    pub fn is_zero(self) -> bool {
        self.coarse.to_u8() == 0 && self.fine == ColumnInTile::Zero
    }

    pub fn tile_column(self, pixel_column: PixelColumn) -> (TileColumn, ColumnInTile) {
        let offset_pixel_column = self.to_u8().wrapping_add(pixel_column.to_u8());
        let (coarse, fine) = splitbits_named_ux!(offset_pixel_column, "cccccfff");
        (coarse.into(), fine.into())
    }

    pub fn to_u8(self) -> u8 {
        combinebits!(self.coarse, self.fine, "cccccfff")
    }
}

#[derive(Clone, Copy, Debug)]
pub struct YScroll {
    coarse: TileRow,
    fine: RowInTile,
}

impl YScroll {
    pub const ZERO: YScroll = YScroll { coarse: TileRow::ZERO, fine: RowInTile::Zero };

    fn from_u8(value: u8) -> YScroll {
        let (coarse, fine) = splitbits_named_into_ux!(value, "cccccfff");
        YScroll { coarse, fine }
    }

    pub fn shift_down(self) -> YScroll {
        YScroll::from_u8(self.to_u8().wrapping_sub(240))
    }

    pub fn coarse(self) -> TileRow {
        self.coarse
    }

    pub fn fine(self) -> RowInTile {
        self.fine
    }

    pub fn is_zero(self) -> bool {
        self.coarse.to_u8() == 0 && self.fine == RowInTile::Zero
    }

    pub fn tile_row(self, pixel_row: PixelRow) -> (TileRow, RowInTile) {
        let offset_pixel_row = self.to_u8().wrapping_add(pixel_row.to_u8());
        splitbits_named_into_ux!(offset_pixel_row, "cccccfff")
    }

    pub fn to_u8(self) -> u8 {
        combinebits!(self.coarse, self.fine, "cccccfff")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_and_to_u16() {
        for address in 0x0000..=0xFFFF {
            assert_eq!(address & 0x3FFF, PpuAddress::from_u16(address).to_u16());
        }
    }

    #[test]
    fn reduce_bit_1_from_high_byte() {
        let mut address = PpuAddress::from_u16(0);
        address.set_high_byte(0b1111_1111);
        assert_eq!(address.to_scroll_u16(), 0b0011_1111_0000_0000);
        assert_eq!(address.to_u16(), 0b0011_1111_0000_0000);
    }

    #[test]
    fn reduce_bit_1_from_y_scroll() {
        let mut address = PpuAddress::from_u16(0);
        address.set_y_scroll(0b1111_1111);
        assert_eq!(address.to_scroll_u16(), 0b0111_0011_1110_0000);
        assert_eq!(address.to_u16(), 0b0011_0011_1110_0000);
    }

    #[test]
    fn wrap_advance() {
        let mut address = PpuAddress::from_u16(0x3FFF);
        address.advance(AddressIncrement::Right);
        assert_eq!(address.to_scroll_u16(), 0x0000);
        assert_eq!(address.to_u16(), 0x0000);
    }

    #[test]
    fn set_x_scroll() {
        let mut address = PpuAddress::from_u16(0);
        assert_eq!(address.x_scroll().to_u8(), 0x00);
        address.set_x_scroll(0xFF);
        assert_eq!(address.x_scroll().to_u8(), 0xFF);
    }

    #[test]
    fn set_y_scroll() {
        let mut address = PpuAddress::from_u16(0);
        assert_eq!(address.y_scroll().to_u8(), 0x00);
        address.set_y_scroll(0xFF);
        assert_eq!(address.y_scroll().to_u8(), 0xFF);
    }

    #[test]
    fn set_x_y_scroll() {
        let mut address = PpuAddress::from_u16(0);
        assert_eq!(address.x_scroll().to_u8(), 0x00);
        assert_eq!(address.y_scroll().to_u8(), 0x00);
        address.set_x_scroll(0xFD);
        address.set_y_scroll(0xFF);
        assert_eq!(address.x_scroll().to_u8(), 0xFD);
        assert_eq!(address.y_scroll().to_u8(), 0xFF);
    }
}