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

Go to the source code of this file.

Data Structures

struct  FsmRecodePass
 

Functions

USING_YOSYS_NAMESPACE static
PRIVATE_NAMESPACE_BEGIN void 
fm_set_fsm_print (RTLIL::Cell *cell, RTLIL::Module *module, FsmData &fsm_data, const char *prefix, FILE *f)
 
static void fsm_recode (RTLIL::Cell *cell, RTLIL::Module *module, FILE *fm_set_fsm_file, std::string default_encoding)
 

Variables

FsmRecodePass FsmRecodePass
 

Function Documentation

USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN void fm_set_fsm_print ( RTLIL::Cell cell,
RTLIL::Module module,
FsmData fsm_data,
const char *  prefix,
FILE *  f 
)
static

Definition at line 33 of file fsm_recode.cc.

34 {
35  std::string name = cell->parameters["\\NAME"].decode_string();
36 
37  fprintf(f, "set_fsm_state_vector {");
38  for (int i = fsm_data.state_bits-1; i >= 0; i--)
39  fprintf(f, " %s_reg[%d]", name[0] == '\\' ? name.substr(1).c_str() : name.c_str(), i);
40  fprintf(f, " } -name {%s_%s} {%s:/WORK/%s}\n", prefix, RTLIL::unescape_id(name).c_str(),
41  prefix, RTLIL::unescape_id(module->name).c_str());
42 
43  fprintf(f, "set_fsm_encoding {");
44  for (int i = 0; i < GetSize(fsm_data.state_table); i++) {
45  fprintf(f, " s%d=2#", i);
46  for (int j = GetSize(fsm_data.state_table[i].bits)-1; j >= 0; j--)
47  fprintf(f, "%c", fsm_data.state_table[i].bits[j] == RTLIL::State::S1 ? '1' : '0');
48  }
49  fprintf(f, " } -name {%s_%s} {%s:/WORK/%s}\n",
50  prefix, RTLIL::unescape_id(name).c_str(),
51  prefix, RTLIL::unescape_id(module->name).c_str());
52 }
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
int state_bits
Definition: fsmdata.h:29
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::IdString name
Definition: rtlil.h:599
std::vector< RTLIL::Const > state_table
Definition: fsmdata.h:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void fsm_recode ( RTLIL::Cell cell,
RTLIL::Module module,
FILE *  fm_set_fsm_file,
std::string  default_encoding 
)
static

Definition at line 54 of file fsm_recode.cc.

55 {
56  std::string encoding = cell->attributes.count("\\fsm_encoding") ? cell->attributes.at("\\fsm_encoding").decode_string() : "auto";
57 
58  log("Recoding FSM `%s' from module `%s' using `%s' encoding:\n", cell->name.c_str(), module->name.c_str(), encoding.c_str());
59 
60  if (encoding != "none" && encoding != "one-hot" && encoding != "binary" && encoding != "auto") {
61  log(" unknown encoding `%s': using auto instead.\n", encoding.c_str());
62  encoding = "auto";
63  }
64 
65  if (encoding == "none") {
66  log(" nothing to do for encoding `none'.\n");
67  return;
68  }
69 
70  FsmData fsm_data;
71  fsm_data.copy_from_cell(cell);
72 
73  if (fm_set_fsm_file != NULL)
74  fm_set_fsm_print(cell, module, fsm_data, "r", fm_set_fsm_file);
75 
76  if (encoding == "auto") {
77  if (!default_encoding.empty())
78  encoding = default_encoding;
79  else
80  encoding = GetSize(fsm_data.state_table) < 32 ? "one-hot" : "binary";
81  log(" mapping auto encoding to `%s` for this FSM.\n", encoding.c_str());
82  }
83 
84  if (encoding == "one-hot") {
85  fsm_data.state_bits = fsm_data.state_table.size();
86  } else
87  if (encoding == "binary") {
88  int new_num_state_bits = ceil(log2(fsm_data.state_table.size()));
89  if (fsm_data.state_bits == new_num_state_bits) {
90  log(" existing encoding is already a packed binary encoding.\n");
91  return;
92  }
93  fsm_data.state_bits = new_num_state_bits;
94  } else
95  log_error("FSM encoding `%s' is not supported!\n", encoding.c_str());
96 
97  int state_idx_counter = fsm_data.reset_state >= 0 ? 1 : 0;
98  for (int i = 0; i < int(fsm_data.state_table.size()); i++)
99  {
100  int state_idx = fsm_data.reset_state == i ? 0 : state_idx_counter++;
101  RTLIL::Const new_code;
102 
103  if (encoding == "one-hot") {
104  new_code = RTLIL::Const(RTLIL::State::Sa, fsm_data.state_bits);
105  new_code.bits[state_idx] = RTLIL::State::S1;
106  } else
107  if (encoding == "binary") {
108  new_code = RTLIL::Const(state_idx, fsm_data.state_bits);
109  } else
110  log_abort();
111 
112  log(" %s -> %s\n", fsm_data.state_table[i].as_string().c_str(), new_code.as_string().c_str());
113  fsm_data.state_table[i] = new_code;
114  }
115 
116  if (fm_set_fsm_file != NULL)
117  fm_set_fsm_print(cell, module, fsm_data, "i", fm_set_fsm_file);
118 
119  fsm_data.copy_to_cell(cell);
120 }
const char * c_str() const
Definition: rtlil.h:178
RTLIL::IdString name
Definition: rtlil.h:853
void copy_to_cell(RTLIL::Cell *cell)
Definition: fsmdata.h:34
void log_error(const char *format,...)
Definition: log.cc:204
#define log_abort()
Definition: log.h:84
int state_bits
Definition: fsmdata.h:29
std::string as_string() const
Definition: rtlil.cc:116
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::IdString name
Definition: rtlil.h:599
#define NULL
std::vector< RTLIL::Const > state_table
Definition: fsmdata.h:32
void log(const char *format,...)
Definition: log.cc:180
void copy_from_cell(RTLIL::Cell *cell)
Definition: fsmdata.h:79
std::vector< RTLIL::State > bits
Definition: rtlil.h:438
USING_YOSYS_NAMESPACE static PRIVATE_NAMESPACE_BEGIN void fm_set_fsm_print(RTLIL::Cell *cell, RTLIL::Module *module, FsmData &fsm_data, const char *prefix, FILE *f)
Definition: fsm_recode.cc:33
int reset_state
Definition: fsmdata.h:29

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation