2 * GUSEMU32 - bus interface part
4 * Copyright (C) 2000-2007 Tibor "TS" Schütz
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * TODO: check mixer: see 7.20 of sdk for panning pos (applies to all gus models?)?
29 #include "qemu/osdep.h"
33 #define GUSregb(position) (* (gusptr+(position)))
34 #define GUSregw(position) (*(GUSword *) (gusptr+(position)))
35 #define GUSregd(position) (*(GUSdword *)(gusptr+(position)))
37 /* size given in bytes */
38 unsigned int gus_read(GUSEmuState
* state
, int port
, int size
)
43 gusptr
= state
->gusdatapos
;
44 GUSregd(portaccesses
)++;
46 switch (port
& 0xff0f)
48 /* MixerCtrlReg (read not supported on GUS classic) */
49 /* case 0x200: return GUSregb(MixerCtrlReg2x0); */
50 case 0x206: /* IRQstatReg / SB2x6IRQ */
51 /* adlib/sb bits set in port handlers */
52 /* timer/voice bits set in gus_irqgen() */
53 /* dma bit set in gus_dma_transferdata */
54 /* midi not implemented yet */
55 return GUSregb(IRQStatReg2x6
);
56 /* case 0x308: */ /* AdLib388 */
58 if (GUSregb(GUS45TimerCtrl
) & 1)
59 return GUSregb(TimerStatus2x8
);
60 return GUSregb(AdLibStatus2x8
); /* AdLibStatus */
61 case 0x309: /* AdLib389 */
63 return GUSregb(AdLibData2x9
); /* AdLibData */
65 return GUSregb(AdLibCommand2xA
); /* AdLib2x8_2xA */
68 case 0x20B: /* GUS hidden registers (read not supported on GUS classic) */
69 switch (GUSregb(RegCtrl_2xF
) & 0x07)
71 case 0: /* IRQ/DMA select */
72 if (GUSregb(MixerCtrlReg2x0
) & 0x40)
73 return GUSregb(IRQ_2xB
); /* control register select bit */
75 return GUSregb(DMA_2xB
);
76 /* case 1-5: */ /* general purpose emulation regs */
77 /* return ... */ /* + status reset reg (write only) */
79 return GUSregb(Jumper_2xB
); /* Joystick/MIDI enable (JumperReg) */
85 case 0x20C: /* SB2xCd */
86 value_read
= GUSregb(SB2xCd
);
87 if (GUSregb(StatRead_2xF
) & 0x20)
88 GUSregb(SB2xCd
) ^= 0x80; /* toggle MSB on read */
90 /* case 0x20D: */ /* SB2xD is write only -> 2xE writes to it*/
92 if (GUSregb(RegCtrl_2xF
) & 0x80) /* 2xE read IRQ enabled? */
94 GUSregb(StatRead_2xF
) |= 0x80;
95 GUS_irqrequest(state
, state
->gusirq
, 1);
97 return GUSregb(SB2xE
); /* SB2xE */
98 case 0x20F: /* StatRead_2xF */
99 /*set/clear fixed bits */
100 /*value_read = (GUSregb(StatRead_2xF) & 0xf9)|1; */ /*(LSB not set on GUS classic!)*/
101 value_read
= (GUSregb(StatRead_2xF
) & 0xf9);
102 if (GUSregb(MixerCtrlReg2x0
) & 0x08)
103 value_read
|= 2; /* DMA/IRQ enabled flag */
105 /* case 0x300: */ /* MIDI (not implemented) */
106 /* case 0x301: */ /* MIDI (not implemented) */
108 return GUSregb(VoiceSelReg3x2
); /* VoiceSelReg */
110 return GUSregb(FunkSelReg3x3
); /* FunkSelReg */
111 case 0x304: /* DataRegLoByte3x4 + DataRegWord3x4 */
112 case 0x305: /* DataRegHiByte3x5 */
113 switch (GUSregb(FunkSelReg3x3
))
115 /* common functions */
116 case 0x41: /* DramDMAContrReg */
117 value_read
= GUSregb(GUS41DMACtrl
); /* &0xfb */
118 GUSregb(GUS41DMACtrl
) &= 0xbb;
119 if (state
->gusdma
>= 4)
121 if (GUSregb(IRQStatReg2x6
) & 0x80)
124 GUSregb(IRQStatReg2x6
) &= 0x7f;
125 if (!GUSregb(IRQStatReg2x6
))
126 GUS_irqclear(state
, state
->gusirq
);
128 return (GUSbyte
) value_read
;
129 /* DramDMAmemPosReg */
130 /* case 0x42: value_read=GUSregw(GUS42DMAStart); break;*/
131 /* 43h+44h write only */
133 return GUSregb(GUS45TimerCtrl
); /* TimerCtrlReg */
134 /* 46h+47h write only */
135 /* 48h: samp freq - write only */
137 return GUSregb(GUS49SampCtrl
) & 0xbf; /* SampCtrlReg */
138 /* case 4bh: */ /* joystick trim not supported */
139 /* case 0x4c: return GUSregb(GUS4cReset); */ /* GUSreset: write only*/
140 /* voice specific functions */
156 int offset
= 2 * (GUSregb(FunkSelReg3x3
) & 0x0f);
157 offset
+= ((int) GUSregb(VoiceSelReg3x2
) & 0x1f) << 5; /* = Voice*32 + Funktion*2 */
158 value_read
= GUSregw(offset
);
161 /* voice unspecific functions */
162 case 0x8e: /* NumVoice */
163 return GUSregb(NumVoices
);
164 case 0x8f: /* irqstatreg */
165 /* (pseudo IRQ-FIFO is processed during a gus_write(0x3X3,0x8f)) */
166 return GUSregb(SynVoiceIRQ8f
);
172 if ((port
& 0xff0f) == 0x305)
173 value_read
= value_read
>> 8;
176 return (GUSword
) value_read
;
177 /* case 0x306: */ /* Mixer/Version info */
178 /* return 0xff; */ /* Pre 3.6 boards, ICS mixer NOT present */
179 case 0x307: /* DRAMaccess */
182 adr
= state
->himemaddr
+ (GUSregd(GUSDRAMPOS24bit
) & 0xfffff);
190 void gus_write(GUSEmuState
* state
, int port
, int size
, unsigned int data
)
193 gusptr
= state
->gusdatapos
;
194 GUSregd(portaccesses
)++;
196 switch (port
& 0xff0f)
198 case 0x200: /* MixerCtrlReg */
199 GUSregb(MixerCtrlReg2x0
) = (GUSbyte
) data
;
201 case 0x206: /* IRQstatReg / SB2x6IRQ */
202 if (GUSregb(GUS45TimerCtrl
) & 0x20) /* SB IRQ enabled? -> set 2x6IRQ bit */
204 GUSregb(TimerStatus2x8
) |= 0x08;
205 GUSregb(IRQStatReg2x6
) = 0x10;
206 GUS_irqrequest(state
, state
->gusirq
, 1);
209 case 0x308: /* AdLib 388h */
210 case 0x208: /* AdLibCommandReg */
211 GUSregb(AdLibCommand2xA
) = (GUSbyte
) data
;
213 case 0x309: /* AdLib 389h */
214 case 0x209: /* AdLibDataReg */
215 if ((GUSregb(AdLibCommand2xA
) == 0x04) && (!(GUSregb(GUS45TimerCtrl
) & 1))) /* GUS auto timer mode enabled? */
218 GUSregb(TimerStatus2x8
) &= 0x1f; /* AdLib IRQ reset? -> clear maskable adl. timer int regs */
220 GUSregb(TimerDataReg2x9
) = (GUSbyte
) data
;
224 GUSregb(AdLibData2x9
) = (GUSbyte
) data
;
225 if (GUSregb(GUS45TimerCtrl
) & 0x02)
227 GUSregb(TimerStatus2x8
) |= 0x01;
228 GUSregb(IRQStatReg2x6
) = 0x10;
229 GUS_irqrequest(state
, state
->gusirq
, 1);
234 GUSregb(AdLibStatus2x8
) = (GUSbyte
) data
;
235 break; /* AdLibStatus2x8 */
236 case 0x20B: /* GUS hidden registers */
237 switch (GUSregb(RegCtrl_2xF
) & 0x7)
240 if (GUSregb(MixerCtrlReg2x0
) & 0x40)
241 GUSregb(IRQ_2xB
) = (GUSbyte
) data
; /* control register select bit */
243 GUSregb(DMA_2xB
) = (GUSbyte
) data
;
245 /* case 1-4: general purpose emulation regs */
246 case 5: /* clear stat reg 2xF */
247 GUSregb(StatRead_2xF
) = 0; /* ToDo: is this identical with GUS classic? */
248 if (!GUSregb(IRQStatReg2x6
))
249 GUS_irqclear(state
, state
->gusirq
);
251 case 6: /* Jumper reg (Joystick/MIDI enable) */
252 GUSregb(Jumper_2xB
) = (GUSbyte
) data
;
257 case 0x20C: /* SB2xCd */
258 if (GUSregb(GUS45TimerCtrl
) & 0x20)
260 GUSregb(TimerStatus2x8
) |= 0x10; /* SB IRQ enabled? -> set 2xCIRQ bit */
261 GUSregb(IRQStatReg2x6
) = 0x10;
262 GUS_irqrequest(state
, state
->gusirq
, 1);
264 case 0x20D: /* SB2xCd no IRQ */
265 GUSregb(SB2xCd
) = (GUSbyte
) data
;
267 case 0x20E: /* SB2xE */
268 GUSregb(SB2xE
) = (GUSbyte
) data
;
271 GUSregb(RegCtrl_2xF
) = (GUSbyte
) data
;
272 break; /* CtrlReg2xF */
273 case 0x302: /* VoiceSelReg */
274 GUSregb(VoiceSelReg3x2
) = (GUSbyte
) data
;
276 case 0x303: /* FunkSelReg */
277 GUSregb(FunkSelReg3x3
) = (GUSbyte
) data
;
278 if ((GUSbyte
) data
== 0x8f) /* set irqstatreg, get voicereg and clear IRQ */
281 if (GUSregd(voicewavetableirq
)) /* WavetableIRQ */
283 for (voice
= 0; voice
< 31; voice
++)
285 if (GUSregd(voicewavetableirq
) & (1 << voice
))
287 GUSregd(voicewavetableirq
) ^= (1 << voice
); /* clear IRQ bit */
288 GUSregb(voice
<< 5) &= 0x7f; /* clear voice reg irq bit */
289 if (!GUSregd(voicewavetableirq
))
290 GUSregb(IRQStatReg2x6
) &= 0xdf;
291 if (!GUSregb(IRQStatReg2x6
))
292 GUS_irqclear(state
, state
->gusirq
);
293 GUSregb(SynVoiceIRQ8f
) = voice
| 0x60; /* (bit==0 => IRQ wartend) */
298 else if (GUSregd(voicevolrampirq
)) /* VolRamp IRQ */
300 for (voice
= 0; voice
< 31; voice
++)
302 if (GUSregd(voicevolrampirq
) & (1 << voice
))
304 GUSregd(voicevolrampirq
) ^= (1 << voice
); /* clear IRQ bit */
305 GUSregb((voice
<< 5) + VSRVolRampControl
) &= 0x7f; /* clear voice volume reg irq bit */
306 if (!GUSregd(voicevolrampirq
))
307 GUSregb(IRQStatReg2x6
) &= 0xbf;
308 if (!GUSregb(IRQStatReg2x6
))
309 GUS_irqclear(state
, state
->gusirq
);
310 GUSregb(SynVoiceIRQ8f
) = voice
| 0x80; /* (bit==0 => IRQ wartend) */
315 GUSregb(SynVoiceIRQ8f
) = 0xe8; /* kein IRQ wartet */
321 GUSword writedata
= (GUSword
) data
;
322 GUSword readmask
= 0x0000;
327 if ((port
& 0xff0f) == 0x305)
329 writedata
= (GUSword
) (writedata
<< 8);
333 switch (GUSregb(FunkSelReg3x3
))
335 /* voice specific functions */
352 if (!(GUSregb(GUS4cReset
) & 0x01))
353 break; /* reset flag active? */
354 offset
= 2 * (GUSregb(FunkSelReg3x3
) & 0x0f);
355 offset
+= (GUSregb(VoiceSelReg3x2
) & 0x1f) << 5; /* = Voice*32 + Funktion*2 */
356 GUSregw(offset
) = (GUSword
) ((GUSregw(offset
) & readmask
) | writedata
);
359 /* voice unspecific functions */
360 case 0x0e: /* NumVoices */
361 GUSregb(NumVoices
) = (GUSbyte
) data
;
363 /* case 0x0f: */ /* read only */
364 /* common functions */
365 case 0x41: /* DramDMAContrReg */
366 GUSregb(GUS41DMACtrl
) = (GUSbyte
) data
;
368 GUS_dmarequest(state
);
370 case 0x42: /* DramDMAmemPosReg */
371 GUSregw(GUS42DMAStart
) = (GUSregw(GUS42DMAStart
) & readmask
) | writedata
;
372 GUSregb(GUS50DMAHigh
) &= 0xf; /* compatibility stuff... */
374 case 0x43: /* DRAMaddrLo */
375 GUSregd(GUSDRAMPOS24bit
) =
376 (GUSregd(GUSDRAMPOS24bit
) & (readmask
| 0xff0000)) | writedata
;
378 case 0x44: /* DRAMaddrHi */
379 GUSregd(GUSDRAMPOS24bit
) =
380 (GUSregd(GUSDRAMPOS24bit
) & 0xffff) | ((data
& 0x0f) << 16);
382 case 0x45: /* TCtrlReg */
383 GUSregb(GUS45TimerCtrl
) = (GUSbyte
) data
;
385 GUSregb(TimerStatus2x8
) &= 0xe7; /* sb IRQ dis? -> clear 2x8/2xC sb IRQ flags */
387 GUSregb(TimerStatus2x8
) &= 0xfe; /* adlib data IRQ dis? -> clear 2x8 adlib IRQ flag */
388 if (!(GUSregb(TimerStatus2x8
) & 0x19))
389 GUSregb(IRQStatReg2x6
) &= 0xef; /* 0xe6; $$clear IRQ if both IRQ bits are inactive or cleared */
390 /* catch up delayed timer IRQs: */
391 if ((GUSregw(TimerIRQs
) > 1) && (GUSregb(TimerDataReg2x9
) & 3))
393 if (GUSregb(TimerDataReg2x9
) & 1) /* start timer 1 (80us decrement rate) */
395 if (!(GUSregb(TimerDataReg2x9
) & 0x40))
396 GUSregb(TimerStatus2x8
) |= 0xc0; /* maskable bits */
397 if (data
& 4) /* timer1 irq enable */
399 GUSregb(TimerStatus2x8
) |= 4; /* nonmaskable bit */
400 GUSregb(IRQStatReg2x6
) |= 4; /* timer 1 irq pending */
403 if (GUSregb(TimerDataReg2x9
) & 2) /* start timer 2 (320us decrement rate) */
405 if (!(GUSregb(TimerDataReg2x9
) & 0x20))
406 GUSregb(TimerStatus2x8
) |= 0xa0; /* maskable bits */
407 if (data
& 8) /* timer2 irq enable */
409 GUSregb(TimerStatus2x8
) |= 2; /* nonmaskable bit */
410 GUSregb(IRQStatReg2x6
) |= 8; /* timer 2 irq pending */
413 GUSregw(TimerIRQs
)--;
414 if (GUSregw(BusyTimerIRQs
) > 1)
415 GUSregw(BusyTimerIRQs
)--;
417 GUSregw(BusyTimerIRQs
) =
418 GUS_irqrequest(state
, state
->gusirq
, GUSregw(TimerIRQs
));
421 GUSregw(TimerIRQs
) = 0;
425 GUSregb(TimerStatus2x8
) &= 0xfb; /* clear non-maskable timer1 bit */
426 GUSregb(IRQStatReg2x6
) &= 0xfb;
430 GUSregb(TimerStatus2x8
) &= 0xfd; /* clear non-maskable timer2 bit */
431 GUSregb(IRQStatReg2x6
) &= 0xf7;
433 if (!GUSregb(IRQStatReg2x6
))
434 GUS_irqclear(state
, state
->gusirq
);
436 case 0x46: /* Counter1 */
437 GUSregb(GUS46Counter1
) = (GUSbyte
) data
;
439 case 0x47: /* Counter2 */
440 GUSregb(GUS47Counter2
) = (GUSbyte
) data
;
442 /* case 0x48: */ /* sampling freq reg not emulated (same as interwave) */
443 case 0x49: /* SampCtrlReg */
444 GUSregb(GUS49SampCtrl
) = (GUSbyte
) data
;
446 /* case 0x4b: */ /* joystick trim not emulated */
447 case 0x4c: /* GUSreset */
448 GUSregb(GUS4cReset
) = (GUSbyte
) data
;
449 if (!(GUSregb(GUS4cReset
) & 1)) /* reset... */
451 GUSregd(voicewavetableirq
) = 0;
452 GUSregd(voicevolrampirq
) = 0;
453 GUSregw(TimerIRQs
) = 0;
454 GUSregw(BusyTimerIRQs
) = 0;
455 GUSregb(NumVoices
) = 0xcd;
456 GUSregb(IRQStatReg2x6
) = 0;
457 GUSregb(TimerStatus2x8
) = 0;
458 GUSregb(AdLibData2x9
) = 0;
459 GUSregb(TimerDataReg2x9
) = 0;
460 GUSregb(GUS41DMACtrl
) = 0;
461 GUSregb(GUS45TimerCtrl
) = 0;
462 GUSregb(GUS49SampCtrl
) = 0;
463 GUSregb(GUS4cReset
) &= 0xf9; /* clear IRQ and DAC enable bits */
464 GUS_irqclear(state
, state
->gusirq
);
466 /* IRQ enable bit checked elsewhere */
467 /* EnableDAC bit may be used by external callers */
472 case 0x307: /* DRAMaccess */
475 adr
= state
->himemaddr
+ (GUSregd(GUSDRAMPOS24bit
) & 0xfffff);
476 *adr
= (GUSbyte
) data
;
482 /* Attention when breaking up a single DMA transfer to multiple ones:
483 * it may lead to multiple terminal count interrupts and broken transfers:
485 * 1. Whenever you transfer a piece of data, the gusemu callback is invoked
486 * 2. The callback may generate a TC irq (if the register was set up to do so)
487 * 3. The irq may result in the program using the GUS to reprogram the GUS
489 * Some programs also decide to upload by just checking if TC occurs
490 * (via interrupt or a cleared GUS dma flag)
491 * and then start the next transfer, without checking DMA state
493 * Thus: Always make sure to set the TC flag correctly!
495 * Note that the genuine GUS had a granularity of 16 bytes/words for low/high DMA
496 * while later cards had atomic granularity provided by an additional GUS50DMAHigh register
497 * GUSemu also uses this register to support byte-granular transfers for better compatibility
498 * with emulators other than GUSemu32
501 void gus_dma_transferdata(GUSEmuState
* state
, char *dma_addr
, unsigned int count
, int TC
)
503 /* this function gets called by the callback function as soon as a DMA transfer is about to start
504 * dma_addr is a translated address within accessible memory, not the physical one,
505 * count is (real dma count register)+1
506 * note that the amount of bytes transferred is fully determined by values in the DMA registers
507 * do not forget to update DMA states after transferring the entire block:
508 * DREQ cleared & TC asserted after the _whole_ transfer */
514 gusptr
= state
->gusdatapos
;
516 srcaddr
= dma_addr
; /* system memory address */
518 int offset
= (GUSregw(GUS42DMAStart
) << 4) + (GUSregb(GUS50DMAHigh
) & 0xf);
519 if (state
->gusdma
>= 4)
520 offset
= (offset
& 0xc0000) + (2 * (offset
& 0x1fff0)); /* 16 bit address translation */
521 destaddr
= (char *) state
->himemaddr
+ offset
; /* wavetable RAM address */
524 GUSregw(GUS42DMAStart
) += (GUSword
) (count
>> 4); /* ToDo: add 16bit GUS page limit? */
525 GUSregb(GUS50DMAHigh
) = (GUSbyte
) ((count
+ GUSregb(GUS50DMAHigh
)) & 0xf); /* ToDo: add 16bit GUS page limit? */
527 if (GUSregb(GUS41DMACtrl
) & 0x02) /* direction, 0 := sysram->gusram */
529 char *tmpaddr
= destaddr
;
534 if ((GUSregb(GUS41DMACtrl
) & 0x80) && (!(GUSregb(GUS41DMACtrl
) & 0x02)))
535 msbmask
= (const char) 0x80; /* invert MSB */
536 for (; count
> 0; count
--)
538 if (GUSregb(GUS41DMACtrl
) & 0x40)
539 *(destaddr
++) = *(srcaddr
++); /* 16 bit lobyte */
541 *(destaddr
++) = (msbmask
^ (*(srcaddr
++))); /* 8 bit */
542 if (state
->gusdma
>= 4)
543 *(destaddr
++) = (msbmask
^ (*(srcaddr
++))); /* 16 bit hibyte */
548 (GUSregb(GUS41DMACtrl
)) &= 0xfe; /* clear DMA request bit */
549 if (GUSregb(GUS41DMACtrl
) & 0x20) /* DMA terminal count IRQ */
551 GUSregb(IRQStatReg2x6
) |= 0x80;
552 GUS_irqrequest(state
, state
->gusirq
, 1);