-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYM2149F.ino
318 lines (307 loc) · 6.51 KB
/
YM2149F.ino
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
#include <SPI.h>
#include <SD.h>
#include "Streaming.h" // for operator <<
template<int SIZE, typename TYPE = uint8_t>
class RingBuffer
{
TYPE m_buff[SIZE];
int m_ri = 0;
int m_wi = 0;
public:
bool isEmpty() const
{
return m_ri == m_wi;
}
int szData() const
{
if (m_ri == m_wi)
return 0;
if (m_wi > m_ri)
return m_wi - m_ri;
return SIZE + m_wi - m_ri;
}
bool isFull() const
{
return szData() == (SIZE - 1);
}
bool write(const TYPE data)
{
if (isFull())
return false;
m_buff[m_wi] = data;
if (++m_wi == SIZE)
m_wi = 0;
return true;
}
bool read(TYPE& data)
{
if (isEmpty())
return false;
data = m_buff[m_ri];
if (++m_ri == SIZE)
m_ri = 0;
return true;
}
};
//
// AY
//
// ay pin arduino pin
//-----------------------------
// DA0 - PC0 (pin 14 <A0>)
// DA1 - PC1 (pin 15 <A1>)
// DA2 - PC2 (pin 16 <A2>)
// DA3 - PC3 (pin 17 <A3>)
// DA4 - PD4 (pin 4)
// DA5 - PD5 (pin 5)
// DA6 - PD6 (pin 6)
// DA7 - PD7 (pin 7)
//
// BC1 - PB0 (pin 8)
// BDIR - PB1 (pin 9)
//
// RESET - PD2 (pin 2)
// CLOCK - PD3 (pin 3) OC2B Fast PWM mode 7
//
class AyPlayer
{
RingBuffer<200, uint8_t> m_buff;
int m_count_delay = 0;
void _clock()
{
// mode 7 fast pwm no prescale
TCCR2A = 0x23; // COM2B1 | WGM21 | WGM20;
TCCR2B = 0x09; // WGM22 | CS20
OCR2A = 8;
OCR2B = 3;
}
void _reset()
{
PORTD = PIND & 0b11111011; // PD2 - LOW
_delay_ms(100);
PORTD = PIND | 0b00000100; // PD2 - HIGH
_delay_ms(100);
}
void _cmd_nothing() // BC1 = 0, BDIR = 0
{
PORTB = PINB & 0b11111100;
}
void _cmd_fix_addr() // BC1 = 1, BDIR = 1
{
PORTB = PINB | 0b00000011;
}
void _cmd_fix_data() // BC1 = 1, BDIR = 0
{
PORTB = PINB | 0b00000010;
}
void _out(uint8_t port, uint8_t data)
{
PORTC = (PINC & 0xF0) | (port & 0x0F);
PORTD = (PIND & 0x0F);
_cmd_fix_addr();
_delay_us(1);
_cmd_nothing();
PORTC = (PINC & 0xF0) | (data & 0x0F);
PORTD = (PIND & 0x0F) | (data & 0xF0);
_cmd_fix_data();
_delay_us(1);
_cmd_nothing();
}
void _silent()
{
for (int i = 0; i < 16; i++)
_out(i, 0);
}
public:
AyPlayer() : m_count_delay(0)
{
}
void init()
{
DDRC = DDRC | 0b00001111; //out - PC3,PC2,PC1,PC0
DDRD = DDRD | 0b11111100; //out - PD7,PD6,PD5,PD4,PD3,PD2
DDRB = DDRB | 0b00000011; //out - PB1,PB0
_clock();
_reset();
_silent();
}
void read2buff(File& f)
{
while(!m_buff.isFull())
{
if (!f.available())
return;
m_buff.write(f.read());
}
}
void play20ms() // interrupt
{
if (m_count_delay)
{
m_count_delay--;
return;
}
uint8_t data;
while (m_buff.read(data))
{
if (data == 0xFF)
return;
if (data == 0xFE)
{
if (m_buff.read(data))
m_count_delay = 4 * data;
return;
}
if (data == 0xFD)
break;
uint8_t val;
if (m_buff.read(val))
_out(data, val);
}
_silent();
}
};
//
// SD CARD
//
// CS - pin 10
// SCK - pin 13 (SCK)
// MOSI - pin 11 (MOSI)
// MISO - pin 12 (MISO)
//
class SdReader
{
private:
struct PSG_HEADER
{
char id[3]; //PSG
char _1a; //0x1a
char ver;
char freq;
char data[10];
};
File m_root;
File m_file;
int m_countFiles = 0;
int _get_count(File& dir)
{
int res = 0;
dir.rewindDirectory();
while (true)
{
File entry = dir.openNextFile();
if (!entry) break;
if (!entry.isDirectory())
res++;
entry.close();
}
return res;
}
bool _prepare(const char* fname)
{
m_file.close();
m_file = SD.open(fname);
if (!m_file)
{
Serial << "failed open \"" << fname << "\"" << endl;
return false;
}
PSG_HEADER hdr;
if (m_file.read(&hdr, sizeof(hdr)) != sizeof(hdr) ||
hdr.id[0] != 'P' ||
hdr.id[1] != 'S' ||
hdr.id[2] != 'G')
{
Serial << "bad format \"" << fname <<"\"" << endl;
return false;
}
Serial << "file \"" << fname << "\" opened" << endl;
return true;
}
public:
bool init()
{
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(10))
{
Serial << "sd card initialization failed" << endl;
return false;
}
m_root = SD.open("/");
if (!m_root)
{
Serial << "sd root read failed" << endl;
return false;
}
m_countFiles = _get_count(m_root);
Serial << m_countFiles << " files found" << endl;
return true;
}
bool openRandomFile()
{
int index = random(m_countFiles);
m_root.rewindDirectory();
int i = 0;
while (true)
{
File entry = m_root.openNextFile();
if (!entry) break;
if (!entry.isDirectory())
{
if (i == index)
{
bool ok = _prepare(entry.name());
entry.close();
return ok;
}
i++;
}
entry.close();
}
return false;
}
File& file()
{
return m_file;
}
};
void setupTimer()
{
cli();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1250;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
sei();
}
AyPlayer ay;
SdReader sd;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(4) + analogRead(5));
ay.init();
if (sd.init())
{
if (sd.openRandomFile())
{
ay.read2buff(sd.file());
setupTimer();
}
}
}
void loop()
{
if (!sd.file().available())
if (!sd.openRandomFile())
return;
ay.read2buff(sd.file());
}
ISR(TIMER1_COMPA_vect)
{
ay.play20ms();
}