| 1 |
/* |
| 2 |
* ted-sound.c |
| 3 |
* |
| 4 |
* Written by |
| 5 |
* Andreas Boose <[email protected]> |
| 6 |
* Tibor Biczo <crown @ axelero . hu> |
| 7 |
* Marco van den Heuvel <[email protected]> |
| 8 |
* |
| 9 |
* This file is part of VICE, the Versatile Commodore Emulator. |
| 10 |
* See README for copyright notice. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License as published by |
| 14 |
* the Free Software Foundation; either version 2 of the License, or |
| 15 |
* (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 25 |
* 02111-1307 USA. |
| 26 |
* |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "ted-sound.h" |
| 34 |
|
| 35 |
/* ------------------------------------------------------------------------- */ |
| 36 |
|
| 37 |
/* FIXME: Find proper volume multiplier. */ |
| 38 |
const int16_t volume_tab[16] = { |
| 39 |
0x0000, 0x0800, 0x1000, 0x1800, 0x2000, 0x2800, 0x3000, 0x3800, |
| 40 |
0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff, 0x3fff |
| 41 |
}; |
| 42 |
|
| 43 |
int ted_sound_machine_calculate_samples(struct plus4_sound_s* snd, int16_t *pbuf, int nr, int scc) |
| 44 |
{ |
| 45 |
int i; |
| 46 |
int j; |
| 47 |
int16_t volume; |
| 48 |
|
| 49 |
if (snd->digital) { |
| 50 |
for (i = 0; i < nr; i++) { |
| 51 |
pbuf[i] = (snd->volume * (snd->voice0_output_enabled + snd->voice1_output_enabled)); |
| 52 |
} |
| 53 |
} else { |
| 54 |
for (i = 0; i < nr; i++) { |
| 55 |
snd->sample_position_remainder += snd->sample_length_remainder; |
| 56 |
if (snd->sample_position_remainder >= snd->speed) { |
| 57 |
snd->sample_position_remainder -= snd->speed; |
| 58 |
snd->sample_position_integer++; |
| 59 |
} |
| 60 |
snd->sample_position_integer += snd->sample_length_integer; |
| 61 |
if (snd->sample_position_integer >= 8) { |
| 62 |
/* Advance state engine */ |
| 63 |
uint32_t ticks = snd->sample_position_integer >> 3; |
| 64 |
if (snd->voice0_accu <= ticks) { |
| 65 |
uint32_t delay = ticks - snd->voice0_accu; |
| 66 |
snd->voice0_sign ^= 1; |
| 67 |
snd->voice0_accu = 1023 - snd->voice0_reload; |
| 68 |
if (snd->voice0_accu == 0) { |
| 69 |
snd->voice0_accu = 1024; |
| 70 |
} |
| 71 |
if (delay >= snd->voice0_accu) { |
| 72 |
snd->voice0_sign = ((delay / snd->voice0_accu) |
| 73 |
& 1) ? snd->voice0_sign ^ 1 |
| 74 |
: snd->voice0_sign; |
| 75 |
snd->voice0_accu = snd->voice0_accu - (delay % snd->voice0_accu); |
| 76 |
} else { |
| 77 |
snd->voice0_accu -= delay; |
| 78 |
} |
| 79 |
} else { |
| 80 |
snd->voice0_accu -= ticks; |
| 81 |
} |
| 82 |
|
| 83 |
if (snd->voice1_accu <= ticks) { |
| 84 |
uint32_t delay = ticks - snd->voice1_accu; |
| 85 |
snd->voice1_sign ^= 1; |
| 86 |
snd->noise_shift_register |
| 87 |
= (snd->noise_shift_register << 1) + |
| 88 |
( 1 ^ ((snd->noise_shift_register >> 7) & 1) ^ |
| 89 |
((snd->noise_shift_register >> 5) & 1) ^ |
| 90 |
((snd->noise_shift_register >> 4) & 1) ^ |
| 91 |
((snd->noise_shift_register >> 1) & 1)); |
| 92 |
snd->voice1_accu = 1023 - snd->voice1_reload; |
| 93 |
if (snd->voice1_accu == 0) { |
| 94 |
snd->voice1_accu = 1024; |
| 95 |
} |
| 96 |
if (delay >= snd->voice1_accu) { |
| 97 |
snd->voice1_sign = ((delay / snd->voice1_accu) |
| 98 |
& 1) ? snd->voice1_sign ^ 1 |
| 99 |
: snd->voice1_sign; |
| 100 |
for (j = 0; j < (int)(delay / snd->voice1_accu); |
| 101 |
j++) { |
| 102 |
snd->noise_shift_register |
| 103 |
= (snd->noise_shift_register << 1) + |
| 104 |
( 1 ^ ((snd->noise_shift_register >> 7) & 1) ^ |
| 105 |
((snd->noise_shift_register >> 5) & 1) ^ |
| 106 |
((snd->noise_shift_register >> 4) & 1) ^ |
| 107 |
((snd->noise_shift_register >> 1) & 1)); |
| 108 |
} |
| 109 |
snd->voice1_accu = snd->voice1_accu - (delay % snd->voice1_accu); |
| 110 |
} else { |
| 111 |
snd->voice1_accu -= delay; |
| 112 |
} |
| 113 |
} else { |
| 114 |
snd->voice1_accu -= ticks; |
| 115 |
} |
| 116 |
} |
| 117 |
snd->sample_position_integer = snd->sample_position_integer & 7; |
| 118 |
|
| 119 |
volume = 0; |
| 120 |
|
| 121 |
if (snd->voice0_output_enabled && snd->voice0_sign) { |
| 122 |
volume += snd->volume; |
| 123 |
} |
| 124 |
if (snd->voice1_output_enabled && !snd->noise && snd->voice1_sign) { |
| 125 |
volume += snd->volume; |
| 126 |
} |
| 127 |
if (snd->voice1_output_enabled && snd->noise && (!(snd->noise_shift_register & 1))) { |
| 128 |
volume += snd->volume; |
| 129 |
} |
| 130 |
|
| 131 |
pbuf[i] = volume; |
| 132 |
} |
| 133 |
} |
| 134 |
return nr; |
| 135 |
} |
| 136 |
|
| 137 |
int ted_sound_machine_init(struct plus4_sound_s* snd, int speed, int cycles_per_sec) |
| 138 |
{ |
| 139 |
uint8_t val; |
| 140 |
memset(snd,0,sizeof(struct plus4_sound_s)); |
| 141 |
|
| 142 |
snd->speed = speed; |
| 143 |
snd->sample_length_integer = cycles_per_sec / speed; |
| 144 |
snd->sample_length_remainder = cycles_per_sec % speed; |
| 145 |
snd->sample_position_integer = 0; |
| 146 |
snd->sample_position_remainder = 0; |
| 147 |
snd->noise_shift_register = 0; |
| 148 |
|
| 149 |
snd->voice0_reload = (snd->plus4_sound_data[0] | (snd->plus4_sound_data[4] << 8)); |
| 150 |
snd->voice1_reload = (snd->plus4_sound_data[1] | (snd->plus4_sound_data[2] << 8)); |
| 151 |
val = snd->plus4_sound_data[3]; |
| 152 |
snd->volume = volume_tab[val & 0x0f]; |
| 153 |
snd->voice0_output_enabled = (val & 0x10) ? 1 : 0; |
| 154 |
snd->voice1_output_enabled = (val & 0x60) ? 1 : 0; |
| 155 |
snd->noise = ((val & 0x60) == 0x40) ? 1 : 0; |
| 156 |
snd->digital = val & 0x80; |
| 157 |
if (snd->digital) { |
| 158 |
snd->voice0_sign = 1; |
| 159 |
snd->voice0_accu = 0; |
| 160 |
snd->voice1_sign = 1; |
| 161 |
snd->voice1_accu = 0; |
| 162 |
snd->noise_shift_register = 0; |
| 163 |
} |
| 164 |
|
| 165 |
return 1; |
| 166 |
} |
| 167 |
|
| 168 |
void ted_sound_machine_store(struct plus4_sound_s* snd, uint16_t addr, uint8_t val) |
| 169 |
{ |
| 170 |
switch (addr) { |
| 171 |
case 0x0e: |
| 172 |
snd->plus4_sound_data[0] = val; |
| 173 |
snd->voice0_reload = (snd->plus4_sound_data[0] | (snd->plus4_sound_data[4] << 8)); |
| 174 |
break; |
| 175 |
case 0x0f: |
| 176 |
snd->plus4_sound_data[1] = val; |
| 177 |
snd->voice1_reload = (snd->plus4_sound_data[1] | (snd->plus4_sound_data[2] << 8)); |
| 178 |
break; |
| 179 |
case 0x10: |
| 180 |
snd->plus4_sound_data[2] = val & 3; |
| 181 |
snd->voice1_reload = (snd->plus4_sound_data[1] | (snd->plus4_sound_data[2] << 8)); |
| 182 |
break; |
| 183 |
case 0x11: |
| 184 |
snd->volume = volume_tab[val & 0x0f]; |
| 185 |
snd->voice0_output_enabled = (val & 0x10) ? 1 : 0; |
| 186 |
snd->voice1_output_enabled = (val & 0x60) ? 1 : 0; |
| 187 |
snd->noise = ((val & 0x60) == 0x40) ? 1 : 0; |
| 188 |
snd->digital = val & 0x80; |
| 189 |
if (snd->digital) { |
| 190 |
snd->voice0_sign = 1; |
| 191 |
snd->voice0_accu = 0; |
| 192 |
snd->voice1_sign = 1; |
| 193 |
snd->voice1_accu = 0; |
| 194 |
snd->noise_shift_register = 0; |
| 195 |
} |
| 196 |
snd->plus4_sound_data[3] = val; |
| 197 |
break; |
| 198 |
case 0x12: |
| 199 |
snd->plus4_sound_data[4] = val & 3; |
| 200 |
snd->voice0_reload = (snd->plus4_sound_data[0] | (snd->plus4_sound_data[4] << 8)); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
uint8_t ted_sound_machine_read(struct plus4_sound_s* snd, uint16_t addr) |
| 206 |
{ |
| 207 |
switch (addr) { |
| 208 |
case 0x0e: |
| 209 |
return snd->plus4_sound_data[0]; |
| 210 |
case 0x0f: |
| 211 |
return snd->plus4_sound_data[1]; |
| 212 |
case 0x10: |
| 213 |
return snd->plus4_sound_data[2] | 0xc0; |
| 214 |
case 0x11: |
| 215 |
return snd->plus4_sound_data[3]; |
| 216 |
case 0x12: |
| 217 |
return snd->plus4_sound_data[4]; |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
void ted_sound_reset(struct plus4_sound_s* snd) |
| 224 |
{ |
| 225 |
uint16_t i; |
| 226 |
|
| 227 |
snd->noise_shift_register = 0; |
| 228 |
for (i = 0x0e; i <= 0x12; i++) { |
| 229 |
ted_sound_machine_store(snd,i,0); |
| 230 |
} |
| 231 |
} |