Skip to content

Commit

Permalink
Colored output
Browse files Browse the repository at this point in the history
  • Loading branch information
Kujawadl committed Apr 12, 2016
1 parent 5443163 commit 382cf44
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 69 deletions.
38 changes: 23 additions & 15 deletions machine.cpp
Expand Up @@ -55,7 +55,6 @@ bool interpreter() {
bool success = true;
//While no error flag and no timer interrupt
while (success && timer_interrupt < QUANTUM) {
usleep(1500000); // Sleep 1.5 seconds to clearly demonstrate quantum
machine.IR = main_memory[MMU(machine.PC)];
machine.PC++; // Increment Program Counter
unsigned short int op = getOpcode(machine.IR);
Expand All @@ -75,9 +74,10 @@ bool interpreter() {
case 12: success = JLT(); break;
case 13: success = CMP(); break;
case 14: success = CLR(); break;
case 15: return HLT(); break; //Quit early on HLT
case 15: return HLT(); break; //Quit early on HLT
default: success = false; break;
}
usleep(1000000); // Sleep 1 second to allow easier instruction tracing
sysclock++;
timer_interrupt++;
}
Expand All @@ -91,7 +91,7 @@ unsigned short int getOpcode(unsigned short int num) {
unsigned short int ret = (num & 61440)>>12;

#ifdef DEBUG_VERBOSE
cerr << "In getOpCode(). IR: " << machine.IR << ", Opcode: " << ret << endl;
cout << "In getOpCode(). IR: " << machine.IR << ", Opcode: " << ret << endl;
#endif

return ret;
Expand All @@ -103,7 +103,7 @@ unsigned short int getAddrMode(unsigned short int num) {
unsigned short int ret = (num & 2048)>>11;

#ifdef DEBUG_VERBOSE
cerr << "In getAddrMode(). IR: " << machine.IR << ", Addr: " << ret << endl;
cout << "In getAddrMode(). IR: " << machine.IR << ", Addr: " << ret << endl;
#endif

return ret;
Expand All @@ -115,7 +115,7 @@ unsigned short int getRegCode(unsigned short int num) {
unsigned short int ret = (num & 1792)>>8;

#ifdef DEBUG_VERBOSE
cerr << "In getRegCode(). IR: " << machine.IR << ", Regcode: " << ret << endl;
cout << "In getRegCode(). IR: " << machine.IR << ", Regcode: " << ret << endl;
#endif

return ret;
Expand All @@ -127,7 +127,7 @@ unsigned short int getOperand(unsigned short int num) {
unsigned short int ret = (num & 255);

#ifdef DEBUG_VERBOSE
cerr << "In getOperand(). IR: " << machine.IR << ", Operand: " << ret << endl;
cout << "In getOperand(). IR: " << machine.IR << ", Operand: " << ret << endl;
#endif

return ret;
Expand Down Expand Up @@ -178,7 +178,9 @@ bool ADD() {
return true;
} else {
#ifdef DEBUG
cout << "!! OVERFLOW IN ADD()" << endl;
cerr << red;
cerr << "!! OVERFLOW IN ADD()" << endl;
cerr << normal;
#endif
return false;
}
Expand All @@ -204,7 +206,9 @@ bool SUB() {
return true;
} else {
#ifdef DEBUG
cout << "!! OVERFLOW IN SUB()" << endl;
cerr << red;
cerr << "!! OVERFLOW IN SUB()" << endl;
cerr << normal;
#endif
return false;
}
Expand All @@ -231,7 +235,9 @@ bool ADR() {
return true;
} else {
#ifdef DEBUG
cerr << red;
cerr << "!! OVERFLOW IN ADR()" << endl;
cerr << normal;
#endif
return false;
}
Expand All @@ -258,7 +264,9 @@ bool SUR() {
return true;
} else {
#ifdef DEBUG
cout << "!! OVERFLOW IN SUR()" << endl;
cerr << red;
cerr << "!! OVERFLOW IN SUR()" << endl;
cerr << normal;
#endif
return false;
}
Expand Down Expand Up @@ -447,7 +455,7 @@ unsigned short int* getRegister() {
unsigned short int regCode = getRegCode(machine.IR); // Returns 0-3

#ifdef DEBUG_VERBOSE
cerr << "In getRegister(). IR: " << machine.IR << \
cout << "In getRegister(). IR: " << machine.IR << \
", Regcode: %d" << regCode << endl;
#endif

Expand All @@ -473,14 +481,14 @@ void printDebug(string op) {
unsigned short int IR = machine.IR; // TODO: Convert this to hex or binary
unsigned short int PC = machine.PC;
unsigned short int CR = machine.CR;
cerr << "Instruction with opcode " << op << " finished executing." << endl;
cerr << "\t0x" << oldPC << ": " << op << " " << addr \
cout << "Instruction with opcode " << op << " finished executing." << endl;
cout << "\t0x" << oldPC << ": " << op << " " << addr \
<< " r" << reg << " " << operand << endl;
#ifdef DEBUG_VERBOSE
cerr << "\tDumping Registers:" << endl;
cerr << "\trA: " << rA << ", r1: " << r1 << ", r2: " \
cout << "\tDumping Registers:" << endl;
cout << "\trA: " << rA << ", r1: " << r1 << ", r2: " \
<< r2 << ", r3: " << r3 << endl;
cerr << "\tIR: " << IR << ", PC: " << PC << ", CR: " << CR << endl;
cout << "\tIR: " << IR << ", PC: " << PC << ", CR: " << CR << endl;
#endif
}
#endif
14 changes: 10 additions & 4 deletions machine.hpp
Expand Up @@ -9,13 +9,19 @@
#ifndef MACHINE
#define MACHINE

#include <string>
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <string> // string
#include <iostream> // cout, cerr
#include <iomanip> // boolalpha, setw, setfill, dec, hex, endl
#include <unistd.h> // usleep
#include "utils.hpp"
using namespace std;

#define normal "\033[0m"
#define red "\033[31m"
#define green "\033[32m"
#define blue "\033[34m"


// Debugging flags
#define DEBUG
#define DEBUG_VERBOSE
Expand Down
25 changes: 19 additions & 6 deletions os.cpp
Expand Up @@ -157,11 +157,13 @@ void scheduler() {
}
// Just a quick print of the queue for Verification
#ifdef DEBUG_VERBOSE
cout << textbox("Dumping scheduler queues");
cout << "RQ1: " << qtos(RQ1) << endl;
cout << "RQ2: " << qtos(RQ2) << endl;
cout << "SQ1: " << qtos(SQ1) << endl;
cout << "SQ2: " << qtos(SQ2) << endl;
cout << blue;
cout << textbox("Dumping scheduler queues");
cout << "RQ1: " << qtos(RQ1) << endl;
cout << "RQ2: " << qtos(RQ2) << endl;
cout << "SQ1: " << qtos(SQ1) << endl;
cout << "SQ2: " << qtos(SQ2) << endl;
cout << normal;
#endif
// Search the queues for the next process
if (!RQ1.empty()) {
Expand Down Expand Up @@ -202,6 +204,12 @@ void scheduler() {
if (!success || machine.IR == 61440) {
currentProcess->running = false;
currentProcess->time = 0;

if (!success) {
cerr << red;
cerr << "\nError condition signaled! Killing process...\n";
cerr << normal;
}
}
// Return process to the shadow queue
// If normal user process, return to S2
Expand Down Expand Up @@ -243,7 +251,9 @@ void userinterface() {
cout << "Exited cleanly. Goodbye." << endl;
exit(EXIT_SUCCESS);
} else {
cout << "Force terminated; processes were still running." << endl;
cerr << red;
cerr << "Force terminated; processes were still running." << endl;
cerr << normal;
exit(EXIT_FAILURE);
}
break;
Expand Down Expand Up @@ -407,8 +417,11 @@ void init()
int main(int argc, char** argv)
{
// Print OS startup header
cout << green;
cout << titlebox("CSC 341 OS Lab" + padding(80) + \
"Aaron Baker, Andrew Ballard, and Dylan Jager-Kujawa");
cout << normal;

// Initialization
init();
// Start scheduler
Expand Down
33 changes: 6 additions & 27 deletions os.hpp
Expand Up @@ -6,16 +6,11 @@
* Spring 2016
*/

#include <string>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <string>
#include <cctype>
#include <string> // string
#include <queue> // queue
#include <iostream> // cout, cerr
#include <iomanip> // boolalpha, setw, setfill, hex, dec, endl
#include <sstream> // sstream
#include "machine.hpp"
#include "utils.hpp"
using namespace std;
Expand All @@ -30,23 +25,7 @@ extern FrameTable framesLocked;

// Define the user struct
enum userID {sys, u1, u2};
// userID& operator++ (userID& curr) {
// switch (curr) {
// case sys: curr = u1; break;
// case u1: curr = u2; break;
// case u2: curr = sys; break;
// }
// return curr;
// }
// userID operator++ (userID& curr, int) {
// userID orig = curr;
// switch (curr) {
// case sys: curr = u1; break;
// case u1: curr = u2; break;
// case u2: curr = sys; break;
// }
// return orig;
// }

struct Process {
userID id;
int time;
Expand Down
29 changes: 15 additions & 14 deletions utils.cpp
Expand Up @@ -9,8 +9,7 @@
#include "utils.hpp"

// Case-insensitive string comparison
bool my_strcasecmp(string str1, string str2)
{
bool my_strcasecmp(string str1, string str2) {
int length1 = str1.length();

for (int i = 0; i < length1; i++) {
Expand All @@ -33,8 +32,7 @@ string itos(int i) {
}

// Constructor; takes a FrameTable by reference
PageTable::PageTable(FrameTable& F) : _framesInUse(F)
{
PageTable::PageTable(FrameTable& F) : _framesInUse(F) {
// Initialize page table with -1 (i.e. no associated frame)
for (int i = 0; i < NUM_FRAMES; i++) {
_pageTable[i] = -1;
Expand All @@ -48,8 +46,7 @@ PageTable::PageTable(FrameTable& F) : _framesInUse(F)
frame, assign said free frame to the specified page number entry,
and set the FrameTable entry to indicate the frame is now allocated.
*/
int& PageTable::operator[] (const int index)
{
int& PageTable::operator[] (const int index) {
// If index out of bounds, throw an exception
if (index > 63 || index < 0) {
stringstream ss;
Expand All @@ -61,8 +58,8 @@ int& PageTable::operator[] (const int index)
// If page table contains no frame for specified page
if (_pageTable[index] < 0) {
#ifdef DEBUG_VERBOSE
cerr << "Page #" << index << " has no associated frame." << endl;
cerr << "Searching for next available frame:" << endl;
cout << "Page #" << index << " has no associated frame." << endl;
cout << "Searching for next available frame:" << endl;
#endif

// If no more available frames to allocate, throw an exception
Expand All @@ -78,26 +75,25 @@ int& PageTable::operator[] (const int index)
int i = rand() % 64;
while (_framesInUse[i] == true) {
#ifdef DEBUG_VERBOSE
cerr << "\tFrame #" << i << " is in use..." << endl;
cout << "\tFrame #" << i << " is in use..." << endl;
#endif
i = rand() % 64;
}
#ifdef DEBUG_VERBOSE
cerr << "Frame #" << i << " is available!" << endl;
cout << "Frame #" << i << " is available!" << endl;
#endif
_framesInUse[i] = true; // Set FrameTable entry to indicate frame is in use
_pageTable[index] = i; // Set PageTable entry to refer to the frame in question
}
#ifdef DEBUG_VERBOSE
cerr << "Page #" << index << " was referenced and has Frame #"
cout << "Page #" << index << " was referenced and has Frame #"
<< _pageTable[index] << endl;
#endif
return _pageTable[index];
}

// Creates a string representation of the page table
string PageTable::toString()
{
string PageTable::toString() {
string out = "";
// Header
out += "*" + padding(14, '-') + "*\n";
Expand All @@ -117,10 +113,12 @@ string PageTable::toString()
return out;
}

// Print the string representation of the page table
void PageTable::print() {
cout << PageTable::toString();
}

// Return a string consisting of n of the specified character (default: space)
string padding(int n) { return padding(n, ' '); }
string padding(int n, char c) {
string out = "";
Expand All @@ -130,6 +128,7 @@ string padding(int n, char c) {
return out;
}

// Return a horizontal rule of # characters (std console width = 80chars)
string horizontalrule() {
string out = "";
for (int i = 0; i < 80; i++) {
Expand All @@ -140,6 +139,7 @@ string horizontalrule() {
return out;
}

// Divide the string into 80 character lines with textbox borders left and right
string textboxline(string text) {
// Trim trailing and leading spaces
text = text.erase(text.find_last_not_of(" \t\n\r") + 1);
Expand Down Expand Up @@ -174,16 +174,17 @@ string textboxline(string text) {
return out;
}

// Return a textbox of width = 80chars, wrapped with # symbols
string textbox(string text) {
string out = "\n";
out += horizontalrule();
out += textboxline(text);
out += horizontalrule();
out += "\n";

return out;
}

// Return a thicker textbox (i.e.: extra horizontalrule above and below).
string titlebox(string text) {
string out = "\n";
out += horizontalrule();
Expand Down

0 comments on commit 382cf44

Please sign in to comment.