-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathchip_strp.c
More file actions
694 lines (606 loc) · 13.8 KB
/
chip_strp.c
File metadata and controls
694 lines (606 loc) · 13.8 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "stdtype.h"
#include "stdbool.h"
#include "chip_strp.h"
typedef struct sn76496_data
{
UINT8 LastChn;
} SN76496_DATA;
typedef struct ym2413_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} YM2413_DATA;
typedef struct ym2612_data
{
UINT8 RegData[0x200];
UINT8 RegFirst[0x200];
UINT8 KeyOn[0x08];
UINT8 KeyFirst[0x08];
} YM2612_DATA;
typedef struct ym2151_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
UINT8 MCMask[0x08];
UINT8 MCFirst[0x08];
} YM2151_DATA;
typedef struct _rf5c68_channel
{
UINT8 ChnReg[0x07];
UINT8 RegFirst[0x07];
} RF5C68_CHANNEL;
typedef struct rf5c68_data
{
UINT8 cbank;
UINT8 wbank;
} RF5C68_DATA;
typedef struct ym2203_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
UINT8 KeyOn[0x04];
UINT8 KeyFirst[0x04];
UINT8 PreSclCmd;
} YM2203_DATA;
typedef struct ym2608_data
{
UINT8 RegData[0x200];
UINT8 RegFirst[0x200];
UINT8 KeyOn[0x08];
UINT8 KeyFirst[0x08];
UINT8 PreSclCmd;
} YM2608_DATA;
typedef struct ym2610_data
{
UINT8 RegData[0x200];
UINT8 RegFirst[0x200];
UINT8 KeyOn[0x08];
UINT8 KeyFirst[0x08];
UINT8 PreSclCmd;
} YM2610_DATA;
typedef struct ym3812_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} YM3812_DATA;
typedef struct ym3526_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} YM3526_DATA;
typedef struct y8950_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} Y8950_DATA;
typedef struct ymf262_data
{
UINT8 RegData[0x200];
UINT8 RegFirst[0x200];
} YMF262_DATA;
typedef struct ymf271_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} YMF271_DATA;
typedef struct ymf278b_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} YMF278B_DATA;
typedef struct ymz280b_data
{
UINT8 RegData[0x100];
UINT8 RegFirst[0x100];
} YMZ280B_DATA;
typedef struct c140_data
{
UINT8 BankType;
UINT8 RegData[0x200];
UINT8 RegFirst[0x200];
} C140_DATA;
typedef struct all_chips
{
//UINT8 GGSt;
SN76496_DATA SN76496;
YM2413_DATA YM2413;
YM2612_DATA YM2612;
YM2151_DATA YM2151;
//SEGAPCM_DATA SegaPCM;
RF5C68_DATA RF5C68;
YM2203_DATA YM2203;
YM2608_DATA YM2608;
YM2610_DATA YM2610;
YM3812_DATA YM3812;
YM3526_DATA YM3526;
Y8950_DATA Y8950;
YMF262_DATA YMF262;
YMF271_DATA YMF271;
YMF278B_DATA YMF278B;
YMZ280B_DATA YMZ280B;
RF5C68_DATA RF5C164;
C140_DATA C140;
} ALL_CHIPS;
UINT8 ChipCount = 0x02;
ALL_CHIPS* ChipData = NULL;
ALL_CHIPS* ChDat;
extern STRIP_DATA StripVGM[2];
STRIP_DATA* StpDat;
void InitAllChips(void)
{
UINT8 CurChip;
if (ChipData == NULL)
ChipData = (ALL_CHIPS*)malloc(ChipCount * sizeof(ALL_CHIPS));
for (CurChip = 0x00; CurChip < ChipCount; CurChip ++)
{
memset(ChipData, 0xFF, ChipCount * sizeof(ALL_CHIPS));
}
SetChipSet(0x00);
return;
}
void FreeAllChips(void)
{
if (ChipData == NULL)
return;
free(ChipData);
ChipData = NULL;
return;
}
void SetChipSet(UINT8 ChipID)
{
ChDat = ChipData + ChipID;
StpDat = StripVGM + ChipID;
return;
}
bool GGStereo(UINT8 Data)
{
STRIP_PSG* strip = &StpDat->SN76496;
if (strip->All || (strip->Other & 0x01))
return false;
return true;
}
bool sn76496_write(UINT8 Command)
{
SN76496_DATA* chip = &ChDat->SN76496;
STRIP_PSG* strip = &StpDat->SN76496;
UINT8 Channel;
if (strip->All)
return false;
if (Command & 0x80)
{
Channel = (Command & 0x60) >> 5;
chip->LastChn = Channel;
}
else
{
Channel = chip->LastChn;
}
if (strip->ChnMask & (0x01 << Channel))
return false;
return true;
}
bool ym2413_write(UINT8 Register, UINT8 Data)
{
STRIP_OPL* strip = &StpDat->YM2413;
if (strip->All)
return false;
return true;
ChDat->YM2413.RegFirst[Register] = 0x00;
ChDat->YM2413.RegData[Register] = Data;
return true;
}
bool ym2612_write(UINT8 Port, UINT8 Register, UINT8 Data)
{
YM2612_DATA* chip = &ChDat->YM2612;
STRIP_OPN* strip = &StpDat->YM2612;
UINT16 RegVal;
UINT8 CurChn;
if (strip->All)
return false;
RegVal = (Port << 8) | Register;
switch(RegVal)
{
case 0x28: // Key on/off
CurChn = Data & 0x07;
if (CurChn >= 4)
CurChn--;
return !(strip->ChnMask & (1 << CurChn));
case 0x2A: // DAC data
case 0x2B: // DAC enable
case 0x2C: // DAC test register
return !(strip->ChnMask & (1 << 6));
case 0x1B6: // stereo control for FM6/DAC
// don't strip if either is kept, as the VGM may switch modes anytime
if (!(strip->ChnMask & (1 << 5)) || !(strip->ChnMask & (1 << 6)))
return true;
break;
}
CurChn = 0xFF;
if (Register >= 0x30 && Register < 0xC0)
{
if (Register >= 0xA8 && Register < 0xB0) // port 0 only
{
if (Port)
CurChn = 3;
}
else
{
CurChn = Port * 3 + (Register & 0x03);
}
}
if (CurChn != 0xFF)
return !(strip->ChnMask & (1 << CurChn));
else
return true;
}
bool ym2151_write(UINT8 Register, UINT8 Data)
{
YM2151_DATA* chip;
STRIP_OPM* strip = &StpDat->YM2151;
UINT8 Channel;
if (strip->All)
return false;
return true;
chip = &ChDat->YM2151;
switch(Register)
{
case 0x08:
Channel = Data & 0x07;
if (! chip->MCFirst[Channel] && (Data & 0xF8) == chip->MCMask[Channel])
return false;
chip->MCFirst[Channel] = 0x00;
chip->MCMask[Channel] = Data & 0xF8;
break;
default:
if (! chip->RegFirst[Register] && Data == chip->RegData[Register])
return false;
chip->RegFirst[Register] = 0x00;
chip->RegData[Register] = Data;
break;
}
return true;
}
bool segapcm_mem_write(UINT16 Offset, UINT8 Data)
{
STRIP_PCM* strip = &StpDat->SegaPCM;
UINT8 CurChn;
if (strip->All)
return false;
CurChn = (Offset & 0x78) >> 3;
if (strip->ChnMask & (0x01 << CurChn))
return false;
return true;
}
bool rf5cxx_reg_write(RF5C68_DATA* chip, STRIP_PCM* strip, UINT8 Register, UINT8* Data)
{
UINT8 CurChn;
UINT8 ChnMask;
if (strip->All)
return false;
switch(Register)
{
case 0x00: // Envelope
case 0x01: // Pan
case 0x02: // FD Low / step
case 0x03: // FD High / step
case 0x04: // Loop Start Low
case 0x05: // Loop Start High
case 0x06: // Start
CurChn = chip->cbank;
break;
case 0x07: // Control Register
if (*Data & 0x40)
{
chip->cbank = *Data & 0x07;
CurChn = chip->cbank;
}
else
{
if (strip->Other & 0x01)
return false;
chip->wbank = *Data & 0x0F;
CurChn = 0x0F;
}
break;
case 0x08: // Channel On/Off Register
ChnMask = 0x01;
for (CurChn = 0; CurChn < 8; CurChn ++, ChnMask <<= 1)
{
// 0 - enable, 1 - disable
// Channel Enable && Strip Channel
if (! (*Data & ChnMask) && (strip->ChnMask & (0x01 << CurChn)))
*Data |= ChnMask; // disable channel
}
return true;
}
if (strip->ChnMask & (0x01 << CurChn))
return false;
return true;
}
bool rf5c68_reg_write(UINT8 Register, UINT8* Data)
{
RF5C68_DATA* chip = &ChDat->RF5C68;
STRIP_PCM* strip = &StpDat->RF5C68;
return rf5cxx_reg_write(chip, strip, Register, Data);
}
bool rf5c68_mem_write(UINT16 Offset, UINT8 Data)
{
//RF5C68_DATA* chip = &ChDat->RF5C68;
STRIP_PCM* strip = &StpDat->RF5C68;
if (strip->All || (strip->Other & 0x01))
return false;
return true;
}
bool ym2203_write(UINT8 Register, UINT8 Data)
{
YM2203_DATA* chip;
STRIP_OPN* strip = &StpDat->YM2203;
UINT8 Channel;
if (strip->All)
return false;
return true;
chip = &ChDat->YM2203;
switch(Register)
{
case 0x24: // Timer Registers
case 0x25:
case 0x26:
return false;
case 0x2D: // OPN Prescaler Registers
case 0x2E:
case 0x2F:
if (chip->PreSclCmd == Register)
return false;
chip->PreSclCmd = Register;
break;
case 0x28:
Channel = Data & 0x03;
if (! chip->KeyFirst[Channel] && Data == chip->KeyOn[Channel])
return false;
chip->KeyFirst[Channel] = 0x00;
chip->KeyOn[Channel] = Data;
break;
case 0x27:
Data &= 0xC0; // mask out all timer-relevant bits
// fall through
default:
if ((Register & 0xF0) < 0x10)
return true; // I don't know anything about the SSG emulator
switch(Register & 0xF4)
{
case 0xA0: // A0-A3 and A8-AB
return true; // Rewrite is neccessary: Phase Increment is recalculated
case 0xA4: // A4-A7 and AC-AF - Frequence Latch
return true;
// For some reason (I really can't say why) the code below doesn't work properly
/*if ((RegVal & 0x03) == 0x03)
break;
RegVal &= 0x0FC; // Registers A4-A6 (AC-AE for Ch3) write to the same offset
break;*/
}
if (! chip->RegFirst[Register] && Data == chip->RegData[Register])
return false;
chip->RegFirst[Register] = 0x00;
chip->RegData[Register] = Data;
break;
}
return true;
}
bool ym2608_write(UINT8 Port, UINT8 Register, UINT8 Data)
{
YM2608_DATA* chip;
STRIP_OPN* strip = &StpDat->YM2608;
UINT16 RegVal;
UINT8 Channel;
if (strip->All)
return false;
return true;
chip = &ChDat->YM2608;
RegVal = (Port << 8) | Register;
switch(RegVal)
{
case 0x24: // Timer Registers
case 0x25:
case 0x26:
return false;
case 0x2D: // OPN Prescaler Registers
case 0x2E:
case 0x2F:
if (chip->PreSclCmd == Register)
return false;
break;
case 0x28:
Channel = Data & 0x07;
if (! chip->KeyFirst[Channel] && Data == chip->KeyOn[Channel])
return false;
chip->KeyFirst[Channel] = 0x00;
chip->KeyOn[Channel] = Data;
break;
case 0x27:
Data &= 0xC0; // mask out all timer-relevant bits
// fall through
default:
if ((RegVal & 0x1F0) < 0x10)
return true; // I don't know anything about the SSG emulator
if ((RegVal & 0x1F0) >= 0x100 && (RegVal & 0x1F0) < 0x110) // DELTA-T
{
if ((RegVal & 0x0F) != 0x0E) // DAC Data is handled like DAC of YM2612
return true; // I assume that these registers are not spammed
}
switch(RegVal & 0xF4)
{
case 0xA0: // A0-A3 and A8-AB
return true; // Rewrite is neccessary: Phase Increment is recalculated
case 0xA4: // A4-A7 and AC-AF - Frequence Latch
return true;
// For some reason (I really can't say why) the code below doesn't work properly
/*if ((RegVal & 0x03) == 0x03)
break;
RegVal &= 0x0FC; // Registers A4-A6 (AC-AE for Ch3) write to the same offset
break;*/
}
if (! chip->RegFirst[RegVal] && Data == chip->RegData[RegVal])
return false;
chip->RegFirst[RegVal] = 0x00;
chip->RegData[RegVal] = Data;
break;
}
return true;
}
bool ym2610_write(UINT8 Port, UINT8 Register, UINT8 Data)
{
YM2610_DATA* chip;
STRIP_OPN* strip = &StpDat->YM2610;
UINT16 RegVal;
UINT8 Channel;
if (strip->All)
return false;
return true;
chip = &ChDat->YM2610;
RegVal = (Port << 8) | Register;
switch(RegVal)
{
case 0x24: // Timer Registers
case 0x25:
case 0x26:
return false;
// no OPN Prescaler Registers for YM2610
case 0x28:
Channel = Data & 0x07;
if (! chip->KeyFirst[Channel] && Data == chip->KeyOn[Channel])
return false;
chip->KeyFirst[Channel] = 0x00;
chip->KeyOn[Channel] = Data;
break;
case 0x27:
Data &= 0xC0; // mask out all timer-relevant bits
// fall through
default:
if (RegVal == 0x1C)
return false; // controls only Status Bits
if ((RegVal & 0x1F0) < 0x10)
return true; // I don't know anything about the SSG emulator
if ((RegVal & 0x1F0) >= 0x010 && (RegVal & 0x1F0) < 0x020) // DELTA-T
return true; // I assume that these registers are not spammed
if ((RegVal & 0x1F0) >= 0x100 && (RegVal & 0x1F0) < 0x130) // ADPCM
return true; // I assume that these registers are not spammed
switch(RegVal & 0xF4)
{
case 0xA0: // A0-A3 and A8-AB
return true; // Rewrite is neccessary: Phase Increment is recalculated
case 0xA4: // A4-A7 and AC-AF - Frequence Latch
return true;
// For some reason (I really can't say why) the code below doesn't work properly
/*if ((RegVal & 0x03) == 0x03)
break;
RegVal &= 0x0FC; // Registers A4-A6 (AC-AE for Ch3) write to the same offset
break;*/
}
if (! chip->RegFirst[RegVal] && Data == chip->RegData[RegVal])
return false;
chip->RegFirst[RegVal] = 0x00;
chip->RegData[RegVal] = Data;
break;
}
return true;
}
bool ym3812_write(UINT8 Register, UINT8 Data)
{
STRIP_OPL* strip = &StpDat->YM3812;
if (strip->All)
return false;
return true;
ChDat->YM3812.RegFirst[Register] = 0x00;
ChDat->YM3812.RegData[Register] = Data;
return true;
}
bool ym3526_write(UINT8 Register, UINT8 Data)
{
STRIP_OPL* strip = &StpDat->YM3526;
if (strip->All)
return false;
return true;
ChDat->YM3526.RegFirst[Register] = 0x00;
ChDat->YM3526.RegData[Register] = Data;
return true;
}
bool y8950_write(UINT8 Register, UINT8 Data)
{
STRIP_OPL* strip = &StpDat->Y8950;
if (strip->All)
return false;
return true;
ChDat->Y8950.RegFirst[Register] = 0x00;
ChDat->Y8950.RegData[Register] = Data;
return true;
}
bool ymf262_write(UINT8 Port, UINT8 Register, UINT8 Data)
{
STRIP_OPL* strip = &StpDat->YMF262;
UINT16 RegVal;
if (strip->All)
return false;
RegVal = (Port << 8) | Register;
return true;
ChDat->YMF262.RegFirst[RegVal] = 0x00;
ChDat->YMF262.RegData[RegVal] = Data;
return true;
}
bool ymz280b_write(UINT8 Register, UINT8 Data)
{
STRIP_YMZ* strip = &StpDat->YMZ280B;
if (strip->All)
return false;
return true;
ChDat->YMZ280B.RegFirst[Register] = 0x00;
ChDat->YMZ280B.RegData[Register] = Data;
return true;
}
bool rf5c164_reg_write(UINT8 Register, UINT8* Data)
{
RF5C68_DATA* chip = &ChDat->RF5C164;
STRIP_PCM* strip = &StpDat->RF5C164;
return rf5cxx_reg_write(chip, strip, Register, Data);
}
bool rf5c164_mem_write(UINT16 Offset, UINT8 Data)
{
//RF5C68_DATA* chip = &ChDat->RF5C164;
STRIP_PCM* strip = &StpDat->RF5C164;
if (strip->All || (strip->Other & 0x01))
return false;
return true;
}
bool c140_write(UINT8 Port, UINT8 Register, UINT8 Data)
{
STRIP_OPL4WT* strip = &StpDat->C140;
C140_DATA* chip = &ChDat->C140;
UINT16 RegVal;
UINT8 Channel;
if (strip->All)
return false;
if (Port == 0xFF)
{
chip->BankType = Data;
return true;
}
RegVal = (Port << 8) | Register;
RegVal &= 0x1FF;
// mirror the bank registers on the 219, fixes bkrtmaq
if ((RegVal >= 0x1F8) && (chip->BankType >= 0x02))
RegVal &= 0x1F7;
if (RegVal < 0x180)
{
Channel = RegVal >> 4;
if (strip->ChnMask & (0x01 << Channel))
return false;
}
else
{
if (strip->Other & 0x01)
return false;
}
return true;
}