From 5ef3d80f47d0729e2fbbe2363da40f57cf8c8723 Mon Sep 17 00:00:00 2001 From: "feal87@gmail.com" Date: Sun, 19 Dec 2010 14:41:11 +0000 Subject: [PATCH] Rewritten all the dump functionality to be completely managed by my application and not relying anymore on gsdx REPLAY function. This is the first of a series of commit. The plan is to have a decent debug tool to understand problems with gsdx. Plan to do some other commit to clear up the code not used anymore git-svn-id: http://pcsx2.googlecode.com/svn/trunk@4104 96395faa-99c1-11dd-bbfe-3dabce05a288 --- tools/GSDumpGUI/Core/Program.cs | 6 +- tools/GSDumpGUI/GSDumpGUI.csproj | 4 +- tools/GSDumpGUI/Library/GSDXWrapper.cs | 157 ++++++++++++++++++++++- tools/GSDumpGUI/Library/GSDump/GSData.cs | 20 +++ tools/GSDumpGUI/Library/GSDump/GSDump.cs | 84 ++++++++++++ tools/GSDumpGUI/Library/NativeMethods.cs | 4 + 6 files changed, 267 insertions(+), 8 deletions(-) create mode 100644 tools/GSDumpGUI/Library/GSDump/GSData.cs create mode 100644 tools/GSDumpGUI/Library/GSDump/GSDump.cs diff --git a/tools/GSDumpGUI/Core/Program.cs b/tools/GSDumpGUI/Core/Program.cs index ee7698911f..3e50e15798 100644 --- a/tools/GSDumpGUI/Core/Program.cs +++ b/tools/GSDumpGUI/Core/Program.cs @@ -55,11 +55,9 @@ namespace GSDumpGUI Directory.SetCurrentDirectory(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "GSDumpGSDXConfigs\\" + Path.GetFileName(DLLPath) + "\\")); if (Operation == "GSReplay") { + GSDump dump = GSDump.LoadDump(DumpPath); + wrap.Run(dump, Renderer); ChangeIcon = true; - if (Renderer != -1) - wrap.GSReplayDump(Renderer + " " + DumpPath); - else - wrap.GSReplayDump(DumpPath); } else wrap.GSConfig(); diff --git a/tools/GSDumpGUI/GSDumpGUI.csproj b/tools/GSDumpGUI/GSDumpGUI.csproj index 1631d2bd9a..b3f957acfd 100644 --- a/tools/GSDumpGUI/GSDumpGUI.csproj +++ b/tools/GSDumpGUI/GSDumpGUI.csproj @@ -36,7 +36,7 @@ TRACE prompt 4 - false + true x86 @@ -54,6 +54,8 @@ frmMain.cs + + diff --git a/tools/GSDumpGUI/Library/GSDXWrapper.cs b/tools/GSDumpGUI/Library/GSDXWrapper.cs index 79b430cb1b..21f208f7f0 100644 --- a/tools/GSDumpGUI/Library/GSDXWrapper.cs +++ b/tools/GSDumpGUI/Library/GSDXWrapper.cs @@ -6,21 +6,48 @@ using System.IO; namespace GSDumpGUI { + public delegate void GSReplay(IntPtr HWND, IntPtr HInstance, String File, Boolean Show); + public delegate void GSgifTransfer1(IntPtr data, int size); + public delegate void GSgifTransfer2(IntPtr data, int size); + public delegate void GSgifTransfer3(IntPtr data, int size); + public delegate void GSVSync(byte field); + public delegate void GSreadFIFO2(IntPtr data, int size); + public delegate void GSsetGameCRC(int crc, int options); + public delegate void GSfreeze(int mode, IntPtr data); + public delegate void GSopen(IntPtr hwnd, String Title, int renderer); + public delegate void GSclose(); + public delegate void GSshutdown(); + public delegate void GSConfigure(); + public delegate void GSsetBaseMem(IntPtr data); + public delegate IntPtr PSEgetLibName(); + public delegate void GSinit(); + public class GSDXWrapper { - private delegate void GSReplay(IntPtr HWND, IntPtr HInstance, String File, Boolean Show); - private delegate void GSConfigure(); - private delegate IntPtr PSEgetLibName(); private GSReplay gsReplay; private GSConfigure gsConfigure; private PSEgetLibName PsegetLibName; + private GSgifTransfer1 GSgifTransfer1; + private GSgifTransfer2 GSgifTransfer2; + private GSgifTransfer3 GSgifTransfer3; + private GSVSync GSVSync; + private GSreadFIFO2 GSreadFIFO2; + private GSsetGameCRC GSsetGameCRC; + private GSfreeze GSfreeze; + private GSopen GSopen; + private GSclose GSclose; + private GSshutdown GSshutdown; + private GSsetBaseMem GSsetBaseMem; + private GSinit GSinit; private Boolean Loaded; private String DLL; private IntPtr DLLAddr; + private Boolean Running; + static public Boolean IsValidGSDX(String DLL) { NativeMethods.SetErrorMode(0x8007); @@ -34,6 +61,19 @@ namespace GSDumpGUI IntPtr funcaddrLibName = NativeMethods.GetProcAddress(hmod, "PS2EgetLibName"); IntPtr funcaddrConfig = NativeMethods.GetProcAddress(hmod, "GSconfigure"); + IntPtr funcaddrGIF1 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer1"); + IntPtr funcaddrGIF2 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer2"); + IntPtr funcaddrGIF3 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer3"); + IntPtr funcaddrVSync = NativeMethods.GetProcAddress(hmod, "GSvsync"); + IntPtr funcaddrSetBaseMem = NativeMethods.GetProcAddress(hmod, "GSsetBaseMem"); + IntPtr funcaddrOpen = NativeMethods.GetProcAddress(hmod, "GSopen"); + IntPtr funcaddrSetCRC = NativeMethods.GetProcAddress(hmod, "GSsetGameCRC"); + IntPtr funcaddrClose = NativeMethods.GetProcAddress(hmod, "GSclose"); + IntPtr funcaddrShutdown = NativeMethods.GetProcAddress(hmod, "GSshutdown"); + IntPtr funcaddrFreeze = NativeMethods.GetProcAddress(hmod, "GSfreeze"); + IntPtr funcaddrGSreadFIFO2 = NativeMethods.GetProcAddress(hmod, "GSreadFIFO2"); + IntPtr funcaddrinit = NativeMethods.GetProcAddress(hmod, "GSinit"); + NativeMethods.FreeLibrary(hmod); if (!((funcaddrConfig.ToInt64() > 0) && (funcaddrLibName.ToInt64() > 0) && (funcaddrReplay.ToInt64() > 0))) { @@ -73,9 +113,36 @@ namespace GSDumpGUI IntPtr funcaddrLibName = NativeMethods.GetProcAddress(hmod, "PS2EgetLibName"); IntPtr funcaddrConfig = NativeMethods.GetProcAddress(hmod, "GSconfigure"); + IntPtr funcaddrGIF1 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer1"); + IntPtr funcaddrGIF2 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer2"); + IntPtr funcaddrGIF3 = NativeMethods.GetProcAddress(hmod, "GSgifTransfer3"); + IntPtr funcaddrVSync = NativeMethods.GetProcAddress(hmod, "GSvsync"); + IntPtr funcaddrSetBaseMem = NativeMethods.GetProcAddress(hmod, "GSsetBaseMem"); + IntPtr funcaddrOpen = NativeMethods.GetProcAddress(hmod, "GSopen"); + IntPtr funcaddrSetCRC = NativeMethods.GetProcAddress(hmod, "GSsetGameCRC"); + IntPtr funcaddrClose = NativeMethods.GetProcAddress(hmod, "GSclose"); + IntPtr funcaddrShutdown = NativeMethods.GetProcAddress(hmod, "GSshutdown"); + IntPtr funcaddrFreeze = NativeMethods.GetProcAddress(hmod, "GSfreeze"); + IntPtr funcaddrGSreadFIFO2 = NativeMethods.GetProcAddress(hmod, "GSreadFIFO2"); + IntPtr funcaddrinit = NativeMethods.GetProcAddress(hmod, "GSinit"); + gsReplay = (GSReplay)Marshal.GetDelegateForFunctionPointer(funcaddrReplay, typeof(GSReplay)); gsConfigure = (GSConfigure)Marshal.GetDelegateForFunctionPointer(funcaddrConfig, typeof(GSConfigure)); PsegetLibName = (PSEgetLibName)Marshal.GetDelegateForFunctionPointer(funcaddrLibName, typeof(PSEgetLibName)); + + this.GSgifTransfer1 = (GSgifTransfer1)Marshal.GetDelegateForFunctionPointer(funcaddrGIF1, typeof(GSgifTransfer1)); + this.GSgifTransfer2 = (GSgifTransfer2)Marshal.GetDelegateForFunctionPointer(funcaddrGIF2, typeof(GSgifTransfer2)); + this.GSgifTransfer3 = (GSgifTransfer3)Marshal.GetDelegateForFunctionPointer(funcaddrGIF3, typeof(GSgifTransfer3)); + this.GSVSync = (GSVSync)Marshal.GetDelegateForFunctionPointer(funcaddrVSync, typeof(GSVSync)); + this.GSsetBaseMem = (GSsetBaseMem)Marshal.GetDelegateForFunctionPointer(funcaddrSetBaseMem, typeof(GSsetBaseMem)); + this.GSopen = (GSopen)Marshal.GetDelegateForFunctionPointer(funcaddrOpen, typeof(GSopen)); + this.GSsetGameCRC = (GSsetGameCRC)Marshal.GetDelegateForFunctionPointer(funcaddrSetCRC, typeof(GSsetGameCRC)); + this.GSclose = (GSclose)Marshal.GetDelegateForFunctionPointer(funcaddrClose, typeof(GSclose)); + this.GSshutdown = (GSshutdown)Marshal.GetDelegateForFunctionPointer(funcaddrShutdown, typeof(GSshutdown)); + this.GSfreeze = (GSfreeze)Marshal.GetDelegateForFunctionPointer(funcaddrFreeze, typeof(GSfreeze)); + this.GSreadFIFO2 = (GSreadFIFO2)Marshal.GetDelegateForFunctionPointer(funcaddrGSreadFIFO2, typeof(GSreadFIFO2)); + this.GSinit = (GSinit)Marshal.GetDelegateForFunctionPointer(funcaddrinit, typeof(GSinit)); + DLLAddr = hmod; } NativeMethods.SetErrorMode(0x0000); @@ -107,5 +174,89 @@ namespace GSDumpGUI throw new Exception("GSDX is not loaded"); return Marshal.PtrToStringAnsi(PsegetLibName.Invoke()); } + + public unsafe void Run(GSDump dump, int rendererOverride) + { + Running = true; + GSinit(); + fixed (byte* pointer = dump.Registers) + { + GSsetBaseMem(new IntPtr(pointer)); + Int32 HWND = 0; + GSopen(new IntPtr(&HWND), "", rendererOverride); + GSsetGameCRC(dump.CRC, 0); + fixed (byte* freeze = dump.StateData) + { + GSfreeze(0, new IntPtr(freeze)); + GSVSync(1); + + while (Running) + { /*"C:\Users\Alessio\Desktop\Plugins\Dll\gsdx-sse4-r3878.dll" "C:\Users\Alessio\Desktop\Plugins\Dumps\gsdx_20100603052628.gs" "GSReplay" 0*/ + if (!NativeMethods.IsWindowVisible(new IntPtr(HWND))) + { + Running = false; + break; + } + foreach (var itm in dump.Data) + { + switch (itm.id) + { + case GSType.Transfer: + switch (itm.data[0]) + { + case 0: + fixed (byte* gifdata = itm.data) + { + byte[] data = new byte[4]; + data[0]=*(gifdata+1); + data[1]=*(gifdata+2); + data[2]=*(gifdata+3); + data[3]=*(gifdata+4); + Int32 size = BitConverter.ToInt32(data, 0); + GSgifTransfer1(new IntPtr(gifdata + 5), 16384 - size); + } + break; + case 1: + fixed (byte* gifdata = itm.data) + { + GSgifTransfer2(new IntPtr(gifdata + 1), (itm.data.Length - 1) / 16); + } + break; + case 2: + fixed (byte* gifdata = itm.data) + { + GSgifTransfer3(new IntPtr(gifdata + 1), (itm.data.Length - 1) / 16); + } + break; + } + break; + case GSType.VSync: + GSVSync(itm.data[0]); + break; + case GSType.ReadFIFO2: + fixed (byte* FIFO = itm.data) + { + GSreadFIFO2(new IntPtr(FIFO), itm.data.Length / 16); + } + break; + case GSType.Registers: + Marshal.Copy(itm.data, 0, new IntPtr(pointer), 8192); + break; + default: + break; + } + } + } + + GSclose(); + GSshutdown(); + } + } + } + + public void Stop() + { + Running = false; + } } } diff --git a/tools/GSDumpGUI/Library/GSDump/GSData.cs b/tools/GSDumpGUI/Library/GSDump/GSData.cs new file mode 100644 index 0000000000..cd06622db6 --- /dev/null +++ b/tools/GSDumpGUI/Library/GSDump/GSData.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GSDumpGUI +{ + public class GSData + { + public GSType id; + public byte[] data; + } + + public enum GSType + { + Transfer = 0, + VSync = 1, + ReadFIFO2 = 2, + Registers = 3 + } +} diff --git a/tools/GSDumpGUI/Library/GSDump/GSDump.cs b/tools/GSDumpGUI/Library/GSDump/GSDump.cs new file mode 100644 index 0000000000..c1303141c3 --- /dev/null +++ b/tools/GSDumpGUI/Library/GSDump/GSDump.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; + +namespace GSDumpGUI +{ + public class GSDump + { + public Int32 CRC; + public byte[] StateData; + public byte[] Registers; // 8192 bytes + + public List Data; + + public GSDump() + { + Data = new List(); + } + + static public GSDump LoadDump(String FileName) + { + GSDump dmp = new GSDump(); + + BinaryReader br = new BinaryReader(System.IO.File.Open(FileName, FileMode.Open)); + dmp.CRC = br.ReadInt32(); + Int32 ss = br.ReadInt32(); + dmp.StateData = br.ReadBytes(ss); + dmp.Registers = br.ReadBytes(8192); + + while (br.PeekChar() != -1) + { + GSData data = new GSData(); + data.id = (GSType)br.ReadByte(); + + switch (data.id) + { + case GSType.Transfer: + byte index = br.ReadByte(); + if (index == 0) + { + Int32 size = br.ReadInt32(); + + byte[] dat = new byte[16384]; + List Data = new List(); + Data.Add(index); + Data.AddRange(BitConverter.GetBytes(size)); + Data.AddRange(dat); + + int startaddr = 16389 - size; + + for (int i = 0; i < size; i++) + Data[startaddr + i] = br.ReadByte(); + data.data = Data.ToArray(); + } + else + { + Int32 size = br.ReadInt32(); + + List Data = new List(); + Data.Add(index); + Data.AddRange(br.ReadBytes(size)); + data.data = Data.ToArray(); + } + break; + case GSType.VSync: + data.data = br.ReadBytes(1); + break; + case GSType.ReadFIFO2: + Int32 sF = br.ReadInt32(); + data.data = br.ReadBytes(sF); + break; + case GSType.Registers: + data.data = br.ReadBytes(8192); + break; + } + dmp.Data.Add(data); + } + br.Close(); + + return dmp; + } + } +} diff --git a/tools/GSDumpGUI/Library/NativeMethods.cs b/tools/GSDumpGUI/Library/NativeMethods.cs index b24b39df46..192d7dc52d 100644 --- a/tools/GSDumpGUI/Library/NativeMethods.cs +++ b/tools/GSDumpGUI/Library/NativeMethods.cs @@ -35,5 +35,9 @@ namespace GSDumpGUI [SuppressUnmanagedCodeSecurityAttribute] [DllImport("user32", CharSet = CharSet.Ansi)] public extern static int SetClassLong(IntPtr HWND, int index, long newlong); + + [SuppressUnmanagedCodeSecurityAttribute] + [DllImport("user32", CharSet = CharSet.Ansi)] + public extern static bool IsWindowVisible(IntPtr HWND); } }