librsmsx 0.5.2

a MSX emulator written in rust, a port from gomsx
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
use serde::{Deserialize, Serialize};

use super::{base_system::BaseSystem, graphics::GraphicsDriver};

use super::graphics::GraphicsType;

pub(crate) const SCREEN0: u8 = 0;
pub(crate) const SCREEN1: u8 = 1;
pub(crate) const SCREEN2: u8 = 2;
pub(crate) const SCREEN3: u8 = 3;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VdpData {
    screen_enabled: bool,
    screen_mode: u8,
    value_read: u8,
    write_state: bool,
    enabled_interrupts: bool,
    registers: [u8; 10],
    write_to_vram: bool,
    vram: Vec<u8>, //[u8; 0x10000]),
    pointer_vram: u16,
    status_reg: u8,
    magnif_num: u8,
}
impl Default for VdpData {
    fn default() -> Self {
        Self::new()
    }
}

impl VdpData {
    pub fn new() -> Self {
        Self {
            screen_enabled: false,
            screen_mode: 0,
            value_read: 0,
            write_state: false,
            enabled_interrupts: false,
            registers: [0; 10],
            write_to_vram: false,
            vram: vec![0; 0x10000],
            pointer_vram: 0,
            status_reg: 0,
            magnif_num: 0,
        }
    }
}

pub struct Vdp<'a> {
    pub(crate) data: VdpData,
    graphics: GraphicsDriver<'a>,
}

impl Vdp<'_> {
    pub fn new(graphics_type: GraphicsType, quality: bool) -> Self {
        let graphics = graphics_type.create(quality);
        Self {
            data: VdpData::default(),
            graphics,
        }
    }

    // pub fn save_state(&self) -> Vdp {
    //     self.clone()
    // }

    // pub fn restore_state(&mut self, vdp2: &Vdp) {
    //     self.data.screen_enabled = vdp2.screen_enabled;
    //     self.data.screen_mode = vdp2.screen_mode;
    //     self.data.value_read = vdp2.value_read;
    //     self.data.write_state = vdp2.write_state;
    //     self.data.enabled_interrupts = vdp2.enabled_interrupts;
    //     self.data.registers = vdp2.registers;
    //     self.data.write_to_vram = vdp2.write_to_vram;
    //     self.data.vram = vdp2.vram.clone();
    //     self.data.pointer_vram = vdp2.pointer_vram;
    //     self.data.status_reg = vdp2.status_reg;
    //     self.data.magnif_num = vdp2.magnif_num;
    // }

    pub fn has_enabled_interrupts(&self) -> bool {
        self.data.enabled_interrupts
    }
    pub fn set_frame_flag(&mut self) {
        self.data.status_reg |= 0x80;
    }

    pub fn update_registers(&mut self) {
        self.data.screen_enabled = self.data.registers[1] & 0x40 != 0;
        self.data.enabled_interrupts = self.data.registers[1] & 0x20 != 0;
        let m1 = self.data.registers[1] & 0x10 != 0;
        let m2 = self.data.registers[1] & 0x08 != 0;
        let m3 = self.data.registers[0] & 0x02 != 0;
        let m4 = self.data.registers[0] & 0x04 != 0;
        let m5 = self.data.registers[0] & 0x08 != 0;
        let scm = self.data.screen_mode;

        if !m4 && !m5 {
            if !m1 && !m2 && !m3 {
                self.data.screen_mode = SCREEN1;
            }
            if m1 && !m2 && !m3 {
                self.data.screen_mode = SCREEN0;
            }
            if !m1 && m2 && !m3 {
                self.data.screen_mode = SCREEN3;
            }
            if !m1 && !m2 && m3 {
                self.data.screen_mode = SCREEN2;
            }
        }
        if scm != self.data.screen_mode {
            log::info!("Change screen mode: {}", self.data.screen_mode);
            self.graphics.set_logical_resolution(self.data.screen_mode);
        }
    }

    pub fn write_port(&mut self, ad: u8, mut val: u8) {
        // log::info!("VDP: Out({:02x}, {:02x})", ad, val);
        match ad {
            0x99 => {
                if !self.data.write_state {
                    self.data.value_read = val;
                    self.data.write_state = true;
                } else {
                    self.data.write_state = false;
                    // Bit 7 must be 1 for write
                    if val & 0x80 != 0 {
                        let regn = val - 128;
                        self.data.registers[regn as usize] = self.data.value_read;
                        self.update_registers();
                    } else {
                        self.data.write_to_vram = val & 0x40 != 0;
                        val &= 0xBF;
                        self.data.pointer_vram = 0;
                        self.data.pointer_vram |= self.data.value_read as u16;
                        self.data.pointer_vram |= (val as u16) << 8;
                    }
                }
            }
            0x98 => {
                // Writing to VRAM
                // log::info!("Writing to VRAM: {:04x} -> {:02x}", self.data.pointer_vram, val);
                self.data.vram[self.data.pointer_vram as usize] = val;
                self.data.pointer_vram += 1;
            }
            _ => {
                log::error!("Not implemented: VDP: Out({:02x}, {:02x})", ad, val);
                unimplemented!()
            }
        }
    }

    pub fn read_port(&mut self, ad: u8) -> u8 {
        match ad {
            0x98 => {
                // Reading from VRAM
                //log.Printf("Reading from VRAM: %04x", vdp.pointerVRAM)
                let r = self.data.vram[self.data.pointer_vram as usize];
                self.data.pointer_vram += 1;
                r
            }
            0x99 => {
                // Reading status register
                // TODO: look at it carefully....
                let r = self.data.status_reg;
                self.data.status_reg &= 0x7F; // Clear frame flag
                r
            }
            _ => {
                log::error!("Not implemented: VDP: In({:02x})", ad);
                0
            }
        }
    }

    pub fn graphics_render(&mut self, sys: &mut BaseSystem) {
        self.graphics.render(sys);
    }
    pub fn update_buffer(&mut self) {
        if !self.data.screen_enabled {
            return;
        }
        let name_table_addr = (self.data.registers[2] as usize) << 10;
        let pat_table_addr = (self.data.registers[4] as usize) << 11;
        let color_table_addr = (self.data.registers[3] as usize) << 6;

        match self.data.screen_mode {
            SCREEN0 => {
                // Render SCREEN0 (40x24)
                let color1 = (self.data.registers[7] & 0xF0) >> 4;
                let color2 = self.data.registers[7] & 0x0F;
                for y in 0..24 {
                    for x in 0..40 {
                        let name_table = &self.data.vram[name_table_addr..];
                        let pt = name_table[(x + y * 40) as usize] as u16 * 8;
                        self.draw_patterns_s0(x * 8, y * 8, pt, pat_table_addr, color1, color2);
                    }
                }
            }
            SCREEN1 => {
                // Render SCREEN1 (32x24)
                for y in 0..24 {
                    for x in 0..32 {
                        let name_table = &self.data.vram[name_table_addr..];
                        let pat = name_table[(x + y * 32) as usize];
                        let color_table = &self.data.vram[color_table_addr..];
                        let color = color_table[(pat / 8) as usize];
                        self.draw_patterns_s1(x * 8, y * 8, pat as u16 * 8, pat_table_addr, color);
                    }
                }
                self.draw_sprites();
            }
            SCREEN2 => {
                // Render SCREEN2
                // Pattern table: 0000H to 17FFH
                let pat_table_addr = ((self.data.registers[4] & 0x04) as usize) << 11;
                let color_table_addr = ((self.data.registers[3] & 0x80) as usize) << 6;
                for y in 0..24 {
                    for x in 0..32 {
                        let name_table = &self.data.vram[name_table_addr..];
                        let pat = name_table[(x + y * 32) as usize];
                        self.draw_patterns_s2(
                            x * 8,
                            y * 8,
                            pat as u16 * 8,
                            pat_table_addr,
                            color_table_addr,
                        );
                    }
                }
                self.draw_sprites();
            }
            SCREEN3 => {
                // Render SCREEN3
                log::error!("Drawing in screen3 not implemented yet");
            }
            _ => {
                panic!("RenderScreen: impossible mode");
            }
        }
    }

    pub fn get_vram_data(&self, base_addr: usize, offset: usize) -> u8 {
        self.data.vram[base_addr + offset]
    }
    pub fn draw_patterns_s0(
        &mut self,
        x: u16,
        y: u16,
        pt: u16,
        pat_table_addr: usize,
        color1: u8,
        color2: u8,
    ) {
        for i in 0..8 {
            let b = self.get_vram_data(pat_table_addr, (i + pt) as usize);
            let mut xx = 0;
            let mut mask = 0x80;
            while mask > 0 {
                if mask & b != 0 {
                    self.graphics
                        .draw_pixel((x + xx).into(), (y + i).into(), color1.into());
                } else {
                    self.graphics
                        .draw_pixel((x + xx).into(), (y + i).into(), color2.into());
                }
                xx += 1;
                mask >>= 1;
            }
        }
    }

    fn draw_patterns_s1(&mut self, x: u16, y: u16, pt: u16, pat_table_addr: usize, color: u8) {
        let color1 = (color & 0xF0) as usize >> 4;
        let color2 = (color & 0x0F) as usize;
        // let mask: u8 = 0;
        for i in 0..8 {
            let b = self.get_vram_data(pat_table_addr, (i + pt) as usize);
            let mut xx = 0;
            let mut mask = 0x80;
            while mask > 0 {
                if mask & b != 0 {
                    self.graphics
                        .draw_pixel((x + xx).into(), (y + i).into(), color1);
                } else {
                    self.graphics
                        .draw_pixel((x + xx).into(), (y + i).into(), color2);
                }
                xx += 1;
                mask >>= 1;
            }
        }
    }

    fn draw_patterns_s2(
        &mut self,
        x: u16,
        y: u16,
        pt: u16,
        pat_table_addr: usize,
        color_table_addr: usize,
    ) {
        for i in 0..8 {
            let idx = if y < 64 {
                (i + pt) as usize
            } else if y < 128 {
                (i + pt + 2048) as usize
            } else {
                (i + pt + 2048 * 2) as usize
            };
            let b = self.get_vram_data(pat_table_addr, idx);
            let color = self.get_vram_data(color_table_addr, idx);
            let color1 = (color & 0xF0) >> 4;
            let color2 = color & 0x0F;
            let mut xx = 0;
            let mut mask = 0x80;
            while mask > 0 {
                if mask & b != 0 {
                    self.graphics
                        .draw_pixel((x + xx).into(), (y + i).into(), color1.into());
                } else {
                    self.graphics
                        .draw_pixel((x + xx).into(), (y + i).into(), color2.into());
                }
                xx += 1;
                mask >>= 1;
            }
        }
    }

    fn draw_sprites(&mut self) {
        // Sprite name table: 1B00H to 1B7FH
        // Sprite pattern table: 3800H to 3FFFH
        let spr_table_addr = (self.data.registers[5] as usize) << 7;
        let spr_pat_table_addr = (self.data.registers[6] as usize) << 11;
        let magnif = (self.data.registers[1] & 0x01) != 0;
        let spr16x16 = (self.data.registers[1] & 0x02) != 0;
        self.data.magnif_num = 0;
        let mut i = 0;
        let mut j = 0;
        while i < 32 {
            let ypos = self.data.vram[spr_table_addr + j] as u16;
            if ypos == 0xd0 {
                // Ignore all sprites
                return;
            }
            let mut xpos = self.data.vram[spr_table_addr + j + 1] as u16;
            let pattern = self.data.vram[spr_table_addr + j + 2];
            let ec = (self.data.vram[spr_table_addr + j + 3] & 0x80) != 0;
            if ec {
                xpos -= 32
            }
            let color = self.data.vram[spr_table_addr + j + 3] & 0x0F;
            if !spr16x16 {
                let pattern_addr = spr_pat_table_addr + (pattern as usize) * 8;
                self.draw_spr(magnif, xpos, ypos, pattern_addr, color);
            } else {
                let pattern_addr = spr_pat_table_addr + ((pattern >> 2) as usize) * 8 * 4;
                self.draw_spr(magnif, xpos, ypos, pattern_addr, color);
                self.draw_spr(magnif, xpos, ypos + 8, pattern_addr + 8, color);
                self.draw_spr(magnif, xpos + 8, ypos, pattern_addr + 16, color);
                self.draw_spr(magnif, xpos + 8, ypos + 8, pattern_addr + 24, color);
            }

            i += 1;
            j += 4;
        }
    }

    fn draw_spr(
        &mut self,
        magnif: bool,
        xpos: u16,
        ypos: u16,
        pattern_addr: usize,
        // ec: bool,
        color: u8,
    ) {
        if ypos > 191 {
            return;
        }

        for y in 0..8 {
            let b = self.get_vram_data(pattern_addr, y as usize);
            let mut x = 0;
            let mut mask = 0x80;
            while mask > 0 {
                if magnif && x == 0 && y == 0 {
                    if self.data.magnif_num == 4 {
                        self.data.magnif_num = 1;
                    } else {
                        self.data.magnif_num += 1;
                    }
                }
                if mask & b != 0 {
                    if magnif {
                        let mut x = x * 2;
                        let mut y = y * 2;
                        if self.data.magnif_num == 2 || self.data.magnif_num == 4 {
                            y += 8;
                        }
                        if self.data.magnif_num == 3 || self.data.magnif_num == 4 {
                            x += 8;
                        }
                        self.graphics.draw_pixel(
                            (xpos + x).into(),
                            (ypos + y).into(),
                            color.into(),
                        );
                        self.graphics.draw_pixel(
                            (xpos + x + 1).into(),
                            (ypos + y).into(),
                            color.into(),
                        );
                        self.graphics.draw_pixel(
                            (xpos + x).into(),
                            (ypos + y + 1).into(),
                            color.into(),
                        );
                        self.graphics.draw_pixel(
                            (xpos + x + 1).into(),
                            (ypos + y + 1).into(),
                            color.into(),
                        );
                    } else {
                        self.graphics.draw_pixel(
                            (xpos + x).into(),
                            (ypos + y).into(),
                            color.into(),
                        );
                    }
                }
                x += 1;

                mask >>= 1;
            }
        }
    }
    pub fn get_data(&self) -> VdpData {
        self.data.clone()
    }
    pub fn set_data(&mut self, data: VdpData) {
        self.data = data;
    }
}