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
/*
    Copyright (C) 2020  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::cmp::{Ord, PartialEq, PartialOrd};
use core::convert::TryInto;
use core::fmt::Debug;
use core::ops::{Add, Sub, AddAssign, SubAssign};

use crate::video::VideoFrame;
use super::{VFrameTsCounter, VFrameTs, VideoTs, FTs, Ts};

/// A trait providing calculation methods for timestamps.
///
/// Allows [BusDevice][crate::bus::BusDevice] implementations to depend on a generic timestamp type.
pub trait TimestampOps: Copy
                        + PartialEq
                        + Eq
                        + PartialOrd
                        + Ord
                        + Debug
                        + Add<FTs, Output=Self>
                        + Sub<FTs, Output=Self>
                        + AddAssign<FTs>
                        + SubAssign<FTs>
{
    /// Returns a normalized timestamp from the given number of T-states.
    ///
    /// # Panics
    /// Panics when the given `ts` overflows the capacity of the timestamp.
    fn from_tstates(ts: FTs) -> Self;
    /// Converts the timestamp to FTs.
    ///
    /// # Panics
    /// Panics when `self` overflows the capacity of the result type.
    fn into_tstates(self) -> FTs;
    /// Returns the largest value that can be represented by a normalized timestamp.
    fn max_value() -> Self;
    /// Returns the smallest value that can be represented by a normalized timestamp.
    fn min_value() -> Self;
    /// Returns the difference between `ts_from` and `self` in the number of T-states.
    ///
    /// # Panics
    /// May panic if the result would exceed the capacity of the result type.
    fn diff_from(self, ts_from: Self) -> FTs;
    /// Returns a normalized timestamp after adding `other` to it.
    ///
    /// Saturates at [TimestampOps::min_value] or [TimestampOps::max_value].
    fn saturating_add(self, other: Self) -> Self;
    /// Returns a normalized timestamp after subtracting `other` from it.
    ///
    /// Saturates at [TimestampOps::min_value] or [TimestampOps::max_value].
    fn saturating_sub(self, other: Self) -> Self;
}

impl TimestampOps for FTs {
    #[inline(always)]
    fn from_tstates(ts: FTs) -> Self {
        ts
    }
    #[inline(always)]
    fn into_tstates(self) -> FTs {
        self
    }
    #[inline(always)]
    fn max_value() -> Self {
        FTs::max_value()
    }
    #[inline(always)]
    fn min_value() -> Self {
        FTs::min_value()
    }
    #[inline(always)]
    fn diff_from(self, ts_from: Self) -> FTs {
        self - ts_from
    }
    #[inline(always)]
    fn saturating_add(self, other: Self) -> Self {
        self.saturating_add(other)
    }
    #[inline(always)]
    fn saturating_sub(self, other: Self) -> Self {
        self.saturating_sub(other)
    }
}

impl <V: VideoFrame> TimestampOps for VFrameTs<V> {
    #[inline]
    fn from_tstates(ts: FTs) -> Self {
        VFrameTs::from_tstates(ts)
    }
    #[inline]
    fn into_tstates(self) -> FTs {
        VFrameTs::into_tstates(self)
    }
    #[inline(always)]
    fn max_value() -> Self {
        VFrameTs::max_value()
    }
    #[inline(always)]
    fn min_value() -> Self {
        VFrameTs::min_value()
    }
    #[inline]
    fn diff_from(self, vts_from: Self) -> FTs {
        (self.vc as FTs - vts_from.vc as FTs) * V::HTS_COUNT as FTs +
        (self.hc as FTs - vts_from.hc as FTs)
    }
    /// # Panics
    /// May panic if `self` or `other` hasn't been normalized.
    fn saturating_add(self, other: Self) -> Self {
        let VideoTs { vc, hc } = self.ts;
        let vc = vc.saturating_add(other.vc);
        let hc = hc + other.hc;
        VFrameTs::new(vc, hc).saturating_normalized()
    }
    /// # Panics
    /// May panic if `self` or `other` hasn't been normalized.
    fn saturating_sub(self, other: Self) -> Self {
        let VideoTs { vc, hc } = self.ts;
        let vc = vc.saturating_sub(other.vc);
        let hc = hc - other.hc;
        VFrameTs::new(vc, hc).saturating_normalized()
    }
}

impl<V: VideoFrame> Add<FTs> for VFrameTs<V> {
    type Output = Self;
    /// Returns a normalized video timestamp after adding `delta` T-states.
    ///
    /// # Panics
    /// Panics when normalized timestamp after addition leads to an overflow of the capacity of [VideoTs].
    #[inline]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn add(self, delta: FTs) -> Self {
        let VideoTs { vc, hc } = self.ts;
        let dvc: Ts = (delta / V::HTS_COUNT as FTs).try_into().expect("absolute delta FTs is too large");
        let dhc = (delta % V::HTS_COUNT as FTs) as Ts;
        VFrameTs::new(vc + dvc, hc + dhc).normalized()
    }
}

impl<V: VideoFrame> AddAssign<FTs> for VFrameTs<V> {
    #[inline(always)]
    fn add_assign(&mut self, delta: FTs) {
        *self = *self + delta
    }
}

impl<V: VideoFrame> Sub<FTs> for VFrameTs<V> {
    type Output = Self;
    /// Returns a normalized video timestamp after subtracting `delta` T-states.
    ///
    /// # Panics
    /// Panics when normalized timestamp after addition leads to an overflow of the capacity of [VideoTs].
    #[inline]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn sub(self, delta: FTs) -> Self {
        let VideoTs { vc, hc } = self.ts;
        let dvc: Ts = (delta / V::HTS_COUNT as FTs).try_into().expect("delta too large");
        let dhc = (delta % V::HTS_COUNT as FTs) as Ts;
        VFrameTs::new(vc - dvc, hc - dhc).normalized()
    }
}

impl<V: VideoFrame> SubAssign<FTs> for VFrameTs<V> {
    #[inline(always)]
    fn sub_assign(&mut self, delta: FTs) {
        *self = *self - delta
    }
}

impl<V: VideoFrame> Add<u32> for VFrameTs<V> {
    type Output = Self;
    /// Returns a normalized video timestamp after adding a `delta` T-state count.
    ///
    /// # Panics
    /// Panics when normalized timestamp after addition leads to an overflow of the capacity of [VideoTs].
    #[inline]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn add(self, delta: u32) -> Self {
        let VideoTs { vc, hc } = self.ts;
        let dvc = (delta / V::HTS_COUNT as u32).try_into().expect("delta too large");
        let dhc = (delta % V::HTS_COUNT as u32) as Ts;
        let vc = vc.checked_add(dvc).expect("delta too large");
        VFrameTs::new(vc, hc + dhc).normalized()
    }
}

impl<V: VideoFrame> AddAssign<u32> for VFrameTs<V> {
    #[inline(always)]
    fn add_assign(&mut self, delta: u32) {
        *self = *self + delta
    }
}

impl<V: VideoFrame> Sub<u32> for VFrameTs<V> {
    type Output = Self;
    /// Returns a normalized video timestamp after adding a `delta` T-state count.
    ///
    /// # Panics
    /// Panics when normalized timestamp after addition leads to an overflow of the capacity of [VideoTs].
    #[inline]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn sub(self, delta: u32) -> Self {
        let VideoTs { vc, hc } = self.ts;
        let dvc = (delta / V::HTS_COUNT as u32).try_into().expect("delta too large");
        let dhc = (delta % V::HTS_COUNT as u32) as Ts;
        let vc = vc.checked_sub(dvc).expect("delta too large");
        VFrameTs::new(vc, hc - dhc).normalized()
    }
}

impl<V: VideoFrame> SubAssign<u32> for VFrameTs<V> {
    #[inline(always)]
    fn sub_assign(&mut self, delta: u32) {
        *self = *self - delta
    }
}

impl<V: VideoFrame, C> AddAssign<u32> for VFrameTsCounter<V, C> {
    fn add_assign(&mut self, delta: u32) {
        self.vts += delta;
    }
}

impl<V: VideoFrame, C> SubAssign<u32> for VFrameTsCounter<V, C> {
    fn sub_assign(&mut self, delta: u32) {
        self.vts -= delta;
    }
}