-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
227 lines (198 loc) · 6.43 KB
/
Copy pathmain.cpp
File metadata and controls
227 lines (198 loc) · 6.43 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* _ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
. ' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
.............................................
佛祖保佑 永无BUG
佛曰:
写字楼里写字间,写字间里程序员;
程序人员写程序,又拿程序换酒钱。
酒醒只在网上坐,酒醉还来网下眠;
酒醉酒醒日复日,网上网下年复年。
但愿老死电脑间,不愿鞠躬老板前;
奔驰宝马贵者趣,公交自行程序员。
别人笑我忒疯癫,我笑自己命太贱;
不见满街漂亮妹,哪个归得程序员?
*/
/**
* main.cpp
*
* Copyright (C) 2024 SCTY
*
* Description:
*
* Revision History:
*
* 2024-12-14 Created By YangLing (yl.tienon@gmail.com)
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "basic_type.cpp"
#include "bsb.hpp"
#include "fsb.hpp"
#include "memory.hpp"
#include "cache.hpp"
#include "processor.hpp"
#include "system.hpp"
#if defined(HAVE_BSB_TEST)
#include "test/bsb_test.hpp"
#endif
#if defined(HAVE_FSB_TEST)
#include "test/fsb_test.hpp"
#endif
#if defined(HAVE_STORE_BUFFER)
#include "mob.hpp"
#endif // HAVE_STORE_BUFFER
static void print_version(void)
{
fprintf(stderr, "Version: %s\n", "1.0.1");
}
static void print_usage(void)
{
fprintf(stderr, "mos_run [-0 infile | -1 infile | -2 infile | -3 infile]\n");
fprintf(stderr, " -0 Set the instruction file to be processed by processor 0, Default file is a.out.\n");
fprintf(stderr, " -1 Set the instruction file to be processed by processor 1, Default file is a.out.\n");
fprintf(stderr, " -2 Set the instruction file to be processed by processor 2, Default file is a.out.\n");
fprintf(stderr, " -3 Set the instruction file to be processed by processor 3, Default file is a.out.\n");
fprintf(stderr, " -h Print the help info and quit.\n");
fprintf(stderr, " -v Print the version and quit.\n");
}
std::binary_semaphore system::sem_a { 0 };
std::binary_semaphore system::sem_b { 0 };
std::binary_semaphore system::sem_c { 0 };
std::binary_semaphore system::sem_d { 0 };
std::binary_semaphore system::sem_e { 0 };
std::binary_semaphore system::sem_f { 0 };
int main(int argc, char * argv[])
{
int c;
const char * optstr = "0:1:2:3:t:vh";
const unsigned int nr_cpus = 4;
const char * p0 = "a.out";
const char * p1 = "a.out";
const char * p2 = "a.out";
const char * p3 = "a.out";
const char * tf = NULL;
while ((c = getopt(argc, argv, optstr)) > 0)
{
switch (c)
{
case '0':
p0 = optarg;
break;
case '1':
p1 = optarg;
break;
case '2':
p2 = optarg;
break;
case '3':
p3 = optarg;
break;
case 't':
tf = optarg;
break;
case 'v':
print_version();
return 0;
case 'h':
print_usage();
return EINVAL;
}
}
if (tf)
{
#if defined(HAVE_BSB_TEST)
if (0 == strcmp("bsb", tf))
bsb_tester::bsb_tester_main();
#endif // HAVE_BSB_TEST
#if defined(HAVE_FSB_TEST)
if (0 == strcmp("fsb", tf))
fsb_tester::fsb_tester_main();
#endif // HAVE_FSB_TEST
}
else
{
bsb bsb_obj_0;
bsb bsb_obj_1;
bsb bsb_obj_2;
bsb bsb_obj_3;
#if defined(HAVE_STORE_BUFFER)
mob mob_obj_0(&bsb_obj_0, 4); // SB 最大 4 个条目
mob mob_obj_1(&bsb_obj_1, 4); // SB 最大 4 个条目
mob mob_obj_2(&bsb_obj_2, 4); // SB 最大 4 个条目
mob mob_obj_3(&bsb_obj_3, 4); // SB 最大 4 个条目
#endif // HAVE_STORE_BUFFER
fsb fsb_obj_X;
memory mem_obj(nr_cpus, &fsb_obj_X);
cache cache_obj_0(0, &bsb_obj_0, &fsb_obj_X);
cache cache_obj_1(1, &bsb_obj_1, &fsb_obj_X);
cache cache_obj_2(2, &bsb_obj_2, &fsb_obj_X);
cache cache_obj_3(3, &bsb_obj_3, &fsb_obj_X);
#if defined(HAVE_STORE_BUFFER)
processor cpu_obj_0(0, &mob_obj_0, p0);
processor cpu_obj_1(1, &mob_obj_1, p1);
processor cpu_obj_2(2, &mob_obj_2, p2);
processor cpu_obj_3(3, &mob_obj_3, p3);
#else
processor cpu_obj_0(0, &bsb_obj_0, p0);
processor cpu_obj_1(1, &bsb_obj_1, p1);
processor cpu_obj_2(2, &bsb_obj_2, p2);
processor cpu_obj_3(3, &bsb_obj_3, p3);
#endif // HAVE_STORE_BUFFER
// 启动(内存工作线程)
std::thread t_memory(memory::snoop_thread, &mem_obj);
// 启动(缓存snoop线程: 处理 fsb 事务)
std::thread t_cache_00(cache::snoop_thread, &cache_obj_0);
std::thread t_cache_10(cache::snoop_thread, &cache_obj_1);
std::thread t_cache_20(cache::snoop_thread, &cache_obj_2);
std::thread t_cache_30(cache::snoop_thread, &cache_obj_3);
// 启动(缓存processor侧的线程: 处理 bsb 事务)
std::thread t_cache_01(cache::cache_thread, &cache_obj_0);
std::thread t_cache_11(cache::cache_thread, &cache_obj_1);
std::thread t_cache_21(cache::cache_thread, &cache_obj_2);
std::thread t_cache_31(cache::cache_thread, &cache_obj_3);
#if defined(HAVE_STORE_BUFFER)
// 启动(mob侧的线程: 处理 mob 事务)
std::thread t_mob_0(mob::mob_thread, &mob_obj_0);
std::thread t_mob_1(mob::mob_thread, &mob_obj_1);
std::thread t_mob_2(mob::mob_thread, &mob_obj_2);
std::thread t_mob_3(mob::mob_thread, &mob_obj_3);
#endif // HAVE_STORE_BUFFER
// 启动 processor 指令处理线程
std::thread t_cpu_0(processor::processor_thread, &cpu_obj_0);
std::thread t_cpu_1(processor::processor_thread, &cpu_obj_1);
std::thread t_cpu_2(processor::processor_thread, &cpu_obj_2);
std::thread t_cpu_3(processor::processor_thread, &cpu_obj_3);
t_cpu_0.join();
t_cpu_1.join();
t_cpu_2.join();
t_cpu_3.join();
t_cache_01.join();
t_cache_11.join();
t_cache_21.join();
t_cache_31.join();
t_cache_00.join();
t_cache_10.join();
t_cache_20.join();
t_cache_30.join();
t_memory.join();
}
return 0;
}