-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpsg.c
480 lines (426 loc) · 10.7 KB
/
psg.c
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
/*
* YM2149 Connected to
*
* DA0-DA7 BUS:D8-D15
* CLOCK 2MHz
* RESET RESET
* A8 +5V
* A9 GND
* BDIR SNDCS nor RW
* BC1 SNDCS nor A1
* BC2 +5V
* IOA0 FDC:SIDE SELECT
* IOA1 FDC:DRIVE 0 SELECT
* IOA2 FDC:DRIVE 1 SELECT
* IOA3 RS232:RS
* IOA4 RS232:ER
* IOA5 PARALLEL:STROBE
* IOA6 MONITOR:GPO
* IOA7 -
* IOB0-7 PARALLEL:D0-D7
* ANALOG A AUDIO:OUT
* ANALOG B AUDIO:OUT
* ANALOG C AUDIO:OUT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <SDL.h>
#include "common.h"
#include "floppy.h"
#include "mmu.h"
#include "state.h"
#include "diag.h"
#define PSGSIZE 256
#define PSGBASE 0xff8800
#define PSG_APERL 0
#define PSG_APERH 1
#define PSG_BPERL 2
#define PSG_BPERH 3
#define PSG_CPERL 4
#define PSG_CPERH 5
#define PSG_NOISE 6
#define PSG_MIXER 7
#define PSG_AVOL 8
#define PSG_BVOL 9
#define PSG_CVOL 10
#define PSG_EPERL 11
#define PSG_EPERH 12
#define PSG_ENV 13
#define PSG_IOA 14
#define PSG_IOB 15
#define PSG_CHA 0
#define PSG_CHB 1
#define PSG_CHC 2
#define PSG_ENV_CONTINUE (psgreg[PSG_ENV]&0x8)
#define PSG_ENV_ATTACK (psgreg[PSG_ENV]&0x4)
#define PSG_ENV_ALTERNATE (psgreg[PSG_ENV]&0x2)
#define PSG_ENV_HOLD (psgreg[PSG_ENV]&0x1)
/* Floppy uses IO port A */
#define PSG_IOA_SIDE (psgreg[PSG_IOA]&0x1)
#define PSG_IOA_DEV0 (psgreg[PSG_IOA]&0x2)
#define PSG_IOA_DEV1 (psgreg[PSG_IOA]&0x4)
#define PSG_BASEFREQ 2000000
#define PSG_PERIODDIV 16.0
/* Presample is a 2MS/s data stream for use to build the sample in
output sample rate */
#define PSG_PRESAMPLESIZE 4096
#define PSG_OUTPUT_FREQ 44100
#define PSG_PRESAMPLES_PER_SAMPLE (PSG_BASEFREQ/PSG_OUTPUT_FREQ)
static BYTE psgreg[16];
static int psgactive = 0;
static int psg_periodcnt[3] = { -1, -1, -1 };
static int psg_volume[3];
static int psg_noisecnt = -1;
static int psg_noise = 1;
static int env_attack = 0;
static int env_held = 0;
static int env_first = 1;
static int env_volume;
static int env_cnt = -1;
static SDL_AudioDeviceID psg_audio_device;
static SDL_AudioSpec want,have;
static Uint16 psg_audio_buffer[PSG_OUTPUT_FREQ*2*2];
static int psg_audio_buffer_cnt = 0;
#define VOLUME_OFFSET(x) ((x==0)?0:(1+x*2))
/* Fixed point volume levels, voltage*65535 */
/* 32 values because 2149 has 32 levels in envelope, and 16 in non-env */
static signed long psg_volume_voltage[32] = {
0, 366, 435, 517, 615, 732, 870, 1035,
1231, 1464, 1740, 2069, 2460, 2924, 3476, 4132,
4912, 5838, 6939, 8248, 9803, 11651, 13848, 16459,
19562, 23250, 27633, 32843, 39035, 46394, 55140, 65535
};
static int psg_register_mask[16] = {
0xff, 0xf, 0xff, 0xf, 0xff, 0xf, 0x1f, 0xff,
0x1f, 0x1f, 0x1f, 0xff, 0xff, 0xf, 0xff, 0xff
};
static signed long psg_presample[PSG_PRESAMPLESIZE][3];
static int psg_presamplepos = 0; /* circular buffer */
static int psg_presamplestart = 0;
static long lastcpucnt = 0;
static int snd_fd;
static int psg_running = 1;
static void psg_audio_callback(void *, Uint8 *, int);
static void psg_do_interrupts(struct cpu *cpu);
HANDLE_DIAGNOSTICS(psg)
static int psg_set_period(int channel)
{
return PSG_PERIODDIV*
(psgreg[PSG_APERL+channel*2]|(psgreg[PSG_APERH+channel*2]<<8));
}
static int env_set_period()
{
return (PSG_PERIODDIV*16)*(psgreg[PSG_EPERL]|(psgreg[PSG_EPERH]<<8));
}
static void psg_set_register(BYTE data)
{
psgreg[psgactive] = data & psg_register_mask[psgactive];
switch(psgactive) {
case PSG_APERL:
case PSG_APERH:
break;
case PSG_BPERL:
case PSG_BPERH:
break;
case PSG_CPERL:
case PSG_CPERH:
break;
case PSG_NOISE:
case PSG_MIXER:
break;
case PSG_AVOL:
if(!(psgreg[psgactive]&0x10))
psg_volume[PSG_CHA] =
psg_volume_voltage[VOLUME_OFFSET(psgreg[psgactive])];
break;
case PSG_BVOL:
if(!(psgreg[psgactive]&0x10))
psg_volume[PSG_CHB] =
psg_volume_voltage[VOLUME_OFFSET(psgreg[psgactive])];
break;
case PSG_CVOL:
if(!(psgreg[psgactive]&0x10))
psg_volume[PSG_CHC] =
psg_volume_voltage[VOLUME_OFFSET(psgreg[psgactive])];
break;
case PSG_EPERL:
case PSG_EPERH:
env_first = 0;
env_cnt = env_set_period();
break;
case PSG_ENV:
env_first = 0;
env_held = 0;
env_attack = PSG_ENV_ATTACK;
env_cnt = env_set_period();
break;
case PSG_IOA:
floppy_side(PSG_IOA_SIDE);
floppy_active((PSG_IOA_DEV0|PSG_IOA_DEV1)>>1);
break;
case PSG_IOB:
default:
break;
}
}
static BYTE psg_read_byte(LONG addr)
{
addr &= 2;
if(addr) return 0;
return psgreg[psgactive];
}
static WORD psg_read_word(LONG addr)
{
return (psg_read_byte(addr)<<8)|psg_read_byte(addr+1);
}
static void psg_write_byte(LONG addr, BYTE data)
{
if(addr&1) return;
addr &= 2;
CLOCK("Wait states: 2");
cpu_add_extra_cycles(2);
switch(addr) {
case 0:
psgactive = data&0xf;
break;
case 2:
psg_set_register(data);
break;
}
}
static void psg_write_word(LONG addr, WORD data)
{
psg_write_byte(addr, (data&0xff00)>>8);
psg_write_byte(addr+1, (data&0xff));
}
static int psg_state_collect(struct mmu_state *state)
{
state->size = 0;
return STATE_VALID;
}
static void psg_state_restore(struct mmu_state *state)
{
}
void psg_init()
{
int i;
struct mmu *psg;
psg = mmu_create("PSG0", "Programmable Sound Generator");
psg->start = PSGBASE;
psg->size = PSGSIZE;
psg->read_byte = psg_read_byte;
psg->read_word = psg_read_word;
psg->write_byte = psg_write_byte;
psg->write_word = psg_write_word;
psg->state_collect = psg_state_collect;
psg->state_restore = psg_state_restore;
psg->diagnostics = psg_diagnostics;
psg->interrupt = psg_do_interrupts;
mmu_register(psg);
if(psgoutput) {
snd_fd = open("psg.raw", O_WRONLY|O_TRUNC|O_CREAT, 0644);
}
if(play_audio) {
int first_device = 0;
int count = SDL_GetNumAudioDevices(0);
SDL_memset(&want, 0, sizeof(want));
want.freq = PSG_OUTPUT_FREQ;
want.format = AUDIO_S16LSB;
want.channels = 2;
want.samples = 4096;
want.callback = psg_audio_callback;
DEBUG("Audio devices: %d", count);
if(audio_device > 0) {
first_device = audio_device;
}
for (i = first_device; i < count; ++i) {
SDL_Log("Audio device %d: %s\n", i, SDL_GetAudioDeviceName(i, 0));
if(audio_device != -1) {
psg_audio_device = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(i, 0), 0,
&want, &have, 0);
SDL_PauseAudioDevice(psg_audio_device, 0);
break;
}
}
if(audio_device == -1) {
exit(-1);
}
}
for(i=0;i<32;i++) {
psg_volume_voltage[i] /= 2;
}
}
void psg_print_status()
{
int i;
printf("PSG: %d: ", psgactive);
for(i=0;i<16;i++)
printf("%02x ",psgreg[i]);
printf("\n\n");
}
static void psg_generate_presamples(long cycles)
{
int i,c;
int out;
int noise;
int tone[3];
int tonemask, noisemask;
for(i=0;i<cycles;i++) {
for(c=0;c<3;c++) {
psg_periodcnt[c]--;
if(psg_periodcnt[c] < 0) {
psg_periodcnt[c] = psg_set_period(c);
}
if(psg_periodcnt[c] > (psg_set_period(c)/2)) {
tone[c] = 1;
} else {
tone[c] = 0;
}
}
psg_noisecnt--;
if(psg_noisecnt < 0) {
psg_noisecnt = PSG_PERIODDIV*psgreg[PSG_NOISE];
psg_noise |= ((psg_noise^(psg_noise>>2))&1)<<17;
psg_noise >>= 1;
}
noise = psg_noise&1;
if(env_set_period()) {
env_cnt--;
if(env_cnt < 0) {
env_cnt = env_set_period();
if(env_first) { /* Make sure initial -1 is ignored */
env_first = 0;
} else {
if(!PSG_ENV_CONTINUE) {
env_volume = 0;
env_held = 1;
} else {
if(PSG_ENV_HOLD) {
env_held = 1;
if((psgreg[PSG_ENV] == 11) || (psgreg[PSG_ENV] == 13)) {
/* (11, 13) end high */
env_volume = 31;
} else { /* (9, 15) should end low */
env_volume = 0;
}
} else {
if(PSG_ENV_ALTERNATE) {
if(env_attack) {
env_attack = 0;
} else {
env_attack = 1;
}
}
}
}
}
}
if(!env_held) {
int vol;
vol = (31*env_cnt)/env_set_period();
if(env_attack) {
env_volume = psg_volume_voltage[31-vol];
} else {
env_volume = psg_volume_voltage[vol];
}
}
}
for(c=0;c<3;c++) {
tonemask = (psgreg[PSG_MIXER]>>c)&1;
noisemask = (psgreg[PSG_MIXER]>>(c+3))&1;
out = (tone[c]|tonemask)&(noise|noisemask);
out = (out<<1)-1;
if(psgreg[PSG_AVOL+c]&0x10) {
psg_presample[(psg_presamplepos)%PSG_PRESAMPLESIZE][c] =
out * env_volume;
} else {
psg_presample[(psg_presamplepos)%PSG_PRESAMPLESIZE][c] =
out * psg_volume[c];
}
}
psg_presamplepos++;
if(psg_presamplepos > (PSG_PRESAMPLESIZE-1))
psg_presamplepos = 0;
}
}
static void psg_generate_samples()
{
int presamples,pos;
int i,c;
signed long out;
signed short outsign;
if(psg_presamplepos < psg_presamplestart) {
pos = psg_presamplepos + PSG_PRESAMPLESIZE;
} else {
pos = psg_presamplepos;
}
presamples = pos - psg_presamplestart;
while(presamples >= PSG_PRESAMPLES_PER_SAMPLE) {
out = 0;
for(i=0;i<PSG_PRESAMPLES_PER_SAMPLE;i++) {
for(c=0;c<3;c++) {
out += psg_presample[(psg_presamplestart+i)%PSG_PRESAMPLESIZE][c];
}
}
out = (out/(PSG_PRESAMPLES_PER_SAMPLE*3));
if(psgoutput) {
outsign = out&0xffff;
}
if(!psg_running && (out != 0))
psg_running = 1;
if(psg_running) {
if(psgoutput) {
if(write(snd_fd, &outsign, 2) != 2)
WARNING(write);
if(write(snd_fd, &outsign, 2) != 2)
WARNING(write);
}
if(play_audio) {
if(psg_audio_buffer_cnt < sizeof(psg_audio_buffer)-2) {
psg_audio_buffer[psg_audio_buffer_cnt++] = out;
psg_audio_buffer[psg_audio_buffer_cnt++] = out;
}
}
}
psg_presamplestart += PSG_PRESAMPLES_PER_SAMPLE;
if(psg_presamplestart > (PSG_PRESAMPLESIZE-1)) {
psg_presamplestart -= PSG_PRESAMPLESIZE;
}
if(psg_presamplepos < psg_presamplestart) {
pos = psg_presamplepos + PSG_PRESAMPLESIZE;
} else {
pos = psg_presamplepos;
}
presamples = pos - psg_presamplestart;
}
}
static void psg_do_interrupts(struct cpu *cpu)
{
long tmpcpu;
tmpcpu = cpu->cycle - lastcpucnt;
if(tmpcpu < 0) {
tmpcpu += MAX_CYCLE;
}
if(psgoutput || play_audio) {
psg_generate_presamples(tmpcpu/4);
psg_generate_samples();
}
lastcpucnt = cpu->cycle;
}
static void psg_audio_callback(void *userdata, Uint8 *stream, int len)
{
Uint8 *psgbuftmp = (Uint8 *)&psg_audio_buffer[0];
int tmplen;
if(psg_audio_buffer_cnt * 2 < len) {
tmplen = psg_audio_buffer_cnt * 2;
} else {
tmplen = len;
}
memcpy(stream, psgbuftmp, tmplen);
memset(stream+tmplen, 0, len-tmplen);
memcpy(psgbuftmp, psgbuftmp + tmplen, tmplen);
psg_audio_buffer_cnt = 0;
}