1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "qemu/osdep.h"
30#include "gustate.h"
31#include "gusemu.h"
32
33#define GUSregb(position) (* (gusptr+(position)))
34#define GUSregw(position) (*(GUSword *) (gusptr+(position)))
35#define GUSregd(position) (*(GUSdword *)(gusptr+(position)))
36
37
38unsigned int gus_read(GUSEmuState * state, int port, int size)
39{
40 int value_read = 0;
41
42 GUSbyte *gusptr;
43 gusptr = state->gusdatapos;
44 GUSregd(portaccesses)++;
45
46 switch (port & 0xff0f)
47 {
48
49
50 case 0x206:
51
52
53
54
55 return GUSregb(IRQStatReg2x6);
56
57 case 0x208:
58 if (GUSregb(GUS45TimerCtrl) & 1)
59 return GUSregb(TimerStatus2x8);
60 return GUSregb(AdLibStatus2x8);
61 case 0x309:
62 case 0x209:
63 return GUSregb(AdLibData2x9);
64 case 0x20A:
65 return GUSregb(AdLibCommand2xA);
66
67#if 0
68 case 0x20B:
69 switch (GUSregb(RegCtrl_2xF) & 0x07)
70 {
71 case 0:
72 if (GUSregb(MixerCtrlReg2x0) & 0x40)
73 return GUSregb(IRQ_2xB);
74 else
75 return GUSregb(DMA_2xB);
76
77
78 case 6:
79 return GUSregb(Jumper_2xB);
80 default:;
81 }
82 break;
83#endif
84
85 case 0x20C:
86 value_read = GUSregb(SB2xCd);
87 if (GUSregb(StatRead_2xF) & 0x20)
88 GUSregb(SB2xCd) ^= 0x80;
89 return value_read;
90
91 case 0x20E:
92 if (GUSregb(RegCtrl_2xF) & 0x80)
93 {
94 GUSregb(StatRead_2xF) |= 0x80;
95 GUS_irqrequest(state, state->gusirq, 1);
96 }
97 return GUSregb(SB2xE);
98 case 0x20F:
99
100
101 value_read = (GUSregb(StatRead_2xF) & 0xf9);
102 if (GUSregb(MixerCtrlReg2x0) & 0x08)
103 value_read |= 2;
104 return value_read;
105
106
107 case 0x302:
108 return GUSregb(VoiceSelReg3x2);
109 case 0x303:
110 return GUSregb(FunkSelReg3x3);
111 case 0x304:
112 case 0x305:
113 switch (GUSregb(FunkSelReg3x3))
114 {
115
116 case 0x41:
117 value_read = GUSregb(GUS41DMACtrl);
118 GUSregb(GUS41DMACtrl) &= 0xbb;
119 if (state->gusdma >= 4)
120 value_read |= 0x04;
121 if (GUSregb(IRQStatReg2x6) & 0x80)
122 {
123 value_read |= 0x40;
124 GUSregb(IRQStatReg2x6) &= 0x7f;
125 if (!GUSregb(IRQStatReg2x6))
126 GUS_irqclear(state, state->gusirq);
127 }
128 return (GUSbyte) value_read;
129
130
131
132 case 0x45:
133 return GUSregb(GUS45TimerCtrl);
134
135
136 case 0x49:
137 return GUSregb(GUS49SampCtrl) & 0xbf;
138
139
140
141 case 0x80:
142 case 0x81:
143 case 0x82:
144 case 0x83:
145 case 0x84:
146 case 0x85:
147 case 0x86:
148 case 0x87:
149 case 0x88:
150 case 0x89:
151 case 0x8a:
152 case 0x8b:
153 case 0x8c:
154 case 0x8d:
155 {
156 int offset = 2 * (GUSregb(FunkSelReg3x3) & 0x0f);
157 offset += ((int) GUSregb(VoiceSelReg3x2) & 0x1f) << 5;
158 value_read = GUSregw(offset);
159 }
160 break;
161
162 case 0x8e:
163 return GUSregb(NumVoices);
164 case 0x8f:
165
166 return GUSregb(SynVoiceIRQ8f);
167 default:
168 return 0xffff;
169 }
170 if (size == 1)
171 {
172 if ((port & 0xff0f) == 0x305)
173 value_read = value_read >> 8;
174 value_read &= 0xff;
175 }
176 return (GUSword) value_read;
177
178
179 case 0x307:
180 {
181 GUSbyte *adr;
182 adr = state->himemaddr + (GUSregd(GUSDRAMPOS24bit) & 0xfffff);
183 return *adr;
184 }
185 default:;
186 }
187 return 0xffff;
188}
189
190void gus_write(GUSEmuState * state, int port, int size, unsigned int data)
191{
192 GUSbyte *gusptr;
193 gusptr = state->gusdatapos;
194 GUSregd(portaccesses)++;
195
196 switch (port & 0xff0f)
197 {
198 case 0x200:
199 GUSregb(MixerCtrlReg2x0) = (GUSbyte) data;
200 break;
201 case 0x206:
202 if (GUSregb(GUS45TimerCtrl) & 0x20)
203 {
204 GUSregb(TimerStatus2x8) |= 0x08;
205 GUSregb(IRQStatReg2x6) = 0x10;
206 GUS_irqrequest(state, state->gusirq, 1);
207 }
208 break;
209 case 0x308:
210 case 0x208:
211 GUSregb(AdLibCommand2xA) = (GUSbyte) data;
212 break;
213 case 0x309:
214 case 0x209:
215 if ((GUSregb(AdLibCommand2xA) == 0x04) && (!(GUSregb(GUS45TimerCtrl) & 1)))
216 {
217 if (data & 0x80)
218 GUSregb(TimerStatus2x8) &= 0x1f;
219 else
220 GUSregb(TimerDataReg2x9) = (GUSbyte) data;
221 }
222 else
223 {
224 GUSregb(AdLibData2x9) = (GUSbyte) data;
225 if (GUSregb(GUS45TimerCtrl) & 0x02)
226 {
227 GUSregb(TimerStatus2x8) |= 0x01;
228 GUSregb(IRQStatReg2x6) = 0x10;
229 GUS_irqrequest(state, state->gusirq, 1);
230 }
231 }
232 break;
233 case 0x20A:
234 GUSregb(AdLibStatus2x8) = (GUSbyte) data;
235 break;
236 case 0x20B:
237 switch (GUSregb(RegCtrl_2xF) & 0x7)
238 {
239 case 0:
240 if (GUSregb(MixerCtrlReg2x0) & 0x40)
241 GUSregb(IRQ_2xB) = (GUSbyte) data;
242 else
243 GUSregb(DMA_2xB) = (GUSbyte) data;
244 break;
245
246 case 5:
247 GUSregb(StatRead_2xF) = 0;
248 if (!GUSregb(IRQStatReg2x6))
249 GUS_irqclear(state, state->gusirq);
250 break;
251 case 6:
252 GUSregb(Jumper_2xB) = (GUSbyte) data;
253 break;
254 default:;
255 }
256 break;
257 case 0x20C:
258 if (GUSregb(GUS45TimerCtrl) & 0x20)
259 {
260 GUSregb(TimerStatus2x8) |= 0x10;
261 GUSregb(IRQStatReg2x6) = 0x10;
262 GUS_irqrequest(state, state->gusirq, 1);
263 }
264 case 0x20D:
265 GUSregb(SB2xCd) = (GUSbyte) data;
266 break;
267 case 0x20E:
268 GUSregb(SB2xE) = (GUSbyte) data;
269 break;
270 case 0x20F:
271 GUSregb(RegCtrl_2xF) = (GUSbyte) data;
272 break;
273 case 0x302:
274 GUSregb(VoiceSelReg3x2) = (GUSbyte) data;
275 break;
276 case 0x303:
277 GUSregb(FunkSelReg3x3) = (GUSbyte) data;
278 if ((GUSbyte) data == 0x8f)
279 {
280 int voice;
281 if (GUSregd(voicewavetableirq))
282 {
283 for (voice = 0; voice < 31; voice++)
284 {
285 if (GUSregd(voicewavetableirq) & (1 << voice))
286 {
287 GUSregd(voicewavetableirq) ^= (1 << voice);
288 GUSregb(voice << 5) &= 0x7f;
289 if (!GUSregd(voicewavetableirq))
290 GUSregb(IRQStatReg2x6) &= 0xdf;
291 if (!GUSregb(IRQStatReg2x6))
292 GUS_irqclear(state, state->gusirq);
293 GUSregb(SynVoiceIRQ8f) = voice | 0x60;
294 return;
295 }
296 }
297 }
298 else if (GUSregd(voicevolrampirq))
299 {
300 for (voice = 0; voice < 31; voice++)
301 {
302 if (GUSregd(voicevolrampirq) & (1 << voice))
303 {
304 GUSregd(voicevolrampirq) ^= (1 << voice);
305 GUSregb((voice << 5) + VSRVolRampControl) &= 0x7f;
306 if (!GUSregd(voicevolrampirq))
307 GUSregb(IRQStatReg2x6) &= 0xbf;
308 if (!GUSregb(IRQStatReg2x6))
309 GUS_irqclear(state, state->gusirq);
310 GUSregb(SynVoiceIRQ8f) = voice | 0x80;
311 return;
312 }
313 }
314 }
315 GUSregb(SynVoiceIRQ8f) = 0xe8;
316 }
317 break;
318 case 0x304:
319 case 0x305:
320 {
321 GUSword writedata = (GUSword) data;
322 GUSword readmask = 0x0000;
323 if (size == 1)
324 {
325 readmask = 0xff00;
326 writedata &= 0xff;
327 if ((port & 0xff0f) == 0x305)
328 {
329 writedata = (GUSword) (writedata << 8);
330 readmask = 0x00ff;
331 }
332 }
333 switch (GUSregb(FunkSelReg3x3))
334 {
335
336 case 0x00:
337 case 0x01:
338 case 0x02:
339 case 0x03:
340 case 0x04:
341 case 0x05:
342 case 0x06:
343 case 0x07:
344 case 0x08:
345 case 0x09:
346 case 0x0a:
347 case 0x0b:
348 case 0x0c:
349 case 0x0d:
350 {
351 int offset;
352 if (!(GUSregb(GUS4cReset) & 0x01))
353 break;
354 offset = 2 * (GUSregb(FunkSelReg3x3) & 0x0f);
355 offset += (GUSregb(VoiceSelReg3x2) & 0x1f) << 5;
356 GUSregw(offset) = (GUSword) ((GUSregw(offset) & readmask) | writedata);
357 }
358 break;
359
360 case 0x0e:
361 GUSregb(NumVoices) = (GUSbyte) data;
362 break;
363
364
365 case 0x41:
366 GUSregb(GUS41DMACtrl) = (GUSbyte) data;
367 if (data & 0x01)
368 GUS_dmarequest(state);
369 break;
370 case 0x42:
371 GUSregw(GUS42DMAStart) = (GUSregw(GUS42DMAStart) & readmask) | writedata;
372 GUSregb(GUS50DMAHigh) &= 0xf;
373 break;
374 case 0x43:
375 GUSregd(GUSDRAMPOS24bit) =
376 (GUSregd(GUSDRAMPOS24bit) & (readmask | 0xff0000)) | writedata;
377 break;
378 case 0x44:
379 GUSregd(GUSDRAMPOS24bit) =
380 (GUSregd(GUSDRAMPOS24bit) & 0xffff) | ((data & 0x0f) << 16);
381 break;
382 case 0x45:
383 GUSregb(GUS45TimerCtrl) = (GUSbyte) data;
384 if (!(data & 0x20))
385 GUSregb(TimerStatus2x8) &= 0xe7;
386 if (!(data & 0x02))
387 GUSregb(TimerStatus2x8) &= 0xfe;
388 if (!(GUSregb(TimerStatus2x8) & 0x19))
389 GUSregb(IRQStatReg2x6) &= 0xef;
390
391 if ((GUSregw(TimerIRQs) > 1) && (GUSregb(TimerDataReg2x9) & 3))
392 {
393 if (GUSregb(TimerDataReg2x9) & 1)
394 {
395 if (!(GUSregb(TimerDataReg2x9) & 0x40))
396 GUSregb(TimerStatus2x8) |= 0xc0;
397 if (data & 4)
398 {
399 GUSregb(TimerStatus2x8) |= 4;
400 GUSregb(IRQStatReg2x6) |= 4;
401 }
402 }
403 if (GUSregb(TimerDataReg2x9) & 2)
404 {
405 if (!(GUSregb(TimerDataReg2x9) & 0x20))
406 GUSregb(TimerStatus2x8) |= 0xa0;
407 if (data & 8)
408 {
409 GUSregb(TimerStatus2x8) |= 2;
410 GUSregb(IRQStatReg2x6) |= 8;
411 }
412 }
413 GUSregw(TimerIRQs)--;
414 if (GUSregw(BusyTimerIRQs) > 1)
415 GUSregw(BusyTimerIRQs)--;
416 else
417 GUSregw(BusyTimerIRQs) =
418 GUS_irqrequest(state, state->gusirq, GUSregw(TimerIRQs));
419 }
420 else
421 GUSregw(TimerIRQs) = 0;
422
423 if (!(data & 0x04))
424 {
425 GUSregb(TimerStatus2x8) &= 0xfb;
426 GUSregb(IRQStatReg2x6) &= 0xfb;
427 }
428 if (!(data & 0x08))
429 {
430 GUSregb(TimerStatus2x8) &= 0xfd;
431 GUSregb(IRQStatReg2x6) &= 0xf7;
432 }
433 if (!GUSregb(IRQStatReg2x6))
434 GUS_irqclear(state, state->gusirq);
435 break;
436 case 0x46:
437 GUSregb(GUS46Counter1) = (GUSbyte) data;
438 break;
439 case 0x47:
440 GUSregb(GUS47Counter2) = (GUSbyte) data;
441 break;
442
443 case 0x49:
444 GUSregb(GUS49SampCtrl) = (GUSbyte) data;
445 break;
446
447 case 0x4c:
448 GUSregb(GUS4cReset) = (GUSbyte) data;
449 if (!(GUSregb(GUS4cReset) & 1))
450 {
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;
464 GUS_irqclear(state, state->gusirq);
465 }
466
467
468 break;
469 }
470 }
471 break;
472 case 0x307:
473 {
474 GUSbyte *adr;
475 adr = state->himemaddr + (GUSregd(GUSDRAMPOS24bit) & 0xfffff);
476 *adr = (GUSbyte) data;
477 }
478 break;
479 }
480}
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501void gus_dma_transferdata(GUSEmuState * state, char *dma_addr, unsigned int count, int TC)
502{
503
504
505
506
507
508
509
510 char *srcaddr;
511 char *destaddr;
512 char msbmask = 0;
513 GUSbyte *gusptr;
514 gusptr = state->gusdatapos;
515
516 srcaddr = dma_addr;
517 {
518 int offset = (GUSregw(GUS42DMAStart) << 4) + (GUSregb(GUS50DMAHigh) & 0xf);
519 if (state->gusdma >= 4)
520 offset = (offset & 0xc0000) + (2 * (offset & 0x1fff0));
521 destaddr = (char *) state->himemaddr + offset;
522 }
523
524 GUSregw(GUS42DMAStart) += (GUSword) (count >> 4);
525 GUSregb(GUS50DMAHigh) = (GUSbyte) ((count + GUSregb(GUS50DMAHigh)) & 0xf);
526
527 if (GUSregb(GUS41DMACtrl) & 0x02)
528 {
529 char *tmpaddr = destaddr;
530 destaddr = srcaddr;
531 srcaddr = tmpaddr;
532 }
533
534 if ((GUSregb(GUS41DMACtrl) & 0x80) && (!(GUSregb(GUS41DMACtrl) & 0x02)))
535 msbmask = (const char) 0x80;
536 for (; count > 0; count--)
537 {
538 if (GUSregb(GUS41DMACtrl) & 0x40)
539 *(destaddr++) = *(srcaddr++);
540 else
541 *(destaddr++) = (msbmask ^ (*(srcaddr++)));
542 if (state->gusdma >= 4)
543 *(destaddr++) = (msbmask ^ (*(srcaddr++)));
544 }
545
546 if (TC)
547 {
548 (GUSregb(GUS41DMACtrl)) &= 0xfe;
549 if (GUSregb(GUS41DMACtrl) & 0x20)
550 {
551 GUSregb(IRQStatReg2x6) |= 0x80;
552 GUS_irqrequest(state, state->gusirq, 1);
553 }
554 }
555}
556