C++自动testbench

5 篇文章 4 订阅
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>

void generate_testbench()
{
    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1));
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open) //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2)
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();
    // std::vector<std::string> parameter_type;

    std::string module_name;
    struct singal
    {
        std::string flag;
        std::string type;
        std::string width;
        std::string name;
    };
    std::vector<singal> singal_s;
    // struct parameter
    // {
    //     std::string name;
    //     std::string width;
    // };
    std::vector<std::string> parameter_s;
    struct internal_singal
    {
        std::string type;
        std::string name;
        std::string width;
    };
    std::vector<internal_singal> internal_singal_s;
    bool module_name_end = false;
    std::vector<std::string> parameter_sentence;
    std::vector<std::string> internal_blcok_signal_s;
    for (const auto &cur_s : s_s)
    {

        if (!module_name_end)
        {
            if (cur_s.length() > 5)
            {
                auto it = cur_s.cbegin();
                if (*it == 'm' && *(it + 1) == 'o' && *(it + 2) == 'd' && *(it + 3) == 'u' && *(it + 4) == 'l' && *(it + 5) == 'e')
                {
                    it += 6;
                    for (; it != cur_s.cend() && *it != '(' && *it != '#'; ++it)
                        module_name.push_back(*it);
                }
                else if ((*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'p' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'o' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'o' && *(it + 1) == 'u' && *(it + 2) == 't' && *(it + 3) == 'p' && *(it + 4) == 'u' && *(it + 5) == 't'))
                {
                    singal cur_singal;
                    if (*it == 'i')
                    {
                        if (*(it + 2) == 'p')
                            cur_singal.flag = "input";
                        else
                            cur_singal.flag = "inout";
                        it += 5;
                    }
                    else
                    {
                        cur_singal.flag = "output";
                        it += 6;
                    }

                    if (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g') //缺省时信号名也可以r或者w打头
                    {
                        cur_singal.type = "reg";
                        it += 3;
                    }
                    else
                    {
                        cur_singal.type = "wire";
                        if (*it == 'w' && *(it + 2) == 'i' && *(it + 1) == 'r' && *(it + 1) == 'e') //缺省时信号名也可以r或者w打头
                            it += 4;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                        cur_singal.name.push_back(*it);

                    singal_s.push_back(cur_singal);
                }

                it = cur_s.begin();
                for (auto it_02 = it; it_02 != cur_s.end(); ++it_02)
                {
                    if (*it_02 == '=')
                    {
                        if ((it_02 - it) > 8)
                        {
                            auto it_03 = it;
                            for (; (it_03 + 8) != it_02; ++it_03)
                                if (*it_03 == 'p' && *(it_03 + 1) == 'a' && *(it_03 + 2) == 'r' && *(it_03 + 3) == 'a' && *(it_03 + 4) == 'm' && *(it_03 + 5) == 'e' && *(it_03 + 6) == 't' && *(it_03 + 7) == 'e' && *(it_03 + 8) == 'r')
                                {
                                    it = it_03 + 10;

                                    std::string cur_parameter_name;
                                    for (; it != it_02; ++it)
                                        cur_parameter_name.push_back(*it);
                                    parameter_s.push_back(cur_parameter_name);

                                    parameter_sentence.push_back(cur_s);
                                    break;
                                }
                        }
                    }
                }
            }
        }

        auto it = cur_s.end() - 2;
        if (*it == ')' && *(it + 1) == ';')
        {
            module_name_end = true;
        }

        if (module_name_end)
        {
            if (cur_s.length() > 5)
            {
                auto it = cur_s.cbegin();
                if ((*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'p' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                    (*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'o' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                    (*it == 'o' && *(it + 1) == 'u' && *(it + 2) == 't' && *(it + 3) == 'p' && *(it + 4) == 'u' && *(it + 5) == 't'))
                {
                    singal cur_singal;
                    if (*it == 'i')
                    {
                        if (*(it + 2) == 'p')
                            cur_singal.flag = "input";
                        else
                            cur_singal.flag = "inout";
                        it += 5;
                    }
                    else
                    {
                        cur_singal.flag = "output";
                        it += 6;
                    }

                    if (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g') //缺省时信号名也可以r或者w打头
                    {
                        cur_singal.type = "reg";
                        it += 3;
                    }
                    else
                    {
                        cur_singal.type = "wire";
                        if (*it == 'w' && *(it + 2) == 'i' && *(it + 1) == 'r' && *(it + 1) == 'e') //缺省时信号名也可以r或者w打头
                            it += 4;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                        cur_singal.name.push_back(*it);

                    singal_s.push_back(cur_singal);
                }
            }

            auto it = cur_s.cbegin();
            if ((*it == 'w' && *(it + 1) == 'i' && *(it + 2) == 'r' && *(it + 3) == 'e') || (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g'))
            {
                internal_singal cur_internal_singal;
                if (*it == 'w')
                {
                    cur_internal_singal.type = "wire";
                    it += 4;
                }
                else if (*it == 'r')
                {
                    cur_internal_singal.type = "reg";
                    it += 3;
                }

                if (*it == '[')
                    for (; *(it - 1) != ']'; ++it)
                        cur_internal_singal.width.push_back(*it);

                for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                    cur_internal_singal.name.push_back(*it);

                internal_singal_s.push_back(cur_internal_singal);
            }
            else if (*it == '.')
            {
                std::string cur_internal_blcok_signal;
                for (; *it != '('; ++it)
                    ;
                ++it;
                for (; *it != ')'; ++it)
                    cur_internal_blcok_signal.push_back(*it);
                internal_blcok_signal_s.push_back(cur_internal_blcok_signal);
            }
        }
    }

    system("CLS");
    int max_singal_length = 0;
    for (const auto &it : singal_s)
        if (it.name.length() > max_singal_length)
            max_singal_length = it.name.length();
    bool occur_flag = false;
    for (const auto &it : internal_blcok_signal_s)
    {
        bool occur = false;
        for (const auto &it_1 : singal_s)
            if (it_1.name == it)
                occur = true;
        for (const auto &it_1 : internal_singal_s)
            if (it_1.name == it)
                occur = true;
        // if (!occur && !it.empty())
        // {
        //     if (!occur_flag)
        //         std::cout << "warning:   " << '\n';
        //     occur_flag = true;
        //     std::cout << "    " << it << " undefined" << '\n';
        // }
    }
    // if (occur_flag)
    //     std::cout << '\n';

    std::cout << "module " << module_name + "_tb();" << '\n'
              << '\n';
    std::cout << "    localparam period = 10;" << '\n'
              << '\n';
    if (!parameter_s.empty())
        for (auto it = parameter_sentence.cbegin(); it != parameter_sentence.cend() - 1; ++it)
        {
            std::cout << "    ";
            for (auto it_1 = (*it).cbegin(); it_1 != (*it).cend() - 1; ++it_1)
            {
                std::cout << *it_1;
                if (*(it_1 + 1) == '=' || *(it_1) == '=' || it_1 - (*it).cbegin() == 8)
                    std::cout << ' ';
            }
            std::cout << ';';
            std::cout << '\n';
        }
    for (const auto &it : singal_s)
    {
        if (it.flag[0] == 'i')
            std::cout << "    reg " << it.width << ' ' << it.name << ";" << '\n';
        else
            std::cout << "    wire " << it.width << ' ' << it.name << ";" << '\n';
    }
    std::cout << '\n';
    std::cout << "    " << module_name << " ";
    if (!parameter_s.empty())
    {
        std::cout << " #(" << '\n';
        for (auto it = parameter_s.cbegin(); it != parameter_s.cend(); ++it)
            if (it != parameter_s.cend() - 1)
                std::cout << "        ." << *it << " ()," << '\n';
            else
            {
                std::cout << "        ." << *it << " ()" << '\n';
                std::cout << "        )" << '\n';
            }
        std::cout << "    "
                  << "inst_" + module_name << '(' << '\n';
    }
    else
        std::cout << "inst_" + module_name << '(' << '\n';
    for (auto it = singal_s.cbegin(); it != singal_s.cend(); ++it)
    {
        std::cout << "        ." << (*it).name;
        for (int i = 0; i < max_singal_length - (*it).name.length(); ++i)
            std::cout << " ";
        std::cout << " (" << (*it).name << ")";
        if (it != singal_s.cend() - 1)
            std::cout << ',' << '\n';
        else
            std::cout << '\n'
                      << "    );" << '\n';
    }
    std::cout << '\n';

    std::cout << "    initial begin" << '\n';
    for (const auto &it : singal_s)
        if (it.name.find("clk") != std::string::npos)
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
    for (const auto &it : singal_s)
        if (it.flag[0] == 'i')
        {
            if (it.name.find("clk") == std::string::npos && it.name.find("rst") == std::string::npos)
            {
                if (it.width.empty())
                    std::cout << "        " << it.name << " = 1'b0;" << '\n';
                else
                {
                    int width = 0;
                    for (auto it_2 = it.width.begin() + 1; *it_2 != ':'; ++it_2)
                    {
                        width *= 10;
                        width += *it_2 - '0';
                    }
                    width += 1;
                    if (width <= 8)
                        std::cout
                            << "        " << it.name << " = " << width << "'b0;" << '\n';
                    else
                        std::cout
                            << "        " << it.name << " = " << width << "'h0;" << '\n';
                }
            }
        }
    std::cout << '\n';
    for (const auto &it : singal_s)
        if (it.name.find("rst_n") != std::string::npos)
        {
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            break;
        }
        else if (it.name.find("rst") != std::string::npos)
        {
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
            break;
        }

    std::cout << '\n';
    std::cout << "        #(period * 10)" << '\n';
    for (const auto &it : singal_s)
        if (it.flag[0] == 'i')
        {
            if (it.name.find("clk") == std::string::npos && it.name.find("rst") == std::string::npos)
            {
                if (it.width.empty())
                    std::cout << "        " << it.name << " = 1'b;" << '\n';
                else
                {
                    int width = 0;
                    for (auto it_2 = it.width.begin() + 1; *it_2 != ':'; ++it_2)
                    {
                        width *= 10;
                        width += *it_2 - '0';
                    }
                    width += 1;
                    if (width <= 8)
                        std::cout
                            << "        " << it.name << " = " << width << "'b;" << '\n';
                    else
                        std::cout
                            << "        " << it.name << " = " << width << "'h;" << '\n';
                }
            }
        }
    std::cout << '\n'
              << "        #(period)" << '\n';
    std::cout << "    end" << '\n';
    std::cout << '\n';

    /* for (const auto &it : singal_s)
    {
        int clk_num = 0;
        if (it.name.find("clk") != std::string::npos)
        {
            ++clk_num;
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            std::cout << "            always# ( period / 2 ) " << it.name << " = ~ " << it.name << ";" << '\n';
            if (clk_num == 1)
            {
                std::cout << "        for(i =0 ;i<499 ;i=i+1) begin" << '\n';
                std::cout << "            if( i ==  ) begin" << '\n'
                          << '\n';
                std::cout << "            end" << '\n';
                std::cout << "            #period;" << '\n';
                //std::cout << "            # ( period / 2 ) clk_i = ~ clk_i;" << '\n';
                //std::cout << "            # ( period / 2 ) clk_i = ~ clk_i;" << '\n';
                std::cout << "        end" << '\n';
            }

            //break;
        }
    }
    std::cout << '\n'; */

    for (const auto &it : singal_s)
        if (it.name.find("clk") != std::string::npos)
            std::cout << "    always #(period / 2) " << it.name << " = ~" << it.name << ";" << '\n';
    std::cout << '\n';
    std::cout << "endmodule" << '\n';
    /*  std::cout << module_name << '\n';
    for (auto &it : singal_s)
    {
        std::cout << it.type << '\n';
        std::cout << it.width << '\n';
        std::cout << it.name << '\n';
    } */
    for (const auto it : internal_singal_s)
    {
        std::cout << "    output " << it.width << " " << it.name << "_test," << '\n';
    }
    for (const auto it : internal_singal_s)
    {
        std::cout << "    assign " << it.name << "_test = " << it.name << ";" << '\n';
    }

    // system("CLS");
    //  generate_testbench();
}
void gen_inst()
{
    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1));
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open) //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2)
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();
    // std::vector<std::string> parameter_type;

    std::string module_name;
    struct singal
    {
        std::string flag;
        std::string type;
        std::string width;
        std::string name;
    };
    std::vector<singal> singal_s;
    // struct parameter
    // {
    //     std::string name;
    //     std::string width;
    // };
    std::vector<std::string> parameter_s;
    struct internal_singal
    {
        std::string type;
        std::string name;
        std::string width;
    };
    std::vector<internal_singal> internal_singal_s;
    bool module_name_end = false;
    std::vector<std::string> parameter_sentence;
    std::vector<std::string> internal_blcok_signal_s;
    for (const auto &cur_s : s_s)
    {

        if (!module_name_end)
        {
            if (cur_s.length() > 5)
            {
                auto it = cur_s.cbegin();
                if (*it == 'm' && *(it + 1) == 'o' && *(it + 2) == 'd' && *(it + 3) == 'u' && *(it + 4) == 'l' && *(it + 5) == 'e')
                {
                    it += 6;
                    for (; it != cur_s.cend() && *it != '(' && *it != '#'; ++it)
                        module_name.push_back(*it);
                }
                else if ((*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'p' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'o' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'o' && *(it + 1) == 'u' && *(it + 2) == 't' && *(it + 3) == 'p' && *(it + 4) == 'u' && *(it + 5) == 't'))
                {
                    singal cur_singal;
                    if (*it == 'i')
                    {
                        if (*(it + 2) == 'p')
                            cur_singal.flag = "input";
                        else
                            cur_singal.flag = "inout";
                        it += 5;
                    }
                    else
                    {
                        cur_singal.flag = "output";
                        it += 6;
                    }

                    if (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g') //缺省时信号名也可以r或者w打头
                    {
                        cur_singal.type = "reg";
                        it += 3;
                    }
                    else
                    {
                        cur_singal.type = "wire";
                        if (*it == 'w' && *(it + 2) == 'i' && *(it + 1) == 'r' && *(it + 1) == 'e') //缺省时信号名也可以r或者w打头
                            it += 4;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                        cur_singal.name.push_back(*it);

                    singal_s.push_back(cur_singal);
                }

                it = cur_s.begin();
                for (auto it_02 = it; it_02 != cur_s.end(); ++it_02)
                {
                    if (*it_02 == '=')
                    {
                        if ((it_02 - it) > 8)
                        {
                            auto it_03 = it;
                            for (; (it_03 + 8) != it_02; ++it_03)
                                if (*it_03 == 'p' && *(it_03 + 1) == 'a' && *(it_03 + 2) == 'r' && *(it_03 + 3) == 'a' && *(it_03 + 4) == 'm' && *(it_03 + 5) == 'e' && *(it_03 + 6) == 't' && *(it_03 + 7) == 'e' && *(it_03 + 8) == 'r')
                                {
                                    it = it_03 + 10;

                                    std::string cur_parameter_name;
                                    for (; it != it_02; ++it)
                                        cur_parameter_name.push_back(*it);
                                    parameter_s.push_back(cur_parameter_name);

                                    parameter_sentence.push_back(cur_s);
                                    break;
                                }
                        }
                    }
                }
            }
        }

        auto it = cur_s.end() - 2;
        if (*it == ')' && *(it + 1) == ';')
        {
            module_name_end = true;
        }

        if (module_name_end)
        {
            if (cur_s.length() > 5)
            {
                auto it = cur_s.cbegin();
                if ((*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'p' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                    (*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'o' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                    (*it == 'o' && *(it + 1) == 'u' && *(it + 2) == 't' && *(it + 3) == 'p' && *(it + 4) == 'u' && *(it + 5) == 't'))
                {
                    singal cur_singal;
                    if (*it == 'i')
                    {
                        if (*(it + 2) == 'p')
                            cur_singal.flag = "input";
                        else
                            cur_singal.flag = "inout";
                        it += 5;
                    }
                    else
                    {
                        cur_singal.flag = "output";
                        it += 6;
                    }

                    if (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g') //缺省时信号名也可以r或者w打头
                    {
                        cur_singal.type = "reg";
                        it += 3;
                    }
                    else
                    {
                        cur_singal.type = "wire";
                        if (*it == 'w' && *(it + 2) == 'i' && *(it + 1) == 'r' && *(it + 1) == 'e') //缺省时信号名也可以r或者w打头
                            it += 4;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                        cur_singal.name.push_back(*it);

                    singal_s.push_back(cur_singal);
                }
            }

            auto it = cur_s.cbegin();
            if ((*it == 'w' && *(it + 1) == 'i' && *(it + 2) == 'r' && *(it + 3) == 'e') || (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g'))
            {
                internal_singal cur_internal_singal;
                if (*it == 'w')
                {
                    cur_internal_singal.type = "wire";
                    it += 4;
                }
                else if (*it == 'r')
                {
                    cur_internal_singal.type = "reg";
                    it += 3;
                }

                if (*it == '[')
                    for (; *(it - 1) != ']'; ++it)
                        cur_internal_singal.width.push_back(*it);

                for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                    cur_internal_singal.name.push_back(*it);

                internal_singal_s.push_back(cur_internal_singal);
            }
            else if (*it == '.')
            {
                std::string cur_internal_blcok_signal;
                for (; *it != '('; ++it)
                    ;
                ++it;
                for (; *it != ')'; ++it)
                    cur_internal_blcok_signal.push_back(*it);
                internal_blcok_signal_s.push_back(cur_internal_blcok_signal);
            }
        }
    }

    system("CLS");
    int max_singal_length = 0;
    for (const auto &it : singal_s)
        if (it.name.length() > max_singal_length)
            max_singal_length = it.name.length();
    bool occur_flag = false;
    for (const auto &it : internal_blcok_signal_s)
    {
        bool occur = false;
        for (const auto &it_1 : singal_s)
            if (it_1.name == it)
                occur = true;
        for (const auto &it_1 : internal_singal_s)
            if (it_1.name == it)
                occur = true;
        if (!occur && !it.empty())
        {
            if (!occur_flag)
                std::cout << "warning:   " << '\n';
            occur_flag = true;
            std::cout << "    " << it << " undefined" << '\n';
        }
    }
    if (occur_flag)
        std::cout << '\n';

    std::cout << "    " << module_name << " ";
    if (!parameter_s.empty())
    {
        std::cout << " #(" << '\n';
        for (auto it = parameter_s.cbegin(); it != parameter_s.cend(); ++it)
            if (it != parameter_s.cend() - 1)
                std::cout << "        ." << *it << " ()," << '\n';
            else
            {
                std::cout << "        ." << *it << " ()" << '\n';
                std::cout << "        )" << '\n';
            }
        std::cout << "    "
                  << "inst_" + module_name << '(' << '\n';
    }
    else
        std::cout << "inst_" + module_name << '(' << '\n';
    for (auto it = singal_s.cbegin(); it != singal_s.cend(); ++it)
    {
        std::cout << "        ." << (*it).name;
        for (int i = 0; i < max_singal_length - (*it).name.length(); ++i)
            std::cout << " ";
        std::cout << " (" << (*it).name << ")";
        if (it != singal_s.cend() - 1)
            std::cout << ',' << '\n';
        else
            std::cout << '\n'
                      << "    );" << '\n';
    }
    std::cout << '\n';
}
void generate_testbench(std::vector<std::string> &s_s)
{
    std::vector<std::string> s_s_temp;

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1));
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open) //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2)
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();
    // std::vector<std::string> parameter_type;

    std::string module_name;
    struct singal
    {
        std::string flag;
        std::string type;
        std::string width;
        std::string name;
    };
    std::vector<singal> singal_s;
    // struct parameter
    // {
    //     std::string name;
    //     std::string width;
    // };
    std::vector<std::string> parameter_s;
    struct internal_singal
    {
        std::string type;
        std::string name;
        std::string width;
    };
    std::vector<internal_singal> internal_singal_s;
    bool module_name_end = false;
    std::vector<std::string> parameter_sentence;
    std::vector<std::string> internal_blcok_signal_s;
    for (const auto &cur_s : s_s)
    {

        if (!module_name_end)
        {
            if (cur_s.length() > 5)
            {
                auto it = cur_s.cbegin();
                if (*it == 'm' && *(it + 1) == 'o' && *(it + 2) == 'd' && *(it + 3) == 'u' && *(it + 4) == 'l' && *(it + 5) == 'e')
                {
                    it += 6;
                    for (; it != cur_s.cend() && *it != '(' && *it != '#'; ++it)
                        module_name.push_back(*it);
                }
                else if ((*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'p' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'o' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'o' && *(it + 1) == 'u' && *(it + 2) == 't' && *(it + 3) == 'p' && *(it + 4) == 'u' && *(it + 5) == 't'))
                {
                    singal cur_singal;
                    if (*it == 'i')
                    {
                        if (*(it + 2) == 'p')
                            cur_singal.flag = "input";
                        else
                            cur_singal.flag = "inout";
                        it += 5;
                    }
                    else
                    {
                        cur_singal.flag = "output";
                        it += 6;
                    }

                    if (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g') //缺省时信号名也可以r或者w打头
                    {
                        cur_singal.type = "reg";
                        it += 3;
                    }
                    else
                    {
                        cur_singal.type = "wire";
                        if (*it == 'w' && *(it + 2) == 'i' && *(it + 1) == 'r' && *(it + 1) == 'e') //缺省时信号名也可以r或者w打头
                            it += 4;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ',' && *it != '\n'; ++it)
                        cur_singal.name.push_back(*it);

                    singal_s.push_back(cur_singal);
                }

                it = cur_s.begin();
                for (auto it_02 = it; it_02 != cur_s.end(); ++it_02)
                {
                    if (*it_02 == '=')
                    {
                        if ((it_02 - it) > 8)
                        {
                            auto it_03 = it;
                            for (; (it_03 + 8) != it_02; ++it_03)
                                if (*it_03 == 'p' && *(it_03 + 1) == 'a' && *(it_03 + 2) == 'r' && *(it_03 + 3) == 'a' && *(it_03 + 4) == 'm' && *(it_03 + 5) == 'e' && *(it_03 + 6) == 't' && *(it_03 + 7) == 'e' && *(it_03 + 8) == 'r')
                                {
                                    it = it_03 + 10;

                                    std::string cur_parameter_name;
                                    for (; it != it_02; ++it)
                                        cur_parameter_name.push_back(*it);
                                    parameter_s.push_back(cur_parameter_name);

                                    parameter_sentence.push_back(cur_s);
                                    break;
                                }
                        }
                    }
                }
            }
        }

        auto it = cur_s.end() - 2;
        if (*it == ')' && *(it + 1) == ';')
        {
            module_name_end = true;
        }

        if (module_name_end)
        {
            it = cur_s.cbegin();
            if ((*it == 'w' && *(it + 1) == 'i' && *(it + 2) == 'r' && *(it + 3) == 'e') || (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g'))
            {
                internal_singal cur_internal_singal;
                if (*it == 'w')
                {
                    cur_internal_singal.type = "wire";
                    it += 4;
                }
                else if (*it == 'r')
                {
                    cur_internal_singal.type = "reg";
                    it += 3;
                }

                if (*it == '[')
                    for (; *(it - 1) != ']'; ++it)
                        cur_internal_singal.width.push_back(*it);

                for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                    cur_internal_singal.name.push_back(*it);

                internal_singal_s.push_back(cur_internal_singal);
            }
            else if (*it == '.')
            {
                std::string cur_internal_blcok_signal;
                for (; *it != '('; ++it)
                    ;
                ++it;
                for (; *it != ')'; ++it)
                    cur_internal_blcok_signal.push_back(*it);
                internal_blcok_signal_s.push_back(cur_internal_blcok_signal);
            }
        }
    }

    // system("CLS");
    int max_singal_length = 0;
    for (const auto &it : singal_s)
        if (it.name.length() > max_singal_length)
            max_singal_length = it.name.length();
    std::cout << "module " << module_name + "_tb();" << '\n'
              << '\n';
    std::cout << "    localparam period = 10;" << '\n'
              << '\n';
    if (!parameter_s.empty())
        for (auto it = parameter_sentence.cbegin(); it != parameter_sentence.cend() - 1; ++it)
        {
            std::cout << "    ";
            for (auto it_1 = (*it).cbegin(); it_1 != (*it).cend() - 1; ++it_1)
            {
                std::cout << *it_1;
                if (*(it_1 + 1) == '=' || *(it_1) == '=' || it_1 - (*it).cbegin() == 8)
                    std::cout << ' ';
            }
            std::cout << ';';
            std::cout << '\n';
        }
    {
        bool test_flag = false;
        for (const auto &it : singal_s)
        {
            if (!test_flag)
                if (it.name.find("test") == std::string::npos)
                {
                    test_flag = true;
                    std::cout << '\n';
                }
            if (it.flag[0] == 'i')
                std::cout << "    reg " << it.width << ' ' << it.name << ";" << '\n';
            else
                std::cout << "    wire " << it.width << ' ' << it.name << ";" << '\n';
        }
    }
    std::cout << '\n';
    std::cout << "    " << module_name << " ";
    if (!parameter_s.empty())
    {
        std::cout << " #(" << '\n';
        for (auto it = parameter_s.cbegin(); it != parameter_s.cend(); ++it)
            if (it != parameter_s.cend() - 1)
                std::cout << "        ." << *it << " ()," << '\n';
            else
            {
                std::cout << "        ." << *it << " ()" << '\n';
                std::cout << "        )" << '\n';
            }
        std::cout << "    "
                  << "inst_" + module_name << '(' << '\n';
    }
    else
        std::cout << "inst_" + module_name << '(' << '\n';
    {
        bool test_flag = false;
        for (auto it = singal_s.cbegin(); it != singal_s.cend(); ++it)
        {
            if (!test_flag)
                if ((*it).name.find("test") == std::string::npos)
                {
                    test_flag = true;
                    std::cout << '\n';
                }
            std::cout << "        ." << (*it).name;
            for (int i = 0; i < max_singal_length - (*it).name.length(); ++i)
                std::cout << " ";
            std::cout << " (" << (*it).name << ")";
            if (it != singal_s.cend() - 1)
                std::cout << ',' << '\n';
            else
                std::cout << '\n'
                          << "    );" << '\n';
        }
    }
    std::cout << '\n';

    std::cout << "    initial begin" << '\n';
    for (const auto &it : singal_s)
        if (it.name.find("clk") != std::string::npos)
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
    for (const auto &it : singal_s)
        if (it.flag[0] == 'i')
        {
            if (it.name.find("clk") == std::string::npos && it.name.find("rst") == std::string::npos)
            {
                if (it.width.empty())
                    std::cout << "        " << it.name << " = 1'b0;" << '\n';
                else
                {
                    int width = 0;
                    for (auto it_2 = it.width.begin() + 1; *it_2 != ':'; ++it_2)
                    {
                        width *= 10;
                        width += *it_2 - '0';
                    }
                    width += 1;
                    if (width <= 8)
                        std::cout
                            << "        " << it.name << " = " << width << "'b0;" << '\n';
                    else
                        std::cout
                            << "        " << it.name << " = " << width << "'h0;" << '\n';
                }
            }
        }
    std::cout << '\n';
    for (const auto &it : singal_s)
        if (it.name.find("rst_n") != std::string::npos)
        {
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            break;
        }
        else if (it.name.find("rst") != std::string::npos)
        {
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            std::cout << "        #(period)" << '\n';
            std::cout << "        " << it.name << " = 1'b0;" << '\n';
            break;
        }

    std::cout << '\n';
    std::cout << "        #(period * 10)" << '\n';
    for (const auto &it : singal_s)
        if (it.flag[0] == 'i')
        {
            if (it.name.find("clk") == std::string::npos && it.name.find("rst") == std::string::npos)
            {
                if (it.width.empty())
                    std::cout << "        " << it.name << " = 1'b;" << '\n';
                else
                {
                    int width = 0;
                    for (auto it_2 = it.width.begin() + 1; *it_2 != ':'; ++it_2)
                    {
                        width *= 10;
                        width += *it_2 - '0';
                    }
                    width += 1;
                    if (width <= 8)
                        std::cout
                            << "        " << it.name << " = " << width << "'b;" << '\n';
                    else
                        std::cout
                            << "        " << it.name << " = " << width << "'h;" << '\n';
                }
            }
        }
    std::cout << '\n'
              << "        #(period)" << '\n';
    std::cout << "    end" << '\n';
    std::cout << '\n';

    /* for (const auto &it : singal_s)
    {
        int clk_num = 0;
        if (it.name.find("clk") != std::string::npos)
        {
            ++clk_num;
            std::cout << "        " << it.name << " = 1'b1;" << '\n';
            std::cout << "            always# ( period / 2 ) " << it.name << " = ~ " << it.name << ";" << '\n';
            if (clk_num == 1)
            {
                std::cout << "        for(i =0 ;i<499 ;i=i+1) begin" << '\n';
                std::cout << "            if( i ==  ) begin" << '\n'
                          << '\n';
                std::cout << "            end" << '\n';
                std::cout << "            #period;" << '\n';
                //std::cout << "            # ( period / 2 ) clk_i = ~ clk_i;" << '\n';
                //std::cout << "            # ( period / 2 ) clk_i = ~ clk_i;" << '\n';
                std::cout << "        end" << '\n';
            }

            //break;
        }
    }
    std::cout << '\n'; */

    for (const auto &it : singal_s)
        if (it.name.find("clk") != std::string::npos)
            std::cout << "    always #(period / 2) " << it.name << " = ~" << it.name << ";" << '\n';
    std::cout << '\n';
    std::cout << "endmodule" << '\n';

    // system("CLS");
    //  generate_testbench();
}

void generate_testbench_internal()
{
    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    std::vector<std::string> s_s_reserve; //用于重新生成
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);
    s_s_reserve = s_s;
    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1));
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open) //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2)
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();
    // std::vector<std::string> parameter_type;

    std::string module_name;
    struct singal
    {
        std::string flag;
        std::string type;
        std::string width;
        std::string name;
    };
    std::vector<singal> singal_s;
    // struct parameter
    // {
    //     std::string name;
    //     std::string width;
    // };
    std::vector<std::string> parameter_s;
    struct internal_singal
    {
        std::string type;
        std::string name;
        std::string width;
    };
    std::vector<internal_singal> internal_singal_s;
    bool module_name_end = false;
    std::vector<std::string> parameter_sentence;
    std::vector<std::string> internal_blcok_signal_s;
    for (const auto &cur_s : s_s)
    {

        if (!module_name_end)
        {
            if (cur_s.length() > 5)
            {
                auto it = cur_s.cbegin();
                if (*it == 'm' && *(it + 1) == 'o' && *(it + 2) == 'd' && *(it + 3) == 'u' && *(it + 4) == 'l' && *(it + 5) == 'e')
                {
                    it += 6;
                    for (; it != cur_s.cend() && *it != '(' && *it != '#'; ++it)
                        module_name.push_back(*it);
                }
                else if ((*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'p' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'i' && *(it + 1) == 'n' && *(it + 2) == 'o' && *(it + 3) == 'u' && *(it + 4) == 't') ||
                         (*it == 'o' && *(it + 1) == 'u' && *(it + 2) == 't' && *(it + 3) == 'p' && *(it + 4) == 'u' && *(it + 5) == 't'))
                {
                    singal cur_singal;
                    if (*it == 'i')
                    {
                        if (*(it + 2) == 'p')
                            cur_singal.flag = "input";
                        else
                            cur_singal.flag = "inout";
                        it += 5;
                    }
                    else
                    {
                        cur_singal.flag = "output";
                        it += 6;
                    }

                    if (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g') //缺省时信号名也可以r或者w打头
                    {
                        cur_singal.type = "reg";
                        it += 3;
                    }
                    else
                    {
                        cur_singal.type = "wire";
                        if (*it == 'w' && *(it + 2) == 'i' && *(it + 1) == 'r' && *(it + 1) == 'e') //缺省时信号名也可以r或者w打头
                            it += 4;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ',' && *it != '\n'; ++it)
                        cur_singal.name.push_back(*it);

                    singal_s.push_back(cur_singal);
                }

                it = cur_s.begin();
                for (auto it_02 = it; it_02 != cur_s.end(); ++it_02)
                {
                    if (*it_02 == '=')
                    {
                        if ((it_02 - it) > 8)
                        {
                            auto it_03 = it;
                            for (; (it_03 + 8) != it_02; ++it_03)
                                if (*it_03 == 'p' && *(it_03 + 1) == 'a' && *(it_03 + 2) == 'r' && *(it_03 + 3) == 'a' && *(it_03 + 4) == 'm' && *(it_03 + 5) == 'e' && *(it_03 + 6) == 't' && *(it_03 + 7) == 'e' && *(it_03 + 8) == 'r')
                                {
                                    it = it_03 + 10;

                                    std::string cur_parameter_name;
                                    for (; it != it_02; ++it)
                                        cur_parameter_name.push_back(*it);
                                    parameter_s.push_back(cur_parameter_name);

                                    parameter_sentence.push_back(cur_s);
                                    break;
                                }
                        }
                    }
                }
            }
        }

        {
            auto it = cur_s.end() - 2;
            if (*it == ')' && *(it + 1) == ';')
            {
                module_name_end = true;
            }

            if (module_name_end)
            {
                it = cur_s.cbegin();
                if ((*it == 'w' && *(it + 1) == 'i' && *(it + 2) == 'r' && *(it + 3) == 'e') || (*it == 'r' && *(it + 1) == 'e' && *(it + 2) == 'g'))
                {
                    internal_singal cur_internal_singal;
                    if (*it == 'w')
                    {
                        cur_internal_singal.type = "wire";
                        it += 4;
                    }
                    else if (*it == 'r')
                    {
                        cur_internal_singal.type = "reg";
                        it += 3;
                    }

                    if (*it == '[')
                        for (; *(it - 1) != ']'; ++it)
                            cur_internal_singal.width.push_back(*it);

                    for (; it != cur_s.end() && *it != ')' && *it != ';' && *it != ','; ++it)
                        cur_internal_singal.name.push_back(*it);

                    internal_singal_s.push_back(cur_internal_singal);
                }
                else if (*it == '.')
                {
                    std::string cur_internal_blcok_signal;
                    for (; *it != '('; ++it)
                        ;
                    ++it;
                    for (; *it != ')'; ++it)
                        cur_internal_blcok_signal.push_back(*it);
                    internal_blcok_signal_s.push_back(cur_internal_blcok_signal);
                }
            }
        }
    }

    {
        auto insert_pos = s_s_reserve.begin();
        std::vector<std::string> s_s_reserve_temp;
        for (;; ++insert_pos)
        {
            auto it_1 = (*insert_pos).cbegin();
            for (; it_1 < (*insert_pos).cend() && (*it_1) == ' '; ++it_1) //边界判断是必须做的其他只是辅助
                ;
            if ((*insert_pos).cend() - it_1 > 5)
            {
                if (*(it_1) == 'm' && *(it_1 + 1) == 'o' && *(it_1 + 2) == 'd' && *(it_1 + 3) == 'u' && *(it_1 + 4) == 'l' && *(it_1 + 5) == 'e')
                    break;
            }
            // if((*it_1).find(module_name)==std::string::npos;
            s_s_reserve_temp.push_back((*insert_pos) + "\n");
        }
        s_s_reserve_temp.push_back((*insert_pos) + "\n");
        ++insert_pos;
        if (!(*insert_pos).empty())
            if (*(insert_pos) == "(")
            {
                s_s_reserve_temp.push_back(*(insert_pos) + "\n");
                ++insert_pos;
            }
        for (const auto it : internal_singal_s)
        {
            // std::cout << "    output " << it.width << " " << it.name << "_test," << '\n';
            std::string temp = "    output " + it.width + it.name + "_test,\n";
            s_s_reserve_temp.push_back(temp);
        }
        s_s_reserve_temp.push_back("\n");
        for (; insert_pos != s_s_reserve.end() - 1; ++insert_pos)
            s_s_reserve_temp.push_back((*insert_pos) + "\n");
        for (const auto it : internal_singal_s)
        {
            // std::cout << "    assign " << it.name << "_test = " << it.name << ";" << '\n';
            std::string temp = "    assign " + it.name + "_test = " + it.name + ";\n";
            s_s_reserve_temp.push_back(temp);
        }
        s_s_reserve_temp.push_back("\n");
        s_s_reserve_temp.push_back("endmodule\n");
        s_s_reserve = s_s_reserve_temp;
        s_s_reserve_temp.clear();
    }
    system("cls");
    bool occur_flag = false;
    for (const auto &it : internal_blcok_signal_s)
    {
        bool occur = false;
        for (const auto &it_1 : singal_s)
            if (it_1.name == it)
                occur = true;
        for (const auto &it_1 : internal_singal_s)
            if (it_1.name == it)
                occur = true;
        if (!occur && !it.empty())
        {
            if (!occur_flag)
                std::cout << "warning:   " << '\n';
            occur_flag = true;
            std::cout << "    " << it << " undefined" << '\n';
        }
    }
    if (occur_flag)
        std::cout << '\n';
    for (const auto &it : s_s_reserve)
        std::cout << it;
    generate_testbench(s_s_reserve);

    // system("CLS");
}

void lut() //输入有多少个操作数,真值为1的位置得到结果
{

    std::vector<std::string> s_s;
    for (std::string cur_s; std::cin >> cur_s && cur_s != "e";)
        s_s.push_back(cur_s);

    int input_num = s_s[0][0] - '0';
    std::string init_b(64, '0');
    for (auto it = s_s.cbegin() + 1; it != s_s.cend(); ++it)
    {
        int num = 0;
        int base = 1;
        for (auto it_1 = (*it).crbegin(); it_1 != (*it).crend(); ++it_1)
        {
            if (*it_1 == '1')
                num += base * 1;
            base *= 2;
        }
        init_b[63 - num] = '1'; //高位为0
    }

    std::string init_h(16, 0);
    for (int i = 15; i > 0; --i)
    {
        if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '0')
            init_h[i] = '0';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '1')
            init_h[i] = '1';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '0')
            init_h[i] = '2';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '1')
            init_h[i] = '3';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '0')
            init_h[i] = '4';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '1')
            init_h[i] = '5';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '0')
            init_h[i] = '6';
        else if (init_b[0 + i * 4] == '0' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '1')
            init_h[i] = '7';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '0')
            init_h[i] = '8';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '1')
            init_h[i] = '9';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '0')
            init_h[i] = 'a';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '0' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '1')
            init_h[i] = 'b';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '0')
            init_h[i] = 'c';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '0' && init_b[3 + i * 4] == '1')
            init_h[i] = 'd';
        else if (init_b[0 + i * 4] == '1' && init_b[1 + i * 4] == '1' && init_b[2 + i * 4] == '1' && init_b[3 + i * 4] == '0')
            init_h[i] = 'e';
        else
            init_h[i] = 'f';
    }

    system("cls");
    std::cout << "    LUT" << input_num << " #(\n";
    std::cout << "        .INIT(" << (1 << input_num) << "'h";
    for (int i = 15 - (1 << input_num) / 4 + 1; i < 16; ++i)
        std::cout << init_h[i];
    std::cout << ")\n";
    std::cout << "    ) _(\n";
    for (int i = input_num - 1; i >= 0; --i)
    {
        std::cout << "        .I" << i << "(),\n";
        if (i == 0)
            std::cout << "\n        .O()\n    );\n";
    }
}
/*
    0 1
    00 01 10 11
    000 001 010 011 100 101 110 111
    0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
    00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111
    10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111
    */
void formate()
{

    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1));
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open) //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2)
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();
}

std::vector<std::string> align(std::vector<std::string> &s_s, char c)
{
    std::vector<std::string> temp_s_s;
    int max_num = 0;
    std::vector<int> temp_num(s_s.size(), 0);
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        for (auto &it_2 : (*it))
            if (it_2 != c)
                ++temp_num[(it - s_s.begin())];
            else
                break;
        if (temp_num[it - s_s.begin()] > max_num)
            max_num = temp_num[it - s_s.begin()];
    }

    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp_s;
        auto it_2 = (*it).begin();
        for (; (it_2 - (*it).begin()) < temp_num[it - s_s.begin()]; ++it_2)
            temp_s.push_back(*it_2);
        for (int j = 0; j < (max_num - temp_num[it - s_s.begin()]); ++j)
            temp_s.push_back(' ');
        for (; it_2 != (*it).end(); ++it_2)
            temp_s.push_back(*it_2);

        temp_s_s.push_back(temp_s);
    }
    return temp_s_s;
}

void auto_machine_internal(std::vector<std::string> &s_s)
{
    if ((*(s_s.end() - 1)).find("reg") != std::string::npos)
        s_s.pop_back();
    // int num = s_s.size();

    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        std::string one_hot;
        if (it == s_s.begin())
            temp += "    localparam ";
        else
            temp += "               ";

        for (auto it_1 = (*it).begin(); it_1 != (*it).end(); ++it_1)
        {
            temp.push_back(*it_1);
            if (*it_1 == '=')
            {
                break;
            }
        }
        if (s_s.size() <= 48)
        {
            temp += ' ' + std::to_string(s_s.size()) + "'b";
            one_hot.resize(s_s.size());
            for (int j = 0; j < s_s.size(); ++j)
                if (j == (it - s_s.begin()))
                    one_hot[j] = '1';
                else
                    one_hot[j] = '0';
        }
        else
        {
            temp += ' ' + std::to_string(s_s.size()) + "'h";
            one_hot.resize(s_s.size() / 4 + 1);
            for (int j = 0; j < (s_s.size() / 4 + 1); ++j)

                if (j == ((it - s_s.begin()) / 4))
                {
                    if ((it - s_s.begin()) % 4 == 0)
                        one_hot[j] = '1';
                    else if ((it - s_s.begin()) % 4 == 1)
                        one_hot[j] = '2';
                    else if ((it - s_s.begin()) % 4 == 2)
                        one_hot[j] = '4';
                    else
                        one_hot[j] = '8';
                }
                else
                    one_hot[j] = '0';
        }
        for (auto it = one_hot.rbegin(); it != one_hot.rend(); ++it)
        {
            if ((it - one_hot.rend()) % 4 == 0 && it != one_hot.rend())
                temp += '_';
            temp += (*it);
        }

        if (it == s_s.end() - 1)
            temp.push_back(';');
        else
            temp.push_back(',');
        temp += " //" + std::to_string(it - s_s.begin());
        std::swap(*it, temp);
    }
    // s_s.push_back("    reg[" + std::to_string(s_s.size() - 1) + ":0] state;");

    s_s = align(s_s, '=');

    system("cls");
    for (int i = 0; i < 2; ++i)
    {
        printf("    reg             [%d:0]", (s_s.size() - 1));
        int occur = (s_s.size() - 1) / 10;
        for (int j = 0; j < 31 - occur; ++j)
            printf(" ");
        if (i == 0)
            printf("c_state ;\n");
        else
            printf("n_state ;\n");
    }
    for (const auto &it : s_s)
        std::cout << it << std::endl;

    printf("    always @(posedge clk_i) begin\n");
    printf("        if(rst_i)\n");
    printf("            c_state <= s_idle;\n");
    printf("        else\n");
    printf("            c_state <= n_state;\n");
    printf("    end\n");
}
void auto_machine(std::vector<std::string> s_s)
{
    std::vector<std::string> s_s_temp;

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();

    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1)); //防止漏掉最后一个元素
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open)   //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2) //有可能只有*/
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    auto s_s_out_no_comment = s_s;

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();

    std::vector<std::string> name_s;
    std::vector<std::string> name_s_no_equal;
    for (const auto &it : s_s)
    {
        std::string name;
        if (it.size() > 7)
            if ((it[0] == 'd' && it[1] == 'e' && it[2] == 'f' && it[3] == 'a' && it[4] == 'u' && it[5] == 'l' && it[6] == 't' && it[7] == ':'))
                continue;

        if (it.size() > 6)
        {

            if ((it[it.size() - 6] == ':') && (it[it.size() - 5] == 'b') && (it[it.size() - 4] == 'e') && (it[it.size() - 3] == 'g') && (it[it.size() - 2] == 'i') && (it[it.size() - 1] == 'n') && !isdigit(it[0]))
            {
                // flag = true;
                // name = "    localparam ";
                for (int j = 0; j < it.size() - 6; ++j)
                    name.push_back(it[j]);
                name_s_no_equal.push_back(name);
                name += " = ";
                name_s.push_back(name);
                continue;
            }
        }
        //两种情况不是互斥的
        if (!it.empty())
            if ((it[it.size() - 1] == ':') && !isdigit(it[0]))
            {
                // name = "    localparam ";
                for (auto it_1 = it.begin(); it_1 < it.end() - 1; ++it_1)
                    name.push_back(*it_1);
                name_s_no_equal.push_back(name);
                name += " = ";
                name_s.push_back(name);
                continue;
            }
    }
    auto_machine_internal(name_s);
}

void auto_machine(const std::string &s)
{
    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);
    std::vector<std::string> s_s_out(s_s.size());
    for (int i = 0; i < s_s.size(); ++i)
        s_s_out[i] = s_s[i];
    auto_machine(s_s);

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    s_s.clear();
    for (auto &it : s_s_temp)
        if (it.size() > 3)
            s_s.push_back(it);

    s_s_temp.clear();
    for (auto &cur_s : s_s) //去除注释
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
        {
            if (it != cur_s.cend() - 1)
                if (*it == '/' && *(it + 1) == '/')
                    break;
            temp.push_back(*it);
        }
        s_s_temp.push_back(temp);
    }
    s_s.clear();
    for (auto &it : s_s_temp)
        if (it.size() > 3)
            s_s.push_back(it);
    s_s_temp.clear();

    struct variable
    {
        std::string name; // maybe inlcude bit wid
        std::string width;
    };
    std::vector<variable> variable_s;
    for (auto it = s_s.cbegin(); it != s_s.cend(); ++it)
    {
        variable temp;
        bool is_signal = false;
        auto it_j = (*it).cbegin();
        for (; it_j != (*it).cend() - 1; ++it_j)
        {
            if (*it_j == '=')
            {
                if (*(it_j + 1) != '=')
                {
                    ++it_j;
                    is_signal = true;
                }

                break;
            }
            temp.name.push_back(*it_j);
        }

        if (is_signal)
        {
            bool have_occur = false;
            for (const auto &el_j : variable_s)
                if (el_j.name == temp.name)
                {
                    have_occur = true;
                    break;
                }
            if (!have_occur)
            {
                for (; it_j != (*it).cend(); ++it_j)
                {
                    if (!isdigit(*it_j))
                        break;
                    temp.width.push_back(*it_j);
                }
                variable_s.push_back(temp);
            }
        }
    }

    s_s.clear();
    for (const auto &el_i : variable_s)
    {
        if (el_i.name == "n_state")
            continue;
        std::string temp = "        ";
        temp += (el_i.name);
        if (!el_i.width.empty())
        {
            if (el_i.name.find("[") == std::string::npos)
            {
                int i_wid_sub_1 = atoi((el_i.width).c_str());
                if (i_wid_sub_1 != 1)
                {
                    i_wid_sub_1 -= 1;
                    std::string wid_sub_1 = std::to_string(i_wid_sub_1);
                    temp += "[" + wid_sub_1 + ":0]";
                }
            }
        }
        temp += (" = ");
        if (!el_i.width.empty())
        {
            temp += el_i.width + "'b";
            int i_width = atoi(el_i.width.c_str());
            for (int j = 0; j < i_width; ++j)
            {
                if (j % 4 == 0 && j != 0)
                    temp += '_';
                temp += '0';
            }
            temp += ';';
        }
        else
            temp += "1'b0;";
        s_s.push_back(temp);
    }
    s_s = align(s_s, '=');

    // system("CLS");
    printf("    always @* begin\n");
    for (const auto &el_i : s_s)
        std::cout << el_i << '\n';
    for (auto &it : s_s_out)
        std::cout << it << '\n';
    printf("    end\n");
}

void auto_machine()
{
    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);

    for (auto &cur_s : s_s) //去除空格和空行
    {
        std::string temp;
        for (auto it = cur_s.cbegin(); it != cur_s.cend(); ++it)
            if (*it != ' ')
                temp.push_back(*it);

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();

    bool mul_line_command_open = false;
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;
        if (!mul_line_command_open)
            // if ((*it).length() > 2)
            for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            {
                if (*it_1 == '/' && *(it_1 + 1) == '*')
                {
                    mul_line_command_open = true;
                    break;
                }
                else
                {
                    temp.push_back(*it_1);
                    if (it_1 == (*it).end() - 2)
                        temp.push_back(*(it_1 + 1)); //防止漏掉最后一个元素
                }
                /*else if (*it_1 == '*' && *(it_1 + 1) == '/')
                    mul_line_command_open = false;*/
            }

        if (mul_line_command_open)   //多行注释可能在本行也可能在下一行
            if ((*it).length() >= 2) //有可能只有*/
                for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
                    if (*it_1 == '*' && *(it_1 + 1) == '/')
                    {
                        mul_line_command_open = false;
                        for (auto cur_pos = it_1 + 2; cur_pos != (*it).end(); ++cur_pos) //加上本行剩余的
                            temp.push_back(*cur_pos);
                        break;
                    }

        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    auto s_s_out_no_comment = s_s;

    s_s_temp.clear();
    for (auto it = s_s.begin(); it != s_s.end(); ++it)
    {
        std::string temp;

        // if ((*it).length() > 2)
        for (auto it_1 = (*it).begin(); it_1 != (*it).end() - 1; ++it_1)
            if (*it_1 == '/' && *(it_1 + 1) == '/')
                break;
            else
            {
                temp.push_back(*it_1);
                if (it_1 == (*it).end() - 2)
                    temp.push_back(*(it_1 + 1));
            }
        if (!temp.empty())
            s_s_temp.push_back(temp);
    }
    std::swap(s_s_temp, s_s);
    s_s_temp.clear();

    std::vector<std::string> name_s;
    std::vector<std::string> name_s_no_equal;
    for (const auto &it : s_s)
    {
        std::string name;
        if (it.size() > 7)
            if ((it[0] == 'd' && it[1] == 'e' && it[2] == 'f' && it[3] == 'a' && it[4] == 'u' && it[5] == 'l' && it[6] == 't' && it[7] == ':'))
                continue;

        if (it.size() > 6)
        {

            if ((it[it.size() - 6] == ':') && (it[it.size() - 5] == 'b') && (it[it.size() - 4] == 'e') && (it[it.size() - 3] == 'g') && (it[it.size() - 2] == 'i') && (it[it.size() - 1] == 'n') && !isdigit(it[0]))
            {
                // flag = true;
                // name = "    localparam ";
                for (int j = 0; j < it.size() - 6; ++j)
                    name.push_back(it[j]);
                name_s_no_equal.push_back(name);
                name += " = ";
                name_s.push_back(name);
                continue;
            }
        }
        //两种情况不是互斥的
        if (!it.empty())
            if ((it[it.size() - 1] == ':') && !isdigit(it[0]))
            {
                // name = "    localparam ";
                for (auto it_1 = it.begin(); it_1 < it.end() - 1; ++it_1)
                    name.push_back(*it_1);
                name_s_no_equal.push_back(name);
                name += " = ";
                name_s.push_back(name);
                continue;
            }
    }
    auto_machine_internal(name_s);
}

void mux_2i1o(const int num)
{
    if (num == 1)
    {
        printf("module mux_2i1o_1b(\n");
        printf("    input                                               clk_i   ,\n");
        printf("    input                                               rst_i   ,\n");
        printf("    input                                               ce_i    ,\n");
        printf("\n");
        printf("    input                                               sel_i   ,\n");
        printf("\n");
        printf("    input                                               in_0_i  ,\n");
        printf("    input                                               in_1_i  ,\n");
        printf("    output                                              out_o   \n");
        printf("    );\n");
        printf("\n");
        printf("    wire                                                out ;\n");
        printf("\n");
        printf("    LUT3 #(\n");
        printf("        .INIT (8'hca)\n");
        printf("        ) inst_mux_0(\n");
        printf("        .I0 (in_0_i),\n");
        printf("        .I1 (in_1_i),\n");
        printf("        .I2 (sel_i),\n");
        printf("        .O  (out)\n");
        printf("    );\n");
        printf("    FDRE #(\n");
        printf("        .INIT (1'b0)\n");
        printf("        ) inst_out_o_0(\n");
        printf("        .C  (clk_i),\n");
        printf("        .R  (rst_i),\n");
        printf("        .CE (ce_i),\n");
        printf("        .D  (out),\n");
        printf("        .Q  (out_o)\n");
        printf("    );\n");
        printf("\n");
        printf("endmodule\n");
    }
    else
    {
        printf("module mux_2i1o_%db(\n    input                                               clk_i,\n    input                                               rst_i,\n    input                                               ce_i,\n\n    input                                               sel_i,\n\n    input           [%d:0]                              in_0_i,\n    input           [%d:0]                              in_1_i,\n    output          [%d:0]                              out_o\n    );\n\n    wire            [%d:0]                              out;\n\n", num, num - 1, num - 1, num - 1, num - 1);
        for (int i = 0; i < num; ++i)
        {
            printf("    LUT3 #(\n        .INIT (8'hca)\n        ) inst_mux_%d(\n        .I0 (in_0_i[%d]),\n        .I1 (in_1_i[%d]), \n        .I2 (sel_i),\n        .O  (out[%d])\n    );\n", i, i, i, i);
            printf("    FDRE #(\n        .INIT (1'b0)\n        ) inst_out_o_%d(\n        .C  (clk_i),      \n        .R  (rst_i),\n        .CE (ce_i),\n        .D  (out[%d]),\n        .Q  (out_o[%d])\n    );\n", i, i, i);
            printf("\n");
        }
        printf("endmodule\n");
    }
}
void mux_2i1o_no_dly(const int num)
{
    printf("module mux_2i1o_%db_no_dly(\n    input                                               sel_i,\n\n    input           [%d:0]                              in_0_i,\n    input           [%d:0]                              in_1_i,\n    output          [%d:0]                              out_o\n    );\n\n", num, num - 1, num - 1, num - 1, num - 1);
    for (int i = 0; i < num; ++i)
        printf("    LUT3 #(\n        .INIT (8'hca)\n        ) inst_mux_%d(\n        .I0 (in_0_i[%d]),\n        .I1 (in_1_i[%d]), \n        .I2 (sel_i),\n        .O  (out_o[%d])\n    );\n", i, i, i, i);
    printf("\n");
    printf("endmodule\n");
}
void mux_4i1o_no_dly(int num)
{
    printf("module mux_4i1o_%db_no_dly(\n", num);
    printf("    input           [1:0]                               sel_i   ,\n");
    printf("\n");
    if (num < 10)
    {
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,	\n", num - 1);
        printf("    output          [%d:0]                               out_o   \n", num - 1);
        printf("    );\n\n");
    }
    else if (num < 100)
    {
        printf("    input           [%d:0]                              in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                              in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                              in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                              in_3_i  ,	\n", num - 1);
        printf("    output          [%d:0]                              out_o   \n", num - 1);
        printf("    );\n\n");
    }
    else
    {
        printf("    input           [%d:0]                             in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                             in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                             in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                             in_3_i  ,	\n", num - 1);
        printf("    output          [%d:0]                             out_o   \n", num - 1);
        printf("    );\n\n");
    }

    for (int i = 0; i < num; ++i)
    {
        printf("    LUT6 #(\n");
        printf("        .INIT(64'hff00f0f0ccccaaaa)\n");
        printf("        ) inst_out_o_b%d(\n", i);
        printf("        .I0 (in_0_i[%d]),\n", i);
        printf("        .I1 (in_1_i[%d]),\n", i);
        printf("        .I2 (in_2_i[%d]),\n", i);
        printf("        .I3 (in_3_i[%d]),\n", i);
        printf("        .I4 (sel_i[0]),\n");
        printf("        .I5 (sel_i[1]),\n");
        printf("        .O  (out_o[%d])\n", i);
        printf("    );\n");
    }
    printf("\n");
    printf("endmodule\n");
}
void mux_4i1o(int num)
{
    printf("module mux_4i1o_%db(\n", num);
    printf("    input                                               clk_i   ,\n");
    printf("    input                                               rst_i   ,\n");
    printf("    input                                               ce_i    ,\n");
    printf("\n");
    printf("    input           [1:0]                               sel_i   ,\n");
    printf("\n");
    if (num < 10)
    {
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,	\n", num - 1);
        printf("    output          [%d:0]                               out_o   \n", num - 1);
        printf("    );\n\n");

        printf("    wire            [%d:0]                              out ;\n\n", num - 1);
    }
    else if (num < 100)
    {
        printf("    input           [%d:0]                              in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                              in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                              in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                              in_3_i  ,	\n", num - 1);
        printf("    output          [%d:0]                              out_o   \n", num - 1);
        printf("    );\n\n");

        printf("    wire            [%d:0]                              out ;\n\n", num - 1);
    }
    else
    {
        printf("    input           [%d:0]                             in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                             in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                             in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                             in_3_i  ,	\n", num - 1);
        printf("    output          [%d:0]                             out_o   \n", num - 1);
        printf("    );\n\n");

        printf("    wire            [%d:0]                              out ;\n\n", num - 1);
    }

    for (int i = 0; i < num; ++i)
    {
        printf("    LUT6 #(\n");
        printf("        .INIT(64'hff00f0f0ccccaaaa)\n");
        printf("        ) inst_out_b%d(\n", i);
        printf("        .I0 (in_0_i[%d]),\n", i);
        printf("        .I1 (in_1_i[%d]),\n", i);
        printf("        .I2 (in_2_i[%d]),\n", i);
        printf("        .I3 (in_3_i[%d]),\n", i);
        printf("        .I4 (sel_i[0]),\n");
        printf("        .I5 (sel_i[1]),\n");
        printf("        .O  (out[%d])\n", i);
        printf("    );\n");
        printf("    FDRE #(\n        .INIT (1'b0)\n        ) inst_out_o_%d(\n        .C  (clk_i),      \n        .R  (rst_i),\n        .CE (ce_i),\n        .D  (out[%d]),\n        .Q  (out_o[%d])\n    );\n", i, i, i);
        printf("\n");
    }
    printf("endmodule\n");
}
void mux_8i1o_no_dly(int num)
{
    if (num == 1)
    {
        printf("module mux_8i1o_1b_no_dly(\n");
        printf("    input           [2:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input                                               in_0_i  ,\n");
        printf("    input                                               in_1_i  ,\n");
        printf("    input                                               in_2_i  ,\n");
        printf("    input                                               in_3_i  ,\n");
        printf("    input                                               in_4_i  ,\n");
        printf("    input                                               in_5_i  ,\n");
        printf("    input                                               in_6_i  ,\n");
        printf("    input                                               in_7_i  ,	 	 \n");
        printf("    output                                              out_o   \n");
        printf("    );\n");
        printf("\n");
        printf("    wire            [1:0]                               lut_out ;\n");
        printf("\n");
        printf("   LUT6 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) \n");
        printf("   ) LUT_0 (\n");
        printf("      .O(lut_out[0]),   \n");
        printf("      .I0(in_0_i), // LUT input\n");
        printf("      .I1(in_1_i), // LUT input\n");
        printf("      .I2(in_2_i), // LUT input\n");
        printf("      .I3(in_3_i), // LUT input\n");
        printf("      .I4(sel_i[0]), // LUT input\n");
        printf("      .I5(sel_i[1])  // LUT input\n");
        printf("   );\n");
        printf("\n");
        printf("   LUT6 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa)  // Specify LUT Contents\n");
        printf("   ) LUT_1 (\n");
        printf("      .O(lut_out[1]),   \n");
        printf("      .I0(in_4_i), // LUT input\n");
        printf("      .I1(in_5_i), // LUT input\n");
        printf("      .I2(in_6_i), // LUT input\n");
        printf("      .I3(in_7_i), // LUT input\n");
        printf("      .I4(sel_i[0]), // LUT input\n");
        printf("      .I5(sel_i[1])  // LUT input\n");
        printf("   );\n");
        printf("		\n");
        printf("   MUXF5 MUX (\n");
        printf("      .O(out_o),    // Output of MUX to general routing\n");
        printf("      .I0(lut_out[0]),  // Input (tie directly to the output of LUT4)\n");
        printf("      .I1(lut_out[1]),  // Input (tie directoy to the output of LUT4)\n");
        printf("      .S(sel_i[2])     // Input select to MUX\n");
        printf("   );\n");
        printf("	 \n");
        printf("endmodule        \n");
    }
    else
    {
        printf("module mux_8i1o_%db_no_dly(\n", num);
        printf("    input           [2:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,	\n", num - 1);
        printf("    input           [%d:0]                               in_4_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_5_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_6_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_7_i  ,	 \n", num - 1);
        printf("    output          [%d:0]                               out_o   \n", num - 1);
        printf("    );\n\n");

        for (int i = 0; i < num; ++i)
        {
            printf("    mux_8i1o_1b_no_dly out_b%d_inst (\n", i);
            printf("        .sel_i   (sel_i), \n");
            printf("        .in_0_i  (in_0_i[%d]), \n", i);
            printf("        .in_1_i  (in_1_i[%d]), \n", i);
            printf("        .in_2_i  (in_2_i[%d]), \n", i);
            printf("        .in_3_i  (in_3_i[%d]), \n", i);
            printf("        .in_4_i  (in_4_i[%d]), \n", i);
            printf("        .in_5_i  (in_5_i[%d]), \n", i);
            printf("        .in_6_i  (in_6_i[%d]), \n", i);
            printf("        .in_7_i  (in_7_i[%d]), \n", i);
            printf("        .out_o   (out_o[%d])\n", i);
            printf("    );\n", i);
        }
        printf("\n");
        printf("endmodule\n");
    }
}
void mux_8i1o(int num)
{
    if (num == 1)
    {
        printf("module mux_8i1o_1b(\n");
        printf("    input                                               clk_i   ,\n");
        printf("    input                                               rst_i   ,\n");
        printf("    input                                               ce_i    ,\n");
        printf("    input           [2:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input                                               in_0_i  ,\n");
        printf("    input                                               in_1_i  ,\n");
        printf("    input                                               in_2_i  ,\n");
        printf("    input                                               in_3_i  ,\n");
        printf("    input                                               in_4_i  ,\n");
        printf("    input                                               in_5_i  ,\n");
        printf("    input                                               in_6_i  ,\n");
        printf("    input                                               in_7_i  ,	 	 \n");
        printf("    output                                              out_o   \n");
        printf("    );\n");
        printf("\n");
        printf("    wire            [1:0]                               lut ;\n");
        printf("    wire                                                mux_out ;\n");
        printf("\n");
        printf("    LUT6 #(\n");
        printf("        .INIT(64'hff00f0f0ccccaaaa) \n");
        printf("    ) LUT_0 (\n");
        printf("        .O(lut[0]),   \n");
        printf("        .I0(in_0_i), // LUT input\n");
        printf("        .I1(in_1_i), // LUT input\n");
        printf("        .I2(in_2_i), // LUT input\n");
        printf("        .I3(in_3_i), // LUT input\n");
        printf("        .I4(sel_i[0]), // LUT input\n");
        printf("        .I5(sel_i[1])  // LUT input\n");
        printf("    );\n");
        printf("\n");
        printf("    LUT6 #(\n");
        printf("        .INIT(64'hff00f0f0ccccaaaa)  // Specify LUT Contents\n");
        printf("    ) LUT_1 (\n");
        printf("        .O(lut[1]),   \n");
        printf("        .I0(in_4_i), // LUT input\n");
        printf("        .I1(in_5_i), // LUT input\n");
        printf("        .I2(in_6_i), // LUT input\n");
        printf("        .I3(in_7_i), // LUT input\n");
        printf("        .I4(sel_i[0]), // LUT input\n");
        printf("        .I5(sel_i[1])  // LUT input\n");
        printf("    );\n");
        printf("        \n");
        printf("    MUXF5 MUX (\n");
        printf("        .O(mux_out),    // Output of MUX to general routing\n");
        printf("        .I0(lut[0]),  // Input (tie directly to the output of LUT4)\n");
        printf("        .I1(lut[1]),  // Input (tie directoy to the output of LUT4)\n");
        printf("        .S(sel_i[2])     // Input select to MUX\n");
        printf("    );\n");
        printf("\n");
        printf("   	FDRE #(\n");
        printf("      	.INIT(1'b0)\n");
        printf("    	) inst_out_o(\n");
        printf("        .C  (clk_i),\n");
        printf("        .R  (rst_i),\n");
        printf("        .CE (ce_i),\n");
        printf("        .D  (mux_out),\n");
        printf("        .Q  (out_o)\n");
        printf("    );\n");
        printf("	 \n");
        printf("endmodule        \n");
    }
    else
    {
        printf("module mux_8i1o_%db(\n", num);
        printf("    input                                               clk_i   ,\n");
        printf("    input                                               rst_i   ,\n");
        printf("    input                                               ce_i    ,\n");
        printf("\n");
        printf("    input           [2:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,	\n", num - 1);
        printf("    input           [%d:0]                               in_4_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_5_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_6_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_7_i  ,	 \n", num - 1);
        printf("    output          [%d:0]                               out_o   \n", num - 1);
        printf("    );\n\n");

        for (int i = 0; i < num; ++i)
        {
            printf("    mux_8i1o_1b out_b%d_inst (\n", i);
            printf("        .clk_i   (clk_i), \n");
            printf("        .rst_i   (rst_i), \n");
            printf("        .ce_i    (ce_i), \n");
            printf("        .sel_i   (sel_i), \n");
            printf("        .in_0_i  (in_0_i[%d]), \n", i);
            printf("        .in_1_i  (in_1_i[%d]), \n", i);
            printf("        .in_2_i  (in_2_i[%d]), \n", i);
            printf("        .in_3_i  (in_3_i[%d]), \n", i);
            printf("        .in_4_i  (in_4_i[%d]), \n", i);
            printf("        .in_5_i  (in_5_i[%d]), \n", i);
            printf("        .in_6_i  (in_6_i[%d]), \n", i);
            printf("        .in_7_i  (in_7_i[%d]), \n", i);
            printf("        .out_o   (out_o[%d])\n", i);
            printf("    );\n", i);
        }
        printf("\n");
        printf("endmodule\n");
    }
}
void mux_16i1o(int num)
{
    if (num == 1)
    {
        printf("module mux_16i1o_1b(\n");
        printf("    clk_i,\n");
        printf("    rst_i,\n");
        printf("    ce_i,\n");
        printf("\n");
        printf("    in_0_i,\n");
        printf("    in_1_i,\n");
        printf("    in_2_i,\n");
        printf("    in_3_i,    \n");
        printf("    in_4_i,\n");
        printf("    in_5_i,\n");
        printf("    in_6_i,\n");
        printf("    in_7_i,     \n");
        printf("    in_8_i,\n");
        printf("    in_9_i,\n");
        printf("    in_10_i,\n");
        printf("    in_11_i,    \n");
        printf("    in_12_i,\n");
        printf("    in_13_i,\n");
        printf("    in_14_i,\n");
        printf("    in_15_i,	 	 \n");
        printf("\n");
        printf("    sel_i,\n");
        printf("	 \n");
        printf("    out_o\n");
        printf("    );\n");
        printf("\n");
        printf("    input  clk_i;\n");
        printf("    input  rst_i;\n");
        printf("    input  ce_i;\n");
        printf("\n");
        printf("    input  in_0_i;\n");
        printf("    input  in_1_i;\n");
        printf("    input  in_2_i;\n");
        printf("    input  in_3_i;    \n");
        printf("    input  in_4_i;\n");
        printf("    input  in_5_i;\n");
        printf("    input  in_6_i;\n");
        printf("    input  in_7_i;     \n");
        printf("    input  in_8_i;\n");
        printf("    input  in_9_i;\n");
        printf("    input  in_10_i;\n");
        printf("    input  in_11_i;    \n");
        printf("    input  in_12_i;\n");
        printf("    input  in_13_i;\n");
        printf("    input  in_14_i;\n");
        printf("    input  in_15_i;          \n");
        printf("\n");
        printf("    input  [3:0]  sel_i;\n");
        printf("     \n");
        printf("    output out_o;\n");
        printf("\n");
        printf("    wire   lut6_out_0;\n");
        printf("    wire   lut6_out_1;\n");
        printf("    wire   lut6_out_2;\n");
        printf("    wire   lut6_out_3;\n");
        printf("     \n");
        printf("    wire   mux_out_0;\n");
        printf("    wire   mux_out_1;\n");
        printf("    wire   mux_out;\n");
        printf("     \n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_0_inst (\n");
        printf("      .O6(lut6_out_0), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_0_i), // 1-bit LUT input\n");
        printf("      .I1(in_1_i), // 1-bit LUT input\n");
        printf("      .I2(in_2_i), // 1-bit LUT input\n");
        printf("      .I3(in_3_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_1_inst (\n");
        printf("      .O6(lut6_out_1), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_4_i), // 1-bit LUT input\n");
        printf("      .I1(in_5_i), // 1-bit LUT input\n");
        printf("      .I2(in_6_i), // 1-bit LUT input\n");
        printf("      .I3(in_7_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   MUXF7_L MUXF7_0_inst (\n");
        printf("      .LO(mux_out_0),  // Output of MUX to local routing\n");
        printf("      .I0(lut6_out_0),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .I1(lut6_out_1),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .S(sel_i[2])     // Input select to MUX\n");
        printf("   );    \n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_2_inst (\n");
        printf("      .O6(lut6_out_2), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_8_i), // 1-bit LUT input\n");
        printf("      .I1(in_9_i), // 1-bit LUT input\n");
        printf("      .I2(in_10_i), // 1-bit LUT input\n");
        printf("      .I3(in_11_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_3_inst (\n");
        printf("      .O6(lut6_out_3), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_12_i), // 1-bit LUT input\n");
        printf("      .I1(in_13_i), // 1-bit LUT input\n");
        printf("      .I2(in_14_i), // 1-bit LUT input\n");
        printf("      .I3(in_15_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );    \n");
        printf("   MUXF7_L MUXF7_1_inst (\n");
        printf("      .LO(mux_out_1),  // Output of MUX to local routing\n");
        printf("      .I0(lut6_out_2),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .I1(lut6_out_3),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .S(sel_i[2])     // Input select to MUX\n");
        printf("   );\n");
        printf("   MUXF8 MUXF8_inst (\n");
        printf("      .O(mux_out),    // Output of MUX to general routing\n");
        printf("      .I0(mux_out_0),  // Input (tie to MUXF7 LO out)\n");
        printf("      .I1(mux_out_1),  // Input (tie to MUXF7 LO out)\n");
        printf("      .S(sel_i[3])     // Input select to MUX\n");
        printf("   );    \n");
        printf("    \n");
        printf("   FDCE #(\n");
        printf("      .INIT(1'b0) // Initial value of register (1'b0 or 1'b1)\n");
        printf("   ) FDCE_inst (\n");
        printf("      .Q(out_o),      // 1-bit Data output\n");
        printf("      .C(clk_i),      // 1-bit Clock input\n");
        printf("      .CE(ce_i),    // 1-bit Clock enable input\n");
        printf("      .CLR(rst_i),  // 1-bit Asynchronous clear input\n");
        printf("      .D(mux_out)       // 1-bit Data input\n");
        printf("   );\n");
        printf("    \n");
        printf("endmodule\n");
    }
    else
    {
        printf("module mux_16i1o_%db(\n", num);
        printf("    input                                               clk_i   ,\n");
        printf("    input                                               rst_i   ,\n");
        printf("    input                                               ce_i    ,\n");
        printf("\n");
        printf("    input           [3:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,	\n", num - 1);
        printf("    input           [%d:0]                               in_4_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_5_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_6_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_7_i  ,	 \n", num - 1);
        printf("    input           [%d:0]                               in_8_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_9_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_10_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_11_i ,	\n", num - 1);
        printf("    input           [%d:0]                               in_12_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_13_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_14_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_15_i ,	 	 \n", num - 1);
        printf("    output          [%d:0]                               out_o   \n", num - 1);
        printf("    );\n\n");

        for (int i = 0; i < num; ++i)
        {
            printf("    mux_16i1o_1b out_b%d_inst (\n", i);
            printf("        .clk_i   (clk_i), \n");
            printf("        .rst_i   (rst_i), \n");
            printf("        .ce_i    (ce_i), \n");
            printf("        .sel_i   (sel_i), \n");
            printf("        .in_0_i  (in_0_i[%d]), \n", i);
            printf("        .in_1_i  (in_1_i[%d]), \n", i);
            printf("        .in_2_i  (in_2_i[%d]), \n", i);
            printf("        .in_3_i  (in_3_i[%d]), \n", i);
            printf("        .in_4_i  (in_4_i[%d]), \n", i);
            printf("        .in_5_i  (in_5_i[%d]), \n", i);
            printf("        .in_6_i  (in_6_i[%d]), \n", i);
            printf("        .in_7_i  (in_7_i[%d]), \n", i);
            printf("        .in_8_i  (in_8_i[%d]), \n", i);
            printf("        .in_9_i  (in_9_i[%d]), \n", i);
            printf("        .in_10_i (in_10_i[%d]), \n", i);
            printf("        .in_11_i (in_11_i[%d]), \n", i);
            printf("        .in_12_i (in_12_i[%d]), \n", i);
            printf("        .in_13_i (in_13_i[%d]), \n", i);
            printf("        .in_14_i (in_14_i[%d]), \n", i);
            printf("        .in_15_i (in_15_i[%d]), \n", i);
            printf("        .out_o   (out_o[%d])\n", i);
            printf("    );\n", i);
        }
        printf("\n");
        printf("endmodule\n");
    }
}

void mux_16i1o_no_dly(const int num)
{
    if (num == 1)
    {
        printf("module mux_16i1o_1b_no_dly(\n");
        printf("    clk_i,\n");
        printf("    rst_i,\n");
        printf("    ce_i,\n");
        printf("\n");
        printf("    in_0_i,\n");
        printf("    in_1_i,\n");
        printf("    in_2_i,\n");
        printf("    in_3_i,\n");
        printf("    in_4_i,\n");
        printf("    in_5_i,\n");
        printf("    in_6_i,\n");
        printf("    in_7_i,\n");
        printf("    in_8_i,\n");
        printf("    in_9_i,\n");
        printf("    in_10_i,\n");
        printf("    in_11_i,\n");
        printf("    in_12_i,\n");
        printf("    in_13_i,\n");
        printf("    in_14_i,\n");
        printf("    in_15_i,\n");
        printf("\n");
        printf("    sel_i,\n");
        printf("\n");
        printf("    out_o\n");
        printf("    );\n");
        printf("\n");
        printf("    input  clk_i;\n");
        printf("    input  rst_i;\n");
        printf("    input  ce_i;\n");
        printf("\n");
        printf("    input  in_0_i;\n");
        printf("    input  in_1_i;\n");
        printf("    input  in_2_i;\n");
        printf("    input  in_3_i;\n");
        printf("    input  in_4_i;\n");
        printf("    input  in_5_i;\n");
        printf("    input  in_6_i;\n");
        printf("    input  in_7_i;\n");
        printf("    input  in_8_i;\n");
        printf("    input  in_9_i;\n");
        printf("    input  in_10_i;\n");
        printf("    input  in_11_i;\n");
        printf("    input  in_12_i;\n");
        printf("    input  in_13_i;\n");
        printf("    input  in_14_i;\n");
        printf("    input  in_15_i;\n");
        printf("\n");
        printf("    input  [3:0]  sel_i;\n");
        printf("\n");
        printf("    output out_o;\n");
        printf("\n");
        printf("    wire   lut6_out_0;\n");
        printf("    wire   lut6_out_1;\n");
        printf("    wire   lut6_out_2;\n");
        printf("    wire   lut6_out_3;\n");
        printf("\n");
        printf("    wire   mux_out_0;\n");
        printf("    wire   mux_out_1;\n");
        printf("\n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_0_inst (\n");
        printf("      .O6(lut6_out_0), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_0_i), // 1-bit LUT input\n");
        printf("      .I1(in_1_i), // 1-bit LUT input\n");
        printf("      .I2(in_2_i), // 1-bit LUT input\n");
        printf("      .I3(in_3_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_1_inst (\n");
        printf("      .O6(lut6_out_1), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_4_i), // 1-bit LUT input\n");
        printf("      .I1(in_5_i), // 1-bit LUT input\n");
        printf("      .I2(in_6_i), // 1-bit LUT input\n");
        printf("      .I3(in_7_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   MUXF7_L MUXF7_0_inst (\n");
        printf("      .LO(mux_out_0),  // Output of MUX to local routing\n");
        printf("      .I0(lut6_out_0),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .I1(lut6_out_1),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .S(sel_i[2])     // Input select to MUX\n");
        printf("   );\n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_2_inst (\n");
        printf("      .O6(lut6_out_2), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_8_i), // 1-bit LUT input\n");
        printf("      .I1(in_9_i), // 1-bit LUT input\n");
        printf("      .I2(in_10_i), // 1-bit LUT input\n");
        printf("      .I3(in_11_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   LUT6_2 #(\n");
        printf("      .INIT(64'hff00f0f0ccccaaaa) // Specify LUT Contents\n");
        printf("   ) LUT6_3_inst (\n");
        printf("      .O6(lut6_out_3), // 1-bit LUT6 output\n");
        printf("      .O5(), // 1-bit lower LUT5 output\n");
        printf("      .I0(in_12_i), // 1-bit LUT input\n");
        printf("      .I1(in_13_i), // 1-bit LUT input\n");
        printf("      .I2(in_14_i), // 1-bit LUT input\n");
        printf("      .I3(in_15_i), // 1-bit LUT input\n");
        printf("      .I4(sel_i[0]), // 1-bit LUT input\n");
        printf("      .I5(sel_i[1])  // 1-bit LUT input (fast MUX select only available to O6 output)\n");
        printf("   );\n");
        printf("   MUXF7_L MUXF7_1_inst (\n");
        printf("      .LO(mux_out_1),  // Output of MUX to local routing\n");
        printf("      .I0(lut6_out_2),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .I1(lut6_out_3),  // Input (tie to LUT6 O6 pin)\n");
        printf("      .S(sel_i[2])     // Input select to MUX\n");
        printf("   );\n");
        printf("   MUXF8 MUXF8_inst (\n");
        printf("      .O(out_o),    // Output of MUX to general routing\n");
        printf("      .I0(mux_out_0),  // Input (tie to MUXF7 LO out)\n");
        printf("      .I1(mux_out_1),  // Input (tie to MUXF7 LO out)\n");
        printf("      .S(sel_i[3])     // Input select to MUX\n");
        printf("   );\n");
        printf("\n");
        printf("endmodule\n");
    }
    else
    {
        printf("module mux_16i1o_%db_no_dly(\n", num);
        printf("    input           [3:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,	\n", num - 1);
        printf("    input           [%d:0]                               in_4_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_5_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_6_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_7_i  ,	 \n", num - 1);
        printf("    input           [%d:0]                               in_8_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_9_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_10_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_11_i ,	\n", num - 1);
        printf("    input           [%d:0]                               in_12_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_13_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_14_i ,\n", num - 1);
        printf("    input           [%d:0]                               in_15_i ,	 	 \n", num - 1);
        printf("    output          [%d:0]                               out_o   \n", num - 1);
        printf("    );\n\n");

        for (int i = 0; i < num; ++i)
        {
            printf("    mux_16i1o_1b_no_dly out_b%d_inst (\n", i);
            printf("        .sel_i   (sel_i), \n");
            printf("        .in_0_i  (in_0_i[%d]), \n", i);
            printf("        .in_1_i  (in_1_i[%d]), \n", i);
            printf("        .in_2_i  (in_2_i[%d]), \n", i);
            printf("        .in_3_i  (in_3_i[%d]), \n", i);
            printf("        .in_4_i  (in_4_i[%d]), \n", i);
            printf("        .in_5_i  (in_5_i[%d]), \n", i);
            printf("        .in_6_i  (in_6_i[%d]), \n", i);
            printf("        .in_7_i  (in_7_i[%d]), \n", i);
            printf("        .in_8_i  (in_8_i[%d]), \n", i);
            printf("        .in_9_i  (in_9_i[%d]), \n", i);
            printf("        .in_10_i (in_10_i[%d]), \n", i);
            printf("        .in_11_i (in_11_i[%d]), \n", i);
            printf("        .in_12_i (in_12_i[%d]), \n", i);
            printf("        .in_13_i (in_13_i[%d]), \n", i);
            printf("        .in_14_i (in_14_i[%d]), \n", i);
            printf("        .in_15_i (in_15_i[%d]), \n", i);
            printf("        .out_o   (out_o[%d])\n", i);
            printf("    );\n", i);
        }
        printf("\n");
        printf("endmodule\n");
    }
}
void mux_32i1o(const int num)
{
    if (num == 0)
    {
        printf("module mux_32i1o_1b(\n");
        printf("    input                                               clk_i   ,\n");
        printf("    input                                               rst_i   ,\n");
        printf("    input                                               ce_i    ,\n");
        printf("    \n");
        printf("    input           [4:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input                                               in_0_i  ,\n");
        printf("    input                                               in_1_i  ,\n");
        printf("    input                                               in_2_i  ,\n");
        printf("    input                                               in_3_i  ,\n");
        printf("    input                                               in_4_i  ,\n");
        printf("    input                                               in_5_i  ,\n");
        printf("    input                                               in_6_i  ,\n");
        printf("    input                                               in_7_i  ,\n");
        printf("    input                                               in_8_i  ,\n");
        printf("    input                                               in_9_i  ,\n");
        printf("    input                                               in_10_i ,\n");
        printf("    input                                               in_11_i ,\n");
        printf("    input                                               in_12_i ,\n");
        printf("    input                                               in_13_i ,\n");
        printf("    input                                               in_14_i ,\n");
        printf("    input                                               in_15_i ,\n");
        printf("    input                                               in_16_i ,\n");
        printf("    input                                               in_17_i ,\n");
        printf("    input                                               in_18_i ,\n");
        printf("    input                                               in_19_i ,\n");
        printf("    input                                               in_20_i ,\n");
        printf("    input                                               in_21_i ,\n");
        printf("    input                                               in_22_i ,\n");
        printf("    input                                               in_23_i ,\n");
        printf("    input                                               in_24_i ,\n");
        printf("    input                                               in_25_i ,\n");
        printf("    input                                               in_26_i ,\n");
        printf("    input                                               in_27_i ,\n");
        printf("    input                                               in_28_i ,\n");
        printf("    input                                               in_29_i ,\n");
        printf("    input                                               in_30_i ,\n");
        printf("    input                                               in_31_i ,\n");
        printf("    output                                              out_o   \n");
        printf("    );\n");
        printf("\n");
        printf("    wire                                                mux_16l_out ;\n");
        printf("    wire                                                mux_16h_out ;\n");
        printf("\n");
        printf("    mux_16i1o_1b_no_dly inst_mux_16l_out (\n");
        printf("        .sel_i   (sel_i[3:0]),\n");
        printf("        .in_0_i  (in_0_i),\n");
        printf("        .in_1_i  (in_1_i),\n");
        printf("        .in_2_i  (in_2_i),\n");
        printf("        .in_3_i  (in_3_i),\n");
        printf("        .in_4_i  (in_4_i),\n");
        printf("        .in_5_i  (in_5_i),\n");
        printf("        .in_6_i  (in_6_i),\n");
        printf("        .in_7_i  (in_7_i),\n");
        printf("        .in_8_i  (in_8_i),\n");
        printf("        .in_9_i  (in_9_i),\n");
        printf("        .in_10_i (in_10_i),\n");
        printf("        .in_11_i (in_11_i),\n");
        printf("        .in_12_i (in_12_i),\n");
        printf("        .in_13_i (in_13_i),\n");
        printf("        .in_14_i (in_14_i),\n");
        printf("        .in_15_i (in_15_i),\n");
        printf("        .out_o   (mux_16l_out)\n");
        printf("    );\n");
        printf("    mux_16i1o_1b_no_dly inst_mux_16h_out (\n");
        printf("        .sel_i   (sel_i[3:0]),\n");
        printf("        .in_0_i  (in_16_i),\n");
        printf("        .in_1_i  (in_17_i),\n");
        printf("        .in_2_i  (in_18_i),\n");
        printf("        .in_3_i  (in_19_i),\n");
        printf("        .in_4_i  (in_20_i),\n");
        printf("        .in_5_i  (in_21_i),\n");
        printf("        .in_6_i  (in_22_i),\n");
        printf("        .in_7_i  (in_23_i),\n");
        printf("        .in_8_i  (in_24_i),\n");
        printf("        .in_9_i  (in_25_i),\n");
        printf("        .in_10_i (in_26_i),\n");
        printf("        .in_11_i (in_27_i),\n");
        printf("        .in_12_i (in_28_i),\n");
        printf("        .in_13_i (in_29_i),\n");
        printf("        .in_14_i (in_30_i),\n");
        printf("        .in_15_i (in_31_i),\n");
        printf("        .out_o   (mux_16h_out)\n");
        printf("    );\n");
        printf("\n");
        printf("    mux_2i1o_1b inst_out_o(\n");
        printf("        .clk_i   (clk_i),\n");
        printf("        .rst_i   (rst_i),\n");
        printf("        .ce_i    (ce_i),\n");
        printf("        .sel_i   (sel_i[4]),\n");
        printf("        .in_0_i  (mux_16l_out),\n");
        printf("        .in_1_i  (mux_16h_out),\n");
        printf("        .out_o   (out_o)\n");
        printf("    );\n");
        printf("    \n");
        printf("endmodule\n");
    }
    else
    {
        printf("module mux_32i1o_%db(\n", num);
        printf("    input                                               clk_i   ,\n");
        printf("    input                                               rst_i   ,\n");
        printf("    input                                               ce_i    ,\n");
        printf("    input           [4:0]                               sel_i   ,\n");
        printf("\n");
        printf("    input           [%d:0]                               in_0_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_1_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_2_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_3_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_4_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_5_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_6_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_7_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_8_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_9_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_10_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_11_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_12_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_13_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_14_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_15_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_16_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_17_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_18_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_19_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_20_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_21_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_22_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_23_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_24_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_25_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_26_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_27_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_28_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_29_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_30_i  ,\n", num - 1);
        printf("    input           [%d:0]                               in_31_i  ,\n", num - 1);
        printf("    \n");
        printf("    output          [%d:0]                               out_o\n", num - 1);
        printf("    );\n");
        printf("\n");
        printf("    wire            [%d:0]                               mux_16l_out ;\n", num - 1);
        printf("    wire            [%d:0]                               mux_16h_out ;\n", num - 1);
        printf("\n");

        for (int i = 0; i < num; ++i)
        {
            printf("    mux_16i1o_1b_no_dly inst_mux_16l_out_b%d (\n", i);
            printf("        .sel_i   (sel_i[3:0]),\n");
            printf("        .in_0_i  (in_0_i[%d]),\n", i);
            printf("        .in_1_i  (in_1_i[%d]),\n", i);
            printf("        .in_2_i  (in_2_i[%d]),\n", i);
            printf("        .in_3_i  (in_3_i[%d]),\n", i);
            printf("        .in_4_i  (in_4_i[%d]),\n", i);
            printf("        .in_5_i  (in_5_i[%d]),\n", i);
            printf("        .in_6_i  (in_6_i[%d]),\n", i);
            printf("        .in_7_i  (in_7_i[%d]),\n", i);
            printf("        .in_8_i  (in_8_i[%d]),\n", i);
            printf("        .in_9_i  (in_9_i[%d]),\n", i);
            printf("        .in_10_i (in_10_i[%d]),\n", i);
            printf("        .in_11_i (in_11_i[%d]),\n", i);
            printf("        .in_12_i (in_12_i[%d]),\n", i);
            printf("        .in_13_i (in_13_i[%d]),\n", i);
            printf("        .in_14_i (in_14_i[%d]),\n", i);
            printf("        .in_15_i (in_15_i[%d]),\n", i);
            printf("        .out_o   (mux_16l_out[%d])\n", i);
            printf("    );\n");
            printf("    mux_16i1o_1b_no_dly inst_mux_16h_out_b%d (\n", i);
            printf("        .sel_i   (sel_i[3:0]),\n");
            printf("        .in_0_i  (in_16_i[%d]),\n", i);
            printf("        .in_1_i  (in_17_i[%d]),\n", i);
            printf("        .in_2_i  (in_18_i[%d]),\n", i);
            printf("        .in_3_i  (in_19_i[%d]),\n", i);
            printf("        .in_4_i  (in_20_i[%d]),\n", i);
            printf("        .in_5_i  (in_21_i[%d]),\n", i);
            printf("        .in_6_i  (in_22_i[%d]),\n", i);
            printf("        .in_7_i  (in_23_i[%d]),\n", i);
            printf("        .in_8_i  (in_24_i[%d]),\n", i);
            printf("        .in_9_i  (in_25_i[%d]),\n", i);
            printf("        .in_10_i (in_26_i[%d]),\n", i);
            printf("        .in_11_i (in_27_i[%d]),\n", i);
            printf("        .in_12_i (in_28_i[%d]),\n", i);
            printf("        .in_13_i (in_29_i[%d]),\n", i);
            printf("        .in_14_i (in_30_i[%d]),\n", i);
            printf("        .in_15_i (in_31_i[%d]),\n", i);
            printf("        .out_o   (mux_16h_out[%d])\n", i);
            printf("    );\n");
            printf("\n");
            printf("    mux_2i1o_1b inst_out_o_b%d(\n", i);
            printf("        .clk_i   (clk_i),\n");
            printf("        .rst_i   (rst_i),\n");
            printf("        .ce_i    (ce_i),\n");
            printf("        .sel_i   (sel_i[4]),\n");
            printf("        .in_0_i  (mux_16l_out[%d]),\n", i);
            printf("        .in_1_i  (mux_16h_out[%d]),\n", i);
            printf("        .out_o   (out_o[%d])\n", i);
            printf("    );\n");
        }
        printf("    \n");
        printf("endmodule\n");
    }
}

void delay_gen(std::string s, const int dly_clk, const int bit_num)
{
    if (s == "dly")
        printf("    dly_%d_%db inst_(\n        .clk_i (clk_i),\n        .rst_i (rst_i),\n        .ce_i  (1'b1),\n\n        .in_i  (),\n        .out_o ()\n    );\n", dly_clk, bit_num);
    else
    {
        if (bit_num != 1)
            printf("module dly_%d_%db(\n    input clk_i,\n    input rst_i,\n    input ce_i,\n\n    input[%d:0] in_i,\n    output[%d:0] out_o\n    );\n", dly_clk, bit_num, bit_num - 1, bit_num - 1);
        else
            printf("module dly_%d_%db(\n    input clk_i,\n    input rst_i,\n    input ce_i,\n\n    input in_i,\n    output out_o\n    );\n", dly_clk, bit_num);
        printf("\n");
        if (dly_clk > 1)
        {
            for (int i = 1; i < dly_clk; ++i)
                if (bit_num != 1)
                    printf("    wire[%d:0] in_dly_%d;\n", bit_num - 1, i);
                else
                    printf("    wire in_dly_%d;\n", i);
            printf("\n");
        }
        /*         FDE #(
      .INIT(1'b0) // Initial value of register (1'b0 or 1'b1)
   ) in_dly_2_4_inst (
      .Q(in_dly_2[4]),      // Data output
      .C(clk_i),      // Clock input
      .CE(ce_i),    // Clock enable input
      .D(in_dly_1[4])       // Data input
   ); */
        for (int i = 0; i < dly_clk; ++i)
        {
            if (dly_clk == 1)
                for (int j = 0; j < bit_num; ++j)
                    printf("    FDRE #(   \n       .INIT(1'b0) \n    ) inst_out_o_b%d (\n       .C (clk_i),      \n       .R (rst_i),      // 1-bit Synchronous clear input   \n       .CE (ce_i),    \n       .D (in_i[%d]),       \n       .Q (out_o[%d])      \n    );\n", j, j, j);
            else if (bit_num == 1)
            {
                if (i == 0 && dly_clk != 1)
                    printf("    FDRE #(   \n       .INIT(1'b0) \n    ) inst_in_dly_%d (\n       .C (clk_i),      \n       .R (rst_i),      // 1-bit Synchronous clear input   \n       .CE (ce_i),    \n       .D (in_i),       \n       .Q (in_dly_%d)      \n    );\n", i + 1, i + 1);
                else if (i != dly_clk - 1)
                    printf("    FDRE #(   \n       .INIT(1'b0)  \n    ) inst_in_dly_%d (\n       .C (clk_i),      \n       .R (rst_i),      // 1-bit Synchronous clear input   \n       .CE (ce_i),   \n       .D (in_dly_%d),       \n       .Q (in_dly_%d)         \n    );\n", i + 1, i, i + 1);
                else
                    printf("    FDRE #(\n        .INIT(1'b0)  \n    ) inst_out_o (\n        .C (clk_i),      \n       .R (rst_i),      // 1-bit Synchronous clear input\n        .CE (ce_i),    \n        .D (in_dly_%d),       \n        .Q (out_o)      \n    );\n", i);
            }
            else
            {
                if (i == 0 && dly_clk != 1)
                {
                    printf("    dly_1_%db inst_in_dly_%d (\n", bit_num, i + 1);
                    printf("        .clk_i (clk_i),\n");
                    printf("        .rst_i (rst_i),\n");
                    printf("        .ce_i  (ce_i),\n");
                    printf("        .in_i  (in_i),\n");
                    printf("        .out_o (in_dly_%d)\n", i + 1);
                    printf("    );	\n");
                }
                else if (i == 0 && dly_clk == 1)
                {
                    printf("    dly_1_%db inst_out_o (\n", bit_num);
                    printf("        .clk_i (clk_i),\n");
                    printf("        .rst_i (rst_i),\n");
                    printf("        .ce_i  (ce_i),\n");
                    printf("        .in_i  (in_i),\n");
                    printf("        .out_o (out_o)\n");
                    printf("    );	\n");
                }
                else if (i == dly_clk - 1)
                {
                    printf("    dly_1_%db inst_out_o (\n", bit_num);
                    printf("        .clk_i (clk_i),\n");
                    printf("        .rst_i (rst_i),\n");
                    printf("        .ce_i  (1'b1),\n");
                    printf("        .in_i  (in_dly_%d),\n", i);
                    printf("        .out_o (out_o)\n", i);
                    printf("    );	\n");
                }
                else
                {
                    printf("    dly_1_%db inst_in_dly_%d (\n", bit_num, i + 1);
                    printf("        .clk_i (clk_i),\n");
                    printf("        .rst_i (rst_i),\n");
                    printf("        .ce_i  (1'b1),\n");
                    printf("        .in_i  (in_dly_%d),\n", i);
                    printf("        .out_o (in_dly_%d)\n", i + 1);
                    printf("    );	\n");
                }
            }
            printf("\n");
        }
        printf("endmodule\n");
    }
}
void spilt(int num, int length)
{

    for (int i = 0; i < num; ++i)
        printf("[%d:%d]\n", (i + 1) * length - 1, i * length);
}
void spilt(std::string l_name, int num, int length)
{

    for (int i = 0; i < num; ++i)
    {
        std::cout << l_name;
        printf("[%d:%d]\n", (i + 1) * length - 1, i * length);
    }
}
void spilt(int group, int num, int length, std::string cmd)
{
    if (cmd == "r")
    {
        for (int i = num - 1; i >= 0; --i)
            for (int j = 0; j < group; ++j)
            {
                if (length != 1)
                    printf("[%d:%d]\n", (i + 1) * length - 1, i * length);
                else
                    printf("%d ", i * length);
            }
        if (length == 1)
            printf("\n");
    }
    else
        for (int i = 0; i < num; ++i)
            for (int j = 0; j < group; ++j)
                printf("[%d:%d]\n", (i + 1) * length - 1, i * length);
}
void spilt(int num, int length, std::string cmd)
{
    if (cmd == "r")
    {
        for (int i = num - 1; i >= 0; --i)
        {
            if (length != 1)
                printf("[%d:%d]\n", (i + 1) * length - 1, i * length);
            else
                printf("%d ", i * length);
        }
        if (length == 1)
            printf("\n");
    }
    else
        for (int i = 0; i < num; ++i)
            printf("[%d:%d]\n", (i + 1) * length - 1, i * length);
}
void spilt(int num, int length, std::string cmd, int offset)
{
    if (cmd == "r")
    {
        for (int i = num - 1; i >= 0; --i)
        {
            if (length != 1)
                printf("[%d:%d]\n", i * length + offset - 1, i * length);
            else
                printf("%d ", i * length);
        }
        if (length == 1)
            printf("\n");
    }
    else
        for (int i = 0; i < num; ++i)
            printf("[%d:%d]\n", i * length + offset - 1, i * length);
}
void spilt(int num, int length, std::string cmd, int offset_1, int offset_2)
{
    if (cmd == "r")
    {
        for (int i = num - 1; i >= 0; --i)
        {
            if (length != 1)
                printf("[%d:%d]\n", i * length + offset_1 - 1, i * length);
            else
                printf("%d ", i * length);
        }
        for (int i = num - 1; i >= 0; --i)
        {
            if (length != 1)
                printf("[%d:%d]\n", i * length + offset_1 + offset_2 - 1, i * length + offset_1);
            else
                printf("%d ", i * length);
        }
        if (length == 1)
            printf("\n");
    }
    else
    {
        for (int i = 0; i < num; ++i)
        {
            if (length != 1)
                printf("[%d:%d]\n", i * length + offset_1 - 1, i * length);
            else
                printf("%d ", i * length);
        }
        for (int i = 0; i < num; ++i)
        {
            if (length != 1)
                printf("[%d:%d]\n", i * length + offset_1 + offset_2 - 1, i * length + offset_1);
            else
                printf("%d ", i * length);
        }
    }
}
void spilt(std::string l_name, int num, int length, std::string r_name, std::string cmd, int offset)
{
    if (cmd == "r")
    {
        for (int i = num - 1; i >= 0; --i)
        {
            if (length != 1)
            {
                std::cout << l_name;
                printf("[%d:%d]\n", i * length + offset - 1, i * length);
                std::cout << r_name;
                printf("\n");
            }
            else
            {
                std::cout << l_name;
                printf("%d", i * length);
                std::cout << r_name;
                printf("\n");
            }
        }
        if (length == 1)
            printf("\n");
    }
    else
        for (int i = 0; i < num; ++i)
        {
            std::cout << l_name;
            printf("[%d:%d]\n", i * length + offset - 1, i * length);
            std::cout << r_name;
            printf("\n");
        }
}
void spilt(std::string l_name, int num, int length, std::string r_name, std::string cmd)
{
    if (cmd == "r")
    {
        for (int i = num - 1; i >= 0; --i)
        {
            if (length != 1)
            {
                std::cout << l_name;
                printf("[%d:%d]", (i + 1) * length - 1, i * length);
                std::cout << r_name;
                printf("\n");
            }
            else
            {
                std::cout << l_name;
                printf("%d", i * length);
                std::cout << r_name;
                printf("\n");
            }
        }
        if (length == 1)
            printf("\n");
    }
    else
        for (int i = 0; i < num; ++i)
        {
            std::cout << l_name;
            printf("[%d:%d]", (i + 1) * length - 1, i * length);
            std::cout << r_name;
            printf("\n");
        }
}

void dupulicate_text(const int num, bool is_plus_zero = false)
{
    std::vector<std::string> text;
    for (std::string cur_s; getline(std::cin, cur_s);)
        text.push_back(cur_s);

    system("cls");
    for (int i = 0; i < num; ++i)
    {
        for (auto &el_j : text)
        {
            for (auto &el_k : el_j)
                if (el_k != '$')
                    printf("%c", el_k);
                else
                {
                    if (num < 10)
                        printf("%d", i);
                    else if (i < 10 && is_plus_zero)
                        printf("0%d", i);
                    else
                        printf("%d", i);
                }
            printf("\n");
        }
        // printf("\n");
    }
}
void inverse_print(const int num, bool is_plus_zero = false)
{
    std::vector<std::string> text;
    for (std::string cur_s; getline(std::cin, cur_s);)
        text.push_back(cur_s);

    system("cls");
    for (int i = num - 1; i >= 0; --i)
    {
        for (auto &el_j : text)
        {
            for (auto &el_k : el_j)
                if (el_k != '$')
                    printf("%c", el_k);
                else
                {
                    if (num < 10)
                        printf("%d", i);
                    else if (i < 10 && is_plus_zero)
                        printf("0%d", i);
                    else
                        printf("%d", i);
                }
            printf("\n");
        }
        // printf("\n");
    }
}

void inverse_print(const int num, int row_num = 0)
{
    std::vector<std::string> text;
    for (std::string cur_s; getline(std::cin, cur_s);)
        text.push_back(cur_s);

    system("cls");
    for (int i = num - 1; i >= 0; --i)
    {
        for (auto &el_j : text)
        {
            for (auto &el_k : el_j)
                if (el_k != '$')
                    printf("%c", el_k);
                else
                    printf("%d", i);
            if(row_num!=0)
                printf(", ", i);
            if (((num-1 - i) + 1) % row_num == 0)
                printf("\n");
        }
        // printf("\n");
    }
}
/*
    FDRE #(
        .INIT(1'b0)
        ) inst_(
        .C  (clk_i),
        .R  (rst_i),
        .CE (1'b1),
        .D  (),
        .Q  ()
    );
*/
void gen_shift_reg(int num)
{
    printf("module shift_reg_%db(\n", num);
    printf("    input                                               clk_i   ,\n");
    printf("    input                                               rst_i   ,\n");
    printf("    input                                               ce_i    ,\n");
    printf("\n");
    printf("    input                                               in_i   ,\n");
    if (num < 10)
        printf("    output          [%d:0]                               out_o  \n", num - 1);
    else if (num < 100)
        printf("    output          [%d:0]                              out_o  \n", num - 1);
    else
        printf("    output          [%d:0]                             out_o  \n", num - 1);
    printf("    );\n");
    printf("\n");
    printf("    FDRE #(\n");
    printf("        .INIT(1'b0)\n");
    printf("    ) inst_dout_b0 (\n");
    printf("        .C  (clk_i),     \n");
    printf("        .R  (rst_i),      // 1-bit Synchronous clear input\n");
    printf("        .CE (ce_i),    \n");
    printf("        .D  (in_i),       \n");
    printf("        .Q  (out_o[0])     \n");
    printf("    );\n");

    for (int i = 1; i < num; ++i)
    {
        printf("    FDRE #(\n");
        printf("        .INIT(1'b0)\n");
        printf("    ) inst_dout_b%d (\n", i);
        printf("        .C  (clk_i),     \n");
        printf("        .R  (rst_i),      // 1-bit Synchronous clear input\n");
        printf("        .CE (ce_i),    \n");
        printf("        .D  (out_o[%d]),       \n", i - 1);
        printf("        .Q  (out_o[%d])     \n", i);
        printf("    );\n");
    }
    printf("\n");
    printf("endmodule\n");
}
void gen_circ_shift_reg(int num)
{
    printf("module circ_shift_reg_%db(\n", num);
    printf("    input                                               clk_i   ,\n");
    printf("    input                                               rst_i   ,\n");
    printf("    input                                               ce_i    ,\n");
    printf("\n");
    // if (num < 10)
    //     printf("    input           [%d:0]                               init_i  ,\n", num - 1);
    // else if (num < 100)
    //     printf("    input           [%d:0]                              init_i  ,\n", num - 1);
    // else
    //     printf("    input           [%d:0]                             init_i  ,\n", num - 1);
    // printf("\n");
    if (num < 10)
        printf("    output          [%d:0]                               out_o  \n", num - 1);
    else if (num < 100)
        printf("    output          [%d:0]                              out_o  \n", num - 1);
    else
        printf("    output          [%d:0]                             out_o  \n", num - 1);
    printf("    );\n");
    printf("\n");

    printf("    FDSE #(\n");
    printf("        .INIT(1'b1)\n");
    printf("    ) inst_out_b0 (\n");
    printf("        .C  (clk_i),     \n");
    printf("        .S  (rst_i),      // 1-bit Synchronous clear input\n");
    printf("        .CE (ce_i),    \n");
    printf("        .D  (out_o[%d]),       \n", num - 1);
    printf("        .Q  (out_o[0])     \n");
    printf("    );\n");
    printf("\n");

    for (int i = 1; i < num; ++i)
    {
        printf("    FDRE #(\n");
        printf("        .INIT(1'b0)\n", i);
        printf("    ) inst_out_b%d (\n", i);
        printf("        .C  (clk_i),     \n");
        printf("        .R  (rst_i),      // 1-bit Synchronous clear input\n");
        printf("        .CE (ce_i),    \n");
        printf("        .D  (out_o[%d]),       \n", i - 1);
        printf("        .Q  (out_o[%d])     \n", i);
        printf("    );\n");
    }
    printf("\n");
    printf("endmodule\n");
}
void reg_gen(int num)
{
    printf("module reg_%db(\n", num);
    printf("    input                                               clk_i   ,\n");
    printf("    input                                               rst_i   ,\n");
    printf("    input                                               ce_i    ,\n");
    printf("\n");
    printf("    input           [%d:0]                             din_i   ,\n", num - 1);
    printf("    output          [%d:0]                             dout_o  \n", num - 1);
    printf("    );\n");
    for (int i = 0; i < num; ++i)
    {
        printf("    FDRE #(\n");
        printf("        .INIT(1'b0) \n");
        printf("    ) inst_dout_o_b%d (\n", i);
        printf("        .C  (clk_i),     \n");
        printf("        .R  (rst_i ),      // 1-bit Synchronous clear input\n");
        printf("        .CE (ce_i),    \n");
        printf("        .D  (din_i[%d]),       \n", i);
        printf("        .Q  (dout_o[%d])\n", i);
        printf("    );\n");
    }
    printf("\n");
    printf("endmodule\n");
}
void gen_reg_array(int num, int wid)
{
    printf("    module reg_16b_array(\n");
    printf("    input                                               clk_i   ,\n");
    printf("    input                                               rst_i   ,\n");
    printf("    input                                               ce_i    ,\n");
    printf("\n");
    printf("    input           [%d:0]                              wr_pos  ,\n", num - 1);
    printf("\n");
    for (int i = 0; i < num; ++i)
        printf("    input           [%d:0]                              din_%d_i ,\n", wid - 1, i);
    printf("\n");
    for (int i = 0; i < num - 1; ++i)
        printf("    output          [%d:0]                              dout_%d_o    ,\n", wid - 1, i);
    printf("    output          [%d:0]                              dout_%d_o    \n", wid - 1, num - 1);
    printf("    );\n");
    printf("\n");

    for (int i = 0; i < num; ++i)
    {
        printf("    reg_%db inst_dout_%d_o(\n", wid, i);
        printf("        .clk_i  (clk_i),\n");
        printf("        .rst_i  (rst_i),\n");
        printf("        .ce_i   (ce_i & wr_pos[%d]),\n", i);
        printf("        .din_i  (din_%d_i[%d:0]),\n", i, wid - 1);
        printf("        .dout_o (dout_%d_o[%d:0])\n", i, wid - 1);
        printf("    );\n");
    }
    printf("\n");
    printf("endmodule\n");
}

void io_inverse()
{
    std::vector<std::string> s_s;
    std::vector<std::string> s_s_temp;
    for (std::string cur_s; getline(std::cin, cur_s);)
        s_s.push_back(cur_s);

    system("cls");
    for (auto &it : s_s)
    {
        std::string::size_type pos;
        if ((pos = it.find("input")) != std::string::npos)
            it.replace(pos, 5, "output");
        else if ((pos = it.find("output reg")) != std::string::npos)
            it.replace(pos, 10, "input");
        else if ((pos = it.find("output")) != std::string::npos)
            it.replace(pos, 6, "input");

        if ((pos = it.find("_i ")) != std::string::npos)
            it.replace(pos, 2, "_o ");
        else if ((pos = it.find("_o ")) != std::string::npos)
            it.replace(pos, 2, "_i ");
        std::cout << it << std::endl;
    }
}

int main()
{

    //   for (int i = 31; i >= 0; --i)
    //     {
    //         printf("branch_0_unitary_%d[15:0], ", i);
    //         if(i%4==0)
    //             printf("\n");
    //     }
    // inverse_print(32);
    //inverse_print(32,4);
    // dupulicate_text(32);
    //   double d = 1000.0 * (250.0 * 250.0 / (800.0 * 300) + 270.0 * 270.0 / (800.0 * 300) + 280.0 * 280.0 / (800.0 * 400) + 50.0 * 50.0 / (200.0 * 300) + 30.0 * 30.0 / (200.0 * 300) + 120.0 * 120.0 / (200.0 * 400.0) - 1.0);
    //    lut();
    //移位寄存器
    //  printf("    always @(posedge clk_i or posedge rst_i) begin\n		if(rst_i) begin\n			c_state <= s_idle;\n		end\n		else begin\n			c_state <= n_state;\n		end\n    end\n");

    // delay_gen("dl", 2, 10);
     auto_machine("check");
    // auto_machine();
    // io_inverse();
    // generate_testbench();
    // delay_gen("dl", 8, 15);

    //spilt(8, 64);
    //  spilt(4, 32, "rr", 18, 10);
    // spilt("grid_comp_rdf_data_i",32, 16);
    //  spilt("prod_0[", 17, 1, "],", "r");
    // spilt("grid_comp_rdf_data_i", 4, 128);
    // mux_2i1o(13);
    // mux_2i1o_no_dly(512);
    // mux_4i1o(9);
    // mux_4i1o_no_dly(3);
    // mux_8i1o(1);
    // mux_8i1o_no_dly(1);
    // mux_16i1o(1);
    // mux_16i1o_no_dly(2);
    // mux_32i1o(2);
    //  delay_gen("dl",8, 13);
    // gen_reg_array(32,16);
    // gen_shift_reg(4);
    // gen_circ_shift_reg(128);

    // reg_gen(128);
    system("pause");
}

代码如上,原因是用VSCODE,然后发现VSCODE居然没有类似的插件,这就很不好了,但发现自己拿手的只有C++,就自己花了几个小时写了一个,可能有点小bug,可能有的没有注意到,但灵活啊,可以根据自己要求自己改,我生成的testbench主要是根据自己的审美,测试的时候发现忘记输出一个module了 代码已改

效果如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值