-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConsoleWindow.cpp
More file actions
288 lines (259 loc) · 7.79 KB
/
ConsoleWindow.cpp
File metadata and controls
288 lines (259 loc) · 7.79 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
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
// Copyright 2003 Ashley Wise
// University Of Illinois Urbana-Champaign
// awise@crhc.uiuc.edu
#include "ConsoleWindow.h"
#include <FL/Fl.H>
#include <FL/fl_ask.H>
#include "Project.h"
#include "SimulatorWindow.h"
#include "MainWindow.h"
#include "../AsmConvertLC3/AsmConvertLC3.h"
using namespace std;
using namespace JMT;
using namespace Assembler;
namespace AshIDE {
Fl_Menu_Item ConsoleWindow::MenuItems[] =
{
{ "&Console", 0, 0, 0, FL_SUBMENU },
{ "&Save", FL_CTRL + 's', SaveCB, 0, FL_MENU_DIVIDER },
{ "&Copy", FL_CTRL + 'c', CopyCB },
{ "&Clear", 0, ClearCB, 0, FL_MENU_DIVIDER },
{ "&Help", FL_F + 1, HelpCB, 0, FL_MENU_DIVIDER },
{ "C&lose", FL_Escape, HideCB },
{ 0 },
{ "&Search", 0, 0, 0, FL_SUBMENU },
{ "&Find...", FL_CTRL + 'f', FindCB },
{ "F&ind Again", FL_CTRL + 'g', FindAgainCB },
{ 0 },
{ "S&imulate", 0, 0, 0, FL_SUBMENU },
{ "&Go", FL_F + 5, SimulatorWindow::GoCB },
{ "Go One &Instruction", FL_F + 6, SimulatorWindow::GoOneCB, 0, FL_MENU_DIVIDER },
{ "Step I&nto Next Function", FL_F + 8, SimulatorWindow::StepInCB },
{ "Step O&ver Next Function", FL_F + 9, SimulatorWindow::StepOverCB },
{ "Step &Out Of Current Function", FL_F + 10, SimulatorWindow::StepOutCB, 0, FL_MENU_DIVIDER },
{ "&Signal Interrupt", 0, SimulatorWindow::InterruptCB },
{ "Brea&k", FL_F + 11, SimulatorWindow::BreakCB },
{ "&Reset", FL_F + 12, SimulatorWindow::ResetCB },
{ "&End Simulation", FL_CTRL + 'q', SimulatorWindow::StopCB, 0, FL_MENU_DIVIDER },
{ "&Breakpoints", 0, 0, 0, FL_SUBMENU },
{ "&Breakpoints", FL_CTRL + 'b', SimulatorWindow::BreakpointsCB },
{ 0 },
{ "Vie&w", 0, 0, 0, FL_SUBMENU },
{ "&Console I/O", FL_CTRL + 'i', SimulatorWindow::ConsoleCB, 0, FL_MENU_DIVIDER },
{ "&Disassembly", FL_CTRL + 'd', SimulatorWindow::DisassemblyCB },
{ "Call &Stack", FL_CTRL + 'k', SimulatorWindow::CallStackCB, 0, FL_MENU_DIVIDER },
{ "&Instructions", FL_CTRL + '1', SimulatorWindow::InstructionsCB },
{ "D&ata Values", FL_CTRL + '2', SimulatorWindow::DataValuesCB },
{ "&Memory Bytes", FL_CTRL + '3', SimulatorWindow::MemoryBytesCB },
{ "&Registers", FL_CTRL + '4', SimulatorWindow::RegistersCB },
{ "Pipe&lines", FL_CTRL + '5', SimulatorWindow::PipelinesCB },
{ "&Programs", FL_CTRL + '6', SimulatorWindow::ProgramsCB },
{ 0 },
{ "&Edit && Continue", 0, 0, 0, FL_SUBMENU },
{ "Edit &Instruction/Data/Memory", FL_F + 2, SimulatorWindow::WriteDataCB },
{ "Edit &Register", FL_F + 3, SimulatorWindow::WriteRegisterCB },
{ 0 },
{ 0 },
{ 0 }
};
ConsoleWindow::ConsoleWindow() : Fl_Double_Window(320, 200)
{
{
//Setup the menu
pMainMenu = new Fl_Menu_Bar(0, 0, w(), 28);
pMainMenu->copy(MenuItems, this);
//Setup the console output
pTextDisplay = new ReadOnlyEditor(0, 28, w(), h()-28*4);
//Setup the status bar
pStatus = new Fl_Output(0, h()-28, w(), 28);
pStatus->box(FL_PLASTIC_UP_BOX);
//Setup the console input
Fl_Box *pB = new Fl_Box(0, h()-28*3, 80, 28, "Input Text:");
pB->box(FL_PLASTIC_UP_BOX);
pInput = new Fl_Input(80, h()-28*3, w()-160, 28);
pEnter = new Fl_Return_Button(w()-80, h()-28*3, 80, 28, "Enter");
pEnter->callback(EnterCB, this);
//Setup the ascii input
pB = new Fl_Box(0, h()-28*2, 80, 28, "ASCII Code:");
pB->box(FL_PLASTIC_UP_BOX);
pInputAscii = new Fl_Input(80, h()-28*2, w()-160, 28);
pEnterAscii = new Fl_Button(w()-80, h()-28*2, 80, 28, "Enter Code");
pEnterAscii->callback(EnterAsciiCB, this);
}
end();
resizable(pTextDisplay);
callback(HideCB, this);
// fWaitForInput = false;
}
ConsoleWindow::~ConsoleWindow()
{
hide();
TheSimulatorWindow.pConsoleWindow = NULL;
Fl::first_window()->show();
}
void ConsoleWindow::HelpCB(Fl_Widget *pW, void *pV)
{
TheMainWindow.Help(AshIDEIndex, "ConsoleWindow");
}
bool ConsoleWindow::SetTitle()
{
string sTitle = "Console";
// if(fWaitForInput)
// sTitle += " (Waiting For User Input)";
label(sTitle.c_str());
return true;
}
bool ConsoleWindow::Show()
{
show();
SetTitle();
return true;
}
bool ConsoleWindow::Hide()
{
// if(fWaitForInput)
// {
// fl_message("The console cannot close while it is waiting for user input.");
// return false;
// }
// else
// {
hide();
Fl::first_window()->show();
return true;
// }
}
int ConsoleWindow::handle(int Event)
{
int RetVal = Fl_Double_Window::handle(Event);
switch(Event)
{
case FL_FOCUS:
//*NOTE: Some bug/feature in FLTK causes the Status widget to always get
//the focus instead of the text editor
pInput->take_focus();
}
return RetVal;
}
bool ConsoleWindow::Enter()
{
const char *sInput = pInput->value();
pInput->value("");
if(sInput[0] == 0)
{ //string is blank
if(sConsoleInput.size() == 0 || sConsoleInput[sConsoleInput.size()-1] != 4)
sConsoleInput += 4; //EOI
return false;
}
if(sConsoleInput.size() && sConsoleInput[sConsoleInput.size()-1] == 4)
sConsoleInput.resize(sConsoleInput.size()-1); //remove EOI
sConsoleInput += sInput;
for(unsigned int i = 0; i < strlen(sInput); i++)
TheArch(Interrupt(TheArch(KeyboardInterruptVector)));
return true;
}
bool ConsoleWindow::EnterAscii()
{
const char *sInput = pInputAscii->value();
string sAscii;
AsmConvertLC3Line(sInput, sAscii, LocationVector(), MainWindow::SimMessageCallBack);
list<Token *> TokenList;
LC3ISA::Lexer TheLexer(TokenList, MainWindow::MessageCallBack);
TheLexer.LexLine(LocationVector(), sAscii);
if(TokenList.empty() || (*TokenList.begin())->TokenType != TInteger)
{
//get rid of the used tokens
for(list<Token *>::iterator TokenIter = TokenList.begin(); TokenIter != TokenList.end(); TokenIter++)
delete *TokenIter;
fl_alert("You must provide a number for the ASCII code.");
return false;
}
if(((IntegerToken *)(*TokenList.begin()))->Integer >= 256)
{
//get rid of the used tokens
for(list<Token *>::iterator TokenIter = TokenList.begin(); TokenIter != TokenList.end(); TokenIter++)
delete *TokenIter;
fl_alert("ASCII code must be less than 256.");
return false;
}
if(sConsoleInput.size() && sConsoleInput[sConsoleInput.size()-1] == 4)
sConsoleInput.resize(sConsoleInput.size()-1); //remove EOI
sConsoleInput += (char)((IntegerToken *)(*TokenList.begin()))->Integer;
TheArch(Interrupt(TheArch(KeyboardInterruptVector)));
for(list<Token *>::iterator TokenIter = TokenList.begin(); TokenIter != TokenList.end(); TokenIter++)
delete *TokenIter;
return true;
}
bool ConsoleWindow::SimReadConsole(string &sBuffer, unsigned int CharsToRead, unsigned int &CharsRead)
{
if(!shown())
Show();
CharsRead = 0;
sBuffer = "";
fflush(NULL);
if(CharsToRead == 0)
return true;
/*Blocking read code
while(TheProject.fSimulating)
{
if(sConsoleInput.size() > 0)
{
fWaitForInput = false;
if(sConsoleInput[0] == 4)
{
return true;
}
else if(sConsoleInput[0] == -1)
{
return false;
}
else
{
sBuffer += sConsoleInput[0];
sConsoleInput = sConsoleInput.substr(1);
CharsRead++;
if(CharsRead == CharsToRead)
return true;
}
}
else
{
fWaitForInput = true;
Fl::check();
}
}
fWaitForInput = false;
*/
//Non-blocking read code
for(unsigned int i = 0; i < CharsToRead; i++)
{
if(sConsoleInput.size() == 0)
return true;
if(sConsoleInput[0] == 4)
return true;
else if(sConsoleInput[0] == -1)
return false;
sBuffer += sConsoleInput[0];
sConsoleInput = sConsoleInput.substr(1);
CharsRead++;
}
return true;
}
bool ConsoleWindow::SimWriteConsole(const string &sBuffer, unsigned int CharsToWrite, unsigned int &CharsWritten)
{
if(!shown())
Show();
CharsWritten = 0;
char Buff[2] = {0,0};
for(unsigned int i = 0; i < sBuffer.size(); i++)
{
if(i == CharsToWrite)
break;
Buff[0] = sBuffer[i];
pTextDisplay->pTextBuffer->append(Buff);
CharsWritten++;
}
pTextDisplay->scroll((unsigned int)-1 >> 1, 0);
return true;
}
} //namespace AshIDE