1 | #ifndef CACHE_H |
---|
2 | #define CACHE_H |
---|
3 | |
---|
4 | #include "Common/include/BitManipulation.h" |
---|
5 | #include "Common/include/Debug.h" |
---|
6 | #include "Common/include/Log2.h" |
---|
7 | #include "Common/include/Test.h" |
---|
8 | #include "Behavioural/include/Constants.h" |
---|
9 | #include "Behavioural/include/Types.h" |
---|
10 | #include <list> |
---|
11 | |
---|
12 | //================================================================={Cache_t} |
---|
13 | |
---|
14 | typedef int32_t cycle_t; |
---|
15 | |
---|
16 | typedef struct |
---|
17 | { |
---|
18 | cycle_t _cycle ; |
---|
19 | morpheo::behavioural::Tcontext_t _context_id; |
---|
20 | morpheo::behavioural::Tpacket_t _packet_id ; |
---|
21 | morpheo::behavioural::Tdcache_data_t _rdata ; |
---|
22 | morpheo::behavioural::Tdcache_error_t _error ; |
---|
23 | } cache_rsp_t; |
---|
24 | |
---|
25 | class Cache_t |
---|
26 | { |
---|
27 | private : const uint32_t _miss_rate; |
---|
28 | private : const uint32_t _miss_penality; |
---|
29 | private : list<cache_rsp_t> _list_respons; |
---|
30 | |
---|
31 | public : Cache_t (uint32_t miss_rate, uint32_t miss_penality): |
---|
32 | _miss_rate (miss_rate ), |
---|
33 | _miss_penality (miss_penality) |
---|
34 | { |
---|
35 | // srand(0); |
---|
36 | |
---|
37 | if (miss_rate > 100) |
---|
38 | throw morpheo::ErrorMorpheo(_("Miss_rate can be higher than 100")); |
---|
39 | } |
---|
40 | |
---|
41 | public : ~Cache_t (void) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | public : void push (morpheo::behavioural::Tcontext_t context_id, |
---|
46 | morpheo::behavioural::Tpacket_t packet_id , |
---|
47 | morpheo::behavioural::Tdcache_data_t rdata , |
---|
48 | morpheo::behavioural::Tdcache_error_t error ) |
---|
49 | { |
---|
50 | cycle_t cycle = ((static_cast<uint32_t>(rand())%100)<_miss_rate)?_miss_penality:1; |
---|
51 | |
---|
52 | cache_rsp_t rsp; |
---|
53 | |
---|
54 | rsp._cycle = cycle ; |
---|
55 | rsp._context_id = context_id; |
---|
56 | rsp._packet_id = packet_id ; |
---|
57 | rsp._rdata = rdata ; |
---|
58 | rsp._error = error ; |
---|
59 | |
---|
60 | // find good place |
---|
61 | list<cache_rsp_t>::iterator i; |
---|
62 | for(i = _list_respons.begin(); (i != _list_respons.end()) and (i->_cycle<=cycle); i++); |
---|
63 | |
---|
64 | _list_respons.insert(i,rsp); |
---|
65 | } |
---|
66 | |
---|
67 | public : void pop (void) |
---|
68 | { |
---|
69 | _list_respons.pop_front(); |
---|
70 | } |
---|
71 | |
---|
72 | public : cache_rsp_t front (void) |
---|
73 | { |
---|
74 | return _list_respons.front(); |
---|
75 | } |
---|
76 | |
---|
77 | public : bool have_rsp (void) |
---|
78 | { |
---|
79 | return (not _list_respons.empty()) and (_list_respons.front()._cycle <= 0); |
---|
80 | } |
---|
81 | |
---|
82 | public : void end_cycle (void) |
---|
83 | { |
---|
84 | for(list<cache_rsp_t>::iterator i = _list_respons.begin(); i != _list_respons.end(); i++) |
---|
85 | { |
---|
86 | i->_cycle --; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | public : void print (void) |
---|
91 | { |
---|
92 | for(list<cache_rsp_t>::iterator i = _list_respons.begin(); i != _list_respons.end(); i++) |
---|
93 | { |
---|
94 | std::cout << "{" << i->_cycle << "}\t" |
---|
95 | << i->_context_id << " - " |
---|
96 | << i->_packet_id << " - " |
---|
97 | << i->_rdata << " - " |
---|
98 | << i->_error << std::endl; |
---|
99 | |
---|
100 | } |
---|
101 | } |
---|
102 | }; |
---|
103 | |
---|
104 | inline void test_Cache_t (void) |
---|
105 | { |
---|
106 | |
---|
107 | Cache_t * cache = new Cache_t (30,12); |
---|
108 | |
---|
109 | uint32_t cpt = 0; |
---|
110 | for (uint32_t i=0; i<10; i++) |
---|
111 | { |
---|
112 | for (uint32_t j=0; j<5; j++) |
---|
113 | cache->push(0,cpt++,0,0); |
---|
114 | |
---|
115 | for (uint32_t j=0; j<3; j++) |
---|
116 | if (cache->have_rsp()) |
---|
117 | { |
---|
118 | std::cout << "pop : " << cache->front()._packet_id << std::endl; |
---|
119 | cache->pop(); |
---|
120 | } |
---|
121 | cache->print(); |
---|
122 | cache->end_cycle(); |
---|
123 | } |
---|
124 | |
---|
125 | delete cache; |
---|
126 | } |
---|
127 | |
---|
128 | #endif |
---|