|
| 1 | +/* Exercise 25.10 */ |
| 2 | + |
| 3 | +#include<iostream> |
| 4 | +#include<sstream> |
| 5 | +#include<iomanip> |
| 6 | +#include"C25_Exercise_25.10.h" |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +inline void error(string s) { throw runtime_error(s); } |
| 11 | +inline void error(const string& s, const string& s2) { error(s + s2); } |
| 12 | +inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); } |
| 13 | +inline void keep_window_open() { char ch; cin >> ch; } |
| 14 | + |
| 15 | +int main() |
| 16 | +{ |
| 17 | + enum Action { |
| 18 | + EXIT = -1, PRINTACTIONLIST, |
| 19 | + CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10 |
| 20 | + }; |
| 21 | + const string actionList = "\tList of actions:\n" |
| 22 | + " (1) PPN (bitfield), (2) PPN (unsigned int)\n" |
| 23 | + " (-1) Exit, (0) Print the list of actions\n"; |
| 24 | + cout << actionList; |
| 25 | + int action; |
| 26 | + bool cond{ true }; |
| 27 | + while (cond) try { |
| 28 | + cout << "\nPlease enter the action: "; |
| 29 | + if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); } |
| 30 | + char ch; |
| 31 | + cin.get(ch); |
| 32 | + switch (action) { |
| 33 | + case CASE1: { |
| 34 | + cout << endl; |
| 35 | + Page::PPN ppn01{ 66789, 7, true, true, true, false }; |
| 36 | + cout << sp_2 << "PPN ppn01:" << endl; |
| 37 | + cout << ppn01 << endl; |
| 38 | + |
| 39 | + ppn01.SetPFN(155); |
| 40 | + ppn01.CCA = 2; |
| 41 | + ppn01.nonreachable = false; |
| 42 | + ppn01.dirty = false; |
| 43 | + ppn01.valid = false; |
| 44 | + ppn01.global = true; |
| 45 | + |
| 46 | + cout << sp_2 << "PPN ppn01 after change:" << endl; |
| 47 | + cout << ppn01 << endl; |
| 48 | + |
| 49 | + cout << vsp_2; |
| 50 | + break; |
| 51 | + } |
| 52 | + case CASE2: { |
| 53 | + cout << endl; |
| 54 | + Page::PPNint ppn02; |
| 55 | + // initialization |
| 56 | + Page::SetPFN(ppn02, 66789); |
| 57 | + Page::SetCCA(ppn02, 7); |
| 58 | + Page::SetNonreachable(ppn02, false); |
| 59 | + Page::SetDirty(ppn02, true); |
| 60 | + Page::SetValid(ppn02, true); |
| 61 | + Page::SetGlobal(ppn02, true); |
| 62 | + |
| 63 | + cout << sp_2 << "PPNint ppn02:" << endl; |
| 64 | + cout << ppn02 << endl; |
| 65 | + |
| 66 | + // changing |
| 67 | + Page::SetPFN(ppn02, 155); |
| 68 | + Page::SetCCA(ppn02, 2); |
| 69 | + Page::SetNonreachable(ppn02, true); |
| 70 | + Page::SetDirty(ppn02, true); |
| 71 | + Page::SetValid(ppn02, false); |
| 72 | + Page::SetGlobal(ppn02, false); |
| 73 | + |
| 74 | + cout << sp_2 << "PPNint ppn02 after change:" << endl; |
| 75 | + cout << ppn02 << endl; |
| 76 | + |
| 77 | + // changing |
| 78 | + Page::SetPFN(ppn02, 44444); |
| 79 | + Page::SetCCA(ppn02, 6); |
| 80 | + Page::SetNonreachable(ppn02, false); |
| 81 | + Page::SetDirty(ppn02, false); |
| 82 | + Page::SetValid(ppn02, true); |
| 83 | + Page::SetGlobal(ppn02, true); |
| 84 | + |
| 85 | + cout << sp_2 << "PPNint ppn02 after change:" << endl; |
| 86 | + cout << ppn02 << endl; |
| 87 | + |
| 88 | + cout << vsp_2; |
| 89 | + break; |
| 90 | + } |
| 91 | + case PRINTACTIONLIST: |
| 92 | + cout << actionList; |
| 93 | + break; |
| 94 | + case EXIT: |
| 95 | + cond = false; |
| 96 | + break; |
| 97 | + default: |
| 98 | + error("Error. Incorrect action number"); |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + catch (runtime_error& e) { |
| 103 | + cerr << e.what() << endl; |
| 104 | + } |
| 105 | + catch (...) { |
| 106 | + cerr << "Error. Exception\n"; |
| 107 | + return 1; |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +void ClearInput(istream& is) |
| 113 | +{ |
| 114 | + is.clear(); |
| 115 | + is.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 116 | +} |
| 117 | + |
| 118 | +Page::PPN::PPN(unsigned int PFN, unsigned CCA, bool nonreachable, bool dirty, bool valid, bool global) |
| 119 | + : nonreachable{ nonreachable }, dirty{ dirty }, valid{ valid }, global{ global } |
| 120 | +{ |
| 121 | + if (PFN > PFNmax) throw InvalidValue{}; |
| 122 | + if (CCA > CCAmax) throw InvalidValue{}; |
| 123 | + this->PFN = PFN; |
| 124 | + this->CCA = CCA; |
| 125 | +} |
| 126 | + |
| 127 | +void Page::PPN::SetPFN(unsigned int PFN) |
| 128 | +{ |
| 129 | + if (PFN > PFNmax) throw InvalidValue{}; |
| 130 | + this->PFN = PFN; |
| 131 | +} |
| 132 | + |
| 133 | +void Page::PPN::SetCCA(unsigned int CCA) |
| 134 | +{ |
| 135 | + if (CCA > CCAmax) throw InvalidValue{}; |
| 136 | + this->CCA = CCA; |
| 137 | +} |
| 138 | + |
| 139 | +ostream& Page::operator<<(ostream& os, const PPN& ppn) |
| 140 | +{ |
| 141 | + cout << sp_4 << "PFN: " << ppn.PFN << endl; |
| 142 | + cout << sp_4 << "CCA: " << ppn.CCA << endl; |
| 143 | + cout << sp_4 << "nonreachable: " << boolalpha << ppn.nonreachable << endl; |
| 144 | + cout << sp_4 << "dirty: " << ppn.dirty << endl; |
| 145 | + cout << sp_4 << "valid: " << ppn.valid << endl; |
| 146 | + cout << sp_4 << "global: " << ppn.global << noboolalpha << endl; |
| 147 | + return os; |
| 148 | +} |
| 149 | + |
| 150 | +void Page::SetPFN(PPNint& ppn, unsigned int PFN) |
| 151 | +{ |
| 152 | + if (PFN > PFNmax) throw InvalidValue{}; |
| 153 | + constexpr int shift = 10; |
| 154 | + // erase old CCA |
| 155 | + unsigned int bits = PFNmax << shift; |
| 156 | + bits = ~bits; |
| 157 | + ppn.v &= bits; |
| 158 | + // assign new CCA |
| 159 | + PFN <<= shift; |
| 160 | + ppn.v |= PFN; |
| 161 | +} |
| 162 | + |
| 163 | +void Page::SetCCA(PPNint& ppn, unsigned int CCA) |
| 164 | +{ |
| 165 | + if (CCA > CCAmax) throw InvalidValue{}; |
| 166 | + constexpr int shift = 4; |
| 167 | + // erase old CCA |
| 168 | + unsigned int bits = CCAmax << shift; |
| 169 | + bits = ~bits; |
| 170 | + ppn.v &= bits; |
| 171 | + // assign new CCA |
| 172 | + CCA <<= shift; |
| 173 | + ppn.v |= CCA; |
| 174 | +} |
| 175 | + |
| 176 | +void Page::SetNonreachable(PPNint& ppn, bool nonreachable) |
| 177 | +{ |
| 178 | + unsigned int bit = 0x8; |
| 179 | + if (nonreachable) ppn.v |= bit; |
| 180 | + else ppn.v &= ~bit; |
| 181 | +} |
| 182 | + |
| 183 | +void Page::SetDirty(PPNint& ppn, bool dirty) |
| 184 | +{ |
| 185 | + unsigned int bit = 0x4; |
| 186 | + if (dirty) ppn.v |= bit; |
| 187 | + else ppn.v &= ~bit; |
| 188 | +} |
| 189 | + |
| 190 | +void Page::SetValid(PPNint& ppn, bool valid) |
| 191 | +{ |
| 192 | + unsigned int bit = 0x2; |
| 193 | + if (valid) ppn.v |= bit; |
| 194 | + else ppn.v &= ~bit; |
| 195 | +} |
| 196 | + |
| 197 | +void Page::SetGlobal(PPNint& ppn, bool global) |
| 198 | +{ |
| 199 | + unsigned int bit = 0x1; |
| 200 | + if (global) ppn.v |= bit; |
| 201 | + else ppn.v &= ~bit; |
| 202 | +} |
| 203 | + |
| 204 | +ostream& Page::operator<<(ostream& os, const PPNint& ppn) |
| 205 | +{ |
| 206 | + constexpr int shiftPFN = 10; |
| 207 | + cout << sp_4 << "PFN: " << (ppn.v >> shiftPFN) << endl; |
| 208 | + constexpr int shiftCCA = 4; |
| 209 | + cout << sp_4 << "CCA: " << ((ppn.v >> shiftCCA) & CCAmax) << endl; |
| 210 | + constexpr int shiftNonreachable = 3; |
| 211 | + cout << sp_4 << "nonreachable: " << boolalpha << static_cast<bool>(ppn.v & 0x8) << endl; |
| 212 | + constexpr int shiftDirty = 2; |
| 213 | + cout << sp_4 << "dirty: " << static_cast<bool>(ppn.v & 0x4) << endl; |
| 214 | + constexpr int shiftValid = 1; |
| 215 | + cout << sp_4 << "valid: " << static_cast<bool>(ppn.v & 0x2) << endl; |
| 216 | + cout << sp_4 << "global: " << static_cast<bool>(ppn.v & 0x1) << noboolalpha << endl; |
| 217 | + return os; |
| 218 | +} |
0 commit comments