-
Notifications
You must be signed in to change notification settings - Fork 9
/
DEV9.cs
384 lines (364 loc) · 11.5 KB
/
DEV9.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
using CLRDEV9.Config;
using PSE;
using PSE.CLR_PSE_Callbacks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace CLRDEV9
{
internal class CLR_DEV9
{
#if DEBUG
private const string libraryName = "CLR DEV9 DEBUG";
#else
private const string libraryName = "CLR DEV9";
#endif
private static string logFolderPath = "logs";
private static string iniFolderPath = "inis";
private static DEV9.DEV9_State dev9 = null;
private static bool doLog = true;
public static string Name { get { return libraryName; } }
private static void LogInit()
{
if (doLog)
{
//some legwork to setup the logger
Dictionary<ushort, string> logSources = new Dictionary<ushort, string>();
IEnumerable<DEV9LogSources> sources = Enum.GetValues(typeof(DEV9LogSources)).Cast<DEV9LogSources>();
foreach (DEV9LogSources source in sources)
{
logSources.Add((ushort)source, source.ToString());
}
CLR_PSE_PluginLog.Open(logFolderPath, "DEV9_CLR.log", "CLR_DEV9", logSources);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.Test);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.Dev9);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.SPEED);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.SMAP);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.ATA);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.Winsock);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.NetAdapter);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.UDPSession);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.DNSPacket);
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.DNSSession);
#if DEBUG
CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.PluginInterface);
//CLR_PSE_PluginLog.SetSourceUseStdOut(true, (int)DEV9LogSources.TCPSession);
//Info is defualt
CLR_PSE_PluginLog.SetFileLevel(SourceLevels.All);
//CLR_PSE_PluginLog.SetSourceLogLevel(SourceLevels.All, (int)DEV9LogSources.ATA);
//CLR_PSE_PluginLog.SetSourceLogLevel(SourceLevels.All, (int)DEV9LogSources.TCPSession);
CLR_PSE_PluginLog.SetSourceLogLevel(SourceLevels.All, (int)DEV9LogSources.Dev9);
CLR_PSE_PluginLog.SetSourceLogLevel(SourceLevels.All, (int)DEV9LogSources.SMAP);
#endif
doLog = false;
}
}
public static Int32 Init()
{
try
{
LogInit();
Log_Info("Init");
dev9 = new DEV9.DEV9_State();
Log_Info("Init ok");
return 0;
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
return -1;
}
}
public static Int32 Open(IntPtr winHandle)
{
try
{
int ret = 0;
Log_Info("Open");
ConfigFile.LoadConf(iniFolderPath, "CLR_DEV9.ini");
Log_Info("Config Loaded");
if (DEV9Header.config.Hdd.Contains("\\") || DEV9Header.config.Hdd.Contains("/"))
ret = dev9.Open(DEV9Header.config.Hdd);
else
ret = dev9.Open(iniFolderPath + "\\" + DEV9Header.config.Hdd);
if (ret == 0)
Log_Info("Open ok");
else
CLR_PSE_PluginLog.MsgBoxErrorTrapper(new Exception("Open Failed"));
return ret;
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
return -1;
}
}
public static void Close()
{
try
{
dev9.Close();
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void Shutdown()
{
try
{
Log_Info("Shutdown");
CLR_PSE_PluginLog.Close();
//PluginLog.Close(); //fclose(dev9Log);
if (irqHandle.IsAllocated)
{
irqHandle.Free(); //allow garbage collection
}
//Do dispose()? (of what?)
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void SetSettingsDir(string dir)
{
try
{
iniFolderPath = dir;
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void SetLogDir(string dir)
{
try
{
logFolderPath = dir;
//LogInit();
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static byte DEV9read8(uint addr)
{
try
{
return dev9.DEV9read8(addr);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static ushort DEV9read16(uint addr)
{
try
{
return dev9.DEV9_Read16(addr);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static uint DEV9read32(uint addr)
{
try
{
return dev9.DEV9_Read32(addr);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void DEV9write8(uint addr, byte value)
{
try
{
dev9.DEV9_Write8(addr, value);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void DEV9write16(uint addr, ushort value)
{
try
{
dev9.DEV9_Write16(addr, value);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void DEV9write32(uint addr, uint value)
{
try
{
dev9.DEV9_Write32(addr, value);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
unsafe public static void DEV9readDMA8Mem(byte* memPointer, int size)
{
try
{
System.IO.UnmanagedMemoryStream pMem = new System.IO.UnmanagedMemoryStream(memPointer, size, size, System.IO.FileAccess.Write);
dev9.DEV9_ReadDMA8Mem(pMem, size);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
unsafe public static void DEV9writeDMA8Mem(byte* memPointer, int size)
{
try
{
System.IO.UnmanagedMemoryStream pMem = new System.IO.UnmanagedMemoryStream(memPointer, size, size, System.IO.FileAccess.Read);
dev9.DEV9_WriteDMA8Mem(pMem, size);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void DEV9async(uint cycles)
{
try
{
dev9.DEV9_Async(cycles);
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static void DEV9irqCallback(CLR_CyclesCallback callback)
{
try
{
DEV9Header.DEV9irq = callback;
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
static GCHandle irqHandle;
public static int _DEV9irqHandler()
{
try
{
return dev9._DEV9irqHandler();
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
public static CLR_IRQHandler DEV9irqHandler()
{
try
{
// Pass our handler to pcsx2.
if (irqHandle.IsAllocated)
{
irqHandle.Free(); //allow garbage collection
}
Log_Info("Get IRQ");
CLR_IRQHandler fp = new CLR_IRQHandler(_DEV9irqHandler);
irqHandle = GCHandle.Alloc(fp); //prevent GC
return fp;
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
//freeze
//config
//about
public static int Test()
{
try
{
CLR_PSE_Version_PCSX2 minVer = new CLR_PSE_Version_PCSX2(1, 3, 1);
if (CLR_PSE.EmuName != "PCSX2")
{
return 0; //Hope it works
}
if (CLR_PSE.EmuVersion >= minVer)
{
return 0;
}
else
{
return -1;
}
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
return 1;
}
}
public static void Configure()
{
try
{
LogInit();
ConfigFile.LoadConf(iniFolderPath, "CLR_DEV9.ini");
ConfigFile.DoConfig(iniFolderPath, "CLR_DEV9.ini");
ConfigFile.SaveConf(iniFolderPath, "CLR_DEV9.ini");
}
catch (Exception e)
{
CLR_PSE_PluginLog.MsgBoxErrorTrapper(e);
throw;
}
}
private static void Log_Error(string str)
{
CLR_PSE_PluginLog.WriteLine(TraceEventType.Error, (int)DEV9LogSources.PluginInterface, str);
}
private static void Log_Info(string str)
{
CLR_PSE_PluginLog.WriteLine(TraceEventType.Information, (int)DEV9LogSources.PluginInterface, str);
}
private static void Log_Verb(string str)
{
CLR_PSE_PluginLog.WriteLine(TraceEventType.Verbose, (int)DEV9LogSources.PluginInterface, str);
}
}
}