From 26bd5e3aa90e49ccccc533f70437c32c25797781 Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Thu, 1 Apr 2010 23:00:24 +0000 Subject: [PATCH] Cosmetic changes. Modified to clear audio buffer on reset. Refactored keyboard services and unified key bindings where practical. --HG-- extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4044385 --- Library/Silverlight/ApplicationBase.cs | 9 +- Library/Wpf/ApplicationBase.cs | 11 +- Library/Xna/GameBase.cs | 6 + Virtu/DiskDsk.cs | 11 +- Virtu/Keyboard.cs | 25 --- Virtu/Machine.cs | 2 +- Virtu/Services/AudioService.cs | 9 +- Virtu/Services/GamePortService.cs | 2 +- Virtu/Services/KeyboardService.cs | 43 +++-- Virtu/Services/VideoService.cs | 2 +- Virtu/Silverlight/MainPage.xaml | 9 +- .../Services/SilverlightAudioService.cs | 11 +- .../Services/SilverlightKeyboardService.cs | 93 +++++------ .../Services/SilverlightVideoService.cs | 2 +- Virtu/Speaker.cs | 7 + Virtu/Wpf/MainWindow.xaml | 9 +- Virtu/Wpf/Services/WpfAudioService.cs | 11 +- Virtu/Wpf/Services/WpfKeyboardService.cs | 86 ++++------ Virtu/Wpf/Services/WpfVideoService.cs | 2 +- Virtu/Xna/MainGame.cs | 5 +- Virtu/Xna/Services/XnaAudioService.cs | 2 +- Virtu/Xna/Services/XnaGamePortService.cs | 2 +- Virtu/Xna/Services/XnaKeyboardService.cs | 156 +++++++----------- Virtu/Xna/Services/XnaVideoService.cs | 2 +- 24 files changed, 233 insertions(+), 284 deletions(-) diff --git a/Library/Silverlight/ApplicationBase.cs b/Library/Silverlight/ApplicationBase.cs index dafeb81..b3c287d 100644 --- a/Library/Silverlight/ApplicationBase.cs +++ b/Library/Silverlight/ApplicationBase.cs @@ -6,6 +6,11 @@ namespace Jellyfish.Library { public class ApplicationBase : Application { + public ApplicationBase() : + this(null) + { + } + public ApplicationBase(string name) { Name = name; @@ -17,6 +22,7 @@ public ApplicationBase(string name) private void OnApplicationUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { MessageBox.Show(GetExceptionMessage(e.ExceptionObject), GetExceptionCaption("Application Exception", false), MessageBoxButton.OK); + e.Handled = true; } //private void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) @@ -48,8 +54,7 @@ private static string GetExceptionMessage(Exception exception) { message.Append(exception.Message.ToString()); message.Append(Environment.NewLine); - message.Append(Environment.NewLine); - message.Append(exception.ToString()); // includes stack trace + message.Append(exception.StackTrace.ToString()); } return message.ToString(); diff --git a/Library/Wpf/ApplicationBase.cs b/Library/Wpf/ApplicationBase.cs index 61de726..b6aad18 100644 --- a/Library/Wpf/ApplicationBase.cs +++ b/Library/Wpf/ApplicationBase.cs @@ -9,6 +9,12 @@ namespace Jellyfish.Library { public class ApplicationBase : Application { + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + public ApplicationBase() : + this(null) + { + } + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] public ApplicationBase(string name) { @@ -21,8 +27,8 @@ public ApplicationBase(string name) private void OnApplicationDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { MessageBox.Show(GetExceptionMessage(e.Exception), GetExceptionCaption("Application Dispatcher Exception", true)); - Shutdown(); e.Handled = true; + Shutdown(); } private void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) @@ -55,8 +61,7 @@ private static string GetExceptionMessage(Exception exception) { message.Append(exception.Message.ToString()); message.Append(Environment.NewLine); - message.Append(Environment.NewLine); - message.Append(exception.ToString()); // includes stack trace + message.Append(exception.StackTrace.ToString()); } return message.ToString(); diff --git a/Library/Xna/GameBase.cs b/Library/Xna/GameBase.cs index 7a57708..c63981b 100644 --- a/Library/Xna/GameBase.cs +++ b/Library/Xna/GameBase.cs @@ -7,6 +7,11 @@ namespace Jellyfish.Library { public class GameBase : Game { + public GameBase() : + this(null) + { + } + public GameBase(string name) { Name = name; @@ -29,6 +34,7 @@ protected override void Update(GameTime gameTime) { Exit(); } + base.Update(gameTime); } diff --git a/Virtu/DiskDsk.cs b/Virtu/DiskDsk.cs index 7ced3b0..b7f1492 100644 --- a/Virtu/DiskDsk.cs +++ b/Virtu/DiskDsk.cs @@ -63,7 +63,7 @@ public override void WriteTrack(int number, int fraction, byte[] buffer) if (!Read3Nibbles(0xD5, 0xAA, 0x96, 0x304)) break; // no address prologue - int readVolume = ReadNibble44(); + /*int readVolume = */ReadNibble44(); int readTrack = ReadNibble44(); if (readTrack != track) @@ -94,7 +94,7 @@ public override void WriteTrack(int number, int fraction, byte[] buffer) } if (sectorsDone != 0xFFFF) - throw new Exception("disk error"); // TODO: we should alert the user and "dump" a NIB + throw new InvalidOperationException("disk error"); // TODO: we should alert the user and "dump" a NIB } private byte ReadNibble() @@ -110,16 +110,15 @@ private byte ReadNibble() private bool Read3Nibbles(byte data1, byte data2, byte data3, int maxReads) { bool result = false; - byte nibble; while (--maxReads > 0) { - if ((nibble = ReadNibble()) != data1) + if (ReadNibble() != data1) continue; - if ((nibble = ReadNibble()) != data2) + if (ReadNibble() != data2) continue; - if ((nibble = ReadNibble()) != data3) + if (ReadNibble() != data3) continue; result = true; diff --git a/Virtu/Keyboard.cs b/Virtu/Keyboard.cs index 1f4ed3c..ef9584c 100644 --- a/Virtu/Keyboard.cs +++ b/Virtu/Keyboard.cs @@ -18,7 +18,6 @@ public override void Initialize() _gamePortService = Machine.Services.GetService(); _keyboardService.AsciiKeyDown += (sender, e) => Latch = e.AsciiKey; - Machine.Video.VSync += OnVideoVSync; } [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] @@ -121,30 +120,6 @@ public void ResetStrobe() Strobe = false; } - private void OnVideoVSync(object sender, EventArgs e) - { - if (_keyboardService.IsResetKeyDown) - { - Machine.Reset(); - _keyboardService.WaitForResetKeyUp(); - } - else if (_keyboardService.IsCpuThrottleKeyDown) - { - Machine.Cpu.ToggleThrottle(); - _keyboardService.WaitForKeyUp(); - } - else if (_keyboardService.IsVideoFullScreenKeyDown) - { - Machine.Video.ToggleFullScreen(); - _keyboardService.WaitForKeyUp(); - } - else if (_keyboardService.IsVideoMonochromeKeyDown) - { - Machine.Video.ToggleMonochrome(); - _keyboardService.WaitForKeyUp(); - } - } - public bool IsAnyKeyDown { get { return _keyboardService.IsAnyKeyDown; } } public bool Strobe { get; private set; } diff --git a/Virtu/Machine.cs b/Virtu/Machine.cs index 97bc8c4..77b05a4 100644 --- a/Virtu/Machine.cs +++ b/Virtu/Machine.cs @@ -38,7 +38,7 @@ public void Dispose() public void Reset() { - Components.ForEach(component => component.Reset()); + Components.ForEach(component => component.Reset()); // while machine starting or paused } public void Start() diff --git a/Virtu/Services/AudioService.cs b/Virtu/Services/AudioService.cs index ee4bc33..819589b 100644 --- a/Virtu/Services/AudioService.cs +++ b/Virtu/Services/AudioService.cs @@ -24,6 +24,11 @@ public void Output(int data) // machine thread } } + public void Reset() + { + Buffer.BlockCopy(SampleZero, 0, _buffer, 0, SampleSize); + } + public override void Stop() // main thread { _readEvent.Set(); // signal events; avoids deadlock @@ -49,7 +54,9 @@ protected void Update(int bufferSize, Action updateBuffer) // audio public const int SampleChannels = 1; public const int SampleBits = 8; public const int SampleLatency = 40; // ms - public const int SampleSize = (int)(SampleRate * SampleLatency / 1000f) * SampleChannels * SampleBits / 8; + public const int SampleSize = (SampleRate * SampleLatency / 1000) * SampleChannels * (SampleBits / 8); + + private static readonly byte[] SampleZero = new byte[SampleSize]; private byte[] _buffer = new byte[SampleSize]; private int _index; diff --git a/Virtu/Services/GamePortService.cs b/Virtu/Services/GamePortService.cs index 08b34f4..395eebb 100644 --- a/Virtu/Services/GamePortService.cs +++ b/Virtu/Services/GamePortService.cs @@ -56,7 +56,7 @@ public GamePortService(Machine machine) : Paddle0 = Paddle1 = Paddle2 = Paddle3 = 255; // not connected } - public virtual void Update() { } + public virtual void Update() { } // main thread public int Paddle0 { get; protected set; } public int Paddle1 { get; protected set; } diff --git a/Virtu/Services/KeyboardService.cs b/Virtu/Services/KeyboardService.cs index 31e9df4..bebfbd0 100644 --- a/Virtu/Services/KeyboardService.cs +++ b/Virtu/Services/KeyboardService.cs @@ -30,6 +30,24 @@ protected KeyboardService(Machine machine) : public abstract bool IsKeyDown(int key); + public virtual void Update() // main thread + { + if (IsResetKeyDown) + { + if (!_resetKeyDown) + { + _resetKeyDown = true; // entering reset; pause until key released + Machine.Pause(); + Machine.Reset(); + } + } + else if (_resetKeyDown) + { + _resetKeyDown = false; // leaving reset + Machine.Unpause(); + } + } + protected void OnAsciiKeyDown(int asciiKey) { EventHandler handler = AsciiKeyDown; @@ -39,33 +57,14 @@ protected void OnAsciiKeyDown(int asciiKey) } } - public abstract void Update(); - - public void WaitForKeyUp() - { - while (IsAnyKeyDown) - { - Thread.Sleep(10); - } - } - - public void WaitForResetKeyUp() - { - while (IsResetKeyDown) - { - Thread.Sleep(10); - } - } - public event EventHandler AsciiKeyDown; public bool IsAnyKeyDown { get; protected set; } public bool IsOpenAppleKeyDown { get; protected set; } public bool IsCloseAppleKeyDown { get; protected set; } - public bool IsResetKeyDown { get; protected set; } - public bool IsCpuThrottleKeyDown { get; protected set; } - public bool IsVideoFullScreenKeyDown { get; protected set; } - public bool IsVideoMonochromeKeyDown { get; protected set; } + protected bool IsResetKeyDown { get; set; } + + private bool _resetKeyDown; } } diff --git a/Virtu/Services/VideoService.cs b/Virtu/Services/VideoService.cs index 3e47ac1..e56968c 100644 --- a/Virtu/Services/VideoService.cs +++ b/Virtu/Services/VideoService.cs @@ -8,7 +8,7 @@ protected VideoService(Machine machine) : } public abstract void SetPixel(int x, int y, uint color); - public abstract void Update(); + public abstract void Update(); // main thread public void ToggleFullScreen() { diff --git a/Virtu/Silverlight/MainPage.xaml b/Virtu/Silverlight/MainPage.xaml index 2aa6080..9b17489 100644 --- a/Virtu/Silverlight/MainPage.xaml +++ b/Virtu/Silverlight/MainPage.xaml @@ -6,13 +6,16 @@ xmlns:jv="clr-namespace:Jellyfish.Virtu;assembly=Jellyfish.Virtu"> -