-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cpp
145 lines (122 loc) · 4.26 KB
/
Form1.cpp
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
#include "pch.h"
#include "Form1.h"
#include <sstream>
#include <stdexcept>
#include <string>
System::Void CppCLRWinFormsProject::Form1::Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
for (int i = 0; i < 16; i++) {
RegistersValueList[i]->Text = machine->getRegisterNumber(i);
}
MemoryDataGrid->Rows->Clear();
for (int i = 0; i < 256; i++) {
MemoryDataGrid->Rows->Add(makeHex(i), machine->getTheMemoryNumber(i));
}
outputBox->Text = makeSysString(machine->output);
Form1::GetPC();
Form1::GetIR();
}
System::Void CppCLRWinFormsProject::Form1::LoadDataButton_Click(System::Object^ sender, System::EventArgs^ e)
{
vector <int> instructionsVector = Form1::GetInstructions();
instructions = &instructionsVector;
machine->ResetMemoryAndRegisters();
machine->loadMemory(*instructions);
UpdateMemoryAndRegisters();
}
System::Void CppCLRWinFormsProject::Form1::RunButton_Click(System::Object^ sender, System::EventArgs^ e)
{
try {
machine->executeSimulator();
}
catch (...){
MessageBox::Show("There might be something wrong please check your instructions are valid or not", "Something went wrong", MessageBoxButtons::OK, MessageBoxIcon::Error);
}
UpdateMemoryAndRegisters();
}
System::Void CppCLRWinFormsProject::Form1::SingelStepButton_Click(System::Object^ sender, System::EventArgs^ e)
{
try {
machine->executeNext();
}
catch (...) {
MessageBox::Show("There might be something wrong please check your instructions are valid or not", "Something went wrong", MessageBoxButtons::OK, MessageBoxIcon::Error);
}
UpdateMemoryAndRegisters();
}
System::Void CppCLRWinFormsProject::Form1::ResetButton_Click(System::Object^ sender, System::EventArgs^ e)
{
machine->ResetMemoryAndRegisters();
UpdateMemoryAndRegisters();
}
System::Void CppCLRWinFormsProject::Form1::HaltButton_Click(System::Object^ sender, System::EventArgs^ e)
{
machine->executeOp(0xC, 000);
}
// gets a list of textboxes of the Registers
void CppCLRWinFormsProject::Form1::InitializeRegList()
{
RegistersValueList = gcnew List<System::Windows::Forms::TextBox^>();
RegistersValueList->Add(R0ValueBox);
RegistersValueList->Add(R1ValueBox);
RegistersValueList->Add(R2ValueBox);
RegistersValueList->Add(R3ValueBox);
RegistersValueList->Add(R4ValueBox);
RegistersValueList->Add(R5ValueBox);
RegistersValueList->Add(R6ValueBox);
RegistersValueList->Add(R7ValueBox);
RegistersValueList->Add(R8ValueBox);
RegistersValueList->Add(R9ValueBox);
RegistersValueList->Add(RAValueBox);
RegistersValueList->Add(RBValueBox);
RegistersValueList->Add(RCValueBox);
RegistersValueList->Add(RDValueBox);
RegistersValueList->Add(REValueBox);
RegistersValueList->Add(RFValueBox);
}
void CppCLRWinFormsProject::Form1::UpdateMemoryAndRegisters() {
for (int i = 0; i < 16; i++) {
RegistersValueList[i]->Text = machine->getRegisterNumber(i);
}
for (int i = machine->counter.store(); i < 256; i++) {
MemoryDataGrid->Rows[i]->Cells[0]->Value = makeHex(i);
MemoryDataGrid->Rows[i]->Cells[1]->Value = machine->getTheMemoryNumber(i);
}
outputBox->Text = makeSysString(machine->output);
Form1::GetPC();
Form1::GetIR();
}
void CppCLRWinFormsProject::Form1::GetPC() {
PCBOX->Text = machine->getProgramCounter();
SelectCurrentInstructionRow(machine->counter.store());
}
void CppCLRWinFormsProject::Form1::GetIR()
{
System::String^ IR = makeSysString(machine->IR);
IRBOX->Text = IR;
}
// converts from string to system::string
System::String^ CppCLRWinFormsProject::Form1::makeSysString(string NormalString) {
return msclr::interop::marshal_as<System::String^>(NormalString);
}
// converts decimal integers to hexadicimal
System::String^ CppCLRWinFormsProject::Form1::makeHex(int number) {
stringstream stream;
stream << hex << uppercase << number;
string Hex = stream.str();
return makeSysString(Hex);
}
// reads the instructions from the inputBox
vector<int> CppCLRWinFormsProject::Form1::GetInstructions() {
vector<int> Instructions;
string Instruction = msclr::interop::marshal_as<std::string>(InstructionsBox->Text);
stringstream ss(Instruction);
while (ss >> Instruction) {
Instructions.push_back(stoi(Instruction, nullptr, 16));
}
return Instructions;
}
void CppCLRWinFormsProject::Form1::SelectCurrentInstructionRow(int index) {
MemoryDataGrid->ClearSelection();
MemoryDataGrid->Rows[index]->Selected = true;
}