/
Genesis.cs
491 lines (414 loc) · 14.6 KB
/
Genesis.cs
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
#define MUSASHI
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.Components;
using BizHawk.Emulation.Cores.Components.M68000;
using BizHawk.Emulation.Cores.Components.Z80;
using Native68000;
namespace BizHawk.Emulation.Cores.Sega.Genesis
{
[CoreAttributes(
"GenesisHawk",
"Vecna",
isPorted: false,
isReleased: false
)]
public sealed partial class Genesis : IEmulator, ISaveRam, IStatable, IInputPollable
{
private int _lagcount = 0;
private bool lagged = true;
private bool islag = false;
// ROM
public byte[] RomData;
// Machine stuff
public MC68000 MainCPU;
public Z80A SoundCPU;
public GenVDP VDP;
public SN76489 PSG;
public YM2612 YM2612;
public byte[] Ram = new byte[0x10000];
public byte[] Z80Ram = new byte[0x2000];
private bool M68000HasZ80Bus = false;
private bool Z80Reset = false;
private bool Z80Runnable { get { return (Z80Reset == false && M68000HasZ80Bus == false); } }
private SoundMixer SoundMixer;
[FeatureNotImplemented]
public IInputCallbackSystem InputCallbacks { get { throw new NotImplementedException(); } }
public void ResetCounters()
{
Frame = 0;
_lagcount = 0;
islag = false;
}
// Genesis timings:
// 53,693,175 Machine clocks / sec
// 7,670,454 Main 68000 cycles / sec (7 mclk divisor)
// 3,579,545 Z80 cycles / sec (15 mclk divisor)
// At 59.92 FPS:
// 896,081 mclks / frame
// 128,011 Main 68000 cycles / frame
// 59,738 Z80 cycles / frame
// At 262 lines/frame:
// 3420 mclks / line
// ~ 488.5 Main 68000 cycles / line
// 228 Z80 cycles / line
// Video characteristics:
// 224 lines are active display. The remaining 38 lines are vertical blanking.
// In H40 mode, the dot clock is 480 pixels per line.
// 320 are active display, the remaining 160 are horizontal blanking.
// A total of 3420 mclks per line, but 2560 mclks are active display and 860 mclks are blanking.
#if MUSASHI
VdpCallback _vdp;
ReadCallback read8;
ReadCallback read16;
ReadCallback read32;
WriteCallback write8;
WriteCallback write16;
WriteCallback write32;
#endif
public Genesis(CoreComm comm, GameInfo game, byte[] rom)
{
ServiceProvider = new BasicServiceProvider(this);
CoreComm = comm;
MainCPU = new MC68000();
SoundCPU = new Z80A();
YM2612 = new YM2612() { MaxVolume = 23405 };
PSG = new SN76489() { MaxVolume = 4681 };
VDP = new GenVDP();
VDP.DmaReadFrom68000 = ReadWord;
(ServiceProvider as BasicServiceProvider).Register<IVideoProvider>(VDP);
SoundMixer = new SoundMixer(YM2612, PSG);
MainCPU.ReadByte = ReadByte;
MainCPU.ReadWord = ReadWord;
MainCPU.ReadLong = ReadLong;
MainCPU.WriteByte = WriteByte;
MainCPU.WriteWord = WriteWord;
MainCPU.WriteLong = WriteLong;
MainCPU.IrqCallback = InterruptCallback;
// ---------------------- musashi -----------------------
#if MUSASHI
_vdp = vdpcallback;
read8 = Read8;
read16 = Read16;
read32 = Read32;
write8 = Write8;
write16 = Write16;
write32 = Write32;
Musashi.RegisterVdpCallback(Marshal.GetFunctionPointerForDelegate(_vdp));
Musashi.RegisterRead8(Marshal.GetFunctionPointerForDelegate(read8));
Musashi.RegisterRead16(Marshal.GetFunctionPointerForDelegate(read16));
Musashi.RegisterRead32(Marshal.GetFunctionPointerForDelegate(read32));
Musashi.RegisterWrite8(Marshal.GetFunctionPointerForDelegate(write8));
Musashi.RegisterWrite16(Marshal.GetFunctionPointerForDelegate(write16));
Musashi.RegisterWrite32(Marshal.GetFunctionPointerForDelegate(write32));
#endif
// ---------------------- musashi -----------------------
SoundCPU.ReadMemory = ReadMemoryZ80;
SoundCPU.WriteMemory = WriteMemoryZ80;
SoundCPU.WriteHardware = (a, v) => { Console.WriteLine("Z80: Attempt I/O Write {0:X2}:{1:X2}", a, v); };
SoundCPU.ReadHardware = x => 0xFF;
SoundCPU.IRQCallback = () => SoundCPU.Interrupt = false;
Z80Reset = true;
RomData = new byte[0x400000];
for (int i = 0; i < rom.Length; i++)
RomData[i] = rom[i];
SetupMemoryDomains();
#if MUSASHI
Musashi.Init();
Musashi.Reset();
VDP.GetPC = () => Musashi.PC;
#else
MainCPU.Reset();
VDP.GetPC = () => MainCPU.PC;
#endif
InitializeCartHardware(game);
}
public IEmulatorServiceProvider ServiceProvider { get; private set; }
void InitializeCartHardware(GameInfo game)
{
LogCartInfo();
InitializeEeprom(game);
InitializeSaveRam(game);
}
public void FrameAdvance(bool render, bool rendersound)
{
lagged = true;
Frame++;
PSG.BeginFrame(SoundCPU.TotalExecutedCycles);
YM2612.BeginFrame(SoundCPU.TotalExecutedCycles);
// Do start-of-frame events
VDP.HIntLineCounter = VDP.Registers[10];
//VDP.VdpStatusWord &=
unchecked { VDP.VdpStatusWord &= (ushort)~GenVDP.StatusVerticalBlanking; }
for (VDP.ScanLine = 0; VDP.ScanLine < 262; VDP.ScanLine++)
{
//Log.Error("VDP","FRAME {0}, SCANLINE {1}", Frame, VDP.ScanLine);
if (VDP.ScanLine < VDP.FrameHeight)
VDP.RenderLine();
Exec68k(365);
RunZ80(171);
// H-Int now?
VDP.HIntLineCounter--;
if (VDP.HIntLineCounter < 0 && VDP.ScanLine < 224) // FIXME
{
VDP.HIntLineCounter = VDP.Registers[10];
VDP.VdpStatusWord |= GenVDP.StatusHorizBlanking;
if (VDP.HInterruptsEnabled)
{
Set68kIrq(4);
//Console.WriteLine("Fire hint!");
}
}
Exec68k(488 - 365);
RunZ80(228 - 171);
if (VDP.ScanLine == 224)
{
VDP.VdpStatusWord |= GenVDP.StatusVerticalInterruptPending;
VDP.VdpStatusWord |= GenVDP.StatusVerticalBlanking;
Exec68k(16); // this is stupidly wrong.
// End-frame stuff
if (VDP.VInterruptEnabled)
Set68kIrq(6);
SoundCPU.Interrupt = true;
//The INT output is asserted every frame for exactly one scanline, and it can't be disabled. A very short Z80 interrupt routine would be triggered multiple times if it finishes within 228 Z80 clock cycles. I think (but cannot recall the specifics) that some games have delay loops in the interrupt handler for this very reason.
}
}
PSG.EndFrame(SoundCPU.TotalExecutedCycles);
YM2612.EndFrame(SoundCPU.TotalExecutedCycles);
if (lagged)
{
_lagcount++;
islag = true;
}
else
islag = false;
}
void Exec68k(int cycles)
{
#if MUSASHI
Musashi.Execute(cycles);
#else
MainCPU.ExecuteCycles(cycles);
#endif
}
void RunZ80(int cycles)
{
// I emulate the YM2612 synced to Z80 clock, for better or worse.
// So we still need to keep the Z80 cycle count accurate even if the Z80 isn't running.
if (Z80Runnable)
SoundCPU.ExecuteCycles(cycles);
else
SoundCPU.TotalExecutedCycles += cycles;
}
void Set68kIrq(int irq)
{
#if MUSASHI
Musashi.SetIRQ(irq);
#else
MainCPU.Interrupt = irq;
#endif
}
public IDictionary<string, RegisterValue> GetCpuFlagsAndRegisters()
{
return new Dictionary<string, RegisterValue>
{
{ "A-0", MainCPU.A[0].s32 },
{ "A-1", MainCPU.A[1].s32 },
{ "A-2", MainCPU.A[2].s32 },
{ "A-3", MainCPU.A[3].s32 },
{ "A-4", MainCPU.A[4].s32 },
{ "A-5", MainCPU.A[5].s32 },
{ "A-6", MainCPU.A[6].s32 },
{ "A-7", MainCPU.A[7].s32 },
{ "D-0", MainCPU.D[0].s32 },
{ "D-1", MainCPU.D[1].s32 },
{ "D-2", MainCPU.D[2].s32 },
{ "D-3", MainCPU.D[3].s32 },
{ "D-4", MainCPU.D[4].s32 },
{ "D-5", MainCPU.D[5].s32 },
{ "D-6", MainCPU.D[6].s32 },
{ "D-7", MainCPU.D[7].s32 },
{ "SR", MainCPU.SR },
{ "Flag X", MainCPU.X },
{ "Flag N", MainCPU.N },
{ "Flag Z", MainCPU.Z },
{ "Flag V", MainCPU.V },
{ "Flag C", MainCPU.C }
};
}
int vdpcallback(int level) // Musashi handler
{
InterruptCallback(level);
return -1;
}
void InterruptCallback(int level)
{
unchecked { VDP.VdpStatusWord &= (ushort)~GenVDP.StatusVerticalInterruptPending; }
}
public CoreComm CoreComm { get; private set; }
public ISoundProvider SoundProvider
{
get { return SoundMixer; }
}
public ISyncSoundProvider SyncSoundProvider { get { return new FakeSyncSound(SoundMixer, 735); } }
public bool StartAsyncSound() { return true; }
public void EndAsyncSound() { }
public int Frame { get; set; }
public int LagCount { get { return _lagcount; } set { _lagcount = value; } }
public bool IsLagFrame { get { return islag; } set { islag = value; } }
public bool DeterministicEmulation { get { return true; } }
public string SystemId { get { return "GEN"; } }
public string BoardName { get { return null; } }
public void SaveStateText(TextWriter writer)
{
var buf = new byte[141501 + SaveRAM.Length];
var stream = new MemoryStream(buf);
var bwriter = new BinaryWriter(stream);
SaveStateBinary(bwriter);
writer.WriteLine("Version 1");
writer.Write("BigFatBlob ");
buf.SaveAsHex(writer);
/*writer.WriteLine("[MegaDrive]");
MainCPU.SaveStateText(writer, "Main68K");
SoundCPU.SaveStateText(writer);
PSG.SaveStateText(writer);
VDP.SaveStateText(writer);
writer.WriteLine("Frame {0}", Frame);
writer.WriteLine("Lag {0}", _lagcount);
writer.WriteLine("IsLag {0}", islag);
writer.Write("MainRAM ");
Ram.SaveAsHex(writer);
writer.Write("Z80RAM ");
Z80Ram.SaveAsHex(writer);
writer.WriteLine("[/MegaDrive]");*/
}
public void LoadStateText(TextReader reader)
{
var buf = new byte[141501 + SaveRAM.Length];
var version = reader.ReadLine();
if (version != "Version 1")
throw new Exception("Not a valid state vesrion! sorry! your state is bad! Robust states will be added later!");
var omgstate = reader.ReadLine().Split(' ')[1];
buf.ReadFromHex(omgstate);
LoadStateBinary(new BinaryReader(new MemoryStream(buf)));
/*while (true)
{
string[] args = reader.ReadLine().Split(' ');
if (args[0].Trim() == "") continue;
if (args[0] == "[MegaDrive]") continue;
if (args[0] == "[/MegaDrive]") break;
if (args[0] == "MainRAM")
Ram.ReadFromHex(args[1]);
else if (args[0] == "Z80RAM")
Z80Ram.ReadFromHex(args[1]);
else if (args[0] == "[Main68K]")
MainCPU.LoadStateText(reader, "Main68K");
else if (args[0] == "[Z80]")
SoundCPU.LoadStateText(reader);
else if (args[0] == "Frame")
Frame = int.Parse(args[1]);
else if (args[0] == "Lag")
_lagcount = int.Parse(args[1]);
else if (args[0] == "IsLag")
islag = bool.Parse(args[1]);
else if (args[0] == "[PSG]")
PSG.LoadStateText(reader);
else if (args[0] == "[VDP]")
VDP.LoadStateText(reader);
else
Console.WriteLine("Skipping unrecognized identifier " + args[0]);
}*/
}
public void SaveStateBinary(BinaryWriter writer)
{
Musashi.SaveStateBinary(writer); // 124
//SoundCPU.SaveStateBinary(writer); // 46 TODO fix this, there is no way to invoke this core from the UI for testing anyway.
//PSG.SaveStateBinary(writer); // 15
VDP.SaveStateBinary(writer); // 65781
YM2612.SaveStateBinary(writer); // 1785
writer.Write(Ram); // 65535
writer.Write(Z80Ram); // 8192
writer.Write(Frame); // 4
writer.Write(M68000HasZ80Bus); // 1
writer.Write(Z80Reset); // 1
writer.Write(BankRegion); // 4
for (int i = 0; i < 3; i++)
{
writer.Write(IOPorts[i].Data);
writer.Write(IOPorts[i].TxData);
writer.Write(IOPorts[i].RxData);
writer.Write(IOPorts[i].SCtrl);
}
if (SaveRAM.Length > 0)
writer.Write(SaveRAM);
// TODO: EEPROM/cart HW state
// TODO: lag counter crap
}
public void LoadStateBinary(BinaryReader reader)
{
Musashi.LoadStateBinary(reader);
//SoundCPU.LoadStateBinary(reader);
//PSG.LoadStateBinary(reader);
VDP.LoadStateBinary(reader);
YM2612.LoadStateBinary(reader);
Ram = reader.ReadBytes(Ram.Length);
Z80Ram = reader.ReadBytes(Z80Ram.Length);
Frame = reader.ReadInt32();
M68000HasZ80Bus = reader.ReadBoolean();
Z80Reset = reader.ReadBoolean();
BankRegion = reader.ReadInt32();
for (int i = 0; i < 3; i++)
{
IOPorts[i].Data = reader.ReadByte();
IOPorts[i].TxData = reader.ReadByte();
IOPorts[i].RxData = reader.ReadByte();
IOPorts[i].SCtrl = reader.ReadByte();
}
if (SaveRAM.Length > 0)
SaveRAM = reader.ReadBytes(SaveRAM.Length);
}
public byte[] SaveStateBinary()
{
var buf = new byte[141501 + SaveRAM.Length];
var stream = new MemoryStream(buf);
var writer = new BinaryWriter(stream);
SaveStateBinary(writer);
//Console.WriteLine("buf len = {0}", stream.Position);
writer.Close();
return buf;
}
public bool BinarySaveStatesPreferred { get { return false; } }
MemoryDomainList memoryDomains;
void SetupMemoryDomains()
{
var domains = new List<MemoryDomain>(3);
var MainMemoryDomain = new MemoryDomain("Main RAM", Ram.Length, MemoryDomain.Endian.Big,
addr => Ram[addr & 0xFFFF],
(addr, value) => Ram[addr & 0xFFFF] = value);
var Z80Domain = new MemoryDomain("Z80 RAM", Z80Ram.Length, MemoryDomain.Endian.Little,
addr => Z80Ram[addr & 0x1FFF],
(addr, value) => { Z80Ram[addr & 0x1FFF] = value; });
var VRamDomain = new MemoryDomain("Video RAM", VDP.VRAM.Length, MemoryDomain.Endian.Big,
addr => VDP.VRAM[addr & 0xFFFF],
(addr, value) => VDP.VRAM[addr & 0xFFFF] = value);
var RomDomain = new MemoryDomain("MD CART", RomData.Length, MemoryDomain.Endian.Big,
addr => RomData[addr], //adelikat: For speed considerations, I didn't mask this, every tool that uses memory domains is smart enough not to overflow, if I'm wrong let me know!
(addr, value) => RomData[addr & (RomData.Length - 1)] = value);
var SystemBusDomain = new MemoryDomain("System Bus", 0x1000000, MemoryDomain.Endian.Big,
addr => (byte)ReadByte((int)addr),
(addr, value) => Write8((uint)addr, (uint)value));
domains.Add(MainMemoryDomain);
domains.Add(Z80Domain);
domains.Add(VRamDomain);
domains.Add(RomDomain);
domains.Add(SystemBusDomain);
memoryDomains = new MemoryDomainList(domains);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(memoryDomains);
}
public void Dispose() { }
}
}