| 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: gus.cpp,v 1.37 2009-09-03 16:03:01 c2woody Exp $ */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <iomanip> |
| 23 | #include <sstream> |
| 24 | #include "dosbox.h" |
| 25 | #include "inout.h" |
| 26 | #include "mixer.h" |
| 27 | #include "dma.h" |
| 28 | #include "pic.h" |
| 29 | #include "setup.h" |
| 30 | #include "shell.h" |
| 31 | #include "math.h" |
| 32 | #include "regs.h" |
| 33 | using namespace std; |
| 34 | |
| 35 | //Extra bits of precision over normal gus |
| 36 | #define WAVE_BITS 2 |
| 37 | #define WAVE_FRACT (9+WAVE_BITS) |
| 38 | #define WAVE_FRACT_MASK ((1 << WAVE_FRACT)-1) |
| 39 | #define WAVE_MSWMASK ((1 << (16+WAVE_BITS))-1) |
| 40 | #define WAVE_LSWMASK (0xffffffff ^ WAVE_MSWMASK) |
| 41 | |
| 42 | //Amount of precision the volume has |
| 43 | #define RAMP_FRACT (10) |
| 44 | #define RAMP_FRACT_MASK ((1 << RAMP_FRACT)-1) |
| 45 | |
| 46 | #define GUS_BASE myGUS.portbase |
| 47 | #define GUS_RATE myGUS.rate |
| 48 | #define LOG_GUS 0 |
| 49 | |
| 50 | Bit8u adlib_commandreg; |
| 51 | static MixerChannel * gus_chan; |
| 52 | static Bit8u irqtable[8] = { 0, 2, 5, 3, 7, 11, 12, 15 }; |
| 53 | static Bit8u dmatable[8] = { 0, 1, 3, 5, 6, 7, 0, 0 }; |
| 54 | static Bit8u GUSRam[1024*1024]; // 1024K of GUS Ram |
| 55 | static Bit32s AutoAmp = 512; |
| 56 | static Bit16u vol16bit[4096]; |
| 57 | static Bit32u pantable[16]; |
| 58 | |
| 59 | class GUSChannels; |
| 60 | static void CheckVoiceIrq(void); |
| 61 | |
| 62 | struct GFGus { |
| 63 | Bit8u gRegSelect; |
| 64 | Bit16u gRegData; |
| 65 | Bit32u gDramAddr; |
| 66 | Bit16u gCurChannel; |
| 67 | |
| 68 | Bit8u DMAControl; |
| 69 | Bit16u dmaAddr; |
| 70 | Bit8u TimerControl; |
| 71 | Bit8u SampControl; |
| 72 | Bit8u mixControl; |
| 73 | Bit8u ActiveChannels; |
| 74 | Bit32u basefreq; |
| 75 | |
| 76 | struct GusTimer { |
| 77 | Bit8u value; |
| 78 | bool reached; |
| 79 | bool raiseirq; |
| 80 | bool masked; |
| 81 | bool running; |
| 82 | float delay; |
| 83 | } timers[2]; |
| 84 | Bit32u rate; |
| 85 | Bitu portbase; |
| 86 | Bit8u dma1; |
| 87 | Bit8u dma2; |
| 88 | |
| 89 | Bit8u irq1; |
| 90 | Bit8u irq2; |
| 91 | |
| 92 | bool irqenabled; |
| 93 | bool ChangeIRQDMA; |
| 94 | // IRQ status register values |
| 95 | Bit8u IRQStatus; |
| 96 | Bit32u ActiveMask; |
| 97 | Bit8u IRQChan; |
| 98 | Bit32u RampIRQ; |
| 99 | Bit32u WaveIRQ; |
| 100 | } myGUS; |
| 101 | |
| 102 | Bitu DEBUG_EnableDebugger(void); |
| 103 | |
| 104 | static void GUS_DMA_Callback(DmaChannel * chan,DMAEvent event); |
| 105 | |
| 106 | // Returns a single 16-bit sample from the Gravis's RAM |
| 107 | static INLINE Bit32s GetSample(Bit32u Delta, Bit32u CurAddr, bool eightbit) { |
| 108 | Bit32u useAddr; |
| 109 | Bit32u holdAddr; |
| 110 | useAddr = CurAddr >> WAVE_FRACT; |
| 111 | if (eightbit) { |
| 112 | if (Delta >= (1 << WAVE_FRACT)) { |
| 113 | Bit32s tmpsmall = (Bit8s)GUSRam[useAddr]; |
| 114 | return tmpsmall << 8; |
| 115 | } else { |
| 116 | // Interpolate |
| 117 | Bit32s w1 = ((Bit8s)GUSRam[useAddr+0]) << 8; |
| 118 | Bit32s w2 = ((Bit8s)GUSRam[useAddr+1]) << 8; |
| 119 | Bit32s diff = w2 - w1; |
| 120 | return (w1+((diff*(Bit32s)(CurAddr&WAVE_FRACT_MASK ))>>WAVE_FRACT)); |
| 121 | } |
| 122 | } else { |
| 123 | // Formula used to convert addresses for use with 16-bit samples |
| 124 | holdAddr = useAddr & 0xc0000L; |
| 125 | useAddr = useAddr & 0x1ffffL; |
| 126 | useAddr = useAddr << 1; |
| 127 | useAddr = (holdAddr | useAddr); |
| 128 | |
| 129 | if(Delta >= (1 << WAVE_FRACT)) { |
| 130 | return (GUSRam[useAddr+0] | (((Bit8s)GUSRam[useAddr+1]) << 8)); |
| 131 | } else { |
| 132 | // Interpolate |
| 133 | Bit32s w1 = (GUSRam[useAddr+0] | (((Bit8s)GUSRam[useAddr+1]) << 8)); |
| 134 | Bit32s w2 = (GUSRam[useAddr+2] | (((Bit8s)GUSRam[useAddr+3]) << 8)); |
| 135 | Bit32s diff = w2 - w1; |
| 136 | return (w1+((diff*(Bit32s)(CurAddr&WAVE_FRACT_MASK ))>>WAVE_FRACT)); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | class GUSChannels { |
| 142 | public: |
| 143 | Bit32u WaveStart; |
| 144 | Bit32u WaveEnd; |
| 145 | Bit32u WaveAddr; |
| 146 | Bit32u WaveAdd; |
| 147 | Bit8u WaveCtrl; |
| 148 | Bit16u WaveFreq; |
| 149 | |
| 150 | Bit32u RampStart; |
| 151 | Bit32u RampEnd; |
| 152 | Bit32u RampVol; |
| 153 | Bit32u RampAdd; |
| 154 | Bit32u RampAddReal; |
| 155 | |
| 156 | Bit8u RampRate; |
| 157 | Bit8u RampCtrl; |
| 158 | |
| 159 | Bit8u PanPot; |
| 160 | Bit8u channum; |
| 161 | Bit32u irqmask; |
| 162 | Bit32u PanLeft; |
| 163 | Bit32u PanRight; |
| 164 | Bit32s VolLeft; |
| 165 | Bit32s VolRight; |
| 166 | |
| 167 | GUSChannels(Bit8u num) { |
| 168 | channum = num; |
| 169 | irqmask = 1 << num; |
| 170 | WaveStart = 0; |
| 171 | WaveEnd = 0; |
| 172 | WaveAddr = 0; |
| 173 | WaveAdd = 0; |
| 174 | WaveFreq = 0; |
| 175 | WaveCtrl = 3; |
| 176 | RampRate = 0; |
| 177 | RampStart = 0; |
| 178 | RampEnd = 0; |
| 179 | RampCtrl = 3; |
| 180 | RampAdd = 0; |
| 181 | RampVol = 0; |
| 182 | VolLeft = 0; |
| 183 | VolRight = 0; |
| 184 | PanLeft = 0; |
| 185 | PanRight = 0; |
| 186 | PanPot = 0x7; |
| 187 | }; |
| 188 | void WriteWaveFreq(Bit16u val) { |
| 189 | WaveFreq = val; |
| 190 | double frameadd = double(val >> 1)/512.0; //Samples / original gus frame |
| 191 | double realadd = (frameadd*(double)myGUS.basefreq/(double)GUS_RATE) * (double)(1 << WAVE_FRACT); |
| 192 | WaveAdd = (Bit32u)realadd; |
| 193 | } |
| 194 | void WriteWaveCtrl(Bit8u val) { |
| 195 | Bit32u oldirq=myGUS.WaveIRQ; |
| 196 | WaveCtrl = val & 0x7f; |
| 197 | if ((val & 0xa0)==0xa0) myGUS.WaveIRQ|=irqmask; |
| 198 | else myGUS.WaveIRQ&=~irqmask; |
| 199 | if (oldirq != myGUS.WaveIRQ) |
| 200 | CheckVoiceIrq(); |
| 201 | } |
| 202 | INLINE Bit8u ReadWaveCtrl(void) { |
| 203 | Bit8u ret=WaveCtrl; |
| 204 | if (myGUS.WaveIRQ & irqmask) ret|=0x80; |
| 205 | return ret; |
| 206 | } |
| 207 | void UpdateWaveRamp(void) { |
| 208 | WriteWaveFreq(WaveFreq); |
| 209 | WriteRampRate(RampRate); |
| 210 | } |
| 211 | void WritePanPot(Bit8u val) { |
| 212 | PanPot = val; |
| 213 | PanLeft = pantable[0x0f-(val & 0xf)]; |
| 214 | PanRight = pantable[(val & 0xf)]; |
| 215 | UpdateVolumes(); |
| 216 | } |
| 217 | Bit8u ReadPanPot(void) { |
| 218 | return PanPot; |
| 219 | } |
| 220 | void WriteRampCtrl(Bit8u val) { |
| 221 | Bit32u old=myGUS.RampIRQ; |
| 222 | RampCtrl = val & 0x7f; |
| 223 | if ((val & 0xa0)==0xa0) myGUS.RampIRQ|=irqmask; |
| 224 | else myGUS.RampIRQ&=~irqmask; |
| 225 | if (old != myGUS.RampIRQ) CheckVoiceIrq(); |
| 226 | } |
| 227 | INLINE Bit8u ReadRampCtrl(void) { |
| 228 | Bit8u ret=RampCtrl; |
| 229 | if (myGUS.RampIRQ & irqmask) ret|=0x80; |
| 230 | return ret; |
| 231 | } |
| 232 | void WriteRampRate(Bit8u val) { |
| 233 | RampRate = val; |
| 234 | double frameadd = (double)(RampRate & 63)/(double)(1 << (3*(val >> 6))); |
| 235 | double realadd = (frameadd*(double)myGUS.basefreq/(double)GUS_RATE) * (double)(1 << RAMP_FRACT); |
| 236 | RampAdd = (Bit32u)realadd; |
| 237 | } |
| 238 | INLINE void WaveUpdate(void) { |
| 239 | if (WaveCtrl & 0x3) return; |
| 240 | Bit32s WaveLeft; |
| 241 | if (WaveCtrl & 0x40) { |
| 242 | WaveAddr-=WaveAdd; |
| 243 | WaveLeft=WaveStart-WaveAddr; |
| 244 | } else { |
| 245 | WaveAddr+=WaveAdd; |
| 246 | WaveLeft=WaveAddr-WaveEnd; |
| 247 | } |
| 248 | if (WaveLeft<0) return; |
| 249 | /* Generate an IRQ if needed */ |
| 250 | if (WaveCtrl & 0x20) { |
| 251 | myGUS.WaveIRQ|=irqmask; |
| 252 | } |
| 253 | /* Check for not being in PCM operation */ |
| 254 | if (RampCtrl & 0x04) return; |
| 255 | /* Check for looping */ |
| 256 | if (WaveCtrl & 0x08) { |
| 257 | /* Bi-directional looping */ |
| 258 | if (WaveCtrl & 0x10) WaveCtrl^=0x40; |
| 259 | WaveAddr = (WaveCtrl & 0x40) ? (WaveEnd-WaveLeft) : (WaveStart+WaveLeft); |
| 260 | } else { |
| 261 | WaveCtrl|=1; //Stop the channel |
| 262 | WaveAddr = (WaveCtrl & 0x40) ? WaveStart : WaveEnd; |
| 263 | } |
| 264 | } |
| 265 | INLINE void UpdateVolumes(void) { |
| 266 | Bit32s templeft=RampVol - PanLeft; |
| 267 | templeft&=~(templeft >> 31); |
| 268 | Bit32s tempright=RampVol - PanRight; |
| 269 | tempright&=~(tempright >> 31); |
| 270 | VolLeft=vol16bit[templeft >> RAMP_FRACT]; |
| 271 | VolRight=vol16bit[tempright >> RAMP_FRACT]; |
| 272 | } |
| 273 | INLINE void RampUpdate(void) { |
| 274 | /* Check if ramping enabled */ |
| 275 | if (RampCtrl & 0x3) return; |
| 276 | Bit32s RampLeft; |
| 277 | if (RampCtrl & 0x40) { |
| 278 | RampVol-=RampAdd; |
| 279 | RampLeft=RampStart-RampVol; |
| 280 | } else { |
| 281 | RampVol+=RampAdd; |
| 282 | RampLeft=RampVol-RampEnd; |
| 283 | } |
| 284 | if (RampLeft<0) { |
| 285 | UpdateVolumes(); |
| 286 | return; |
| 287 | } |
| 288 | /* Generate an IRQ if needed */ |
| 289 | if (RampCtrl & 0x20) { |
| 290 | myGUS.RampIRQ|=irqmask; |
| 291 | } |
| 292 | /* Check for looping */ |
| 293 | if (RampCtrl & 0x08) { |
| 294 | /* Bi-directional looping */ |
| 295 | if (RampCtrl & 0x10) RampCtrl^=0x40; |
| 296 | RampVol = (RampCtrl & 0x40) ? (RampEnd-RampLeft) : (RampStart+RampLeft); |
| 297 | } else { |
| 298 | RampCtrl|=1; //Stop the channel |
| 299 | RampVol = (RampCtrl & 0x40) ? RampStart : RampEnd; |
| 300 | } |
| 301 | UpdateVolumes(); |
| 302 | } |
| 303 | void generateSamples(Bit32s * stream,Bit32u len) { |
| 304 | int i; |
| 305 | Bit32s tmpsamp; |
| 306 | bool eightbit; |
| 307 | if (RampCtrl & WaveCtrl & 3) return; |
| 308 | eightbit = ((WaveCtrl & 0x4) == 0); |
| 309 | |
| 310 | for(i=0;i<(int)len;i++) { |
| 311 | // Get sample |
| 312 | tmpsamp = GetSample(WaveAdd, WaveAddr, eightbit); |
| 313 | // Output stereo sample |
| 314 | stream[i<<1]+= tmpsamp * VolLeft; |
| 315 | stream[(i<<1)+1]+= tmpsamp * VolRight; |
| 316 | WaveUpdate(); |
| 317 | RampUpdate(); |
| 318 | } |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | static GUSChannels *guschan[32]; |
| 323 | static GUSChannels *curchan; |
| 324 | |
| 325 | static void GUSReset(void) { |
| 326 | if((myGUS.gRegData & 0x1) == 0x1) { |
| 327 | // Reset |
| 328 | adlib_commandreg = 85; |
| 329 | myGUS.IRQStatus = 0; |
| 330 | myGUS.timers[0].raiseirq = false; |
| 331 | myGUS.timers[1].raiseirq = false; |
| 332 | myGUS.timers[0].reached = false; |
| 333 | myGUS.timers[1].reached = false; |
| 334 | myGUS.timers[0].running = false; |
| 335 | myGUS.timers[1].running = false; |
| 336 | |
| 337 | myGUS.timers[0].value = 0xff; |
| 338 | myGUS.timers[1].value = 0xff; |
| 339 | myGUS.timers[0].delay = 0.080f; |
| 340 | myGUS.timers[1].delay = 0.320f; |
| 341 | |
| 342 | myGUS.ChangeIRQDMA = false; |
| 343 | myGUS.mixControl = 0x0b; // latches enabled by default LINEs disabled |
| 344 | // Stop all channels |
| 345 | int i; |
| 346 | for(i=0;i<32;i++) { |
| 347 | guschan[i]->RampVol=0; |
| 348 | guschan[i]->WriteWaveCtrl(0x1); |
| 349 | guschan[i]->WriteRampCtrl(0x1); |
| 350 | guschan[i]->WritePanPot(0x7); |
| 351 | } |
| 352 | myGUS.IRQChan = 0; |
| 353 | } |
| 354 | if ((myGUS.gRegData & 0x4) != 0) { |
| 355 | myGUS.irqenabled = true; |
| 356 | } else { |
| 357 | myGUS.irqenabled = false; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static INLINE void GUS_CheckIRQ(void) { |
| 362 | if (myGUS.IRQStatus && (myGUS.mixControl & 0x08)) |
| 363 | PIC_ActivateIRQ(myGUS.irq1); |
| 364 | |
| 365 | } |
| 366 | |
| 367 | static void CheckVoiceIrq(void) { |
| 368 | myGUS.IRQStatus&=0x9f; |
| 369 | Bitu totalmask=(myGUS.RampIRQ|myGUS.WaveIRQ) & myGUS.ActiveMask; |
| 370 | if (!totalmask) return; |
| 371 | if (myGUS.RampIRQ) myGUS.IRQStatus|=0x40; |
| 372 | if (myGUS.WaveIRQ) myGUS.IRQStatus|=0x20; |
| 373 | GUS_CheckIRQ(); |
| 374 | for (;;) { |
| 375 | Bit32u check=(1 << myGUS.IRQChan); |
| 376 | if (totalmask & check) return; |
| 377 | myGUS.IRQChan++; |
| 378 | if (myGUS.IRQChan>=myGUS.ActiveChannels) myGUS.IRQChan=0; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static Bit16u ExecuteReadRegister(void) { |
| 383 | Bit8u tmpreg; |
| 384 | // LOG_MSG("Read global reg %x",myGUS.gRegSelect); |
| 385 | switch (myGUS.gRegSelect) { |
| 386 | case 0x41: // Dma control register - read acknowledges DMA IRQ |
| 387 | tmpreg = myGUS.DMAControl & 0xbf; |
| 388 | tmpreg |= (myGUS.IRQStatus & 0x80) >> 1; |
| 389 | myGUS.IRQStatus&=0x7f; |
| 390 | return (Bit16u)(tmpreg << 8); |
| 391 | case 0x42: // Dma address register |
| 392 | return myGUS.dmaAddr; |
| 393 | case 0x45: // Timer control register. Identical in operation to Adlib's timer |
| 394 | return (Bit16u)(myGUS.TimerControl << 8); |
| 395 | break; |
| 396 | case 0x49: // Dma sample register |
| 397 | tmpreg = myGUS.DMAControl & 0xbf; |
| 398 | tmpreg |= (myGUS.IRQStatus & 0x80) >> 1; |
| 399 | return (Bit16u)(tmpreg << 8); |
| 400 | case 0x80: // Channel voice control read register |
| 401 | if (curchan) return curchan->ReadWaveCtrl() << 8; |
| 402 | else return 0x0300; |
| 403 | |
| 404 | case 0x82: // Channel MSB start address register |
| 405 | if (curchan) return (Bit16u)(curchan->WaveStart >> (WAVE_BITS+16)); |
| 406 | else return 0x0000; |
| 407 | case 0x83: // Channel LSW start address register |
| 408 | if (curchan) return (Bit16u)(curchan->WaveStart >> WAVE_BITS); |
| 409 | else return 0x0000; |
| 410 | |
| 411 | case 0x89: // Channel volume register |
| 412 | if (curchan) return (Bit16u)((curchan->RampVol >> RAMP_FRACT) << 4); |
| 413 | else return 0x0000; |
| 414 | case 0x8a: // Channel MSB current address register |
| 415 | if (curchan) return (Bit16u)(curchan->WaveAddr >> (WAVE_BITS+16)); |
| 416 | else return 0x0000; |
| 417 | case 0x8b: // Channel LSW current address register |
| 418 | if (curchan) return (Bit16u)(curchan->WaveAddr >> WAVE_BITS); |
| 419 | else return 0x0000; |
| 420 | |
| 421 | case 0x8d: // Channel volume control register |
| 422 | if (curchan) return curchan->ReadRampCtrl() << 8; |
| 423 | else return 0x0300; |
| 424 | case 0x8f: // General channel IRQ status register |
| 425 | tmpreg=myGUS.IRQChan|0x20; |
| 426 | Bit32u mask; |
| 427 | mask=1 << myGUS.IRQChan; |
| 428 | if (!(myGUS.RampIRQ & mask)) tmpreg|=0x40; |
| 429 | if (!(myGUS.WaveIRQ & mask)) tmpreg|=0x80; |
| 430 | myGUS.RampIRQ&=~mask; |
| 431 | myGUS.WaveIRQ&=~mask; |
| 432 | CheckVoiceIrq(); |
| 433 | return (Bit16u)(tmpreg << 8); |
| 434 | default: |
| 435 | #if LOG_GUS |
| 436 | LOG_MSG("Read Register num 0x%x", myGUS.gRegSelect); |
| 437 | #endif |
| 438 | return myGUS.gRegData; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static void GUS_TimerEvent(Bitu val) { |
| 443 | if (!myGUS.timers[val].masked) myGUS.timers[val].reached=true; |
| 444 | if (myGUS.timers[val].raiseirq) { |
| 445 | myGUS.IRQStatus|=0x4 << val; |
| 446 | GUS_CheckIRQ(); |
| 447 | } |
| 448 | if (myGUS.timers[val].running) |
| 449 | PIC_AddEvent(GUS_TimerEvent,myGUS.timers[val].delay,val); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | static void ExecuteGlobRegister(void) { |
| 454 | int i; |
| 455 | // if (myGUS.gRegSelect|1!=0x44) LOG_MSG("write global register %x with %x", myGUS.gRegSelect, myGUS.gRegData); |
| 456 | switch(myGUS.gRegSelect) { |
| 457 | case 0x0: // Channel voice control register |
| 458 | if(curchan) curchan->WriteWaveCtrl((Bit16u)myGUS.gRegData>>8); |
| 459 | break; |
| 460 | case 0x1: // Channel frequency control register |
| 461 | if(curchan) curchan->WriteWaveFreq(myGUS.gRegData); |
| 462 | break; |
| 463 | case 0x2: // Channel MSW start address register |
| 464 | if (curchan) { |
| 465 | Bit32u tmpaddr = (Bit32u)(myGUS.gRegData & 0x1fff) << (16+WAVE_BITS); |
| 466 | curchan->WaveStart = (curchan->WaveStart & WAVE_MSWMASK) | tmpaddr; |
| 467 | } |
| 468 | break; |
| 469 | case 0x3: // Channel LSW start address register |
| 470 | if(curchan != NULL) { |
| 471 | Bit32u tmpaddr = (Bit32u)(myGUS.gRegData) << WAVE_BITS; |
| 472 | curchan->WaveStart = (curchan->WaveStart & WAVE_LSWMASK) | tmpaddr; |
| 473 | } |
| 474 | break; |
| 475 | case 0x4: // Channel MSW end address register |
| 476 | if(curchan != NULL) { |
| 477 | Bit32u tmpaddr = (Bit32u)(myGUS.gRegData & 0x1fff) << (16+WAVE_BITS); |
| 478 | curchan->WaveEnd = (curchan->WaveEnd & WAVE_MSWMASK) | tmpaddr; |
| 479 | } |
| 480 | break; |
| 481 | case 0x5: // Channel MSW end address register |
| 482 | if(curchan != NULL) { |
| 483 | Bit32u tmpaddr = (Bit32u)(myGUS.gRegData) << WAVE_BITS; |
| 484 | curchan->WaveEnd = (curchan->WaveEnd & WAVE_LSWMASK) | tmpaddr; |
| 485 | } |
| 486 | break; |
| 487 | case 0x6: // Channel volume ramp rate register |
| 488 | if(curchan != NULL) { |
| 489 | Bit8u tmpdata = (Bit16u)myGUS.gRegData>>8; |
| 490 | curchan->WriteRampRate(tmpdata); |
| 491 | } |
| 492 | break; |
| 493 | case 0x7: // Channel volume ramp start register EEEEMMMM |
| 494 | if(curchan != NULL) { |
| 495 | Bit8u tmpdata = (Bit16u)myGUS.gRegData >> 8; |
| 496 | curchan->RampStart = tmpdata << (4+RAMP_FRACT); |
| 497 | } |
| 498 | break; |
| 499 | case 0x8: // Channel volume ramp end register EEEEMMMM |
| 500 | if(curchan != NULL) { |
| 501 | Bit8u tmpdata = (Bit16u)myGUS.gRegData >> 8; |
| 502 | curchan->RampEnd = tmpdata << (4+RAMP_FRACT); |
| 503 | } |
| 504 | break; |
| 505 | case 0x9: // Channel current volume register |
| 506 | if(curchan != NULL) { |
| 507 | Bit16u tmpdata = (Bit16u)myGUS.gRegData >> 4; |
| 508 | curchan->RampVol = tmpdata << RAMP_FRACT; |
| 509 | curchan->UpdateVolumes(); |
| 510 | } |
| 511 | break; |
| 512 | case 0xA: // Channel MSW current address register |
| 513 | if(curchan != NULL) { |
| 514 | Bit32u tmpaddr = (Bit32u)(myGUS.gRegData & 0x1fff) << (16+WAVE_BITS); |
| 515 | curchan->WaveAddr = (curchan->WaveAddr & WAVE_MSWMASK) | tmpaddr; |
| 516 | } |
| 517 | break; |
| 518 | case 0xB: // Channel LSW current address register |
| 519 | if(curchan != NULL) { |
| 520 | Bit32u tmpaddr = (Bit32u)(myGUS.gRegData) << (WAVE_BITS); |
| 521 | curchan->WaveAddr = (curchan->WaveAddr & WAVE_LSWMASK) | tmpaddr; |
| 522 | } |
| 523 | break; |
| 524 | case 0xC: // Channel pan pot register |
| 525 | if(curchan) curchan->WritePanPot((Bit16u)myGUS.gRegData>>8); |
| 526 | break; |
| 527 | case 0xD: // Channel volume control register |
| 528 | if(curchan) curchan->WriteRampCtrl((Bit16u)myGUS.gRegData>>8); |
| 529 | break; |
| 530 | case 0xE: // Set active channel register |
| 531 | myGUS.gRegSelect = myGUS.gRegData>>8; //JAZZ Jackrabbit seems to assume this? |
| 532 | myGUS.ActiveChannels = 1+((myGUS.gRegData>>8) & 63); |
| 533 | if(myGUS.ActiveChannels < 14) myGUS.ActiveChannels = 14; |
| 534 | if(myGUS.ActiveChannels > 32) myGUS.ActiveChannels = 32; |
| 535 | myGUS.ActiveMask=0xffffffffU >> (32-myGUS.ActiveChannels); |
| 536 | gus_chan->Enable(true); |
| 537 | myGUS.basefreq = (Bit32u)((float)1000000/(1.619695497*(float)(myGUS.ActiveChannels))); |
| 538 | #if LOG_GUS |
| 539 | LOG_MSG("GUS set to %d channels", myGUS.ActiveChannels); |
| 540 | #endif |
| 541 | for (i=0;i<myGUS.ActiveChannels;i++) guschan[i]->UpdateWaveRamp(); |
| 542 | break; |
| 543 | case 0x10: // Undocumented register used in Fast Tracker 2 |
| 544 | break; |
| 545 | case 0x41: // Dma control register |
| 546 | myGUS.DMAControl = (Bit8u)(myGUS.gRegData>>8); |
| 547 | GetDMAChannel(myGUS.dma1)->Register_Callback( |
| 548 | (myGUS.DMAControl & 0x1) ? GUS_DMA_Callback : 0); |
| 549 | break; |
| 550 | case 0x42: // Gravis DRAM DMA address register |
| 551 | myGUS.dmaAddr = myGUS.gRegData; |
| 552 | break; |
| 553 | case 0x43: // MSB Peek/poke DRAM position |
| 554 | myGUS.gDramAddr = (0xff0000 & myGUS.gDramAddr) | ((Bit32u)myGUS.gRegData); |
| 555 | break; |
| 556 | case 0x44: // LSW Peek/poke DRAM position |
| 557 | myGUS.gDramAddr = (0xffff & myGUS.gDramAddr) | ((Bit32u)myGUS.gRegData>>8) << 16; |
| 558 | break; |
| 559 | case 0x45: // Timer control register. Identical in operation to Adlib's timer |
| 560 | myGUS.TimerControl = (Bit8u)(myGUS.gRegData>>8); |
| 561 | myGUS.timers[0].raiseirq=(myGUS.TimerControl & 0x04)>0; |
| 562 | if (!myGUS.timers[0].raiseirq) myGUS.IRQStatus&=~0x04; |
| 563 | myGUS.timers[1].raiseirq=(myGUS.TimerControl & 0x08)>0; |
| 564 | if (!myGUS.timers[1].raiseirq) myGUS.IRQStatus&=~0x08; |
| 565 | break; |
| 566 | case 0x46: // Timer 1 control |
| 567 | myGUS.timers[0].value = (Bit8u)(myGUS.gRegData>>8); |
| 568 | myGUS.timers[0].delay = (0x100 - myGUS.timers[0].value) * 0.080f; |
| 569 | break; |
| 570 | case 0x47: // Timer 2 control |
| 571 | myGUS.timers[1].value = (Bit8u)(myGUS.gRegData>>8); |
| 572 | myGUS.timers[1].delay = (0x100 - myGUS.timers[1].value) * 0.320f; |
| 573 | break; |
| 574 | case 0x49: // DMA sampling control register |
| 575 | myGUS.SampControl = (Bit8u)(myGUS.gRegData>>8); |
| 576 | GetDMAChannel(myGUS.dma1)->Register_Callback( |
| 577 | (myGUS.SampControl & 0x1) ? GUS_DMA_Callback : 0); |
| 578 | break; |
| 579 | case 0x4c: // GUS reset register |
| 580 | GUSReset(); |
| 581 | break; |
| 582 | default: |
| 583 | #if LOG_GUS |
| 584 | LOG_MSG("Unimplemented global register %x -- %x", myGUS.gRegSelect, myGUS.gRegData); |
| 585 | #endif |
| 586 | break; |
| 587 | } |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | |
| 592 | static Bitu read_gus(Bitu port,Bitu iolen) { |
| 593 | // LOG_MSG("read from gus port %x",port); |
| 594 | switch(port - GUS_BASE) { |
| 595 | case 0x206: |
| 596 | return myGUS.IRQStatus; |
| 597 | case 0x208: |
| 598 | Bit8u tmptime; |
| 599 | tmptime = 0; |
| 600 | if (myGUS.timers[0].reached) tmptime |= (1 << 6); |
| 601 | if (myGUS.timers[1].reached) tmptime |= (1 << 5); |
| 602 | if (tmptime & 0x60) tmptime |= (1 << 7); |
| 603 | if (myGUS.IRQStatus & 0x04) tmptime|=(1 << 2); |
| 604 | if (myGUS.IRQStatus & 0x08) tmptime|=(1 << 1); |
| 605 | return tmptime; |
| 606 | case 0x20a: |
| 607 | return adlib_commandreg; |
| 608 | case 0x302: |
| 609 | return (Bit8u)myGUS.gCurChannel; |
| 610 | case 0x303: |
| 611 | return myGUS.gRegSelect; |
| 612 | case 0x304: |
| 613 | if (iolen==2) return ExecuteReadRegister() & 0xffff; |
| 614 | else return ExecuteReadRegister() & 0xff; |
| 615 | case 0x305: |
| 616 | return ExecuteReadRegister() >> 8; |
| 617 | case 0x307: |
| 618 | if(myGUS.gDramAddr < sizeof(GUSRam)) { |
| 619 | return GUSRam[myGUS.gDramAddr]; |
| 620 | } else { |
| 621 | return 0; |
| 622 | } |
| 623 | default: |
| 624 | #if LOG_GUS |
| 625 | LOG_MSG("Read GUS at port 0x%x", port); |
| 626 | #endif |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | return 0xff; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | static void write_gus(Bitu port,Bitu val,Bitu iolen) { |
| 635 | // LOG_MSG("Write gus port %x val %x",port,val); |
| 636 | switch(port - GUS_BASE) { |
| 637 | case 0x200: |
| 638 | myGUS.mixControl = (Bit8u)val; |
| 639 | myGUS.ChangeIRQDMA = true; |
| 640 | return; |
| 641 | case 0x208: |
| 642 | adlib_commandreg = (Bit8u)val; |
| 643 | break; |
| 644 | case 0x209: |
| 645 | //TODO adlib_commandreg should be 4 for this to work else it should just latch the value |
| 646 | if (val & 0x80) { |
| 647 | myGUS.timers[0].reached=false; |
| 648 | myGUS.timers[1].reached=false; |
| 649 | return; |
| 650 | } |
| 651 | myGUS.timers[0].masked=(val & 0x40)>0; |
| 652 | myGUS.timers[1].masked=(val & 0x20)>0; |
| 653 | if (val & 0x1) { |
| 654 | if (!myGUS.timers[0].running) { |
| 655 | PIC_AddEvent(GUS_TimerEvent,myGUS.timers[0].delay,0); |
| 656 | myGUS.timers[0].running=true; |
| 657 | } |
| 658 | } else myGUS.timers[0].running=false; |
| 659 | if (val & 0x2) { |
| 660 | if (!myGUS.timers[1].running) { |
| 661 | PIC_AddEvent(GUS_TimerEvent,myGUS.timers[1].delay,1); |
| 662 | myGUS.timers[1].running=true; |
| 663 | } |
| 664 | } else myGUS.timers[1].running=false; |
| 665 | break; |
| 666 | //TODO Check if 0x20a register is also available on the gus like on the interwave |
| 667 | case 0x20b: |
| 668 | if (!myGUS.ChangeIRQDMA) break; |
| 669 | myGUS.ChangeIRQDMA=false; |
| 670 | if (myGUS.mixControl & 0x40) { |
| 671 | // IRQ configuration, only use low bits for irq 1 |
| 672 | if (irqtable[val & 0x7]) myGUS.irq1=irqtable[val & 0x7]; |
| 673 | #if LOG_GUS |
| 674 | LOG_MSG("Assigned GUS to IRQ %d", myGUS.irq1); |
| 675 | #endif |
| 676 | } else { |
| 677 | // DMA configuration, only use low bits for dma 1 |
| 678 | if (dmatable[val & 0x7]) myGUS.dma1=dmatable[val & 0x7]; |
| 679 | #if LOG_GUS |
| 680 | LOG_MSG("Assigned GUS to DMA %d", myGUS.dma1); |
| 681 | #endif |
| 682 | } |
| 683 | break; |
| 684 | case 0x302: |
| 685 | myGUS.gCurChannel = val & 31 ; |
| 686 | curchan = guschan[myGUS.gCurChannel]; |
| 687 | break; |
| 688 | case 0x303: |
| 689 | myGUS.gRegSelect = (Bit8u)val; |
| 690 | myGUS.gRegData = 0; |
| 691 | break; |
| 692 | case 0x304: |
| 693 | if (iolen==2) { |
| 694 | myGUS.gRegData=(Bit16u)val; |
| 695 | ExecuteGlobRegister(); |
| 696 | } else myGUS.gRegData = (Bit16u)val; |
| 697 | break; |
| 698 | case 0x305: |
| 699 | myGUS.gRegData = (Bit16u)((0x00ff & myGUS.gRegData) | val << 8); |
| 700 | ExecuteGlobRegister(); |
| 701 | break; |
| 702 | case 0x307: |
| 703 | if(myGUS.gDramAddr < sizeof(GUSRam)) GUSRam[myGUS.gDramAddr] = (Bit8u)val; |
| 704 | break; |
| 705 | default: |
| 706 | #if LOG_GUS |
| 707 | LOG_MSG("Write GUS at port 0x%x with %x", port, val); |
| 708 | #endif |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | static void GUS_DMA_Callback(DmaChannel * chan,DMAEvent event) { |
| 714 | if (event!=DMA_UNMASKED) return; |
| 715 | Bitu dmaaddr = myGUS.dmaAddr << 4; |
| 716 | if((myGUS.DMAControl & 0x2) == 0) { |
| 717 | Bitu read=chan->Read(chan->currcnt+1,&GUSRam[dmaaddr]); |
| 718 | //Check for 16 or 8bit channel |
| 719 | read*=(chan->DMA16+1); |
| 720 | if((myGUS.DMAControl & 0x80) != 0) { |
| 721 | //Invert the MSB to convert twos compliment form |
| 722 | Bitu i; |
| 723 | if((myGUS.DMAControl & 0x40) == 0) { |
| 724 | // 8-bit data |
| 725 | for(i=dmaaddr;i<(dmaaddr+read);i++) GUSRam[i] ^= 0x80; |
| 726 | } else { |
| 727 | // 16-bit data |
| 728 | for(i=dmaaddr+1;i<(dmaaddr+read);i+=2) GUSRam[i] ^= 0x80; |
| 729 | } |
| 730 | } |
| 731 | } else { |
| 732 | //Read data out of UltraSound |
| 733 | chan->Write(chan->currcnt+1,&GUSRam[dmaaddr]); |
| 734 | } |
| 735 | /* Raise the TC irq if needed */ |
| 736 | if((myGUS.DMAControl & 0x20) != 0) { |
| 737 | myGUS.IRQStatus |= 0x80; |
| 738 | GUS_CheckIRQ(); |
| 739 | } |
| 740 | chan->Register_Callback(0); |
| 741 | } |
| 742 | |
| 743 | static void GUS_CallBack(Bitu len) { |
| 744 | memset(&MixTemp,0,len*8); |
| 745 | Bitu i; |
| 746 | Bit16s * buf16 = (Bit16s *)MixTemp; |
| 747 | Bit32s * buf32 = (Bit32s *)MixTemp; |
| 748 | for(i=0;i<myGUS.ActiveChannels;i++) |
| 749 | guschan[i]->generateSamples(buf32,len); |
| 750 | for(i=0;i<len*2;i++) { |
| 751 | Bit32s sample=((buf32[i] >> 13)*AutoAmp)>>9; |
| 752 | if (sample>32767) { |
| 753 | sample=32767; |
| 754 | AutoAmp--; |
| 755 | } else if (sample<-32768) { |
| 756 | sample=-32768; |
| 757 | AutoAmp--; |
| 758 | } |
| 759 | buf16[i] = (Bit16s)(sample); |
| 760 | } |
| 761 | gus_chan->AddSamples_s16(len,buf16); |
| 762 | CheckVoiceIrq(); |
| 763 | } |
| 764 | |
| 765 | // Generate logarithmic to linear volume conversion tables |
| 766 | static void MakeTables(void) { |
| 767 | int i; |
| 768 | double out = (double)(1 << 13); |
| 769 | for (i=4095;i>=0;i--) { |
| 770 | vol16bit[i]=(Bit16s)out; |
| 771 | out/=1.002709201; /* 0.0235 dB Steps */ |
| 772 | } |
| 773 | pantable[0]=0; |
| 774 | for (i=1;i<16;i++) { |
| 775 | pantable[i]=(Bit32u)(-128.0*(log((double)i/15.0)/log(2.0))*(double)(1 << RAMP_FRACT)); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | class GUS:public Module_base{ |
| 780 | private: |
| 781 | IO_ReadHandleObject ReadHandler[8]; |
| 782 | IO_WriteHandleObject WriteHandler[9]; |
| 783 | AutoexecObject autoexecline[2]; |
| 784 | MixerObject MixerChan; |
| 785 | public: |
| 786 | GUS(Section* configuration):Module_base(configuration){ |
| 787 | if(!IS_EGAVGA_ARCH) return; |
| 788 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 789 | if(!section->Get_bool("gus")) return; |
| 790 | |
| 791 | memset(&myGUS,0,sizeof(myGUS)); |
| 792 | memset(GUSRam,0,1024*1024); |
| 793 | |
| 794 | myGUS.rate=section->Get_int("gusrate"); |
| 795 | |
| 796 | myGUS.portbase = section->Get_hex("gusbase") - 0x200; |
| 797 | int dma_val = section->Get_int("gusdma"); |
| 798 | if ((dma_val<0) || (dma_val>255)) dma_val = 3; // sensible default |
| 799 | int irq_val = section->Get_int("gusirq"); |
| 800 | if ((irq_val<0) || (irq_val>255)) irq_val = 5; // sensible default |
| 801 | myGUS.dma1 = (Bit8u)dma_val; |
| 802 | myGUS.dma2 = (Bit8u)dma_val; |
| 803 | myGUS.irq1 = (Bit8u)irq_val; |
| 804 | myGUS.irq2 = (Bit8u)irq_val; |
| 805 | |
| 806 | // We'll leave the MIDI interface to the MPU-401 |
| 807 | // Ditto for the Joystick |
| 808 | // GF1 Synthesizer |
| 809 | ReadHandler[0].Install(0x302 + GUS_BASE,read_gus,IO_MB); |
| 810 | WriteHandler[0].Install(0x302 + GUS_BASE,write_gus,IO_MB); |
| 811 | |
| 812 | WriteHandler[1].Install(0x303 + GUS_BASE,write_gus,IO_MB); |
| 813 | ReadHandler[1].Install(0x303 + GUS_BASE,read_gus,IO_MB); |
| 814 | |
| 815 | WriteHandler[2].Install(0x304 + GUS_BASE,write_gus,IO_MB|IO_MW); |
| 816 | ReadHandler[2].Install(0x304 + GUS_BASE,read_gus,IO_MB|IO_MW); |
| 817 | |
| 818 | WriteHandler[3].Install(0x305 + GUS_BASE,write_gus,IO_MB); |
| 819 | ReadHandler[3].Install(0x305 + GUS_BASE,read_gus,IO_MB); |
| 820 | |
| 821 | ReadHandler[4].Install(0x206 + GUS_BASE,read_gus,IO_MB); |
| 822 | |
| 823 | WriteHandler[4].Install(0x208 + GUS_BASE,write_gus,IO_MB); |
| 824 | ReadHandler[5].Install(0x208 + GUS_BASE,read_gus,IO_MB); |
| 825 | |
| 826 | WriteHandler[5].Install(0x209 + GUS_BASE,write_gus,IO_MB); |
| 827 | |
| 828 | WriteHandler[6].Install(0x307 + GUS_BASE,write_gus,IO_MB); |
| 829 | ReadHandler[6].Install(0x307 + GUS_BASE,read_gus,IO_MB); |
| 830 | |
| 831 | // Board Only |
| 832 | |
| 833 | WriteHandler[7].Install(0x200 + GUS_BASE,write_gus,IO_MB); |
| 834 | ReadHandler[7].Install(0x20A + GUS_BASE,read_gus,IO_MB); |
| 835 | WriteHandler[8].Install(0x20B + GUS_BASE,write_gus,IO_MB); |
| 836 | |
| 837 | // DmaChannels[myGUS.dma1]->Register_TC_Callback(GUS_DMA_TC_Callback); |
| 838 | |
| 839 | MakeTables(); |
| 840 | |
| 841 | for (Bit8u chan_ct=0; chan_ct<32; chan_ct++) { |
| 842 | guschan[chan_ct] = new GUSChannels(chan_ct); |
| 843 | } |
| 844 | // Register the Mixer CallBack |
| 845 | gus_chan=MixerChan.Install(GUS_CallBack,GUS_RATE,"GUS"); |
| 846 | myGUS.gRegData=0x1; |
| 847 | GUSReset(); |
| 848 | myGUS.gRegData=0x0; |
| 849 | int portat = 0x200+GUS_BASE; |
| 850 | |
| 851 | // ULTRASND=Port,DMA1,DMA2,IRQ1,IRQ2 |
| 852 | // [GUS port], [GUS DMA (recording)], [GUS DMA (playback)], [GUS IRQ (playback)], [GUS IRQ (MIDI)] |
| 853 | ostringstream temp; |
| 854 | temp << "SET ULTRASND=" << hex << setw(3) << portat << "," |
| 855 | << dec << (Bitu)myGUS.dma1 << "," << (Bitu)myGUS.dma2 << "," |
| 856 | << (Bitu)myGUS.irq1 << "," << (Bitu)myGUS.irq2 << ends; |
| 857 | // Create autoexec.bat lines |
| 858 | autoexecline[0].Install(temp.str()); |
| 859 | autoexecline[1].Install(std::string("SET ULTRADIR=") + section->Get_string("ultradir")); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | ~GUS() { |
| 864 | if(!IS_EGAVGA_ARCH) return; |
| 865 | Section_prop * section=static_cast<Section_prop *>(m_configuration); |
| 866 | if(!section->Get_bool("gus")) return; |
| 867 | |
| 868 | myGUS.gRegData=0x1; |
| 869 | GUSReset(); |
| 870 | myGUS.gRegData=0x0; |
| 871 | |
| 872 | for(Bitu i=0;i<32;i++) { |
| 873 | delete guschan[i]; |
| 874 | } |
| 875 | |
| 876 | memset(&myGUS,0,sizeof(myGUS)); |
| 877 | memset(GUSRam,0,1024*1024); |
| 878 | } |
| 879 | }; |
| 880 | |
| 881 | static GUS* test; |
| 882 | |
| 883 | void GUS_ShutDown(Section* /*sec*/) { |
| 884 | delete test; |
| 885 | } |
| 886 | |
| 887 | void GUS_Init(Section* sec) { |
| 888 | test = new GUS(sec); |
| 889 | sec->AddDestroyFunction(&GUS_ShutDown,true); |
| 890 | } |