-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
272 lines (239 loc) · 9.19 KB
/
Form1.cs
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
269
270
271
272
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ChocolateCHIP
{
public partial class Form1 : Form
{
//Event loop helper functions/structs
[StructLayout(LayoutKind.Sequential)]
public struct NativeMessage
{
public IntPtr Handle;
public uint Message;
public IntPtr WParameter;
public IntPtr LParameter;
public uint Time;
public Point Location;
}
[DllImport("user32.dll")]
public static extern int PeekMessage(out NativeMessage message, IntPtr window, uint filterMin, uint filterMax, uint remove);
private Chip8 chip8;
private Color foregroundColor;
private Color backgroundColor;
//Scale a bitmap to a given size (used to scale CHIP-8's 64 x 32 frame to a viewable size)
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.NearestNeighbor; //NO FILTERING!!
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
public Bitmap GetFrameBitmap(byte[] frame)
{
Bitmap frameBitmap = new Bitmap(64, 32);
for (int y = 0; y < 32; y++)
{
for (int x = 0; x < 64; x++)
{
if (frame[(y * 64) + x] == 0)
{
frameBitmap.SetPixel(x, y, backgroundColor);
}
else
{
frameBitmap.SetPixel(x, y, foregroundColor);
}
}
}
return ResizeImage(frameBitmap, 640, 320);
}
public Form1()
{
chip8 = new Chip8();
chip8.Initialize();
Application.Idle += HandleApplicationIdle;
InitializeComponent();
clockSpdUpDown.Value = chip8.GetClockSpeedHz();
foregroundColor = Color.White;
backgroundColor = Color.Black;
colorComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
colorComboBox.Items.Insert(0, "White on black");
colorComboBox.Items.Insert(1, "Black on white");
colorComboBox.Items.Insert(2, "GameBoy");
colorComboBox.Items.Insert(3, "LED");
colorComboBox.Items.Insert(4, "Vapor");
colorComboBox.Items.Insert(5, "Mainframe");
colorComboBox.Items.Insert(6, "Slate");
colorComboBox.SelectedIndex = 0;
}
public void HandleApplicationIdle(object sender, EventArgs e)
{
//Winforms event loop wrapper
while (IsApplicationIdle())
{
if (!debugCheckBox.Checked)
{
RunCPU();
//simulate clock tick by suspending the main thread
int sleepTimeMS = (int)(((double)1 / chip8.GetClockSpeedHz()) * 1000);
Thread.Sleep(sleepTimeMS);
}
}
}
//check if the winforms app is currently idle (used for CHIP8 event loop)
bool IsApplicationIdle()
{
NativeMessage result;
return PeekMessage(out result, IntPtr.Zero, (uint)0, (uint)0, (uint)0) == 0;
}
//Execute 1 CPU cycle and render the result
public void RunCPU()
{
chip8.EmulateCycle();
if (printDebugCheckBox.Checked)
{
debugTextBox.Text = chip8.GetDebugInfo();
//set the instruction list box index to the current instruction
if (instructionsListBox.Items.Count > 0)
{
instructionsListBox.SetSelected((chip8.GetPC() - 0x200), true);
}
}
else
{
debugTextBox.Clear();
}
if (chip8.GetDrawFlag())
{
frameBox.Image = GetFrameBitmap(chip8.GetFrameBuffer());
chip8.SetDrawFlag(false);
}
}
private void ResetButton_Click(object sender, EventArgs e)
{
chip8.Reset();
chip8.LoadROM(chip8.GetFileName());
instructionsListBox.DataSource = chip8.GetROMInstructions();
if (printDebugCheckBox.Checked)
{
debugTextBox.Text = chip8.GetDebugInfo();
}
else
{
debugTextBox.Clear();
}
}
//Step through one CPU cycle for debug purposes
private void StepButton_Click(object sender, EventArgs e)
{
RunCPU();
}
//view CHIP8 memory at a user-requested address for debug purposes
private void ViewMemoryButton_Click(object sender, EventArgs e)
{
ushort addr = Convert.ToUInt16(memoryTextBox.Text, 16);
string hexVal = "0x" + String.Format("{0:X4}", Convert.ToString(chip8.GetByteAtAddr(addr), 16));
memoryTextBox.Text = hexVal;
}
private void SetClockSpeedButton_Click(object sender, EventArgs e)
{
uint newClockSpeed = (uint)clockSpdUpDown.Value;
chip8.SetClockSpeedHz(newClockSpeed);
}
private void openROMButton_Click(object sender, EventArgs e)
{
DialogResult result = openROMDialog.ShowDialog();
if (result == DialogResult.OK)
{
string ROMName = openROMDialog.FileName;
chip8.Reset();
try
{
chip8.LoadROM(ROMName);
if (printDebugCheckBox.Checked)
{
instructionsListBox.DataSource = chip8.GetROMInstructions();
}
debugCheckBox.Checked = false;
}
catch (System.IO.IOException)
{
MessageBox.Show("Please select a valid CHIP-8 ROM.", "ROM Load Error");
}
}
}
private void setThemeButton_Click(object sender, EventArgs e)
{
int themeIndex = colorComboBox.SelectedIndex;
switch (themeIndex)
{
case 0:
foregroundColor = Color.White;
backgroundColor = Color.Black;
break;
case 1:
foregroundColor = Color.Black;
backgroundColor = Color.White;
break;
case 2:
foregroundColor = Color.DarkGreen;
backgroundColor = Color.YellowGreen;
break;
case 3:
foregroundColor = Color.Red;
backgroundColor = Color.Black;
break;
case 4:
foregroundColor = Color.DarkViolet;
backgroundColor = Color.DarkTurquoise;
break;
case 5:
foregroundColor = Color.Lime;
backgroundColor = Color.Black;
break;
case 6:
foregroundColor = Color.LightSlateGray;
backgroundColor = Color.DarkSlateGray;
break;
default:
return;
}
frameBox.Image = GetFrameBitmap(chip8.GetFrameBuffer());
}
private void printDebugCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (printDebugCheckBox.Checked)
{
instructionsListBox.DataSource = chip8.GetROMInstructions();
}
else
{
instructionsListBox.DataSource = null;
}
}
}
}