forked from PalatinCoder/PIC-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
162 lines (143 loc) · 5.63 KB
/
MainPage.xaml.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
using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace PIC_Simulator
{
/// <summary>
/// Die Hauptseite des PIC Simulators, auf der das Sourcecode Listing und die Register angezeigt werden.
/// </summary>
public sealed partial class MainPage : Page, ISourcecodeViewInterface
{
private MainPageViewModel ViewModel;
private Processor processor;
public MainPage()
{
this.InitializeComponent();
this.processor = new Processor(this);
this.ViewModel = new MainPageViewModel();
this.DataContext = this.ViewModel;
}
private async void HelpButton_Click(object sender, RoutedEventArgs e)
{
StorageFile pdf = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\HilfedokuPicSim.pdf");
await Windows.System.Launcher.LaunchFileAsync(pdf);
}
private async void OpenFileChooser_Click(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker()
{
ViewMode = PickerViewMode.List
};
picker.FileTypeFilter.Add(".LST");
StorageFile file = await picker.PickSingleFileAsync();
if (file == null) return; // Benutzer hat abbrechen gedrückt
this.statusbar.Text = "Programm " + file.DisplayName + " wird geöffnet";
try
{
this.processor.ProgramMemory.Clear();
this.ViewModel.Sourcecode.Clear();
await ListingFileParser.Parse(file, SourcecodeLineParsed);
this.processor.Reset();
this.statusbar.Text = file.DisplayName + " geladen";
this.ViewModel.IsSimInitialized = true;
}
catch (ArgumentOutOfRangeException)
{
var dialog = new MessageDialog(
"Beim Einlesen der Datei ist ein Fehler aufgetreten.\r\nMeistens hilft es die Datei nach UTF-8 zu konvertieren",
"Programm kann nicht geladen werden");
#pragma warning disable CS4014 // Da dieser Aufruf nicht abgewartet wird, wird die Ausführung der aktuellen Methode fortgesetzt, bevor der Aufruf abgeschlossen ist
dialog.ShowAsync();
#pragma warning restore CS4014
}
#if !DEBUG
catch (Exception ex)
{
var dialog = new MessageDialog(
"Beim Einlesen der Datei ist ein Fehler aufgetreten:\r\n" + ex.Message,
"Programm kann nicht geladen werden");
#pragma warning disable CS4014 // Da dieser Aufruf nicht abgewartet wird, wird die Ausführung der aktuellen Methode fortgesetzt, bevor der Aufruf abgeschlossen ist
dialog.ShowAsync();
#pragma warning restore CS4014
}
#endif
}
/// <summary>
/// Callback wenn eine Zeile geparst wurde. Fügt die Instruction dem Listing hinzu.
/// </summary>
/// <param name="instruction">Die generierte Instruction</param>
private void SourcecodeLineParsed(Instruction instruction)
{
this.ViewModel.Sourcecode.Add(instruction);
if (instruction is ProcessorInstruction) processor.ProgramMemory.Add((ProcessorInstruction)instruction);
}
public void SetCurrentSourcecodeLine(int line)
{
this.SourcecodeListingView.SelectedIndex = line;
this.SourcecodeListingView.ScrollIntoView(this.SourcecodeListingView.SelectedItem, ScrollIntoViewAlignment.Leading);
}
public void SetIsProgrammRunning(bool value)
{
this.ViewModel.IsProgramRunning = value;
}
private void StepButton_Click(object sender, RoutedEventArgs e)
{
this.processor.Step();
}
private void RunButton_Click(object sender, RoutedEventArgs e)
{
this.processor.Clock.Start();
this.ViewModel.IsProgramRunning = true;
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
this.processor.Clock.Stop();
this.ViewModel.IsProgramRunning = false;
}
private void ResetButton_Click(object sender, RoutedEventArgs e)
{
this.processor.Reset();
}
public void IncreaseStopwatch(TimeSpan value)
{
this.ViewModel.Stopwatch = this.ViewModel.Stopwatch.Add(value);
}
public void ResetStopwatch()
{
this.ViewModel.Stopwatch = new TimeSpan();
}
private void IORegister_FlipBit(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
byte i = (byte)(sender as GridView).SelectedIndex;
ushort address;
switch ((sender as GridView).Tag)
{
case "PortA":
address = 0x05;
break;
case "PortB":
address = 0x06;
break;
case "TrisA":
address = 0x85;
break;
case "TrisB":
address = 0x86;
break;
default:
address = 0xFF;
break;
}
if (this.processor.memController.GetBit(address, i) == 0)
{
this.processor.memController.SetBit(address, i);
} else
{
this.processor.memController.ClearBit(address, i);
}
}
}
}