Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c26499

Browse files
committed
Exercise 25.5; Exercise 25.6; Exercise 25.7; Exercise 25.8; Exercise 25.9; Exercise 25.10
1 parent ee70ff5 commit 4c26499

13 files changed

+749
-0
lines changed

‎Chapter_25/C25_Exercise_25.10.cpp

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
}

‎Chapter_25/C25_Exercise_25.10.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Exercise 25.10 */
2+
3+
using namespace std;
4+
5+
const char* sp_2 = " ";
6+
const char* sp_4 = " ";
7+
const char* sp_6 = " ";
8+
const char* sp_8 = " ";
9+
const char* vsp_2 = "\n\n";
10+
const char* vsp_3 = "\n\n\n";
11+
const char* vsp_4 = "\n\n\n\n";
12+
13+
void ClearInput(istream& is);
14+
15+
namespace Page {
16+
constexpr unsigned int CalculateMax(int numberOfBits)
17+
{
18+
if (numberOfBits <= 0) return 0;
19+
unsigned int max = 1;
20+
for (int i = 1; i < numberOfBits; ++i) {
21+
max <<= 1;
22+
max |= 1;
23+
}
24+
return max;
25+
}
26+
27+
constexpr int PFNbits = 22;
28+
constexpr int CCAbits = 3;
29+
constexpr int boolBits = 1;
30+
31+
constexpr unsigned int PFNmax = CalculateMax(PFNbits);
32+
constexpr unsigned int CCAmax = CalculateMax(CCAbits);
33+
34+
class InvalidValue{};
35+
36+
struct PPN { // R6000 Physical Page Number
37+
unsigned int PFN : PFNbits; // Page Frame Number
38+
int : 3; // unused
39+
unsigned int CCA : CCAbits; // Cache Coherency Algorithm
40+
bool nonreachable : boolBits;
41+
bool dirty : boolBits;
42+
bool valid : boolBits;
43+
bool global : boolBits;
44+
PPN(unsigned int PFN, unsigned CCA, bool nonreachable, bool dirty, bool valid, bool global);
45+
void SetPFN(unsigned int PFN);
46+
void SetCCA(unsigned int CCA);
47+
};
48+
49+
ostream& operator<<(ostream& os, const PPN& ppn);
50+
51+
struct PPNint {
52+
unsigned int v;
53+
PPNint() : v{ 0 } {}
54+
};
55+
56+
void SetPFN(PPNint& ppn, unsigned int PFN);
57+
void SetCCA(PPNint& ppn, unsigned int CCA);
58+
void SetNonreachable(PPNint& ppn, bool nonreachable);
59+
void SetDirty(PPNint& ppn, bool dirty);
60+
void SetValid(PPNint& ppn, bool valid);
61+
void SetGlobal(PPNint& ppn, bool global);
62+
63+
ostream& operator<<(ostream& os, const PPNint& ppn);
64+
}

‎Chapter_25/C25_Exercise_25.5.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Exercise 25.5 */
2+
3+
#include<iostream>
4+
#include<sstream>
5+
#include<iomanip>
6+
#include"C25_Exercise_25.5.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) Infinite loop\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+
int x = 0;
36+
while (true) {
37+
++x;
38+
}
39+
cout << "x = " << x << endl;
40+
cout << vsp_2;
41+
break;
42+
}
43+
case PRINTACTIONLIST:
44+
cout << actionList;
45+
break;
46+
case EXIT:
47+
cond = false;
48+
break;
49+
default:
50+
error("Error. Incorrect action number");
51+
break;
52+
}
53+
}
54+
catch (runtime_error& e) {
55+
cerr << e.what() << endl;
56+
}
57+
catch (...) {
58+
cerr << "Error. Exception\n";
59+
return 1;
60+
}
61+
return 0;
62+
}
63+
64+
void ClearInput(istream& is)
65+
{
66+
is.clear();
67+
is.ignore(numeric_limits<streamsize>::max(), '\n');
68+
}

‎Chapter_25/C25_Exercise_25.5.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Exercise 25.5 */
2+
3+
using namespace std;
4+
5+
const char* sp_2 = " ";
6+
const char* sp_4 = " ";
7+
const char* sp_6 = " ";
8+
const char* sp_8 = " ";
9+
const char* vsp_2 = "\n\n";
10+
const char* vsp_3 = "\n\n\n";
11+
const char* vsp_4 = "\n\n\n\n";
12+
13+
void ClearInput(istream& is);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /