1
/*****************************************************************************/
3
/* Module: TIA Chip Sound Simulator */
4
/* Purpose: To emulate the sound generation hardware of the Atari TIA chip. */
5
/* Author: Ron Fries */
7
/* Revision History: */
8
/* 10-Sep-96 - V1.0 - Initial Release */
9
/* 14-Jan-97 - V1.1 - Cleaned up sound output by eliminating counter */
12
/*****************************************************************************/
14
/* License Information and Copyright Notice */
15
/* ======================================== */
17
/* TiaSound is Copyright(c) 1996 by Ron Fries */
19
/* This library is free software; you can redistribute it and/or modify it */
20
/* under the terms of version 2 of the GNU Library General Public License */
21
/* as published by the Free Software Foundation. */
23
/* This library is distributed in the hope that it will be useful, but */
24
/* WITHOUT ANY WARRANTY; without even the implied warranty of */
25
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library */
26
/* General Public License for more details. */
27
/* To obtain a copy of the GNU Library General Public License, write to the */
28
/* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
30
/* Any permitted reproduction of these routines, in whole or in part, must */
31
/* bear this legend. */
33
/*****************************************************************************/
40
/* define some data types to keep it platform independent */
51
#define uint8 unsigned int8
52
#define uint16 unsigned int16
53
#define uint32 unsigned int32
56
/* CONSTANT DEFINITIONS */
58
/* definitions for AUDCx (15, 16) */
59
#define SET_TO_1 0x00 /* 0000 */
60
#define POLY4 0x01 /* 0001 */
61
#define DIV31_POLY4 0x02 /* 0010 */
62
#define POLY5_POLY4 0x03 /* 0011 */
63
#define PURE 0x04 /* 0100 */
64
#define PURE2 0x05 /* 0101 */
65
#define DIV31_PURE 0x06 /* 0110 */
66
#define POLY5_2 0x07 /* 0111 */
67
#define POLY9 0x08 /* 1000 */
68
#define POLY5 0x09 /* 1001 */
69
#define DIV31_POLY5 0x0a /* 1010 */
70
#define POLY5_POLY5 0x0b /* 1011 */
71
#define DIV3_PURE 0x0c /* 1100 */
72
#define DIV3_PURE2 0x0d /* 1101 */
73
#define DIV93_PURE 0x0e /* 1110 */
74
#define DIV3_POLY5 0x0f /* 1111 */
76
#define DIV3_MASK 0x0c
85
/* the size (in entries) of the 4 polynomial tables */
86
#define POLY4_SIZE 0x000f
87
#define POLY5_SIZE 0x001f
88
#define POLY9_SIZE 0x01ff
90
/* channel definitions */
98
/* LOCAL GLOBAL VARIABLE DEFINITIONS */
100
/* structures to hold the 6 tia sound control bytes */
101
static uint8 AUDC[2]; /* AUDCx (15, 16) */
102
static uint8 AUDF[2]; /* AUDFx (17, 18) */
103
static uint8 AUDV[2]; /* AUDVx (19, 1A) */
105
static uint8 Outvol[2]; /* last output volume for each channel */
108
/* Initialze the bit patterns for the polynomials. */
110
/* The 4bit and 5bit patterns are the identical ones used in the tia chip. */
111
/* Though the patterns could be packed with 8 bits per byte, using only a */
112
/* single bit per byte keeps the math simple, which is important for */
113
/* efficient processing. */
115
static uint8 Bit4[POLY4_SIZE] =
116
{ 1,1,0,1,1,1,0,0,0,0,1,0,1,0,0 };
118
static uint8 Bit5[POLY5_SIZE] =
119
{ 0,0,1,0,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,0,0,0,1 };
121
/* I've treated the 'Div by 31' counter as another polynomial because of */
122
/* the way it operates. It does not have a 50% duty cycle, but instead */
123
/* has a 13:18 ratio (of course, 13+18 = 31). This could also be */
124
/* implemented by using counters. */
126
static uint8 Div31[POLY5_SIZE] =
127
{ 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 };
129
/* Rather than have a table with 511 entries, I use a random number */
132
static uint8 Bit9[POLY9_SIZE];
134
static uint8 P4[2]; /* Position pointer for the 4-bit POLY array */
135
static uint8 P5[2]; /* Position pointer for the 5-bit POLY array */
136
static uint16 P9[2]; /* Position pointer for the 9-bit POLY array */
138
static uint8 Div_n_cnt[2]; /* Divide by n counter. one for each channel */
139
static uint8 Div_n_max[2]; /* Divide by n maximum, one for each channel */
142
/* In my routines, I treat the sample output as another divide by N counter. */
143
/* For better accuracy, the Samp_n_cnt has a fixed binary decimal point */
144
/* which has 8 binary digits to the right of the decimal point. */
146
static uint16 Samp_n_max; /* Sample max, multiplied by 256 */
147
static uint16 Samp_n_cnt; /* Sample cnt. */
151
/*****************************************************************************/
152
/* Module: Tia_sound_init() */
153
/* Purpose: to handle the power-up initialization functions */
154
/* these functions should only be executed on a cold-restart */
156
/* Author: Ron Fries */
157
/* Date: September 10, 1996 */
159
/* Inputs: sample_freq - the value for the '30 Khz' Tia audio clock */
160
/* playback_freq - the playback frequency in samples per second */
162
/* Outputs: Adjusts local globals - no return value */
164
/*****************************************************************************/
166
void Tia_sound_init (uint16 sample_freq, uint16 playback_freq)
171
/* fill the 9bit polynomial with random bits */
172
for (n=0; n<POLY9_SIZE; n++)
174
Bit9[n] = rand() & 0x01; /* fill poly9 with random bits */
177
/* calculate the sample 'divide by N' value based on the playback freq. */
178
Samp_n_max = (uint16)(((uint32)sample_freq<<8)/playback_freq);
179
Samp_n_cnt = 0; /* initialize all bits of the sample counter */
181
/* initialize the local globals */
182
for (chan = CHAN1; chan <= CHAN2; chan++)
197
/*****************************************************************************/
198
/* Module: Update_tia_sound() */
199
/* Purpose: To process the latest control values stored in the AUDF, AUDC, */
200
/* and AUDV registers. It pre-calculates as much information as */
201
/* possible for better performance. This routine has not been */
204
/* Author: Ron Fries */
205
/* Date: January 14, 1997 */
207
/* Inputs: addr - the address of the parameter to be changed */
208
/* val - the new value to be placed in the specified address */
210
/* Outputs: Adjusts local globals - no return value */
212
/*****************************************************************************/
214
void Update_tia_sound (uint16 addr, uint8 val)
219
/* determine which address was changed */
223
AUDC[0] = val & 0x0f;
228
AUDC[1] = val & 0x0f;
233
AUDF[0] = val & 0x1f;
238
AUDF[1] = val & 0x1f;
243
AUDV[0] = (val & 0x0f) << 3;
248
AUDV[1] = (val & 0x0f) << 3;
257
/* if the output value changed */
260
/* an AUDC value of 0 is a special case */
261
if (AUDC[chan] == SET_TO_1)
263
/* indicate the clock is zero so no processing will occur */
266
/* and set the output to the selected volume */
267
Outvol[chan] = AUDV[chan];
271
/* otherwise calculate the 'divide by N' value */
272
new_val = AUDF[chan] + 1;
274
/* if bits 2 & 3 are set, then multiply the 'div by n' count by 3 */
275
if ((AUDC[chan] & DIV3_MASK) == DIV3_MASK)
281
/* only reset those channels that have changed */
282
if (new_val != Div_n_max[chan])
284
/* reset the divide by n counters */
285
Div_n_max[chan] = new_val;
287
/* if the channel is now volume only or was volume only */
288
if ((Div_n_cnt[chan] == 0) || (new_val == 0))
290
/* reset the counter (otherwise let it complete the previous) */
291
Div_n_cnt[chan] = new_val;
298
/*****************************************************************************/
299
/* Module: Tia_process_2() */
300
/* Purpose: To fill the output buffer with the sound output based on the */
301
/* tia chip parameters. This routine has not been optimized. */
302
/* Though it is not used by the program, I've left it for reference.*/
304
/* Author: Ron Fries */
305
/* Date: September 10, 1996 */
307
/* Inputs: *buffer - pointer to the buffer where the audio output will */
309
/* n - size of the playback buffer */
311
/* Outputs: the buffer will be filled with n bytes of audio - no return val */
313
/*****************************************************************************/
315
void Tia_process_2 (register unsigned char *buffer, register uint16 n)
319
/* loop until the buffer is filled */
322
/* loop through the channels */
323
for (chan = CHAN1; chan <= CHAN2; chan++)
325
/* NOTE: this routine intentionally does not count down to zero */
326
/* since 0 is used as a special case - no clock */
328
/* if the divide by N counter can count down */
329
if (Div_n_cnt[chan] > 1)
331
/* decrement and loop */
334
/* otherwise if we've reached the bottom */
335
else if (Div_n_cnt[chan] == 1)
337
/* reset the counter */
338
Div_n_cnt[chan] = Div_n_max[chan];
340
/* the P5 counter has multiple uses, so we inc it here */
342
if (P5[chan] == POLY5_SIZE)
345
/* check clock modifier for clock tick */
347
/* if we're using pure tones OR
348
we're using DIV31 and the DIV31 bit is set OR
349
we're using POLY5 and the POLY5 bit is set */
350
if (((AUDC[chan] & 0x02) == 0) ||
351
(((AUDC[chan] & 0x01) == 0) && Div31[P5[chan]]) ||
352
(((AUDC[chan] & 0x01) == 1) && Bit5[P5[chan]]))
354
if (AUDC[chan] & 0x04) /* pure modified clock selected */
356
if (Outvol[chan]) /* if the output was set */
357
Outvol[chan] = 0; /* turn it off */
359
Outvol[chan] = AUDV[chan]; /* else turn it on */
361
else if (AUDC[chan] & 0x08) /* check for p5/p9 */
363
if (AUDC[chan] == POLY9) /* check for poly9 */
365
/* inc the poly9 counter */
367
if (P9[chan] == POLY9_SIZE)
370
if (Bit9[P9[chan]]) /* if poly9 bit is set */
371
Outvol[chan] = AUDV[chan];
375
else /* must be poly5 */
378
Outvol[chan] = AUDV[chan];
383
else /* poly4 is the only remaining option */
385
/* inc the poly4 counter */
387
if (P4[chan] == POLY4_SIZE)
391
Outvol[chan] = AUDV[chan];
399
/* decrement the sample counter - value is 256 since the lower
400
byte contains the fractional part */
403
/* if the count down has reached zero */
404
if (Samp_n_cnt < 256)
406
/* adjust the sample counter */
407
Samp_n_cnt += Samp_n_max;
409
/* calculate the latest output value and place in buffer */
410
*(buffer++) = Outvol[0] + Outvol[1];
412
/* and indicate one less byte to process */
419
/*****************************************************************************/
420
/* Module: Tia_process() */
421
/* Purpose: To fill the output buffer with the sound output based on the */
422
/* tia chip parameters. This routine has been optimized. */
424
/* Author: Ron Fries */
425
/* Date: September 10, 1996 */
427
/* Inputs: *buffer - pointer to the buffer where the audio output will */
429
/* n - size of the playback buffer */
431
/* Outputs: the buffer will be filled with n bytes of audio - no return val */
433
/*****************************************************************************/
435
void Tia_process (register unsigned char *buffer, register uint16 n)
437
register uint8 audc0,audv0,audc1,audv1;
438
register uint8 div_n_cnt0,div_n_cnt1;
439
register uint8 p5_0, p5_1,outvol_0,outvol_1;
446
/* make temporary local copy */
449
outvol_0 = Outvol[0];
450
outvol_1 = Outvol[1];
451
div_n_cnt0 = Div_n_cnt[0];
452
div_n_cnt1 = Div_n_cnt[1];
454
/* loop until the buffer is filled */
457
/* Process channel 0 */
462
else if (div_n_cnt0 == 1)
464
div_n_cnt0 = Div_n_max[0];
466
/* the P5 counter has multiple uses, so we inc it here */
468
if (p5_0 == POLY5_SIZE)
471
/* check clock modifier for clock tick */
472
if (((audc0 & 0x02) == 0) ||
473
(((audc0 & 0x01) == 0) && Div31[p5_0]) ||
474
(((audc0 & 0x01) == 1) && Bit5[p5_0]))
476
if (audc0 & 0x04) /* pure modified clock selected */
478
if (outvol_0) /* if the output was set */
479
outvol_0 = 0; /* turn it off */
481
outvol_0 = audv0; /* else turn it on */
483
else if (audc0 & 0x08) /* check for p5/p9 */
485
if (audc0 == POLY9) /* check for poly9 */
487
/* inc the poly9 counter */
489
if (P9[0] == POLY9_SIZE)
497
else /* must be poly5 */
505
else /* poly4 is the only remaining option */
507
/* inc the poly4 counter */
509
if (P4[0] == POLY4_SIZE)
521
/* Process channel 1 */
526
else if (div_n_cnt1 == 1)
528
div_n_cnt1 = Div_n_max[1];
530
/* the P5 counter has multiple uses, so we inc it here */
532
if (p5_1 == POLY5_SIZE)
535
/* check clock modifier for clock tick */
536
if (((audc1 & 0x02) == 0) ||
537
(((audc1 & 0x01) == 0) && Div31[p5_1]) ||
538
(((audc1 & 0x01) == 1) && Bit5[p5_1]))
540
if (audc1 & 0x04) /* pure modified clock selected */
542
if (outvol_1) /* if the output was set */
543
outvol_1 = 0; /* turn it off */
545
outvol_1 = audv1; /* else turn it on */
547
else if (audc1 & 0x08) /* check for p5/p9 */
549
if (audc1 == POLY9) /* check for poly9 */
551
/* inc the poly9 counter */
553
if (P9[1] == POLY9_SIZE)
561
else /* must be poly5 */
569
else /* poly4 is the only remaining option */
571
/* inc the poly4 counter */
573
if (P4[1] == POLY4_SIZE)
584
/* decrement the sample counter - value is 256 since the lower
585
byte contains the fractional part */
588
/* if the count down has reached zero */
589
if (Samp_n_cnt < 256)
591
/* adjust the sample counter */
592
Samp_n_cnt += Samp_n_max;
594
/* calculate the latest output value and place in buffer */
595
*(buffer++) = outvol_0 + outvol_1;
597
/* and indicate one less byte to process */
602
/* save for next round */
605
Outvol[0] = outvol_0;
606
Outvol[1] = outvol_1;
607
Div_n_cnt[0] = div_n_cnt0;
608
Div_n_cnt[1] = div_n_cnt1;