1 // See LICENSE for license details.
2 
3 #include <iostream>
4 #include <assert.h>
5 #include "htif_hexwriter.h"
6 
htif_hexwriter_t(size_t b,size_t w,size_t d)7 htif_hexwriter_t::htif_hexwriter_t(size_t b, size_t w, size_t d)
8   : base(b), width(w), depth(d)
9 {
10 }
11 
read_chunk(addr_t taddr,size_t len,void * vdst)12 void htif_hexwriter_t::read_chunk(addr_t taddr, size_t len, void* vdst)
13 {
14   taddr -= base;
15 
16   assert(len % chunk_align() == 0);
17   assert(taddr < width*depth);
18   assert(taddr+len <= width*depth);
19 
20   uint8_t* dst = (uint8_t*)vdst;
21   while(len)
22   {
23     if(mem[taddr/width].size() == 0)
24       mem[taddr/width].resize(width,0);
25 
26     for(size_t j = 0; j < width; j++)
27       dst[j] = mem[taddr/width][j];
28 
29     len -= width;
30     taddr += width;
31     dst += width;
32   }
33 }
34 
write_chunk(addr_t taddr,size_t len,const void * vsrc)35 void htif_hexwriter_t::write_chunk(addr_t taddr, size_t len, const void* vsrc)
36 {
37   taddr -= base;
38 
39   assert(len % chunk_align() == 0);
40   assert(taddr < width*depth);
41   assert(taddr+len <= width*depth);
42 
43   const uint8_t* src = (const uint8_t*)vsrc;
44   while(len)
45   {
46     if(mem[taddr/width].size() == 0)
47       mem[taddr/width].resize(width,0);
48 
49     for(size_t j = 0; j < width; j++)
50       mem[taddr/width][j] = src[j];
51 
52     len -= width;
53     taddr += width;
54   }
55 }
56 
operator <<(std::ostream & o,const htif_hexwriter_t & h)57 std::ostream& operator<< (std::ostream& o, const htif_hexwriter_t& h)
58 {
59   std::ios_base::fmtflags flags = o.setf(std::ios::hex,std::ios::basefield);
60 
61   for(size_t addr = 0; addr < h.depth; addr++)
62   {
63     std::map<addr_t,std::vector<char> >::const_iterator i = h.mem.find(addr);
64     if(i == h.mem.end())
65       for(size_t j = 0; j < h.width; j++)
66         o << "00";
67     else
68       for(size_t j = 0; j < h.width; j++)
69         o << ((i->second[h.width-j-1] >> 4) & 0xF) << (i->second[h.width-j-1] & 0xF);
70     o << std::endl;
71   }
72 
73   o.setf(flags);
74 
75   return o;
76 }
77