-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
244 lines (216 loc) · 7.73 KB
/
Form1.cs
File metadata and controls
244 lines (216 loc) · 7.73 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace GameLoadingInterface
{
public partial class Form1 : Form
{
// MUST DEFINE ROM FILENAMES HERE
const byte MCU_signal_byte = 0x55;
public enum Game
{
PONG,
SPACE_INVADERS,
TETRIS,
BREAKOUT,
TEST_OPCODE,
CONNECT4,
INVALID,
};
public Dictionary<Game, string> games = new Dictionary<Game, string>
{
{ Game.PONG, pong_filename },
{ Game.SPACE_INVADERS, space_invaders_filename },
{ Game.TETRIS, tetris_filename },
{ Game.BREAKOUT, breakout_filename },
{ Game.TEST_OPCODE, test_opcode_filename },
{ Game.CONNECT4, connect4_filename }
};
Game current_game = Game.INVALID;
public Form1()
{
InitializeComponent();
buttonUpload.Enabled = false;
}
int game_data_length;
byte[] game_data;
private void ReadProgram()
{
var fs = new FileStream(games[current_game], FileMode.Open);
game_data_length = (int)fs.Length;
Console.WriteLine("Opened a game that is " + game_data_length.ToString() + " bytes large.");
game_data = new byte[game_data_length];
fs.Read(game_data, 0, game_data_length);
/* Memory dump */
for (int ix = 0; ix < game_data_length; ix += 16)
{
var cnt = Math.Min(16, game_data_length - ix);
var line = new byte[cnt];
Array.Copy(game_data, ix, line, 0, cnt);
// Write address + hex + ascii
Console.Write("{0:X6} ", ix);
Console.Write(BitConverter.ToString(line));
Console.Write(" ");
// Convert non-ascii characters to .
for (int jx = 0; jx < cnt; ++jx)
if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';
Console.WriteLine(Encoding.ASCII.GetString(line));
}
Console.ReadLine();
}
private void SendGameFile()
{
if (serialPort1.IsOpen)
{
byte[] header = { 255, (byte)(game_data_length >> 8), (byte)(game_data_length & 0x00FF) };
serialPort1.Write(header, /*offset=*/0, /*num_bytes=*/3);
serialPort1.Write(game_data, /*offset=*/0, /*num_bytes=*/game_data_length);
}
}
private void buttonUpload_MouseClick(object sender, MouseEventArgs e)
{
if (current_game == Game.INVALID || !serialPort1.IsOpen)
{
return;
}
ReadProgram();
}
private void checkBoxPong_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxPong.Checked)
{
current_game = Game.PONG;
buttonUpload.Enabled = true;
checkBoxSpaceInvaders.Checked = false;
checkBoxTetris.Checked = false;
checkBoxBreakout.Checked = false;
checkBoxTestOpcode.Checked = false;
checkBoxConnect4.Checked = false;
}
}
private void checkBoxSpaceInvaders_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxSpaceInvaders.Checked)
{
current_game = Game.SPACE_INVADERS;
buttonUpload.Enabled = true;
checkBoxPong.Checked = false;
checkBoxTetris.Checked = false;
checkBoxBreakout.Checked = false;
checkBoxTestOpcode.Checked = false;
checkBoxConnect4.Checked = false;
}
}
private void checkBoxTetris_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxTetris.Checked)
{
current_game = Game.TETRIS;
buttonUpload.Enabled = true;
checkBoxSpaceInvaders.Checked = false;
checkBoxPong.Checked = false;
checkBoxBreakout.Checked = false;
checkBoxTestOpcode.Checked = false;
checkBoxConnect4.Checked = false;
}
}
private void checkBoxBreakout_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxBreakout.Checked)
{
current_game = Game.BREAKOUT;
buttonUpload.Enabled = true;
checkBoxSpaceInvaders.Checked = false;
checkBoxTetris.Checked = false;
checkBoxPong.Checked = false;
checkBoxTestOpcode.Checked = false;
checkBoxConnect4.Checked = false;
}
}
private void checkBoxTestOpcode_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxTestOpcode.Checked)
{
current_game = Game.TEST_OPCODE;
buttonUpload.Enabled = true;
checkBoxSpaceInvaders.Checked = false;
checkBoxTetris.Checked = false;
checkBoxPong.Checked = false;
checkBoxBreakout.Checked = false;
checkBoxConnect4.Checked = false;
}
}
private void comboBoxCOMPort_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort1.PortName = comboBoxCOMPort.SelectedItem.ToString();
}
private void buttonCOMPort_MouseClick(object sender, MouseEventArgs e)
{
// open/close serial port
if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
buttonCOMPort.Text = "Close Serial";
} catch
{
// unhandled error
}
} else
{
serialPort1.Close();
buttonCOMPort.Text = "Connect Serial";
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Update COM box
comboBoxCOMPort.Items.Clear();
comboBoxCOMPort.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
if (comboBoxCOMPort.Items.Count == 0)
{
comboBoxCOMPort.Text = "No COM ports!";
} else
{
comboBoxCOMPort.SelectedIndex = 0;
}
}
private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int new_byte = 0;
int bytes_to_read = serialPort1.BytesToRead;
while (bytes_to_read > 0)
{
new_byte = serialPort1.ReadByte();
// Check if MCU is signaling a new game download
if (new_byte == MCU_signal_byte)
{
SendGameFile();
}
bytes_to_read = serialPort1.BytesToRead;
}
}
private void checkBoxConnect4_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxConnect4.Checked)
{
current_game = Game.CONNECT4;
buttonUpload.Enabled = true;
checkBoxSpaceInvaders.Checked = false;
checkBoxTetris.Checked = false;
checkBoxPong.Checked = false;
checkBoxBreakout.Checked = false;
}
}
}
}