1 | #ifndef CACHE_H |
---|
2 | #define CACHE_H |
---|
3 | |
---|
4 | #include <stdint.h> |
---|
5 | #include <stdarg.h> |
---|
6 | #include <iostream> |
---|
7 | #include "cache_multilevel.h" |
---|
8 | #include "cache_onelevel.h" |
---|
9 | #include "type_req_cache.h" |
---|
10 | #include "type_rsp_cache.h" |
---|
11 | |
---|
12 | using namespace std; |
---|
13 | using namespace hierarchy_memory::cache::cache_multilevel; |
---|
14 | |
---|
15 | |
---|
16 | namespace hierarchy_memory { |
---|
17 | namespace cache { |
---|
18 | typedef enum |
---|
19 | { |
---|
20 | INSTRUCTION_CACHE , |
---|
21 | DATA_CACHE |
---|
22 | } cache_t; |
---|
23 | |
---|
24 | class param_t |
---|
25 | { |
---|
26 | public : const char * name ; |
---|
27 | public : uint32_t nb_entity ; |
---|
28 | public : cache_multilevel::param_t * param_icache_dedicated; // 1 each entity |
---|
29 | public : cache_multilevel::param_t * param_dcache_dedicated; // 1 each entity |
---|
30 | public : cache_multilevel::param_t param_cache_shared ; // 1 to all entity and I/D is unify |
---|
31 | |
---|
32 | public : param_t () {}; |
---|
33 | |
---|
34 | public : param_t (const char * name , |
---|
35 | uint32_t nb_entity , |
---|
36 | cache_multilevel::param_t * param_icache_dedicated, |
---|
37 | cache_multilevel::param_t * param_dcache_dedicated, |
---|
38 | cache_multilevel::param_t param_cache_shared |
---|
39 | ) : |
---|
40 | name (name) |
---|
41 | { |
---|
42 | this->name = name ; |
---|
43 | this->nb_entity = nb_entity ; |
---|
44 | this->param_icache_dedicated = param_icache_dedicated ; |
---|
45 | this->param_dcache_dedicated = param_dcache_dedicated ; |
---|
46 | this->param_cache_shared = param_cache_shared ; |
---|
47 | } |
---|
48 | |
---|
49 | friend ostream& operator<< (ostream& output_stream, const param_t & x) |
---|
50 | { |
---|
51 | output_stream << "<" << x.name << ">" << endl |
---|
52 | << " * nb_entity : " << x.nb_entity << endl; |
---|
53 | |
---|
54 | for (uint32_t it = 0; it < x.nb_entity; it ++) |
---|
55 | output_stream << x.param_icache_dedicated [it] << endl; |
---|
56 | for (uint32_t it = 0; it < x.nb_entity; it ++) |
---|
57 | output_stream << x.param_dcache_dedicated [it] << endl; |
---|
58 | |
---|
59 | output_stream << x.param_cache_shared << endl; |
---|
60 | |
---|
61 | return output_stream; |
---|
62 | } |
---|
63 | |
---|
64 | };//end param_t |
---|
65 | |
---|
66 | |
---|
67 | class Cache |
---|
68 | { |
---|
69 | private : char * name ; |
---|
70 | protected: const uint32_t nb_entity ; |
---|
71 | protected: uint32_t nb_iport ; |
---|
72 | protected: uint32_t nb_dport ; |
---|
73 | |
---|
74 | protected: Cache_Multilevel ** icache_dedicated; |
---|
75 | protected: Cache_Multilevel ** dcache_dedicated; |
---|
76 | protected: Cache_Multilevel * cache_shared ; |
---|
77 | |
---|
78 | //*****[ constructor ]***** |
---|
79 | public : Cache (param_t param): |
---|
80 | nb_entity (param.nb_entity) |
---|
81 | { |
---|
82 | uint32_t size_name = strlen(param.name)+1; |
---|
83 | name = new char [size_name]; |
---|
84 | strncpy(name,param.name,size_name); |
---|
85 | |
---|
86 | cache_shared = new Cache_Multilevel (param.param_cache_shared); |
---|
87 | icache_dedicated = new Cache_Multilevel * [nb_entity]; |
---|
88 | dcache_dedicated = new Cache_Multilevel * [nb_entity]; |
---|
89 | |
---|
90 | nb_iport = 0; |
---|
91 | nb_dport = 0; |
---|
92 | for (uint32_t it = 0; it < nb_entity ; it++) |
---|
93 | { |
---|
94 | icache_dedicated [it] = new Cache_Multilevel (param.param_icache_dedicated[it]); |
---|
95 | dcache_dedicated [it] = new Cache_Multilevel (param.param_dcache_dedicated[it]); |
---|
96 | nb_iport += param.param_icache_dedicated[it].nb_port; |
---|
97 | nb_dport += param.param_dcache_dedicated[it].nb_port; |
---|
98 | } |
---|
99 | |
---|
100 | information(); |
---|
101 | }; |
---|
102 | |
---|
103 | //*****[ destructor ]***** |
---|
104 | public : ~Cache () |
---|
105 | { |
---|
106 | for (uint32_t it = 0; it < nb_entity ; it++) |
---|
107 | { |
---|
108 | delete icache_dedicated [it]; |
---|
109 | delete dcache_dedicated [it]; |
---|
110 | } |
---|
111 | delete cache_shared ; |
---|
112 | }; |
---|
113 | |
---|
114 | //*****[ reset ]***** |
---|
115 | void reset () |
---|
116 | { |
---|
117 | cache_shared -> reset(); |
---|
118 | // Reset buffer of respons |
---|
119 | for (uint32_t it = 0; it < nb_entity; it++) |
---|
120 | { |
---|
121 | icache_dedicated [it] ->reset(); |
---|
122 | dcache_dedicated [it] ->reset(); |
---|
123 | } |
---|
124 | }//end reset |
---|
125 | |
---|
126 | //*****[ transition ]***** |
---|
127 | void transition() |
---|
128 | { |
---|
129 | cache_shared -> transition(); |
---|
130 | // Transition buffer of respons |
---|
131 | for (uint32_t it = 0; it < nb_entity; it++) |
---|
132 | { |
---|
133 | icache_dedicated [it] ->transition(); |
---|
134 | dcache_dedicated [it] ->transition(); |
---|
135 | } |
---|
136 | }//end transition |
---|
137 | |
---|
138 | //*****[ range_port ]***** |
---|
139 | public : uint32_t range_port (cache_t type_cache, |
---|
140 | uint32_t num_entity) |
---|
141 | { |
---|
142 | uint32_t nb_port_dedicated = 0; |
---|
143 | |
---|
144 | if (type_cache == INSTRUCTION_CACHE) |
---|
145 | for (uint32_t it = 1; it < num_entity; it++ ) |
---|
146 | nb_port_dedicated += icache_dedicated [it]->nb_port; |
---|
147 | else |
---|
148 | { |
---|
149 | nb_port_dedicated = nb_iport; |
---|
150 | for (uint32_t it = 1; it < num_entity; it++) |
---|
151 | nb_port_dedicated += dcache_dedicated [it]->nb_port; |
---|
152 | } |
---|
153 | return nb_port_dedicated; |
---|
154 | } |
---|
155 | |
---|
156 | //*****[ latence ]***** |
---|
157 | // Return the time to have the data |
---|
158 | // latence is call on the same port in a single cycle, only the last access is save |
---|
159 | // { NOTE } : type_cache = 0 : icache, 1 : dcache |
---|
160 | public : uint32_t latence (cache_t type_cache, |
---|
161 | uint32_t num_entity, |
---|
162 | uint32_t num_port , |
---|
163 | uint32_t address , |
---|
164 | uint32_t trdid , |
---|
165 | type_req_cache_t type , |
---|
166 | direction_req_cache_t dir ) |
---|
167 | { |
---|
168 | if (num_entity >= nb_entity) |
---|
169 | { |
---|
170 | cerr << "<Cache.latence> {ERROR} num_entity ("<< num_entity << ") can be >= nb_entity (" << nb_entity << ")" << endl; |
---|
171 | exit (1); |
---|
172 | } |
---|
173 | |
---|
174 | Cache_Multilevel * cache; |
---|
175 | if (type_cache == INSTRUCTION_CACHE) |
---|
176 | cache = icache_dedicated [num_entity]; |
---|
177 | else |
---|
178 | cache = dcache_dedicated [num_entity]; |
---|
179 | |
---|
180 | if (num_port >= cache->nb_port) |
---|
181 | { |
---|
182 | cerr << "<Cache.latence> {ERROR} num_port ("<< num_port << ") can be >= nb_port (" << cache->nb_port << ")" << endl; |
---|
183 | exit (1); |
---|
184 | } |
---|
185 | // Make a access with this level "dedicated" |
---|
186 | |
---|
187 | access_t access_dedicated = cache->access(num_port,address,trdid,type,dir); |
---|
188 | |
---|
189 | // cout << "access_dedicated : " << access_dedicated << endl; |
---|
190 | |
---|
191 | if (access_dedicated.hit == MISS) |
---|
192 | { |
---|
193 | // Make a access with this level "shared" |
---|
194 | access_t access_shared = cache_shared->access(range_port (type_cache,num_entity)+num_port,address,trdid,type,dir); |
---|
195 | // cout << "access_shared : " << access_shared << endl; |
---|
196 | |
---|
197 | cache_shared->update_access (access_shared); |
---|
198 | |
---|
199 | access_dedicated.last_nb_level = cache->nb_level; // Update all cache |
---|
200 | access_dedicated.latence += access_shared.latence; |
---|
201 | |
---|
202 | // cout << "access_dedicated (after) : " << access_dedicated << endl; |
---|
203 | } |
---|
204 | |
---|
205 | cache->update_access (access_dedicated); |
---|
206 | return access_dedicated.latence; |
---|
207 | } |
---|
208 | |
---|
209 | //*****[ information ]***** |
---|
210 | public : void information (void) |
---|
211 | { |
---|
212 | cout << "<" << name << ">" << endl; |
---|
213 | |
---|
214 | for (uint32_t it = 0; it < nb_entity; it ++) |
---|
215 | icache_dedicated [it]->information(); |
---|
216 | for (uint32_t it = 0; it < nb_entity; it ++) |
---|
217 | dcache_dedicated [it]->information(); |
---|
218 | |
---|
219 | cache_shared->information(); |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | friend ostream& operator<< (ostream& output_stream, const Cache & x) |
---|
224 | { |
---|
225 | output_stream << "<" << x.name << ">" << endl; |
---|
226 | |
---|
227 | for (uint32_t it = 0; it < x.nb_entity; it ++) |
---|
228 | output_stream << *x.icache_dedicated [it] << endl; |
---|
229 | for (uint32_t it = 0; it < x.nb_entity; it ++) |
---|
230 | output_stream << *x.dcache_dedicated [it] << endl; |
---|
231 | |
---|
232 | output_stream << *x.cache_shared << endl; |
---|
233 | |
---|
234 | return output_stream; |
---|
235 | } |
---|
236 | |
---|
237 | };//end Cache |
---|
238 | };}; |
---|
239 | #endif //!CACHE_H |
---|