[71] | 1 | #ifndef MEMORY_H |
---|
| 2 | #define MEMORY_H |
---|
| 3 | |
---|
| 4 | #include <list> |
---|
| 5 | #include "systemc.h" |
---|
| 6 | #include "Common/include/BitManipulation.h" |
---|
| 7 | #include "Common/include/Debug.h" |
---|
| 8 | #include "Common/include/Log2.h" |
---|
| 9 | #include "Common/include/Test.h" |
---|
| 10 | #include "Behavioural/include/Constants.h" |
---|
| 11 | #include "Behavioural/include/Types.h" |
---|
| 12 | |
---|
| 13 | //================================================================{Memory_t} |
---|
| 14 | typedef struct |
---|
| 15 | { |
---|
| 16 | double _cycle ; |
---|
| 17 | uint32_t _context ; |
---|
| 18 | morpheo::behavioural::Tdcache_address_t _address ; |
---|
| 19 | morpheo::behavioural::Tdcache_type_t _type ; |
---|
| 20 | morpheo::behavioural::Tdcache_data_t _data_old; |
---|
| 21 | morpheo::behavioural::Tdcache_data_t _data_new; |
---|
| 22 | } trace_memory_t; |
---|
| 23 | |
---|
| 24 | class Memory_t |
---|
| 25 | { |
---|
| 26 | private : const uint32_t _nb_context; |
---|
| 27 | private : const uint32_t _nb_word ; |
---|
| 28 | private : const uint32_t _size_word ; |
---|
| 29 | private : const uint32_t _shift_addr; |
---|
| 30 | private : const morpheo::behavioural::Tdcache_address_t _mask_addr ; |
---|
| 31 | private : morpheo::behavioural::Tdcache_data_t ** _data ; |
---|
| 32 | |
---|
| 33 | private : std::list<trace_memory_t> _trace_memory; |
---|
| 34 | |
---|
| 35 | public : Memory_t (uint32_t nb_context, |
---|
| 36 | uint32_t nb_word, |
---|
| 37 | uint32_t size_word): |
---|
| 38 | _nb_context (nb_context), |
---|
| 39 | _nb_word (nb_word ), |
---|
| 40 | _size_word (size_word ), |
---|
| 41 | _shift_addr (morpheo::log2(size_word/8)), |
---|
| 42 | _mask_addr (morpheo::gen_mask<morpheo::behavioural::Tdcache_address_t>(_shift_addr)) |
---|
| 43 | { |
---|
| 44 | _data = new morpheo::behavioural::Tdcache_data_t * [nb_context]; |
---|
| 45 | |
---|
| 46 | for (uint32_t i=0; i<nb_context; i++) |
---|
| 47 | { |
---|
| 48 | _data [i] = new morpheo::behavioural::Tdcache_data_t [nb_word]; |
---|
| 49 | |
---|
| 50 | // random data |
---|
| 51 | for (uint32_t j=0; j<nb_word; j++) |
---|
| 52 | _data [i][j] = static_cast<morpheo::behavioural::Tdcache_data_t>(rand()); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | cout << "=====[ Memory's information ]" << endl |
---|
| 56 | << " * _nb_context : " << _nb_context << endl |
---|
| 57 | << " * _nb_word : " << _nb_word << endl |
---|
| 58 | << " * _size_word : " << _size_word << endl |
---|
| 59 | << " * _shift_addr : " << _shift_addr << endl |
---|
| 60 | << " * _mask_addr : " << hex << _mask_addr << dec << endl; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | public : ~Memory_t (void) |
---|
| 64 | { |
---|
| 65 | delete [] _data; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public : morpheo::behavioural::Tdcache_data_t access (uint32_t context, |
---|
| 69 | morpheo::behavioural::Tdcache_address_t address, |
---|
| 70 | morpheo::behavioural::Tdcache_type_t type, |
---|
| 71 | morpheo::behavioural::Tdcache_data_t data) |
---|
| 72 | { |
---|
| 73 | cout << "<Memory::access>" << endl |
---|
| 74 | << " * context : " << context << endl |
---|
| 75 | << " * address : " << hex << address << dec << endl |
---|
| 76 | << " * type : " << type << endl |
---|
| 77 | << " * wdata : " << hex << data << dec << endl; |
---|
| 78 | |
---|
| 79 | morpheo::behavioural::Tdcache_data_t rdata; |
---|
| 80 | |
---|
[72] | 81 | if (type == DCACHE_TYPE_LOAD) |
---|
[71] | 82 | rdata = read (context, address, type); |
---|
| 83 | else |
---|
[72] | 84 | if ((type == DCACHE_TYPE_STORE_8 ) or |
---|
| 85 | (type == DCACHE_TYPE_STORE_16) or |
---|
| 86 | (type == DCACHE_TYPE_STORE_32) or |
---|
| 87 | (type == DCACHE_TYPE_STORE_64) ) |
---|
[71] | 88 | rdata = write (context, address, type, data); |
---|
| 89 | else |
---|
| 90 | rdata = other (context, address, type); |
---|
| 91 | |
---|
| 92 | cout << " * rdata : " << hex << rdata << dec << endl; |
---|
| 93 | |
---|
| 94 | return rdata; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public : morpheo::behavioural::Tdcache_data_t read_lsq (uint32_t context, |
---|
| 98 | morpheo::behavioural::Tdcache_address_t address, |
---|
| 99 | morpheo::behavioural::Tdcache_type_t type) |
---|
| 100 | { |
---|
| 101 | if (context>_nb_context) |
---|
| 102 | TEST_KO("<Memory_t::read> nb context is too high"); |
---|
| 103 | |
---|
| 104 | morpheo::behavioural::Tdcache_address_t LSB = address & _mask_addr; |
---|
| 105 | morpheo::behavioural::Tdcache_address_t MSB = address >> _shift_addr; |
---|
| 106 | |
---|
| 107 | if (MSB >_nb_word) |
---|
| 108 | TEST_KO("<Memory_t::read> address is too high : %8.x", address); |
---|
| 109 | |
---|
| 110 | morpheo::behavioural::Tdcache_data_t data = _data [context][MSB] >> (LSB<<3); |
---|
| 111 | |
---|
| 112 | switch (type) |
---|
| 113 | { |
---|
| 114 | case OPERATION_MEMORY_LOAD_8_Z : |
---|
| 115 | case OPERATION_MEMORY_LOAD_16_Z : |
---|
| 116 | case OPERATION_MEMORY_LOAD_32_Z : |
---|
| 117 | case OPERATION_MEMORY_LOAD_64_Z : |
---|
| 118 | case OPERATION_MEMORY_LOAD_8_S : |
---|
| 119 | case OPERATION_MEMORY_LOAD_16_S : |
---|
| 120 | case OPERATION_MEMORY_LOAD_32_S : |
---|
| 121 | case OPERATION_MEMORY_LOAD_64_S : return morpheo::extend<morpheo::behavioural::Tdcache_data_t>(_size_word,data, is_operation_memory_load_signed(type),memory_size(type)); |
---|
| 122 | default : TEST_KO("<Memory_t::read_lsq> invalide type"); return data; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | private : morpheo::behavioural::Tdcache_data_t read (uint32_t context, |
---|
| 127 | morpheo::behavioural::Tdcache_address_t address, |
---|
| 128 | morpheo::behavioural::Tdcache_type_t type) |
---|
| 129 | { |
---|
| 130 | // Address's Read must be aligned |
---|
| 131 | |
---|
| 132 | if ((address & _mask_addr) != 0) |
---|
| 133 | TEST_KO("<Memory_t::read> Address is not aligned"); |
---|
| 134 | |
---|
| 135 | if (context>_nb_context) |
---|
| 136 | TEST_KO("<Memory_t::read> nb context is too high"); |
---|
| 137 | |
---|
| 138 | morpheo::behavioural::Tdcache_address_t MSB = address >> _shift_addr; |
---|
| 139 | |
---|
| 140 | if (MSB >_nb_word) |
---|
| 141 | TEST_KO("<Memory_t::read> address is too high"); |
---|
| 142 | |
---|
| 143 | morpheo::behavioural::Tdcache_data_t rdata = _data [context][MSB]; |
---|
| 144 | trace_memory_t trace; |
---|
| 145 | |
---|
| 146 | trace._cycle = sc_simulation_time(); |
---|
| 147 | trace._context = context; |
---|
| 148 | trace._address = address; |
---|
| 149 | trace._type = type; |
---|
| 150 | trace._data_old = rdata; |
---|
| 151 | trace._data_new = rdata; |
---|
| 152 | |
---|
| 153 | _trace_memory.push_back(trace); |
---|
| 154 | |
---|
| 155 | return rdata; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | private : morpheo::behavioural::Tdcache_data_t write (uint32_t context, |
---|
| 159 | morpheo::behavioural::Tdcache_address_t address, |
---|
| 160 | morpheo::behavioural::Tdcache_type_t type, |
---|
| 161 | morpheo::behavioural::Tdcache_data_t data) |
---|
| 162 | { |
---|
| 163 | cout << " * write" << endl; |
---|
| 164 | |
---|
| 165 | if (context>_nb_context) |
---|
| 166 | TEST_KO("<Memory_t::read> nb context is too high"); |
---|
| 167 | |
---|
| 168 | if (address>_nb_word) |
---|
| 169 | TEST_KO("<Memory_t::read> address is too high"); |
---|
| 170 | |
---|
| 171 | morpheo::behavioural::Tdcache_address_t LSB = address & _mask_addr; |
---|
| 172 | morpheo::behavioural::Tdcache_address_t MSB = address >> _shift_addr; |
---|
| 173 | |
---|
| 174 | cout << hex |
---|
| 175 | << " * LSB : " << LSB << endl |
---|
| 176 | << " * MSB : " << MSB << endl |
---|
| 177 | << dec; |
---|
| 178 | |
---|
| 179 | morpheo::behavioural::Tdcache_data_t data_old = _data [context][MSB]; |
---|
| 180 | |
---|
| 181 | // exemple to size_word = 32b |
---|
| 182 | // LSB index_min |
---|
| 183 | // 0 0 |
---|
| 184 | // 1 8 |
---|
| 185 | // 2 16 |
---|
| 186 | // 3 24 |
---|
| 187 | |
---|
[72] | 188 | uint32_t memory_size = ((type==DCACHE_TYPE_STORE_16)?MEMORY_SIZE_16: |
---|
| 189 | ((type==DCACHE_TYPE_STORE_32)?MEMORY_SIZE_32: |
---|
| 190 | ((type==DCACHE_TYPE_STORE_64 )?MEMORY_SIZE_64:MEMORY_SIZE_8))); |
---|
[71] | 191 | |
---|
| 192 | uint32_t index_min = LSB<<3; // *8 |
---|
| 193 | uint32_t index_max = index_min+memory_size; |
---|
| 194 | |
---|
| 195 | cout << " * type : " << type << endl |
---|
| 196 | << " * memory_size : " << memory_size << endl |
---|
| 197 | << " * index_min : " << index_min << endl |
---|
| 198 | << " * index_max : " << index_max << endl; |
---|
| 199 | |
---|
| 200 | morpheo::behavioural::Tdcache_data_t data_insert = data<<index_min; // the data is aligned at LSB |
---|
| 201 | |
---|
| 202 | // cout << "read :" << endl |
---|
| 203 | // << " * context : " << context << endl |
---|
| 204 | // << hex |
---|
| 205 | // << " * address : " << address << endl |
---|
| 206 | // << " * LSB : " << LSB << endl |
---|
| 207 | // << " * MSB : " << MSB << endl |
---|
| 208 | // << dec |
---|
| 209 | // << " * index_min : " << index_min << endl |
---|
| 210 | // << " * index_max : " << index_max << endl |
---|
| 211 | // << " * type : " << type << endl; |
---|
| 212 | |
---|
| 213 | if (index_max > _size_word) |
---|
| 214 | TEST_KO("<Memory_t::read> illegal value of index_max : %d, size_word is %d.",index_max,_size_word); |
---|
| 215 | |
---|
| 216 | morpheo::behavioural::Tdcache_data_t data_new = morpheo::insert<morpheo::behavioural::Tdcache_data_t>(data_old, data_insert, index_max-1, index_min); |
---|
| 217 | |
---|
| 218 | _data [context][MSB] = data_new; |
---|
| 219 | |
---|
| 220 | cout << hex |
---|
| 221 | << " * data_old : " << data_old << endl |
---|
| 222 | << " * data_new : " << data_new << endl |
---|
| 223 | << dec; |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | trace_memory_t trace; |
---|
| 227 | |
---|
| 228 | trace._cycle = sc_simulation_time(); |
---|
| 229 | trace._context = context; |
---|
| 230 | trace._address = address; |
---|
| 231 | trace._type = type; |
---|
| 232 | trace._data_old = data_old; |
---|
| 233 | trace._data_new = data_new; |
---|
| 234 | |
---|
| 235 | _trace_memory.push_back(trace); |
---|
| 236 | |
---|
| 237 | return data_old; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | private : morpheo::behavioural::Tdcache_data_t other (uint32_t context, |
---|
| 241 | morpheo::behavioural::Tdcache_address_t address, |
---|
| 242 | morpheo::behavioural::Tdcache_type_t type) |
---|
| 243 | { |
---|
| 244 | trace_memory_t trace; |
---|
| 245 | |
---|
| 246 | trace._cycle = sc_simulation_time(); |
---|
| 247 | trace._context = context; |
---|
| 248 | trace._address = address; |
---|
| 249 | trace._type = type; |
---|
| 250 | trace._data_old = 0; |
---|
| 251 | trace._data_new = 0; |
---|
| 252 | |
---|
| 253 | _trace_memory.push_back(trace); |
---|
| 254 | |
---|
| 255 | return 0; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | public : void trace (void) |
---|
| 259 | { |
---|
| 260 | for (std::list<trace_memory_t>::iterator i=_trace_memory.begin(); i!= _trace_memory.end(); i++) |
---|
| 261 | { |
---|
| 262 | std::cout << "{" << i->_cycle << "}\t" |
---|
| 263 | << i->_context << " - "; |
---|
| 264 | |
---|
| 265 | switch(i->_type) |
---|
| 266 | { |
---|
[72] | 267 | case DCACHE_TYPE_LOAD : std::cout << "DCACHE_TYPE_LOAD "; break; |
---|
| 268 | case DCACHE_TYPE_LOCK : std::cout << "DCACHE_TYPE_LOCK "; break; |
---|
| 269 | case DCACHE_TYPE_INVALIDATE : std::cout << "DCACHE_TYPE_INVALIDATE "; break; |
---|
| 270 | case DCACHE_TYPE_PREFETCH : std::cout << "DCACHE_TYPE_PREFETCH "; break; |
---|
| 271 | case DCACHE_TYPE_FLUSH : std::cout << "DCACHE_TYPE_FLUSH "; break; |
---|
| 272 | case DCACHE_TYPE_SYNCHRONIZATION : std::cout << "DCACHE_TYPE_SYNCHRONIZATION"; break; |
---|
| 273 | case DCACHE_TYPE_STORE_8 : std::cout << "DCACHE_TYPE_STORE_8 "; break; |
---|
| 274 | case DCACHE_TYPE_STORE_16 : std::cout << "DCACHE_TYPE_STORE_16 "; break; |
---|
| 275 | case DCACHE_TYPE_STORE_32 : std::cout << "DCACHE_TYPE_STORE_32 "; break; |
---|
| 276 | case DCACHE_TYPE_STORE_64 : std::cout << "DCACHE_TYPE_STORE_64 "; break; |
---|
[71] | 277 | } |
---|
| 278 | std::cout << " - " |
---|
| 279 | << hex |
---|
| 280 | << i->_address << " : " |
---|
| 281 | << i->_data_old << " -> " |
---|
| 282 | << i->_data_new << std::endl |
---|
| 283 | << hex; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | }; |
---|
| 287 | |
---|
| 288 | inline void test_Memory_t (void) |
---|
| 289 | { |
---|
| 290 | const uint32_t _nb_context = 4; |
---|
| 291 | const uint32_t _size_word = 32; |
---|
| 292 | const uint32_t _nb_word = 100; |
---|
| 293 | |
---|
| 294 | Memory_t * memory = new Memory_t (_nb_context, _nb_word, _size_word); |
---|
| 295 | |
---|
[72] | 296 | memory -> access (2, 0x10, DCACHE_TYPE_STORE_32, 0xdeadbeef); |
---|
| 297 | memory -> access (2, 0x14, DCACHE_TYPE_STORE_16, 0xdada5678); |
---|
| 298 | memory -> access (2, 0x16, DCACHE_TYPE_STORE_16, 0xbead1234); |
---|
| 299 | memory -> access (2, 0x18, DCACHE_TYPE_STORE_8 , 0x45675681); |
---|
| 300 | memory -> access (2, 0x19, DCACHE_TYPE_STORE_8 , 0x1f311219); |
---|
| 301 | memory -> access (2, 0x1a, DCACHE_TYPE_STORE_8 , 0x2e075607); |
---|
| 302 | memory -> access (2, 0x1b, DCACHE_TYPE_STORE_8 , 0x19811221); |
---|
[71] | 303 | |
---|
[72] | 304 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> access (2, 0x10, DCACHE_TYPE_LOAD, 0), 0xdeadbeef); |
---|
| 305 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> access (2, 0x14, DCACHE_TYPE_LOAD, 0), 0x12345678); |
---|
| 306 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> access (2, 0x18, DCACHE_TYPE_LOAD, 0), 0x21071981); |
---|
[71] | 307 | |
---|
| 308 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x10, OPERATION_MEMORY_LOAD_8_Z ), 0x000000ef); |
---|
| 309 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x10, OPERATION_MEMORY_LOAD_8_S ), 0xffffffef); |
---|
| 310 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x10, OPERATION_MEMORY_LOAD_16_Z), 0x0000beef); |
---|
| 311 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x10, OPERATION_MEMORY_LOAD_16_S), 0xffffbeef); |
---|
| 312 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x10, OPERATION_MEMORY_LOAD_32_Z), 0xdeadbeef); |
---|
| 313 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x10, OPERATION_MEMORY_LOAD_32_S), 0xdeadbeef); |
---|
| 314 | |
---|
| 315 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x12, OPERATION_MEMORY_LOAD_8_Z ), 0x000000ad); |
---|
| 316 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x12, OPERATION_MEMORY_LOAD_8_S ), 0xffffffad); |
---|
| 317 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x12, OPERATION_MEMORY_LOAD_16_Z), 0x0000dead); |
---|
| 318 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x12, OPERATION_MEMORY_LOAD_16_S), 0xffffdead); |
---|
| 319 | |
---|
| 320 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x14, OPERATION_MEMORY_LOAD_8_Z ), 0x00000078); |
---|
| 321 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x14, OPERATION_MEMORY_LOAD_8_S ), 0x00000078); |
---|
| 322 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x18, OPERATION_MEMORY_LOAD_16_Z), 0x00001981); |
---|
| 323 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x18, OPERATION_MEMORY_LOAD_16_S), 0x00001981); |
---|
| 324 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x18, OPERATION_MEMORY_LOAD_32_Z), 0x21071981); |
---|
| 325 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x18, OPERATION_MEMORY_LOAD_32_S), 0x21071981); |
---|
| 326 | |
---|
| 327 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x1a, OPERATION_MEMORY_LOAD_8_Z ), 0x00000007); |
---|
| 328 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x1a, OPERATION_MEMORY_LOAD_8_S ), 0x00000007); |
---|
| 329 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x1a, OPERATION_MEMORY_LOAD_16_Z), 0x00002107); |
---|
| 330 | TEST(morpheo::behavioural::Tdcache_data_t, memory -> read_lsq (2, 0x1a, OPERATION_MEMORY_LOAD_16_S), 0x00002107); |
---|
| 331 | |
---|
| 332 | delete memory; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | #endif |
---|