From 69409ff729157e0bd3df30f22559a320b0e468c3 Mon Sep 17 00:00:00 2001 From: alyosha-tas Date: Sun, 29 Mar 2020 22:30:19 -0400 Subject: [PATCH] GBHawk: bug fixes --- .../GBHawk_new/GBHawkNew.IEmulator.cs | 52 +++++++++++++++---- .../Consoles/Nintendo/GBHawk_new/GBHawkNew.cs | 3 +- .../Consoles/Nintendo/GBHawk_new/LibGBHawk.cs | 4 +- libHawk/GBHawk/GBHawk/Core.h | 37 ++++++++++--- libHawk/GBHawk/GBHawk/GBAudio.h | 12 ++--- libHawk/GBHawk/GBHawk/LR35902.h | 5 +- libHawk/GBHawk/GBHawk/PPU.h | 6 +-- 7 files changed, 86 insertions(+), 33 deletions(-) diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.IEmulator.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.IEmulator.cs index 45a57fab66..84081cdc31 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.IEmulator.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.IEmulator.cs @@ -109,7 +109,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkNew public void Dispose() { + DisposeSound(); + if (GB_Pntr != IntPtr.Zero) + { + LibGBHawk.GB_destroy(GB_Pntr); + GB_Pntr = IntPtr.Zero; + } } #region Video provider @@ -139,12 +145,15 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkNew #region Audio - public BlipBuffer blip = new BlipBuffer(4500); + public BlipBuffer blip_L = new BlipBuffer(9000); + public BlipBuffer blip_R = new BlipBuffer(9000); - public int[] Aud = new int[9000]; - public uint num_samp; + public int[] Aud_L = new int[9000]; + public int[] Aud_R = new int[9000]; + public uint num_samp_L; + public uint num_samp_R; - const int blipbuffsize = 4500; + const int blipbuffsize = 9000; public bool CanProvideAsync => false; @@ -165,24 +174,45 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkNew public void GetSamplesSync(out short[] samples, out int nsamp) { - uint f_clock = LibGBHawk.GB_get_audio(GB_Pntr, Aud, ref num_samp); + uint f_clock = LibGBHawk.GB_get_audio(GB_Pntr, Aud_L, ref num_samp_L, Aud_R, ref num_samp_R); - for (int i = 0; i < num_samp; i++) + for (int i = 0; i < num_samp_L; i++) { - blip.AddDelta((uint)Aud[i * 2], Aud[i * 2 + 1]); + blip_L.AddDelta((uint)Aud_L[i * 2], Aud_L[i * 2 + 1]); } - blip.EndFrame(f_clock); + for (int i = 0; i < num_samp_R; i++) + { + blip_R.AddDelta((uint)Aud_R[i * 2], Aud_R[i * 2 + 1]); + } - nsamp = blip.SamplesAvailable(); + blip_L.EndFrame(f_clock); + blip_R.EndFrame(f_clock); + + nsamp = blip_L.SamplesAvailable(); samples = new short[nsamp * 2]; - blip.ReadSamples(samples, nsamp, true); + if (nsamp != 0) + { + blip_L.ReadSamplesLeft(samples, nsamp); + blip_R.ReadSamplesRight(samples, nsamp); + } } public void DiscardSamples() { - blip.Clear(); + blip_L.Clear(); + blip_R.Clear(); + } + + public void DisposeSound() + { + blip_L.Clear(); + blip_R.Clear(); + blip_L.Dispose(); + blip_R.Dispose(); + blip_L = null; + blip_R = null; } #endregion diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.cs index 677709a606..13a0e49ed4 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/GBHawkNew.cs @@ -77,7 +77,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkNew LibGBHawk.GB_load_bios(GB_Pntr, _bios, is_GBC, _syncSettings.GBACGB); LibGBHawk.GB_load(GB_Pntr, rom, (uint)rom.Length, MD5_temp, (uint)_syncSettings.RTCInitialTime, (uint)_syncSettings.RTCOffset); - blip.SetRates(3579545, 44100); + blip_L.SetRates(4194304, 44100); + blip_R.SetRates(4194304, 44100); (ServiceProvider as BasicServiceProvider).Register(this); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/LibGBHawk.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/LibGBHawk.cs index 7606e4f3b2..b73db463b8 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/LibGBHawk.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk_new/LibGBHawk.cs @@ -83,7 +83,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkNew /// where to send left audio to /// number of left samples [DllImport("GBHawk.dll", CallingConvention = CallingConvention.Cdecl)] - public static extern uint GB_get_audio(IntPtr core, int[] aud_buf, ref uint n_samp); + public static extern uint GB_get_audio(IntPtr core, int[] aud_buf_L, ref uint n_samp_L, int[] aud_buf_R, ref uint n_samp_R); #endregion @@ -102,7 +102,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkNew /// /// opaque state pointer /// load buffer - [DllImport("MSXHAWK.dll", CallingConvention = CallingConvention.Cdecl)] + [DllImport("GBHAWK.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void GB_load_state(IntPtr core, byte[] loader); #endregion diff --git a/libHawk/GBHawk/GBHawk/Core.h b/libHawk/GBHawk/GBHawk/Core.h index d13f861fde..d1b5122804 100644 --- a/libHawk/GBHawk/GBHawk/Core.h +++ b/libHawk/GBHawk/GBHawk/Core.h @@ -62,6 +62,7 @@ namespace GBHawk } MemMap.ppu_pntr = &ppu[0]; + ppu->mem_ctrl = &MemMap; // initialize the proper mapper Setup_Mapper(MD5, RTC_initial, RTC_offset); @@ -133,7 +134,21 @@ namespace GBHawk bool FrameAdvance(uint8_t new_controller_1, uint32_t new_accx, uint32_t new_accy, bool render, bool rendersound) { - for (int i = 0; i < 70224; i++) + int temp_check = 0; + /* + if (cpu.TotalExecutedCycles < 23805935) { + temp_check = 70224; + } + else if (cpu.TotalExecutedCycles < 23853139) { + temp_check = 200; + } + else { + temp_check = 5; + } + */ + temp_check = 70224; + + for (int i = 0; i < temp_check; i++) { // These things do not change speed in GBC double spped mode psg.tick(); @@ -337,7 +352,13 @@ namespace GBHawk std::memcpy(dst, src, sizeof int32_t * psg.num_samples_R * 2); n_samp_R[0] = psg.num_samples_R; - return psg.master_audio_clock; + uint32_t temp_int = psg.master_audio_clock; + psg.master_audio_clock = 0; + + psg.num_samples_L = 0; + psg.num_samples_R = 0; + + return temp_int; } void Setup_Mapper(string MD5, uint32_t RTC_initial, uint32_t RTC_offset) @@ -561,8 +582,8 @@ namespace GBHawk { saver = ppu->SaveState(saver); saver = cpu.SaveState(saver); - saver = psg.SaveState(saver); - saver = MemMap.SaveState(saver); + saver = MemMap.SaveState(saver); + saver = psg.SaveState(saver); saver = timer.SaveState(saver); saver = serialport.SaveState(saver); saver = mapper->SaveState(saver); @@ -572,11 +593,11 @@ namespace GBHawk { loader = ppu->LoadState(loader); loader = cpu.LoadState(loader); + loader = MemMap.LoadState(loader); loader = psg.LoadState(loader); - loader = MemMap.LoadState(loader); - loader = timer.SaveState(loader); - loader = serialport.SaveState(loader); - loader = mapper->SaveState(loader); + loader = timer.LoadState(loader); + loader = serialport.LoadState(loader); + loader = mapper->LoadState(loader); } #pragma endregion diff --git a/libHawk/GBHawk/GBHawk/GBAudio.h b/libHawk/GBHawk/GBHawk/GBAudio.h index e7e2765fd7..25134bd4d4 100644 --- a/libHawk/GBHawk/GBHawk/GBAudio.h +++ b/libHawk/GBHawk/GBHawk/GBAudio.h @@ -728,20 +728,20 @@ namespace GBHawk if (L_final != latched_sample_L) { - samples_L[num_samples_L * 2] = master_audio_clock; - samples_L[num_samples_L * 2 + 1] = L_final - latched_sample_L; + //samples_L[num_samples_L * 2] = master_audio_clock; + //samples_L[num_samples_L * 2 + 1] = L_final - latched_sample_L; - num_samples_L++; + //num_samples_L++; latched_sample_L = L_final; } if (R_final != latched_sample_R) { - samples_R[num_samples_R * 2] = master_audio_clock; - samples_R[num_samples_R * 2 + 1] = R_final - latched_sample_R; + //samples_R[num_samples_R * 2] = master_audio_clock; + //samples_R[num_samples_R * 2 + 1] = R_final - latched_sample_R; - num_samples_R++; + //num_samples_R++; latched_sample_R = R_final; } diff --git a/libHawk/GBHawk/GBHawk/LR35902.h b/libHawk/GBHawk/GBHawk/LR35902.h index 747ca96eaa..ad2406e99a 100644 --- a/libHawk/GBHawk/GBHawk/LR35902.h +++ b/libHawk/GBHawk/GBHawk/LR35902.h @@ -2974,8 +2974,8 @@ namespace GBHawk *saver = (uint8_t)(TotalExecutedCycles & 0xFF); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 8) & 0xFF); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 16) & 0xFF); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 24) & 0xFF); saver++; - *saver = (uint8_t)((TotalExecutedCycles >> 16) & 0x32); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 40) & 0xFF); saver++; - *saver = (uint8_t)((TotalExecutedCycles >> 16) & 0x48); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 56) & 0xFF); saver++; + *saver = (uint8_t)((TotalExecutedCycles >> 32) & 0xFF); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 40) & 0xFF); saver++; + *saver = (uint8_t)((TotalExecutedCycles >> 48) & 0xFF); saver++; *saver = (uint8_t)((TotalExecutedCycles >> 56) & 0xFF); saver++; return saver; } @@ -2989,6 +2989,7 @@ namespace GBHawk jammed = *loader == 1; loader++; was_FlagI = *loader == 1; loader++; FlagI = *loader == 1; loader++; + halted = *loader == 1; loader++; opcode = *loader; loader++; LY = *loader; loader++; diff --git a/libHawk/GBHawk/GBHawk/PPU.h b/libHawk/GBHawk/GBHawk/PPU.h index ce6b117a10..d281bc9558 100644 --- a/libHawk/GBHawk/GBHawk/PPU.h +++ b/libHawk/GBHawk/GBHawk/PPU.h @@ -1620,7 +1620,7 @@ namespace GBHawk // So transfers nominally from higher memory areas are actually still from there (i.e. FF -> DF) uint8_t DMA_actual = DMA_addr; if (DMA_addr > 0xDF) { DMA_actual &= 0xDF; } - DMA_byte = ReadMemory((uint32_t)((DMA_actual << 8) + DMA_inc)); + DMA_byte = ReadMemory(((uint32_t)(DMA_actual << 8) + DMA_inc)); DMA_start = true; } else if ((DMA_clock % 4) == 3) @@ -3095,7 +3095,7 @@ namespace GBHawk void DMA_tick() { // Note that DMA is halted when the CPU is halted - if (DMA_start && !cpu_halted) + if (DMA_start && !cpu_halted[0]) { if (DMA_clock >= 4) { @@ -4688,7 +4688,7 @@ namespace GBHawk void DMA_tick() { // Note that DMA is halted when the CPU is halted - if (DMA_start && !cpu_halted) + if (DMA_start && !cpu_halted[0]) { if (DMA_clock >= 4) {