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
extern crate cdg;
extern crate image;

use std::ops::{Index,IndexMut,Fn,Add};

pub trait One {
    fn one() -> Self;
}

impl One for u16 {
    fn one() -> Self { 1 }
}
impl One for u8 {
    fn one() -> Self { 1 }
}

#[derive(Clone,Copy,Debug)]
pub struct Position<T> {
    x: T,
    y: T,
}

impl <T> Position<T> {
    pub fn new(x: T, y: T) -> Self {
        Position{x: x, y: y}
    }
}

#[derive(Copy,Clone,Debug)]
pub struct Rectangle<T> {
    pub nw: Position<T>,
    pub se: Position<T>,
}

impl <T: Ord + Copy + Add<Output=T> + One> Rectangle<T> {
    pub fn new(p0: Position<T>, p1: Position<T>) -> Self {
        use std::cmp::{min,max};
        Rectangle{nw: Position{ x: min(p0.x, p1.x), y: min(p0.y, p1.y)},
                  se: Position{ x: max(p0.x, p1.x), y: max(p0.y, p1.y)}}
    }
    pub fn expand(&self, p: Position<T>) -> Self {
        use std::cmp::{min,max};
        Rectangle{nw: Position{ x: min(self.nw.x, p.x), y: min(self.nw.y, p.y)},
                  se: Position{ x: max(self.se.x, p.x+T::one()), y: max(self.se.y, p.y+T::one())}}
    }
}

const TILE_ROWS: usize = 18;
const TILE_COLS: usize = 50;

pub struct CdgInterpreter {
    tile_shift: Position<u16>,
    pixel_shift: Position<u16>,
    clut: [cdg::RgbColor; 16],
    dirty: Option<Rectangle<u16>>, // in tiles
    content: [[u8;300];216],
    border: u8,
    transparent: u8, // is 0..15 if a color is transparent, 0xff if not
}

struct TileView<'a> {
    interp: &'a mut CdgInterpreter,
    x: usize, // both x and y are in pixels
    y: usize,
}

impl <'a> TileView<'a> {
    // takes x,y, old_val
    fn map_pixels<F>(&mut self, func: F)
        where F : Fn(u8,u8, &mut u8) {
        for y in 0..12 {
            for x in 0..6 {
                func(x,y, &mut self[(x,y)])
            }
        }
    }
    
    fn draw_normal(&mut self, tile: &cdg::Tile) {
        self.map_pixels(|x,y, px| *px = tile.get_pixel(x,y))
    }

    fn draw_xor(&mut self, tile: &cdg::Tile) {
        self.map_pixels(|x,y, px| *px ^= tile.get_pixel(x,y))
    }
}

impl <'b> Index<(u8,u8)> for TileView<'b> {
    type Output = u8;
    fn index<'a>(&'a self, pos: (u8,u8)) -> &'a u8 {
        assert!(pos.0 < 6 && pos.1 < 12);
        unsafe {
            self.interp.content.get_unchecked(self.y + pos.1 as usize).get_unchecked(self.x + pos.0 as usize)
        }
    }
}

impl <'b> IndexMut<(u8,u8)> for TileView<'b> {
    fn index_mut<'a>(&'a mut self, pos: (u8,u8)) -> &'a mut u8 {
        assert!(pos.0 < 6 && pos.1 < 12);
        unsafe {
            self.interp.content.get_unchecked_mut(self.y + pos.1 as usize).get_unchecked_mut(self.x + pos.0 as usize)
        }
    }
}


/// Basic accessors, constructors
impl CdgInterpreter {
    pub fn new() -> Self {
        use cdg::RgbColor;
        CdgInterpreter {
            tile_shift: Position::new(0,0),
            pixel_shift: Position::new(0,0),
            // Default to CGA-ish palette
            clut: [RgbColor::from_rgb(0,0,0),
                   RgbColor::from_rgb(0,0,170),
                   RgbColor::from_rgb(0,170,0),
                   RgbColor::from_rgb(0,170,170),
                   RgbColor::from_rgb(170,0,0),
                   RgbColor::from_rgb(170,0,170),
                   RgbColor::from_rgb(170,170,0),
                   RgbColor::from_rgb(170,170,170),
                   RgbColor::from_rgb(85,85,85),
                   RgbColor::from_rgb(85,85,255),
                   RgbColor::from_rgb(85,255,85),
                   RgbColor::from_rgb(85,255,255),
                   RgbColor::from_rgb(255,85,85),
                   RgbColor::from_rgb(255,85,255),
                   RgbColor::from_rgb(255,255,85),
                   RgbColor::from_rgb(255,255,255)],
            dirty: Some(Rectangle::new(Position::new(0,0),
                                       Position::new(50,18))),
            content: [[0;300];216],
            border: 0,
            transparent: 255,
        }
    }
    #[allow(unused)]
    fn map_pxrow(&self, row: usize) -> usize {
        (row + self.tile_shift.y as usize * 12) % 216
    }

    #[allow(unused)]
    fn map_pxcol(&self, row: usize) -> usize {
        (row + self.tile_shift.x as usize * 6) % 300
    }

    fn map_trow(&self, row: usize) -> usize {
        (row + self.tile_shift.y as usize) % TILE_ROWS
    }

    fn map_tcol(&self, col: usize) -> usize {
        (col + self.tile_shift.x as usize) % TILE_COLS
    }

    fn clear_col(&mut self, n: usize, color: Option<u8>) {
        match color {
            None => (),
            Some(color) => {
                let col = self.map_tcol(n) * 6;
                for r in 0..216 {
                    for c in col..col+6 {
                        unsafe {*self.content.get_unchecked_mut(r).get_unchecked_mut(c) = color; }
                    }
                }
            }
        }
    }

    fn clear_row(&mut self, n: usize, color: Option<u8>) {
        match color {
            None => (),
            Some(color) => {
                let col = self.map_trow(n) * 12;
                for c in col..col+12 {
                    for r in 0..216 {
                        unsafe {*self.content.get_unchecked_mut(r).get_unchecked_mut(c) = color; }
                    }
                }
            }
        }
    }    
    
    fn get_tile(&mut self, pos: (u8,u8)) -> TileView {
        let x = (pos.0 as u16 + self.tile_shift.x) as usize % TILE_COLS;
        let y = (pos.1 as u16 + self.tile_shift.y) as usize % TILE_ROWS;

        TileView{
            interp: self,
            x: x * 6,
            y: y * 12,
        }
    }

    pub fn dirty(&self) -> Option<Rectangle<u16>> {
        self.dirty
    }

    fn invalidate_tile(&mut self, pos: (u8, u8)) {
        let new_tile = Position::new(pos.0 as u16, pos.1 as u16);
        let new_tilep = Position::new(pos.0 as u16 + 1, pos.1 as u16 + 1);
        self.dirty = self.dirty
            .map(|x| x.expand(new_tile))
            .or(Some(Rectangle::new(new_tile,new_tilep)));
    }

    fn invalidate_all(&mut self) {
        self.dirty = Some(Rectangle::new(Position::new(0,0),
                                         Position::new(50,18)));
    }

    /// Mark the entire region clean
    pub fn clear_dirty_region(&mut self) {
        self.dirty = None;
    }

    /*
    // For performance reasons, always produces an RGBA image
    fn scanout<Image>(&self, image: &mut Image, region: Rectangle<u16>) {
        for y in region.nw.y as usize..region.se.y as usize {
            for x in region.nw.x as usize..region.se.x as usize {
                pos = (y * stride + x) * 4;
                buffer[y * stride + x] = 
            }
        }
    }
     */
}

impl image::GenericImage for CdgInterpreter {
    type Pixel = image::Rgba<u8>;

    fn dimensions(&self) -> (u32,u32) {
        (300,216)
    }

    fn bounds(&self) -> (u32,u32,u32,u32) {
        (0, 0, 300, 216)
    }

    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
        use image::Pixel;
        let cindex = self.content[self.map_pxrow(y as usize)][self.map_pxcol(x as usize)];
        if self.transparent == cindex {
            image::Rgba::from_channels(0,0,0,0)
        } else {
            let c = self.clut[cindex as usize];
            image::Rgba::from_channels(c.r(), c.g(), c.b(), 255)
        }
    }

    #[allow(unused_variables)]
    fn get_pixel_mut(&mut self, x: u32, y: u32) -> &mut Self::Pixel {
        unimplemented!();
    }

    #[allow(unused_variables)]
    fn put_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel) {
        unimplemented!();
    }

    #[allow(unused_variables)]
    fn blend_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel) {
        unimplemented!();
    }
}


impl CdgInterpreter {
    pub fn handle_cmd(&mut self, command: cdg::Command) {
        use cdg::Command::*;
        match command {
            MemoryPreset{color, repeat: 0} => {
                for y in 0..216 {
                    for x in 0..300 {
                        self.content[y][x] = color;
                    }
                }
                self.invalidate_all();
            },
            MemoryPreset{..} => (),
            BorderPreset{color} => {self.border = color; self.invalidate_all(); },
            TileNormal{tile} => { self.get_tile(tile.pos).draw_normal(&tile); self.invalidate_tile(tile.pos); },
            TileXOR{tile} => { self.get_tile(tile.pos).draw_xor(&tile); self.invalidate_tile(tile.pos); },
            Scroll{color, cmd: (xc, yc), offset: (xo,yo)} => {
                use cdg::ScrollCommand::{NW,SE,Noop};
                // Handle horizontal scrolling first
                match xc {
                    NW => {
                        self.clear_col(0, color);
                        self.tile_shift.x = (self.tile_shift.x + 1) % TILE_COLS as u16;
                    }
                    SE => {
                        self.tile_shift.x = (self.tile_shift.x + TILE_COLS as u16 - 1) % TILE_COLS as u16;
                        self.clear_col(0, color);
                    }
                    Noop => (),
                }
                match yc {
                    NW => {
                        self.clear_row(0, color);
                        self.tile_shift.y = (self.tile_shift.y + 1) % TILE_ROWS as u16;
                    }
                    SE => {
                        self.tile_shift.y = (self.tile_shift.y + TILE_ROWS as u16 - 1) % TILE_ROWS as u16;
                        self.clear_row(0, color);
                    }
                    Noop => (),
                }
                self.pixel_shift = Position::new(xo as u16 % 6, yo as u16 % 12);
                self.invalidate_all();
            },
            SetTransparent{color} => {self.transparent = color; self.invalidate_all(); },
            LoadPalette{offset, clut} => {
                let off = offset as usize;
                self.clut[off..off+8].copy_from_slice(&clut);
                self.invalidate_all();
            }
            
        }
    }    
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}