| 1 |
// license:BSD-3-Clause |
| 2 |
// copyright-holders:Juergen Buchmueller, Manuel Abadia |
| 3 |
/*************************************************************************** |
| 4 |
|
| 5 |
Philips SAA1099 Sound driver |
| 6 |
|
| 7 |
By Juergen Buchmueller and Manuel Abadia |
| 8 |
|
| 9 |
SAA1099 register layout: |
| 10 |
======================== |
| 11 |
|
| 12 |
offs | 7654 3210 | description |
| 13 |
-----+-----------+--------------------------- |
| 14 |
0x00 | ---- xxxx | Amplitude channel 0 (left) |
| 15 |
0x00 | xxxx ---- | Amplitude channel 0 (right) |
| 16 |
0x01 | ---- xxxx | Amplitude channel 1 (left) |
| 17 |
0x01 | xxxx ---- | Amplitude channel 1 (right) |
| 18 |
0x02 | ---- xxxx | Amplitude channel 2 (left) |
| 19 |
0x02 | xxxx ---- | Amplitude channel 2 (right) |
| 20 |
0x03 | ---- xxxx | Amplitude channel 3 (left) |
| 21 |
0x03 | xxxx ---- | Amplitude channel 3 (right) |
| 22 |
0x04 | ---- xxxx | Amplitude channel 4 (left) |
| 23 |
0x04 | xxxx ---- | Amplitude channel 4 (right) |
| 24 |
0x05 | ---- xxxx | Amplitude channel 5 (left) |
| 25 |
0x05 | xxxx ---- | Amplitude channel 5 (right) |
| 26 |
| | |
| 27 |
0x08 | xxxx xxxx | Frequency channel 0 |
| 28 |
0x09 | xxxx xxxx | Frequency channel 1 |
| 29 |
0x0a | xxxx xxxx | Frequency channel 2 |
| 30 |
0x0b | xxxx xxxx | Frequency channel 3 |
| 31 |
0x0c | xxxx xxxx | Frequency channel 4 |
| 32 |
0x0d | xxxx xxxx | Frequency channel 5 |
| 33 |
| | |
| 34 |
0x10 | ---- -xxx | Channel 0 octave select |
| 35 |
0x10 | -xxx ---- | Channel 1 octave select |
| 36 |
0x11 | ---- -xxx | Channel 2 octave select |
| 37 |
0x11 | -xxx ---- | Channel 3 octave select |
| 38 |
0x12 | ---- -xxx | Channel 4 octave select |
| 39 |
0x12 | -xxx ---- | Channel 5 octave select |
| 40 |
| | |
| 41 |
0x14 | ---- ---x | Channel 0 frequency enable (0 = off, 1 = on) |
| 42 |
0x14 | ---- --x- | Channel 1 frequency enable (0 = off, 1 = on) |
| 43 |
0x14 | ---- -x-- | Channel 2 frequency enable (0 = off, 1 = on) |
| 44 |
0x14 | ---- x--- | Channel 3 frequency enable (0 = off, 1 = on) |
| 45 |
0x14 | ---x ---- | Channel 4 frequency enable (0 = off, 1 = on) |
| 46 |
0x14 | --x- ---- | Channel 5 frequency enable (0 = off, 1 = on) |
| 47 |
| | |
| 48 |
0x15 | ---- ---x | Channel 0 noise enable (0 = off, 1 = on) |
| 49 |
0x15 | ---- --x- | Channel 1 noise enable (0 = off, 1 = on) |
| 50 |
0x15 | ---- -x-- | Channel 2 noise enable (0 = off, 1 = on) |
| 51 |
0x15 | ---- x--- | Channel 3 noise enable (0 = off, 1 = on) |
| 52 |
0x15 | ---x ---- | Channel 4 noise enable (0 = off, 1 = on) |
| 53 |
0x15 | --x- ---- | Channel 5 noise enable (0 = off, 1 = on) |
| 54 |
| | |
| 55 |
0x16 | ---- --xx | Noise generator parameters 0 |
| 56 |
0x16 | --xx ---- | Noise generator parameters 1 |
| 57 |
| | |
| 58 |
0x18 | --xx xxxx | Envelope generator 0 parameters |
| 59 |
0x18 | x--- ---- | Envelope generator 0 control enable (0 = off, 1 = on) |
| 60 |
0x19 | --xx xxxx | Envelope generator 1 parameters |
| 61 |
0x19 | x--- ---- | Envelope generator 1 control enable (0 = off, 1 = on) |
| 62 |
| | |
| 63 |
0x1c | ---- ---x | All channels enable (0 = off, 1 = on) |
| 64 |
0x1c | ---- --x- | Synch & Reset generators |
| 65 |
|
| 66 |
Unspecified bits should be written as zero. |
| 67 |
|
| 68 |
***************************************************************************/ |
| 69 |
|
| 70 |
#include "saa1099.h" |
| 71 |
#include <string.h> |
| 72 |
|
| 73 |
#define BIT(x,n) (((x)>>(n))&1) |
| 74 |
|
| 75 |
static constexpr int clock_divider = 32; |
| 76 |
|
| 77 |
static constexpr int LEFT = 0x00; |
| 78 |
static constexpr int RIGHT = 0x01; |
| 79 |
|
| 80 |
static constexpr unsigned short amplitude_lookup[16] = { |
| 81 |
0*32767/96, 1*32767/96, 2*32767/96, 3*32767/96, |
| 82 |
4*32767/96, 5*32767/96, 6*32767/96, 7*32767/96, |
| 83 |
8*32767/96, 9*32767/96, 10*32767/96, 11*32767/96, |
| 84 |
12*32767/96, 13*32767/96, 14*32767/96, 15*32767/96 |
| 85 |
}; |
| 86 |
|
| 87 |
static constexpr unsigned char envelope[8][64] = { |
| 88 |
/* zero amplitude */ |
| 89 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 90 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 91 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 92 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 93 |
/* maximum amplitude */ |
| 94 |
{15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, |
| 95 |
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, |
| 96 |
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, |
| 97 |
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, }, |
| 98 |
/* single decay */ |
| 99 |
{15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
| 100 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 101 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 102 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 103 |
/* repetitive decay */ |
| 104 |
{15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
| 105 |
15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
| 106 |
15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
| 107 |
15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, |
| 108 |
/* single triangular */ |
| 109 |
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 110 |
15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
| 111 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 112 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 113 |
/* repetitive triangular */ |
| 114 |
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 115 |
15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
| 116 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 117 |
15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, |
| 118 |
/* single attack */ |
| 119 |
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 120 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 121 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 122 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 123 |
/* repetitive attack */ |
| 124 |
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 125 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 126 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, |
| 127 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 } |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
//************************************************************************** |
| 132 |
// LIVE DEVICE |
| 133 |
//************************************************************************** |
| 134 |
|
| 135 |
//------------------------------------------------- |
| 136 |
// saa1099_device - constructor |
| 137 |
//------------------------------------------------- |
| 138 |
|
| 139 |
saa1099_device::saa1099_device() |
| 140 |
: m_noise_params{ 0, 0 } |
| 141 |
, m_env_enable{ false, false } |
| 142 |
, m_env_reverse_right{ false, false } |
| 143 |
, m_env_mode{ 0, 0 } |
| 144 |
, m_env_bits{ false, false } |
| 145 |
, m_env_clock{ false, false } |
| 146 |
, m_env_step{ 0, 0 } |
| 147 |
, m_all_ch_enable(false) |
| 148 |
, m_sync_state(false) |
| 149 |
, m_selected_reg(0) |
| 150 |
{ |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
//------------------------------------------------- |
| 155 |
// device_start - device-specific startup |
| 156 |
//------------------------------------------------- |
| 157 |
|
| 158 |
void saa1099_device::device_start() |
| 159 |
{ |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
//------------------------------------------------- |
| 164 |
// sound_stream_update - handle a stream update |
| 165 |
//------------------------------------------------- |
| 166 |
|
| 167 |
void saa1099_device::sound_stream_update(short** outputs, int len, DivDispatchOscBuffer** oscBuf) |
| 168 |
{ |
| 169 |
int j, ch; |
| 170 |
/* if the channels are disabled we're done */ |
| 171 |
if (!m_all_ch_enable) |
| 172 |
{ |
| 173 |
/* init output data */ |
| 174 |
memset(outputs[LEFT],0,len*sizeof(short)); |
| 175 |
memset(outputs[RIGHT],0,len*sizeof(short)); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
for (ch = 0; ch < 2; ch++) |
| 180 |
{ |
| 181 |
switch (m_noise_params[ch]) |
| 182 |
{ |
| 183 |
case 0: |
| 184 |
case 1: |
| 185 |
case 2: m_noise[ch].freq = 256 << m_noise_params[ch]; break; |
| 186 |
case 3: m_noise[ch].freq = m_channels[ch * 3].freq(); break; // todo: this case will be clock()/[ch*3's octave divisor, 0 is = 256*2, higher numbers are higher] * 2 if the tone generator phase reset bit (0x1c bit 1) is set. |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* fill all data needed */ |
| 191 |
for( j = 0; j < len; j++ ) |
| 192 |
{ |
| 193 |
int output_l = 0, output_r = 0; |
| 194 |
|
| 195 |
/* for each channel */ |
| 196 |
for (ch = 0; ch < 6; ch++) |
| 197 |
{ |
| 198 |
/* check the actual position in the square wave */ |
| 199 |
while (m_channels[ch].counter <= 0) |
| 200 |
{ |
| 201 |
/* calculate new frequency now after the half wave is updated */ |
| 202 |
m_channels[ch].counter += m_channels[ch].freq(); |
| 203 |
m_channels[ch].level ^= 1; |
| 204 |
|
| 205 |
/* eventually clock the envelope counters */ |
| 206 |
if (ch == 1 && m_env_clock[0] == 0) |
| 207 |
envelope_w(0); |
| 208 |
if (ch == 4 && m_env_clock[1] == 0) |
| 209 |
envelope_w(1); |
| 210 |
} |
| 211 |
m_channels[ch].counter -= clock_divider; |
| 212 |
|
| 213 |
// if the noise is enabled |
| 214 |
unsigned char level = 0; |
| 215 |
if (m_channels[ch].noise_enable) |
| 216 |
{ |
| 217 |
// if the noise level is high (noise 0: chan 0-2, noise 1: chan 3-5) |
| 218 |
level ^= m_noise[ch/3].level & 1; |
| 219 |
} |
| 220 |
// if the square wave is enabled |
| 221 |
if (m_channels[ch].freq_enable) |
| 222 |
{ |
| 223 |
// if the channel level is high |
| 224 |
level ^= m_channels[ch].level & 1; |
| 225 |
} |
| 226 |
if (level) |
| 227 |
{ |
| 228 |
int this_output_l = m_channels[ch].amplitude[ LEFT] * m_channels[ch].envelope[ LEFT] / 16; |
| 229 |
int this_output_r = m_channels[ch].amplitude[RIGHT] * m_channels[ch].envelope[RIGHT] / 16; |
| 230 |
output_l+=this_output_l; |
| 231 |
output_r+=this_output_r; |
| 232 |
oscBuf[ch]->data[oscBuf[ch]->needle++]=(this_output_l+this_output_r)<<1; |
| 233 |
} else if (oscBuf!=NULL) { |
| 234 |
oscBuf[ch]->data[oscBuf[ch]->needle++]=0; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
for (ch = 0; ch < 2; ch++) |
| 239 |
{ |
| 240 |
/* update the state of the noise generator |
| 241 |
* polynomial is x^18 + x^11 + x (i.e. 0x20400) and is a plain XOR, initial state is probably all 1s |
| 242 |
* see http://www.vogons.org/viewtopic.php?f=9&t=51695 */ |
| 243 |
while (m_noise[ch].counter <= 0) |
| 244 |
{ |
| 245 |
m_noise[ch].counter += m_noise[ch].freq; // clock / ((511 - frequency) * 2^(8 - octave)) or clock / 2^(8 + noise period) |
| 246 |
if( ((m_noise[ch].level & 0x20000) == 0) != ((m_noise[ch].level & 0x0400) == 0) ) |
| 247 |
m_noise[ch].level = (m_noise[ch].level << 1) | 1; |
| 248 |
else |
| 249 |
m_noise[ch].level <<= 1; |
| 250 |
} |
| 251 |
m_noise[ch].counter -= clock_divider; |
| 252 |
} |
| 253 |
/* write sound data to the buffer */ |
| 254 |
outputs[LEFT][j]=output_l; |
| 255 |
outputs[RIGHT][j]=output_r; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
void saa1099_device::envelope_w(int ch) |
| 261 |
{ |
| 262 |
if (m_env_enable[ch]) |
| 263 |
{ |
| 264 |
int step, mode, mask; |
| 265 |
mode = m_env_mode[ch]; |
| 266 |
/* step from 0..63 and then loop in steps 32..63 */ |
| 267 |
step = m_env_step[ch] = |
| 268 |
((m_env_step[ch] + 1) & 0x3f) | (m_env_step[ch] & 0x20); |
| 269 |
|
| 270 |
mask = 15; |
| 271 |
if (m_env_bits[ch]) |
| 272 |
mask &= ~1; /* 3 bit resolution, mask LSB */ |
| 273 |
|
| 274 |
m_channels[ch*3+0].envelope[ LEFT] = |
| 275 |
m_channels[ch*3+1].envelope[ LEFT] = |
| 276 |
m_channels[ch*3+2].envelope[ LEFT] = envelope[mode][step] & mask; |
| 277 |
if (m_env_reverse_right[ch]) |
| 278 |
{ |
| 279 |
m_channels[ch*3+0].envelope[RIGHT] = |
| 280 |
m_channels[ch*3+1].envelope[RIGHT] = |
| 281 |
m_channels[ch*3+2].envelope[RIGHT] = (15 - envelope[mode][step]) & mask; |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
m_channels[ch*3+0].envelope[RIGHT] = |
| 286 |
m_channels[ch*3+1].envelope[RIGHT] = |
| 287 |
m_channels[ch*3+2].envelope[RIGHT] = envelope[mode][step] & mask; |
| 288 |
} |
| 289 |
} |
| 290 |
else |
| 291 |
{ |
| 292 |
/* envelope mode off, set all envelope factors to 16 */ |
| 293 |
m_channels[ch*3+0].envelope[ LEFT] = |
| 294 |
m_channels[ch*3+1].envelope[ LEFT] = |
| 295 |
m_channels[ch*3+2].envelope[ LEFT] = |
| 296 |
m_channels[ch*3+0].envelope[RIGHT] = |
| 297 |
m_channels[ch*3+1].envelope[RIGHT] = |
| 298 |
m_channels[ch*3+2].envelope[RIGHT] = 16; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
void saa1099_device::control_w(unsigned char data) |
| 304 |
{ |
| 305 |
if ((data & 0xff) > 0x1c) |
| 306 |
{ |
| 307 |
/* Error! */ |
| 308 |
} |
| 309 |
|
| 310 |
m_selected_reg = data & 0x1f; |
| 311 |
if (m_selected_reg == 0x18 || m_selected_reg == 0x19) |
| 312 |
{ |
| 313 |
/* clock the envelope channels */ |
| 314 |
if (m_env_clock[0]) |
| 315 |
envelope_w(0); |
| 316 |
if (m_env_clock[1]) |
| 317 |
envelope_w(1); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
void saa1099_device::data_w(unsigned char data) |
| 323 |
{ |
| 324 |
int reg = m_selected_reg; |
| 325 |
int ch; |
| 326 |
|
| 327 |
switch (reg) |
| 328 |
{ |
| 329 |
/* channel i amplitude */ |
| 330 |
case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: |
| 331 |
ch = reg & 7; |
| 332 |
m_channels[ch].amplitude[LEFT] = amplitude_lookup[data & 0x0f]; |
| 333 |
m_channels[ch].amplitude[RIGHT] = amplitude_lookup[(data >> 4) & 0x0f]; |
| 334 |
break; |
| 335 |
/* channel i frequency */ |
| 336 |
case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: |
| 337 |
ch = reg & 7; |
| 338 |
m_channels[ch].frequency = data & 0xff; |
| 339 |
break; |
| 340 |
/* channel i octave */ |
| 341 |
case 0x10: case 0x11: case 0x12: |
| 342 |
ch = (reg - 0x10) << 1; |
| 343 |
m_channels[ch + 0].octave = data & 0x07; |
| 344 |
m_channels[ch + 1].octave = (data >> 4) & 0x07; |
| 345 |
break; |
| 346 |
/* channel i frequency enable */ |
| 347 |
case 0x14: |
| 348 |
for (ch = 0; ch < 6; ch++) |
| 349 |
m_channels[ch].freq_enable = BIT(data, ch); |
| 350 |
break; |
| 351 |
/* channel i noise enable */ |
| 352 |
case 0x15: |
| 353 |
for (ch = 0; ch < 6; ch++) |
| 354 |
m_channels[ch].noise_enable = BIT(data, ch); |
| 355 |
break; |
| 356 |
/* noise generators parameters */ |
| 357 |
case 0x16: |
| 358 |
m_noise_params[0] = data & 0x03; |
| 359 |
m_noise_params[1] = (data >> 4) & 0x03; |
| 360 |
break; |
| 361 |
/* envelope generators parameters */ |
| 362 |
case 0x18: case 0x19: |
| 363 |
ch = reg - 0x18; |
| 364 |
m_env_reverse_right[ch] = BIT(data, 0); |
| 365 |
m_env_mode[ch] = (data >> 1) & 0x07; |
| 366 |
m_env_bits[ch] = BIT(data, 4); |
| 367 |
m_env_clock[ch] = BIT(data, 5); |
| 368 |
m_env_enable[ch] = BIT(data, 7); |
| 369 |
/* reset the envelope */ |
| 370 |
m_env_step[ch] = 0; |
| 371 |
break; |
| 372 |
/* channels enable & reset generators */ |
| 373 |
case 0x1c: |
| 374 |
m_all_ch_enable = BIT(data, 0); |
| 375 |
m_sync_state = BIT(data, 1); |
| 376 |
if (BIT(data, 1)) |
| 377 |
{ |
| 378 |
/* Synch & Reset generators */ |
| 379 |
for (int i = 0; i < 6; i++) |
| 380 |
{ |
| 381 |
m_channels[i].level = 0; |
| 382 |
m_channels[i].counter = m_channels[i].freq(); |
| 383 |
} |
| 384 |
} |
| 385 |
break; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
void saa1099_device::write(int offset, unsigned char data) |
| 390 |
{ |
| 391 |
if (offset & 1) |
| 392 |
control_w(data); |
| 393 |
else |
| 394 |
data_w(data); |
| 395 |
} |