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) (*(uint16_t *) (gusptr+(position)))
35#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))
36
37
38unsigned int gus_read(GUSEmuState * state, int port, int size)
39{
40 int value_read = 0;
41
42 uint8_t *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 (uint8_t) 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 (uint16_t) value_read;
177
178
179 case 0x307:
180 {
181 uint8_t *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 uint8_t *gusptr;
193 gusptr = state->gusdatapos;
194 GUSregd(portaccesses)++;
195
196 switch (port & 0xff0f)
197 {
198 case 0x200:
199 GUSregb(MixerCtrlReg2x0) = (uint8_t) 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) = (uint8_t) 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) = (uint8_t) data;
221 }
222 else
223 {
224 GUSregb(AdLibData2x9) = (uint8_t) 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) = (uint8_t) 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) = (uint8_t) data;
242 else
243 GUSregb(DMA_2xB) = (uint8_t) 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) = (uint8_t) 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
265 case 0x20D:
266 GUSregb(SB2xCd) = (uint8_t) data;
267 break;
268 case 0x20E:
269 GUSregb(SB2xE) = (uint8_t) data;
270 break;
271 case 0x20F:
272 GUSregb(RegCtrl_2xF) = (uint8_t) data;
273 break;
274 case 0x302:
275 GUSregb(VoiceSelReg3x2) = (uint8_t) data;
276 break;
277 case 0x303:
278 GUSregb(FunkSelReg3x3) = (uint8_t) data;
279 if ((uint8_t) data == 0x8f)
280 {
281 int voice;
282 if (GUSregd(voicewavetableirq))
283 {
284 for (voice = 0; voice < 31; voice++)
285 {
286 if (GUSregd(voicewavetableirq) & (1 << voice))
287 {
288 GUSregd(voicewavetableirq) ^= (1 << voice);
289 GUSregb(voice << 5) &= 0x7f;
290 if (!GUSregd(voicewavetableirq))
291 GUSregb(IRQStatReg2x6) &= 0xdf;
292 if (!GUSregb(IRQStatReg2x6))
293 GUS_irqclear(state, state->gusirq);
294 GUSregb(SynVoiceIRQ8f) = voice | 0x60;
295 return;
296 }
297 }
298 }
299 else if (GUSregd(voicevolrampirq))
300 {
301 for (voice = 0; voice < 31; voice++)
302 {
303 if (GUSregd(voicevolrampirq) & (1 << voice))
304 {
305 GUSregd(voicevolrampirq) ^= (1 << voice);
306 GUSregb((voice << 5) + VSRVolRampControl) &= 0x7f;
307 if (!GUSregd(voicevolrampirq))
308 GUSregb(IRQStatReg2x6) &= 0xbf;
309 if (!GUSregb(IRQStatReg2x6))
310 GUS_irqclear(state, state->gusirq);
311 GUSregb(SynVoiceIRQ8f) = voice | 0x80;
312 return;
313 }
314 }
315 }
316 GUSregb(SynVoiceIRQ8f) = 0xe8;
317 }
318 break;
319 case 0x304:
320 case 0x305:
321 {
322 uint16_t writedata = (uint16_t) data;
323 uint16_t readmask = 0x0000;
324 if (size == 1)
325 {
326 readmask = 0xff00;
327 writedata &= 0xff;
328 if ((port & 0xff0f) == 0x305)
329 {
330 writedata = (uint16_t) (writedata << 8);
331 readmask = 0x00ff;
332 }
333 }
334 switch (GUSregb(FunkSelReg3x3))
335 {
336
337 case 0x00:
338 case 0x01:
339 case 0x02:
340 case 0x03:
341 case 0x04:
342 case 0x05:
343 case 0x06:
344 case 0x07:
345 case 0x08:
346 case 0x09:
347 case 0x0a:
348 case 0x0b:
349 case 0x0c:
350 case 0x0d:
351 {
352 int offset;
353 if (!(GUSregb(GUS4cReset) & 0x01))
354 break;
355 offset = 2 * (GUSregb(FunkSelReg3x3) & 0x0f);
356 offset += (GUSregb(VoiceSelReg3x2) & 0x1f) << 5;
357 GUSregw(offset) = (uint16_t) ((GUSregw(offset) & readmask) | writedata);
358 }
359 break;
360
361 case 0x0e:
362 GUSregb(NumVoices) = (uint8_t) data;
363 break;
364
365
366 case 0x41:
367 GUSregb(GUS41DMACtrl) = (uint8_t) data;
368 if (data & 0x01)
369 GUS_dmarequest(state);
370 break;
371 case 0x42:
372 GUSregw(GUS42DMAStart) = (GUSregw(GUS42DMAStart) & readmask) | writedata;
373 GUSregb(GUS50DMAHigh) &= 0xf;
374 break;
375 case 0x43:
376 GUSregd(GUSDRAMPOS24bit) =
377 (GUSregd(GUSDRAMPOS24bit) & (readmask | 0xff0000)) | writedata;
378 break;
379 case 0x44:
380 GUSregd(GUSDRAMPOS24bit) =
381 (GUSregd(GUSDRAMPOS24bit) & 0xffff) | ((data & 0x0f) << 16);
382 break;
383 case 0x45:
384 GUSregb(GUS45TimerCtrl) = (uint8_t) data;
385 if (!(data & 0x20))
386 GUSregb(TimerStatus2x8) &= 0xe7;
387 if (!(data & 0x02))
388 GUSregb(TimerStatus2x8) &= 0xfe;
389 if (!(GUSregb(TimerStatus2x8) & 0x19))
390 GUSregb(IRQStatReg2x6) &= 0xef;
391
392 if ((GUSregw(TimerIRQs) > 1) && (GUSregb(TimerDataReg2x9) & 3))
393 {
394 if (GUSregb(TimerDataReg2x9) & 1)
395 {
396 if (!(GUSregb(TimerDataReg2x9) & 0x40))
397 GUSregb(TimerStatus2x8) |= 0xc0;
398 if (data & 4)
399 {
400 GUSregb(TimerStatus2x8) |= 4;
401 GUSregb(IRQStatReg2x6) |= 4;
402 }
403 }
404 if (GUSregb(TimerDataReg2x9) & 2)
405 {
406 if (!(GUSregb(TimerDataReg2x9) & 0x20))
407 GUSregb(TimerStatus2x8) |= 0xa0;
408 if (data & 8)
409 {
410 GUSregb(TimerStatus2x8) |= 2;
411 GUSregb(IRQStatReg2x6) |= 8;
412 }
413 }
414 GUSregw(TimerIRQs)--;
415 if (GUSregw(BusyTimerIRQs) > 1)
416 GUSregw(BusyTimerIRQs)--;
417 else
418 GUSregw(BusyTimerIRQs) =
419 GUS_irqrequest(state, state->gusirq, GUSregw(TimerIRQs));
420 }
421 else
422 GUSregw(TimerIRQs) = 0;
423
424 if (!(data & 0x04))
425 {
426 GUSregb(TimerStatus2x8) &= 0xfb;
427 GUSregb(IRQStatReg2x6) &= 0xfb;
428 }
429 if (!(data & 0x08))
430 {
431 GUSregb(TimerStatus2x8) &= 0xfd;
432 GUSregb(IRQStatReg2x6) &= 0xf7;
433 }
434 if (!GUSregb(IRQStatReg2x6))
435 GUS_irqclear(state, state->gusirq);
436 break;
437 case 0x46:
438 GUSregb(GUS46Counter1) = (uint8_t) data;
439 break;
440 case 0x47:
441 GUSregb(GUS47Counter2) = (uint8_t) data;
442 break;
443
444 case 0x49:
445 GUSregb(GUS49SampCtrl) = (uint8_t) data;
446 break;
447
448 case 0x4c:
449 GUSregb(GUS4cReset) = (uint8_t) data;
450 if (!(GUSregb(GUS4cReset) & 1))
451 {
452 GUSregd(voicewavetableirq) = 0;
453 GUSregd(voicevolrampirq) = 0;
454 GUSregw(TimerIRQs) = 0;
455 GUSregw(BusyTimerIRQs) = 0;
456 GUSregb(NumVoices) = 0xcd;
457 GUSregb(IRQStatReg2x6) = 0;
458 GUSregb(TimerStatus2x8) = 0;
459 GUSregb(AdLibData2x9) = 0;
460 GUSregb(TimerDataReg2x9) = 0;
461 GUSregb(GUS41DMACtrl) = 0;
462 GUSregb(GUS45TimerCtrl) = 0;
463 GUSregb(GUS49SampCtrl) = 0;
464 GUSregb(GUS4cReset) &= 0xf9;
465 GUS_irqclear(state, state->gusirq);
466 }
467
468
469 break;
470 }
471 }
472 break;
473 case 0x307:
474 {
475 uint8_t *adr;
476 adr = state->himemaddr + (GUSregd(GUSDRAMPOS24bit) & 0xfffff);
477 *adr = (uint8_t) data;
478 }
479 break;
480 }
481}
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502void gus_dma_transferdata(GUSEmuState * state, char *dma_addr, unsigned int count, int TC)
503{
504
505
506
507
508
509
510
511 char *srcaddr;
512 char *destaddr;
513 char msbmask = 0;
514 uint8_t *gusptr;
515 gusptr = state->gusdatapos;
516
517 srcaddr = dma_addr;
518 {
519 int offset = (GUSregw(GUS42DMAStart) << 4) + (GUSregb(GUS50DMAHigh) & 0xf);
520 if (state->gusdma >= 4)
521 offset = (offset & 0xc0000) + (2 * (offset & 0x1fff0));
522 destaddr = (char *) state->himemaddr + offset;
523 }
524
525 GUSregw(GUS42DMAStart) += (uint16_t) (count >> 4);
526 GUSregb(GUS50DMAHigh) = (uint8_t) ((count + GUSregb(GUS50DMAHigh)) & 0xf);
527
528 if (GUSregb(GUS41DMACtrl) & 0x02)
529 {
530 char *tmpaddr = destaddr;
531 destaddr = srcaddr;
532 srcaddr = tmpaddr;
533 }
534
535 if ((GUSregb(GUS41DMACtrl) & 0x80) && (!(GUSregb(GUS41DMACtrl) & 0x02)))
536 msbmask = (const char) 0x80;
537 for (; count > 0; count--)
538 {
539 if (GUSregb(GUS41DMACtrl) & 0x40)
540 *(destaddr++) = *(srcaddr++);
541 else
542 *(destaddr++) = (msbmask ^ (*(srcaddr++)));
543 if (state->gusdma >= 4)
544 *(destaddr++) = (msbmask ^ (*(srcaddr++)));
545 }
546
547 if (TC)
548 {
549 (GUSregb(GUS41DMACtrl)) &= 0xfe;
550 if (GUSregb(GUS41DMACtrl) & 0x20)
551 {
552 GUSregb(IRQStatReg2x6) |= 0x80;
553 GUS_irqrequest(state, state->gusirq, 1);
554 }
555 }
556}
557