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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * Copyright (c) 2017 Sebastian Jastrzebski <sebby2k@gmail.com>. All rights reserved.
 * Portions (c) 2004 Dag Lem <resid@nimrod.no>
 *
 * This file is part of resid-rs.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use super::ChipModel;
use super::envelope::{State as EnvState};
use super::external_filter::ExternalFilter;
use super::filter::Filter;
use super::voice::Voice;

const FIXP_SHIFT: i32 = 16;
const FIXP_MASK: i32 = 0xffff;
const OUTPUT_RANGE: u32 = 1 << 16;
const OUTPUT_HALF: i32 = (OUTPUT_RANGE >> 1) as i32;
const SAMPLES_PER_OUTPUT: u32 = (((4095 * 255) >> 7) * 3 * 15 * 2 / OUTPUT_RANGE);

#[derive(Clone, Copy)]
pub enum Reg {
    FREQLO1,
    FREQHI1,
    PWLO1,
    PWHI1,
    CR1,
    AD1,
    SR1,
    FREQLO2,
    FREQHI2,
    PWLO2,
    PWHI2,
    CR2,
    AD2,
    SR2,
    FREQLO3,
    FREQHI3,
    PWLO3,
    PWHI3,
    CR3,
    AD3,
    SR3,
    FCLO,
    FCHI,
    RESFILT,
    MODVOL,
    POTX,
    POTY,
    OSC3,
    ENV3,
}

impl Reg {
    pub fn from(reg: u8) -> Reg {
        match reg {
            0x00 => Reg::FREQLO1,
            0x01 => Reg::FREQHI1,
            0x02 => Reg::PWLO1,
            0x03 => Reg::PWHI1,
            0x04 => Reg::CR1,
            0x05 => Reg::AD1,
            0x06 => Reg::SR1,
            0x07 => Reg::FREQLO2,
            0x08 => Reg::FREQHI2,
            0x09 => Reg::PWLO2,
            0x0a => Reg::PWHI2,
            0x0b => Reg::CR2,
            0x0c => Reg::AD2,
            0x0d => Reg::SR2,
            0x0e => Reg::FREQLO3,
            0x0f => Reg::FREQHI3,
            0x10 => Reg::PWLO3,
            0x11 => Reg::PWHI3,
            0x12 => Reg::CR3,
            0x13 => Reg::AD3,
            0x14 => Reg::SR3,
            0x15 => Reg::FCLO,
            0x16 => Reg::FCHI,
            0x17 => Reg::RESFILT,
            0x18 => Reg::MODVOL,
            0x19 => Reg::POTX,
            0x1a => Reg::POTY,
            0x1b => Reg::OSC3,
            0x1c => Reg::ENV3,
            _ => panic!("invalid reg {}", reg),
        }
    }

    #[allow(dead_code)]
    pub fn addr(&self) -> u8 {
        *self as u8
    }
}

#[derive(Debug)]
pub struct State {
    // Sid
    pub sid_register: [u8; 32],
    pub bus_value: u8,
    pub bus_value_ttl: u32,
    pub ext_in: i32,
    // Wave
    pub accumulator: [u32; 3],
    pub shift_register: [u32; 3],
    // Envelope
    pub envelope_state: [u8; 3],
    pub envelope_counter: [u8; 3],
    pub exponential_counter: [u8; 3],
    pub exponential_counter_period: [u8; 3],
    pub hold_zero: [u8; 3],
    pub rate_counter: [u16; 3],
    pub rate_counter_period: [u16; 3],
}

pub struct Sid {
    // Functional Units
    ext_filter: ExternalFilter,
    filter: Filter,
    voices: [Voice; 3],
    // Runtime State
    bus_value: u8,
    bus_value_ttl: u32,
    ext_in: i32,
    // Sampling State
    cycles_per_sample: u32,
    sample_offset: i32,
    sample_prev: i16,
}

impl Sid {
    pub fn new(chip_model: ChipModel) -> Sid {
        let mut voice1 = Voice::new(chip_model);
        let mut voice2 = Voice::new(chip_model);
        let mut voice3 = Voice::new(chip_model);
        voice1.set_sync_source(&mut voice3);
        voice2.set_sync_source(&mut voice1);
        voice3.set_sync_source(&mut voice2);
        let mut sid = Sid {
            ext_filter: ExternalFilter::new(chip_model),
            filter: Filter::new(chip_model),
            voices: [voice1, voice2, voice3],
            bus_value: 0,
            bus_value_ttl: 0,
            ext_in: 0,
            cycles_per_sample: 0,
            sample_offset: 0,
            sample_prev: 0,
        };
        sid.set_sampling_parameters(985248, 44100);
        sid
    }

    pub fn clock(&mut self) {
        // Age bus value.
        if self.bus_value_ttl > 0 {
            self.bus_value_ttl -= 1;
            if self.bus_value_ttl == 0 {
                self.bus_value = 0;
            }
        }
        // Clock amplitude modulators.
        for i in 0..3 {
            self.voices[i].envelope.clock();
        }
        // Clock oscillators.
        for i in 0..3 {
            self.voices[i].wave.borrow_mut().clock();
        }
        // Synchronize oscillators.
        for i in 0..3 {
            self.voices[i].wave.borrow_mut().synchronize();
        }
        // Clock filter.
        self.filter.clock(self.voices[0].output(),
                          self.voices[1].output(),
                          self.voices[2].output(),
                          self.ext_in);
        // Clock external filter.
        self.ext_filter.clock(self.filter.output());
    }

    pub fn clock_delta(&mut self, delta: u32) {
        // Age bus value.
        if self.bus_value_ttl >= delta {
            self.bus_value_ttl -= delta;
        } else {
            self.bus_value_ttl = 0;
        }
        if self.bus_value_ttl == 0 {
            self.bus_value = 0;
        }
        // Clock amplitude modulators.
        for i in 0..3 {
            self.voices[i].envelope.clock_delta(delta);
        }
        let mut delta_osc = delta;
        while delta_osc != 0 {
            // Find minimum number of cycles to an oscillator accumulator MSB toggle.
            // We have to clock on each MSB on / MSB off for hard sync to operate
            // correctly.
            let mut delta_min = delta_osc;
            for i in 0..3 {
                let wave = &self.voices[i].wave;
                // It is only necessary to clock on the MSB of an oscillator that is
                // a sync source and has freq != 0.
                if !(wave.borrow().get_sync_dest_sync() && wave.borrow().get_frequency() != 0) {
                    continue;
                }
                let freq = wave.borrow().get_frequency() as u32;
                let acc = wave.borrow().get_acc();
                // Clock on MSB off if MSB is on, clock on MSB on if MSB is off.
                let delta_acc = if acc & 0x800000 != 0 {
                    0x1000000 - acc
                } else {
                    0x800000 - acc
                };
                let mut delta_next = delta_acc / freq;
                if delta_acc % freq != 0 {
                    delta_next += 1;
                }
                if delta_next < delta_min {
                    delta_min = delta_next;
                }
            }
            // Clock oscillators.
            for i in 0..3 {
                self.voices[i].wave.borrow_mut().clock_delta(delta_min);
            }
            // Synchronize oscillators.
            for i in 0..3 {
                self.voices[i].wave.borrow_mut().synchronize();
            }
            delta_osc -= delta_min;
        }
        // Clock filter.
        self.filter.clock_delta(delta,
                                self.voices[0].output(),
                                self.voices[1].output(),
                                self.voices[2].output(),
                                self.ext_in);
        // Clock external filter.
        self.ext_filter.clock_delta(delta, self.filter.output());
    }

    pub fn enable_external_filter(&mut self, enabled: bool) {
        self.ext_filter.set_enabled(enabled);
    }

    pub fn enable_filter(&mut self, enabled: bool) {
        self.filter.set_enabled(enabled);
    }

    pub fn input(&mut self, sample: i32) {
        // Voice outputs are 20 bits. Scale up to match three voices in order
        // to facilitate simulation of the MOS8580 "digi boost" hardware hack.
        self.ext_in = (sample << 4) * 3;
    }

    pub fn output(&self) -> i16 {
        // Read sample from audio output.
        let sample = self.ext_filter.output() / SAMPLES_PER_OUTPUT as i32;
        if sample >= OUTPUT_HALF {
            (OUTPUT_HALF - 1) as i16
        } else if sample < -OUTPUT_HALF {
            (-OUTPUT_HALF) as i16
        } else {
            sample as i16
        }
    }

    pub fn reset(&mut self) {
        self.ext_filter.reset();
        self.filter.reset();
        for i in 0..3 {
            self.voices[i].reset();
        }
        self.bus_value = 0;
        self.bus_value_ttl = 0;
        self.ext_in = 0;
        self.sample_offset = 0;
        self.sample_prev = 0;
    }

    // ----------------------------------------------------------------------------
    // SID clocking with audio sampling.
    // Fixpoint arithmetics is used.
    //
    // The example below shows how to clock the SID a specified amount of cycles
    // while producing audio output:
    //
    // while (delta_t) {
    //   bufindex += sid.clock(delta_t, buf + bufindex, buflength - bufindex);
    //   write(dsp, buf, bufindex*2);
    //   bufindex = 0;
    // }
    //
    // ----------------------------------------------------------------------------
    pub fn sample(&mut self,
                  delta: u32,
                  buffer: &mut [i16],
                  n: usize,
                  interleave: usize) -> (usize, u32) {
        self.sample_fast(delta, buffer, n, interleave)
    }

    // ----------------------------------------------------------------------------
    // SID clocking with audio sampling - delta clocking picking nearest sample.
    // ----------------------------------------------------------------------------
    fn sample_fast(&mut self,
                   mut delta: u32,
                   buffer: &mut [i16],
                   n: usize,
                   interleave: usize) -> (usize, u32) {
        let mut s = 0;
        loop {
            let next_sample_offset = self.sample_offset + self.cycles_per_sample as i32 + (1 << (FIXP_SHIFT - 1));
            let delta_sample = (next_sample_offset >> FIXP_SHIFT) as u32;
            if delta_sample > delta {
                break;
            }
            if s >= n {
                return (s, delta);
            }
            self.clock_delta(delta_sample);
            delta -= delta_sample;
            self.sample_offset = (next_sample_offset & FIXP_MASK) - (1 << (FIXP_SHIFT - 1));
            buffer[(s * interleave) as usize] = self.output();
            s += 1; // TODO check w/ ref impl
        }
        self.clock_delta(delta);
        self.sample_offset -= (delta as i32) << FIXP_SHIFT;
        delta = 0;
        (s, delta)
    }

    #[allow(dead_code)]
    fn sample_interpolate(&mut self,
                          mut delta_t: u32,
                          buffer: &mut [i16],
                          n: usize,
                          interleave: usize) -> (usize, u32) {
        let mut s = 0;
        loop {
            let next_sample_offset = self.sample_offset + self.cycles_per_sample as i32;
            let delta_t_sample = (next_sample_offset >> FIXP_SHIFT) as u32;
            if delta_t_sample > delta_t {
                break;
            }
            if s >= n {
                return (s, delta_t);
            }
            for _i in 0..(delta_t_sample - 1) {
                self.sample_prev = self.output();
                self.clock();
            }
            delta_t -= delta_t_sample;
            self.sample_offset = next_sample_offset & FIXP_MASK;
            let sample_now = self.output();
            buffer[s * interleave] = self.sample_prev + ((self.sample_offset * (sample_now - self.sample_prev) as i32) >> FIXP_SHIFT) as i16;
            s += 1; // TODO check w/ ref impl
            self.sample_prev = sample_now;
        }
        for _i in 0..(delta_t - 1) {
            self.clock();
        }
        self.sample_offset -= (delta_t as i32) << FIXP_SHIFT;
        delta_t = 0;
        (s, delta_t)
    }

    // ----------------------------------------------------------------------------
    // Setting of SID sampling parameters.
    //
    // Use a clock freqency of 985248Hz for PAL C64, 1022730Hz for NTSC C64.
    // The default end of passband frequency is pass_freq = 0.9*sample_freq/2
    // for sample frequencies up to ~ 44.1kHz, and 20kHz for higher sample
    // frequencies.
    //
    // For resampling, the ratio between the clock frequency and the sample
    // frequency is limited as follows:
    //   125*clock_freq/sample_freq < 16384
    // E.g. provided a clock frequency of ~ 1MHz, the sample frequency can not
    // be set lower than ~ 8kHz. A lower sample frequency would make the
    // resampling code overfill its 16k sample ring buffer.
    //
    // The end of passband frequency is also limited:
    //   pass_freq <= 0.9*sample_freq/2
    //
    // E.g. for a 44.1kHz sampling rate the end of passband frequency is limited
    // to slightly below 20kHz. This constraint ensures that the FIR table is
    // not overfilled.
    // ----------------------------------------------------------------------------
    pub fn set_sampling_parameters(&mut self, clock_freq: u32, sample_freq: u32) {
        self.cycles_per_sample = (clock_freq as f64 / sample_freq as f64 * (1 << FIXP_SHIFT) as f64 + 0.5) as u32;
        self.sample_offset = 0;
        self.sample_prev = 0;
    }

    // -- Device I/O

    pub fn read(&self, reg: u8) -> u8 {
        match Reg::from(reg) {
            Reg::POTX => 0,
            Reg::POTY => 0,
            Reg::OSC3 => self.voices[2].wave.borrow().read_osc(),
            Reg::ENV3 => self.voices[2].envelope.read_env(),
            _ => self.bus_value,
        }
    }

    pub fn write(&mut self, reg: u8, value: u8) {
        self.bus_value = value;
        self.bus_value_ttl = 0x2000;
        match Reg::from(reg) {
            Reg::FREQLO1 => {
                self.voices[0].wave.borrow_mut().set_frequency_lo(value);
            }
            Reg::FREQHI1 => {
                self.voices[0].wave.borrow_mut().set_frequency_hi(value);
            }
            Reg::PWLO1 => {
                self.voices[0].wave.borrow_mut().set_pulse_width_lo(value);
            }
            Reg::PWHI1 => {
                self.voices[0].wave.borrow_mut().set_pulse_width_hi(value);
            }
            Reg::CR1 => {
                self.voices[0].set_control(value);
            }
            Reg::AD1 => {
                self.voices[0].envelope.set_attack_decay(value);
            }
            Reg::SR1 => {
                self.voices[0].envelope.set_sustain_release(value);
            }
            Reg::FREQLO2 => {
                self.voices[1].wave.borrow_mut().set_frequency_lo(value);
            }
            Reg::FREQHI2 => {
                self.voices[1].wave.borrow_mut().set_frequency_hi(value);
            }
            Reg::PWLO2 => {
                self.voices[1].wave.borrow_mut().set_pulse_width_lo(value);
            }
            Reg::PWHI2 => {
                self.voices[1].wave.borrow_mut().set_pulse_width_hi(value);
            }
            Reg::CR2 => {
                self.voices[1].set_control(value);
            }
            Reg::AD2 => {
                self.voices[1].envelope.set_attack_decay(value);
            }
            Reg::SR2 => {
                self.voices[1].envelope.set_sustain_release(value);
            }
            Reg::FREQLO3 => {
                self.voices[2].wave.borrow_mut().set_frequency_lo(value);
            }
            Reg::FREQHI3 => {
                self.voices[2].wave.borrow_mut().set_frequency_hi(value);
            }
            Reg::PWLO3 => {
                self.voices[2].wave.borrow_mut().set_pulse_width_lo(value);
            }
            Reg::PWHI3 => {
                self.voices[2].wave.borrow_mut().set_pulse_width_hi(value);
            }
            Reg::CR3 => {
                self.voices[2].set_control(value);
            }
            Reg::AD3 => {
                self.voices[2].envelope.set_attack_decay(value);
            }
            Reg::SR3 => {
                self.voices[2].envelope.set_sustain_release(value);
            }
            Reg::FCLO => {
                self.filter.set_fc_lo(value);
            }
            Reg::FCHI => {
                self.filter.set_fc_hi(value);
            }
            Reg::RESFILT => {
                self.filter.set_res_filt(value);
            }
            Reg::MODVOL => {
                self.filter.set_mode_vol(value);
            }
            _ => {}
        }
    }

    // -- State

    pub fn read_state(&self) -> State {
        let mut state = State {
            sid_register: [0; 32],
            bus_value: 0,
            bus_value_ttl: 0,
            ext_in: 0,
            accumulator: [0; 3],
            shift_register: [0; 3],
            envelope_state: [0; 3],
            envelope_counter: [0; 3],
            exponential_counter: [0; 3],
            exponential_counter_period: [0; 3],
            hold_zero: [0; 3],
            rate_counter: [0; 3],
            rate_counter_period: [0; 3],
        };
        for i in 0..3 {
            let j = i * 7;
            let wave = self.voices[i].wave.borrow();
            let envelope = &self.voices[i].envelope;
            state.sid_register[j + 0] = wave.get_frequency_lo();
            state.sid_register[j + 1] = wave.get_frequency_hi();
            state.sid_register[j + 2] = wave.get_pulse_width_lo();
            state.sid_register[j + 3] = wave.get_pulse_width_hi();
            state.sid_register[j + 4] = wave.get_control() | envelope.get_control();
            state.sid_register[j + 5] = envelope.get_attack_decay();
            state.sid_register[j + 6] = envelope.get_sustain_release();
        }
        state.sid_register[0x15] = self.filter.get_fc_lo();
        state.sid_register[0x16] = self.filter.get_fc_hi();
        state.sid_register[0x17] = self.filter.get_res_filt();
        state.sid_register[0x18] = self.filter.get_mode_vol();
        for i in 0x19..0x1d {
            state.sid_register[i] = self.read(i as u8);
        }
        for i in 0x1d..0x20 {
            state.sid_register[i] = 0;
        }
        state.bus_value = self.bus_value;
        state.bus_value_ttl = self.bus_value_ttl;
        state.ext_in = self.ext_in;
        for i in 0..3 {
            let wave = self.voices[i].wave.borrow();
            let envelope = &self.voices[i].envelope;
            state.accumulator[i] = wave.get_acc();
            state.shift_register[i] = wave.get_shift();
            state.envelope_state[i] = envelope.state as u8;
            state.envelope_counter[i] = envelope.envelope_counter;
            state.exponential_counter[i] = envelope.exponential_counter;
            state.exponential_counter_period[i] = envelope.exponential_counter_period;
            state.hold_zero[i] = if envelope.hold_zero { 1 } else { 0 };
            state.rate_counter[i] = envelope.rate_counter;
            state.rate_counter_period[i] = envelope.rate_counter_period;
        }
        state
    }

    pub fn write_state(&mut self, state: State) {
        for i in 0..0x19 {
            self.write(i, state.sid_register[i as usize]);
        }
        self.bus_value = state.bus_value;
        self.bus_value_ttl = state.bus_value_ttl;
        self.ext_in = state.ext_in;
        for i in 0..3 {
            let envelope = &mut self.voices[i].envelope;
            self.voices[i].wave.borrow_mut().acc = state.accumulator[i];
            self.voices[i].wave.borrow_mut().shift = state.shift_register[i];
            envelope.state = match state.envelope_state[i] {
                0 => EnvState::Attack,
                1 => EnvState::DecaySustain,
                2 => EnvState::Release,
                _ => panic!("invalid envelope state"),
            };
            envelope.envelope_counter = state.envelope_counter[i];
            envelope.exponential_counter = state.exponential_counter[i];
            envelope.exponential_counter_period = state.exponential_counter_period[i];
            envelope.hold_zero = if state.hold_zero[i] != 0 { true } else { false };
            envelope.rate_counter = state.rate_counter[i] ;
            envelope.rate_counter_period = state.rate_counter_period[i];
        }
    }
}