Belle II Software development
Cosim.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <framework/logging/Logger.h>
11
12#include <string>
13#include <iostream>
14#include <iomanip>
15#include <bitset>
16
17#include <unistd.h>
18
22namespace Cosim {
23 /* Note: VHDL std_logic value is stored in a byte (char). The
24 * 9 values are mapped as 'U':0, 'X':1, '0':2, '1':3
25 * 'Z':4, 'W':5, 'L':6, 'H':7, '-':8 . The std_logic_vector
26 * is stored as a contiguous array of bytes. For example
27 * "0101Z" is stored in five bytes as char s[5] = {2,3,2,3,4}
28 * An HDL integer type is stored as C int, a HDL real type is
29 * stored as a C double and a VHDL string type is stored as char*.
30 * An array of HDL integer or double is stored as an array of C
31 * integer or double respectively
32 */
33
40 const char* std_logic_literal[] = {"U", "X", "0", "1", "Z", "W", "L", "H", "-"};
41
43 const char one_val = 3;
45 const char zero_val = 2;
46
48 std::string display_value(const char* count, int size)
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 }
61
63 template<size_t N>
64 std::string slv_to_bin_string(std::array<char, N> signal, bool padding = false)
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 }
78
80 template<size_t N>
81 void display_hex(const std::array<char, N>& signal)
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 }
101
103 template<size_t N>
104 void display_nonzero_hex(const std::array<char, N>& signal)
105 {
106 if (std::any_of(signal.begin(), signal.end(), [](char i)
107 {return i != zero_val;})) {
108 display_hex(signal);
109 }
110 }
111
119 template<size_t nbits, size_t min, size_t max>
120 std::bitset < max - min + 1 > subset(std::bitset<nbits> set)
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 }
126}
127
128#include <ext/stdio_filebuf.h>
129
130using __gnu_cxx::stdio_filebuf;
131using std::istream;
132using std::ostream;
133
134inline stdio_filebuf<char>* fileBufFromFD(int fd, std::_Ios_Openmode mode)
135{
136 return (new stdio_filebuf<char> (fd, mode));
137}
138
139istream* createInStreamFromFD(int fd)
140{
141 stdio_filebuf<char>* fileBuf = fileBufFromFD(fd, std::ios::in);
142 return (new istream(fileBuf));
143}
144
145ostream* createOutStreamFromFD(int fd)
146{
147 stdio_filebuf<char>* fileBuf = fileBufFromFD(fd, std::ios::out);
148 return (new ostream(fileBuf));
149}
150
151std::string getcurrentdir()
152{
153 char buf[1024];
154 getcwd(buf, sizeof(buf) - 1);
155 buf[sizeof(buf) - 1] = 0;
156 return buf;
157}
Helper class for software (C++) / firmware (VHDL) co-simulation.
Definition: Cosim.h:22
std::bitset< max - min+1 > subset(std::bitset< nbits > set)
extract a subset of bitstring, like substring.
Definition: Cosim.h:120
const char one_val
'1' in XSI VHDL simulation
Definition: Cosim.h:43
void display_hex(const std::array< char, N > &signal)
Display signal in hex.
Definition: Cosim.h:81
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
void display_nonzero_hex(const std::array< char, N > &signal)
Display nonzero value of signal in hex.
Definition: Cosim.h:104
std::string slv_to_bin_string(std::array< char, N > signal, bool padding=false)
Transform into string.
Definition: Cosim.h:64
std::string display_value(const char *count, int size)
Display value of the signal.
Definition: Cosim.h:48
const char zero_val
'0' in XSI VHDL simulation
Definition: Cosim.h:45