Add CellTypes support for $specify2 and $specify3
[yosys.git] / kernel / satgen.h
index 779c97506fd6aca719e2caf3a863bb1e08cb7603..210cca3f3c89d82b0aca25dbd8da94c7a6f058be 100644 (file)
@@ -1,12 +1,12 @@
-/*
+/* -*- c++ -*-
  *  yosys -- Yosys Open SYnthesis Suite
  *
  *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
- *  
+ *
  *  Permission to use, copy, modify, and/or distribute this software for any
  *  purpose with or without fee is hereby granted, provided that the above
  *  copyright notice and this permission notice appear in all copies.
- *  
+ *
  *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
 YOSYS_NAMESPACE_BEGIN
 
-typedef ezMiniSAT ezDefaultSAT;
+// defined in kernel/register.cc
+extern struct SatSolver *yosys_satsolver_list;
+extern struct SatSolver *yosys_satsolver;
+
+struct SatSolver
+{
+       string name;
+       SatSolver *next;
+       virtual ezSAT *create() = 0;
+
+       SatSolver(string name) : name(name) {
+               next = yosys_satsolver_list;
+               yosys_satsolver_list = this;
+       }
+
+       virtual ~SatSolver() {
+               auto p = &yosys_satsolver_list;
+               while (*p) {
+                       if (*p == this)
+                               *p = next;
+                       else
+                               p = &(*p)->next;
+               }
+               if (yosys_satsolver == this)
+                       yosys_satsolver = yosys_satsolver_list;
+       }
+};
+
+struct ezSatPtr : public std::unique_ptr<ezSAT> {
+       ezSatPtr() : unique_ptr<ezSAT>(yosys_satsolver->create()) { }
+};
 
 struct SatGen
 {
@@ -38,7 +68,9 @@ struct SatGen
        std::string prefix;
        SigPool initial_state;
        std::map<std::string, RTLIL::SigSpec> asserts_a, asserts_en;
+       std::map<std::string, RTLIL::SigSpec> assumes_a, assumes_en;
        std::map<std::string, std::map<RTLIL::SigBit, int>> imported_signals;
+       std::map<std::pair<std::string, int>, bool> initstates;
        bool ignore_div_by_zero;
        bool model_undef;
 
@@ -68,7 +100,7 @@ struct SatGen
                                else
                                        vec.push_back(bit == (undef_mode ? RTLIL::State::Sx : RTLIL::State::S1) ? ez->CONST_TRUE : ez->CONST_FALSE);
                        } else {
-                               std::string name = pf + stringf(bit.wire->width == 1 ?  "%s" : "%s [%d]", RTLIL::id2cstr(bit.wire->name), bit.offset);
+                               std::string name = pf + (bit.wire->width == 1 ? stringf("%s", log_id(bit.wire)) : stringf("%s [%d]", log_id(bit.wire->name), bit.offset));
                                vec.push_back(ez->frozen_literal(name));
                                imported_signals[pf][bit] = vec.back();
                        }
@@ -103,11 +135,25 @@ struct SatGen
                return importSigSpecWorker(bit, pf, false, false).front();
        }
 
+       int importDefSigBit(RTLIL::SigBit bit, int timestep = -1)
+       {
+               log_assert(timestep != 0);
+               std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
+               return importSigSpecWorker(bit, pf, false, true).front();
+       }
+
+       int importUndefSigBit(RTLIL::SigBit bit, int timestep = -1)
+       {
+               log_assert(timestep != 0);
+               std::string pf = "undef:" + prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
+               return importSigSpecWorker(bit, pf, true, false).front();
+       }
+
        bool importedSigBit(RTLIL::SigBit bit, int timestep = -1)
        {
                log_assert(timestep != 0);
                std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
-               return imported_signals[pf].count(bit);
+               return imported_signals[pf].count(bit) != 0;
        }
 
        void getAsserts(RTLIL::SigSpec &sig_a, RTLIL::SigSpec &sig_en, int timestep = -1)
@@ -117,6 +163,13 @@ struct SatGen
                sig_en = asserts_en[pf];
        }
 
+       void getAssumes(RTLIL::SigSpec &sig_a, RTLIL::SigSpec &sig_en, int timestep = -1)
+       {
+               std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
+               sig_a = assumes_a[pf];
+               sig_en = assumes_en[pf];
+       }
+
        int importAsserts(int timestep = -1)
        {
                std::vector<int> check_bits, enable_bits;
@@ -131,6 +184,20 @@ struct SatGen
                return ez->vec_reduce_and(ez->vec_or(check_bits, ez->vec_not(enable_bits)));
        }
 
+       int importAssumes(int timestep = -1)
+       {
+               std::vector<int> check_bits, enable_bits;
+               std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
+               if (model_undef) {
+                       check_bits = ez->vec_and(ez->vec_not(importUndefSigSpec(assumes_a[pf], timestep)), importDefSigSpec(assumes_a[pf], timestep));
+                       enable_bits = ez->vec_and(ez->vec_not(importUndefSigSpec(assumes_en[pf], timestep)), importDefSigSpec(assumes_en[pf], timestep));
+               } else {
+                       check_bits = importDefSigSpec(assumes_a[pf], timestep);
+                       enable_bits = importDefSigSpec(assumes_en[pf], timestep);
+               }
+               return ez->vec_reduce_and(ez->vec_or(check_bits, ez->vec_not(enable_bits)));
+       }
+
        int signals_eq(RTLIL::SigSpec lhs, RTLIL::SigSpec rhs, int timestep_lhs = -1, int timestep_rhs = -1)
        {
                if (timestep_rhs < 0)
@@ -200,6 +267,13 @@ struct SatGen
                ez->assume(ez->OR(undef, ez->IFF(y, yy)));
        }
 
+       void setInitState(int timestep)
+       {
+               auto key = make_pair(prefix, timestep);
+               log_assert(initstates.count(key) == 0 || initstates.at(key) == true);
+               initstates[key] = true;
+       }
+
        bool importCell(RTLIL::Cell *cell, int timestep = -1)
        {
                bool arith_undef_handled = false;
@@ -236,7 +310,7 @@ struct SatGen
                        arith_undef_handled = true;
                }
 
-               if (cell->type.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_",
+               if (cell->type.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_", "$_ANDNOT_", "$_ORNOT_",
                                "$and", "$or", "$xor", "$xnor", "$add", "$sub"))
                {
                        std::vector<int> a = importDefSigSpec(cell->getPort("\\A"), timestep);
@@ -258,6 +332,10 @@ struct SatGen
                                ez->assume(ez->vec_eq(ez->vec_xor(a, b), yy));
                        if (cell->type == "$xnor" || cell->type == "$_XNOR_")
                                ez->assume(ez->vec_eq(ez->vec_not(ez->vec_xor(a, b)), yy));
+                       if (cell->type == "$_ANDNOT_")
+                               ez->assume(ez->vec_eq(ez->vec_and(a, ez->vec_not(b)), yy));
+                       if (cell->type == "$_ORNOT_")
+                               ez->assume(ez->vec_eq(ez->vec_or(a, ez->vec_not(b)), yy));
                        if (cell->type == "$add")
                                ez->assume(ez->vec_eq(ez->vec_add(a, b), yy));
                        if (cell->type == "$sub")
@@ -286,6 +364,19 @@ struct SatGen
                                        std::vector<int> yX = ez->vec_or(undef_a, undef_b);
                                        ez->assume(ez->vec_eq(yX, undef_y));
                                }
+                               else if (cell->type == "$_ANDNOT_") {
+                                       std::vector<int> a0 = ez->vec_and(ez->vec_not(a), ez->vec_not(undef_a));
+                                       std::vector<int> b1 = ez->vec_and(b, ez->vec_not(undef_b));
+                                       std::vector<int> yX = ez->vec_and(ez->vec_or(undef_a, undef_b), ez->vec_not(ez->vec_or(a0, b1)));
+                                       ez->assume(ez->vec_eq(yX, undef_y));
+                               }
+
+                               else if (cell->type == "$_ORNOT_") {
+                                       std::vector<int> a1 = ez->vec_and(a, ez->vec_not(undef_a));
+                                       std::vector<int> b0 = ez->vec_and(ez->vec_not(b), ez->vec_not(undef_b));
+                                       std::vector<int> yX = ez->vec_and(ez->vec_or(undef_a, undef_b), ez->vec_not(ez->vec_or(a1, b0)));
+                                       ez->assume(ez->vec_eq(yX, undef_y));
+                               }
                                else
                                        log_abort();
 
@@ -433,11 +524,7 @@ struct SatGen
                                std::vector<int> undef_s = importUndefSigSpec(cell->getPort("\\S"), timestep);
                                std::vector<int> undef_y = importUndefSigSpec(cell->getPort("\\Y"), timestep);
 
-                               int maybe_one_hot = ez->CONST_FALSE;
-                               int maybe_many_hot = ez->CONST_FALSE;
-
-                               int sure_one_hot = ez->CONST_FALSE;
-                               int sure_many_hot = ez->CONST_FALSE;
+                               int maybe_a = ez->CONST_TRUE;
 
                                std::vector<int> bits_set = std::vector<int>(undef_y.size(), ez->CONST_FALSE);
                                std::vector<int> bits_clr = std::vector<int>(undef_y.size(), ez->CONST_FALSE);
@@ -450,18 +537,12 @@ struct SatGen
                                        int maybe_s = ez->OR(s.at(i), undef_s.at(i));
                                        int sure_s = ez->AND(s.at(i), ez->NOT(undef_s.at(i)));
 
-                                       maybe_one_hot = ez->OR(maybe_one_hot, maybe_s);
-                                       maybe_many_hot = ez->OR(maybe_many_hot, ez->AND(maybe_one_hot, maybe_s));
-
-                                       sure_one_hot = ez->OR(sure_one_hot, sure_s);
-                                       sure_many_hot = ez->OR(sure_many_hot, ez->AND(sure_one_hot, sure_s));
+                                       maybe_a = ez->AND(maybe_a, ez->NOT(sure_s));
 
-                                       bits_set = ez->vec_ite(maybe_s, ez->vec_or(bits_set, ez->vec_or(bits_set, ez->vec_or(part_of_b, part_of_undef_b))), bits_set);
-                                       bits_clr = ez->vec_ite(maybe_s, ez->vec_or(bits_clr, ez->vec_or(bits_clr, ez->vec_or(ez->vec_not(part_of_b), part_of_undef_b))), bits_clr);
+                                       bits_set = ez->vec_ite(maybe_s, ez->vec_or(bits_set, ez->vec_or(part_of_b, part_of_undef_b)), bits_set);
+                                       bits_clr = ez->vec_ite(maybe_s, ez->vec_or(bits_clr, ez->vec_or(ez->vec_not(part_of_b), part_of_undef_b)), bits_clr);
                                }
 
-                               int maybe_a = ez->NOT(maybe_one_hot);
-
                                bits_set = ez->vec_ite(maybe_a, ez->vec_or(bits_set, ez->vec_or(bits_set, ez->vec_or(a, undef_a))), bits_set);
                                bits_clr = ez->vec_ite(maybe_a, ez->vec_or(bits_clr, ez->vec_or(bits_clr, ez->vec_or(ez->vec_not(a), undef_a))), bits_clr);
 
@@ -914,7 +995,7 @@ struct SatGen
                                                div_zero_result.insert(div_zero_result.end(), y.size() - div_zero_result.size(), ez->CONST_FALSE);
                                        }
                                } else {
-                                       int copy_a_bits = std::min(cell->getPort("\\A").size(), cell->getPort("\\B").size());
+                                       int copy_a_bits = min(cell->getPort("\\A").size(), cell->getPort("\\B").size());
                                        div_zero_result.insert(div_zero_result.end(), a.begin(), a.begin() + copy_a_bits);
                                        if (cell->parameters["\\A_SIGNED"].as_bool() && cell->parameters["\\B_SIGNED"].as_bool())
                                                div_zero_result.insert(div_zero_result.end(), y.size() - div_zero_result.size(), div_zero_result.back());
@@ -982,6 +1063,88 @@ struct SatGen
                        return true;
                }
 
+               if (cell->type == "$sop")
+               {
+                       std::vector<int> a = importDefSigSpec(cell->getPort("\\A"), timestep);
+                       int y = importDefSigSpec(cell->getPort("\\Y"), timestep).at(0);
+
+                       int width = cell->getParam("\\WIDTH").as_int();
+                       int depth = cell->getParam("\\DEPTH").as_int();
+
+                       vector<State> table_raw = cell->getParam("\\TABLE").bits;
+                       while (GetSize(table_raw) < 2*width*depth)
+                               table_raw.push_back(State::S0);
+
+                       vector<vector<int>> table(depth);
+
+                       for (int i = 0; i < depth; i++)
+                       for (int j = 0; j < width; j++)
+                       {
+                               bool pat0 = (table_raw[2*width*i + 2*j + 0] == State::S1);
+                               bool pat1 = (table_raw[2*width*i + 2*j + 1] == State::S1);
+
+                               if (pat0 && !pat1)
+                                       table.at(i).push_back(0);
+                               else if (!pat0 && pat1)
+                                       table.at(i).push_back(1);
+                               else
+                                       table.at(i).push_back(-1);
+                       }
+
+                       if (model_undef)
+                       {
+                               std::vector<int> products, undef_products;
+                               std::vector<int> undef_a = importUndefSigSpec(cell->getPort("\\A"), timestep);
+                               int undef_y = importUndefSigSpec(cell->getPort("\\Y"), timestep).at(0);
+
+                               for (int i = 0; i < depth; i++)
+                               {
+                                       std::vector<int> cmp_a, cmp_ua, cmp_b;
+
+                                       for (int j = 0; j < width; j++)
+                                               if (table.at(i).at(j) >= 0) {
+                                                       cmp_a.push_back(a.at(j));
+                                                       cmp_ua.push_back(undef_a.at(j));
+                                                       cmp_b.push_back(table.at(i).at(j) ? ez->CONST_TRUE : ez->CONST_FALSE);
+                                               }
+
+                                       std::vector<int> masked_a = ez->vec_or(cmp_a, cmp_ua);
+                                       std::vector<int> masked_b = ez->vec_or(cmp_b, cmp_ua);
+
+                                       int masked_eq = ez->vec_eq(masked_a, masked_b);
+                                       int any_undef = ez->expression(ezSAT::OpOr, cmp_ua);
+
+                                       undef_products.push_back(ez->AND(any_undef, masked_eq));
+                                       products.push_back(ez->AND(ez->NOT(any_undef), masked_eq));
+                               }
+
+                               int yy = ez->expression(ezSAT::OpOr, products);
+                               ez->SET(undef_y, ez->AND(ez->NOT(yy), ez->expression(ezSAT::OpOr, undef_products)));
+                               undefGating(y, yy, undef_y);
+                       }
+                       else
+                       {
+                               std::vector<int> products;
+
+                               for (int i = 0; i < depth; i++)
+                               {
+                                       std::vector<int> cmp_a, cmp_b;
+
+                                       for (int j = 0; j < width; j++)
+                                               if (table.at(i).at(j) >= 0) {
+                                                       cmp_a.push_back(a.at(j));
+                                                       cmp_b.push_back(table.at(i).at(j) ? ez->CONST_TRUE : ez->CONST_FALSE);
+                                               }
+
+                                       products.push_back(ez->vec_eq(cmp_a, cmp_b));
+                               }
+
+                               ez->SET(y, ez->expression(ezSAT::OpOr, products));
+                       }
+
+                       return true;
+               }
+
                if (cell->type == "$fa")
                {
                        std::vector<int> a = importDefSigSpec(cell->getPort("\\A"), timestep);
@@ -1137,7 +1300,7 @@ struct SatGen
                        return true;
                }
 
-               if (timestep > 0 && (cell->type == "$dff" || cell->type == "$_DFF_N_" || cell->type == "$_DFF_P_"))
+               if (timestep > 0 && cell->type.in("$ff", "$dff", "$_FF_", "$_DFF_N_", "$_DFF_P_"))
                {
                        if (timestep == 1)
                        {
@@ -1163,6 +1326,71 @@ struct SatGen
                        return true;
                }
 
+               if (cell->type == "$anyconst")
+               {
+                       if (timestep < 2)
+                               return true;
+
+                       std::vector<int> d = importDefSigSpec(cell->getPort("\\Y"), timestep-1);
+                       std::vector<int> q = importDefSigSpec(cell->getPort("\\Y"), timestep);
+
+                       std::vector<int> qq = model_undef ? ez->vec_var(q.size()) : q;
+                       ez->assume(ez->vec_eq(d, qq));
+
+                       if (model_undef)
+                       {
+                               std::vector<int> undef_d = importUndefSigSpec(cell->getPort("\\Y"), timestep-1);
+                               std::vector<int> undef_q = importUndefSigSpec(cell->getPort("\\Y"), timestep);
+
+                               ez->assume(ez->vec_eq(undef_d, undef_q));
+                               undefGating(q, qq, undef_q);
+                       }
+                       return true;
+               }
+
+               if (cell->type == "$anyseq")
+               {
+                       return true;
+               }
+
+               if (cell->type == "$_BUF_" || cell->type == "$equiv")
+               {
+                       std::vector<int> a = importDefSigSpec(cell->getPort("\\A"), timestep);
+                       std::vector<int> y = importDefSigSpec(cell->getPort("\\Y"), timestep);
+                       extendSignalWidthUnary(a, y, cell);
+
+                       std::vector<int> yy = model_undef ? ez->vec_var(y.size()) : y;
+                       ez->assume(ez->vec_eq(a, yy));
+
+                       if (model_undef) {
+                               std::vector<int> undef_a = importUndefSigSpec(cell->getPort("\\A"), timestep);
+                               std::vector<int> undef_y = importUndefSigSpec(cell->getPort("\\Y"), timestep);
+                               extendSignalWidthUnary(undef_a, undef_y, cell, false);
+                               ez->assume(ez->vec_eq(undef_a, undef_y));
+                               undefGating(y, yy, undef_y);
+                       }
+                       return true;
+               }
+
+               if (cell->type == "$initstate")
+               {
+                       auto key = make_pair(prefix, timestep);
+                       if (initstates.count(key) == 0)
+                               initstates[key] = false;
+
+                       std::vector<int> y = importDefSigSpec(cell->getPort("\\Y"), timestep);
+                       log_assert(GetSize(y) == 1);
+                       ez->SET(y[0], initstates[key] ? ez->CONST_TRUE : ez->CONST_FALSE);
+
+                       if (model_undef) {
+                               std::vector<int> undef_y = importUndefSigSpec(cell->getPort("\\Y"), timestep);
+                               log_assert(GetSize(undef_y) == 1);
+                               ez->SET(undef_y[0], ez->CONST_FALSE);
+                       }
+
+                       return true;
+               }
+
                if (cell->type == "$assert")
                {
                        std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
@@ -1171,6 +1399,14 @@ struct SatGen
                        return true;
                }
 
+               if (cell->type == "$assume")
+               {
+                       std::string pf = prefix + (timestep == -1 ? "" : stringf("@%d:", timestep));
+                       assumes_a[pf].append((*sigmap)(cell->getPort("\\A")));
+                       assumes_en[pf].append((*sigmap)(cell->getPort("\\EN")));
+                       return true;
+               }
+
                // Unsupported internal cell types: $pow $lut
                // .. and all sequential cells except $dff and $_DFF_[NP]_
                return false;