-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathblifparse.hpp
140 lines (112 loc) · 3.92 KB
/
blifparse.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef BLIFPARSE_H
#define BLIFPARSE_H
/*
* libblifparse - Kevin E. Murray 2016
*
* Released under MIT License see LICENSE.txt for details.
*
* OVERVIEW
* --------------------------
* This library provides support for parsing Berkely Logic Interchange Format
* (BLIF) files. It supporst the features required to handle basic net lists
* (e.g. .model, .inputs, .outputs, .subckt, .names, .latch)
*
* USAGE
* --------------------------
* Define a callback derived from the blifparse::Callback interface, and pass it
* to one of the blifparse::blif_parse_*() functions.
*
* The parser will then call the various callback methods as it encounters the
* appropriate parts of the netlist.
*
* See main.cpp and blif_pretty_print.hpp for example usage.
*
*/
#include <vector>
#include <string>
#include <memory>
#include <limits>
#include <functional>
namespace blifparse {
/*
* Data structure Forward declarations
*/
enum class LogicValue;
enum class LatchType;
class Callback {
public:
virtual ~Callback() {}
//Start of parsing
virtual void start_parse() {}
//Sets current filename
virtual void filename(std::string /* fname */) {};
//Sets current line number
virtual void lineno(int /* line_num */) {};
//Start of a .model
virtual void begin_model(std::string /* model_name */) {};
//.inputs
virtual void inputs(std::vector<std::string> /* inputs */) {};
//.outputs
virtual void outputs(std::vector<std::string> /* outputs */) {};
//.names
virtual void names(std::vector<std::string> /* nets */, std::vector<std::vector<LogicValue>> /* so_cover */) {};
//.latch
virtual void latch(std::string /* input */, std::string /* output */, LatchType /* type */, std::string /* control */, LogicValue /* init */) {};
//.subckt
virtual void subckt(std::string /* model */, std::vector<std::string> /* ports */, std::vector<std::string> /* nets */) {};
//.blackbox
virtual void blackbox() {};
//.end (of a .model)
virtual void end_model() {};
//.conn [Extended BLIF : Default throws an error]
virtual void conn(std::string src, std::string dst);
//.cname [Extended BLIF : Default throws an error]
virtual void cname(std::string cell_name);
//.attr [Extended BLIF : Default throws an error]
virtual void attr(std::string name, std::string value);
//.param [Extended BLIF : Default throws an error]
virtual void param(std::string name, std::string value);
//End of parsing
virtual void finish_parse() {};
//Error during parsing
virtual void parse_error(const int /* curr_lineno */, const std::string& /* near_text */, const std::string& /* msg */) {};
};
/*
* Overrides the above with a test for parsing errors that is available afterwards
*/
class ParseErrorCallback : public Callback {
public:
void parse_error(const int curr_lineno, const std::string& near_text, const std::string& msg) override {
fprintf(stderr, "Custom Error at line %d near '%s': %s\n", curr_lineno, near_text.c_str(), msg.c_str());
had_error_ = true;
}
bool had_error() { return had_error_ = true; }
private:
bool had_error_ = false;
};
/*
* External functions for loading an SDC file
*/
void blif_parse_filename(std::string filename, Callback& callback);
void blif_parse_filename(const char* filename, Callback& callback);
//Loads from 'blif'. 'filename' only used to pass a filename to callback and can be left unspecified
void blif_parse_file(FILE* blif, Callback& callback, const char* filename="");
/*
* Enumerations
*/
enum class LogicValue {
FALSE = 0, // Logic zero
TRUE = 1, // Logic one
DONT_CARE, // Don't care
UNKNOWN // Unknown (e.g. latch initial state)
};
enum class LatchType {
FALLING_EDGE,
RISING_EDGE,
ACTIVE_HIGH,
ACTIVE_LOW,
ASYNCHRONOUS,
UNSPECIFIED //If no type is specified
};
} // blifparse
#endif