forked from Wolf-Tungsten/wolf-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer_producer_dynamic.cpp
More file actions
127 lines (110 loc) · 2.95 KB
/
Copy pathconsumer_producer_dynamic.cpp
File metadata and controls
127 lines (110 loc) · 2.95 KB
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
#include <iostream>
#include "wolf_sim.h"
const int TOTAL_PAYLOAD = 20;
const int PROCESS_DELAY = 3;
class Producer : public wolf_sim::Module {
public:
/* 端口定义 */
Output(payloadValid, bool);
Output(payload, int);
Input(payloadReady, bool);
private:
/* 内部状态定义 */
Reg(nextPayload, int);
/* 初始化函数 */
void init() {
nextPayload = 0;
payloadValid = false;
}
/* 状态更新函数 */
void updateStateOutput() {
if (payloadValid && payloadReady) {
logger() << "Producing payload " << payload << std::endl;
nextPayload = nextPayload + 1;
}
payloadValid = true;
payload = nextPayload;
}
};
class Consumer : public wolf_sim::Module {
public:
Consumer(int maxPayload, int processDelay)
: maxPayload(maxPayload), processDelay(processDelay) {};
/* 端口定义 */
Input(payloadValid, bool);
Input(payload, int);
Output(payloadReady, bool);
private:
int maxPayload;
int processDelay;
/* 内部状态定义 */
Reg(busyCount, int);
/* 初始化函数 */
void init() {
busyCount = 0;
payloadReady = true;
}
/* 状态和输出更新函数 */
void updateStateOutput() {
if (payloadValid && payloadReady) {
busyCount = processDelay - 1;
if (payload == maxPayload) {
terminate();
return;
}
logger() << "Consuming payload " << payload << std::endl;
}
if (busyCount > 0) {
busyCount = busyCount - 1;
payloadReady = false;
} else {
payloadReady = true;
}
}
};
class Top : public wolf_sim::Module {
public:
/* 端口定义 */
Input(anUselessInput, int);
Output(anUselessOutput, int);
private:
/* 子模块定义 */
ChildModule(producer, Producer);
std::shared_ptr<Consumer> consumer;
void construct() {
consumer = std::make_shared<Consumer>(TOTAL_PAYLOAD, PROCESS_DELAY);
addChildModule(consumer);
consumer->setModuleLabel("dynamic consumer");
}
/* 子模块输入更新函数 */
void updateChildInput() {
consumer->payloadValid = producer->payloadValid;
consumer->payload = producer->payload;
producer->payloadReady = consumer->payloadReady;
}
/* 状态和输出更新函数 */
void updateStateOutput() { anUselessOutput = anUselessInput + 47; }
};
int main() {
/* 建议总是使用智能指针保存仿真模型 */
std::shared_ptr<Top> top = std::make_shared<Top>();
// tick once;
top->anUselessInput = 19780823;
top->tick();
std::cout << "anUselessOutput: " << top->anUselessOutput << " @ "
<< top->whatTime() << std::endl;
// tick 10 times;
top->tick(10);
// tick to termination;
while (!top->terminated()) {
// 可在此处设置输入
top->tick();
// 可在此处读取输出
}
std::cout << ">> reset the model and start again <<" << std::endl;
// create (reset) a new model
top = std::make_shared<Top>();
// and then another way to tick to termination
top->tickToTermination();
return 0;
}