Belle II Software  release-05-02-19
Cosim.h
1 #pragma once
2 
3 #include <framework/logging/Logger.h>
4 
5 #include <string>
6 #include <iostream>
7 #include <iomanip>
8 #include <bitset>
9 
10 #include <unistd.h>
11 
15 namespace Cosim {
16  /* Note: VHDL std_logic value is stored in a byte (char). The
17  * 9 values are mapped as 'U':0, 'X':1, '0':2, '1':3
18  * 'Z':4, 'W':5, 'L':6, 'H':7, '-':8 . The std_logic_vector
19  * is stored as a contiguous array of bytes. For example
20  * "0101Z" is stored in five bytes as char s[5] = {2,3,2,3,4}
21  * An HDL integer type is stored as C int, a HDL real type is
22  * stored as a C double and a VHDL string type is stored as char*.
23  * An array of HDL integer or double is stored as an array of C
24  * integer or double respectively
25  */
26 
33  const char* std_logic_literal[] = {"U", "X", "0", "1", "Z", "W", "L", "H", "-"};
34 
36  const char one_val = 3;
38  const char zero_val = 2;
39 
41  std::string display_value(const char* count, int size)
42  {
43  std::string res;
44  for (int i = 0; i < size; i++) {
45  if (count[i] >= 0 && count[i] < 9) {
46  res += std_logic_literal[(int) count[i]];
47  } else {
48  B2WARNING("invalid signal detected: " << static_cast<int>(count[i]));
49  res += "?";
50  }
51  }
52  return res;
53  }
54 
56  template<size_t N>
57  std::string slv_to_bin_string(std::array<char, N> signal, bool padding = false)
58  {
59  int ini = padding ? signal.size() % 4 : 0;
60  std::string res(ini, '0');
61  for (auto const& bit : signal) {
62  if (bit >= 0 && bit < 9) {
63  res += std_logic_literal[(int) bit];
64  } else {
65  B2WARNING("invalid signal detected: " << static_cast<int>(bit));
66  res += "0";
67  }
68  }
69  return res;
70  }
71 
73  template<size_t N>
74  void display_hex(const std::array<char, N>& signal)
75  {
76  std::ios oldState(nullptr);
77  oldState.copyfmt(std::cout);
78  if (std::any_of(signal.begin(), signal.end(), [](char i)
79  {return i != zero_val && i != one_val;})) {
80  B2WARNING("Some bit in the signal vector is neither 0 nor 1. \n" <<
81  "Displaying binary values instead.");
82  std::cout << slv_to_bin_string(signal) << std::endl;
83  } else {
84  std::string binString = slv_to_bin_string(signal, true);
85  std::cout << std::setfill('0');
86  for (unsigned i = 0; i < signal.size(); i += 4) {
87  std::bitset<4> set(binString.substr(i, 4));
88  std::cout << std::setw(1) << std::hex << set.to_ulong();
89  }
90  }
91  std::cout << "\n";
92  std::cout.copyfmt(oldState);
93  }
94 
96  template<size_t N>
97  void display_nonzero_hex(const std::array<char, N>& signal)
98  {
99  if (std::any_of(signal.begin(), signal.end(), [](char i)
100  {return i != zero_val;})) {
101  display_hex(signal);
102  }
103  }
104 
112  template<size_t nbits, size_t min, size_t max>
113  std::bitset < max - min + 1 > subset(std::bitset<nbits> set)
114  {
115  const size_t outWidth = max - min + 1;
116  std::string str = set.to_string();
117  return std::bitset<outWidth>(str.substr(nbits - max - 1, outWidth));
118  }
119 }
120 
121 #include <ext/stdio_filebuf.h>
122 
123 using __gnu_cxx::stdio_filebuf;
124 using std::istream;
125 using std::ostream;
126 
127 inline stdio_filebuf<char>* fileBufFromFD(int fd, std::_Ios_Openmode mode)
128 {
129  return (new stdio_filebuf<char> (fd, mode));
130 }
131 
132 istream* createInStreamFromFD(int fd)
133 {
134  stdio_filebuf<char>* fileBuf = fileBufFromFD(fd, std::ios::in);
135  return (new istream(fileBuf));
136 }
137 
138 ostream* createOutStreamFromFD(int fd)
139 {
140  stdio_filebuf<char>* fileBuf = fileBufFromFD(fd, std::ios::out);
141  return (new ostream(fileBuf));
142 }
143 
144 std::string getcurrentdir()
145 {
146  char buf[1024];
147  getcwd(buf, sizeof(buf) - 1);
148  buf[sizeof(buf) - 1] = 0;
149  return buf;
150 }
Cosim::display_nonzero_hex
void display_nonzero_hex(const std::array< char, N > &signal)
Display nonzero value of signal in hex.
Definition: Cosim.h:97
Cosim::slv_to_bin_string
std::string slv_to_bin_string(std::array< char, N > signal, bool padding=false)
Transform into string.
Definition: Cosim.h:57
Cosim
Helper class for software (C++) / firmware (VHDL) co-simulation.
Definition: Cosim.h:15
Cosim::std_logic_literal
const char * std_logic_literal[]
In case you are not familiar with VHDL simulation, there are 9 possible values defined for the standa...
Definition: Cosim.h:33
Cosim::display_hex
void display_hex(const std::array< char, N > &signal)
Display signal in hex.
Definition: Cosim.h:74
Cosim::zero_val
const char zero_val
'0' in XSI VHDL simulation
Definition: Cosim.h:38
Cosim::subset
std::bitset< max - min+1 > subset(std::bitset< nbits > set)
extract a subset of bitstring, like substring.
Definition: Cosim.h:113
Cosim::one_val
const char one_val
'1' in XSI VHDL simulation
Definition: Cosim.h:36
Cosim::display_value
std::string display_value(const char *count, int size)
Display value of the signal.
Definition: Cosim.h:41