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
use core::fmt::{self, Debug};
use core::num::NonZeroU16;
use core::marker::PhantomData;
pub mod serial128;
#[cfg(feature = "snapshot")] mod serde;
#[cfg(feature = "snapshot")] use ::serde::Serialize;
use spectrusty_core::{
audio::{Blep, AmpLevels},
bus::{
BusDevice, NullDevice,
OptionalBusDevice, DynamicBus, DynamicSerdeBus, NamedBusDevice
},
clock::FTs
};
pub use crate::ay::{
audio::Ay3_891xAudio,
Ay3_8910Io, Ay3_8912Io, Ay3_8913Io, AyIoPort, AyIoNullPort, AyRegister,
AyPortDecode, Ay128kPortDecode, AyFullerBoxPortDecode
};
pub trait PassByAyAudioBusDevice {}
pub type Ay3_891xMelodik<D> = Ay3_891xBusDevice<Ay128kPortDecode,
AyIoNullPort<<D as BusDevice>::Timestamp>,
AyIoNullPort<<D as BusDevice>::Timestamp>,
D>;
pub type Ay3_891xFullerBox<D> = Ay3_891xBusDevice<AyFullerBoxPortDecode,
AyIoNullPort<<D as BusDevice>::Timestamp>,
AyIoNullPort<<D as BusDevice>::Timestamp>,
D>;
impl<D: BusDevice> fmt::Display for Ay3_891xMelodik<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("AY-3-8913 (Melodik)")
}
}
impl<D: BusDevice> fmt::Display for Ay3_891xFullerBox<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("AY-3-8913 (Fuller Box)")
}
}
pub trait AyAudioBusDevice: BusDevice {
fn render_ay_audio<L: AmpLevels<B::SampleDelta>, B: Blep>(
&mut self,
blep: &mut B,
end_ts: <Self as BusDevice>::Timestamp,
frame_tstates: FTs,
chans: [usize; 3]
);
}
#[derive(Clone, Default, Debug)]
#[cfg_attr(feature = "snapshot", derive(Serialize))]
#[cfg_attr(feature = "snapshot", serde(rename_all = "camelCase"))]
pub struct Ay3_891xBusDevice<P, A, B, D: BusDevice> {
pub ay_sound: Ay3_891xAudio,
pub ay_io: Ay3_8910Io<D::Timestamp, A, B>,
bus: D,
#[cfg_attr(feature = "snapshot", serde(skip))]
_port_decode: PhantomData<P>
}
impl<D> PassByAyAudioBusDevice for Box<D> where D: PassByAyAudioBusDevice {}
impl<D, N> AyAudioBusDevice for D
where D::Timestamp: Into<FTs>,
D: BusDevice<NextDevice=N> + PassByAyAudioBusDevice,
N: BusDevice<Timestamp=D::Timestamp> + AyAudioBusDevice
{
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: D::Timestamp, frame_tstates: FTs, chans: [usize; 3])
where B: Blep,
L: AmpLevels<B::SampleDelta>
{
self.next_device_mut().render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
}
macro_rules! impl_ay_audio_boxed_bus_device {
($device:ident<$($ty:ident),*>) => {
impl_ay_audio_boxed_bus_device!($device<$($ty),*> where );
};
($device:ident<$($ty:ident),*> where $($cond:tt)*) => {
impl<$($ty),*> AyAudioBusDevice for Box<$device<$($ty),*>>
where $device<$($ty),*>: AyAudioBusDevice + BusDevice,
$($cond)*
{
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: <Self as BusDevice>::Timestamp, frame_tstates: FTs, chans: [usize; 3])
where B: Blep,
L: AmpLevels<B::SampleDelta>
{
(**self).render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
}
};
}
impl_ay_audio_boxed_bus_device!(OptionalBusDevice<D, N>);
impl<D, N> AyAudioBusDevice for OptionalBusDevice<D, N>
where <D as BusDevice>::Timestamp: Into<FTs> + Copy,
D: AyAudioBusDevice + BusDevice,
N: AyAudioBusDevice + BusDevice<Timestamp=D::Timestamp>
{
#[inline(always)]
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: D::Timestamp, frame_tstates: FTs, chans: [usize; 3])
where B: Blep,
L: AmpLevels<B::SampleDelta>
{
if let Some(ref mut device) = self.device {
device.render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
self.next_device.render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
}
impl<T> AyAudioBusDevice for dyn NamedBusDevice<T>
where T: Into<FTs> + Copy + fmt::Debug + 'static
{
#[inline]
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: T, frame_tstates: FTs, chans: [usize; 3])
where L: AmpLevels<B::SampleDelta>,
B: Blep
{
if let Some(ay_dev) = self.downcast_mut::<Ay3_891xMelodik<NullDevice<T>>>() {
ay_dev.render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
else if let Some(ay_dev) = self.downcast_mut::<Ay3_891xFullerBox<NullDevice<T>>>() {
ay_dev.render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
}
}
impl_ay_audio_boxed_bus_device!(DynamicBus<D> where D: BusDevice);
impl<D> AyAudioBusDevice for DynamicBus<D>
where <D as BusDevice>::Timestamp: Into<FTs> + Copy + fmt::Debug + 'static,
D: AyAudioBusDevice + BusDevice
{
#[inline]
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: D::Timestamp, frame_tstates: FTs, chans: [usize; 3])
where B: Blep,
L: AmpLevels<B::SampleDelta>
{
for dev in self.into_iter() {
dev.render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
self.next_device_mut().render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
}
impl_ay_audio_boxed_bus_device!(DynamicSerdeBus<SD, D> where D: BusDevice);
impl<SD, D> AyAudioBusDevice for DynamicSerdeBus<SD, D>
where <D as BusDevice>::Timestamp: Into<FTs> + Copy + fmt::Debug + 'static,
D: AyAudioBusDevice + BusDevice
{
#[inline]
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: D::Timestamp, frame_tstates: FTs, chans: [usize; 3])
where B: Blep,
L: AmpLevels<B::SampleDelta>
{
(**self).render_ay_audio::<L, B>(blep, end_ts, frame_tstates, chans)
}
}
impl_ay_audio_boxed_bus_device!(Ay3_891xBusDevice<P, PA, PB, D> where D: BusDevice);
impl<P, PA, PB, D> AyAudioBusDevice for Ay3_891xBusDevice<P, PA, PB, D>
where Self: BusDevice<Timestamp=D::Timestamp>,
D: BusDevice,
D::Timestamp: Into<FTs>
{
#[inline(always)]
fn render_ay_audio<L, B>(&mut self, blep: &mut B, end_ts: D::Timestamp, frame_tstates: FTs, chans: [usize; 3])
where B: Blep,
L: AmpLevels<B::SampleDelta>
{
let end_ts = end_ts.into();
let changes = self.ay_io.recorder.drain_ay_reg_changes();
self.ay_sound.render_audio::<L,_,_>(changes, blep, end_ts, frame_tstates, chans)
}
}
impl_ay_audio_boxed_bus_device!(NullDevice<T>);
impl<T: Into<FTs> + fmt::Debug> AyAudioBusDevice for NullDevice<T> {
#[inline(always)]
fn render_ay_audio<L, B>(&mut self, _blep: &mut B, _end_ts: T, _frame_tstates: FTs, _chans: [usize; 3])
where L: AmpLevels<B::SampleDelta>,
B: Blep
{}
}
impl<P, A, B, D> BusDevice for Ay3_891xBusDevice<P, A, B, D>
where P: AyPortDecode,
A: AyIoPort<Timestamp=D::Timestamp> + Debug,
B: AyIoPort<Timestamp=D::Timestamp> + Debug,
D: BusDevice,
D::Timestamp: Debug + Copy
{
type Timestamp = D::Timestamp;
type NextDevice = D;
#[inline]
fn next_device_mut(&mut self) -> &mut Self::NextDevice {
&mut self.bus
}
#[inline]
fn next_device_ref(&self) -> &Self::NextDevice {
&self.bus
}
#[inline]
fn into_next_device(self) -> Self::NextDevice {
self.bus
}
#[inline]
fn reset(&mut self, timestamp: Self::Timestamp) {
self.ay_sound.reset();
self.ay_io.reset(timestamp);
self.bus.reset(timestamp);
}
#[inline]
fn read_io(&mut self, port: u16, timestamp: Self::Timestamp) -> Option<(u8, Option<NonZeroU16>)> {
if P::is_data_read(port) {
return Some((self.ay_io.data_port_read(port, timestamp), None))
}
self.bus.read_io(port, timestamp)
}
#[inline]
fn write_io(&mut self, port: u16, data: u8, timestamp: Self::Timestamp) -> Option<u16> {
if P::write_ay_io(&mut self.ay_io, port, data, timestamp) {
return Some(0)
}
self.bus.write_io(port, data, timestamp)
}
#[inline]
fn next_frame(&mut self, timestamp: Self::Timestamp) {
if !self.ay_io.recorder.is_empty() {
for (reg, val) in self.ay_io.iter_sound_gen_regs() {
self.ay_sound.update_register(reg, val);
}
}
self.ay_io.next_frame(timestamp);
self.bus.next_frame(timestamp)
}
}