-
Notifications
You must be signed in to change notification settings - Fork 1
/
SID6581.cpp
372 lines (300 loc) · 9.89 KB
/
SID6581.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
/********************************************************
* ______ *
* _____|\ \ ____ _____ *
* / / | | ____\_ \__ _____\ \ *
* | |/ /| / / \ / / \ | *
* | |\____/ |/ /\ | | | /___/| *
* |\ \ | /| | | | ____\ \ | || *
* | \ \___|/ | | | | / /\ \|___|/ *
* | \ \ | | / /|| |/ \ \ *
* \ \_____\ |\ \_____/ ||\____\ /____/| *
* \ | | | \_____\ | / | | || | | *
* \|_____| \ | |___|/ \|___||____|/ *
* \|____| *
* *
********************************************************
* SIDI: THE MIDI SID INTERFACE *
* (c)2013-2015 Phar Out Studios *
*------------------------------------------------------*
* This is the implementation of the SID6581 class that *
* is responsible for the actual driving of the chip. *
* *
* It also defines and instantiates a global instance *
* named SID because my current needs don't dictate *
* multiple SID chips on one Arduino. *
*------------------------------------------------------*
* This file is part of SIDI. *
* *
* SIDI 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. *
* *
* SIDI 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 SIDI. *
* *
* If not, see <http://www.gnu.org/licenses/>. *
********************************************************/
#include <Arduino.h>
#include "SID6581.h"
SID6581::SID6581() {
int x;
// Make sure our "chip" is completely 0d
memset( &sidchip, 0, sizeof(sid6581_chip_t) );
// Set up our pins
pinMode( SID6581_PIN_D0, OUTPUT );
pinMode( SID6581_PIN_D1, OUTPUT );
pinMode( SID6581_PIN_D2, OUTPUT );
pinMode( SID6581_PIN_D3, OUTPUT );
pinMode( SID6581_PIN_D4, OUTPUT );
pinMode( SID6581_PIN_D5, OUTPUT );
pinMode( SID6581_PIN_D6, OUTPUT );
pinMode( SID6581_PIN_D7, OUTPUT );
pinMode( SID6581_PIN_A0, OUTPUT );
pinMode( SID6581_PIN_A1, OUTPUT );
pinMode( SID6581_PIN_A2, OUTPUT );
pinMode( SID6581_PIN_A3, OUTPUT );
pinMode( SID6581_PIN_A4, OUTPUT );
pinMode( SID6581_PIN_RESET, OUTPUT );
pinMode( SID6581_PIN_RW, OUTPUT );
pinMode( SID6581_PIN_SEL, OUTPUT );
pinMode( SID6581_PIN_TIMER, INPUT );
}
void SID6581::startClock(void) {
pinMode( SID6581_PIN_CLOCK, OUTPUT );
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 7;
TCCR1A |= (1 << COM1A0);
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10);
delayMicroseconds(1);
resetChip();
}
void SID6581::waitCycle(void) {
/*uint8_t foo = digitalRead( SID6581_PIN_TIMER );
while( foo != HIGH ) {
Serial.println("Delay1");
foo = digitalRead( SID6581_PIN_TIMER );
}
foo = digitalRead( SID6581_PIN_TIMER );
while( foo == HIGH ) {
Serial.println("Delay2");
foo = digitalRead( SID6581_PIN_TIMER );
}*/
delayMicroseconds(2);
}
/*********************************************************************
Writes can only happen when SEL is low and a clock strobe occurs.
We keep SEL high to avoid any unintended writes.
*********************************************************************/
void SID6581::writeData(void) {
waitCycle();
// Ensure chip is in WRITE mode
digitalWrite( SID6581_PIN_RW, LOW );
// Select chip
digitalWrite( SID6581_PIN_SEL, LOW );
// Wait for a high to come and go
waitCycle();
// Deselect chip
digitalWrite( SID6581_PIN_SEL, HIGH );
}
/*********************************************************************
Reset the SID6581 chip
*********************************************************************/
void SID6581::resetChip(void) {
int x = 10;
// Bring reset high
digitalWrite( SID6581_PIN_RESET, HIGH );
// HIGH for 2 cycles
x = 2;
while( x > 0 ) {
waitCycle();
x--;
}
digitalWrite( SID6581_PIN_RESET, LOW );
// Bring reset LOW for 10 clock cycles
x = 10;
while( x > 0 ) {
waitCycle();
x--;
}
digitalWrite( SID6581_PIN_RESET, HIGH );
}
/*********************************************************************
Set a certain address on our A0 ... A4 pins
*********************************************************************/
void SID6581::setAddress( uint16_t what ) {
digitalWrite( SID6581_PIN_A0, CHECK_BIT(what,1)?HIGH:LOW );
digitalWrite( SID6581_PIN_A1, CHECK_BIT(what,2)?HIGH:LOW );
digitalWrite( SID6581_PIN_A2, CHECK_BIT(what,4)?HIGH:LOW );
digitalWrite( SID6581_PIN_A3, CHECK_BIT(what,8)?HIGH:LOW );
digitalWrite( SID6581_PIN_A4, CHECK_BIT(what,16)?HIGH:LOW );
}
/*********************************************************************
Set a certain value on our D0 ... D7 pins
*********************************************************************/
void SID6581::setData( uint16_t what ) {
digitalWrite( SID6581_PIN_D0, CHECK_BIT(what,1)?HIGH:LOW );
digitalWrite( SID6581_PIN_D1, CHECK_BIT(what,2)?HIGH:LOW );
digitalWrite( SID6581_PIN_D2, CHECK_BIT(what,4)?HIGH:LOW );
digitalWrite( SID6581_PIN_D3, CHECK_BIT(what,8)?HIGH:LOW );
digitalWrite( SID6581_PIN_D4, CHECK_BIT(what,16)?HIGH:LOW );
digitalWrite( SID6581_PIN_D5, CHECK_BIT(what,32)?HIGH:LOW );
digitalWrite( SID6581_PIN_D6, CHECK_BIT(what,64)?HIGH:LOW );
digitalWrite( SID6581_PIN_D7, CHECK_BIT(what,128)?HIGH:LOW );
}
void SID6581::setFrequency( int voice, uint16_t frequency ) {
sidchip.voices[voice].frequency = frequency;
}
void SID6581::updateVoiceFrequency( int which ) {
uint8_t hi, lo;
uint16_t freq = sidchip.voices[which].frequency;
switch( which ) {
case 0:
hi = SID6581_REG_F1HI;
lo = SID6581_REG_F1LO;
break;
case 1:
hi = SID6581_REG_F2HI;
lo = SID6581_REG_F2LO;
break;
case 2:
hi = SID6581_REG_F3HI;
lo = SID6581_REG_F3LO;
break;
default:
return;
}
// First, we set low
setAddress( lo );
setData( freq );
writeData();
// Now, we set high
setAddress( hi );
setData( freq>>8 );
writeData();
}
void SID6581::setPulseWidth( int voice, uint16_t width ) {
uint8_t hi, lo;
sidchip.voices[voice].width = width;
switch( voice ) {
case 0:
hi = SID6581_REG_P1HI;
lo = SID6581_REG_P1LO;
break;
case 1:
hi = SID6581_REG_P2HI;
lo = SID6581_REG_P2LO;
break;
case 2:
hi = SID6581_REG_P3HI;
lo = SID6581_REG_P3LO;
break;
default:
return;
}
setAddress( lo );
setData( width );
writeData();
setAddress( hi );
setData( (width >> 8) & B00001111 );
writeData();
}
void SID6581::setEnvelope( int voice, uint8_t atk, uint8_t dec, uint8_t sus, uint8_t rel ) {
sidchip.voices[voice].ad = 0;
//release & B00001111) | (sustain << 4
sidchip.voices[voice].ad = (dec & B00001111) | (atk << 4);
sidchip.voices[voice].sr = 0;
sidchip.voices[voice].sr = (rel & B00001111) | (sus << 4);
}
void SID6581::updateEnvelope( int which ) {
uint8_t ad,sr;
switch( which ) {
case 0:
ad = SID6581_REG_AD1;
sr = SID6581_REG_SR1;
break;
case 1:
ad = SID6581_REG_AD2;
sr = SID6581_REG_SR2;
break;
case 2:
ad = SID6581_REG_AD3;
sr = SID6581_REG_SR3;
break;
default:
return;
}
// First, we write AD
setAddress( ad );
setData( sidchip.voices[which].ad );
writeData();
// Next, our SR
setAddress( sr );
setData( sidchip.voices[which].sr );
writeData();
}
void SID6581::voiceOn( int which ) {
sidchip.voices[which].control |= B00000001;
updateControl(which);
}
void SID6581::voiceOff( int which ) {
sidchip.voices[which].control &= B11111110;
updateControl(which);
}
void SID6581::setShape( int which, uint8_t shape ) {
// Clear other shapes
sidchip.voices[which].control &= B00001111;
sidchip.voices[which].control |= shape;
updateControl(which);
}
void SID6581::setSync( int which, int state ) {
sidchip.voices[which].control &= B11111101;
if( state )
sidchip.voices[which].control += 2;
updateControl(which);
}
void SID6581::setRing( int which, int state ) {
sidchip.voices[which].control &= B11111011;
if( state )
sidchip.voices[which].control += 4;
updateControl(which);
}
void SID6581::updateControl( int which ) {
uint8_t reg;
switch( which ) {
case 0:
reg = SID6581_REG_CTL1;
break;
case 1:
reg = SID6581_REG_CTL2;
break;
case 2:
reg = SID6581_REG_CTL3;
break;
default:
return;
}
// Set control register & data
setAddress( reg );
setData( sidchip.voices[which].control );
writeData();
}
void SID6581::setVolume( uint8_t vol ) {
sidchip.filter.modevol &= B11110000;
sidchip.filter.modevol |= (vol & B00001111);
// Update immediately
setAddress( SID6581_REG_MVOL );
setData( sidchip.filter.modevol );
writeData();
}
SID6581 SID = SID6581();