Skip to content

Commit 980b450

Browse files
authored
Merge pull request #1663 from antmicro/eblif_params
EBLIF cell parameter interpretation
2 parents 58f5413 + 1cbc9b9 commit 980b450

File tree

5 files changed

+112
-6
lines changed

5 files changed

+112
-6
lines changed

doc/src/vpr/file_formats.rst

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,36 @@ The ``.param`` statement allows parameters (e.g. primitive modes) to be tagged o
391391

392392
.. note:: ``.param`` statements apply to the previous primitive instantiation.
393393

394+
Parameters can have one of the three available types. Type is inferred from the format in which a parameter is provided.
395+
396+
* **string**
397+
Whenever a parameter value is quoted it is considered to be a string. BLIF parser does not allow escaped characters hence those are illegal and will cause syntax errors.
398+
399+
* **binary word**
400+
Binary words are specified using strings of characters ``0`` and ``1``. No other characters are allowed. Number of characters denotes the word length.
401+
402+
* **real number**
403+
Real numbers are stored as decimals where the dot ``.`` character separates the integer and fractional part. Presence of the dot character implies that the value is to be treated as a real number.
404+
394405
For example:
395406

396407
.. code-block:: none
397408
398-
.subckt dsp a=a_in b=b_in cin=c_in cout=c_out s=sum_out
399-
.param mode adder
409+
.subckt pll clk_in=gclk clk_out=pclk
410+
.param feedback "internal"
411+
.param multiplier 0.50
412+
.param power 001101
400413
401-
Would set the parameter ``mode`` of the above ``dsp`` ``.subckt`` to ``adder``.
414+
Would set the parameters ``feedback``, ``multiplier`` and ``power`` of the above ``pll`` ``.subckt`` to ``"internal"``, ``0.50`` and ``001101`` respectively.
415+
416+
.. warning:: Integers in notation other than binary (e.g. decimal, hexadecimal) are not supported. Occurrence of params with digits other than 1 and 0 for binary words, not quoted (strings) or not separated with dot ``.`` (real numbers) are considered to be illegal.
417+
418+
Interpretation of parameter values is out of scope of the BLIF format extension.
402419

403420
``.param`` statements propagate to ``<parameter>`` elements in the packed netlist.
404421

422+
Paramerer values propagate also to the post-route Verilog netlist, if it is generated. Strings and real numbers are passed directly while binary words are prepended with the ``<N>'b`` prefix where ``N`` denotes a binary word length.
423+
405424
.attr
406425
~~~~~
407426
The ``.attr`` statement allows attributes (e.g. source file/line) to be tagged on BLIF primitives.

vpr/src/base/netlist_writer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "vpr_error.h"
2121
#include "vpr_types.h"
2222

23+
#include "read_blif.h"
24+
2325
#include "netlist_walker.h"
2426
#include "netlist_writer.h"
2527

@@ -625,7 +627,13 @@ class BlackBoxInst : public Instance {
625627

626628
//Verilog parameters
627629
for (auto iter = params_.begin(); iter != params_.end(); ++iter) {
628-
os << indent(depth + 1) << "." << iter->first << "(" << iter->second << ")";
630+
/* Prepend a prefix if needed */
631+
std::stringstream prefix;
632+
if (is_binary_param(iter->second)) {
633+
prefix << iter->second.length() << "'b";
634+
}
635+
636+
os << indent(depth + 1) << "." << iter->first << "(" << prefix.str() << iter->second << ")";
629637
if (iter != --params_.end()) {
630638
os << ",";
631639
}

vpr/src/base/read_blif.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <sstream>
2121
#include <unordered_set>
2222
#include <cctype> //std::isdigit
23+
#include <algorithm>
24+
#include <regex>
2325

2426
#include "blifparse.hpp"
2527
#include "atom_netlist.h"
@@ -386,6 +388,14 @@ struct BlifAllocCallback : public blifparse::Callback {
386388
parse_error(lineno_, ".param", "Supported only in extended BLIF format");
387389
}
388390

391+
// Validate the parameter value
392+
bool is_valid = is_string_param(value) || is_binary_param(value) || is_real_param(value);
393+
394+
if (!is_valid) {
395+
std::string msg = "Incorrect parameter '" + name + "' value specification. Value '" + value + "' is not recognized as string, binary word or real number. Possible causes:\n\t* lack or inconsistency in quotes (string)\n\t* no dot '.' to separate integer and fractional part (real number)\n\t* use of characters other than '1' and '0' (binary word)";
396+
parse_error(lineno_, ".param", msg);
397+
}
398+
389399
curr_model().set_block_param(curr_block(), name, value);
390400
}
391401

@@ -666,6 +676,67 @@ vtr::LogicValue to_vtr_logic_value(blifparse::LogicValue val) {
666676
return new_val;
667677
}
668678

679+
bool is_string_param(const std::string& param) {
680+
/* Empty param is considered a string */
681+
if (param.empty()) {
682+
return true;
683+
}
684+
685+
/* There have to be at least 2 characters (the quotes) */
686+
if (param.length() < 2) {
687+
return false;
688+
}
689+
690+
/* The first and the last characters must be quotes */
691+
size_t len = param.length();
692+
if (param[0] != '"' || param[len - 1] != '"') {
693+
return false;
694+
}
695+
696+
/* There mustn't be any other quotes except for escaped ones */
697+
for (size_t i = 1; i < (len - 1); ++i) {
698+
if (param[i] == '"' && param[i - 1] != '\\') {
699+
return false;
700+
}
701+
}
702+
703+
/* This is a string param */
704+
return true;
705+
}
706+
707+
bool is_binary_param(const std::string& param) {
708+
/* Must be non-empty */
709+
if (param.empty()) {
710+
return false;
711+
}
712+
713+
/* The string must contain only '0' and '1' */
714+
for (size_t i = 0; i < param.length(); ++i) {
715+
if (param[i] != '0' && param[i] != '1') {
716+
return false;
717+
}
718+
}
719+
720+
/* This is a binary word param */
721+
return true;
722+
}
723+
724+
bool is_real_param(const std::string& param) {
725+
/* Must be non-empty */
726+
if (param.empty()) {
727+
return false;
728+
}
729+
730+
/* The string must match the regular expression */
731+
static const std::regex real_number_expr("[+-]?([0-9]*\\.[0-9]+)|([0-9]+\\.[0-9]*)");
732+
if (!std::regex_match(param, real_number_expr)) {
733+
return false;
734+
}
735+
736+
/* This is a real number param */
737+
return true;
738+
}
739+
669740
AtomNetlist read_blif(e_circuit_format circuit_format,
670741
const char* blif_file,
671742
const t_model* user_models,

vpr/src/base/read_blif.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include "atom_netlist_fwd.h"
55
#include "read_circuit.h"
66

7+
bool is_string_param(const std::string& param);
8+
bool is_binary_param(const std::string& param);
9+
bool is_real_param(const std::string& param);
10+
711
AtomNetlist read_blif(e_circuit_format circuit_format,
812
const char* blif_file,
913
const t_model* user_models,

vtr_flow/benchmarks/tests/test_eblif.eblif

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
.names a b a_and_b
66
11 1
77
.cname lut_a_and_b
8-
.param test_names_param "test_names_param_value"
8+
.param test_names_param_str "test_names_param_str_value"
9+
.param test_names_param_bin 00110101
10+
.param test_names_param_num 2.0
911
.attr test_names_attrib "test_names_param_attrib"
1012

1113
.latch a_and_b dff_q re clk 0
1214
.cname my_dff
13-
.param test_latch_param "test_latch_param_value"
15+
.param test_latch_param "test_latch_param_str_value"
16+
.param test_latch_param_bin 00110101
17+
.param test_latch_param_num 2.0
1418
.attr test_latch_attrib "test_latch_param_attrib"
1519

1620
.conn dff_q o_dff

0 commit comments

Comments
 (0)