1use serde::{Deserialize, Serialize};
17
18use crate::machine::{
19 Machine, ACTIVE_LINES, FRAME_LINES_LONG, FRAME_LINES_SHORT, HSYNC_CLOCKS, LINE_CLOCKS,
20 TOP_BLANK_LINES, VSYNC_LINES,
21};
22use crate::sockets::VideoTiming;
23use crate::vce::Vce;
24
25#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
26pub struct Timeline {
27 lines_follow_vce_bit2: bool,
28 pub frame: i64,
29 pub frame_lines: i64,
30 pub line: i64,
31 pub line_start: i64,
32}
33
34impl Timeline {
35 pub fn new(
39 machine: &Machine,
40 vce: &Vce,
41 start_line: i64,
42 start_offset: i64,
43 ) -> Result<Timeline, String> {
44 let mut timeline = Timeline {
45 lines_follow_vce_bit2: machine.lines_follow_vce_bit2,
46 frame: 0,
47 frame_lines: 0,
48 line: start_line,
49 line_start: -start_offset,
50 };
51 timeline.frame_lines = timeline.lines_for_frame(vce);
52 if !(0..timeline.frame_lines).contains(&start_line) {
53 return Err(format!(
54 "start_line {start_line} outside the {}-line frame",
55 timeline.frame_lines
56 ));
57 }
58 Ok(timeline)
59 }
60
61 fn lines_for_frame(&self, vce: &Vce) -> i64 {
62 if self.lines_follow_vce_bit2 && !vce.bit2() {
64 return FRAME_LINES_SHORT;
65 }
66 FRAME_LINES_LONG
67 }
68
69 #[inline]
70 pub fn next_line_start(&self) -> i64 {
71 self.line_start + LINE_CLOCKS
72 }
73
74 pub fn in_vsync(&self) -> bool {
75 self.line >= self.frame_lines - VSYNC_LINES
76 }
77
78 pub fn timing(&self, vce: &Vce) -> VideoTiming {
79 VideoTiming {
80 frame: self.frame,
81 line: self.line,
82 start_mclk: self.line_start,
83 dot_divider: vce.dot_divider(),
84 frame_lines: self.frame_lines,
85 in_vsync: self.in_vsync(),
86 }
87 }
88
89 pub fn advance(&mut self, vce: &Vce) {
91 self.line_start += LINE_CLOCKS;
92 self.line += 1;
93 if self.line >= self.frame_lines {
94 self.line = 0;
95 self.frame += 1;
96 self.frame_lines = self.lines_for_frame(vce);
97 }
98 }
99}
100
101#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
103pub struct Frame {
104 pub number: i64,
105 pub rows: Vec<Vec<u16>>,
106 pub dividers: Vec<u8>,
107}
108
109#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
110pub struct Capture {
111 shadow: Vec<u16>,
113 rows: Vec<Vec<u16>>,
114 dividers: Vec<u8>,
115 pub last_frame: Option<Frame>,
116}
117
118impl Capture {
119 pub fn new(vce: &Vce) -> Capture {
120 Capture {
121 shadow: vce.palette.clone(),
122 rows: Vec::new(),
123 dividers: Vec::new(),
124 last_frame: None,
125 }
126 }
127
128 pub fn finish_line(&mut self, timing: &VideoTiming, row: &[u16], vce: &mut Vce) {
135 let first = timing.start_mclk + HSYNC_CLOCKS;
136 let end = first + LINE_CLOCKS;
137 let div = timing.dot_divider;
138 let log = &mut vce.palette_log;
139 let mut used = 0;
140 while used < log.len() && log[used].0 < end {
141 used += 1;
142 }
143 let mut colours = Vec::with_capacity(row.len());
144 let mut pending = 0;
145 let shadow = &mut self.shadow;
146 for (x, &pixel) in row.iter().enumerate() {
147 let when = first + x as i64 * div;
148 while pending < used && log[pending].0 <= when {
149 shadow[log[pending].1 as usize] = log[pending].2;
150 pending += 1;
151 }
152 let mut pixel = pixel;
153 if pixel & 0x0F == 0 {
154 pixel &= 0x100;
156 }
157 colours.push(shadow[pixel as usize]);
158 }
159 while pending < used {
160 shadow[log[pending].1 as usize] = log[pending].2;
161 pending += 1;
162 }
163 log.drain(..used);
164 if timing.line == 0 {
165 self.rows = Vec::new();
166 self.dividers = Vec::new();
167 }
168 if (TOP_BLANK_LINES..TOP_BLANK_LINES + ACTIVE_LINES).contains(&timing.line) {
169 self.rows.push(colours);
170 self.dividers.push(div as u8);
171 if self.rows.len() as i64 == ACTIVE_LINES {
172 self.last_frame = Some(Frame {
173 number: timing.frame,
174 rows: std::mem::take(&mut self.rows),
175 dividers: std::mem::take(&mut self.dividers),
176 });
177 }
178 }
179 }
180}