forked from cybernesto/sid-arduino-lib
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SID.cpp
executable file
·569 lines (460 loc) · 15.3 KB
/
SID.cpp
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
/*
SID.cpp - Atmega8 MOS6581 SID Emulator
Copyright (c) 2007 Christoph Haberer, christoph(at)roboterclub-freiburg.de
Arduino Library Conversion by Mario Patino, cybernesto(at)gmail.com
2015-2023 Stereo capability added by Giovanni Giorgi, jj(at)gioorgi.com
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/************************************************************************
Atmega8 MOS6581 SID Emulator
SID = Sound Interface Device
This program tries to emulate the sound chip SID of the famous
historical C64 Commodore computer.
The SID emulator includes all registers of the original SID, but
some functions may not be implemented yet.
If you want to program the SID registers to generate your own sound,
please refer to the MOS6581 datasheet. The emulator tries to be as
compatible as possible.
In the main program there is an interrupt routine which sets the
ouptut values for the PWM-Output at 62.5kHz. Therefore the high
frequency noise of the PWM should not be audible to normal people.
The output is calculated with a 16kHz sample frequency to save
processing cycles.
The envelope generators are updated every 1ms.
The amplitude value is output as an 8Bit PWM value.
The PWM-Output may be directly connected to an audio amplifier.
Giovanni Giorgi Stereo Add on
Simply put, the SID Voice2 is redirected to the PWM pin 10 (on ArduinoUno).
No additional load is expected/required
Is up to the client to know how to use this stereo capability.
************************************************************************
Hardware
processor: ATMEGA8, ATMEGA168
clock: 16MHz Crystal
PIN15 PB1/OC1A 8Bit PWM sound output
PIN19 PB0 test LED
***************************************************************************/
#include <avr/interrupt.h>
#include <Arduino.h>
#include "SID.h"
// attack, decay, release envelope timings
const static uint16_t AttackRate[16]={2,4,16,24,38,58,68,80,100,250,500,800,1000,3000,5000,8000};
const static uint16_t DecayReleaseRate[16]={6,24,48,72,114,168,204,240,300,750,1500,2400,3000,9000,15000,24000};
static uint8_t rightOutput; // pin9
static uint8_t leftOutput; // pin 10 GG Addon for StereoSID
static Sid_t Sid;
static Oscillator_t osc[OSCILLATORS];
/**
* Initialize Arduino Register. Take control of Timer 1 and Timer 2.
* Timer 1 will be used for PWM output (aka Analog-approximation)
* Timer 2 will be used to compute output based on status of SID registers
*
* OCR1A is an output compare register. It is constantly compared with a timer, which generates a PWM pulse.
* The duty cycle of the PWM pulse is determined by the value of OCR1A. The PWM waveform is outputted to the OC1A pin.
*
* timer1A is pin 9 (11 on Mega)
* timer1B is pin 10 (12 on Mega)
* Reference http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM
* for explanation of usage of _BV(COM1B1)
* Additional information on
+ http://www.avrfreaks.net/forum/tut-c-pwm-complete-idiots
+ http://sphinx.mythic-beasts.com/~markt/ATmega-timers.html
+ http://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-pwm-incomplete?name=PNphpBB2&file=viewtopic&t=68302
*/
void initialize()
{
// TIMER1 used to generate sound output
// TIMER1: Fast PWM 8-bit
TCCR1A = (1 << WGM10) | (1 << COM1A1) /** GG ADDED the following to enable pin 10 --> */ | _BV(COM1B1);
// TIMER1: no prescaling
TCCR1B = (1 << WGM12) | (1 << CS10);
// ? TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
// TIMER2 used to generate sample and ms interrupts
// TIMER2: Normal Mode
TCCR2 = 0;
// TIMER2: clock/8 prescaling
TCCR2 |= (1 << CS21);
// TIMER2: set compare value to generate a 16kHz sample rate
OCR2 = SAMPLERATECOUNT;
// interrupt mask register: enable timer2 OCR2A interrupt
TIMSK = (1 << OCIE2);
// interrupt mask register: enable timer1 overflow
TIMSK |= (1 << TOIE1);
#else
// TIMER2 used to generate sample and ms interrupts
// TIMER2: Normal Mode
TCCR2A = 0 ;
// TIMER2: clock/8 prescaling
TCCR2B = (1 << CS21);
// TIMER2: set compare value to generate a 16kHz sample rate
OCR2A = SAMPLERATECOUNT;
// interrupt mask register: enable timer2 OCR2A interrupt
TIMSK2 = (1 << OCIE2A);
// interrupt mask register: enable timer1 overflow
TIMSK1 = (1 << TOIE1);
#endif
}
static int8_t wave(Voice_t *voice, uint16_t phase)
{
int8_t out;
uint8_t n = phase >> 8;
uint8_t wavetype = voice->ControlReg;
if(wavetype & SAWTOOTH)
{
out = n - 128;
}
if(wavetype & TRIANGLE)
{
if(n&0x80)
out = ((n^0xFF)<<1)-128;
else
out = (n<<1)-128;
}
if(wavetype & RECTANGLE)
{
if(n > (voice->PW >> 4)) // SID has 12Bit pwm, here we use only 8Bit
out = 127;
else
out = -127;
}
return out;
}
static uint8_t remapDirectFilter(uint16_t temp, uint16_t temp1);
static void waveforms()
{
static uint16_t phase[OSCILLATORS], sig[OSCILLATORS];
static int16_t temp,temp1;
static uint8_t i,j,k;
static uint16_t noise = 0xACE1;
static uint8_t noise8;
static uint16_t tempphase;
// noise generator based on Galois LFSR
noise = (noise >> 1) ^ (-(noise & 1) & 0xB400u);
noise8 = noise>>8;
for(i = 0; i< OSCILLATORS; i++)
{
j = (i == 0 ? 2 : i - 1);
tempphase=phase[i]+osc[i].freq_coefficient; //0.88us
if(Sid.block.voice[i].ControlReg&NOISE)
{
if((tempphase^phase[i])&0x4000) sig[i]=noise8*osc[i].envelope;
}
else
{
if(Sid.block.voice[i].ControlReg&RINGMOD)
{
if(phase[j]&0x8000)
sig[i]=osc[i].envelope*-wave(&Sid.block.voice[i],phase[i]);
else
sig[i]=osc[i].envelope*wave(&Sid.block.voice[i],phase[i]);
}
else
{
if(Sid.block.voice[i].ControlReg&SYNC)
{
if(tempphase < phase[j])
phase[i] = 0;
}
else
sig[i]=osc[i].envelope*wave(&Sid.block.voice[i],phase[i]); //2.07us
}
}
phase[i]=tempphase;
}
// voice filter selection
temp=0; // direct output variable
temp1=0; // filter output variable
if(Sid.block.RES_Filt&FILT1) temp1+=sig[0];
else temp+=sig[0];
{
uint16_t directVoice2=0, filterVoice2=0;
if(Sid.block.RES_Filt&FILT2) filterVoice2+=sig[1];
else directVoice2+=sig[1];
// VOICE2 Only
leftOutput= remapDirectFilter( directVoice2, filterVoice2);
}
if(Sid.block.RES_Filt&FILT3) temp1+=sig[2];
else if(!(Sid.block.Mode_Vol&VOICE3OFF))temp+=sig[2]; // voice 3 with special turn off bit
//filterOutput = IIR2((struct IIR_filter*)&filter04_06, filterInput);
//IIR2(filter04_06, temp1);
k=remapDirectFilter(temp,temp1);
// Output to PWM:
rightOutput = k;
}
/** GG: This class merges direct and filter-ed output, anyway
* no real filter is implemented.
*/
static uint8_t remapDirectFilter(uint16_t temp, uint16_t temp1){
uint8_t k=(temp>>8)+128;
k+=temp1>>10; // no real filter implemeted yet
return k;
}
static void envelopes()
{
uint8_t n;
uint8_t controll_regadr[OSCILLATORS]={4,11,18};
// if gate is ONE then the attack,decay,sustain cycle begins
// if gate switches to zero the sound decays
for(n=0;n<OSCILLATORS;n++)
{
if(Sid.sidregister[controll_regadr[n]]&GATE) // if gate set then attack,decay,sustain
{
if(osc[n].attackdecay_flag)
{ // if attack cycle
osc[n].amp+=osc[n].m_attack;
if(osc[n].amp>MAXLEVEL)
{
osc[n].amp=MAXLEVEL;
osc[n].attackdecay_flag=false; // if level reached, then switch to decay
}
}
else // decay cycle
{
if(osc[n].amp>osc[n].level_sustain)
{
osc[n].amp-=osc[n].m_decay;
if(osc[n].amp<osc[n].level_sustain) osc[n].amp=osc[n].level_sustain;
}
}
}
else // if gate flag is not set then release
{
osc[n].attackdecay_flag=true; // at next attack/decay cycle start wiht attack
if(osc[n].amp>0)
{
osc[n].amp-=osc[n].m_release;
if(osc[n].amp<0) osc[n].amp=0;
}
}
osc[n].envelope=osc[n].amp>>8;
}
}
/************************************************************************
interrupt routine timer 1 overflow
- set PWM output
************************************************************************/
ISR(TIMER1_OVF_vect)
{
OCR1A = rightOutput; // Output to PWM
OCR1B = leftOutput;
}
/************************************************************************
interrupt routine timer 2 16kHz
- calculate waverform phases
- calculate waveforms
- calculate attack decay release (1kHz)
************************************************************************/
#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
ISR(TIMER2_COMP_vect)
{
static uint8_t mscounter = 0;
OCR2 += SAMPLERATECOUNT; // Output to PWM
waveforms(); //~22us
if(mscounter++ >= MSCOUNT)
{
envelopes(); //~16us
mscounter = 0;
}
}
#else
ISR(TIMER2_COMPA_vect)
{
static uint8_t mscounter = 0;
OCR2A += SAMPLERATECOUNT; // Output to PWM
waveforms(); //~36us
if(mscounter++ >= MSCOUNT)
{
envelopes(); //~16us
mscounter = 0;
}
}
#endif
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
void SID::begin()
{
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
initialize();
//initialize SID-registers
Sid.sidregister[6]=0xF0;
Sid.sidregister[13]=0xF0;
Sid.sidregister[20]=0xF0;
// set all amplitudes to zero
for(int n=0;n<OSCILLATORS;n++) {
osc[n].attackdecay_flag=true;
setenvelope(&Sid.block.voice[n]);
osc[n].amp=0;
}
}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
/************************************************************************
uint8_t set_sidregister(uint8_t regnum, uint8_t value)
The registers of the virtual SID are set by this routine.
For some registers it is necessary to transform the SID-register
values to some internal settings of the emulator.
To select this registers and to start the calculation, the switch/
case statement is used.
For instance: If setting the SID envelope register, new attach, decay
sustain times are calculated.
If an invalid register is requested the returned value will be 0.
4.2007 ch
************************************************************************/
uint8_t SID::set_register(uint8_t regnum, uint8_t value)
{
if(regnum>NUMREGISTERS-1)
return 0;
Sid.sidregister[regnum]=value;
switch(regnum)
{
//voice1
case 1:
osc[0].freq_coefficient=((uint16_t)Sid.sidregister[0]+((uint16_t)Sid.sidregister[1]<<8))>>2;
break;
case 5: setenvelope(&Sid.block.voice[0]);break;
case 6: setenvelope(&Sid.block.voice[0]);break;
//voice2
case 8:
osc[1].freq_coefficient=((uint16_t)Sid.sidregister[7]+((uint16_t)Sid.sidregister[8]<<8))>>2;
break;
case 12: setenvelope(&Sid.block.voice[1]);break;
case 13: setenvelope(&Sid.block.voice[1]);break;
//voice3
case 15:
osc[2].freq_coefficient=((uint16_t)Sid.sidregister[14]+((uint16_t)Sid.sidregister[15]<<8))>>2;
break;
case 19: setenvelope(&Sid.block.voice[2]);break;
case 20: setenvelope(&Sid.block.voice[2]);break;
}
return 1;
}
/************************************************************************
uint8_t get_sidregister(uint8_t regnum)
The registers of the virtual SID are read by this routine.
If an invalid register is requested it returns zero.
************************************************************************/
uint8_t SID::get_register(uint8_t regnum)
{
if(regnum>NUMREGISTERS-1)
return 0;
return Sid.sidregister[regnum];
}
// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library
uint8_t SID::get_wavenum(Voice_t *voice)
{
uint8_t n;
if(voice==&Sid.block.voice[0]) n=0;
if(voice==&Sid.block.voice[1]) n=1;
if(voice==&Sid.block.voice[2]) n=2;
return n;
}
void SID::setfreq(Voice_t *voice,uint16_t freq)
{
uint32_t templong;
uint8_t n;
n=get_wavenum(voice);
templong=freq;
osc[n].freq_coefficient=templong*4000/SAMPLEFREQ;
}
void SID::setenvelope(Voice_t *voice)
{
uint8_t n;
n=get_wavenum(voice);
osc[n].attackdecay_flag=true;
osc[n].level_sustain=(voice->SustainRelease>>4)*SUSTAINFACTOR;
osc[n].m_attack=MAXLEVEL/AttackRate[voice->AttackDecay>>4];
osc[n].m_decay=(MAXLEVEL-osc[n].level_sustain*SUSTAINFACTOR)/DecayReleaseRate[voice->AttackDecay&0x0F];
osc[n].m_release=(osc[n].level_sustain)/DecayReleaseRate[voice->SustainRelease&0x0F];
}
// Include a set of midi constant like
// const uint8_t A4=81; 440Hz
#include "midi_support.h"
uint16_t SID::midi2Sid(uint8_t midin){
return midin <=127 ? MIDI::midi2sidMapper[ midin ]:0;
}
void SID::play(uint8_t voice, uint16_t freq){
byte low = freq & 0x00FF;
byte high= freq >>8;
set_register(voice+0,low);
set_register(voice+1,high);
}
/**
----------------
A Midnight Piano
----------------
A nice piano sound. Works well if a second voice plays the same notes when detuned 10 to 20.
Waveform: Pulse
Pulse Width: 2700
Attack: 2
Decay: 11
Sustain: 5
Release: 0
Release Point: 3
*/
void SID::loadPiano(uint8_t voice){
configVoice(voice,RECTANGLE /*also pulse */,3700,0x2B,0x50,3);
//configVoice(voice,RECTANGLE /*also pulse */,2700,0x2B,0x50,3);
}
/** Fast way of configuring a voice, ADSR included.
ReleasePoint is unimplemented.
It seems how much 1/60 of seconds the note will last.
*/
void SID::configVoice(uint8_t voice, uint8_t waveform, uint16_t pulsewidth /*12bit*/, uint8_t attackDecay, uint8_t sustainRelease, uint8_t releasePoint)
{
uint8_t pwlow = pulsewidth & 0x00FF;
uint8_t pwhigh= pulsewidth >>8;
set_register(voice+2 /*PW LOW*/,pwlow); set_register(voice+3,pwhigh);
set_register(voice+5,attackDecay);
set_register(voice+6,sustainRelease);
// 33=%100001
// ^ Gate on (ADSR on)
// ^ Sawtooth Wave (Bit 5)
set_register(voice+4, waveform | GATE);
}
/* GG Follow complex functions */
void SID::playTestIntro() {
loadPiano(VOICE1_Right);
loadPiano(VOICE2_Left);
loadPiano(VOICE3);
set_register(24,15); // MAX VOLUME
uint8_t i;
for(i=MIDI::nC3; i<=MIDI::nC6; i+=2){
play(VOICE1,midi2Sid(i));
play(VOICE2,midi2Sid(MIDI::nA4));
delay(90);
play(VOICE2,midi2Sid(MIDI::nG4));
play(VOICE1,midi2Sid(i+1));
delay(90);
//play(VOICE3,midi2Sid(MIDI::nD5));
//delay(50);
}
// // play a tadalike
// play(VOICE1,midi2Sid(60 /*DO*/));
// delay(200);
// play(VOICE2,midi2Sid(62 /*RE*/));
// delay(200);
// play(VOICE1,0);
// play(VOICE2,0);
// // Hmmm Too voice seems distort on piezo...time to aplify better?
// play(VOICE1, midi2Sid(64 /*MI*/));
// delay(250);
// play(VOICE1,0);
// play(VOICE2, midi2Sid(60 /*DO*/));
// delay(250);
play(VOICE3,0);
play(VOICE2,0);
play(VOICE1,0);
}