Belle II Software development
Cosim Namespace Reference

Helper class for software (C++) / firmware (VHDL) co-simulation. More...

Functions

std::string display_value (const char *count, int size)
 Display value of the signal.
 
template<size_t N>
std::string slv_to_bin_string (std::array< char, N > signal, bool padding=false)
 Transform into string.
 
template<size_t N>
void display_hex (const std::array< char, N > &signal)
 Display signal in hex.
 
template<size_t N>
void display_nonzero_hex (const std::array< char, N > &signal)
 Display nonzero value of signal in hex.
 
template<size_t nbits, size_t min, size_t max>
std::bitset< max - min+1 > subset (std::bitset< nbits > set)
 extract a subset of bitstring, like substring.
 

Variables

const char * std_logic_literal [] = {"U", "X", "0", "1", "Z", "W", "L", "H", "-"}
 In case you are not familiar with VHDL simulation, there are 9 possible values defined for the standard logic type, instead of just 0 and 1.
 
const char one_val = 3
 '1' in XSI VHDL simulation
 
const char zero_val = 2
 '0' in XSI VHDL simulation
 

Detailed Description

Helper class for software (C++) / firmware (VHDL) co-simulation.

Function Documentation

◆ display_hex()

void display_hex ( const std::array< char, N > &  signal)

Display signal in hex.

Definition at line 81 of file Cosim.h.

82 {
83 std::ios oldState(nullptr);
84 oldState.copyfmt(std::cout);
85 if (std::any_of(signal.begin(), signal.end(), [](char i)
86 {return i != zero_val && i != one_val;})) {
87 B2WARNING("Some bit in the signal vector is neither 0 nor 1. \n" <<
88 "Displaying binary values instead.");
89 std::cout << slv_to_bin_string(signal) << std::endl;
90 } else {
91 std::string binString = slv_to_bin_string(signal, true);
92 std::cout << std::setfill('0');
93 for (unsigned i = 0; i < signal.size(); i += 4) {
94 std::bitset<4> set(binString.substr(i, 4));
95 std::cout << std::setw(1) << std::hex << set.to_ulong();
96 }
97 }
98 std::cout << "\n";
99 std::cout.copyfmt(oldState);
100 }
std::string slv_to_bin_string(std::array< char, N > signal, bool padding=false)
Transform into string.
Definition: Cosim.h:64

◆ display_nonzero_hex()

void display_nonzero_hex ( const std::array< char, N > &  signal)

Display nonzero value of signal in hex.

Definition at line 104 of file Cosim.h.

105 {
106 if (std::any_of(signal.begin(), signal.end(), [](char i)
107 {return i != zero_val;})) {
108 display_hex(signal);
109 }
110 }
void display_hex(const std::array< char, N > &signal)
Display signal in hex.
Definition: Cosim.h:81

◆ display_value()

std::string display_value ( const char *  count,
int  size 
)

Display value of the signal.

Definition at line 48 of file Cosim.h.

49 {
50 std::string res;
51 for (int i = 0; i < size; i++) {
52 if (count[i] >= 0 && count[i] < 9) {
53 res += std_logic_literal[(int) count[i]];
54 } else {
55 B2WARNING("invalid signal detected: " << static_cast<int>(count[i]));
56 res += "?";
57 }
58 }
59 return res;
60 }
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:40

◆ slv_to_bin_string()

std::string slv_to_bin_string ( std::array< char, N >  signal,
bool  padding = false 
)

Transform into string.

Definition at line 64 of file Cosim.h.

65 {
66 int ini = padding ? signal.size() % 4 : 0;
67 std::string res(ini, '0');
68 for (auto const& bit : signal) {
69 if (bit >= 0 && bit < 9) {
70 res += std_logic_literal[(int) bit];
71 } else {
72 B2WARNING("invalid signal detected: " << static_cast<int>(bit));
73 res += "0";
74 }
75 }
76 return res;
77 }

◆ subset()

std::bitset< max - min+1 > subset ( std::bitset< nbits >  set)

extract a subset of bitstring, like substring.

In principle this can be done using only integer manipulations, but for the sake of simplicity, let's just cast them to string. Beware the endianness. 0 refer to the rightmost bit in std::bitset, but the leftmost bit in std::string

Definition at line 120 of file Cosim.h.

121 {
122 const size_t outWidth = max - min + 1;
123 std::string str = set.to_string();
124 return std::bitset<outWidth>(str.substr(nbits - max - 1, outWidth));
125 }

Variable Documentation

◆ one_val

const char one_val = 3

'1' in XSI VHDL simulation

Definition at line 43 of file Cosim.h.

◆ std_logic_literal

const char* std_logic_literal[] = {"U", "X", "0", "1", "Z", "W", "L", "H", "-"}

In case you are not familiar with VHDL simulation, there are 9 possible values defined for the standard logic type, instead of just 0 and 1.

The simulator needs to compute all these possible outcomes. Therefore, XSI uses a byte, instead of a bit, to represent a std_logic. This is represented with a char with possible values ranging from 0 to 8.

Definition at line 40 of file Cosim.h.

◆ zero_val

const char zero_val = 2

'0' in XSI VHDL simulation

Definition at line 45 of file Cosim.h.