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
/*
    Copyright (C) 2020-2022  Rafal Michalski

    This file is part of SPECTRUSTY, a Rust library for building emulators.

    For the full copyright notice, see the lib.rs file.
*/
use core::num::NonZeroU32;
use core::slice;
use std::io::{Result, Write};

use super::consts::*;

const SYNC_PULSE_TOLERANCE: u32 = (SYNC_PULSE2_LENGTH.get() - SYNC_PULSE1_LENGTH.get())/2;
const SYNC_PULSE1_MIN: u32 = SYNC_PULSE1_LENGTH.get() - SYNC_PULSE_TOLERANCE;
const SYNC_PULSE1_MAX: u32 = SYNC_PULSE1_LENGTH.get() + SYNC_PULSE_TOLERANCE - 1;
const SYNC_PULSE2_MIN: u32 = SYNC_PULSE2_LENGTH.get() - SYNC_PULSE_TOLERANCE;
const SYNC_PULSE2_MAX: u32 = SYNC_PULSE2_LENGTH.get() + SYNC_PULSE_TOLERANCE - 1;
const LEAD_PULSE_TOLERANCE: u32 = 250;
const LEAD_PULSE_MIN: u32 = LEAD_PULSE_LENGTH.get() - LEAD_PULSE_TOLERANCE;
const LEAD_PULSE_MAX: u32 = LEAD_PULSE_LENGTH.get() + LEAD_PULSE_TOLERANCE - 1;
const DATA_PULSE_TOLERANCE: u32 = 250;
const DATA_PULSE_MIN: u32 = ZERO_PULSE_LENGTH.get() - DATA_PULSE_TOLERANCE;
const DATA_PULSE_MAX: u32 = ONE_PULSE_LENGTH.get() + DATA_PULSE_TOLERANCE - 1;
const DATA_PULSE_THRESHOLD: u32 = (ZERO_PULSE_LENGTH.get() + ONE_PULSE_LENGTH.get())/2;
const MIN_LEAD_COUNT: u32 = 16;

/// The current state of the [PulseDecodeWriter].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PulseDecodeState {
    /// Initial state, waiting for lead pulses.
    Idle,
    /// Receiving lead pulses.
    Lead {
        counter: u32
    },
    /// Received the 1st sync pulse.
    Sync1,
    /// Received the 2nd sync pulse.
    Sync2,
    /// Receiving data pulses.
    Data{
        /// A data byte being received currently.
        current: u8,
        /// A pulse counter for the current byte.
        pulse: u8,
        /// How many bytes written.
        written: u32
    },
}

/// Provides a decoder of *TAPE* T-state pulse intervals.
///
/// The timing of the pulses should match those produced by ZX Spectrum's ROM loading routines.
///
/// After invoking [PulseDecodeWriter::end] or [PulseDecodeWriter::new] [PulseDecodeWriter] expects
/// a data transfer which consists of:
///
/// * lead pulses followed by
/// * two synchronization pulses followed by 
/// * data pulses
///
/// Provide pulse iterators to [PulseDecodeWriter::write_decoded_pulses] method to write interpreted data
/// to the underlying writer.
///
/// Best used with [tap][crate::tap] utilities.
#[derive(Debug)]
pub struct PulseDecodeWriter<W> {
    state: PulseDecodeState,
    wr: W,
}

impl PulseDecodeState {
    /// Returns `true` if the state is [PulseDecodeState::Idle].
    pub fn is_idle(&self) -> bool {
        matches!(self, PulseDecodeState::Idle)
    }
    /// Returns `true` if receiving lead pulses.
    pub fn is_lead(&self) -> bool {
        matches!(self, PulseDecodeState::Lead {..})
    }
    /// Returns `true` if receiving data pulses.
    pub fn is_data(&self) -> bool {
        matches!(self, PulseDecodeState::Data {..})
    }
    /// Returns `true` if received sync1 pulse.
    pub fn is_sync1(&self) -> bool {
        matches!(self, PulseDecodeState::Sync1)
    }
    /// Returns `true` if received sync2 pulse.
    pub fn is_sync2(&self) -> bool {
        matches!(self, PulseDecodeState::Sync2)
    }
}

impl<W> PulseDecodeWriter<W> {
    /// Resets the state of the [PulseDecodeWriter] to `Idle`, discarding any partially received byte.
    ///
    /// The information about the number of bytes written so far is lost.
    pub fn reset(&mut self) {
        self.state = PulseDecodeState::Idle;
    }
    /// Returns `true` if the state is [PulseDecodeState::Idle].
    pub fn is_idle(&self) -> bool {
        self.state.is_idle()
    }
    /// Returns the underlying writer.
    pub fn into_inner(self) -> W {
        self.wr
    }
    /// Returns a reference to the current state.
    pub fn state(&self) -> PulseDecodeState {
        self.state
    }
    /// Returns a mutable reference to the inner writer.
    pub fn get_mut(&mut self) -> &mut W {
        &mut self.wr
    }
    /// Returns a shared reference to the inner writer.
    pub fn get_ref(&self) -> &W {
        &self.wr
    }
    /// Returns a number of bytes written during current data transfer
    /// if a data transfer is in progress.
    pub fn data_size(&self) -> Option<NonZeroU32> {
        NonZeroU32::new(if let PulseDecodeState::Data { written, .. } = self.state {
            written
        }
        else {
            0
        })
    }
    /// Allows to manually assign `state`.
    /// Can be used to deserialize PulseDecodeWriter.
    pub fn with_state(mut self, state: PulseDecodeState) -> Self {
        self.state = state;
        self
    }
}

impl<W: Write> PulseDecodeWriter<W> {
    /// Creates a new `PulseDecodeWriter` from a given [Writer][Write].
    pub fn new(wr: W) -> Self {
        PulseDecodeWriter { state: PulseDecodeState::Idle, wr }
    }
    /// Optionally writes a partially received data byte and ensures the state of [PulseDecodeWriter] is `Idle`.
    ///
    /// After calling this method, regardless of the return value, the state is changed to [PulseDecodeState::Idle].
    ///
    /// Returns `Ok(None)` if there was no data written in the current transfer.
    ///
    /// Returns `Ok(Some(size))` if data has been written, providing the number of written bytes.
    ///
    /// In the case of [std::io::Error] the information about the number of bytes written is lost.
    pub fn end(&mut self) -> Result<Option<NonZeroU32>> {
        let res = if let PulseDecodeState::Data { mut current, pulse, written } = self.state {
            NonZeroU32::new(if pulse <= 1 {
                written
            }
            else {
                if pulse & 1 == 1 {
                    current &= !1;
                }
                current <<= (16 - (pulse & 15)) >> 1;
                self.write_byte(current)?;
                written + 1
            })
        }
        else {
            None
        };
        self.state = PulseDecodeState::Idle;
        Ok(res)
    }
    /// Interprets pulse intervals from the provided pulse iterator as data bytes, writing them to
    /// the underlying writer.
    ///
    /// The pulse iterator is expected to provide only a fragment of pulses needed for the complete
    /// transfer such as an iterator returned from [MicOut::mic_out_pulse_iter].
    /// Providing an empty iterator is equivalent to calling [PulseDecodeWriter::end] thus ending
    /// the current transfer in progress if there is any.
    ///
    /// Returns `Ok(None)` if there was no data or more pulses are being expected.
    ///
    /// Returns `Ok(Some(size))` if data block has been written, providing the number of written bytes.
    /// In this instance, there can still be some pulses left in the iterator, e.g. for the next
    /// incoming transfer.
    ///
    /// In the case of [std::io::Error] the information about the number of bytes written is lost.
    ///
    /// [MicOut::mic_out_pulse_iter]: spectrusty_core::chip::MicOut::mic_out_pulse_iter
    pub fn write_decoded_pulses<I>(&mut self, iter: I) -> Result<Option<NonZeroU32>>
        where I: Iterator<Item=NonZeroU32>
    {
        let mut iter = iter.peekable();
        if iter.peek().is_none() {
            return self.end(); // empty frame
        }

        for delta in iter {
            match self.state {
                PulseDecodeState::Idle => {
                    if let LEAD_PULSE_MIN..=LEAD_PULSE_MAX = delta.get() {
                        self.state = PulseDecodeState::Lead { counter: 1 };
                    }
                }
                PulseDecodeState::Lead { counter } => match delta.get() {
                    SYNC_PULSE1_MIN..=SYNC_PULSE1_MAX if counter >= MIN_LEAD_COUNT => {
                        self.state = PulseDecodeState::Sync1;
                    }
                    LEAD_PULSE_MIN..=LEAD_PULSE_MAX => {
                        self.state = PulseDecodeState::Lead { counter: counter.saturating_add(1) };
                    }
                    _ => { self.state = PulseDecodeState::Idle },
                }
                PulseDecodeState::Sync1 => match delta.get() {
                    SYNC_PULSE2_MIN..=SYNC_PULSE2_MAX => {
                        self.state = PulseDecodeState::Sync2;
                    }                    
                    _ => { self.state = PulseDecodeState::Idle },
                }
                PulseDecodeState::Sync2 => match delta.get() {
                    delta @ DATA_PULSE_MIN..=DATA_PULSE_MAX => {
                        let current = (delta > DATA_PULSE_THRESHOLD) as u8;
                        self.state = PulseDecodeState::Data { current, pulse: 1, written: 0 }
                    }
                    _ => { self.state = PulseDecodeState::Idle },
                }
                PulseDecodeState::Data { current, pulse, written } => match delta.get() {
                    delta @ DATA_PULSE_MIN..=DATA_PULSE_MAX => {
                        let bit = (delta > DATA_PULSE_THRESHOLD) as u8;
                        let current = if pulse & 1 == 1 {
                            if (current ^ bit) & 1 == 1 {
                                return self.end();
                            }
                            if pulse == 15 {
                                self.state = PulseDecodeState::Data { current: 0, pulse: 0, written: written + 1 };
                                self.write_byte(current)?;
                                continue;
                            }
                            current
                        }
                        else {
                            (current << 1) | bit
                        };
                        self.state = PulseDecodeState::Data { current, pulse: pulse + 1, written }
                    }
                    _ => return self.end()
                }
            }
        }
        Ok(None)
    }

    #[inline]
    fn write_byte(&mut self, byte: u8) -> Result<()> {
        // eprint!("{:02x} ", byte);
        if let Err(e) = self.wr.write_all(slice::from_ref(&byte)) {
            self.state = PulseDecodeState::Idle;
            return Err(e);
        }
        Ok(())
    }
}

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

    #[test]
    fn write_mic_works() {
        let wr = Cursor::new(Vec::new());
        let mut mpw = PulseDecodeWriter::new(wr);
        let pulses = [];
        assert_eq!(PulseDecodeState::Idle, mpw.state());
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
        assert_eq!(PulseDecodeState::Idle, mpw.state());
        assert_eq!(None, mpw.data_size());
        let pulses = [PAUSE_PULSE_LENGTH, LEAD_PULSE_LENGTH];
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
        assert_eq!(PulseDecodeState::Lead {counter:1} , mpw.state());
        assert_eq!(None, mpw.data_size());
        let pulses = vec![LEAD_PULSE_LENGTH;LEAD_PULSES_HEAD as usize];
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.into_iter()).unwrap());
        assert_eq!(PulseDecodeState::Lead {counter:LEAD_PULSES_HEAD as u32 + 1} , mpw.state());
        assert_eq!(None, mpw.data_size());
        let pulses = [SYNC_PULSE1_LENGTH, SYNC_PULSE2_LENGTH,
                      ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
                      ONE_PULSE_LENGTH, ONE_PULSE_LENGTH];
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
        assert_eq!(PulseDecodeState::Data {current: 0b0000_0001, pulse: 4, written: 0} , mpw.state());
        assert_eq!(None, mpw.data_size());
        assert_eq!(0, mpw.get_ref().position());
        let pulses = [ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
                      ONE_PULSE_LENGTH, ONE_PULSE_LENGTH,
                      ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
                      ONE_PULSE_LENGTH, ONE_PULSE_LENGTH,
                      ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
                      ONE_PULSE_LENGTH, ONE_PULSE_LENGTH];
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
        assert_eq!(PulseDecodeState::Data {current: 0, pulse: 0, written: 1} , mpw.state());
        assert_eq!(NonZeroU32::new(1), mpw.data_size());
        assert_eq!(1, mpw.get_ref().position());
        assert_eq!(&[0b0101_0101], &mpw.get_ref().get_ref()[..]);
        assert_eq!(NonZeroU32::new(1), mpw.end().unwrap());
        assert_eq!(PulseDecodeState::Idle, mpw.state());
        assert_eq!(None, mpw.data_size());
    }

    #[test]
    fn write_mic_missing_bits_works() {
        let wr = Cursor::new(Vec::new());
        let mut mpw = PulseDecodeWriter::new(wr);
        let pulses = vec![LEAD_PULSE_LENGTH;LEAD_PULSES_DATA as usize];
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.into_iter()).unwrap());
        assert_eq!(PulseDecodeState::Lead {counter:LEAD_PULSES_DATA as u32} , mpw.state());
        assert_eq!(None, mpw.data_size());
        let pulses = [SYNC_PULSE1_LENGTH, SYNC_PULSE2_LENGTH,
                      ONE_PULSE_LENGTH, ONE_PULSE_LENGTH,
                      ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
                      ONE_PULSE_LENGTH];
        assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
        assert_eq!(PulseDecodeState::Data {current: 0b0000_0101, pulse: 5, written: 0} , mpw.state());
        assert_eq!(None, mpw.data_size());
        assert_eq!(NonZeroU32::new(1), mpw.end().unwrap());
        assert_eq!(1, mpw.get_ref().position());
        assert_eq!(&[0b1000_0000], &mpw.get_ref().get_ref()[..]);
        assert_eq!(PulseDecodeState::Idle, mpw.state());
        assert_eq!(None, mpw.data_size());
    }
}