1 | /* -*- c++ -*- |
---|
2 | * |
---|
3 | * SOCLIB_LGPL_HEADER_BEGIN |
---|
4 | * |
---|
5 | * This file is part of SoCLib, GNU LGPLv2.1. |
---|
6 | * |
---|
7 | * SoCLib is free software; you can redistribute it and/or modify it |
---|
8 | * under the terms of the GNU Lesser General Public License as published |
---|
9 | * by the Free Software Foundation; version 2.1 of the License. |
---|
10 | * |
---|
11 | * SoCLib is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public |
---|
17 | * License along with SoCLib; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
19 | * 02110-1301 USA |
---|
20 | * |
---|
21 | * SOCLIB_LGPL_HEADER_END |
---|
22 | * |
---|
23 | * Copyright (c) UPMC, Lip6, Asim |
---|
24 | * alain.greiner@lip6.fr april 2011 |
---|
25 | * |
---|
26 | * Maintainers: alain |
---|
27 | */ |
---|
28 | |
---|
29 | #include <stdint.h> |
---|
30 | #include <iostream> |
---|
31 | #include <fcntl.h> |
---|
32 | #include "vci_block_device_tsar.h" |
---|
33 | |
---|
34 | namespace soclib { namespace caba { |
---|
35 | |
---|
36 | #define tmpl(t) template<typename vci_param> t VciBlockDeviceTsar<vci_param> |
---|
37 | |
---|
38 | using namespace soclib::caba; |
---|
39 | using namespace soclib::common; |
---|
40 | |
---|
41 | //////////////////////// |
---|
42 | tmpl(void)::transition() |
---|
43 | { |
---|
44 | if(p_resetn.read() == false) |
---|
45 | { |
---|
46 | r_initiator_fsm = M_IDLE; |
---|
47 | r_target_fsm = T_IDLE; |
---|
48 | r_irq_enable = true; |
---|
49 | r_go = false; |
---|
50 | return; |
---|
51 | } |
---|
52 | |
---|
53 | ////////////////////////////////////////////////////////////////////////////// |
---|
54 | // The Target FSM controls the following registers: |
---|
55 | // r_target_fsm, r_irq_enable, r_nblocks, r_buf adress, r_lba, r_go, r_read |
---|
56 | ////////////////////////////////////////////////////////////////////////////// |
---|
57 | |
---|
58 | switch(r_target_fsm) { |
---|
59 | //////////// |
---|
60 | case T_IDLE: |
---|
61 | { |
---|
62 | if ( p_vci_target.cmdval.read() ) |
---|
63 | { |
---|
64 | r_srcid = p_vci_target.srcid.read(); |
---|
65 | r_trdid = p_vci_target.trdid.read(); |
---|
66 | r_pktid = p_vci_target.pktid.read(); |
---|
67 | sc_dt::sc_uint<vci_param::N> address = p_vci_target.address.read(); |
---|
68 | |
---|
69 | bool found = false; |
---|
70 | std::list<soclib::common::Segment>::iterator seg; |
---|
71 | for ( seg = m_seglist.begin() ; seg != m_seglist.end() ; seg++ ) |
---|
72 | { |
---|
73 | if ( seg->contains(address) ) found = true; |
---|
74 | } |
---|
75 | |
---|
76 | bool read = (p_vci_target.cmd.read() == vci_param::CMD_READ); |
---|
77 | uint32_t cell = (uint32_t)((address & 0x3F)>>2); |
---|
78 | |
---|
79 | if ( !read && not found ) r_target_fsm = T_WRITE_ERROR; |
---|
80 | else if( read && not found ) r_target_fsm = T_READ_ERROR; |
---|
81 | else if( !read && not p_vci_target.eop.read() ) r_target_fsm = T_WRITE_ERROR; |
---|
82 | else if( read && not p_vci_target.eop.read() ) r_target_fsm = T_READ_ERROR; |
---|
83 | else if( !read && (cell == BLOCK_DEVICE_BUFFER) ) r_target_fsm = T_WRITE_BUFFER; |
---|
84 | else if( read && (cell == BLOCK_DEVICE_BUFFER) ) r_target_fsm = T_READ_BUFFER; |
---|
85 | else if( !read && (cell == BLOCK_DEVICE_BUFFER_EXT) ) r_target_fsm = T_WRITE_BUFFER_EXT; |
---|
86 | else if( read && (cell == BLOCK_DEVICE_BUFFER_EXT) ) r_target_fsm = T_READ_BUFFER_EXT; |
---|
87 | else if( !read && (cell == BLOCK_DEVICE_COUNT) ) r_target_fsm = T_WRITE_COUNT; |
---|
88 | else if( read && (cell == BLOCK_DEVICE_COUNT) ) r_target_fsm = T_READ_COUNT; |
---|
89 | else if( !read && (cell == BLOCK_DEVICE_LBA) ) r_target_fsm = T_WRITE_LBA; |
---|
90 | else if( read && (cell == BLOCK_DEVICE_LBA) ) r_target_fsm = T_READ_LBA; |
---|
91 | else if( !read && (cell == BLOCK_DEVICE_OP) ) r_target_fsm = T_WRITE_OP; |
---|
92 | else if( read && (cell == BLOCK_DEVICE_STATUS) ) r_target_fsm = T_READ_STATUS; |
---|
93 | else if( !read && (cell == BLOCK_DEVICE_IRQ_ENABLE) ) r_target_fsm = T_WRITE_IRQEN; |
---|
94 | else if( read && (cell == BLOCK_DEVICE_IRQ_ENABLE) ) r_target_fsm = T_READ_IRQEN; |
---|
95 | else if( read && (cell == BLOCK_DEVICE_SIZE) ) r_target_fsm = T_READ_SIZE; |
---|
96 | else if( read && (cell == BLOCK_DEVICE_BLOCK_SIZE) ) r_target_fsm = T_READ_BLOCK; |
---|
97 | } |
---|
98 | break; |
---|
99 | } |
---|
100 | //////////////////// |
---|
101 | case T_WRITE_BUFFER: |
---|
102 | { |
---|
103 | if ( (r_initiator_fsm.read() == M_IDLE) and p_vci_target.rspack.read() ) |
---|
104 | { |
---|
105 | r_buf_address = (uint64_t)p_vci_target.wdata.read(); |
---|
106 | r_target_fsm = T_IDLE; |
---|
107 | } |
---|
108 | break; |
---|
109 | } |
---|
110 | //////////////////////// |
---|
111 | case T_WRITE_BUFFER_EXT: |
---|
112 | { |
---|
113 | if ( (r_initiator_fsm.read() == M_IDLE) and p_vci_target.rspack.read() ) |
---|
114 | { |
---|
115 | r_buf_address = r_buf_address.read() + |
---|
116 | (((uint64_t)p_vci_target.wdata.read())<<32); |
---|
117 | r_target_fsm = T_IDLE; |
---|
118 | } |
---|
119 | break; |
---|
120 | } |
---|
121 | /////////////////// |
---|
122 | case T_WRITE_COUNT: |
---|
123 | { |
---|
124 | if ( (r_initiator_fsm.read() == M_IDLE) and p_vci_target.rspack.read() ) |
---|
125 | { |
---|
126 | r_nblocks = (uint32_t)p_vci_target.wdata.read(); |
---|
127 | r_target_fsm = T_IDLE; |
---|
128 | } |
---|
129 | break; |
---|
130 | } |
---|
131 | ///////////////// |
---|
132 | case T_WRITE_LBA: |
---|
133 | { |
---|
134 | if ( (r_initiator_fsm.read() == M_IDLE) and p_vci_target.rspack.read() ) |
---|
135 | { |
---|
136 | r_lba = (uint32_t)p_vci_target.wdata.read(); |
---|
137 | r_target_fsm = T_IDLE; |
---|
138 | } |
---|
139 | break; |
---|
140 | } |
---|
141 | //////////////// |
---|
142 | case T_WRITE_OP: |
---|
143 | { |
---|
144 | if ( p_vci_target.rspack.read() ) |
---|
145 | { |
---|
146 | if ( ((uint32_t)p_vci_target.wdata.read() == BLOCK_DEVICE_READ) and |
---|
147 | (r_initiator_fsm.read() == M_IDLE) ) |
---|
148 | { |
---|
149 | r_read = true; |
---|
150 | r_go = true; |
---|
151 | } |
---|
152 | else if ( ((uint32_t)p_vci_target.wdata.read() == BLOCK_DEVICE_WRITE) and |
---|
153 | (r_initiator_fsm.read() == M_IDLE) ) |
---|
154 | { |
---|
155 | r_read = false; |
---|
156 | r_go = true; |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | r_go = false; |
---|
161 | } |
---|
162 | r_target_fsm = T_IDLE; |
---|
163 | } |
---|
164 | break; |
---|
165 | } |
---|
166 | /////////////////// |
---|
167 | case T_WRITE_IRQEN: |
---|
168 | { |
---|
169 | if ( p_vci_target.rspack.read() ) |
---|
170 | { |
---|
171 | r_target_fsm = T_IDLE; |
---|
172 | r_irq_enable = (p_vci_target.wdata.read() != 0); |
---|
173 | } |
---|
174 | break; |
---|
175 | } |
---|
176 | /////////////////// |
---|
177 | case T_READ_BUFFER: |
---|
178 | case T_READ_BUFFER_EXT: |
---|
179 | case T_READ_COUNT: |
---|
180 | case T_READ_LBA: |
---|
181 | case T_READ_IRQEN: |
---|
182 | case T_READ_SIZE: |
---|
183 | case T_READ_BLOCK: |
---|
184 | case T_READ_ERROR: |
---|
185 | case T_WRITE_ERROR: |
---|
186 | { |
---|
187 | if ( p_vci_target.rspack.read() ) r_target_fsm = T_IDLE; |
---|
188 | break; |
---|
189 | } |
---|
190 | /////////////////// |
---|
191 | case T_READ_STATUS: |
---|
192 | { |
---|
193 | if ( p_vci_target.rspack.read() ) |
---|
194 | { |
---|
195 | r_target_fsm = T_IDLE; |
---|
196 | if( (r_initiator_fsm == M_READ_SUCCESS ) || |
---|
197 | (r_initiator_fsm == M_READ_ERROR ) || |
---|
198 | (r_initiator_fsm == M_WRITE_SUCCESS) || |
---|
199 | (r_initiator_fsm == M_WRITE_ERROR ) ) r_go = false; |
---|
200 | } |
---|
201 | break; |
---|
202 | } |
---|
203 | } // end switch target fsm |
---|
204 | |
---|
205 | ////////////////////////////////////////////////////////////////////////////// |
---|
206 | // The initiator FSM executes a loop, transfering one block per iteration. |
---|
207 | // Each block is split in bursts, and the number of bursts depends |
---|
208 | // on the memory buffer alignment on a burst boundary: |
---|
209 | // - If buffer aligned, all burst have the same length (m_words_per burst) |
---|
210 | // and the number of bursts is (m_bursts_per_block). |
---|
211 | // - If buffer not aligned, the number of bursts is (m_bursts_per_block + 1) |
---|
212 | // and first and last burst are shorter, because all words in a burst |
---|
213 | // must be contained in a single cache line. |
---|
214 | // first burst => nwords = m_words_per_burst - offset |
---|
215 | // last burst => nwords = offset |
---|
216 | // other burst => nwords = m_words_per_burst |
---|
217 | ////////////////////////////////////////////////////////////////////////////// |
---|
218 | |
---|
219 | switch( r_initiator_fsm.read() ) { |
---|
220 | //////////// |
---|
221 | case M_IDLE: // check buffer alignment to compute the number of bursts |
---|
222 | { |
---|
223 | if ( r_go.read() ) |
---|
224 | { |
---|
225 | r_index = 0; |
---|
226 | r_block_count = 0; |
---|
227 | r_burst_count = 0; |
---|
228 | r_words_count = 0; |
---|
229 | r_latency_count = m_latency; |
---|
230 | |
---|
231 | // compute r_burst_offset (zero when buffer aligned) |
---|
232 | r_burst_offset = (uint32_t)((r_buf_address.read()>>2) % m_words_per_burst); |
---|
233 | |
---|
234 | // start tranfer |
---|
235 | if ( r_read.read() ) r_initiator_fsm = M_READ_BLOCK; |
---|
236 | else r_initiator_fsm = M_WRITE_BURST; |
---|
237 | } |
---|
238 | break; |
---|
239 | } |
---|
240 | ////////////////// |
---|
241 | case M_READ_BLOCK: // read one block from disk after waiting m_latency cycles |
---|
242 | { |
---|
243 | if ( r_latency_count.read() == 0 ) |
---|
244 | { |
---|
245 | r_latency_count = m_latency; |
---|
246 | ::lseek(m_fd, (r_lba + r_block_count)*m_words_per_block*4, SEEK_SET); |
---|
247 | if( ::read(m_fd, r_local_buffer, m_words_per_block*4) < 0 ) |
---|
248 | { |
---|
249 | r_initiator_fsm = M_READ_ERROR; |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | r_burst_count = 0; |
---|
254 | r_words_count = 0; |
---|
255 | r_initiator_fsm = M_READ_BURST; |
---|
256 | } |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | r_latency_count = r_latency_count.read() - 1; |
---|
261 | } |
---|
262 | break; |
---|
263 | } |
---|
264 | ////////////////// |
---|
265 | case M_READ_BURST: // Compute the number of words and the number of flits in the burst |
---|
266 | // The number of flits can be smaller than the number of words |
---|
267 | // in case of 8 bytes flits... |
---|
268 | { |
---|
269 | uint32_t nwords; |
---|
270 | uint32_t offset = r_burst_offset.read(); |
---|
271 | |
---|
272 | if ( offset ) // buffer not aligned |
---|
273 | { |
---|
274 | if ( r_burst_count.read() == 0 ) nwords = m_words_per_burst - offset; |
---|
275 | else if ( r_burst_count.read() == m_bursts_per_block ) nwords = offset; |
---|
276 | else nwords = m_words_per_burst; |
---|
277 | } |
---|
278 | else // buffer aligned |
---|
279 | { |
---|
280 | nwords = m_words_per_burst; |
---|
281 | } |
---|
282 | |
---|
283 | r_burst_nwords = nwords; |
---|
284 | r_initiator_fsm = M_READ_CMD; |
---|
285 | break; |
---|
286 | } |
---|
287 | //////////////// |
---|
288 | case M_READ_CMD: // Send a multi-flits VCI WRITE command |
---|
289 | { |
---|
290 | if ( p_vci_initiator.cmdack.read() ) |
---|
291 | { |
---|
292 | uint32_t nwords = r_burst_nwords.read() - r_words_count.read(); |
---|
293 | |
---|
294 | if ( vci_param::B == 4 ) // one word per flit |
---|
295 | { |
---|
296 | if ( nwords <= 1 ) // last flit |
---|
297 | { |
---|
298 | r_initiator_fsm = M_READ_RSP; |
---|
299 | r_words_count = 0; |
---|
300 | } |
---|
301 | else // not the last flit |
---|
302 | { |
---|
303 | r_words_count = r_words_count.read() + 1; |
---|
304 | } |
---|
305 | |
---|
306 | // compute next word address and next local buffer index |
---|
307 | r_buf_address = r_buf_address.read() + 4; |
---|
308 | r_index = r_index.read() + 1; |
---|
309 | } |
---|
310 | else // 2 words per flit |
---|
311 | { |
---|
312 | if ( nwords <= 2 ) // last flit |
---|
313 | { |
---|
314 | r_initiator_fsm = M_READ_RSP; |
---|
315 | r_words_count = 0; |
---|
316 | } |
---|
317 | else // not the last flit |
---|
318 | { |
---|
319 | r_words_count = r_words_count.read() + 2; |
---|
320 | } |
---|
321 | |
---|
322 | // compute next word address and next local buffer index |
---|
323 | if ( nwords == 1 ) |
---|
324 | { |
---|
325 | r_buf_address = r_buf_address.read() + 4; |
---|
326 | r_index = r_index.read() + 1; |
---|
327 | } |
---|
328 | else |
---|
329 | { |
---|
330 | r_buf_address = r_buf_address.read() + 8; |
---|
331 | r_index = r_index.read() + 2; |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | break; |
---|
336 | } |
---|
337 | //////////////// |
---|
338 | case M_READ_RSP: // Wait a single flit VCI WRITE response |
---|
339 | { |
---|
340 | if ( p_vci_initiator.rspval.read() ) |
---|
341 | { |
---|
342 | bool aligned = (r_burst_offset.read() == 0); |
---|
343 | |
---|
344 | if ( (p_vci_initiator.rerror.read()&0x1) != 0 ) |
---|
345 | { |
---|
346 | r_initiator_fsm = M_READ_ERROR; |
---|
347 | } |
---|
348 | else if ( (not aligned and (r_burst_count.read() == m_bursts_per_block)) or |
---|
349 | (aligned and (r_burst_count.read() == (m_bursts_per_block-1))) ) |
---|
350 | { |
---|
351 | if ( r_block_count.read() == (r_nblocks.read()-1) ) // last burst of last block |
---|
352 | { |
---|
353 | r_initiator_fsm = M_READ_SUCCESS; |
---|
354 | } |
---|
355 | else // last burst not last block |
---|
356 | { |
---|
357 | r_index = 0; |
---|
358 | r_burst_count = 0; |
---|
359 | r_block_count = r_block_count.read() + 1; |
---|
360 | r_initiator_fsm = M_READ_BLOCK; |
---|
361 | } |
---|
362 | } |
---|
363 | else // not the last burst |
---|
364 | { |
---|
365 | r_burst_count = r_burst_count.read() + 1; |
---|
366 | r_initiator_fsm = M_READ_BURST; |
---|
367 | } |
---|
368 | } |
---|
369 | break; |
---|
370 | } |
---|
371 | /////////////////// |
---|
372 | case M_READ_SUCCESS: |
---|
373 | case M_READ_ERROR: |
---|
374 | { |
---|
375 | if( !r_go ) r_initiator_fsm = M_IDLE; |
---|
376 | break; |
---|
377 | } |
---|
378 | /////////////////// |
---|
379 | case M_WRITE_BURST: // Compute the number of words in the burst |
---|
380 | { |
---|
381 | uint32_t nwords; |
---|
382 | uint32_t offset = r_burst_offset.read(); |
---|
383 | |
---|
384 | if ( offset ) // buffer not aligned |
---|
385 | { |
---|
386 | if ( r_burst_count.read() == 0 ) nwords = m_words_per_burst - offset; |
---|
387 | else if ( r_burst_count.read() == m_bursts_per_block ) nwords = offset; |
---|
388 | else nwords = m_words_per_burst; |
---|
389 | } |
---|
390 | else // buffer aligned |
---|
391 | { |
---|
392 | nwords = m_words_per_burst; |
---|
393 | } |
---|
394 | |
---|
395 | r_burst_nwords = nwords; |
---|
396 | r_initiator_fsm = M_WRITE_CMD; |
---|
397 | break; |
---|
398 | } |
---|
399 | ///////////////// |
---|
400 | case M_WRITE_CMD: // This is actually a single flit VCI READ command |
---|
401 | { |
---|
402 | if ( p_vci_initiator.cmdack.read() ) r_initiator_fsm = M_WRITE_RSP; |
---|
403 | break; |
---|
404 | } |
---|
405 | ///////////////// |
---|
406 | case M_WRITE_RSP: // This is actually a multi-words VCI READ response |
---|
407 | { |
---|
408 | if ( p_vci_initiator.rspval.read() ) |
---|
409 | { |
---|
410 | bool aligned = (r_burst_offset.read() == 0); |
---|
411 | |
---|
412 | if ( (vci_param::B == 8) and (r_burst_nwords.read() > 1) ) |
---|
413 | { |
---|
414 | r_local_buffer[r_index.read()] = (uint32_t)p_vci_initiator.rdata.read(); |
---|
415 | r_local_buffer[r_index.read()+1] = (uint32_t)(p_vci_initiator.rdata.read()>>32); |
---|
416 | r_index = r_index.read() + 2; |
---|
417 | } |
---|
418 | else |
---|
419 | { |
---|
420 | r_local_buffer[r_index.read()] = (uint32_t)p_vci_initiator.rdata.read(); |
---|
421 | r_index = r_index.read() + 1; |
---|
422 | } |
---|
423 | |
---|
424 | if ( p_vci_initiator.reop.read() ) // last flit of the burst |
---|
425 | { |
---|
426 | r_words_count = 0; |
---|
427 | r_buf_address = r_buf_address.read() + (r_burst_nwords.read()<<2); |
---|
428 | |
---|
429 | if( (p_vci_initiator.rerror.read()&0x1) != 0 ) |
---|
430 | { |
---|
431 | r_initiator_fsm = M_WRITE_ERROR; |
---|
432 | } |
---|
433 | else if ( (not aligned and (r_burst_count.read() == m_bursts_per_block)) or |
---|
434 | (aligned and (r_burst_count.read() == (m_bursts_per_block-1))) ) // last burst |
---|
435 | { |
---|
436 | r_initiator_fsm = M_WRITE_BLOCK; |
---|
437 | } |
---|
438 | else // not the last burst |
---|
439 | { |
---|
440 | r_burst_count = r_burst_count.read() + 1; |
---|
441 | r_initiator_fsm = M_WRITE_BURST; |
---|
442 | } |
---|
443 | } |
---|
444 | else |
---|
445 | { |
---|
446 | r_words_count = r_words_count.read() + 1; |
---|
447 | } |
---|
448 | } |
---|
449 | break; |
---|
450 | } |
---|
451 | /////////////////// |
---|
452 | case M_WRITE_BLOCK: // write a block to disk after waiting m_latency cycles |
---|
453 | { |
---|
454 | if ( r_latency_count == 0 ) |
---|
455 | { |
---|
456 | r_latency_count = m_latency; |
---|
457 | ::lseek(m_fd, (r_lba + r_block_count)*m_words_per_block*vci_param::B, SEEK_SET); |
---|
458 | if( ::write(m_fd, r_local_buffer, m_words_per_block*vci_param::B) < 0 ) |
---|
459 | { |
---|
460 | r_initiator_fsm = M_WRITE_ERROR; |
---|
461 | } |
---|
462 | else if ( r_block_count.read() == r_nblocks.read() - 1 ) |
---|
463 | { |
---|
464 | r_initiator_fsm = M_WRITE_SUCCESS; |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | r_burst_count = 0; |
---|
469 | r_index = 0; |
---|
470 | r_block_count = r_block_count.read() + 1; |
---|
471 | r_initiator_fsm = M_WRITE_BURST; |
---|
472 | } |
---|
473 | } |
---|
474 | else |
---|
475 | { |
---|
476 | r_latency_count = r_latency_count - 1; |
---|
477 | } |
---|
478 | break; |
---|
479 | } |
---|
480 | ///////////////////// |
---|
481 | case M_WRITE_SUCCESS: |
---|
482 | case M_WRITE_ERROR: |
---|
483 | { |
---|
484 | if( !r_go ) r_initiator_fsm = M_IDLE; |
---|
485 | break; |
---|
486 | } |
---|
487 | } // end switch r_initiator_fsm |
---|
488 | } // end transition |
---|
489 | |
---|
490 | ////////////////////// |
---|
491 | tmpl(void)::genMoore() |
---|
492 | { |
---|
493 | // p_vci_target port |
---|
494 | p_vci_target.rsrcid = (sc_dt::sc_uint<vci_param::S>)r_srcid.read(); |
---|
495 | p_vci_target.rtrdid = (sc_dt::sc_uint<vci_param::T>)r_trdid.read(); |
---|
496 | p_vci_target.rpktid = (sc_dt::sc_uint<vci_param::P>)r_pktid.read(); |
---|
497 | p_vci_target.reop = true; |
---|
498 | |
---|
499 | switch(r_target_fsm) { |
---|
500 | case T_IDLE: |
---|
501 | p_vci_target.cmdack = true; |
---|
502 | p_vci_target.rspval = false; |
---|
503 | break; |
---|
504 | case T_READ_STATUS: |
---|
505 | p_vci_target.cmdack = false; |
---|
506 | p_vci_target.rspval = true; |
---|
507 | if (r_initiator_fsm == M_IDLE) p_vci_target.rdata = BLOCK_DEVICE_IDLE; |
---|
508 | else if(r_initiator_fsm == M_READ_SUCCESS) p_vci_target.rdata = BLOCK_DEVICE_READ_SUCCESS; |
---|
509 | else if(r_initiator_fsm == M_WRITE_SUCCESS) p_vci_target.rdata = BLOCK_DEVICE_WRITE_SUCCESS; |
---|
510 | else if(r_initiator_fsm == M_READ_ERROR) p_vci_target.rdata = BLOCK_DEVICE_READ_ERROR; |
---|
511 | else if(r_initiator_fsm == M_WRITE_ERROR) p_vci_target.rdata = BLOCK_DEVICE_WRITE_ERROR; |
---|
512 | else p_vci_target.rdata = BLOCK_DEVICE_BUSY; |
---|
513 | p_vci_target.rerror = VCI_READ_OK; |
---|
514 | break; |
---|
515 | case T_READ_BUFFER: |
---|
516 | p_vci_target.cmdack = false; |
---|
517 | p_vci_target.rspval = true; |
---|
518 | p_vci_target.rdata = (uint32_t)r_buf_address.read(); |
---|
519 | p_vci_target.rerror = VCI_READ_OK; |
---|
520 | break; |
---|
521 | case T_READ_BUFFER_EXT: |
---|
522 | p_vci_target.cmdack = false; |
---|
523 | p_vci_target.rspval = true; |
---|
524 | p_vci_target.rdata = (uint32_t)(r_buf_address.read()>>32); |
---|
525 | p_vci_target.rerror = VCI_READ_OK; |
---|
526 | break; |
---|
527 | case T_READ_COUNT: |
---|
528 | p_vci_target.cmdack = false; |
---|
529 | p_vci_target.rspval = true; |
---|
530 | p_vci_target.rdata = r_nblocks.read(); |
---|
531 | p_vci_target.rerror = VCI_READ_OK; |
---|
532 | break; |
---|
533 | case T_READ_LBA: |
---|
534 | p_vci_target.cmdack = false; |
---|
535 | p_vci_target.rspval = true; |
---|
536 | p_vci_target.rdata = r_lba.read(); |
---|
537 | p_vci_target.rerror = VCI_READ_OK; |
---|
538 | break; |
---|
539 | case T_READ_IRQEN: |
---|
540 | p_vci_target.cmdack = false; |
---|
541 | p_vci_target.rspval = true; |
---|
542 | p_vci_target.rdata = r_irq_enable.read(); |
---|
543 | p_vci_target.rerror = VCI_READ_OK; |
---|
544 | break; |
---|
545 | case T_READ_SIZE: |
---|
546 | p_vci_target.cmdack = false; |
---|
547 | p_vci_target.rspval = true; |
---|
548 | p_vci_target.rdata = m_device_size; |
---|
549 | p_vci_target.rerror = VCI_READ_OK; |
---|
550 | break; |
---|
551 | case T_READ_BLOCK: |
---|
552 | p_vci_target.cmdack = false; |
---|
553 | p_vci_target.rspval = true; |
---|
554 | p_vci_target.rdata = m_words_per_block*4; |
---|
555 | p_vci_target.rerror = VCI_READ_OK; |
---|
556 | break; |
---|
557 | case T_READ_ERROR: |
---|
558 | p_vci_target.cmdack = false; |
---|
559 | p_vci_target.rspval = true; |
---|
560 | p_vci_target.rdata = 0; |
---|
561 | p_vci_target.rerror = VCI_READ_ERROR; |
---|
562 | break; |
---|
563 | case T_WRITE_ERROR: |
---|
564 | p_vci_target.cmdack = false; |
---|
565 | p_vci_target.rspval = true; |
---|
566 | p_vci_target.rdata = 0; |
---|
567 | p_vci_target.rerror = VCI_WRITE_ERROR; |
---|
568 | break; |
---|
569 | default: |
---|
570 | p_vci_target.cmdack = false; |
---|
571 | p_vci_target.rspval = true; |
---|
572 | p_vci_target.rdata = 0; |
---|
573 | p_vci_target.rerror = VCI_WRITE_OK; |
---|
574 | break; |
---|
575 | } // end switch target fsm |
---|
576 | |
---|
577 | // p_vci_initiator port |
---|
578 | p_vci_initiator.srcid = (sc_dt::sc_uint<vci_param::S>)m_srcid; |
---|
579 | p_vci_initiator.trdid = 0; |
---|
580 | p_vci_initiator.contig = true; |
---|
581 | p_vci_initiator.cons = false; |
---|
582 | p_vci_initiator.wrap = false; |
---|
583 | p_vci_initiator.cfixed = false; |
---|
584 | p_vci_initiator.clen = 0; |
---|
585 | |
---|
586 | switch (r_initiator_fsm) { |
---|
587 | case M_WRITE_CMD: // It is actually a single flit VCI read command |
---|
588 | p_vci_initiator.rspack = false; |
---|
589 | p_vci_initiator.cmdval = true; |
---|
590 | p_vci_initiator.address = (sc_dt::sc_uint<vci_param::N>)r_buf_address.read(); |
---|
591 | p_vci_initiator.cmd = vci_param::CMD_READ; |
---|
592 | p_vci_initiator.pktid = TYPE_READ_DATA_UNC; |
---|
593 | p_vci_initiator.wdata = 0; |
---|
594 | p_vci_initiator.be = 0; |
---|
595 | p_vci_initiator.plen = (sc_dt::sc_uint<vci_param::K>)(r_burst_nwords.read()<<2); |
---|
596 | p_vci_initiator.eop = true; |
---|
597 | break; |
---|
598 | case M_READ_CMD: // It is actually a multi-words VCI WRITE command |
---|
599 | p_vci_initiator.rspack = false; |
---|
600 | p_vci_initiator.cmdval = true; |
---|
601 | p_vci_initiator.address = (sc_dt::sc_uint<vci_param::N>)r_buf_address.read(); |
---|
602 | p_vci_initiator.cmd = vci_param::CMD_WRITE; |
---|
603 | p_vci_initiator.pktid = TYPE_WRITE; |
---|
604 | p_vci_initiator.plen = (sc_dt::sc_uint<vci_param::K>)(r_burst_nwords.read()<<2); |
---|
605 | if ( (vci_param::B == 8) and ((r_burst_nwords.read() - r_words_count.read()) > 1) ) |
---|
606 | { |
---|
607 | p_vci_initiator.wdata = ((uint64_t)r_local_buffer[r_index.read() ]) + |
---|
608 | (((uint64_t)r_local_buffer[r_index.read()+1]) << 32); |
---|
609 | p_vci_initiator.be = 0xFF; |
---|
610 | p_vci_initiator.eop = ( (r_burst_nwords.read() - r_words_count.read()) <= 2 ); |
---|
611 | } |
---|
612 | else |
---|
613 | { |
---|
614 | p_vci_initiator.wdata = r_local_buffer[r_index.read()]; |
---|
615 | p_vci_initiator.be = 0xF; |
---|
616 | p_vci_initiator.eop = ( r_words_count.read() == (r_burst_nwords.read() - 1) ); |
---|
617 | } |
---|
618 | break; |
---|
619 | case M_READ_RSP: |
---|
620 | case M_WRITE_RSP: |
---|
621 | p_vci_initiator.rspack = true; |
---|
622 | p_vci_initiator.cmdval = false; |
---|
623 | break; |
---|
624 | default: |
---|
625 | p_vci_initiator.rspack = false; |
---|
626 | p_vci_initiator.cmdval = false; |
---|
627 | break; |
---|
628 | } |
---|
629 | |
---|
630 | // IRQ signal |
---|
631 | if ( ((r_initiator_fsm == M_READ_SUCCESS) || |
---|
632 | (r_initiator_fsm == M_WRITE_SUCCESS) || |
---|
633 | (r_initiator_fsm == M_READ_ERROR) || |
---|
634 | (r_initiator_fsm == M_WRITE_ERROR) ) && |
---|
635 | r_irq_enable.read() ) p_irq = true; |
---|
636 | else p_irq = false; |
---|
637 | } // end GenMoore() |
---|
638 | |
---|
639 | ////////////////////////////////////////////////////////////////////////////// |
---|
640 | tmpl(/**/)::VciBlockDeviceTsar( sc_core::sc_module_name name, |
---|
641 | const soclib::common::MappingTable &mt, |
---|
642 | const soclib::common::IntTab &srcid, |
---|
643 | const soclib::common::IntTab &tgtid, |
---|
644 | const std::string &filename, |
---|
645 | const uint32_t block_size, |
---|
646 | const uint32_t burst_size, |
---|
647 | const uint32_t latency) |
---|
648 | |
---|
649 | : caba::BaseModule(name), |
---|
650 | m_seglist(mt.getSegmentList(tgtid)), |
---|
651 | m_srcid(mt.indexForId(srcid)), |
---|
652 | m_words_per_block(block_size/4), |
---|
653 | m_words_per_burst(burst_size/4), |
---|
654 | m_bursts_per_block(block_size/burst_size), |
---|
655 | m_latency(latency), |
---|
656 | p_clk("p_clk"), |
---|
657 | p_resetn("p_resetn"), |
---|
658 | p_vci_initiator("p_vci_initiator"), |
---|
659 | p_vci_target("p_vci_target"), |
---|
660 | p_irq("p_irq") |
---|
661 | { |
---|
662 | std::cout << " - Building VciBlockDeviceTsar " << name << std::endl; |
---|
663 | |
---|
664 | SC_METHOD(transition); |
---|
665 | dont_initialize(); |
---|
666 | sensitive << p_clk.pos(); |
---|
667 | |
---|
668 | SC_METHOD(genMoore); |
---|
669 | dont_initialize(); |
---|
670 | sensitive << p_clk.neg(); |
---|
671 | |
---|
672 | size_t nbsegs = 0; |
---|
673 | std::list<soclib::common::Segment>::iterator seg; |
---|
674 | for ( seg = m_seglist.begin() ; seg != m_seglist.end() ; seg++ ) |
---|
675 | { |
---|
676 | nbsegs++; |
---|
677 | |
---|
678 | if ( (seg->baseAddress() & 0x0000003F) != 0 ) |
---|
679 | { |
---|
680 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
681 | << "The base address of segment " << seg->name() |
---|
682 | << " must be multiple of 64 bytes" << std::endl; |
---|
683 | exit(1); |
---|
684 | } |
---|
685 | if ( seg->size() < 64 ) |
---|
686 | { |
---|
687 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
688 | << "The size of segment " << seg->name() |
---|
689 | << " cannot be smaller than 64 bytes" << std::endl; |
---|
690 | exit(1); |
---|
691 | } |
---|
692 | std::cout << " => segment " << seg->name() |
---|
693 | << " / base = " << std::hex << seg->baseAddress() |
---|
694 | << " / size = " << seg->size() << std::endl; |
---|
695 | } |
---|
696 | |
---|
697 | if( nbsegs == 0 ) |
---|
698 | { |
---|
699 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
700 | << " No segment allocated" << std::endl; |
---|
701 | exit(1); |
---|
702 | } |
---|
703 | |
---|
704 | if( (block_size != 128) && |
---|
705 | (block_size != 256) && |
---|
706 | (block_size != 512) && |
---|
707 | (block_size != 1024) && |
---|
708 | (block_size != 2048) && |
---|
709 | (block_size != 4096) ) |
---|
710 | { |
---|
711 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
712 | << " The block size must be 128, 256, 512, 1024, 2048 or 4096 bytes" |
---|
713 | << std::endl; |
---|
714 | exit(1); |
---|
715 | } |
---|
716 | |
---|
717 | if( (burst_size != 8 ) && |
---|
718 | (burst_size != 16) && |
---|
719 | (burst_size != 32) && |
---|
720 | (burst_size != 64) ) |
---|
721 | { |
---|
722 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
723 | << " The burst size must be 8, 16, 32 or 64 bytes" << std::endl; |
---|
724 | exit(1); |
---|
725 | } |
---|
726 | |
---|
727 | if ( (vci_param::B != 4) and (vci_param::B != 8) ) |
---|
728 | { |
---|
729 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
730 | << " The VCI data fields must have 32 bits or 64 bits" << std::endl; |
---|
731 | exit(1); |
---|
732 | } |
---|
733 | |
---|
734 | m_fd = ::open(filename.c_str(), O_RDWR); |
---|
735 | if ( m_fd < 0 ) |
---|
736 | { |
---|
737 | std::cout << "Error in component VciBlockDeviceTsar : " << name |
---|
738 | << " Unable to open file " << filename << std::endl; |
---|
739 | exit(1); |
---|
740 | } |
---|
741 | m_device_size = lseek(m_fd, 0, SEEK_END) / block_size; |
---|
742 | |
---|
743 | if ( m_device_size > ((uint64_t)1<<vci_param::N ) ) |
---|
744 | { |
---|
745 | std::cout << "Error in component VciBlockDeviceTsar" << name |
---|
746 | << " The file " << filename |
---|
747 | << " has more blocks than addressable with the VCI address" << std::endl; |
---|
748 | exit(1); |
---|
749 | } |
---|
750 | |
---|
751 | r_local_buffer = new uint32_t[m_words_per_block]; |
---|
752 | |
---|
753 | } // end constructor |
---|
754 | |
---|
755 | ////////////////////////// |
---|
756 | tmpl(void)::print_trace() |
---|
757 | { |
---|
758 | const char* initiator_str[] = { |
---|
759 | "IDLE", |
---|
760 | |
---|
761 | "READ_BLOCK", |
---|
762 | "READ_BURST", |
---|
763 | "READ_CMD", |
---|
764 | "READ_RSP", |
---|
765 | "READ_TEST", |
---|
766 | "READ_SUCCESS", |
---|
767 | "READ_ERROR", |
---|
768 | |
---|
769 | "WRITE_BURST", |
---|
770 | "WRITE_CMD", |
---|
771 | "WRITE_RSP", |
---|
772 | "WRITE_BLOCK", |
---|
773 | "WRITE_SUCCESS", |
---|
774 | "WRITE_ERROR", |
---|
775 | }; |
---|
776 | const char* target_str[] = { |
---|
777 | "IDLE", |
---|
778 | "WRITE_BUFFER", |
---|
779 | "READ_BUFFER", |
---|
780 | "WRITE_BUFFER_EXT", |
---|
781 | "READ_BUFFER_EXT", |
---|
782 | "WRITE_COUNT", |
---|
783 | "READ_COUNT", |
---|
784 | "WRITE_LBA", |
---|
785 | "READ_LBA", |
---|
786 | "WRITE_OP", |
---|
787 | "READ_STATUS", |
---|
788 | "WRITE_IRQEN", |
---|
789 | "READ_IRQEN", |
---|
790 | "READ_SIZE", |
---|
791 | "READ_BLOCK", |
---|
792 | "READ_ERROR", |
---|
793 | "WRITE_ERROR ", |
---|
794 | }; |
---|
795 | |
---|
796 | std::cout << "BDEV_TGT : " << target_str[r_target_fsm.read()] |
---|
797 | << " BDEV_INI : " << initiator_str[r_initiator_fsm.read()] |
---|
798 | << " buf = " << std::hex << r_buf_address.read() |
---|
799 | << " block = " << std::dec << r_block_count.read() |
---|
800 | << " burst = " << r_burst_count.read() |
---|
801 | << " word = " << r_words_count.read() <<std::endl; |
---|
802 | } |
---|
803 | |
---|
804 | |
---|
805 | }} // end namespace |
---|
806 | |
---|
807 | // Local Variables: |
---|
808 | // tab-width: 4 |
---|
809 | // c-basic-offset: 4 |
---|
810 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
811 | // indent-tabs-mode: nil |
---|
812 | // End: |
---|
813 | |
---|
814 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
815 | |
---|