gem5  v22.1.0.0
bufval.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2022 Google Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "sim/bufval.hh"
29 
30 #include <cassert>
31 #include <sstream>
32 
33 #include "base/intmath.hh"
34 #include "base/logging.hh"
35 #include "sim/byteswap.hh"
36 
37 namespace gem5
38 {
39 
41 getUintX(const void *buf, std::size_t bytes, ByteOrder endian)
42 {
43  assert(buf);
44  switch (bytes) {
45  case sizeof(std::uint64_t):
46  return {gtoh(*(const std::uint64_t *)buf, endian), true};
47  case sizeof(std::uint32_t):
48  return {gtoh(*(const std::uint32_t *)buf, endian), true};
49  case sizeof(std::uint16_t):
50  return {gtoh(*(const std::uint16_t *)buf, endian), true};
51  case sizeof(std::uint8_t):
52  return {gtoh(*(const std::uint8_t *)buf, endian), true};
53  default:
54  return {0, false};
55  }
56 }
57 
58 bool
59 setUintX(std::uint64_t val, void *buf, std::size_t bytes, ByteOrder endian)
60 {
61  assert(buf);
62 
63  switch (bytes) {
64  case sizeof(std::uint64_t):
65  *(std::uint64_t *)buf = htog<std::uint64_t>(val, endian);
66  return true;
67  case sizeof(std::uint32_t):
68  *(std::uint32_t *)buf = htog<std::uint32_t>(val, endian);
69  return true;
70  case sizeof(std::uint16_t):
71  *(std::uint16_t *)buf = htog<std::uint16_t>(val, endian);
72  return true;
73  case sizeof(std::uint8_t):
74  *(std::uint8_t *)buf = htog<std::uint8_t>(val, endian);
75  return true;
76  default:
77  return false;
78  }
79 }
80 
82 printUintX(const void *buf, std::size_t bytes, ByteOrder endian)
83 {
84  auto [val, success] = getUintX(buf, bytes, endian);
85  if (!success)
86  return {"", false};
87 
88  std::ostringstream out;
89  out << "0x";
90  out.width(2 * bytes);
91  out.fill('0');
92  out.setf(std::ios::hex, std::ios::basefield);
93  out << val;
94 
95  return {out.str(), true};
96 }
97 
98 std::string
99 printByteBuf(const void *buf, std::size_t bytes, ByteOrder endian,
100  std::size_t chunk_size)
101 {
102  assert(buf);
103 
104  std::ostringstream out;
105  out << "[";
106 
107  out.width(2);
108  out.fill('0');
109  out.setf(std::ios::hex, std::ios::basefield);
110 
111  // Bytes that fall outside of a complete chunk. Will always be MSBs.
112  size_t extra = bytes % chunk_size;
113 
114  const uint8_t *ptr = (const uint8_t *)buf;
115  int step = 1;
116 
117  if (endian == ByteOrder::big) {
118  step = -1;
119  ptr = ptr + bytes - 1;
120 
121  // If there's an incomplete chunk, start with that.
122  if (extra) {
123  bytes -= extra;
124  while (extra--) {
125  out.width(2);
126  out << (unsigned)*ptr;
127  ptr += step;
128  }
129  if (bytes)
130  out << " ";
131  }
132  }
133 
134  // Print all the complete chunks.
135  while (bytes >= chunk_size) {
136  for (int i = 0; i < chunk_size; i++) {
137  out.width(2);
138  out << (unsigned)*ptr;
139  ptr += step;
140  }
141  bytes -= chunk_size;
142  if (bytes)
143  out << " ";
144  }
145 
146  // Print any trailing leftovers. Only happens for little endian.
147  while (bytes--) {
148  out.width(2);
149  out << (unsigned)*ptr;
150  ptr += step;
151  }
152 
153  out.width(0);
154  out << "]";
155 
156  return out.str();
157 }
158 
159 } // namespace gem5
STL pair class.
Definition: stl.hh:58
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 63 > val
Definition: misc.hh:776
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
std::pair< std::string, bool > printUintX(const void *buf, std::size_t bytes, ByteOrder endian)
Definition: bufval.cc:82
std::pair< std::uint64_t, bool > getUintX(const void *buf, std::size_t bytes, ByteOrder endian)
Definition: bufval.cc:41
T gtoh(T value, ByteOrder guest_byte_order)
Definition: byteswap.hh:194
bool setUintX(std::uint64_t val, void *buf, std::size_t bytes, ByteOrder endian)
Definition: bufval.cc:59
std::string printByteBuf(const void *buf, std::size_t bytes, ByteOrder endian, std::size_t chunk_size)
Definition: bufval.cc:99

Generated on Wed Dec 21 2022 10:22:39 for gem5 by doxygen 1.9.1