Coverage Report

Created: 2026-11-02 16:55

/home/runner/work/LabNeura/LabNeura/src/labneura/eda/netlist/netlist_blif.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "labneura/eda/netlist.h"
2
3
#include <fstream>
4
#include <set>
5
#include <sstream>
6
#include <stdexcept>
7
#include <string>
8
#include <vector>
9
10
namespace labneura::eda {
11
namespace {
12
13
20.8k
std::string base_net(const std::string& net) {
14
20.8k
    const auto bracket = net.find('[');
15
20.8k
    return bracket == std::string::npos ? net : net.substr(0, bracket);
16
20.8k
}
17
18
// All bit-level nets of a port, in a stable order.
19
83
std::vector<std::string> port_bits(const Module& module, const std::string& port) {
20
83
    std::set<std::string> bits;
21
5.09k
    for (const auto& instance : module.get_instances()) {
22
20.8k
        for (const auto& connection : instance.get_connections()) {
23
20.8k
            if (base_net(connection.get_net()) == port) bits.insert(connection.get_net());
24
20.8k
        }
25
5.09k
    }
26
83
    if (bits.empty()) bits.insert(port);
27
83
    return {bits.begin(), bits.end()};
28
83
}
29
30
2.10k
const std::string* find_net(const Instance& instance, const std::string& pin) {
31
6.36k
    for (const auto& connection : instance.get_connections()) {
32
6.36k
        if (connection.get_pin() == pin) return &connection.get_net();
33
6.36k
    }
34
0
    return nullptr;
35
2.10k
}
36
37
8.14k
int hex_digit(char character) {
38
8.14k
    if (character >= '0' && character <= '9') return character - '0';
39
4.71k
    if (character >= 'A' && character <= 'F') return character - 'A' + 10;
40
0
    if (character >= 'a' && character <= 'f') return character - 'a' + 10;
41
0
    throw std::invalid_argument("invalid hex digit in LUT INIT");
42
0
}
43
44
}  // namespace
45
46
12
std::string to_blif(const Module& module) {
47
12
    std::ostringstream out;
48
12
    out << ".model " << module.get_name() << "\n";
49
12
    std::ostringstream inputs, outputs;
50
83
    for (const auto& port : module.get_ports()) {
51
83
        auto& target = port.get_direction() == PortDirection::Input ? inputs : outputs;
52
412
        for (const auto& bit : port_bits(module, port.get_name())) target << ' ' << bit;
53
83
    }
54
12
    out << ".inputs" << inputs.str() << "\n.outputs" << outputs.str() << "\n";
55
56
648
    for (const auto& instance : module.get_instances()) {
57
648
        const auto& type = instance.get_cell_type();
58
648
        if (type.rfind("LUT", 0) == 0 && type.size() == 4) {
59
372
            const std::size_t width = static_cast<std::size_t>(type[3] - '0');
60
372
            const auto init = instance.get_parameters().find("INIT");
61
372
            if (init == instance.get_parameters().end()) {
62
0
                throw std::runtime_error("LUT '" + instance.get_name() + "' has no INIT");
63
0
            }
64
372
            out << ".names";
65
1.93k
            for (std::size_t i = 0; i < width; ++i) {
66
1.56k
                out << ' ' << *find_net(instance, "I" + std::to_string(i));
67
1.56k
            }
68
372
            out << ' ' << *find_net(instance, "O") << "\n";
69
372
            const std::string& text = init->second;
70
8.51k
            for (std::size_t row = 0; row < (1U << width); ++row) {
71
8.14k
                const std::size_t digit_index = text.size() - 1 - row / 4;
72
8.14k
                if (!((hex_digit(text.at(digit_index)) >> (row % 4)) & 1)) continue;
73
18.9k
                for (std::size_t i = 0; i < width; ++i) out << ((row >> i) & 1U ? '1' : '0');
74
3.33k
                out << " 1\n";
75
3.33k
            }
76
372
        } else if (type == "TIEHI" || type == "TIELO") {
77
132
            out << ".names " << *find_net(instance, "Y") << "\n";
78
132
            if (type == "TIEHI") out << "1\n";
79
144
        } else if (type == "DFF") {
80
12
            out << ".latch " << *find_net(instance, "D") << ' ' << *find_net(instance, "Y")
81
12
                << " re " << *find_net(instance, "CLK") << " 3\n";
82
132
        } else {
83
132
            out << ".subckt " << type;
84
528
            for (const auto& connection : instance.get_connections()) {
85
528
                out << ' ' << connection.get_pin() << '=' << connection.get_net();
86
528
            }
87
132
            out << "\n";
88
132
        }
89
648
    }
90
12
    out << ".end\n";
91
12
    return out.str();
92
12
}
93
94
2
void write_blif(const Module& module, const std::string& path) {
95
2
    std::ofstream file(path);
96
2
    if (!file) throw std::runtime_error("cannot open BLIF output: " + path);
97
1
    file << to_blif(module);
98
1
}
99
100
}  // namespace labneura::eda