-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework2.cpp
150 lines (94 loc) · 2.2 KB
/
homework2.cpp
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
#include <systemc.h>
class OSAPI: public virtual sc_interface
{
public:
virtual void start() = 0;
virtual void yield1() = 0;
virtual void yield2() = 0;
};
class OS1: public virtual sc_channel, public OSAPI
{
int current;
sc_event e1, e2;
public:
OS1(const sc_module_name name): sc_channel(name) { }
void start() { current = 1; }
void yield1() { e2.notify(); wait(e1); current = 1; }
void yield2() { e1.notify(); wait(e2); current = 2; }
};
class OS2: public virtual sc_channel, public OSAPI
{
int current;
sc_event e1, e2;
public:
OS2(const sc_module_name name): sc_channel(name) { }
void start() { current = 1; }
void yield1() { e2.notify(SC_ZERO_TIME); wait(e1); current = 1; }
void yield2() { e1.notify(SC_ZERO_TIME); wait(e2); current = 2; }
};
SC_MODULE(A)
{
sc_port<OSAPI> os;
sc_in<bool> e;
SC_CTOR(A) { SC_THREAD(run); }
void run(void) {
cout << "A1: " << sc_time_stamp() << endl;
wait(10, SC_NS);
os->yield1();
cout << "A2: " << sc_time_stamp() << endl;
wait(1, SC_NS);
while(!e) {
wait(e->default_event());
}
wait(5, SC_NS);
os->yield1();
cout << "A3: " << sc_time_stamp() << endl;
wait(1, SC_NS);
os->yield1();
cout << "A4: " << sc_time_stamp() << endl;
wait(1, SC_NS);
}
};
SC_MODULE(B)
{
sc_port<OSAPI> os;
sc_out<bool> e;
SC_CTOR(B) { SC_THREAD(run); }
void run(void) {
cout << "B1: " << sc_time_stamp() << endl;
wait(10, SC_NS);
os->yield2();
cout << "B2: " << sc_time_stamp() << endl;
wait(5, SC_NS);
e = true;
wait(5, SC_NS);
os->yield2();
cout << "B3: " << sc_time_stamp() << endl;
wait(10, SC_NS);
os->yield2();
cout << "B4: " << sc_time_stamp() << endl;
wait(10, SC_NS);
}
};
SC_MODULE(Top) {
sc_signal<bool> e;
OS1 os;
// OS2 os;
A a;
B b;
SC_CTOR(Top): e(false), os("OS"), a("A"), b("B") {
a.os(os);
a.e(e);
b.os(os);
b.e(e);
}
// automatically called by SystemC at the start of simulation (sc_start)
void start_of_simulation() { os.start(); }
};
int sc_main(int, char*[])
{
Top top("Top");
sc_start();
cout << "Done: " << sc_time_stamp() << endl;
return 0;
}