From 382cf44a3ae2fd371913f6a95d2ee6213249d6c5 Mon Sep 17 00:00:00 2001 From: Dylan Jager-Kujawa Date: Tue, 12 Apr 2016 13:39:09 -0500 Subject: [PATCH] Colored output --- machine.cpp | 38 +++++++++++++++++++++++--------------- machine.hpp | 14 ++++++++++---- os.cpp | 25 +++++++++++++++++++------ os.hpp | 33 ++++++--------------------------- utils.cpp | 29 +++++++++++++++-------------- utils.hpp | 15 ++++++++++++--- 6 files changed, 85 insertions(+), 69 deletions(-) diff --git a/machine.cpp b/machine.cpp index 4596d2c..9a730ca 100644 --- a/machine.cpp +++ b/machine.cpp @@ -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); @@ -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++; } @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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; } @@ -231,7 +235,9 @@ bool ADR() { return true; } else { #ifdef DEBUG + cerr << red; cerr << "!! OVERFLOW IN ADR()" << endl; + cerr << normal; #endif return false; } @@ -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; } @@ -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 @@ -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 diff --git a/machine.hpp b/machine.hpp index 4cb7a65..15d1023 100644 --- a/machine.hpp +++ b/machine.hpp @@ -9,13 +9,19 @@ #ifndef MACHINE #define MACHINE -#include -#include -#include -#include +#include // string +#include // cout, cerr +#include // boolalpha, setw, setfill, dec, hex, endl +#include // 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 diff --git a/os.cpp b/os.cpp index 556bccb..8447f09 100644 --- a/os.cpp +++ b/os.cpp @@ -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()) { @@ -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 @@ -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; @@ -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 diff --git a/os.hpp b/os.hpp index 95a6bb3..9b89f76 100644 --- a/os.hpp +++ b/os.hpp @@ -6,16 +6,11 @@ * Spring 2016 */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // string +#include // queue +#include // cout, cerr +#include // boolalpha, setw, setfill, hex, dec, endl +#include // sstream #include "machine.hpp" #include "utils.hpp" using namespace std; @@ -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; diff --git a/utils.cpp b/utils.cpp index 4ab3917..b87d6d7 100644 --- a/utils.cpp +++ b/utils.cpp @@ -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++) { @@ -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; @@ -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; @@ -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 @@ -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"; @@ -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 = ""; @@ -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++) { @@ -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); @@ -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(); diff --git a/utils.hpp b/utils.hpp index a9b16b0..90a21af 100644 --- a/utils.hpp +++ b/utils.hpp @@ -9,11 +9,10 @@ #ifndef UTILS #define UTILS -#include // cout -#include // setw, left, right +#include // cout, cerr +#include // setw, setfill, left, right, dec, endl #include // srand, rand #include // time -#include // begin, end #include // stringstream #include // out_of_range using namespace std; @@ -40,6 +39,7 @@ class PageTable // Unique to this PageTable; contains the actual page-frame mappings int _pageTable[NUM_FRAMES]; + // Iterators not fully supported in C++98; this assists with std::find() struct ary { static int* begin(int (&arr)[64]) {return arr;} static int* end(int (&arr)[64]) {return begin(arr) + NUM_FRAMES;} @@ -55,11 +55,20 @@ class PageTable void print(); }; +// Return a string consisting of n of the specified character (default: space) string padding(int); string padding(int, char); + +// Return a horizontal rule of # characters (std console width = 80chars) string horizontalrule(); + +// Divide the string into 80 character lines with textbox borders left and right string textboxline(string); + +// Return a textbox of width = 80chars, wrapped with # symbols string textbox(string); + +// Return a thicker textbox (i.e.: extra horizontalrule above and below) string titlebox(string); #endif