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
#![warn(missing_docs)]
use std::io::Read;
use std::fmt;
#[derive(Debug)]
pub struct Tile {
pub pos: (u8,u8),
pub color: (u8,u8),
pub content: [u8;12],
}
impl Tile {
fn from(data: &[u8]) -> Self {
if data.len() != 16 {
panic!("Data is the wrong size");
}
let mut content = [0; 12];
iter_copy(content[..].iter_mut(), data[4..16].iter().map(|x| x & 0x3F));
return Tile{
pos: (data[3] & 0x3F, data[2] & 0x1F),
color: (data[0] & 0x0F, data[1] & 0x0F),
content: content,
};
}
pub fn get_pixel(&self, x: u8, y: u8) -> u8 {
assert!(x < 6);
assert!(y < 12);
if self.content[y as usize] & (0x20 >> x) == 0 {
self.color.0
} else {
self.color.1
}
}
}
#[derive(Eq,PartialEq,Debug,Copy,Clone)]
pub enum ScrollCommand {
Noop,
NW,
SE,
}
impl ScrollCommand {
fn from_u8(x: u8) -> Self {
match x & 0x30 {
0x0 => ScrollCommand::Noop,
0x10 => ScrollCommand::SE,
0x20 => ScrollCommand::NW,
_ => ScrollCommand::Noop,
}
}
}
fn iter_copy<'a, T: Copy + 'a, DI: Iterator<Item=&'a mut T>, SI: Iterator<Item=T>>(dest: DI, src: SI) {
for (dest,src) in dest.zip(src) {
*dest = src;
}
}
fn expand4to8(x: u16) -> u8 {
(x | x << 4) as u8
}
#[derive(Copy,Eq,PartialEq,Clone)]
pub struct RgbColor(u16);
impl fmt::Debug for RgbColor {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("RGB")
.field(&self.r())
.field(&self.g())
.field(&self.b())
.finish()
}
}
impl RgbColor {
fn from_subchannel(data0: u8, data1: u8) -> Self {
RgbColor((data0 as u16 & 0x3F) << 6 | (data1 as u16 & 0x3F))
}
pub fn from_rgb(r: u8, g: u8, b: u8) -> RgbColor {
let r = r as u16;
let g = g as u16;
let b = b as u16;
RgbColor((r & 0xF0 << 4) | (g & 0xF0) | (b >> 4))
}
pub fn r(&self) -> u8 {expand4to8((self.0 >> 8) & 0xF)}
pub fn g(&self) -> u8 {expand4to8((self.0 >> 4) & 0xF)}
pub fn b(&self) -> u8 {expand4to8((self.0 >> 0) & 0xF)}
}
#[allow(missing_docs)]
#[derive(Debug)]
pub enum Command {
MemoryPreset{color: u8, repeat: u8},
BorderPreset{color: u8},
TileNormal{tile: Tile},
TileXOR{tile: Tile},
Scroll{color: Option<u8>, cmd: (ScrollCommand, ScrollCommand), offset: (u8, u8)},
SetTransparent{color: u8},
LoadPalette{offset: u8, clut: [RgbColor; 8]},
}
fn parse_scroll(data: &[u8], is_copy: bool) -> Command {
if data.len() != 16 {
panic!("INvalid data length");
}
let color = if is_copy { None } else { Some(data[0] & 0xF) };
let h_scroll_cmd = ScrollCommand::from_u8(data[1]);
let h_scroll_off = data[1] & 0x07;
let v_scroll_cmd = ScrollCommand::from_u8(data[2]);
let v_scroll_off = data[2] & 0x0F;
Command::Scroll{
color: color,
cmd: (h_scroll_cmd, v_scroll_cmd),
offset: (h_scroll_off, v_scroll_off),
}
}
fn parse_clut(data: &[u8]) -> [RgbColor; 8] {
let mut result = [RgbColor::from_subchannel(0,0); 8];
iter_copy(result.iter_mut(), data.chunks(2).map(|c| RgbColor::from_subchannel(c[0], c[1])));
result
}
pub fn decode_subchannel_cmd(block: &[u8]) -> Option<Command> {
if block.len() != 24 {
return None
}
if block[0] & 0x3f != 9 {
return None;
}
let data = &block[4..20];
match block[1] & 0x3f {
1 => Some(Command::MemoryPreset{color: data[0] & 0xF, repeat: data[1] & 0xF}),
2 => Some(Command::BorderPreset{color: data[0] & 0xF}),
6 => Some(Command::TileNormal{tile: Tile::from(data)}),
38 => Some(Command::TileXOR{tile: Tile::from(data)}),
20 => Some(parse_scroll(data, false)),
24 => Some(parse_scroll(data, true)),
28 => Some(Command::SetTransparent{color: data[0] & 0xF}),
30 => Some(Command::LoadPalette{offset: 0, clut: parse_clut(data)}),
31 => Some(Command::LoadPalette{offset: 8, clut: parse_clut(data)}),
_ => None,
}
}
pub struct SectorIter<'a> {
sector_iter: std::slice::Chunks<'a, u8>,
}
impl <'a> SectorIter<'a> {
pub fn new(sector: &'a [u8]) -> Self {
assert!(sector.len() >= 96);
assert!(sector.len() % 24 == 0);
SectorIter{
sector_iter: sector.chunks(24),
}
}
}
impl <'a> Iterator for SectorIter<'a> {
type Item = Command;
fn next(&mut self) -> Option<Self::Item> {
match self.sector_iter.next() {
None => None,
Some(cmd) => decode_subchannel_cmd(cmd).or_else(|| self.next())
}
}
}
pub struct SubchannelStreamIter<R: Read> {
sector_buf: [u8; 96],
reader: R,
}
impl <R: Read> SubchannelStreamIter<R> {
pub fn new(reader: R) -> Self {
SubchannelStreamIter{
sector_buf: [0;96],
reader: reader,
}
}
pub fn next(&mut self) -> Option<SectorIter> {
match self.reader.read_exact(&mut self.sector_buf) {
Ok(_) => Some(SectorIter::new(&self.sector_buf)),
Err(_) => None,
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}