| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: sblaster.cpp,v 1.78 2009-10-25 16:22:22 c2woody Exp $ */ |
| 20 | |
| 21 | #include <iomanip> |
| 22 | #include <sstream> |
| 23 | #include <string.h> |
| 24 | #include <math.h> |
| 25 | #include "dosbox.h" |
| 26 | #include "inout.h" |
| 27 | #include "mixer.h" |
| 28 | #include "dma.h" |
| 29 | #include "pic.h" |
| 30 | #include "hardware.h" |
| 31 | #include "setup.h" |
| 32 | #include "support.h" |
| 33 | #include "shell.h" |
| 34 | using namespace std; |
| 35 | |
| 36 | void MIDI_RawOutByte(Bit8u data); |
| 37 | bool MIDI_Available(void); |
| 38 | |
| 39 | #define SB_PIC_EVENTS 0 |
| 40 | |
| 41 | #define DSP_MAJOR 3 |
| 42 | #define DSP_MINOR 1 |
| 43 | |
| 44 | #define MIXER_INDEX 0x04 |
| 45 | #define MIXER_DATA 0x05 |
| 46 | |
| 47 | #define DSP_RESET 0x06 |
| 48 | #define DSP_READ_DATA 0x0A |
| 49 | #define DSP_WRITE_DATA 0x0C |
| 50 | #define DSP_WRITE_STATUS 0x0C |
| 51 | #define DSP_READ_STATUS 0x0E |
| 52 | #define DSP_ACK_16BIT 0x0f |
| 53 | |
| 54 | #define DSP_NO_COMMAND 0 |
| 55 | |
| 56 | #define DMA_BUFSIZE 1024 |
| 57 | #define DSP_BUFSIZE 64 |
| 58 | #define DSP_DACSIZE 512 |
| 59 | |
| 60 | //Should be enough for sound generated in millisecond blocks |
| 61 | #define SB_BUF_SIZE 8096 |
| 62 | #define SB_SH 14 |
| 63 | #define SB_SH_MASK ((1 << SB_SH)-1) |
| 64 | |
| 65 | enum {DSP_S_RESET,DSP_S_RESET_WAIT,DSP_S_NORMAL,DSP_S_HIGHSPEED}; |
| 66 | enum SB_TYPES {SBT_NONE=0,SBT_1=1,SBT_PRO1=2,SBT_2=3,SBT_PRO2=4,SBT_16=6,SBT_GB=7}; |
| 67 | enum SB_IRQS {SB_IRQ_8,SB_IRQ_16,SB_IRQ_MPU}; |
| 68 | |
| 69 | enum DSP_MODES { |
| 70 | MODE_NONE, |
| 71 | MODE_DAC, |
| 72 | MODE_DMA, |
| 73 | MODE_DMA_PAUSE, |
| 74 | MODE_DMA_MASKED |
| 75 | |
| 76 | }; |
| 77 | |
| 78 | enum DMA_MODES { |
| 79 | DSP_DMA_NONE, |
| 80 | DSP_DMA_2,DSP_DMA_3,DSP_DMA_4,DSP_DMA_8, |
| 81 | DSP_DMA_16,DSP_DMA_16_ALIASED |
| 82 | }; |
| 83 | |
| 84 | enum { |
| 85 | PLAY_MONO,PLAY_STEREO |
| 86 | }; |
| 87 | |
| 88 | struct SB_INFO { |
| 89 | Bitu freq; |
| 90 | struct { |
| 91 | bool stereo,sign,autoinit; |
| 92 | DMA_MODES mode; |
| 93 | Bitu rate,mul; |
| 94 | Bitu total,left,min; |
| 95 | Bit64u start; |
| 96 | union { |
| 97 | Bit8u b8[DMA_BUFSIZE]; |
| 98 | Bit16s b16[DMA_BUFSIZE]; |
| 99 | } buf; |
| 100 | Bitu bits; |
| 101 | DmaChannel * chan; |
| 102 | Bitu remain_size; |
| 103 | } dma; |
| 104 | bool speaker; |
| 105 | bool midi; |
| 106 | Bit8u time_constant; |
| 107 | DSP_MODES mode; |
| 108 | SB_TYPES type; |
| 109 | struct { |
| 110 | bool pending_8bit; |
| 111 | bool pending_16bit; |
| 112 | } irq; |
| 113 | struct { |
| 114 | Bit8u state; |
| 115 | Bit8u cmd; |
| 116 | Bit8u cmd_len; |
| 117 | Bit8u cmd_in_pos; |
| 118 | Bit8u cmd_in[DSP_BUFSIZE]; |
| 119 | struct { |
| 120 | Bit8u lastval; |
| 121 | Bit8u data[DSP_BUFSIZE]; |
| 122 | Bitu pos,used; |
| 123 | } in,out; |
| 124 | Bit8u test_register; |
| 125 | Bitu write_busy; |
| 126 | } dsp; |
| 127 | struct { |
| 128 | Bit16s data[DSP_DACSIZE+1]; |
| 129 | Bitu used; |
| 130 | Bit16s last; |
| 131 | } dac; |
| 132 | struct { |
| 133 | Bit8u index; |
| 134 | Bit8u dac[2],fm[2],cda[2],master[2],lin[2]; |
| 135 | Bit8u mic; |
| 136 | bool stereo; |
| 137 | bool enabled; |
| 138 | bool filtered; |
| 139 | Bit8u unhandled[0x48]; |
| 140 | } mixer; |
| 141 | struct { |
| 142 | Bit8u reference; |
| 143 | Bits stepsize; |
| 144 | bool haveref; |
| 145 | } adpcm; |
| 146 | struct { |
| 147 | Bitu base; |
| 148 | Bitu irq; |
| 149 | Bit8u dma8,dma16; |
| 150 | } hw; |
| 151 | struct { |
| 152 | Bits value; |
| 153 | Bitu count; |
| 154 | } e2; |
| 155 | MixerChannel * chan; |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | |
| 160 | static SB_INFO sb; |
| 161 | |
| 162 | static char const * const copyright_string="COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992."; |
| 163 | |
| 164 | // number of bytes in input for commands (sb/sbpro) |
| 165 | static Bit8u DSP_cmd_len_sb[256] = { |
| 166 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x00 |
| 167 | // 1,0,0,0, 2,0,2,2, 0,0,0,0, 0,0,0,0, // 0x10 |
| 168 | 1,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x10 Wari hack |
| 169 | 0,0,0,0, 2,0,0,0, 0,0,0,0, 0,0,0,0, // 0x20 |
| 170 | 0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, // 0x30 |
| 171 | |
| 172 | 1,2,2,0, 0,0,0,0, 2,0,0,0, 0,0,0,0, // 0x40 |
| 173 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x50 |
| 174 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x60 |
| 175 | 0,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x70 |
| 176 | |
| 177 | 2,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x80 |
| 178 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x90 |
| 179 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xa0 |
| 180 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xb0 |
| 181 | |
| 182 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xc0 |
| 183 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xd0 |
| 184 | 1,0,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, // 0xe0 |
| 185 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 // 0xf0 |
| 186 | }; |
| 187 | |
| 188 | // number of bytes in input for commands (sb16) |
| 189 | static Bit8u DSP_cmd_len_sb16[256] = { |
| 190 | 0,0,0,0, 1,2,0,0, 1,0,0,0, 0,0,2,1, // 0x00 |
| 191 | // 1,0,0,0, 2,0,2,2, 0,0,0,0, 0,0,0,0, // 0x10 |
| 192 | 1,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x10 Wari hack |
| 193 | 0,0,0,0, 2,0,0,0, 0,0,0,0, 0,0,0,0, // 0x20 |
| 194 | 0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, // 0x30 |
| 195 | |
| 196 | 1,2,2,0, 0,0,0,0, 2,0,0,0, 0,0,0,0, // 0x40 |
| 197 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x50 |
| 198 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x60 |
| 199 | 0,0,0,0, 2,2,2,2, 0,0,0,0, 0,0,0,0, // 0x70 |
| 200 | |
| 201 | 2,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x80 |
| 202 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x90 |
| 203 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xa0 |
| 204 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xb0 |
| 205 | |
| 206 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xc0 |
| 207 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0xd0 |
| 208 | 1,0,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, // 0xe0 |
| 209 | 0,0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0 // 0xf0 |
| 210 | }; |
| 211 | |
| 212 | static Bit8u ASP_regs[256]; |
| 213 | static bool ASP_init_in_progress = false; |
| 214 | |
| 215 | static int E2_incr_table[4][9] = { |
| 216 | { 0x01, -0x02, -0x04, 0x08, -0x10, 0x20, 0x40, -0x80, -106 }, |
| 217 | { -0x01, 0x02, -0x04, 0x08, 0x10, -0x20, 0x40, -0x80, 165 }, |
| 218 | { -0x01, 0x02, 0x04, -0x08, 0x10, -0x20, -0x40, 0x80, -151 }, |
| 219 | { 0x01, -0x02, 0x04, -0x08, -0x10, 0x20, -0x40, 0x80, 90 } |
| 220 | }; |
| 221 | |
| 222 | #ifndef max |
| 223 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 224 | #endif |
| 225 | #ifndef min |
| 226 | #define min(a,b) ((a)<(b)?(a):(b)) |
| 227 | #endif |
| 228 | |
| 229 | static void DSP_ChangeMode(DSP_MODES mode); |
| 230 | static void CheckDMAEnd(); |
| 231 | static void END_DMA_Event(Bitu); |
| 232 | static void DMA_Silent_Event(Bitu val); |
| 233 | static void GenerateDMASound(Bitu size); |
| 234 | |
| 235 | static void DSP_SetSpeaker(bool how) { |
| 236 | if (sb.speaker==how) return; |
| 237 | sb.speaker=how; |
| 238 | if (sb.type==SBT_16) return; |
| 239 | sb.chan->Enable(how); |
| 240 | if (sb.speaker) { |
| 241 | PIC_RemoveEvents(DMA_Silent_Event); |
| 242 | CheckDMAEnd(); |
| 243 | } else { |
| 244 | |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static INLINE void SB_RaiseIRQ(SB_IRQS type) { |
| 249 | LOG(LOG_SB,LOG_NORMAL)("Raising IRQ"); |
| 250 | switch (type) { |
| 251 | case SB_IRQ_8: |
| 252 | if (sb.irq.pending_8bit) { |
| 253 | // LOG_MSG("SB: 8bit irq pending"); |
| 254 | return; |
| 255 | } |
| 256 | sb.irq.pending_8bit=true; |
| 257 | PIC_ActivateIRQ(sb.hw.irq); |
| 258 | break; |
| 259 | case SB_IRQ_16: |
| 260 | if (sb.irq.pending_16bit) { |
| 261 | // LOG_MSG("SB: 16bit irq pending"); |
| 262 | return; |
| 263 | } |
| 264 | sb.irq.pending_16bit=true; |
| 265 | PIC_ActivateIRQ(sb.hw.irq); |
| 266 | break; |
| 267 | default: |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static INLINE void DSP_FlushData(void) { |
| 273 | sb.dsp.out.used=0; |
| 274 | sb.dsp.out.pos=0; |
| 275 | } |
| 276 | |
| 277 | static void DSP_DMA_CallBack(DmaChannel * chan, DMAEvent event) { |
| 278 | if (event==DMA_REACHED_TC) return; |
| 279 | else if (event==DMA_MASKED) { |
| 280 | if (sb.mode==MODE_DMA) { |
| 281 | GenerateDMASound(sb.dma.min); |
| 282 | sb.mode=MODE_DMA_MASKED; |
| 283 | // DSP_ChangeMode(MODE_DMA_MASKED); |
| 284 | LOG(LOG_SB,LOG_NORMAL)("DMA masked,stopping output, left %d",chan->currcnt); |
| 285 | } |
| 286 | } else if (event==DMA_UNMASKED) { |
| 287 | if (sb.mode==MODE_DMA_MASKED && sb.dma.mode!=DSP_DMA_NONE) { |
| 288 | DSP_ChangeMode(MODE_DMA); |
| 289 | // sb.mode=MODE_DMA; |
| 290 | CheckDMAEnd(); |
| 291 | LOG(LOG_SB,LOG_NORMAL)("DMA unmasked,starting output, auto %d block %d",chan->autoinit,chan->basecnt); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | #define MIN_ADAPTIVE_STEP_SIZE 0 |
| 297 | #define MAX_ADAPTIVE_STEP_SIZE 32767 |
| 298 | #define DC_OFFSET_FADE 254 |
| 299 | |
| 300 | static INLINE Bit8u decode_ADPCM_4_sample(Bit8u sample,Bit8u & reference,Bits& scale) { |
| 301 | static const Bit8s scaleMap[64] = { |
| 302 | 0, 1, 2, 3, 4, 5, 6, 7, 0, -1, -2, -3, -4, -5, -6, -7, |
| 303 | 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15, |
| 304 | 2, 6, 10, 14, 18, 22, 26, 30, -2, -6, -10, -14, -18, -22, -26, -30, |
| 305 | 4, 12, 20, 28, 36, 44, 52, 60, -4, -12, -20, -28, -36, -44, -52, -60 |
| 306 | }; |
| 307 | static const Bit8u adjustMap[64] = { |
| 308 | 0, 0, 0, 0, 0, 16, 16, 16, |
| 309 | 0, 0, 0, 0, 0, 16, 16, 16, |
| 310 | 240, 0, 0, 0, 0, 16, 16, 16, |
| 311 | 240, 0, 0, 0, 0, 16, 16, 16, |
| 312 | 240, 0, 0, 0, 0, 16, 16, 16, |
| 313 | 240, 0, 0, 0, 0, 16, 16, 16, |
| 314 | 240, 0, 0, 0, 0, 0, 0, 0, |
| 315 | 240, 0, 0, 0, 0, 0, 0, 0 |
| 316 | }; |
| 317 | |
| 318 | Bits samp = sample + scale; |
| 319 | |
| 320 | if ((samp < 0) || (samp > 63)) { |
| 321 | LOG(LOG_SB,LOG_ERROR)("Bad ADPCM-4 sample"); |
| 322 | if(samp < 0 ) samp = 0; |
| 323 | if(samp > 63) samp = 63; |
| 324 | } |
| 325 | |
| 326 | Bits ref = reference + scaleMap[samp]; |
| 327 | if (ref > 0xff) reference = 0xff; |
| 328 | else if (ref < 0x00) reference = 0x00; |
| 329 | else reference = (Bit8u)(ref&0xff); |
| 330 | scale = (scale + adjustMap[samp]) & 0xff; |
| 331 | |
| 332 | return reference; |
| 333 | } |
| 334 | |
| 335 | static INLINE Bit8u decode_ADPCM_2_sample(Bit8u sample,Bit8u & reference,Bits& scale) { |
| 336 | static const Bit8s scaleMap[24] = { |
| 337 | 0, 1, 0, -1, 1, 3, -1, -3, |
| 338 | 2, 6, -2, -6, 4, 12, -4, -12, |
| 339 | 8, 24, -8, -24, 6, 48, -16, -48 |
| 340 | }; |
| 341 | static const Bit8u adjustMap[24] = { |
| 342 | 0, 4, 0, 4, |
| 343 | 252, 4, 252, 4, 252, 4, 252, 4, |
| 344 | 252, 4, 252, 4, 252, 4, 252, 4, |
| 345 | 252, 0, 252, 0 |
| 346 | }; |
| 347 | |
| 348 | Bits samp = sample + scale; |
| 349 | if ((samp < 0) || (samp > 23)) { |
| 350 | LOG(LOG_SB,LOG_ERROR)("Bad ADPCM-2 sample"); |
| 351 | if(samp < 0 ) samp = 0; |
| 352 | if(samp > 23) samp = 23; |
| 353 | } |
| 354 | |
| 355 | Bits ref = reference + scaleMap[samp]; |
| 356 | if (ref > 0xff) reference = 0xff; |
| 357 | else if (ref < 0x00) reference = 0x00; |
| 358 | else reference = (Bit8u)(ref&0xff); |
| 359 | scale = (scale + adjustMap[samp]) & 0xff; |
| 360 | |
| 361 | return reference; |
| 362 | } |
| 363 | |
| 364 | INLINE Bit8u decode_ADPCM_3_sample(Bit8u sample,Bit8u & reference,Bits& scale) { |
| 365 | static const Bit8s scaleMap[40] = { |
| 366 | 0, 1, 2, 3, 0, -1, -2, -3, |
| 367 | 1, 3, 5, 7, -1, -3, -5, -7, |
| 368 | 2, 6, 10, 14, -2, -6, -10, -14, |
| 369 | 4, 12, 20, 28, -4, -12, -20, -28, |
| 370 | 5, 15, 25, 35, -5, -15, -25, -35 |
| 371 | }; |
| 372 | static const Bit8u adjustMap[40] = { |
| 373 | 0, 0, 0, 8, 0, 0, 0, 8, |
| 374 | 248, 0, 0, 8, 248, 0, 0, 8, |
| 375 | 248, 0, 0, 8, 248, 0, 0, 8, |
| 376 | 248, 0, 0, 8, 248, 0, 0, 8, |
| 377 | 248, 0, 0, 0, 248, 0, 0, 0 |
| 378 | }; |
| 379 | |
| 380 | Bits samp = sample + scale; |
| 381 | if ((samp < 0) || (samp > 39)) { |
| 382 | LOG(LOG_SB,LOG_ERROR)("Bad ADPCM-3 sample"); |
| 383 | if(samp < 0 ) samp = 0; |
| 384 | if(samp > 39) samp = 39; |
| 385 | } |
| 386 | |
| 387 | Bits ref = reference + scaleMap[samp]; |
| 388 | if (ref > 0xff) reference = 0xff; |
| 389 | else if (ref < 0x00) reference = 0x00; |
| 390 | else reference = (Bit8u)(ref&0xff); |
| 391 | scale = (scale + adjustMap[samp]) & 0xff; |
| 392 | |
| 393 | return reference; |
| 394 | } |
| 395 | |
| 396 | static void GenerateDMASound(Bitu size) { |
| 397 | Bitu read=0;Bitu done=0;Bitu i=0; |
| 398 | |
| 399 | if(sb.dma.autoinit) { |
| 400 | if (sb.dma.left <= size) size = sb.dma.left; |
| 401 | } else if (sb.dma.left <= sb.dma.min) size = sb.dma.left; |
| 402 | |
| 403 | switch (sb.dma.mode) { |
| 404 | case DSP_DMA_2: |
| 405 | read=sb.dma.chan->Read(size,sb.dma.buf.b8); |
| 406 | if (read && sb.adpcm.haveref) { |
| 407 | sb.adpcm.haveref=false; |
| 408 | sb.adpcm.reference=sb.dma.buf.b8[0]; |
| 409 | sb.adpcm.stepsize=MIN_ADAPTIVE_STEP_SIZE; |
| 410 | i++; |
| 411 | } |
| 412 | for (;i<read;i++) { |
| 413 | MixTemp[done++]=decode_ADPCM_2_sample((sb.dma.buf.b8[i] >> 6) & 0x3,sb.adpcm.reference,sb.adpcm.stepsize); |
| 414 | MixTemp[done++]=decode_ADPCM_2_sample((sb.dma.buf.b8[i] >> 4) & 0x3,sb.adpcm.reference,sb.adpcm.stepsize); |
| 415 | MixTemp[done++]=decode_ADPCM_2_sample((sb.dma.buf.b8[i] >> 2) & 0x3,sb.adpcm.reference,sb.adpcm.stepsize); |
| 416 | MixTemp[done++]=decode_ADPCM_2_sample((sb.dma.buf.b8[i] >> 0) & 0x3,sb.adpcm.reference,sb.adpcm.stepsize); |
| 417 | } |
| 418 | sb.chan->AddSamples_m8(done,MixTemp); |
| 419 | break; |
| 420 | case DSP_DMA_3: |
| 421 | read=sb.dma.chan->Read(size,sb.dma.buf.b8); |
| 422 | if (read && sb.adpcm.haveref) { |
| 423 | sb.adpcm.haveref=false; |
| 424 | sb.adpcm.reference=sb.dma.buf.b8[0]; |
| 425 | sb.adpcm.stepsize=MIN_ADAPTIVE_STEP_SIZE; |
| 426 | i++; |
| 427 | } |
| 428 | for (;i<read;i++) { |
| 429 | MixTemp[done++]=decode_ADPCM_3_sample((sb.dma.buf.b8[i] >> 5) & 0x7,sb.adpcm.reference,sb.adpcm.stepsize); |
| 430 | MixTemp[done++]=decode_ADPCM_3_sample((sb.dma.buf.b8[i] >> 2) & 0x7,sb.adpcm.reference,sb.adpcm.stepsize); |
| 431 | MixTemp[done++]=decode_ADPCM_3_sample((sb.dma.buf.b8[i] & 0x3) << 1,sb.adpcm.reference,sb.adpcm.stepsize); |
| 432 | } |
| 433 | sb.chan->AddSamples_m8(done,MixTemp); |
| 434 | break; |
| 435 | case DSP_DMA_4: |
| 436 | read=sb.dma.chan->Read(size,sb.dma.buf.b8); |
| 437 | if (read && sb.adpcm.haveref) { |
| 438 | sb.adpcm.haveref=false; |
| 439 | sb.adpcm.reference=sb.dma.buf.b8[0]; |
| 440 | sb.adpcm.stepsize=MIN_ADAPTIVE_STEP_SIZE; |
| 441 | i++; |
| 442 | } |
| 443 | for (;i<read;i++) { |
| 444 | MixTemp[done++]=decode_ADPCM_4_sample(sb.dma.buf.b8[i] >> 4,sb.adpcm.reference,sb.adpcm.stepsize); |
| 445 | MixTemp[done++]=decode_ADPCM_4_sample(sb.dma.buf.b8[i]& 0xf,sb.adpcm.reference,sb.adpcm.stepsize); |
| 446 | } |
| 447 | sb.chan->AddSamples_m8(done,MixTemp); |
| 448 | break; |
| 449 | case DSP_DMA_8: |
| 450 | if (sb.dma.stereo) { |
| 451 | read=sb.dma.chan->Read(size,&sb.dma.buf.b8[sb.dma.remain_size]); |
| 452 | Bitu total=read+sb.dma.remain_size; |
| 453 | if (!sb.dma.sign) sb.chan->AddSamples_s8(total>>1,sb.dma.buf.b8); |
| 454 | else sb.chan->AddSamples_s8s(total>>1,(Bit8s*)sb.dma.buf.b8); |
| 455 | if (total&1) { |
| 456 | sb.dma.remain_size=1; |
| 457 | sb.dma.buf.b8[0]=sb.dma.buf.b8[total-1]; |
| 458 | } else sb.dma.remain_size=0; |
| 459 | } else { |
| 460 | read=sb.dma.chan->Read(size,sb.dma.buf.b8); |
| 461 | if (!sb.dma.sign) sb.chan->AddSamples_m8(read,sb.dma.buf.b8); |
| 462 | else sb.chan->AddSamples_m8s(read,(Bit8s *)sb.dma.buf.b8); |
| 463 | } |
| 464 | break; |
| 465 | case DSP_DMA_16: |
| 466 | case DSP_DMA_16_ALIASED: |
| 467 | if (sb.dma.stereo) { |
| 468 | /* In DSP_DMA_16_ALIASED mode temporarily divide by 2 to get number of 16-bit |
| 469 | samples, because 8-bit DMA Read returns byte size, while in DSP_DMA_16 mode |
| 470 | 16-bit DMA Read returns word size */ |
| 471 | read=sb.dma.chan->Read(size,(Bit8u *)&sb.dma.buf.b16[sb.dma.remain_size]) |
| 472 | >> (sb.dma.mode==DSP_DMA_16_ALIASED ? 1:0); |
| 473 | Bitu total=read+sb.dma.remain_size; |
| 474 | #if defined(WORDS_BIGENDIAN) |
| 475 | if (sb.dma.sign) sb.chan->AddSamples_s16_nonnative(total>>1,sb.dma.buf.b16); |
| 476 | else sb.chan->AddSamples_s16u_nonnative(total>>1,(Bit16u *)sb.dma.buf.b16); |
| 477 | #else |
| 478 | if (sb.dma.sign) sb.chan->AddSamples_s16(total>>1,sb.dma.buf.b16); |
| 479 | else sb.chan->AddSamples_s16u(total>>1,(Bit16u *)sb.dma.buf.b16); |
| 480 | #endif |
| 481 | if (total&1) { |
| 482 | sb.dma.remain_size=1; |
| 483 | sb.dma.buf.b16[0]=sb.dma.buf.b16[total-1]; |
| 484 | } else sb.dma.remain_size=0; |
| 485 | } else { |
| 486 | read=sb.dma.chan->Read(size,(Bit8u *)sb.dma.buf.b16) |
| 487 | >> (sb.dma.mode==DSP_DMA_16_ALIASED ? 1:0); |
| 488 | #if defined(WORDS_BIGENDIAN) |
| 489 | if (sb.dma.sign) sb.chan->AddSamples_m16_nonnative(read,sb.dma.buf.b16); |
| 490 | else sb.chan->AddSamples_m16u_nonnative(read,(Bit16u *)sb.dma.buf.b16); |
| 491 | #else |
| 492 | if (sb.dma.sign) sb.chan->AddSamples_m16(read,sb.dma.buf.b16); |
| 493 | else sb.chan->AddSamples_m16u(read,(Bit16u *)sb.dma.buf.b16); |
| 494 | #endif |
| 495 | } |
| 496 | //restore buffer length value to byte size in aliased mode |
| 497 | if (sb.dma.mode==DSP_DMA_16_ALIASED) read=read<<1; |
| 498 | break; |
| 499 | default: |
| 500 | LOG_MSG("Unhandled dma mode %d",sb.dma.mode); |
| 501 | sb.mode=MODE_NONE; |
| 502 | return; |
| 503 | } |
| 504 | sb.dma.left-=read; |
| 505 | if (!sb.dma.left) { |
| 506 | PIC_RemoveEvents(END_DMA_Event); |
| 507 | if (!sb.dma.autoinit) { |
| 508 | LOG(LOG_SB,LOG_NORMAL)("Single cycle transfer ended"); |
| 509 | sb.mode=MODE_NONE; |
| 510 | sb.dma.mode=DSP_DMA_NONE; |
| 511 | } else { |
| 512 | sb.dma.left=sb.dma.total; |
| 513 | if (!sb.dma.left) { |
| 514 | LOG(LOG_SB,LOG_NORMAL)("Auto-init transfer with 0 size"); |
| 515 | sb.mode=MODE_NONE; |
| 516 | } |
| 517 | } |
| 518 | if (sb.dma.mode >= DSP_DMA_16) SB_RaiseIRQ(SB_IRQ_16); |
| 519 | else SB_RaiseIRQ(SB_IRQ_8); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* old version... |
| 524 | static void GenerateDACSound(Bitu len) { |
| 525 | if (!sb.dac.used) { |
| 526 | sb.mode=MODE_NONE; |
| 527 | return; |
| 528 | } |
| 529 | Bitu dac_add=(sb.dac.used<<16)/len; |
| 530 | Bitu dac_pos=0; |
| 531 | Bit16s * out=(Bit16s *)MixTemp; |
| 532 | for (Bitu i=len;i;i--) { |
| 533 | *out++=sb.dac.data[0+(dac_pos>>16)]; |
| 534 | dac_pos+=dac_add; |
| 535 | } |
| 536 | sb.dac.used=0; |
| 537 | sb.chan->AddSamples_m16(len,(Bit16s *)MixTemp); |
| 538 | } |
| 539 | */ |
| 540 | |
| 541 | static void DMA_Silent_Event(Bitu val) { |
| 542 | if (sb.dma.left<val) val=sb.dma.left; |
| 543 | Bitu read=sb.dma.chan->Read(val,sb.dma.buf.b8); |
| 544 | sb.dma.left-=read; |
| 545 | if (!sb.dma.left) { |
| 546 | if (sb.dma.mode >= DSP_DMA_16) SB_RaiseIRQ(SB_IRQ_16); |
| 547 | else SB_RaiseIRQ(SB_IRQ_8); |
| 548 | if (sb.dma.autoinit) sb.dma.left=sb.dma.total; |
| 549 | else { |
| 550 | sb.mode=MODE_NONE; |
| 551 | sb.dma.mode=DSP_DMA_NONE; |
| 552 | } |
| 553 | } |
| 554 | if (sb.dma.left) { |
| 555 | Bitu bigger=(sb.dma.left > sb.dma.min) ? sb.dma.min : sb.dma.left; |
| 556 | float delay=(bigger*1000.0f)/sb.dma.rate; |
| 557 | PIC_AddEvent(DMA_Silent_Event,delay,bigger); |
| 558 | } |
| 559 | |
| 560 | } |
| 561 | |
| 562 | static void END_DMA_Event(Bitu val) { |
| 563 | GenerateDMASound(val); |
| 564 | } |
| 565 | |
| 566 | static void CheckDMAEnd(void) { |
| 567 | if (!sb.dma.left) return; |
| 568 | if (!sb.speaker && sb.type!=SBT_16) { |
| 569 | Bitu bigger=(sb.dma.left > sb.dma.min) ? sb.dma.min : sb.dma.left; |
| 570 | float delay=(bigger*1000.0f)/sb.dma.rate; |
| 571 | PIC_AddEvent(DMA_Silent_Event,delay,bigger); |
| 572 | LOG(LOG_SB,LOG_NORMAL)("Silent DMA Transfer scheduling IRQ in %.3f milliseconds",delay); |
| 573 | } else if (sb.dma.left<sb.dma.min) { |
| 574 | float delay=(sb.dma.left*1000.0f)/sb.dma.rate; |
| 575 | LOG(LOG_SB,LOG_NORMAL)("Short transfer scheduling IRQ in %.3f milliseconds",delay); |
| 576 | PIC_AddEvent(END_DMA_Event,delay,sb.dma.left); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void DSP_ChangeMode(DSP_MODES mode) { |
| 581 | if (sb.mode==mode) return; |
| 582 | else sb.chan->FillUp(); |
| 583 | sb.mode=mode; |
| 584 | } |
| 585 | |
| 586 | static void DSP_RaiseIRQEvent(Bitu /*val*/) { |
| 587 | SB_RaiseIRQ(SB_IRQ_8); |
| 588 | } |
| 589 | |
| 590 | static void DSP_DoDMATransfer(DMA_MODES mode,Bitu freq,bool stereo) { |
| 591 | char const * type; |
| 592 | sb.mode=MODE_DMA_MASKED; |
| 593 | sb.chan->FillUp(); |
| 594 | sb.dma.left=sb.dma.total; |
| 595 | sb.dma.mode=mode; |
| 596 | sb.dma.stereo=stereo; |
| 597 | sb.irq.pending_8bit=false; |
| 598 | sb.irq.pending_16bit=false; |
| 599 | switch (mode) { |
| 600 | case DSP_DMA_2: |
| 601 | type="2-bits ADPCM"; |
| 602 | sb.dma.mul=(1 << SB_SH)/4; |
| 603 | break; |
| 604 | case DSP_DMA_3: |
| 605 | type="3-bits ADPCM"; |
| 606 | sb.dma.mul=(1 << SB_SH)/3; |
| 607 | break; |
| 608 | case DSP_DMA_4: |
| 609 | type="4-bits ADPCM"; |
| 610 | sb.dma.mul=(1 << SB_SH)/2; |
| 611 | break; |
| 612 | case DSP_DMA_8: |
| 613 | type="8-bits PCM"; |
| 614 | sb.dma.mul=(1 << SB_SH); |
| 615 | break; |
| 616 | case DSP_DMA_16_ALIASED: |
| 617 | type="16-bits(aliased) PCM"; |
| 618 | sb.dma.mul=(1 << SB_SH)*2; |
| 619 | break; |
| 620 | case DSP_DMA_16: |
| 621 | type="16-bits PCM"; |
| 622 | sb.dma.mul=(1 << SB_SH); |
| 623 | break; |
| 624 | default: |
| 625 | LOG(LOG_SB,LOG_ERROR)("DSP:Illegal transfer mode %d",mode); |
| 626 | return; |
| 627 | } |
| 628 | if (sb.dma.stereo) sb.dma.mul*=2; |
| 629 | sb.dma.rate=(sb.freq*sb.dma.mul) >> SB_SH; |
| 630 | sb.dma.min=(sb.dma.rate*3)/1000; |
| 631 | sb.chan->SetFreq(freq); |
| 632 | sb.dma.mode=mode; |
| 633 | PIC_RemoveEvents(END_DMA_Event); |
| 634 | sb.dma.chan->Register_Callback(DSP_DMA_CallBack); |
| 635 | #if (C_DEBUG) |
| 636 | LOG(LOG_SB,LOG_NORMAL)("DMA Transfer:%s %s %s freq %d rate %d size %d", |
| 637 | type, |
| 638 | sb.dma.stereo ? "Stereo" : "Mono", |
| 639 | sb.dma.autoinit ? "Auto-Init" : "Single-Cycle", |
| 640 | freq,sb.dma.rate,sb.dma.total |
| 641 | ); |
| 642 | #endif |
| 643 | } |
| 644 | |
| 645 | static void DSP_PrepareDMA_Old(DMA_MODES mode,bool autoinit,bool sign) { |
| 646 | sb.dma.autoinit=autoinit; |
| 647 | sb.dma.sign=sign; |
| 648 | if (!autoinit) sb.dma.total=1+sb.dsp.in.data[0]+(sb.dsp.in.data[1] << 8); |
| 649 | sb.dma.chan=GetDMAChannel(sb.hw.dma8); |
| 650 | DSP_DoDMATransfer(mode,sb.freq / (sb.mixer.stereo ? 2 : 1),sb.mixer.stereo); |
| 651 | } |
| 652 | |
| 653 | static void DSP_PrepareDMA_New(DMA_MODES mode,Bitu length,bool autoinit,bool stereo) { |
| 654 | Bitu freq=sb.freq; |
| 655 | //equal length if data format and dma channel are both 16-bit or 8-bit |
| 656 | sb.dma.total=length; |
| 657 | sb.dma.autoinit=autoinit; |
| 658 | if (mode==DSP_DMA_16) { |
| 659 | if (sb.hw.dma16!=0xff) { |
| 660 | sb.dma.chan=GetDMAChannel(sb.hw.dma16); |
| 661 | if (sb.dma.chan==NULL) { |
| 662 | sb.dma.chan=GetDMAChannel(sb.hw.dma8); |
| 663 | mode=DSP_DMA_16_ALIASED; |
| 664 | sb.dma.total<<=1; |
| 665 | } |
| 666 | } else { |
| 667 | sb.dma.chan=GetDMAChannel(sb.hw.dma8); |
| 668 | mode=DSP_DMA_16_ALIASED; |
| 669 | //UNDOCUMENTED: |
| 670 | //In aliased mode sample length is written to DSP as number of |
| 671 | //16-bit samples so we need double 8-bit DMA buffer length |
| 672 | sb.dma.total<<=1; |
| 673 | } |
| 674 | } else sb.dma.chan=GetDMAChannel(sb.hw.dma8); |
| 675 | DSP_DoDMATransfer(mode,freq,stereo); |
| 676 | } |
| 677 | |
| 678 | |
| 679 | static void DSP_AddData(Bit8u val) { |
| 680 | if (sb.dsp.out.used<DSP_BUFSIZE) { |
| 681 | Bitu start=sb.dsp.out.used+sb.dsp.out.pos; |
| 682 | if (start>=DSP_BUFSIZE) start-=DSP_BUFSIZE; |
| 683 | sb.dsp.out.data[start]=val; |
| 684 | sb.dsp.out.used++; |
| 685 | } else { |
| 686 | LOG(LOG_SB,LOG_ERROR)("DSP:Data Output buffer full"); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | |
| 691 | static void DSP_FinishReset(Bitu /*val*/) { |
| 692 | DSP_FlushData(); |
| 693 | DSP_AddData(0xaa); |
| 694 | sb.dsp.state=DSP_S_NORMAL; |
| 695 | } |
| 696 | |
| 697 | static void DSP_Reset(void) { |
| 698 | LOG(LOG_SB,LOG_ERROR)("DSP:Reset"); |
| 699 | PIC_DeActivateIRQ(sb.hw.irq); |
| 700 | |
| 701 | DSP_ChangeMode(MODE_NONE); |
| 702 | DSP_FlushData(); |
| 703 | sb.dsp.cmd_len=0; |
| 704 | sb.dsp.in.pos=0; |
| 705 | sb.dsp.write_busy=0; |
| 706 | PIC_RemoveEvents(DSP_FinishReset); |
| 707 | |
| 708 | sb.dma.left=0; |
| 709 | sb.dma.total=0; |
| 710 | sb.dma.stereo=false; |
| 711 | sb.dma.sign=false; |
| 712 | sb.dma.autoinit=false; |
| 713 | sb.dma.mode=DSP_DMA_NONE; |
| 714 | sb.dma.remain_size=0; |
| 715 | if (sb.dma.chan) sb.dma.chan->Clear_Request(); |
| 716 | |
| 717 | sb.freq=22050; |
| 718 | sb.time_constant=45; |
| 719 | sb.dac.used=0; |
| 720 | sb.dac.last=0; |
| 721 | sb.e2.value=0xaa; |
| 722 | sb.e2.count=0; |
| 723 | sb.irq.pending_8bit=false; |
| 724 | sb.irq.pending_16bit=false; |
| 725 | sb.chan->SetFreq(22050); |
| 726 | // DSP_SetSpeaker(false); |
| 727 | PIC_RemoveEvents(END_DMA_Event); |
| 728 | } |
| 729 | |
| 730 | static void DSP_DoReset(Bit8u val) { |
| 731 | if (((val&1)!=0) && (sb.dsp.state!=DSP_S_RESET)) { |
| 732 | //TODO Get out of highspeed mode |
| 733 | DSP_Reset(); |
| 734 | sb.dsp.state=DSP_S_RESET; |
| 735 | } else if (((val&1)==0) && (sb.dsp.state==DSP_S_RESET)) { // reset off |
| 736 | sb.dsp.state=DSP_S_RESET_WAIT; |
| 737 | PIC_RemoveEvents(DSP_FinishReset); |
| 738 | PIC_AddEvent(DSP_FinishReset,20.0f/1000.0f,0); // 20 microseconds |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | static void DSP_E2_DMA_CallBack(DmaChannel * /*chan*/, DMAEvent event) { |
| 743 | if (event==DMA_UNMASKED) { |
| 744 | Bit8u val=(Bit8u)(sb.e2.value&0xff); |
| 745 | DmaChannel * chan=GetDMAChannel(sb.hw.dma8); |
| 746 | chan->Register_Callback(0); |
| 747 | chan->Write(1,&val); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static void DSP_ADC_CallBack(DmaChannel * /*chan*/, DMAEvent event) { |
| 752 | if (event!=DMA_UNMASKED) return; |
| 753 | Bit8u val=128; |
| 754 | DmaChannel * ch=GetDMAChannel(sb.hw.dma8); |
| 755 | while (sb.dma.left--) { |
| 756 | ch->Write(1,&val); |
| 757 | } |
| 758 | SB_RaiseIRQ(SB_IRQ_8); |
| 759 | ch->Register_Callback(0); |
| 760 | } |
| 761 | |
| 762 | Bitu DEBUG_EnableDebugger(void); |
| 763 | |
| 764 | #define DSP_SB16_ONLY if (sb.type != SBT_16) { LOG(LOG_SB,LOG_ERROR)("DSP:Command %2X requires SB16",sb.dsp.cmd); break; } |
| 765 | #define DSP_SB2_ABOVE if (sb.type <= SBT_1) { LOG(LOG_SB,LOG_ERROR)("DSP:Command %2X requires SB2 or above",sb.dsp.cmd); break; } |
| 766 | |
| 767 | static void DSP_DoCommand(void) { |
| 768 | // LOG_MSG("DSP Command %X",sb.dsp.cmd); |
| 769 | switch (sb.dsp.cmd) { |
| 770 | case 0x04: |
| 771 | if (sb.type == SBT_16) { |
| 772 | /* SB16 ASP set mode register */ |
| 773 | if ((sb.dsp.in.data[0]&0xf1)==0xf1) ASP_init_in_progress=true; |
| 774 | else ASP_init_in_progress=false; |
| 775 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X (set mode register to %X)",sb.dsp.cmd,sb.dsp.in.data[0]); |
| 776 | } else { |
| 777 | /* DSP Status SB 2.0/pro version. NOT SB16. */ |
| 778 | DSP_FlushData(); |
| 779 | if (sb.type == SBT_2) DSP_AddData(0x88); |
| 780 | else if ((sb.type == SBT_PRO1) || (sb.type == SBT_PRO2)) DSP_AddData(0x7b); |
| 781 | else DSP_AddData(0xff); //Everything enabled |
| 782 | } |
| 783 | break; |
| 784 | case 0x05: /* SB16 ASP set codec parameter */ |
| 785 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X (set codec parameter)",sb.dsp.cmd); |
| 786 | break; |
| 787 | case 0x08: /* SB16 ASP get version */ |
| 788 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X sub %X",sb.dsp.cmd,sb.dsp.in.data[0]); |
| 789 | if (sb.type == SBT_16) { |
| 790 | switch (sb.dsp.in.data[0]) { |
| 791 | case 0x03: |
| 792 | DSP_AddData(0x18); // version ID (??) |
| 793 | break; |
| 794 | default: |
| 795 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X sub %X",sb.dsp.cmd,sb.dsp.in.data[0]); |
| 796 | break; |
| 797 | } |
| 798 | } else { |
| 799 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X sub %X",sb.dsp.cmd,sb.dsp.in.data[0]); |
| 800 | } |
| 801 | break; |
| 802 | case 0x0e: /* SB16 ASP set register */ |
| 803 | if (sb.type == SBT_16) { |
| 804 | // LOG(LOG_SB,LOG_NORMAL)("SB16 ASP set register %X := %X",sb.dsp.in.data[0],sb.dsp.in.data[1]); |
| 805 | ASP_regs[sb.dsp.in.data[0]] = sb.dsp.in.data[1]; |
| 806 | } else { |
| 807 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X (set register)",sb.dsp.cmd); |
| 808 | } |
| 809 | break; |
| 810 | case 0x0f: /* SB16 ASP get register */ |
| 811 | if (sb.type == SBT_16) { |
| 812 | if ((ASP_init_in_progress) && (sb.dsp.in.data[0]==0x83)) { |
| 813 | ASP_regs[0x83] = ~ASP_regs[0x83]; |
| 814 | } |
| 815 | // LOG(LOG_SB,LOG_NORMAL)("SB16 ASP get register %X == %X",sb.dsp.in.data[0],ASP_regs[sb.dsp.in.data[0]]); |
| 816 | DSP_AddData(ASP_regs[sb.dsp.in.data[0]]); |
| 817 | } else { |
| 818 | LOG(LOG_SB,LOG_NORMAL)("DSP Unhandled SB16ASP command %X (get register)",sb.dsp.cmd); |
| 819 | } |
| 820 | break; |
| 821 | case 0x10: /* Direct DAC */ |
| 822 | DSP_ChangeMode(MODE_DAC); |
| 823 | if (sb.dac.used<DSP_DACSIZE) { |
| 824 | sb.dac.data[sb.dac.used++]=(Bit8s(sb.dsp.in.data[0] ^ 0x80)) << 8; |
| 825 | sb.dac.data[sb.dac.used++]=(Bit8s(sb.dsp.in.data[0] ^ 0x80)) << 8; |
| 826 | } |
| 827 | break; |
| 828 | case 0x24: /* Singe Cycle 8-Bit DMA ADC */ |
| 829 | sb.dma.left=sb.dma.total=1+sb.dsp.in.data[0]+(sb.dsp.in.data[1] << 8); |
| 830 | sb.dma.sign=false; |
| 831 | LOG(LOG_SB,LOG_ERROR)("DSP:Faked ADC for %d bytes",sb.dma.total); |
| 832 | GetDMAChannel(sb.hw.dma8)->Register_Callback(DSP_ADC_CallBack); |
| 833 | break; |
| 834 | case 0x14: /* Singe Cycle 8-Bit DMA DAC */ |
| 835 | case 0x15: /* Wari hack. Waru uses this one instead of 0x14, but some weird stuff going on there anyway */ |
| 836 | case 0x91: /* Singe Cycle 8-Bit DMA High speed DAC */ |
| 837 | /* Note: 0x91 is documented only for DSP ver.2.x and 3.x, not 4.x */ |
| 838 | DSP_PrepareDMA_Old(DSP_DMA_8,false,false); |
| 839 | break; |
| 840 | case 0x90: /* Auto Init 8-bit DMA High Speed */ |
| 841 | case 0x1c: /* Auto Init 8-bit DMA */ |
| 842 | DSP_SB2_ABOVE; /* Note: 0x90 is documented only for DSP ver.2.x and 3.x, not 4.x */ |
| 843 | DSP_PrepareDMA_Old(DSP_DMA_8,true,false); |
| 844 | break; |
| 845 | case 0x38: /* Write to SB MIDI Output */ |
| 846 | if (sb.midi == true) MIDI_RawOutByte(sb.dsp.in.data[0]); |
| 847 | break; |
| 848 | case 0x40: /* Set Timeconstant */ |
| 849 | sb.freq=(1000000 / (256 - sb.dsp.in.data[0])); |
| 850 | /* Nasty kind of hack to allow runtime changing of frequency */ |
| 851 | if (sb.dma.mode != DSP_DMA_NONE && sb.dma.autoinit) { |
| 852 | DSP_PrepareDMA_Old(sb.dma.mode,sb.dma.autoinit,sb.dma.sign); |
| 853 | } |
| 854 | break; |
| 855 | case 0x41: /* Set Output Samplerate */ |
| 856 | case 0x42: /* Set Input Samplerate */ |
| 857 | DSP_SB16_ONLY; |
| 858 | sb.freq=(sb.dsp.in.data[0] << 8) | sb.dsp.in.data[1]; |
| 859 | break; |
| 860 | case 0x48: /* Set DMA Block Size */ |
| 861 | DSP_SB2_ABOVE; |
| 862 | //TODO Maybe check limit for new irq? |
| 863 | sb.dma.total=1+sb.dsp.in.data[0]+(sb.dsp.in.data[1] << 8); |
| 864 | break; |
| 865 | case 0x75: /* 075h : Single Cycle 4-bit ADPCM Reference */ |
| 866 | sb.adpcm.haveref=true; |
| 867 | case 0x74: /* 074h : Single Cycle 4-bit ADPCM */ |
| 868 | DSP_PrepareDMA_Old(DSP_DMA_4,false,false); |
| 869 | break; |
| 870 | case 0x77: /* 077h : Single Cycle 3-bit(2.6bit) ADPCM Reference*/ |
| 871 | sb.adpcm.haveref=true; |
| 872 | case 0x76: /* 074h : Single Cycle 3-bit(2.6bit) ADPCM */ |
| 873 | DSP_PrepareDMA_Old(DSP_DMA_3,false,false); |
| 874 | break; |
| 875 | case 0x17: /* 017h : Single Cycle 2-bit ADPCM Reference*/ |
| 876 | sb.adpcm.haveref=true; |
| 877 | case 0x16: /* 074h : Single Cycle 2-bit ADPCM */ |
| 878 | DSP_PrepareDMA_Old(DSP_DMA_2,false,false); |
| 879 | break; |
| 880 | case 0x80: /* Silence DAC */ |
| 881 | PIC_AddEvent(&DSP_RaiseIRQEvent, |
| 882 | (1000.0f*(1+sb.dsp.in.data[0]+(sb.dsp.in.data[1] << 8))/sb.freq)); |
| 883 | break; |
| 884 | case 0xb0: case 0xb1: case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7: |
| 885 | case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf: |
| 886 | case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4: case 0xc5: case 0xc6: case 0xc7: |
| 887 | case 0xc8: case 0xc9: case 0xca: case 0xcb: case 0xcc: case 0xcd: case 0xce: case 0xcf: |
| 888 | DSP_SB16_ONLY; |
| 889 | /* Generic 8/16 bit DMA */ |
| 890 | // DSP_SetSpeaker(true); //SB16 always has speaker enabled |
| 891 | sb.dma.sign=(sb.dsp.in.data[0] & 0x10) > 0; |
| 892 | DSP_PrepareDMA_New((sb.dsp.cmd & 0x10) ? DSP_DMA_16 : DSP_DMA_8, |
| 893 | 1+sb.dsp.in.data[1]+(sb.dsp.in.data[2] << 8), |
| 894 | (sb.dsp.cmd & 0x4)>0, |
| 895 | (sb.dsp.in.data[0] & 0x20) > 0 |
| 896 | ); |
| 897 | break; |
| 898 | case 0xd5: /* Halt 16-bit DMA */ |
| 899 | DSP_SB16_ONLY; |
| 900 | case 0xd0: /* Halt 8-bit DMA */ |
| 901 | // DSP_ChangeMode(MODE_NONE); |
| 902 | // Games sometimes already program a new dma before stopping, gives noise |
| 903 | sb.mode=MODE_DMA_PAUSE; |
| 904 | PIC_RemoveEvents(END_DMA_Event); |
| 905 | break; |
| 906 | case 0xd1: /* Enable Speaker */ |
| 907 | DSP_SetSpeaker(true); |
| 908 | break; |
| 909 | case 0xd3: /* Disable Speaker */ |
| 910 | DSP_SetSpeaker(false); |
| 911 | break; |
| 912 | case 0xd8: /* Speaker status */ |
| 913 | DSP_SB2_ABOVE; |
| 914 | DSP_FlushData(); |
| 915 | if (sb.speaker) DSP_AddData(0xff); |
| 916 | else DSP_AddData(0x00); |
| 917 | break; |
| 918 | case 0xd6: /* Continue DMA 16-bit */ |
| 919 | DSP_SB16_ONLY; |
| 920 | case 0xd4: /* Continue DMA 8-bit*/ |
| 921 | if (sb.mode==MODE_DMA_PAUSE) { |
| 922 | sb.mode=MODE_DMA_MASKED; |
| 923 | sb.dma.chan->Register_Callback(DSP_DMA_CallBack); |
| 924 | } |
| 925 | break; |
| 926 | case 0xd9: /* Exit Autoinitialize 16-bit */ |
| 927 | DSP_SB16_ONLY; |
| 928 | case 0xda: /* Exit Autoinitialize 8-bit */ |
| 929 | DSP_SB2_ABOVE; |
| 930 | /* Set mode to single transfer so it ends with current block */ |
| 931 | sb.dma.autoinit=false; //Should stop itself |
| 932 | break; |
| 933 | case 0xe0: /* DSP Identification - SB2.0+ */ |
| 934 | DSP_FlushData(); |
| 935 | DSP_AddData(~sb.dsp.in.data[0]); |
| 936 | break; |
| 937 | case 0xe1: /* Get DSP Version */ |
| 938 | DSP_FlushData(); |
| 939 | switch (sb.type) { |
| 940 | case SBT_1: |
| 941 | DSP_AddData(0x1);DSP_AddData(0x05);break; |
| 942 | case SBT_2: |
| 943 | DSP_AddData(0x2);DSP_AddData(0x1);break; |
| 944 | case SBT_PRO1: |
| 945 | DSP_AddData(0x3);DSP_AddData(0x0);break; |
| 946 | case SBT_PRO2: |
| 947 | DSP_AddData(0x3);DSP_AddData(0x2);break; |
| 948 | case SBT_16: |
| 949 | DSP_AddData(0x4);DSP_AddData(0x5);break; |
| 950 | default: |
| 951 | break; |
| 952 | } |
| 953 | break; |
| 954 | case 0xe2: /* Weird DMA identification write routine */ |
| 955 | { |
| 956 | LOG(LOG_SB,LOG_NORMAL)("DSP Function 0xe2"); |
| 957 | for (Bitu i = 0; i < 8; i++) |
| 958 | if ((sb.dsp.in.data[0] >> i) & 0x01) sb.e2.value += E2_incr_table[sb.e2.count % 4][i]; |
| 959 | sb.e2.value += E2_incr_table[sb.e2.count % 4][8]; |
| 960 | sb.e2.count++; |
| 961 | GetDMAChannel(sb.hw.dma8)->Register_Callback(DSP_E2_DMA_CallBack); |
| 962 | } |
| 963 | break; |
| 964 | case 0xe3: /* DSP Copyright */ |
| 965 | { |
| 966 | DSP_FlushData(); |
| 967 | for (size_t i=0;i<=strlen(copyright_string);i++) { |
| 968 | DSP_AddData(copyright_string[i]); |
| 969 | } |
| 970 | } |
| 971 | break; |
| 972 | case 0xe4: /* Write Test Register */ |
| 973 | sb.dsp.test_register=sb.dsp.in.data[0]; |
| 974 | break; |
| 975 | case 0xe8: /* Read Test Register */ |
| 976 | DSP_FlushData(); |
| 977 | DSP_AddData(sb.dsp.test_register);; |
| 978 | break; |
| 979 | case 0xf2: /* Trigger 8bit IRQ */ |
| 980 | SB_RaiseIRQ(SB_IRQ_8); |
| 981 | break; |
| 982 | case 0xf8: /* Undocumented, pre-SB16 only */ |
| 983 | DSP_FlushData(); |
| 984 | DSP_AddData(0); |
| 985 | break; |
| 986 | case 0x30: case 0x31: |
| 987 | LOG(LOG_SB,LOG_ERROR)("DSP:Unimplemented MIDI I/O command %2X",sb.dsp.cmd); |
| 988 | break; |
| 989 | case 0x34: case 0x35: case 0x36: case 0x37: |
| 990 | DSP_SB2_ABOVE; |
| 991 | LOG(LOG_SB,LOG_ERROR)("DSP:Unimplemented MIDI UART command %2X",sb.dsp.cmd); |
| 992 | break; |
| 993 | case 0x7d: case 0x7f: case 0x1f: |
| 994 | DSP_SB2_ABOVE; |
| 995 | LOG(LOG_SB,LOG_ERROR)("DSP:Unimplemented auto-init DMA ADPCM command %2X",sb.dsp.cmd); |
| 996 | break; |
| 997 | case 0x20: |
| 998 | DSP_AddData(0x7f); // fake silent input for Creative parrot |
| 999 | break; |
| 1000 | case 0x2c: |
| 1001 | case 0x98: case 0x99: /* Documented only for DSP 2.x and 3.x */ |
| 1002 | case 0xa0: case 0xa8: /* Documented only for DSP 3.x */ |
| 1003 | LOG(LOG_SB,LOG_ERROR)("DSP:Unimplemented input command %2X",sb.dsp.cmd); |
| 1004 | break; |
| 1005 | case 0xf9: /* SB16 ASP ??? */ |
| 1006 | if (sb.type == SBT_16) { |
| 1007 | LOG(LOG_SB,LOG_NORMAL)("SB16 ASP unknown function %x",sb.dsp.in.data[0]); |
| 1008 | // just feed it what it expects |
| 1009 | switch (sb.dsp.in.data[0]) { |
| 1010 | case 0x0b: |
| 1011 | DSP_AddData(0x00); |
| 1012 | break; |
| 1013 | case 0x0e: |
| 1014 | DSP_AddData(0xff); |
| 1015 | break; |
| 1016 | case 0x0f: |
| 1017 | DSP_AddData(0x07); |
| 1018 | break; |
| 1019 | case 0x23: |
| 1020 | DSP_AddData(0x00); |
| 1021 | break; |
| 1022 | case 0x24: |
| 1023 | DSP_AddData(0x00); |
| 1024 | break; |
| 1025 | case 0x2b: |
| 1026 | DSP_AddData(0x00); |
| 1027 | break; |
| 1028 | case 0x2c: |
| 1029 | DSP_AddData(0x00); |
| 1030 | break; |
| 1031 | case 0x2d: |
| 1032 | DSP_AddData(0x00); |
| 1033 | break; |
| 1034 | case 0x37: |
| 1035 | DSP_AddData(0x38); |
| 1036 | break; |
| 1037 | default: |
| 1038 | DSP_AddData(0x00); |
| 1039 | break; |
| 1040 | } |
| 1041 | } else { |
| 1042 | LOG(LOG_SB,LOG_NORMAL)("SB16 ASP unknown function %X",sb.dsp.cmd); |
| 1043 | } |
| 1044 | break; |
| 1045 | default: |
| 1046 | LOG(LOG_SB,LOG_ERROR)("DSP:Unhandled (undocumented) command %2X",sb.dsp.cmd); |
| 1047 | break; |
| 1048 | } |
| 1049 | sb.dsp.cmd=DSP_NO_COMMAND; |
| 1050 | sb.dsp.cmd_len=0; |
| 1051 | sb.dsp.in.pos=0; |
| 1052 | } |
| 1053 | |
| 1054 | static void DSP_DoWrite(Bit8u val) { |
| 1055 | switch (sb.dsp.cmd) { |
| 1056 | case DSP_NO_COMMAND: |
| 1057 | sb.dsp.cmd=val; |
| 1058 | if (sb.type == SBT_16) sb.dsp.cmd_len=DSP_cmd_len_sb16[val]; |
| 1059 | else sb.dsp.cmd_len=DSP_cmd_len_sb[val]; |
| 1060 | sb.dsp.in.pos=0; |
| 1061 | if (!sb.dsp.cmd_len) DSP_DoCommand(); |
| 1062 | break; |
| 1063 | default: |
| 1064 | sb.dsp.in.data[sb.dsp.in.pos]=val; |
| 1065 | sb.dsp.in.pos++; |
| 1066 | if (sb.dsp.in.pos>=sb.dsp.cmd_len) DSP_DoCommand(); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | static Bit8u DSP_ReadData(void) { |
| 1071 | /* Static so it repeats the last value on succesive reads (JANGLE DEMO) */ |
| 1072 | if (sb.dsp.out.used) { |
| 1073 | sb.dsp.out.lastval=sb.dsp.out.data[sb.dsp.out.pos]; |
| 1074 | sb.dsp.out.pos++; |
| 1075 | if (sb.dsp.out.pos>=DSP_BUFSIZE) sb.dsp.out.pos-=DSP_BUFSIZE; |
| 1076 | sb.dsp.out.used--; |
| 1077 | } |
| 1078 | return sb.dsp.out.lastval; |
| 1079 | } |
| 1080 | |
| 1081 | //The soundblaster manual says 2.0 Db steps but we'll go for a bit less |
| 1082 | #define CALCVOL(_VAL) (float)pow(10.0f,((float)(31-_VAL)*-1.3f)/20) |
| 1083 | static void CTMIXER_UpdateVolumes(void) { |
| 1084 | if (!sb.mixer.enabled) return; |
| 1085 | MixerChannel * chan; |
| 1086 | //adjust to get linear master volume slider in trackers |
| 1087 | chan=MIXER_FindChannel("SB"); |
| 1088 | if (chan) chan->SetVolume(float(sb.mixer.master[0])/31.0f*CALCVOL(sb.mixer.dac[0]), |
| 1089 | float(sb.mixer.master[1])/31.0f*CALCVOL(sb.mixer.dac[1])); |
| 1090 | chan=MIXER_FindChannel("FM"); |
| 1091 | if (chan) chan->SetVolume(float(sb.mixer.master[0])/31.0f*CALCVOL(sb.mixer.fm[0]), |
| 1092 | float(sb.mixer.master[1])/31.0f*CALCVOL(sb.mixer.fm[1])); |
| 1093 | } |
| 1094 | |
| 1095 | static void CTMIXER_Reset(void) { |
| 1096 | sb.mixer.fm[0]= |
| 1097 | sb.mixer.fm[1]= |
| 1098 | sb.mixer.dac[0]= |
| 1099 | sb.mixer.dac[1]=31; |
| 1100 | sb.mixer.master[0]= |
| 1101 | sb.mixer.master[1]=31; |
| 1102 | CTMIXER_UpdateVolumes(); |
| 1103 | } |
| 1104 | |
| 1105 | #define SETPROVOL(_WHICH_,_VAL_) \ |
| 1106 | _WHICH_[0]= ((((_VAL_) & 0xf0) >> 3)|(sb.type==SBT_16 ? 1:3)); \ |
| 1107 | _WHICH_[1]= ((((_VAL_) & 0x0f) << 1)|(sb.type==SBT_16 ? 1:3)); \ |
| 1108 | |
| 1109 | #define MAKEPROVOL(_WHICH_) \ |
| 1110 | ((((_WHICH_[0] & 0x1e) << 3) | ((_WHICH_[1] & 0x1e) >> 1)) & (sb.type==SBT_16 ? 0xff:0xee)) |
| 1111 | |
| 1112 | static void DSP_ChangeStereo(bool stereo) { |
| 1113 | if (!sb.dma.stereo && stereo) { |
| 1114 | sb.chan->SetFreq(sb.freq/2); |
| 1115 | sb.dma.mul*=2; |
| 1116 | sb.dma.rate=(sb.freq*sb.dma.mul) >> SB_SH; |
| 1117 | sb.dma.min=(sb.dma.rate*3)/1000; |
| 1118 | } else if (sb.dma.stereo && !stereo) { |
| 1119 | sb.chan->SetFreq(sb.freq); |
| 1120 | sb.dma.mul/=2; |
| 1121 | sb.dma.rate=(sb.freq*sb.dma.mul) >> SB_SH; |
| 1122 | sb.dma.min=(sb.dma.rate*3)/1000; |
| 1123 | } |
| 1124 | sb.dma.stereo=stereo; |
| 1125 | } |
| 1126 | |
| 1127 | static void CTMIXER_Write(Bit8u val) { |
| 1128 | switch (sb.mixer.index) { |
| 1129 | case 0x00: /* Reset */ |
| 1130 | CTMIXER_Reset(); |
| 1131 | LOG(LOG_SB,LOG_WARN)("Mixer reset value %x",val); |
| 1132 | break; |
| 1133 | case 0x02: /* Master Volume (SB2 Only) */ |
| 1134 | SETPROVOL(sb.mixer.master,(val&0xf)|(val<<4)); |
| 1135 | CTMIXER_UpdateVolumes(); |
| 1136 | break; |
| 1137 | case 0x04: /* DAC Volume (SBPRO) */ |
| 1138 | SETPROVOL(sb.mixer.dac,val); |
| 1139 | CTMIXER_UpdateVolumes(); |
| 1140 | break; |
| 1141 | case 0x06: /* FM output selection, Somewhat obsolete with dual OPL SBpro + FM volume (SB2 Only) */ |
| 1142 | //volume controls both channels |
| 1143 | SETPROVOL(sb.mixer.fm,(val&0xf)|(val<<4)); |
| 1144 | CTMIXER_UpdateVolumes(); |
| 1145 | if(val&0x60) LOG(LOG_SB,LOG_WARN)("Turned FM one channel off. not implemented %X",val); |
| 1146 | //TODO Change FM Mode if only 1 fm channel is selected |
| 1147 | break; |
| 1148 | case 0x08: /* CDA Volume (SB2 Only) */ |
| 1149 | SETPROVOL(sb.mixer.cda,(val&0xf)|(val<<4)); |
| 1150 | break; |
| 1151 | case 0x0a: /* Mic Level (SBPRO) or DAC Volume (SB2): 2-bit, 3-bit on SB16 */ |
| 1152 | if (sb.type==SBT_2) sb.mixer.dac[0]=sb.mixer.dac[1]=((val & 0x6) << 2)|3; |
| 1153 | else sb.mixer.mic=((val & 0x7) << 2)|(sb.type==SBT_16?1:3); |
| 1154 | break; |
| 1155 | case 0x0e: /* Output/Stereo Select */ |
| 1156 | sb.mixer.stereo=(val & 0x2) > 0; |
| 1157 | sb.mixer.filtered=(val & 0x20) > 0; |
| 1158 | DSP_ChangeStereo(sb.mixer.stereo); |
| 1159 | LOG(LOG_SB,LOG_WARN)("Mixer set to %s",sb.dma.stereo ? "STEREO" : "MONO"); |
| 1160 | break; |
| 1161 | case 0x22: /* Master Volume (SBPRO) */ |
| 1162 | SETPROVOL(sb.mixer.master,val); |
| 1163 | CTMIXER_UpdateVolumes(); |
| 1164 | break; |
| 1165 | case 0x26: /* FM Volume (SBPRO) */ |
| 1166 | SETPROVOL(sb.mixer.fm,val); |
| 1167 | CTMIXER_UpdateVolumes(); |
| 1168 | break; |
| 1169 | case 0x28: /* CD Audio Volume (SBPRO) */ |
| 1170 | SETPROVOL(sb.mixer.cda,val); |
| 1171 | break; |
| 1172 | case 0x2e: /* Line-in Volume (SBPRO) */ |
| 1173 | SETPROVOL(sb.mixer.lin,val); |
| 1174 | break; |
| 1175 | //case 0x20: /* Master Volume Left (SBPRO) ? */ |
| 1176 | case 0x30: /* Master Volume Left (SB16) */ |
| 1177 | if (sb.type==SBT_16) { |
| 1178 | sb.mixer.master[0]=val>>3; |
| 1179 | CTMIXER_UpdateVolumes(); |
| 1180 | } |
| 1181 | break; |
| 1182 | //case 0x21: /* Master Volume Right (SBPRO) ? */ |
| 1183 | case 0x31: /* Master Volume Right (SB16) */ |
| 1184 | if (sb.type==SBT_16) { |
| 1185 | sb.mixer.master[1]=val>>3; |
| 1186 | CTMIXER_UpdateVolumes(); |
| 1187 | } |
| 1188 | break; |
| 1189 | case 0x32: /* DAC Volume Left (SB16) */ |
| 1190 | if (sb.type==SBT_16) { |
| 1191 | sb.mixer.dac[0]=val>>3; |
| 1192 | CTMIXER_UpdateVolumes(); |
| 1193 | } |
| 1194 | break; |
| 1195 | case 0x33: /* DAC Volume Right (SB16) */ |
| 1196 | if (sb.type==SBT_16) { |
| 1197 | sb.mixer.dac[1]=val>>3; |
| 1198 | CTMIXER_UpdateVolumes(); |
| 1199 | } |
| 1200 | break; |
| 1201 | case 0x34: /* FM Volume Left (SB16) */ |
| 1202 | if (sb.type==SBT_16) { |
| 1203 | sb.mixer.fm[0]=val>>3; |
| 1204 | CTMIXER_UpdateVolumes(); |
| 1205 | } |
| 1206 | break; |
| 1207 | case 0x35: /* FM Volume Right (SB16) */ |
| 1208 | if (sb.type==SBT_16) { |
| 1209 | sb.mixer.fm[1]=val>>3; |
| 1210 | CTMIXER_UpdateVolumes(); |
| 1211 | } |
| 1212 | break; |
| 1213 | case 0x36: /* CD Volume Left (SB16) */ |
| 1214 | if (sb.type==SBT_16) sb.mixer.cda[0]=val>>3; |
| 1215 | break; |
| 1216 | case 0x37: /* CD Volume Right (SB16) */ |
| 1217 | if (sb.type==SBT_16) sb.mixer.cda[1]=val>>3; |
| 1218 | break; |
| 1219 | case 0x38: /* Line-in Volume Left (SB16) */ |
| 1220 | if (sb.type==SBT_16) sb.mixer.lin[0]=val>>3; |
| 1221 | break; |
| 1222 | case 0x39: /* Line-in Volume Right (SB16) */ |
| 1223 | if (sb.type==SBT_16) sb.mixer.lin[1]=val>>3; |
| 1224 | break; |
| 1225 | case 0x3a: |
| 1226 | if (sb.type==SBT_16) sb.mixer.mic=val>>3; |
| 1227 | break; |
| 1228 | case 0x80: /* IRQ Select */ |
| 1229 | sb.hw.irq=0xff; |
| 1230 | if (val & 0x1) sb.hw.irq=2; |
| 1231 | else if (val & 0x2) sb.hw.irq=5; |
| 1232 | else if (val & 0x4) sb.hw.irq=7; |
| 1233 | else if (val & 0x8) sb.hw.irq=10; |
| 1234 | break; |
| 1235 | case 0x81: /* DMA Select */ |
| 1236 | sb.hw.dma8=0xff; |
| 1237 | sb.hw.dma16=0xff; |
| 1238 | if (val & 0x1) sb.hw.dma8=0; |
| 1239 | else if (val & 0x2) sb.hw.dma8=1; |
| 1240 | else if (val & 0x8) sb.hw.dma8=3; |
| 1241 | if (val & 0x20) sb.hw.dma16=5; |
| 1242 | else if (val & 0x40) sb.hw.dma16=6; |
| 1243 | else if (val & 0x80) sb.hw.dma16=7; |
| 1244 | LOG(LOG_SB,LOG_NORMAL)("Mixer select dma8:%x dma16:%x",sb.hw.dma8,sb.hw.dma16); |
| 1245 | break; |
| 1246 | default: |
| 1247 | |
| 1248 | if( ((sb.type == SBT_PRO1 || sb.type == SBT_PRO2) && sb.mixer.index==0x0c) || /* Input control on SBPro */ |
| 1249 | (sb.type == SBT_16 && sb.mixer.index >= 0x3b && sb.mixer.index <= 0x47)) /* New SB16 registers */ |
| 1250 | sb.mixer.unhandled[sb.mixer.index] = val; |
| 1251 | LOG(LOG_SB,LOG_WARN)("MIXER:Write %X to unhandled index %X",val,sb.mixer.index); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | static Bit8u CTMIXER_Read(void) { |
| 1256 | Bit8u ret; |
| 1257 | // if ( sb.mixer.index< 0x80) LOG_MSG("Read mixer %x",sb.mixer.index); |
| 1258 | switch (sb.mixer.index) { |
| 1259 | case 0x00: /* RESET */ |
| 1260 | return 0x00; |
| 1261 | case 0x02: /* Master Volume (SB2 Only) */ |
| 1262 | return ((sb.mixer.master[1]>>1) & 0xe); |
| 1263 | case 0x22: /* Master Volume (SBPRO) */ |
| 1264 | return MAKEPROVOL(sb.mixer.master); |
| 1265 | case 0x04: /* DAC Volume (SBPRO) */ |
| 1266 | return MAKEPROVOL(sb.mixer.dac); |
| 1267 | case 0x06: /* FM Volume (SB2 Only) + FM output selection */ |
| 1268 | return ((sb.mixer.fm[1]>>1) & 0xe); |
| 1269 | case 0x08: /* CD Volume (SB2 Only) */ |
| 1270 | return ((sb.mixer.cda[1]>>1) & 0xe); |
| 1271 | case 0x0a: /* Mic Level (SBPRO) or Voice (SB2 Only) */ |
| 1272 | if (sb.type==SBT_2) return (sb.mixer.dac[0]>>2); |
| 1273 | else return ((sb.mixer.mic >> 2) & (sb.type==SBT_16 ? 7:6)); |
| 1274 | case 0x0e: /* Output/Stereo Select */ |
| 1275 | return 0x11|(sb.mixer.stereo ? 0x02 : 0x00)|(sb.mixer.filtered ? 0x20 : 0x00); |
| 1276 | case 0x26: /* FM Volume (SBPRO) */ |
| 1277 | return MAKEPROVOL(sb.mixer.fm); |
| 1278 | case 0x28: /* CD Audio Volume (SBPRO) */ |
| 1279 | return MAKEPROVOL(sb.mixer.cda); |
| 1280 | case 0x2e: /* Line-IN Volume (SBPRO) */ |
| 1281 | return MAKEPROVOL(sb.mixer.lin); |
| 1282 | case 0x30: /* Master Volume Left (SB16) */ |
| 1283 | if (sb.type==SBT_16) return sb.mixer.master[0]<<3; |
| 1284 | ret=0xa; |
| 1285 | break; |
| 1286 | case 0x31: /* Master Volume Right (S16) */ |
| 1287 | if (sb.type==SBT_16) return sb.mixer.master[1]<<3; |
| 1288 | ret=0xa; |
| 1289 | break; |
| 1290 | case 0x32: /* DAC Volume Left (SB16) */ |
| 1291 | if (sb.type==SBT_16) return sb.mixer.dac[0]<<3; |
| 1292 | ret=0xa; |
| 1293 | break; |
| 1294 | case 0x33: /* DAC Volume Right (SB16) */ |
| 1295 | if (sb.type==SBT_16) return sb.mixer.dac[1]<<3; |
| 1296 | ret=0xa; |
| 1297 | break; |
| 1298 | case 0x34: /* FM Volume Left (SB16) */ |
| 1299 | if (sb.type==SBT_16) return sb.mixer.fm[0]<<3; |
| 1300 | ret=0xa; |
| 1301 | break; |
| 1302 | case 0x35: /* FM Volume Right (SB16) */ |
| 1303 | if (sb.type==SBT_16) return sb.mixer.fm[1]<<3; |
| 1304 | ret=0xa; |
| 1305 | break; |
| 1306 | case 0x36: /* CD Volume Left (SB16) */ |
| 1307 | if (sb.type==SBT_16) return sb.mixer.cda[0]<<3; |
| 1308 | ret=0xa; |
| 1309 | break; |
| 1310 | case 0x37: /* CD Volume Right (SB16) */ |
| 1311 | if (sb.type==SBT_16) return sb.mixer.cda[1]<<3; |
| 1312 | ret=0xa; |
| 1313 | break; |
| 1314 | case 0x38: /* Line-in Volume Left (SB16) */ |
| 1315 | if (sb.type==SBT_16) return sb.mixer.lin[0]<<3; |
| 1316 | ret=0xa; |
| 1317 | break; |
| 1318 | case 0x39: /* Line-in Volume Right (SB16) */ |
| 1319 | if (sb.type==SBT_16) return sb.mixer.lin[1]<<3; |
| 1320 | ret=0xa; |
| 1321 | break; |
| 1322 | case 0x3a: /* Mic Volume (SB16) */ |
| 1323 | if (sb.type==SBT_16) return sb.mixer.mic<<3; |
| 1324 | ret=0xa; |
| 1325 | break; |
| 1326 | case 0x80: /* IRQ Select */ |
| 1327 | switch (sb.hw.irq) { |
| 1328 | case 2: return 0x1; |
| 1329 | case 5: return 0x2; |
| 1330 | case 7: return 0x4; |
| 1331 | case 10: return 0x8; |
| 1332 | } |
| 1333 | case 0x81: /* DMA Select */ |
| 1334 | ret=0; |
| 1335 | switch (sb.hw.dma8) { |
| 1336 | case 0:ret|=0x1;break; |
| 1337 | case 1:ret|=0x2;break; |
| 1338 | case 3:ret|=0x8;break; |
| 1339 | } |
| 1340 | switch (sb.hw.dma16) { |
| 1341 | case 5:ret|=0x20;break; |
| 1342 | case 6:ret|=0x40;break; |
| 1343 | case 7:ret|=0x80;break; |
| 1344 | } |
| 1345 | return ret; |
| 1346 | case 0x82: /* IRQ Status */ |
| 1347 | return (sb.irq.pending_8bit ? 0x1 : 0) | |
| 1348 | (sb.irq.pending_16bit ? 0x2 : 0); |
| 1349 | default: |
| 1350 | if ( ((sb.type == SBT_PRO1 || sb.type == SBT_PRO2) && sb.mixer.index==0x0c) || /* Input control on SBPro */ |
| 1351 | (sb.type == SBT_16 && sb.mixer.index >= 0x3b && sb.mixer.index <= 0x47)) /* New SB16 registers */ |
| 1352 | ret = sb.mixer.unhandled[sb.mixer.index]; |
| 1353 | else |
| 1354 | ret=0xa; |
| 1355 | LOG(LOG_SB,LOG_WARN)("MIXER:Read from unhandled index %X",sb.mixer.index); |
| 1356 | } |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
| 1360 | |
| 1361 | static Bitu read_sb(Bitu port,Bitu /*iolen*/) { |
| 1362 | switch (port-sb.hw.base) { |
| 1363 | case MIXER_INDEX: |
| 1364 | return sb.mixer.index; |
| 1365 | case MIXER_DATA: |
| 1366 | return CTMIXER_Read(); |
| 1367 | case DSP_READ_DATA: |
| 1368 | return DSP_ReadData(); |
| 1369 | case DSP_READ_STATUS: |
| 1370 | //TODO See for high speed dma :) |
| 1371 | if (sb.irq.pending_8bit) { |
| 1372 | sb.irq.pending_8bit=false; |
| 1373 | PIC_DeActivateIRQ(sb.hw.irq); |
| 1374 | } |
| 1375 | if (sb.dsp.out.used) return 0xff; |
| 1376 | else return 0x7f; |
| 1377 | case DSP_ACK_16BIT: |
| 1378 | sb.irq.pending_16bit=false; |
| 1379 | break; |
| 1380 | case DSP_WRITE_STATUS: |
| 1381 | switch (sb.dsp.state) { |
| 1382 | case DSP_S_NORMAL: |
| 1383 | sb.dsp.write_busy++; |
| 1384 | if (sb.dsp.write_busy & 8) return 0xff; |
| 1385 | return 0x7f; |
| 1386 | case DSP_S_RESET: |
| 1387 | case DSP_S_RESET_WAIT: |
| 1388 | return 0xff; |
| 1389 | } |
| 1390 | return 0xff; |
| 1391 | case DSP_RESET: |
| 1392 | return 0xff; |
| 1393 | default: |
| 1394 | LOG(LOG_SB,LOG_NORMAL)("Unhandled read from SB Port %4X",port); |
| 1395 | break; |
| 1396 | } |
| 1397 | return 0xff; |
| 1398 | } |
| 1399 | |
| 1400 | static void write_sb(Bitu port,Bitu val,Bitu /*iolen*/) { |
| 1401 | Bit8u val8=(Bit8u)(val&0xff); |
| 1402 | switch (port-sb.hw.base) { |
| 1403 | case DSP_RESET: |
| 1404 | DSP_DoReset(val8); |
| 1405 | break; |
| 1406 | case DSP_WRITE_DATA: |
| 1407 | DSP_DoWrite(val8); |
| 1408 | break; |
| 1409 | case MIXER_INDEX: |
| 1410 | sb.mixer.index=val8; |
| 1411 | break; |
| 1412 | case MIXER_DATA: |
| 1413 | CTMIXER_Write(val8); |
| 1414 | break; |
| 1415 | default: |
| 1416 | LOG(LOG_SB,LOG_NORMAL)("Unhandled write to SB Port %4X",port); |
| 1417 | break; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | static void adlib_gusforward(Bitu /*port*/,Bitu val,Bitu /*iolen*/) { |
| 1422 | adlib_commandreg=(Bit8u)(val&0xff); |
| 1423 | } |
| 1424 | |
| 1425 | bool SB_Get_Address(Bitu& sbaddr, Bitu& sbirq, Bitu& sbdma) { |
| 1426 | sbaddr=0; |
| 1427 | sbirq =0; |
| 1428 | sbdma =0; |
| 1429 | if (sb.type == SBT_NONE) return false; |
| 1430 | else { |
| 1431 | sbaddr=sb.hw.base; |
| 1432 | sbirq =sb.hw.irq; |
| 1433 | sbdma = sb.hw.dma8; |
| 1434 | return true; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | static void SBLASTER_CallBack(Bitu len) { |
| 1439 | switch (sb.mode) { |
| 1440 | case MODE_NONE: |
| 1441 | case MODE_DMA_PAUSE: |
| 1442 | case MODE_DMA_MASKED: |
| 1443 | sb.chan->AddSilence(); |
| 1444 | break; |
| 1445 | case MODE_DAC: |
| 1446 | // GenerateDACSound(len); |
| 1447 | // break; |
| 1448 | if (!sb.dac.used) { |
| 1449 | sb.mode=MODE_NONE; |
| 1450 | return; |
| 1451 | } |
| 1452 | sb.chan->AddStretched(sb.dac.used,sb.dac.data); |
| 1453 | sb.dac.used=0; |
| 1454 | break; |
| 1455 | case MODE_DMA: |
| 1456 | len*=sb.dma.mul; |
| 1457 | if (len&SB_SH_MASK) len+=1 << SB_SH; |
| 1458 | len>>=SB_SH; |
| 1459 | if (len>sb.dma.left) len=sb.dma.left; |
| 1460 | GenerateDMASound(len); |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | class SBLASTER: public Module_base { |
| 1466 | private: |
| 1467 | /* Data */ |
| 1468 | IO_ReadHandleObject ReadHandler[0x10]; |
| 1469 | IO_WriteHandleObject WriteHandler[0x10]; |
| 1470 | AutoexecObject autoexecline; |
| 1471 | MixerObject MixerChan; |
| 1472 | OPL_Mode oplmode; |
| 1473 | |
| 1474 | /* Support Functions */ |
| 1475 | void Find_Type_And_Opl(Section_prop* config,SB_TYPES& type, OPL_Mode& opl_mode){ |
| 1476 | const char * sbtype=config->Get_string("sbtype"); |
| 1477 | if (!strcasecmp(sbtype,"sb1")) type=SBT_1; |
| 1478 | else if (!strcasecmp(sbtype,"sb2")) type=SBT_2; |
| 1479 | else if (!strcasecmp(sbtype,"sbpro1")) type=SBT_PRO1; |
| 1480 | else if (!strcasecmp(sbtype,"sbpro2")) type=SBT_PRO2; |
| 1481 | else if (!strcasecmp(sbtype,"sb16")) type=SBT_16; |
| 1482 | else if (!strcasecmp(sbtype,"gb")) type=SBT_GB; |
| 1483 | else if (!strcasecmp(sbtype,"none")) type=SBT_NONE; |
| 1484 | else type=SBT_16; |
| 1485 | |
| 1486 | if (type==SBT_16) { |
| 1487 | if ((!IS_EGAVGA_ARCH) || !SecondDMAControllerAvailable()) type=SBT_PRO2; |
| 1488 | } |
| 1489 | |
| 1490 | /* OPL/CMS Init */ |
| 1491 | const char * omode=config->Get_string("oplmode"); |
| 1492 | if (!strcasecmp(omode,"none")) opl_mode=OPL_none; |
| 1493 | else if (!strcasecmp(omode,"cms")) opl_mode=OPL_cms; |
| 1494 | else if (!strcasecmp(omode,"opl2")) opl_mode=OPL_opl2; |
| 1495 | else if (!strcasecmp(omode,"dualopl2")) opl_mode=OPL_dualopl2; |
| 1496 | else if (!strcasecmp(omode,"opl3")) opl_mode=OPL_opl3; |
| 1497 | /* Else assume auto */ |
| 1498 | else { |
| 1499 | switch (type) { |
| 1500 | case SBT_NONE: |
| 1501 | opl_mode=OPL_none; |
| 1502 | break; |
| 1503 | case SBT_GB: |
| 1504 | opl_mode=OPL_cms; |
| 1505 | break; |
| 1506 | case SBT_1: |
| 1507 | case SBT_2: |
| 1508 | opl_mode=OPL_opl2; |
| 1509 | break; |
| 1510 | case SBT_PRO1: |
| 1511 | opl_mode=OPL_dualopl2; |
| 1512 | break; |
| 1513 | case SBT_PRO2: |
| 1514 | case SBT_16: |
| 1515 | opl_mode=OPL_opl3; |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | public: |
| 1521 | SBLASTER(Section* configuration):Module_base(configuration) { |
| 1522 | Bitu i; |
| 1523 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 1524 | |
| 1525 | sb.hw.base=section->Get_hex("sbbase"); |
| 1526 | sb.hw.irq=section->Get_int("irq"); |
| 1527 | Bitu dma8bit=section->Get_int("dma"); |
| 1528 | if (dma8bit>0xff) dma8bit=0xff; |
| 1529 | sb.hw.dma8=(Bit8u)(dma8bit&0xff); |
| 1530 | Bitu dma16bit=section->Get_int("hdma"); |
| 1531 | if (dma16bit>0xff) dma16bit=0xff; |
| 1532 | sb.hw.dma16=(Bit8u)(dma16bit&0xff); |
| 1533 | |
| 1534 | sb.mixer.enabled=section->Get_bool("sbmixer"); |
| 1535 | sb.mixer.stereo=false; |
| 1536 | |
| 1537 | Find_Type_And_Opl(section,sb.type,oplmode); |
| 1538 | |
| 1539 | switch (oplmode) { |
| 1540 | case OPL_none: |
| 1541 | WriteHandler[0].Install(0x388,adlib_gusforward,IO_MB); |
| 1542 | break; |
| 1543 | case OPL_cms: |
| 1544 | WriteHandler[0].Install(0x388,adlib_gusforward,IO_MB); |
| 1545 | CMS_Init(section); |
| 1546 | break; |
| 1547 | case OPL_opl2: |
| 1548 | CMS_Init(section); |
| 1549 | // fall-through |
| 1550 | case OPL_dualopl2: |
| 1551 | case OPL_opl3: |
| 1552 | OPL_Init(section,oplmode); |
| 1553 | break; |
| 1554 | } |
| 1555 | if (sb.type==SBT_NONE || sb.type==SBT_GB) return; |
| 1556 | |
| 1557 | sb.chan=MixerChan.Install(&SBLASTER_CallBack,22050,"SB"); |
| 1558 | sb.dsp.state=DSP_S_NORMAL; |
| 1559 | sb.dsp.out.lastval=0xaa; |
| 1560 | sb.dma.chan=NULL; |
| 1561 | |
| 1562 | for (i=4;i<=0xf;i++) { |
| 1563 | if (i==8 || i==9) continue; |
| 1564 | //Disable mixer ports for lower soundblaster |
| 1565 | if ((sb.type==SBT_1 || sb.type==SBT_2) && (i==4 || i==5)) continue; |
| 1566 | ReadHandler[i].Install(sb.hw.base+i,read_sb,IO_MB); |
| 1567 | WriteHandler[i].Install(sb.hw.base+i,write_sb,IO_MB); |
| 1568 | } |
| 1569 | for (i=0;i<256;i++) ASP_regs[i] = 0; |
| 1570 | ASP_regs[5] = 0x01; |
| 1571 | ASP_regs[9] = 0xf8; |
| 1572 | |
| 1573 | DSP_Reset(); |
| 1574 | CTMIXER_Reset(); |
| 1575 | |
| 1576 | // The documentation does not specify if SB gets initialized with the speaker enabled |
| 1577 | // or disabled. Real SBPro2 has it disabled. |
| 1578 | sb.speaker=false; |
| 1579 | // On SB16 the speaker flag does not affect actual speaker state. |
| 1580 | if (sb.type == SBT_16) sb.chan->Enable(true); |
| 1581 | else sb.chan->Enable(false); |
| 1582 | |
| 1583 | // Create set blaster line |
| 1584 | ostringstream temp; |
| 1585 | temp << "SET BLASTER=A" << setw(3)<< hex << sb.hw.base |
| 1586 | << " I" << dec << (Bitu)sb.hw.irq << " D" << (Bitu)sb.hw.dma8; |
| 1587 | if (sb.type==SBT_16) temp << " H" << (Bitu)sb.hw.dma16; |
| 1588 | temp << " T" << static_cast<unsigned int>(sb.type) << ends; |
| 1589 | |
| 1590 | autoexecline.Install(temp.str()); |
| 1591 | |
| 1592 | /* Soundblaster midi interface */ |
| 1593 | if (!MIDI_Available()) sb.midi = false; |
| 1594 | else sb.midi = true; |
| 1595 | } |
| 1596 | |
| 1597 | ~SBLASTER() { |
| 1598 | switch (oplmode) { |
| 1599 | case OPL_none: |
| 1600 | break; |
| 1601 | case OPL_cms: |
| 1602 | CMS_ShutDown(m_configuration); |
| 1603 | break; |
| 1604 | case OPL_opl2: |
| 1605 | CMS_ShutDown(m_configuration); |
| 1606 | // fall-through |
| 1607 | case OPL_dualopl2: |
| 1608 | case OPL_opl3: |
| 1609 | OPL_ShutDown(m_configuration); |
| 1610 | break; |
| 1611 | } |
| 1612 | if (sb.type==SBT_NONE || sb.type==SBT_GB) return; |
| 1613 | DSP_Reset(); // Stop everything |
| 1614 | } |
| 1615 | }; //End of SBLASTER class |
| 1616 | |
| 1617 | |
| 1618 | static SBLASTER* test; |
| 1619 | void SBLASTER_ShutDown(Section* /*sec*/) { |
| 1620 | delete test; |
| 1621 | } |
| 1622 | |
| 1623 | void SBLASTER_Init(Section* sec) { |
| 1624 | test = new SBLASTER(sec); |
| 1625 | sec->AddDestroyFunction(&SBLASTER_ShutDown,true); |
| 1626 | } |