yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
const2ast.cc File Reference
#include "verilog_frontend.h"
#include "kernel/log.h"
#include <string.h>
#include <math.h>
+ Include dependency graph for const2ast.cc:

Go to the source code of this file.

Functions

static int my_decimal_div_by_two (std::vector< uint8_t > &digits)
 
static int my_ilog2 (int x)
 
static void my_strtobin (std::vector< RTLIL::State > &data, const char *str, int len_in_bits, int base, char case_type)
 

Function Documentation

static int my_decimal_div_by_two ( std::vector< uint8_t > &  digits)
static

Definition at line 47 of file const2ast.cc.

48 {
49  int carry = 0;
50  for (size_t i = 0; i < digits.size(); i++) {
51  log_assert(digits[i] < 10);
52  digits[i] += carry * 10;
53  carry = digits[i] % 2;
54  digits[i] /= 2;
55  }
56  while (!digits.empty() && !digits.front())
57  digits.erase(digits.begin());
58  return carry;
59 }
#define log_assert(_assert_expr_)
Definition: log.h:85

+ Here is the caller graph for this function:

static int my_ilog2 ( int  x)
static

Definition at line 62 of file const2ast.cc.

63 {
64  int ret = 0;
65  while (x != 0 && x != -1) {
66  x = x >> 1;
67  ret++;
68  }
69  return ret;
70 }

+ Here is the caller graph for this function:

static void my_strtobin ( std::vector< RTLIL::State > &  data,
const char *  str,
int  len_in_bits,
int  base,
char  case_type 
)
static

Definition at line 73 of file const2ast.cc.

74 {
75  // all digits in string (MSB at index 0)
76  std::vector<uint8_t> digits;
77 
78  while (*str) {
79  if ('0' <= *str && *str <= '9')
80  digits.push_back(*str - '0');
81  else if ('a' <= *str && *str <= 'f')
82  digits.push_back(10 + *str - 'a');
83  else if ('A' <= *str && *str <= 'F')
84  digits.push_back(10 + *str - 'A');
85  else if (*str == 'x' || *str == 'X')
86  digits.push_back(0xf0);
87  else if (*str == 'z' || *str == 'Z')
88  digits.push_back(0xf1);
89  else if (*str == '?')
90  digits.push_back(0xf2);
91  str++;
92  }
93 
94  if (base == 10) {
95  data.clear();
96  if (len_in_bits < 0) {
97  while (!digits.empty())
98  data.push_back(my_decimal_div_by_two(digits) ? RTLIL::S1 : RTLIL::S0);
99  while (data.size() < 32)
100  data.push_back(RTLIL::S0);
101  } else {
102  for (int i = 0; i < len_in_bits; i++)
103  data.push_back(my_decimal_div_by_two(digits) ? RTLIL::S1 : RTLIL::S0);
104  }
105  return;
106  }
107 
108  int bits_per_digit = my_ilog2(base-1);
109  if (len_in_bits < 0)
110  len_in_bits = std::max<int>(digits.size() * bits_per_digit, 32);
111 
112  data.clear();
113  data.resize(len_in_bits);
114 
115  for (int i = 0; i < len_in_bits; i++) {
116  int bitmask = 1 << (i % bits_per_digit);
117  int digitidx = digits.size() - (i / bits_per_digit) - 1;
118  if (digitidx < 0) {
119  if (i > 0 && (data[i-1] == RTLIL::Sz || data[i-1] == RTLIL::Sx || data[i-1] == RTLIL::Sa))
120  data[i] = data[i-1];
121  else
122  data[i] = RTLIL::S0;
123  } else if (digits[digitidx] == 0xf0)
124  data[i] = case_type == 'x' ? RTLIL::Sa : RTLIL::Sx;
125  else if (digits[digitidx] == 0xf1)
126  data[i] = case_type == 'x' || case_type == 'z' ? RTLIL::Sa : RTLIL::Sz;
127  else if (digits[digitidx] == 0xf2)
128  data[i] = RTLIL::Sa;
129  else
130  data[i] = (digits[digitidx] & bitmask) ? RTLIL::S1 : RTLIL::S0;
131  }
132 }
static int my_decimal_div_by_two(std::vector< uint8_t > &digits)
Definition: const2ast.cc:47
static int my_ilog2(int x)
Definition: const2ast.cc:62

+ Here is the call graph for this function:

+ Here is the caller graph for this function: