-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEmulatorComponent.razor.cs
More file actions
188 lines (170 loc) · 6.18 KB
/
EmulatorComponent.razor.cs
File metadata and controls
188 lines (170 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.JSInterop;
using Microsoft.JSInterop.WebAssembly;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Timers;
using ZXBox.Core.Hardware.Input;
using ZXBox.Hardware.Input;
using ZXBox.Hardware.Input.Joystick;
using ZXBox.Hardware.Output;
using ZXBox.Snapshot;
namespace ZXBox.Blazor.Pages
{
public partial class EmulatorComponentModel : ComponentBase, IAsyncDisposable
{
private ZXSpectrum speccy;
public System.Timers.Timer gameLoop;
int flashcounter = 16;
bool flash = false;
JavaScriptKeyboard Keyboard = new();
Kempston kempston;
Beeper<byte> beeper;
public TapePlayer tapePlayer;
[Inject]
Toolbelt.Blazor.Gamepad.GamepadList GamePadList { get; set; }
[Inject]
protected HttpClient Http { get; set; }
[Inject]
protected IJSInProcessRuntime JSRuntime { get; set; }
public EmulatorComponentModel()
{
gameLoop = new System.Timers.Timer(20);
gameLoop.Elapsed += GameLoop_Elapsed;
}
public ZXSpectrum GetZXSpectrum(RomEnum rom)
{
return new ZXSpectrum(true, true, 20, 20, 20, rom);
}
public void StartZXSpectrum(RomEnum rom)
{
speccy = GetZXSpectrum(rom);
speccy.InputHardware.Add(Keyboard);
kempston = new Kempston();
speccy.InputHardware.Add(kempston);
//48000 samples per second, 50 frames per second (20ms per frame) Mono
beeper = new Beeper<byte>(0, 127, 48000 / 50, 1);
speccy.OutputHardware.Add(beeper);
tapePlayer = new(beeper);
speccy.InputHardware.Add(tapePlayer);
mono = JSRuntime as WebAssemblyJSRuntime;
speccy.Reset();
gameLoop.Start();
}
public string TapeName { get; set; }
public async Task HandleFileSelected(InputFileChangeEventArgs args)
{
if (args.File.Name.ToLower().EndsWith(".tap"))
{
//Load the tape
var file = args.File;
var ms = new MemoryStream();
await file.OpenReadStream().CopyToAsync(ms);
tapePlayer.LoadTape(ms.ToArray());
TapeName = Path.GetFileNameWithoutExtension(args.File.Name);
}
else
{
var file = args.File;
var ms = new MemoryStream();
await file.OpenReadStream().CopyToAsync(ms);
var handler = FileFormatFactory.GetSnapShotHandler(file.Name);
var bytes = ms.ToArray();
handler.LoadSnapshot(bytes, speccy);
}
}
[Inject]
HttpClient httpClient { get; set; }
public string Instructions = "";
public async Task LoadGame(string filename, string instructions)
{
var ms = new MemoryStream();
var handler = FileFormatFactory.GetSnapShotHandler(filename);
var stream = await httpClient.GetStreamAsync("Roms/" + filename + ".json");
await stream.CopyToAsync(ms);
var bytes = ms.ToArray();
handler.LoadSnapshot(bytes, speccy);
Instructions = instructions;
}
private async void GameLoop_Elapsed(object sender, ElapsedEventArgs e)
{
Stopwatch sw = new Stopwatch();
//Get gamepads
kempston.Gamepads = await GamePadList.GetGamepadsAsync();
//Run JavaScriptInterop to find the currently pressed buttons
Keyboard.KeyBuffer = await JSRuntime.InvokeAsync<List<string>>("getKeyStatus");
sw.Start();
speccy.DoInstructions(69888);
beeper.GenerateSound();
await BufferSound();
Paint();
sw.Stop();
if (tapePlayer != null && tapePlayer.IsPlaying)
{
TapeStopped = false;
PercentLoaded = ((Convert.ToDouble(tapePlayer.CurrentTstate) / Convert.ToDouble(tapePlayer.TotalTstates)) * 100);
await InvokeAsync(() => StateHasChanged());
}
if (!TapeStopped && !tapePlayer.IsPlaying)
{
TapeStopped = true;
await InvokeAsync(() => StateHasChanged());
}
}
bool TapeStopped = false;
GCHandle gchsound;
IntPtr pinnedsound;
WebAssemblyJSRuntime mono;
byte[] soundbytes;
protected async Task BufferSound()
{
soundbytes = beeper.GetSoundBuffer();
gchsound = GCHandle.Alloc(soundbytes, GCHandleType.Pinned);
pinnedsound = gchsound.AddrOfPinnedObject();
mono.InvokeUnmarshalled<IntPtr, string>("addAudioBuffer", pinnedsound);
gchsound.Free();
}
public double PercentLoaded = 0;
protected async override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeAsync<bool>("InitCanvas");
}
base.OnAfterRender(firstRender);
}
GCHandle gchscreen;
IntPtr pinnedscreen;
//uint[] screen = new uint[68672]; //Height * width (256+20+20)*(192+20+20)
public async void Paint()
{
if (flashcounter == 0)
{
flashcounter = 16;
flash = !flash;
}
else
{
flashcounter--;
}
var screen = speccy.GetScreenInUint(flash);
//Allocate memory
gchscreen = GCHandle.Alloc(screen, GCHandleType.Pinned);
pinnedscreen = gchscreen.AddrOfPinnedObject();
mono.InvokeUnmarshalled<IntPtr, string>("PaintCanvas", pinnedscreen);
gchscreen.Free();
}
public ValueTask DisposeAsync()
{
gameLoop.Stop();
gameLoop.Dispose();
return ValueTask.CompletedTask;
}
}
}