-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
268 lines (214 loc) · 9.53 KB
/
Program.cs
File metadata and controls
268 lines (214 loc) · 9.53 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// ReSharper disable AccessToDisposedClosure; Disposed after use.
// ReSharper disable InconsistentNaming; Follows library naming style
using CHIP_8.Drivers;
using CHIP_8.Emulation;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using CHIP_8.Graphics;
using CHIP_8.Machines;
using ImGuiNET;
using static SDL2.SDL;
namespace CHIP_8;
/// <summary>
/// Custom binding context for OpenGL <-> SDL.
/// OpenTK 4.x requires explicit binding initialization, this class is used to tell
/// it about the SDL OpenGL context which is created when the renderer is created.
/// </summary>
public class SDL_GL_BindingsContext : IBindingsContext
{
public IntPtr GetProcAddress(string procName) => SDL_GL_GetProcAddress(procName);
}
/// <summary>
/// Main Emulator program loop, this builds all the components to make the emulator
/// run and is responsible for the render pipeline.
/// </summary>
public static class Program
{
private const int MenuH = 20;
private const int ScreenW = 64;
private const int ScreenH = 32;
private const int Scale = 8;
private static void Main(string[] args)
{
// --------------------------------------------------------------------
// ROM Discovery
// --------------------------------------------------------------------
string ROMDir = Path.Combine(AppContext.BaseDirectory, "ROMs");
string[] ROMs = Directory.GetFiles(ROMDir)
.OrderBy(Path.GetFileNameWithoutExtension)
.ToArray();
int currentROM = 0; // currently loaded ROM index
string[] ROMNames = ROMs.Select(Path.GetFileName).ToArray()!;
// --------------------------------------------------------------------
// SDL and emulator initialization
// --------------------------------------------------------------------
const uint flags = SDL_INIT_VIDEO |
SDL_INIT_AUDIO |
SDL_INIT_TIMER |
SDL_INIT_EVENTS;
if (SDL_Init(flags) < 0)
throw new Exception($"SDL init FAIL: {SDL_GetError()}");
// Setup OpenGL Attributes
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GLattr.SDL_GL_STENCIL_SIZE, 8);
nint window = SDL_CreateWindow(
"CHIP-8",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
(ScreenW * Scale),
(ScreenH * Scale) + MenuH,
SDL_WindowFlags.SDL_WINDOW_OPENGL);
if (window == 0)
throw new Exception($"SDL_CreateWindow FAIL: {SDL_GetError()}");
nint glContext = SDL_GL_CreateContext(window);
if (glContext == 0)
throw new Exception($"SDL_GL_CreateContext FAIL: {SDL_GetError()}");
// Set up the Window
SDL_GL_MakeCurrent(window, glContext); // Active window
SDL_GL_SetSwapInterval(1); // VSync
GL.LoadBindings(new SDL_GL_BindingsContext());
GL.Viewport(0, 0, ScreenW * Scale, ScreenH * Scale);
GL.ClearColor(201f / 255f, 219f / 255f, 167f / 255f, 1f);
FullscreenQuad.Init();
// --------------------------------------------------------------------
// ImGUI
// --------------------------------------------------------------------
SDL_GetWindowSize(window, out int windowWidth, out int windowHeight);
var imGui = new ImGuiController(windowWidth, windowHeight);
// --------------------------------------------------------------------
// VM Components
// --------------------------------------------------------------------
CHIP8 machine = new ();
AudioDriver audio = new (machine);
InputDriver input = new (machine);
CHIP8Texture texture = new ();
LCDDecayBuffer lcdBuffer = new (64, 32);
LCDDecayPass lcdPass = new ();
QuadRenderer renderer = new ();
// --------------------------------------------------------------------
// Emulator
// --------------------------------------------------------------------
Emulator emulator = new (machine);
void LoadRom(int romIndex)
{
currentROM = romIndex;
emulator.Reset();
machine.LoadProgram(File.ReadAllBytes(ROMs[currentROM]));
}
if (ROMs.Length > 0) LoadRom(currentROM);
// --------------------------------------------------------------------
// Main program loop
// --------------------------------------------------------------------
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
float lastTime = 0f;
bool pauseForFocus = false;
while (true)
{
// ---- Track Time ----
float now = (float)stopwatch.Elapsed.TotalSeconds;
float delta = now - lastTime;
lastTime = now;
// ---- Process Events ----
while (SDL_PollEvent(out SDL_Event e) != 0)
{
imGui.ProcessEvent(ref e);
switch (e.type)
{
case SDL_EventType.SDL_QUIT: goto Quit;
case SDL_EventType.SDL_WINDOWEVENT:
{
switch (e.window.windowEvent)
{
case SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST:
pauseForFocus = true;
break;
case SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED:
pauseForFocus = false;
break;
}
break;
}
case SDL_EventType.SDL_KEYDOWN:
case SDL_EventType.SDL_KEYUP:
{
// Only pass keyboard input to emulation if not used by ImGui
if (!ImGui.GetIO().WantCaptureKeyboard)
input.HandleEvent(e);
break;
}
}
}
// ---- ImGUI Frame ----
SDL_GetWindowSize(window, out windowWidth, out windowHeight);
imGui.UpdateFrame(delta, windowWidth, windowHeight);
// ---- Draw UI ----
if (ImGui.BeginMainMenuBar())
{
if (ImGui.BeginMenu("ROMs"))
{
if (ROMs.Length == 0)
{
ImGui.MenuItem("No ROMs found", string.Empty, false, false);
}
else
{
for (int i = 0; i < ROMs.Length; i++)
{
if (ImGui.MenuItem(ROMNames[i], string.Empty, i == currentROM))
LoadRom(i);
}
}
ImGui.EndMenu();
}
ImGui.EndMainMenuBar();
}
bool pauseForPopup = ImGui.IsPopupOpen(string.Empty, ImGuiPopupFlags.AnyPopupId);
// Pause the emulation if any of the triggers caused a pause.
if (pauseForFocus || pauseForPopup)
emulator.Pause();
else
emulator.Resume();
// ---- Update Emulation ----
emulator.Update();
// ---- Render to Screen ----
GL.Clear(ClearBufferMask.ColorBufferBit);
// CHIP-8 output (reduced window size)
int menuHeight = (int) ImGui.GetFrameHeight();
int fbWidth = windowWidth;
int fbHeight = windowHeight - menuHeight;
GL.Viewport(0, 0, fbWidth, fbHeight);
texture.Upload(machine.Display);
// LCD simulation pass
lcdPass.Execute(
texture.TextureId,
lcdBuffer.ReadTexture,
lcdBuffer.WriteFbo);
lcdBuffer.Swap();
// Presentation pass
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
renderer.Draw(
lcdBuffer.ReadTexture,
fbWidth,
fbHeight);
// Reset viewport for ImGui
GL.Viewport(0, 0, windowWidth, windowHeight);
imGui.Render();
SDL_GL_SwapWindow(window);
}
// --------------------------------------------------------------------
// Cleanup
// --------------------------------------------------------------------
Quit:
{
renderer.Dispose();
texture.Dispose();
imGui.Dispose();
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
}
}
}