-
Notifications
You must be signed in to change notification settings - Fork 158
/
Mockingboard.cpp
1470 lines (1210 loc) · 48.9 KB
/
Mockingboard.cpp
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
AppleWin : An Apple //e emulator for Windows
Copyright (C) 1994-1996, Michael O'Brien
Copyright (C) 1999-2001, Oliver Schmidt
Copyright (C) 2002-2005, Tom Charlesworth
Copyright (C) 2006-2007, Tom Charlesworth, Michael Pohoreski
AppleWin is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
AppleWin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AppleWin; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Description: Mockingboard/Phasor emulation
*
*/
// Notes on Votrax chip (on original Mockingboards):
// From Crimewave (Penguin Software):
// . Init:
// . DDRB = 0xFF
// . PCR = 0xB0
// . IER = 0x90
// . ORB = 0x03 (PAUSE0) or 0x3F (STOP)
// . IRQ:
// . ORB = Phoneme value
// . IRQ last phoneme complete:
// . IER = 0x10
// . ORB = 0x3F (STOP)
//
#include "StdAfx.h"
#include "Mockingboard.h"
#include "MockingboardDefs.h"
#include "6522.h"
#include "Core.h"
#include "CardManager.h"
#include "CPU.h"
#include "Log.h"
#include "Memory.h"
#include "SoundCore.h"
#include "SynchronousEventManager.h"
#include "YamlHelper.h"
#include "AY8910.h"
#include "SSI263.h"
#define DBG_MB_SS_CARD 0 // NB. From UI, select Mockingboard (not Phasor)
#define DBG_SUPPORT_ECHOPLUS 0 // Allow Phasor (in Echo+ mode) to pass the TMS5220 detection used by Echo+ disk
//---------------------------------------------------------------------------
MockingboardCard::MockingboardCard(UINT slot, SS_CARDTYPE type) : Card(type, slot), m_MBSubUnit{ {slot, type}, {slot, type} }
{
m_lastCumulativeCycle = 0;
m_lastAYUpdateCycle = 0;
for (UINT i = 0; i < NUM_VOICES; i++)
m_ppAYVoiceBuffer[i] = new short[MAX_SAMPLES]; // Buffer can hold a max of 0.37 seconds worth of samples (16384/44100)
m_inActiveCycleCount = 0;
m_regAccessedFlag = false;
m_isActive = false;
m_phasorEnable = (QueryType() == CT_Phasor);
m_phasorMode = PH_Mockingboard;
m_phasorClockScaleFactor = 1;
m_lastMBUpdateCycle = 0;
m_numSamplesError = 0;
//
for (int id = 0; id < kNumSyncEvents; id++)
{
int syncId = (m_slot << 4) + id; // NB. Encode the slot# into the id - used by MB_SyncEventCallback()
m_syncEvent[id] = new SyncEvent(syncId, 0, MB_SyncEventCallback);
}
for (UINT i = 0; i < NUM_SUBUNITS_PER_MB; i++)
{
m_MBSubUnit[i].nAY8910Number = i;
m_MBSubUnit[i].Reset(QueryType());
const UINT id0 = i * SY6522::kNumTimersPer6522 + 0; // TIMER1
const UINT id1 = i * SY6522::kNumTimersPer6522 + 1; // TIMER2
m_MBSubUnit[i].sy6522.InitSyncEvents(m_syncEvent[id0], m_syncEvent[id1]);
m_MBSubUnit[i].ssi263.SetDevice(i);
}
AY8910_InitAll((int)g_fCurrentCLK6502, SAMPLE_RATE);
LogFileOutput("MockingboardCard::ctor: AY8910_InitAll()\n");
Reset(true);
LogFileOutput("MockingboardCard::ctor: Reset()\n");
}
MockingboardCard::~MockingboardCard(void)
{
Destroy();
}
//---------------------------------------------------------------------------
bool MockingboardCard::IsAnyTimer1Active(void)
{
bool active = false;
for (UINT i = 0; i < NUM_SUBUNITS_PER_MB; i++)
active |= m_MBSubUnit[i].sy6522.IsTimer1Active();
return active;
}
//---------------------------------------------------------------------------
#ifdef _DEBUG
void MockingboardCard::Get6522IrqDescription(std::string& desc)
{
bool isIRQ = false;
for (UINT i = 0; i < NUM_SUBUNITS_PER_MB; i++)
{
if (m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & SY6522::IFR_IRQ)
{
isIRQ = true;
break;
}
}
if (!isIRQ)
return;
//
desc += "Slot-";
desc += m_slot;
desc += ": ";
for (UINT i = 0; i < NUM_SUBUNITS_PER_MB; i++)
{
if (m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & SY6522::IFR_IRQ)
{
if (m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & SY6522::IxR_TIMER1)
{
desc += ((i&1)==0) ? "A:" : "B:";
desc += "TIMER1 ";
}
if (m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & SY6522::IxR_TIMER2)
{
desc += ((i&1)==0) ? "A:" : "B:";
desc += "TIMER2 ";
}
if (m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & SY6522::IxR_VOTRAX)
{
desc += ((i&1)==0) ? "A:" : "B:";
desc += "VOTRAX ";
}
if (m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & SY6522::IxR_SSI263)
{
desc += ((i&1)==0) ? "A:" : "B:";
desc += "SSI263 ";
}
}
}
desc += "\n";
}
#endif
//-----------------------------------------------------------------------------
// Notes on Phasor's AY-3-8913 chip-select & r/w: (GH#1192)
// ----------------------------------------------
//
// Where: AY1 is the primary AY-3-8913 connected to 6522, and AY2 is the 2nd-ary.
//
// AFAICT, inputs to the Phasor GAL are:
// . ORB.b4:3 = Chip Select (CS) for AY1 & AY2 (active low)
// .(ORB.b2 : AY /RESET is not an input - see below)
// . ORB.b1:0 = PSG Function (INACTIVE, READ, WRITE, LATCH) [Or since LATCH=%11, then maybe a 2-input AND: b1.b0 -> GAL?]
// . Phasor mode (Mockingboard, Echo+, Phasor-native)
// . Slot inputs (address, reset, etc)
// And outputs from the GAL are:
// . GAL CS' for AY1 & AY2 (not just passed-through, but dependent on PSG Function)
// (Not PSG Function - probably just passed-through from 6522 to the chip-selected AY-3-8913's)
//
// Not an input to Phasor GAL:
// . ORB.b2 = AY /RESET (NB. not a PSG Function). Directly connected to AY's /RESET pin (or in Phasor's case: both AYs' /RESET pins).
//
// In Phasor-native mode, GAL logic:
// . AY2 LATCH func selects AY2 and AY1; sets latch addr for AY2 and AY1
// . AY1 LATCH func selects AY1; deselects AY2; sets latch addr for AY1
// . AY2 & AY1 LATCH func selects AY2 and AY1; sets latch addr for AY2 and AY1
// . AY2 WRITE(READ) func writes(reads) AY2 if it's selected
// . AY1 WRITE(READ) func writes(reads) AY1; writes(reads) AY2 if it's selected. NB. If both chips, then the READ is the OR-sum.
//
// EG, to do a "AY1 LATCH", then write 6522 ORB with b4:3=%01, b2:0=%111
//
void MockingboardCard::WriteToORB(BYTE subunit, BYTE subunitForAY/*=0*/)
{
BYTE value = m_MBSubUnit[subunit].sy6522.GetBusViewOfORB();
if ((QueryType() == CT_MockingboardC || QueryType() == CT_Phasor) && // Not CT_MegaAudio/CT_SDMusic
subunit == 0 && // SC01 only at $Cn00 (not $Cn80)
m_MBSubUnit[subunit].sy6522.Read(SY6522::rPCR) == 0xB0)
{
// Votrax speech data
const BYTE DDRB = m_MBSubUnit[subunit].sy6522.Read(SY6522::rDDRB);
m_MBSubUnit[subunit].ssi263.Votrax_Write((value & DDRB) | (DDRB ^ 0xff)); // DDRB's zero bits (inputs) are high impedence, so output as 1 (GH#952)
return;
}
#if DBG_MB_SS_CARD
if ((subunit & 1) == 1)
AY8910_Write(subunit, 0, nValue);
#else
if (m_phasorEnable)
{
const int kAY1 = 2; // Phasor/Echo+ mode: bit4=0 (active low) selects the 1st AY8913, ie. the only AY8913 in Mockingboard mode (confirmed on real Phasor h/w)
const int kAY2 = 1; // Phasor/Echo+ mode: bit3=0 (active low) selects the 2nd AY8913 attached to this 6522 (unavailable in Mockingboard mode)
const int nAY_CS = (m_phasorMode == PH_Phasor || m_phasorMode == PH_EchoPlus) ? (~(value >> 3) & 3)
: kAY1; // Anything else is Mockingboard
if (m_phasorMode == PH_EchoPlus)
subunit = SY6522_DEVICE_B;
if ((value & 4) == 0)
{
AY8913_Reset(subunit);
return;
}
// NB. For PH_Phasor, when selecting *both* AYs, then order matters: first do AY8913_DEVICE_A then AY8913_DEVICE_B
// Reason: from GAL logic: 'AY1 LATCH func' deselects AY2, then 'AY2 LATCH func' selects AY2 and AY1. (And we want both selected)
if (nAY_CS & kAY1)
AY8913_Write(subunit, AY8913_DEVICE_A, value);
if (nAY_CS & kAY2)
AY8913_Write(subunit, AY8913_DEVICE_B, value);
if (nAY_CS == 0)
m_MBSubUnit[subunit].SetBusState(false);
}
else
{
if ((value & 4) == 0)
{
if (QueryType() == CT_SDMusic)
AY8913_Reset(subunitForAY); // to do: check that AYs can be independently reset
else
AY8913_Reset(subunit);
return;
}
if (QueryType() == CT_SDMusic)
AY8913_Write(subunitForAY, AY8913_DEVICE_A, value);
else
AY8913_Write(subunit, AY8913_DEVICE_A, value);
}
#endif
}
//-----------------------------------------------------------------------------
void MockingboardCard::AY8913_Reset(BYTE subunit)
{
AY8910_reset(subunit, AY8913_DEVICE_A);
if (QueryType() == CT_Phasor)
AY8910_reset(subunit, AY8913_DEVICE_B); // GH#1197: Reset both AYs regardless of Phasor mode & chip-select bits
m_MBSubUnit[subunit].Reset(QueryType());
if (QueryType() == CT_SDMusic)
m_MBSubUnit[0].SetBusState(false);
}
void MockingboardCard::AY8913_Write(BYTE subunit, BYTE ay, BYTE value)
{
m_regAccessedFlag = true;
MB_SUBUNIT* pMB = &m_MBSubUnit[subunit];
SY6522& r6522 = (QueryType() != CT_SDMusic) ? pMB->sy6522 : m_MBSubUnit[0].sy6522;
bool busState = false; // Default: Mockingboard or Phasor(any mode) will read PortA inputs as high.
// Determine the AY8913 inputs
int nBDIR = (value & 2) ? 1 : 0;
const int nBC2 = 1; // Hardwired to +5V
int nBC1 = value & 1;
MockingboardUnitState_e nAYFunc = (MockingboardUnitState_e) ((nBDIR<<2) | (nBC2<<1) | nBC1);
MockingboardUnitState_e& state = pMB->state[ay]; // GH#659
#if _DEBUG
if (!m_phasorEnable || m_phasorMode == PH_Mockingboard)
_ASSERT(ay == AY8913_DEVICE_A);
if (nAYFunc == AY_READ || nAYFunc == AY_WRITE || nAYFunc == AY_LATCH)
if ((nAYFunc != state) || (m_phasorEnable && m_phasorMode != PH_EchoPlus)) // Deater's Xmas2023 demo interleaves writes to both AY's (need this line to avoid ASSERT for Echo+)
_ASSERT(state == AY_INACTIVE);
#endif
if (state == AY_INACTIVE) // GH#320: functions only work from inactive state
{
switch (nAYFunc)
{
case AY_INACTIVE: // 4: INACTIVE
break;
case AY_READ: // 5: READ FROM PSG (need to set DDRA to input)
{
if (pMB->isChipSelected[ay] && pMB->isAYLatchedAddressValid[ay])
{
r6522.SetRegIRA(AYReadReg(subunit, ay, pMB->nAYCurrentRegister[ay]) & (r6522.GetReg(SY6522::rDDRA) ^ 0xff));
busState = true;
}
if (m_phasorEnable && m_phasorMode == PH_Phasor) // GH#1192
{
if (ay == AY8913_DEVICE_A)
{
if (pMB->isChipSelected[AY8913_DEVICE_B] && pMB->isAYLatchedAddressValid[AY8913_DEVICE_B])
r6522.SetRegIRA(r6522.GetReg(SY6522::rORA) | (AYReadReg(subunit, AY8913_DEVICE_B, pMB->nAYCurrentRegister[AY8913_DEVICE_B]) & (r6522.GetReg(SY6522::rDDRA) ^ 0xff)));
}
}
}
break;
case AY_WRITE: // 6: WRITE TO PSG
if (pMB->isChipSelected[ay] && pMB->isAYLatchedAddressValid[ay])
_AYWriteReg(subunit, ay, pMB->nAYCurrentRegister[ay], r6522.GetReg(SY6522::rORA));
// else if invalid then just ignore
if (m_phasorEnable && m_phasorMode == PH_Phasor) // GH#1192
{
if (ay == AY8913_DEVICE_A)
{
if (pMB->isChipSelected[AY8913_DEVICE_B] && pMB->isAYLatchedAddressValid[AY8913_DEVICE_B])
_AYWriteReg(subunit, AY8913_DEVICE_B, pMB->nAYCurrentRegister[AY8913_DEVICE_B], r6522.GetReg(SY6522::rORA));
}
}
break;
case AY_LATCH: // 7: LATCH ADDRESS
// http://www.worldofspectrum.org/forums/showthread.php?t=23327
// Selecting an unused register number above 0x0f puts the AY into a state where
// any values written to the data/address bus are ignored, but can be read back
// within a few tens of thousands of cycles before they decay to zero.
if (r6522.GetReg(SY6522::rORA) <= 0x0F)
{
pMB->nAYCurrentRegister[ay] = r6522.GetReg(SY6522::rORA) & 0x0F;
pMB->isChipSelected[ay] = true;
pMB->isAYLatchedAddressValid[ay] = true;
if (m_phasorEnable && m_phasorMode == PH_Phasor) // GH#1192
{
if (ay == AY8913_DEVICE_A)
{
pMB->isChipSelected[AY8913_DEVICE_B] = false;
}
else // AY8913_DEVICE_B
{
pMB->isChipSelected[AY8913_DEVICE_A] = true;
pMB->nAYCurrentRegister[AY8913_DEVICE_A] = pMB->nAYCurrentRegister[AY8913_DEVICE_B];
pMB->isAYLatchedAddressValid[AY8913_DEVICE_A] = true;
}
}
}
// else Pro-Mockingboard (clone from HK)
break;
}
}
state = nAYFunc;
if (QueryType() == CT_SDMusic)
m_MBSubUnit[0].SetBusState(busState);
else
pMB->SetBusState(busState);
}
//-----------------------------------------------------------------------------
void MockingboardCard::UpdateIFRandIRQ(MB_SUBUNIT* pMB, BYTE clr_mask, BYTE set_mask)
{
pMB->sy6522.UpdateIFR(clr_mask, set_mask); // which calls UpdateIRQ()
}
//---------------------------------------------------------------------------
// Called from MockingboardCardMgr
bool MockingboardCard::Is6522IRQ(void)
{
// Now update the IRQ signal from all 6522s
// . OR-sum of all active TIMER1, TIMER2 & SPEECH sources (from all 6522s)
bool irq = false;
for (UINT i = 0; i < NUM_SUBUNITS_PER_MB; i++)
irq |= m_MBSubUnit[i].sy6522.GetReg(SY6522::rIFR) & 0x80 ? true : false;
// NB. Mockingboard generates IRQ on both 6522s:
// . SSI263's IRQ (A/!R) is routed via the 2nd 6522's CA1 input (at $Cn80) and must generate a 6502 IRQ (not NMI)
// - NB. 2nd SSI263's IRQ is routed via the 1st 6522's CA1 input (at $Cn00) and again generates a 6502 IRQ
// . SC-01's IRQ (!A/R) is routed via the 6522 at $Cn00 (NB. Only the Mockingboard "Sound/Speech I" card supports the SC-01)
// Phasor's SSI263 IRQ (A/!R) line is *also* wired directly to the 6502's IRQ (as well as the 6522's CA1)
return irq;
}
//---------------------------------------------------------------------------
// Called from class SSI263
UINT64 MockingboardCard::GetLastCumulativeCycles(void)
{
return m_lastCumulativeCycle;
}
void MockingboardCard::UpdateIFR(BYTE nDevice, BYTE clr_mask, BYTE set_mask)
{
UpdateIFRandIRQ(&m_MBSubUnit[nDevice], clr_mask, set_mask);
}
BYTE MockingboardCard::GetPCR(BYTE nDevice)
{
return m_MBSubUnit[nDevice].sy6522.GetReg(SY6522::rPCR);
}
//===========================================================================
// Called by:
// . MB_SyncEventCallback() -> MockingboardCardManager::UpdateSoundBuffer() on a TIMER1 (not TIMER2) underflow - when IsAnyTimer1Active() == true (for any MB)
// . MockingboardCardManager::Update() - when IsAnyTimer1Active() == false (for all MB's)
UINT MockingboardCard::MB_Update(void)
{
if (g_bFullSpeed)
{
// Keep AY reg writes relative to the current 'frame'
// - Required for Ultima3:
// . Tune ends
// . g_bFullSpeed:=true (disk-spinning) for ~50 frames
// . U3 sets AY_ENABLE:=0xFF (as a side-effect, this sets g_bFullSpeed:=false)
// o Without this, the write to AY_ENABLE gets ignored (since AY8910's /m_lastCumulativeCycle/ was last set 50 frame ago)
AY8910UpdateSetCycles();
// TODO:
// If any AY regs have changed then push them out to the AY chip
return 0;
}
//
if (!m_regAccessedFlag)
{
if (!m_inActiveCycleCount)
{
m_inActiveCycleCount = g_nCumulativeCycles;
}
else if (g_nCumulativeCycles - m_inActiveCycleCount > (unsigned __int64)g_fCurrentCLK6502 / 10)
{
// After 0.1 sec of Apple time, assume MB is not active
m_isActive = false;
}
}
else
{
m_inActiveCycleCount = 0;
m_regAccessedFlag = false;
m_isActive = true;
}
//
// For small timer periods, wait for a period of 500cy before updating DirectSound ring-buffer.
// NB. A timer period of less than 24cy will yield nNumSamplesPerPeriod=0.
const double kMinimumUpdateInterval = 500.0; // Arbitary (500 cycles = 21 samples)
const double kMaximumUpdateInterval = (double)(0xFFFF + 2); // Max 6522 timer interval (2756 samples)
if (m_lastMBUpdateCycle == 0)
m_lastMBUpdateCycle = m_lastCumulativeCycle; // Initial call to MB_Update() after reset/power-cycle
_ASSERT(m_lastCumulativeCycle >= m_lastMBUpdateCycle);
double updateInterval = (double)(m_lastCumulativeCycle - m_lastMBUpdateCycle);
if (updateInterval < kMinimumUpdateInterval)
return 0;
if (updateInterval > kMaximumUpdateInterval)
updateInterval = kMaximumUpdateInterval;
m_lastMBUpdateCycle = m_lastCumulativeCycle;
const double nIrqFreq = g_fCurrentCLK6502 / updateInterval + 0.5; // Round-up
const int nNumSamplesPerPeriod = (int)((double)SAMPLE_RATE / nIrqFreq); // Eg. For 60Hz this is 735
int nNumSamples = nNumSamplesPerPeriod + m_numSamplesError; // Apply correction
if (nNumSamples <= 0)
nNumSamples = 0;
if (nNumSamples > 2 * nNumSamplesPerPeriod)
nNumSamples = 2 * nNumSamplesPerPeriod;
if (nNumSamples > MAX_SAMPLES)
nNumSamples = MAX_SAMPLES; // Clamp to prevent buffer overflow
if (nNumSamples)
{
for (BYTE subunit = 0; subunit < NUM_SUBUNITS_PER_MB; subunit++)
{
for (BYTE ay = 0; ay < NUM_AY8913_PER_SUBUNIT; ay++)
{
const UINT chip = subunit * NUM_AY8913_PER_SUBUNIT + ay;
AY8910Update(subunit, ay, &m_ppAYVoiceBuffer[chip * NUM_VOICES_PER_AY8913], nNumSamples);
}
}
// Echo+ right speaker is also output to left speaker
if (m_phasorEnable && m_phasorMode == PH_EchoPlus)
{
for (UINT j = 0; j < NUM_VOICES_PER_AY8913; j++)
{
memcpy(m_ppAYVoiceBuffer[0 * NUM_VOICES_PER_AY8913 + j], m_ppAYVoiceBuffer[2 * NUM_VOICES_PER_AY8913 + j], nNumSamples * sizeof(short));
memcpy(m_ppAYVoiceBuffer[1 * NUM_VOICES_PER_AY8913 + j], m_ppAYVoiceBuffer[3 * NUM_VOICES_PER_AY8913 + j], nNumSamples * sizeof(short));
}
}
}
return (UINT) nNumSamples;
}
//-----------------------------------------------------------------------------
// NB. Called when /g_fCurrentCLK6502/ changes
void MockingboardCard::ReinitializeClock(void)
{
AY8910_InitClock((int)g_fCurrentCLK6502); // todo: account for g_PhasorClockScaleFactor?
// NB. Other calls to AY8910_InitClock() use the constant CLK_6502
}
//-----------------------------------------------------------------------------
void MockingboardCard::Destroy(void)
{
for (UINT i = 0; i < NUM_SSI263; i++)
m_MBSubUnit[i].ssi263.DSUninit();
for (UINT i = 0; i < NUM_VOICES; i++)
{
delete[] m_ppAYVoiceBuffer[i];
m_ppAYVoiceBuffer[i] = NULL;
}
for (UINT id = 0; id < kNumSyncEvents; id++)
{
if (m_syncEvent[id] && m_syncEvent[id]->m_active)
g_SynchronousEventMgr.Remove(m_syncEvent[id]->m_id);
delete m_syncEvent[id];
m_syncEvent[id] = NULL;
}
}
//-----------------------------------------------------------------------------
void MockingboardCard::Reset(const bool powerCycle) // CTRL+RESET or power-cycle
{
for (BYTE subunit = 0; subunit < NUM_SUBUNITS_PER_MB; subunit++)
{
m_MBSubUnit[subunit].sy6522.Reset(powerCycle);
for (BYTE ay = 0; ay < NUM_AY8913_PER_SUBUNIT; ay++)
AY8910_reset(subunit, ay);
m_MBSubUnit[subunit].Reset(QueryType());
m_MBSubUnit[subunit].ssi263.SetCardMode(PH_Mockingboard); // Revert to PH_Mockingboard mode
m_MBSubUnit[subunit].ssi263.Reset(powerCycle, m_phasorEnable);
}
// Reset state
{
SetCumulativeCycles();
m_inActiveCycleCount = 0;
m_regAccessedFlag = false;
m_isActive = false;
m_phasorMode = PH_Mockingboard;
m_phasorClockScaleFactor = 1;
m_lastMBUpdateCycle = 0;
for (int id = 0; id < kNumSyncEvents; id++)
{
if (m_syncEvent[id] && m_syncEvent[id]->m_active)
g_SynchronousEventMgr.Remove(m_syncEvent[id]->m_id);
}
// Not this, since no change on a CTRL+RESET or power-cycle:
// m_phasorEnable = false;
}
ReinitializeClock(); // Reset CLK for AY8910s
}
//-----------------------------------------------------------------------------
// Echo+ mode - Phasor's 2nd 6522 is mapped to every 16-byte offset in $Cnxx (Echo+ has a single 6522 controlling two AY-3-8913's)
BYTE __stdcall MockingboardCard::IORead(WORD PC, WORD nAddr, BYTE bWrite, BYTE nValue, ULONG nExecutedCycles)
{
UINT slot = (nAddr >> 8) & 0xf;
MockingboardCard* pCard = (MockingboardCard*)MemGetSlotParameters(slot);
return pCard->IOReadInternal(PC, nAddr, bWrite, nValue, nExecutedCycles);
}
BYTE MockingboardCard::IOReadInternal(WORD PC, WORD nAddr, BYTE bWrite, BYTE nValue, ULONG nExecutedCycles)
{
GetCardMgr().GetMockingboardCardMgr().UpdateCycles(nExecutedCycles);
#ifdef _DEBUG
if (!IS_APPLE2 && MemCheckINTCXROM())
{
_ASSERT(0); // Card ROM disabled, so IO_Cxxx() returns the internal ROM
return mem[nAddr];
}
#endif
if (m_phasorEnable)
{
int CS = 0;
if (m_phasorMode == PH_Mockingboard)
CS = ( ( nAddr & 0x80 ) >> 7 ) + 1; // 1 or 2
else if (m_phasorMode == PH_Phasor)
CS = ( ( nAddr & 0x80 ) >> 6 ) | ( ( nAddr & 0x10 ) >> 4 ); // 0, 1, 2 or 3
else if (m_phasorMode == PH_EchoPlus)
CS = 2;
BYTE nRes = 0;
if (CS & 1)
nRes |= m_MBSubUnit[SY6522_DEVICE_A].sy6522.Read(nAddr & 0xf);
if (CS & 2)
nRes |= m_MBSubUnit[SY6522_DEVICE_B].sy6522.Read(nAddr & 0xf);
bool bAccessedDevice = (CS & 3) ? true : false;
bool CS_SSI263 = !(nAddr & 0x10) && (nAddr & 0x60) && !(nAddr & 0x80); // SSI263 at $Cn2x and/or $Cn4x
if (m_phasorMode == PH_Phasor && CS_SSI263) // NB. Mockingboard mode: SSI263.bit7 not readable
{
_ASSERT(!bAccessedDevice); // In Phasor native mode, 6522 & SSI263 are interleaved in $Cn10-$Cn7F card I/O memory
if (nAddr & 0x40) // Primary SSI263
nRes = m_MBSubUnit[1].ssi263.Read(nExecutedCycles); // SSI263 only drives bit7
if (nAddr & 0x20) // Secondary SSI263
nRes = m_MBSubUnit[0].ssi263.Read(nExecutedCycles); // SSI263 only drives bit7
bAccessedDevice = true;
}
return bAccessedDevice ? nRes : MemReadFloatingBus(nExecutedCycles);
}
#if DBG_MB_SS_CARD
if (nMB == 1)
return MemReadFloatingBus(nExecutedCycles);
#endif
// NB. Mockingboard: SSI263.bit7 not readable
const BYTE subunit = QueryType() == CT_SDMusic ? SY6522_DEVICE_A : !(nAddr & 0x80) ? SY6522_DEVICE_A : SY6522_DEVICE_B;
const BYTE reg = nAddr & 0xf;
return m_MBSubUnit[subunit].sy6522.Read(reg);
}
//-----------------------------------------------------------------------------
BYTE __stdcall MockingboardCard::IOWrite(WORD PC, WORD nAddr, BYTE bWrite, BYTE nValue, ULONG nExecutedCycles)
{
UINT slot = (nAddr >> 8) & 0xf;
MockingboardCard* pCard = (MockingboardCard*)MemGetSlotParameters(slot);
return pCard->IOWriteInternal(PC, nAddr, bWrite, nValue, nExecutedCycles);
}
BYTE MockingboardCard::IOWriteInternal(WORD PC, WORD nAddr, BYTE bWrite, BYTE nValue, ULONG nExecutedCycles)
{
GetCardMgr().GetMockingboardCardMgr().UpdateCycles(nExecutedCycles);
#ifdef _DEBUG
if (!IS_APPLE2 && MemCheckINTCXROM())
{
_ASSERT(0); // Card ROM disabled, so IO_Cxxx() returns the internal ROM
return 0;
}
#endif
// Support 6502/65C02 false-reads of 6522 (GH#52)
if ( ((mem[(PC-2)&0xffff] == 0x91) && GetMainCpu() == CPU_6502) || // sta (zp),y - 6502 only (no-PX variant only) (UTAIIe:4-23)
(mem[(PC-3)&0xffff] == 0x99) || // sta abs16,y - 6502/65C02, but for 65C02 only the no-PX variant that does the false-read (UTAIIe:4-27)
(mem[(PC-3)&0xffff] == 0x9D) ) // sta abs16,x - 6502/65C02, but for 65C02 only the no-PX variant that does the false-read (UTAIIe:4-27)
{
WORD base;
WORD addr16;
if (mem[(PC-2)&0xffff] == 0x91)
{
BYTE zp = mem[(PC-1)&0xffff];
base = (mem[zp] | (mem[(zp+1)&0xff]<<8));
addr16 = base + regs.y;
}
else
{
base = mem[(PC-2)&0xffff] | (mem[(PC-1)&0xffff]<<8);
addr16 = base + ((mem[(PC-3)&0xffff] == 0x99) ? regs.y : regs.x);
}
if (((base ^ addr16) >> 8) == 0) // Only the no-PX variant does the false read (to the same I/O SELECT page)
{
_ASSERT(addr16 == nAddr);
if (addr16 == nAddr) // Check we've reverse looked-up the 6502 opcode correctly
{
if ( ((nAddr&0xf) == 4) || ((nAddr&0xf) == 8) ) // Only reading 6522 reg-4 or reg-8 actually has an effect
IOReadInternal(PC, nAddr, 0, 0, nExecutedCycles);
}
}
}
if (m_phasorEnable)
{
int CS = 0;
if (m_phasorMode == PH_Mockingboard)
CS = ( ( nAddr & 0x80 ) >> 7 ) + 1; // 1 or 2
else if (m_phasorMode == PH_Phasor)
CS = ( ( nAddr & 0x80 ) >> 6 ) | ( ( nAddr & 0x10 ) >> 4 ); // 0, 1, 2 or 3
else if (m_phasorMode == PH_EchoPlus)
CS = 2;
if (CS & 1)
{
const BYTE reg = nAddr & 0xf;
m_MBSubUnit[SY6522_DEVICE_A].sy6522.Write(reg, nValue);
if (reg == SY6522::rORB)
WriteToORB(SY6522_DEVICE_A);
}
if (CS & 2)
{
const BYTE reg = nAddr & 0xf;
m_MBSubUnit[SY6522_DEVICE_B].sy6522.Write(reg, nValue);
if (reg == SY6522::rORB)
WriteToORB(SY6522_DEVICE_B);
}
if (m_phasorMode == PH_Mockingboard || m_phasorMode == PH_Phasor) // No SSI263 for Echo+
{
// Confirmed that Phasor has no extra logic to map SSI263 (it's the same as Mockingboard's)
bool CS_SSI263_A = nAddr & 0x40; // SSI263 at $Cn4x-Cn7x, $CnCx-CnFx
bool CS_SSI263_B = nAddr & 0x20; // SSI263 at $Cn2x-Cn3x, $Cn6x-Cn7x, $CnAx-CnBx, $CnEx-CnFx
// NB. Mockingboard mode: writes to $Cn4x/SSI263 also get written to 1st 6522 (have confirmed on real Phasor h/w)
if (CS_SSI263_A) // Primary SSI263
m_MBSubUnit[1].ssi263.Write(nAddr&0x7, nValue); // 2nd 6522 is used for 1st speech chip
if (CS_SSI263_B) // Secondary SSI263
m_MBSubUnit[0].ssi263.Write(nAddr&0x7, nValue); // 1st 6522 is used for 2nd speech chip
}
return 0;
}
if (QueryType() == CT_SDMusic)
{
const BYTE subunit = SY6522_DEVICE_A; // Only one 6522
const BYTE reg = nAddr & 0xf;
m_MBSubUnit[subunit].sy6522.Write(reg, nValue);
if (reg == SY6522::rORB)
WriteToORB(subunit, !(nAddr & 0x80) ? SY6522_DEVICE_A : SY6522_DEVICE_B);
}
else
{
const BYTE subunit = !(nAddr & 0x80) ? SY6522_DEVICE_A : SY6522_DEVICE_B;
const BYTE reg = nAddr & 0xf;
m_MBSubUnit[subunit].sy6522.Write(reg, nValue);
if (reg == SY6522::rORB)
WriteToORB(subunit);
}
#if !DBG_MB_SS_CARD
if (QueryType() == CT_MockingboardC || QueryType() == CT_Phasor) // Not CT_MegaAudio/CT_SDMusic
{
if (nAddr & 0x40)
m_MBSubUnit[1].ssi263.Write(nAddr & 0x7, nValue); // 2nd 6522 is used for 1st speech chip
if (nAddr & 0x20)
m_MBSubUnit[0].ssi263.Write(nAddr & 0x7, nValue); // 1st 6522 is used for 2nd speech chip
}
#endif
return 0;
}
//-----------------------------------------------------------------------------
// Phasor's DEVICE SELECT' logic:
// . if addr.[b3]==1, then clear the card's mode bits b2:b0
// . if any of addr.[b2:b0] are a logic 1, then set these bits in the card's mode
//
// Example DEVICE SELECT' accesses for Phasor in slot-4: (from empirical observations on real Phasor h/w)
// 1)
// . RESET -> Mockingboard mode (b#000)
// . $C0C5 -> Phasor mode (b#101)
// 2)
// . RESET -> Mockingboard mode (b#000)
// . $C0C1, then $C0C4 (or $C0C4, then $C0C1) -> Phasor mode (b#101)
// . $C0C2 -> Echo+ mode (b#111)
// . $C0C5 -> remaining in Echo+ mode (b#111)
// So $C0C5 seemingly results in 2 different modes.
//
BYTE __stdcall MockingboardCard::PhasorIO(WORD PC, WORD nAddr, BYTE bWrite, BYTE nValue, ULONG nExecutedCycles)
{
UINT slot = ((nAddr & 0xff) >> 4) - 8;
MockingboardCard* pCard = (MockingboardCard*)MemGetSlotParameters(slot);
return pCard->PhasorIOInternal(PC, nAddr, bWrite, nValue, nExecutedCycles);
}
BYTE MockingboardCard::PhasorIOInternal(WORD PC, WORD nAddr, BYTE bWrite, BYTE nValue, ULONG nExecutedCycles)
{
if (!m_phasorEnable)
return MemReadFloatingBus(nExecutedCycles);
UINT bits = (UINT) m_phasorMode;
if (nAddr & 8)
bits = 0;
bits |= (nAddr & 7);
m_phasorMode = (PHASOR_MODE) bits;
if (m_phasorMode == PH_Mockingboard || m_phasorMode == PH_EchoPlus)
m_phasorClockScaleFactor = 1;
else if (m_phasorMode == PH_Phasor)
m_phasorClockScaleFactor = 2;
else // undefined mode
m_phasorClockScaleFactor = 1; // TODO: Check for undefined Phasor mode
if (m_phasorMode == PH_Mockingboard)
{
for (BYTE subunit = 0; subunit < NUM_SUBUNITS_PER_MB; subunit++)
m_MBSubUnit[subunit].isChipSelected[0] = true;
}
AY8910_InitClock((int)(Get6502BaseClock() * m_phasorClockScaleFactor));
for (UINT i = 0; i < NUM_SSI263; i++)
m_MBSubUnit[i].ssi263.SetCardMode(m_phasorMode); // TODO: Check for undefined Phasor mode
#if DBG_SUPPORT_ECHOPLUS
if (m_phasorMode == PH_EchoPlus && (nAddr & 0xf) == 0)
return 0x1f; // for TMS5220 detection
#endif
return MemReadFloatingBus(nExecutedCycles);
}
//-----------------------------------------------------------------------------
void MockingboardCard::InitializeIO(LPBYTE pCxRomPeripheral)
{
if (QueryType() == CT_Phasor)
RegisterIoHandler(m_slot, PhasorIO, PhasorIO, IORead, IOWrite, this, NULL);
else // All other Mockingboard variants
RegisterIoHandler(m_slot, IO_Null, IO_Null, IORead, IOWrite, this, NULL);
if (g_bDisableDirectSound || g_bDisableDirectSoundMockingboard)
return;
#ifdef NO_DIRECT_X
#else // NO_DIRECT_X
for (UINT i = 0; i < NUM_SSI263; i++)
{
if (!m_MBSubUnit[i].ssi263.DSInit())
break;
}
#endif // NO_DIRECT_X
}
//-----------------------------------------------------------------------------
void MockingboardCard::MuteControl(bool mute)
{
if (mute)
{
for (UINT i = 0; i < NUM_SSI263; i++)
m_MBSubUnit[i].ssi263.Mute();
}
else
{
for (UINT i = 0; i < NUM_SSI263; i++)
m_MBSubUnit[i].ssi263.Unmute();
}
}
//-----------------------------------------------------------------------------
#ifdef _DEBUG
void MockingboardCard::CheckCumulativeCycles(void)
{
_ASSERT(m_lastCumulativeCycle == g_nCumulativeCycles);
m_lastCumulativeCycle = g_nCumulativeCycles;
}
#endif
// Called by: ResetState() and Snapshot_LoadState_v2()
void MockingboardCard::SetCumulativeCycles(void)
{
m_lastCumulativeCycle = g_nCumulativeCycles;
}
// Called by ContinueExecution() at the end of every execution period (~1000 cycles or ~3 cycles when MODE_STEPPING)
void MockingboardCard::Update(const ULONG executedCycles)
{
for (UINT i = 0; i < NUM_SSI263; i++)
m_MBSubUnit[i].ssi263.PeriodicUpdate(executedCycles);
}
//-----------------------------------------------------------------------------
// Called by:
// . CpuExecute() every ~1000 cycles @ 1MHz (or ~3 cycles when MODE_STEPPING)
// . MB_SyncEventCallback() on a TIMER1/2 underflow
// . MB_Read() / MB_Write() (for both normal & full-speed)
void MockingboardCard::UpdateCycles(ULONG executedCycles)
{
CpuCalcCycles(executedCycles);
UINT64 uCycles = g_nCumulativeCycles - m_lastCumulativeCycle;
_ASSERT(uCycles >= 0);
if (uCycles == 0)
return;
m_lastCumulativeCycle = g_nCumulativeCycles;
_ASSERT(uCycles < 0x10000 || g_nAppMode == MODE_BENCHMARK);
USHORT nClocks = (USHORT)uCycles;
for (UINT i = 0; i < NUM_SUBUNITS_PER_MB; i++)
{
m_MBSubUnit[i].sy6522.UpdateTimer1(nClocks);
m_MBSubUnit[i].sy6522.UpdateTimer2(nClocks);
}
}
//-----------------------------------------------------------------------------
// Called on a 6522 TIMER1/2 underflow
int MockingboardCard::MB_SyncEventCallback(int id, int /*cycles*/, ULONG uExecutedCycles)
{
UINT slot = (id >> 4);
MockingboardCard* pCard = (MockingboardCard*)MemGetSlotParameters(slot);
return pCard->MB_SyncEventCallbackInternal(id, 0, uExecutedCycles);
}
int MockingboardCard::MB_SyncEventCallbackInternal(int id, int /*cycles*/, ULONG uExecutedCycles)
{
//UpdateCycles(uExecutedCycles); // Underflow: so keep TIMER1/2 counters in sync
// Update all MBs, so that m_lastCumulativeCycle remains in sync for all
GetCardMgr().GetMockingboardCardMgr().UpdateCycles(uExecutedCycles); // Underflow: so keep TIMER1/2 counters in sync
MB_SUBUNIT* pMB = &m_MBSubUnit[(id & 0xf) / SY6522::kNumTimersPer6522];
if ((id & 1) == 0)
{
_ASSERT(pMB->sy6522.IsTimer1Active());
UpdateIFRandIRQ(pMB, 0, SY6522::IxR_TIMER1);
GetCardMgr().GetMockingboardCardMgr().UpdateSoundBuffer();
if ((pMB->sy6522.GetReg(SY6522::rACR) & SY6522::ACR_RUNMODE) == SY6522::ACR_RM_FREERUNNING)
{
pMB->sy6522.StartTimer1();
if (pMB->sy6522.IsTimer1IrqDelay())
return 0x0001; // T1C=0xFFFF, which is really -1, as there's 1 cycle until underflow occurs
// TODO: can also be 0x0002 for MegaAudio
return pMB->sy6522.GetRegT1C() + SY6522::kExtraTimerCycles;
}
// One-shot mode
// - Phasor's playback code uses one-shot mode
pMB->sy6522.StopTimer1();
return 0; // Don't repeat event
}
else
{
// NB. Since not calling UpdateSoundBuffer(), then AppleWin doesn't (accurately?) support AY-playback using T2 (which is one-shot only)
_ASSERT(pMB->sy6522.IsTimer2Active());
UpdateIFRandIRQ(pMB, 0, SY6522::IxR_TIMER2);
pMB->sy6522.StopTimer2(); // TIMER2 only runs in one-shot mode
return 0; // Don't repeat event
}
}
//-----------------------------------------------------------------------------