-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cpp
More file actions
414 lines (346 loc) · 14.1 KB
/
process.cpp
File metadata and controls
414 lines (346 loc) · 14.1 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "process.h"
#include "commands/PrintCommand.h"
#include "commands/DeclareCommand.h"
#include "commands/AddCommand.h"
#include "commands/SubtractCommand.h"
#include "commands/SleepCommand.h"
#include "commands/ForCommand.h"
#include "commands/ReadCommand.h"
#include "commands/WriteCommand.h"
#include <limits>
#include <random>
#include <map>
#include <iostream>
#include <thread>
Process::Process(int pid, std::string name, ScreenConsole* consolePtr)
: pid(pid), name(name), commandCounter(0), consolePtr(consolePtr), state(READY)
{}
//constructor for memory
Process::Process(int pid, std::string name, ScreenConsole* consolePtr, size_t memoryRequired, size_t frameSize)
: pid(pid), name(name), commandCounter(0), consolePtr(consolePtr),
state(READY), memoryRequired(memoryRequired), pageSize(frameSize),
numPages((memoryRequired+frameSize-1)/ frameSize)
{}
void Process::addCommand(ICommand::CommandType type) {
if (type == ICommand::PRINT) {
std::string msg = "Hello world from " + name + "!";
CommandList.push_back(std::make_shared<PrintCommand>(pid, msg));
}
else if (type == ICommand::DECLARE) {
CommandList.push_back(std::make_shared<DeclareCommand>(pid, "x", 0));
}
else if (type == ICommand::ADD) {
CommandList.push_back(std::make_shared<AddCommand>(pid, "var1", "var2", "var3"));
}
else if (type == ICommand::SUBTRACT) {
CommandList.push_back(std::make_shared<SubtractCommand>(pid, "var1", "var2", "var3"));
}
/*else {
std::string msg = "Invalid command type encountered.\n";
}*/
}
void Process::addCommand(ICommand::CommandType type, uint8_t ticks) {
if (type == ICommand::SLEEP) {
CommandList.push_back(std::make_shared<SleepCommand>(pid, ticks));
} else {
addCommand(type);
}
}
void Process::addCommand(const std::vector<std::shared_ptr<ICommand>>& instructions, int repeats) {
CommandList.push_back(std::make_shared<ForCommand>(pid, instructions, repeats));
}
void Process::executeCurrentCommand(int coreId) const {
if (commandCounter < CommandList.size()) {
CommandList[commandCounter]->execute(this->consolePtr, coreId);
}
}
void Process::moveToNextLine() {
if (commandCounter < CommandList.size()) {
++commandCounter;
}
}
bool Process::isFinished() const {
return commandCounter >= CommandList.size();
}
int Process::getRemainingTime() const {
return CommandList.size() - commandCounter;
}
int Process::getCommandCounter() const {
return commandCounter;
}
int Process::getLinesOfCode() const {
return CommandList.size();
}
int Process::getPID() const {
return pid;
}
int Process::getCPUCoreID() const {
return cpuCoreID;
}
Process::ProcessState Process::getState() const {
return state;
}
std::string Process::getName() const {
return name;
}
void Process::setFinishTime(const std::string& time) {
timeFinished = time;
}
void Process::setState(Process::ProcessState newState) {
this->state = newState;
}
std::string Process::getFinishTime() const {
return timeFinished;
}
size_t Process::getNumPages(){
return numPages;
}
// allow nested loops with 3 depth
std::vector<std::shared_ptr<ICommand>> generateLoopBody(int pid, int depth, int maxDepth, std::mt19937& rng) {
std::uniform_int_distribution<int> val(0, 255);
std::uniform_int_distribution<int> bodySizeDist(2, 4);
std::uniform_int_distribution<int> cmdChoiceDist(0, 4); // include 4 = possible nested loop
std::uniform_int_distribution<int> loopCountDist(1, 5);
std::vector<std::shared_ptr<ICommand>> body;
int bodySize = bodySizeDist(rng);
for (int i = 0; i < bodySize; ++i) {
int choice = cmdChoiceDist(rng);
if (choice == 0) {
body.push_back(std::make_shared<PrintCommand>(pid, "Loop print from nested level " + std::to_string(depth)));
} else if (choice == 1) {
std::string var = "loopVar" + std::to_string(i);
body.push_back(std::make_shared<DeclareCommand>(pid, var, val(rng)));
} else if (choice == 2) {
body.push_back(AddCommand::generateRandom(pid));
} else if (choice == 3) {
body.push_back(SubtractCommand::generateRandom(pid));
} else if (choice == 4 && depth < maxDepth) {
int loopCount = loopCountDist(rng);
auto nestedBody = generateLoopBody(pid, depth + 1, maxDepth, rng);
body.push_back(std::make_shared<ForCommand>(pid, nestedBody, loopCount));
}
}
return body;
}
void Process::generateTestCaseAlternatingCommands(int count) {
// Always declare x = 0 first
CommandList.push_back(std::make_shared<DeclareCommand>(pid, "x", 0));
static std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> randVal(1, 10);
for (int i = 0; i < count; ++i) {
if (i % 2 == 0) {
std::string msg = "Value from: x";
CommandList.push_back(std::make_shared<PrintCommand>(pid, msg));
} else {
int randomVal = randVal(rng);
// Generate ADD(x, x, randomValue) as string value
CommandList.push_back(std::make_shared<AddCommand>(pid, "x", "x", std::to_string(randomVal)));
}
}
}
void Process::createCommandsFromInstructions(const std::string& instructions, int min, int max) {
std::stringstream ss(instructions);
std::string instruction;
static std::mt19937 rng(std::random_device{}());
static std::uniform_int_distribution<int> dist(0, 5); // for the various commands
static std::uniform_int_distribution<int> val(0, 255);
static std::uniform_int_distribution<int> sleepTicks(1, 10);
while (std::getline(ss, instruction, ';')) {
instruction.erase(0, instruction.find_first_not_of(" \t"));
instruction.erase(instruction.find_last_not_of(" \t") + 1);
if (instruction.empty()) continue;
std::stringstream tokenStream(instruction);
std::string keyword;
tokenStream >> keyword;
std::transform(keyword.begin(), keyword.end(), keyword.begin(), ::toupper);
if (keyword == "PRINT")
{
std::string message;
getline(tokenStream, message); // the rest of the line
CommandList.push_back(std::make_shared<PrintCommand>(pid, message));
}
else if (keyword == "DECLARE")
{
std::string varName;
int value;
tokenStream >> varName >> value;
CommandList.push_back(std::make_shared<DeclareCommand>(pid, varName, value));
}
else if (keyword == "ADD")
{
std::string target, op1, op2;
tokenStream >> target >> op1 >> op2;
CommandList.push_back(std::make_shared<AddCommand>(pid, target, op1, op2));
}
else if (keyword == "SUBTRACT")
{
std::string target, op1, op2;
tokenStream >> target >> op1 >> op2;
CommandList.push_back(std::make_shared<SubtractCommand>(pid, target, op1, op2));
}
else if (keyword == "SLEEP")
{
uint8_t ticks;
tokenStream >> ticks;
CommandList.push_back(std::make_shared<SleepCommand>(pid, ticks));
}
else if (keyword == "FOR")
{
std::string bodyInstructions;
getline(tokenStream, bodyInstructions);
std::vector<std::shared_ptr<ICommand>> loopBody = generateLoopBody(pid, 1, 3, rng); // max depth 3
int loopCount = std::uniform_int_distribution<int>(1, 5)(rng);
CommandList.push_back(std::make_shared<ForCommand>(pid, loopBody, loopCount));
}
else if (keyword == "READ")
{
std::string varName, addressStr;
tokenStream >> varName >> addressStr;
uint32_t memAddress = std::stoul(addressStr, nullptr, 0); // supports 0x format
//CommandList.push_back(std::make_shared<ReadCommand>(pid, varName, memAddress, this));
}
else if (keyword == "WRITE")
{
std::string addressStr, varName;
tokenStream >> addressStr >> varName;
uint32_t memAddress = std::stoul(addressStr, nullptr, 0); // supports hex like 0x500
uint16_t value = this->getVariable(varName); // fetch from symbol table
//CommandList.push_back(std::make_shared<WriteCommand>(pid, varName, memAddress, value, this));
}
else
{
std::cout << "Invalid command in instructions: " << instruction << "\n";
}
}
}
void Process::test_generateRandomCommands(int min, int max) {
static std::mt19937 rng(std::random_device{}());
static std::uniform_int_distribution<int> instructions(min, max);
static std::uniform_int_distribution<int> dist(0, 5); // for the various commands
static std::uniform_int_distribution<int> val(0, 255);
static std::uniform_int_distribution<int> sleepTicks(1, 10);
for (int i = 0; i < instructions(rng); ++i) {
int choice = dist(rng); //random commands
if (choice == 0) {
CommandList.push_back(std::make_shared<PrintCommand>(pid, "Hello world from " + name + "!"));
} else if (choice == 1) {
std::string var = "var" + std::to_string(i);
CommandList.push_back(std::make_shared<DeclareCommand>(pid, var, val(rng)));
} else if (choice == 2) {
CommandList.push_back(AddCommand::generateRandom(pid));
} else if(choice == 3){
CommandList.push_back(SubtractCommand::generateRandom(pid));
} else if (choice ==4){
int ticks = sleepTicks(rng);
CommandList.push_back(std::make_shared<SleepCommand>(pid, ticks));
} else {
int loopCount = std::uniform_int_distribution<int>(1, 5)(rng);
auto loopBody = generateLoopBody(pid, 1, 3, rng); // maxDepth = 3
CommandList.push_back(std::make_shared<ForCommand>(pid, loopBody, loopCount));
/*
old version without nested
/Generate a small random body of commands for the ForCommand loop
int loopCount = std::uniform_int_distribution<int>(1, 5)(rng);
Create a vector for the body commands
std::vector<std::shared_ptr<ICommand>> loopBody;
For demo, let's add 2-4 commands inside the loop
int bodySize = std::uniform_int_distribution<int>(2, 4)(rng);
for (int j = 0; j < bodySize; ++j) {
int cmdChoice = std::uniform_int_distribution<int>(0, 3)(rng);
if (cmdChoice == 0) {
loopBody.push_back(std::make_shared<PrintCommand>(pid, "Loop print from " + name));
} else if (cmdChoice == 1) {
std::string var = "loopVar" + std::to_string(j);
loopBody.push_back(std::make_shared<DeclareCommand>(pid, var, val(rng)));
} else if (cmdChoice == 2) {
loopBody.push_back(AddCommand::generateRandom(pid));
} else if (cmdChoice == 3) {
loopBody.push_back(SubtractCommand::generateRandom(pid));
}
}
CommandList.push_back(std::make_shared<ForCommand>(pid, loopBody, loopCount));
*/
}
}
}
void Process::screenProcess(int delay){
//launch thread to run its own
std::thread executeProcess(&Process::runScreenProcess, this, delay);
executeProcess.detach();
}
void Process::runScreenProcess(int delay){
this->state = RUNNING;
while (this->state != FINISHED){
this->executeCurrentCommand(0);
this->moveToNextLine();
if (this->isFinished())
this->state = FINISHED;
}
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
size_t Process::getMemoryRequired() const {
return memoryRequired;
}
void Process::setMemoryPtr(void* ptr) {
memoryPtr = ptr;
}
void* Process::getMemoryPtr() const {
return memoryPtr;
}
bool Process::declareVariable(const std::string& name, uint16_t value) {
// Check if variable already declared
if (symbolTable.find(name) != symbolTable.end()) {
if (consolePtr) consolePtr->addLog("Variable '" + name + "' already declared. Ignoring redeclaration.");
return false;
}
// Check if symbol table is full
if (symbolTable.size() >= MAX_SYMBOL_TABLE_SIZE) {
if (consolePtr) consolePtr->addLog("Symbol table full. Ignoring declaration of variable: " + name);
return false;
}
// Clamp value between 0 and UINT16_MAX
value = std::min(value, static_cast<uint16_t>(std::numeric_limits<uint16_t>::max()));
symbolTable[name] = value;
if (consolePtr)
consolePtr->addLog("Declared variable '" + name + "' = " + std::to_string(value));
return true;
}
bool Process::hasVariable(const std::string& name) const {
return symbolTable.find(name) != symbolTable.end();
}
uint16_t Process::getVariable(const std::string& name) const {
auto it = symbolTable.find(name);
if (it != symbolTable.end()) {
return it->second;
}
if (consolePtr)
consolePtr->addLog("Variable '" + name + "' not found!");
return 0;
}
void Process::setVariable(const std::string& name, uint16_t value) {
if (!hasVariable(name)) {
if (symbolTable.size() >= MAX_SYMBOL_TABLE_SIZE) {
if (consolePtr)
consolePtr->addLog("Cannot declare new variable '" + name + "' — symbol table full.");
return;
}
// Automatically declare variable if not present
symbolTable[name] = 0;
if (consolePtr)
consolePtr->addLog("Declared new variable '" + name + "'");
}
value = std::min(value, static_cast<uint16_t>(std::numeric_limits<uint16_t>::max()));
symbolTable[name] = value;
if (consolePtr)
consolePtr->addLog("Updated variable '" + name + "' = " + std::to_string(value));
}
void Process::printSymbolTable() const {
std::cout << "Symbol Table for Process " << pid << " (" << name << "):\n";
if (symbolTable.empty()) {
std::cout << " <empty>\n";
return;
}
for (const auto& [varName, value] : symbolTable) {
std::cout << " " << varName << " = " << value << "\n";
}
}