-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.axaml.cs
325 lines (285 loc) · 10.9 KB
/
MainWindow.axaml.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#define USE_DEBUG_TOOLS
//#define RESET_INPUT_UPON_READING
using Avalonia.Controls;
using Avalonia.Interactivity;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Threading;
namespace Nema
{
public partial class MainWindow : Window
{
private Emulator.Emulator _emulator;
public Emulator.Emulator InterpreterObject => _emulator;
private bool _displayOutAsText = false;
private ObservableCollection<string> _errors = new ObservableCollection<string>();
private ObservableCollection<string> _output = new ObservableCollection<string>();
public ObservableCollection<string> Output => _output;
public ObservableCollection<string> Errors => _errors;
public Emulator.ProcessorFlags Flags => _emulator.Flags;
public Emulator.Registers Registers => _emulator.Registers;
public string ProgramCounter { get; set; } = "00";
private int _emulationSleepLength = 1;
private string? _currentFile;
public bool DisplayOutAsText
{
get => _displayOutAsText;
set
{
_displayOutAsText = value;
if (value)
{
int port = 0;
foreach (byte b in _emulator.OutputPorts)
{
_output[port++] = System.Text.Encoding.ASCII.GetString(new[] { b });
}
}
else
{
int port = 0;
foreach (byte b in _emulator.OutputPorts)
{
_output[port++] = b.ToString("X2");
}
}
}
}
private string _lineCountText = string.Empty;
private string _codeText = string.Empty;
public string LineNumberText => _lineCountText;
public string CodeText
{
get => _codeText;
set
{
_codeText = value;
int lines = value.Split("\n").Length;
_lineCountText = string.Empty;
for (int i = 0; i < lines; i++)
{
_lineCountText += i.ToString() + "\n";
}
LineNumberBox.Text = _lineCountText;
}
}
public MainWindow()
{
InitializeComponent();
_emulator = new Emulator.Emulator();
_emulator.OnOutPortValueChanged += _onOutPortValueChanged;
_emulator.OnInPortRead += _inputRead;
_emulator.OnInputResetRequested += _resetInput;
MemoryGrid.Items = _emulator.Memory.MemoryDisplayGrid;
List<string> temp = new List<string>();
foreach (byte b in _emulator.OutputPorts)
{
temp.Add(b.ToString("X2"));
}
_output = new ObservableCollection<string>(temp);
InputTable.OnPortValueChanged += _onInValueChanged;
this.DataContext = this;
}
private void _inputRead(int port)
{
#if RESET_INPUT_UPON_READING
_interpreter.SetIn(port, 0);
InputTable.SetPortValue(port, 0);
#endif
}
private void _resetInput()
{
InputTable.ResetPorts();
}
private void _displayFatalError(string msg)
{
ErrorMsgBox.Text = msg;
ErrorMsgBox.Foreground= new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Red);
}
private void _displayErrors(IEnumerable<string> errors)
{
ErrorMsgBox.Text = string.Join("\n", errors);
ErrorMsgBox.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Black);
}
private void _clearErrors()
{
ErrorMsgBox.Text = string.Empty;
ErrorMsgBox.Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.Black);
_errors.Clear();
}
public async Task RunEmulator()
{
try
{
while (_emulator.IsRunning)
{
_emulator.Step();
ProgramCounterLabel.Text = _emulator.ProgramCounter.ToString("X2");
if (_emulator.CurrentStepCounter >= _emulator.StepsBeforeSleep)
{
await Task.Delay(_emulationSleepLength);
_emulator.ResetStepCounter();
}
}
System.Console.WriteLine("Exited execution");
}
catch (Emulator.ProtectedMemoryWriteException e)
{
_displayFatalError($"Execution error : {e.Message}");
return;
}
}
private void _onOutPortValueChanged(int port, byte value)
{
_output[port] = _displayOutAsText ? System.Text.Encoding.ASCII.GetString(new[] { value }) : value.ToString("X2");
}
private void _recordErrors(Dictionary<int, string> errors)
{
_clearErrors();
foreach (KeyValuePair<int, string> error in errors)
{
_errors.Add($"Line: {error.Key}. Error: {error.Value}");
}
_displayErrors(_errors);
}
/// <summary>
/// Simply resets memory and starts execution
/// </summary>
private void _run()
{
_emulator.SoftResetProcessor();
Dispatcher.UIThread.Post(() => RunEmulator(), DispatcherPriority.Background);
}
private async void _loadRom()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filters?.Add(new FileDialogFilter() { Name = "Binary files", Extensions = { "bin" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "ROM file", Extensions = { "rom" } });
string? file = await dialog.ShowAsync(this);
if (file == null)
{
return;
}
byte[] rom = System.IO.File.ReadAllBytes(file);
_emulator.Memory.WriteRom(rom);
_emulator.ResetProcessor();
}
private async void _dumpRom()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filters?.Add(new FileDialogFilter() { Name = "Binary files", Extensions = { "bin", "dat" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "ROM file", Extensions = { "rom" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "Any", Extensions = { "*" } });
string? file = await dialog.ShowAsync(this);
if (file == null)
{
return;
}
await System.IO.File.WriteAllBytesAsync(file, _emulator.Memory.ReadRom());
}
private async void _dumpRam()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filters?.Add(new FileDialogFilter() { Name = "Binary files", Extensions = { "bin", "dat" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "Any", Extensions = { "*" } });
string? file = await dialog.ShowAsync(this);
if (file == null)
{
return;
}
await System.IO.File.WriteAllBytesAsync(file, _emulator.Memory.ReadRam());
}
private void _clearRom()
{
_emulator.Memory.ClearMemory();
}
/// <summary>
/// Opens a file dialog for saving code to the file
/// </summary>
/// <param name="newFile">If true function will work as if no file has already been opened</param>
private async void _saveFile(bool newFile)
{
if (!newFile && _currentFile != null)
{
System.IO.File.WriteAllText(_currentFile, CodeInputBox.Text);
return;
}
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filters?.Add(new FileDialogFilter() { Name = "Text files", Extensions = { "txt" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "Assembly file", Extensions = { "asm", "80asm", "nema" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "Any", Extensions = { "*" } });
_currentFile = await dialog.ShowAsync(this);
Title = $"NEMA-8 {_currentFile ?? string.Empty}";
if (_currentFile == null)
{
return;
}
System.IO.File.WriteAllText(_currentFile, CodeInputBox.Text);
}
private async void _loadFile()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filters?.Add(new FileDialogFilter() { Name = "Text files", Extensions = { "txt" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "Assembly file", Extensions = { "asm", "80asm", "nema" } });
dialog.Filters?.Add(new FileDialogFilter() { Name = "Any", Extensions = { "*" } });
_currentFile = await dialog.ShowAsync(this);
Title = $"NEMA-8 {_currentFile ?? string.Empty}";
if (_currentFile == null)
{
return;
}
string text = System.IO.File.ReadAllText(_currentFile);
CodeInputBox.Text = text;
}
private async void _onSettingsButtonPressed(object? sender, RoutedEventArgs e)
{
SettingsWindow settingsWindow = new SettingsWindow();
await settingsWindow.ShowDialog(this);
}
private void _stopEmulation()
{
_emulator.Stop();
}
private void _assemble()
{
if (!string.IsNullOrWhiteSpace(CodeInputBox.Text))
{
Emulator.Converter converter = new Emulator.Converter(CodeInputBox.Text, _emulator.Memory.MemoryData);
_clearErrors();
if (converter.Success)
{
_emulator.SetCode(converter.Result);
_emulator.ResetProcessor();
}
else
{
_recordErrors(converter.Errors);
}
}
}
private void _onInValueChanged(int port, byte value)
{
_emulator.SetIn(port, value);
}
private void _step()
{
_emulator.Step();
}
private void _onExitRequested(object? sender, RoutedEventArgs e)
{
Close();
}
private async void _displaySettings()
{
SettingsWindow settings = new SettingsWindow();
await settings.ShowDialog(this);
_emulationSleepLength = settings.EmulationSpeed;
}
private async void _displayHelp()
{
HelpWindow help = new HelpWindow();
await help.ShowDialog(this);
}
}
}