From 1a944f019c0abda704a7552abafac60044be8359 Mon Sep 17 00:00:00 2001 From: saku-kaarakainen Date: Tue, 30 Aug 2016 23:08:02 +0300 Subject: [PATCH] #32: theaded Skeleton loop for game cycle --- WinBoyEmulator/GameBoy/CPU/LR35902.cs | 15 ++++----------- WinBoyEmulator/GameBoy/Emulator.cs | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/WinBoyEmulator/GameBoy/CPU/LR35902.cs b/WinBoyEmulator/GameBoy/CPU/LR35902.cs index 75d6387..b7ead4a 100644 --- a/WinBoyEmulator/GameBoy/CPU/LR35902.cs +++ b/WinBoyEmulator/GameBoy/CPU/LR35902.cs @@ -30,6 +30,8 @@ public class LR35902 : Flags, IRegisters private static readonly object _syncRoot = new object(); private static volatile LR35902 _instance; + private bool _isCpuRunning = false; + // store registers values to these bytes. private byte _a, _b, _c, _d, _e, _f, _h, _l; private ushort _sp, _pc; @@ -233,18 +235,9 @@ public static LR35902 Instance } } - public void Start() + public void EmulateCycle() { - var cpuIsRunning = true; - /*while(cpuIsRunning) - { - // Issue #32 - // - // Fetch instruction - // Dispatch - // Mask PC to 16 bits - // Add time to CPU clock - }*/ + } public void Reset() diff --git a/WinBoyEmulator/GameBoy/Emulator.cs b/WinBoyEmulator/GameBoy/Emulator.cs index 9d729f9..6a012f3 100644 --- a/WinBoyEmulator/GameBoy/Emulator.cs +++ b/WinBoyEmulator/GameBoy/Emulator.cs @@ -17,6 +17,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using WinBoyEmulator.GameBoy.CPU; @@ -33,6 +34,7 @@ public class Emulator : IEmulator private static volatile Emulator _instance; private byte[] _game; + private bool _isGameBoyOn = false; public static Emulator Instance { @@ -64,6 +66,19 @@ private void _readGameFile(string filename) { var length = (int)reader.BaseStream.Length; _game = reader.ReadBytes(length); + // Issue #29 + } + } + + private void _gameCycle() + { + while(_isGameBoyOn) + { + // Emulate one cycle + LR35902.Instance.EmulateCycle(); + + // If the draw flag is set, update the screen + // Store key press state (Press and Release) } } @@ -79,7 +94,14 @@ public void StartEmulation(string gamePath) // Load game. _readGameFile(gamePath); MMU.Instance.Load(_game); - LR35902.Instance.Start(); + + var thread = new Thread(_gameCycle) + { + IsBackground = true, + Name = "WinBoyEmulator", + }; + thread.Start(); + } } }