Skip to content

Commit

Permalink
Misc refactoring and bug fixes. (#108)
Browse files Browse the repository at this point in the history
* Make sure SystemRunner always uses the same system (C64 etc) in the Renderer, InputHandler, and AudioHandler as is used for emulation.

* Remove generic versions of IRenderer, IInputHandler, and IAudioHandler to reduce unnecessary complexity.
Make SystemRunner more obvious to use.
Remove SystemRunnerBuilder.

* Refactor use of Init and Cleanup methods in Renderers, InputHandlers, AudioHandlers (and their associated context objects) to be more consistent.

* Update doc

* Fix WASM C64 joystick keyboard

* Fix WASM host UI render bug

* Bump SkiaSharp from SkiaSharp to 3.0.0-preview.3.1. to 3.0.0-preview.4.1. This fixes WASM host rendering to work in debug mode again.
  • Loading branch information
highbyte authored Jul 19, 2024
1 parent 45a222d commit efe1bf2
Show file tree
Hide file tree
Showing 52 changed files with 1,110 additions and 1,196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ public void Setup()
LoadProgram(_c64WithInstrumentation.Mem, _startAddress);
LoadProgram(_c64WithoutInstrumentation.Mem, _startAddress);

var systemRunnerBuilderWithInstrumentation = new SystemRunnerBuilder<C64, NullRenderContext, NullInputHandlerContext, NullAudioHandlerContext>(_c64WithInstrumentation);
_systemRunnerWithInstrumentation = systemRunnerBuilderWithInstrumentation.Build();

var systemRunnerBuilderWithoutInstrumentation = new SystemRunnerBuilder<C64, NullRenderContext, NullInputHandlerContext, NullAudioHandlerContext>(_c64WithoutInstrumentation);
_systemRunnerWithoutInstrumentation = systemRunnerBuilderWithoutInstrumentation.Build();

_systemRunnerWithInstrumentation = new SystemRunner(_c64WithInstrumentation);
_systemRunnerWithoutInstrumentation = new SystemRunner(_c64WithoutInstrumentation);
}

private void LoadProgram(Memory mem, ushort startAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ public void Setup()
LoadProgram(_c64WithInstrumentation.Mem, _startAddress);
LoadProgram(_c64WithoutInstrumentation.Mem, _startAddress);

var systemRunnerBuilderWithInstrumentation = new SystemRunnerBuilder<C64, NullRenderContext, NullInputHandlerContext, NullAudioHandlerContext>(_c64WithInstrumentation);
_systemRunnerWithInstrumentation = systemRunnerBuilderWithInstrumentation.Build();

var systemRunnerBuilderWithoutInstrumentation = new SystemRunnerBuilder<C64, NullRenderContext, NullInputHandlerContext, NullAudioHandlerContext>(_c64WithoutInstrumentation);
_systemRunnerWithoutInstrumentation = systemRunnerBuilderWithoutInstrumentation.Build();

_systemRunnerWithInstrumentation = new SystemRunner(_c64WithInstrumentation);
_systemRunnerWithoutInstrumentation = new SystemRunner(_c64WithoutInstrumentation);
}

private void LoadProgram(Memory mem, ushort startAddress)
Expand Down
5 changes: 3 additions & 2 deletions doc/SYSTEMS_C64.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ Current capabilities
- Run Commodore Basic 2.0 from ROM (user supplied Kernal, Basic, and Chargen ROM files) in text mode.
- Limited VIC2 video chip support
- Standard, extended and multi-color character modes
- Standard and multi-color bitmap mode _(native app OpenGL renderer only)_
- Standard and multi-color bitmap mode _(newer SkiaRenderer 2/2b in native & WASM, and OpenGL renderer in native only)
- Sprites (hi-res & multi-color)
- IRQ (raster, sprite collision)
- Background and border color possible to change per raster line
- Background and border color possible to per raster line
- Fine scrolling per raster line (new newer SkiaRenderer 2b in native & WASM only)
- Limited CIA chip support
- Keyboard
- Joystick
Expand Down
47 changes: 27 additions & 20 deletions doc/SYSYEM_DIAGRAM.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ App_SilkNetNative --> SystemRunner
App_SilkNetNative --> System_C64
App_SilkNetNative --> Impl_Skia_C64
App_SilkNetNative --> Impl_SilkNet_C64
App_SilkNetNative --> Impl_NAudio_C64
App_SilkNetNative --> System_X
App_SilkNetNative --> Impl_Skia_X
App_SilkNetNative --> Impl_SilkNet_X
Expand All @@ -43,14 +44,20 @@ Impl_Skia_X --> IRenderer
Impl_SilkNet_C64
Impl_SilkNet_C64 --> System_C64
Impl_SilkNet_C64 --> IInputHandler
Impl_SilkNet_C64 --> IRenderer
Impl_SilkNet_X
Impl_SilkNet_X --> System_X
Impl_SilkNet_X --> IInputHandler
Impl_NAudio_C64
Impl_NAudio_C64 --> System_C64
Impl_NAudio_C64 --> IAudioHandler
Impl_AspNet_C64
Impl_AspNet_C64 --> System_C64
Impl_AspNet_C64 --> IInputHandler
Impl_AspNet_C64 --> IAudioHandler
Impl_AspNet_X
Impl_AspNet_X --> System_X
Expand Down Expand Up @@ -81,11 +88,12 @@ SystemRunner
SystemRunner --> ISystem
SystemRunner --> IInputHandler
SystemRunner --> IRenderer
SystemRunner : bool Run
SystemRunner : bool RunOneFrame
SystemRunner : bool ProcessInput
SystemRunner : bool RunEmulatorOneFrame
SystemRunner : bool Draw
SystemRunner --> IAudioHandler
SystemRunner : void Init
SystemRunner : void ProcessInputBeforeFrame
SystemRunner : ExecEvaluatorTriggerResult RunEmulatorOneFrame
SystemRunner : void Draw
SystemRunner : void Cleanup
ISystem
ISystem --> CPU
Expand All @@ -96,24 +104,23 @@ CPU
CPU : void Execute(Mem mem)
IRenderer
IRenderer : void Init(ISystem system, IRenderContext renderContext)
IRenderer : void Draw(ISystem system)
IRenderer : void Init()
IRenderer : void DrawFrame()
IRenderer : void Cleanup()
IInputHandler
IInputHandler : void Init(ISystem system, IInputHandlerContext inputHandlerContext)
IInputHandler : void ProcessInput(ISystem system)
```

```mermaid
classDiagram
SystemRunnerBuilder
SystemRunnerBuilder --> SystemRunner
SystemRunnerBuilder : ctor(~TSystem~ system)
SystemRunnerBuilder : SystemRunnerBuilder WithRenderer(IRenderer~TSystem, TRenderContext~ renderer)
SystemRunnerBuilder : SystemRunnerBuilder WithInputHandler(IRenderer~TSystem, TInputHandlerContext~ inputHandler)
SystemRunnerBuilder : SystemRunner Build()
IInputHandler : void Init()
IInputHandler : void BeforeFrame()
IInputHandler : void Cleanup()
IAudioHandler
IAudioHandler : void Init()
IAudioHandler : void AfterFrame()
IAudioHandler : void StartPlaying()
IAudioHandler : void StopPlaying()
IAudioHandler : void PausePlaying()
IAudioHandler : void Cleanup()
SystemRunner
```


5 changes: 2 additions & 3 deletions src/apps/Highbyte.DotNet6502.App.ConsoleMonitor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
// });
var computer = computerBuilder.Build();

var systemRunnerBuilder = new SystemRunnerBuilder<GenericComputer, NullRenderContext, NullInputHandlerContext, NullAudioHandlerContext>(computer);

var systemRunner = systemRunnerBuilder.Build();
var systemRunner = new SystemRunner(computer);
systemRunner.Init();

var monitorConfig = new MonitorConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public SilkNetRenderContextContainer(SkiaRenderContext skiaRenderContext, SilkNe
_silkNetOpenGlRenderContext = silkNetOpenGlRenderContext;
}

internal void Cleanup()
public void Init()
{
SkiaRenderContext?.Cleanup();
SilkNetOpenGlRenderContext?.Cleanup();
}

public void Cleanup()
{
_skiaRenderContext?.Cleanup();
_silkNetOpenGlRenderContext?.Cleanup();
}
}
16 changes: 8 additions & 8 deletions src/apps/Highbyte.DotNet6502.App.SilkNetNative/SilkNetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Highbyte.DotNet6502.App.SilkNetNative.SystemSetup;
using Highbyte.DotNet6502.Impl.SilkNet.Commodore64.Video;
using AutoMapper.Internal.Mappers;
using NAudio.Wave.SampleProviders;

namespace Highbyte.DotNet6502.App.SilkNetNative;

Expand Down Expand Up @@ -182,14 +183,13 @@ protected void OnClosing()
// _statsPanel.Cleanup();
DestroyImGuiController();

// Cleanup SkiaSharp resources
_silkNetRenderContextContainer.Cleanup();

// Cleanup SilkNet input resources
_silkNetInputHandlerContext.Cleanup();
// Cleanup systemrunner (which also cleanup renderer, inputhandler, and audiohandler)
_systemRunner?.Cleanup();

// Cleanup NAudio audio resources
_naudioAudioHandlerContext.Cleanup();
// Cleanup contexts
_silkNetRenderContextContainer?.Cleanup();
_silkNetInputHandlerContext?.Cleanup();
_naudioAudioHandlerContext?.Cleanup();
}

/// <summary>
Expand Down Expand Up @@ -359,7 +359,7 @@ private void RunEmulator()
if (!_atLeastOneImGuiWindowHasFocus)
{
_inputTime.Start();
_systemRunner.ProcessInput();
_systemRunner.ProcessInputBeforeFrame();
_inputTime.Stop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,43 +105,27 @@ NAudioAudioHandlerContext audioHandlerContext
var c64 = (C64)system;

IRenderer renderer;
IRenderContext renderContext;
switch (c64HostConfig.Renderer)
{
case C64HostRenderer.SkiaSharp:
renderer = new C64SkiaRenderer();
renderContext = renderContextContainer.SkiaRenderContext;
renderer = new C64SkiaRenderer(c64, renderContextContainer.SkiaRenderContext);
break;
case C64HostRenderer.SkiaSharp2:
renderer = new C64SkiaRenderer2();
renderContext = renderContextContainer.SkiaRenderContext;
renderer = new C64SkiaRenderer2(c64, renderContextContainer.SkiaRenderContext);
break;
case C64HostRenderer.SkiaSharp2b:
renderer = new C64SkiaRenderer2b();
renderContext = renderContextContainer.SkiaRenderContext;
renderer = new C64SkiaRenderer2b(c64, renderContextContainer.SkiaRenderContext);
break;
case C64HostRenderer.SilkNetOpenGl:
renderer = new C64SilkNetOpenGlRenderer(c64HostConfig.SilkNetOpenGlRendererConfig);
renderContext = renderContextContainer.SilkNetOpenGlRenderContext;
renderer = new C64SilkNetOpenGlRenderer(c64, renderContextContainer.SilkNetOpenGlRenderContext, c64HostConfig.SilkNetOpenGlRendererConfig);
break;
default:
throw new NotImplementedException($"Renderer {c64HostConfig.Renderer} not implemented.");
}

var inputHandler = new C64SilkNetInputHandler(_loggerFactory, _c64HostConfig.InputConfig);
var audioHandler = new C64NAudioAudioHandler(_loggerFactory);
var inputHandler = new C64SilkNetInputHandler(c64, inputHandlerContext, _loggerFactory, _c64HostConfig.InputConfig);
var audioHandler = new C64NAudioAudioHandler(c64, audioHandlerContext, _loggerFactory);


renderer.Init(c64, renderContext);
inputHandler.Init(c64, inputHandlerContext);
audioHandler.Init(c64, audioHandlerContext);

var systemRunnerBuilder = new SystemRunnerBuilder<C64, SkiaRenderContext, SilkNetInputHandlerContext, NAudioAudioHandlerContext>(c64);
var systemRunner = systemRunnerBuilder
.WithRenderer(renderer)
.WithInputHandler(inputHandler)
.WithAudioHandler(audioHandler)
.Build();
return systemRunner;
return new SystemRunner(c64, renderer, inputHandler, audioHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Highbyte.DotNet6502.Impl.Skia;
using Highbyte.DotNet6502.Impl.Skia.Generic.Video;
using Highbyte.DotNet6502.Systems;
using Highbyte.DotNet6502.Systems.Commodore64;
using Highbyte.DotNet6502.Systems.Generic;
using Highbyte.DotNet6502.Systems.Generic.Config;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -83,23 +84,13 @@ public SystemRunner BuildSystemRunner(
SilkNetInputHandlerContext inputHandlerContext,
NAudioAudioHandlerContext audioHandlerContext)
{
var genericComputerConfig = (GenericComputerConfig)systemConfig;

var renderer = new GenericComputerSkiaRenderer(genericComputerConfig.Memory.Screen);
var inputHandler = new GenericComputerSilkNetInputHandler(genericComputerConfig.Memory.Input);
var audioHandler = new NullAudioHandler();

var genericComputer = (GenericComputer)system;
var genericComputerConfig = (GenericComputerConfig)systemConfig;

renderer.Init(genericComputer, renderContext.SkiaRenderContext);
inputHandler.Init(genericComputer, inputHandlerContext);
var renderer = new GenericComputerSkiaRenderer(genericComputer, renderContext.SkiaRenderContext, genericComputerConfig.Memory.Screen);
var inputHandler = new GenericComputerSilkNetInputHandler(genericComputer, inputHandlerContext, genericComputerConfig.Memory.Input);
var audioHandler = new NullAudioHandler(genericComputer);

var systemRunnerBuilder = new SystemRunnerBuilder<GenericComputer, SkiaRenderContext, SilkNetInputHandlerContext, NullAudioHandlerContext>(genericComputer);
var systemRunner = systemRunnerBuilder
.WithRenderer(renderer)
.WithInputHandler(inputHandler)
.WithAudioHandler(audioHandler)
.Build();
return systemRunner;
return new SystemRunner(genericComputer, renderer, inputHandler, audioHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.0" />
<PackageReference Include="SkiaSharp.Views.Blazor" Version="3.0.0-preview.3.1" />
<PackageReference Include="SkiaSharp.Views.Blazor" Version="3.0.0-preview.4.1" />
<PackageReference Include="PublishSPAforGitHubPages.Build" Version="2.1.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="Toolbelt.Blazor.Gamepad" Version="8.0.0.1" />
Expand Down
Loading

0 comments on commit efe1bf2

Please sign in to comment.