1use std::{path::Path, str::FromStr};
2use crate::parse::{self, ParseError};
3
4#[derive(Debug)]
5pub struct Verilog {
6 pub modules: Vec<Module>,
7}
8
9impl Verilog {
10 pub fn load_file<P: AsRef<Path>>(path: P) -> Result<Self, ParseError> {
11 parse::load_file(path)
12 }
13}
14
15impl FromStr for Verilog {
16 type Err = ParseError;
17 fn from_str(s: &str) -> Result<Self, Self::Err> {
18 parse::load_str(s)
19 }
20}
21
22#[derive(Debug)]
24pub struct Module {
25 pub name: String,
26 pub port_names: Vec<String>,
27 pub ports: Vec<Port>,
28 pub nets: Vec<Net>,
29 pub parameters: Vec<Parameter>,
30 pub assign: Vec<Assign>,
31 pub always: Vec<Always>,
32 pub instances: Vec<Instance>,
33}
34
35#[derive(Debug)]
37pub struct Port {
38 pub name: String,
39 pub direction: PortDirection,
40 pub width: Option<Range>, }
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum PortDirection {
45 Input,
46 Output,
47 Inout,
48}
49
50#[derive(Debug, PartialEq, Eq)]
52pub struct Range {
53 pub msb: i32,
54 pub lsb: i32,
55}
56
57#[derive(Debug)]
59pub struct Net {
60 pub kind: NetKind,
61 pub name: String,
62 pub width: Option<Range>,
63}
64
65#[derive(Debug, PartialEq, Eq, Clone, Copy)]
66pub enum NetKind {
67 Wire,
68 Reg,
69}
70
71#[derive(Debug)]
73pub struct Parameter {
74 pub name: String,
75 pub value: ConstExpr,
76}
77
78#[derive(Debug)]
80pub struct Assign {
81 pub lhs: VarExpr,
82 pub rhs: Expr,
83}
84
85#[derive(Debug)]
87pub struct Always {
88 pub sensitivity: SensitivityList,
89 pub stmts: Vec<Stmt>,
90}
91
92#[derive(Debug)]
94pub enum SensitivityList {
95 Star, List(Vec<SensitivityEvent>), }
98
99#[derive(Debug)]
100pub struct SensitivityEvent {
101 pub edge: Option<EdgeKind>,
102 pub signal: String,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum EdgeKind {
107 Posedge,
108 Negedge,
109}
110
111#[derive(Debug)]
113pub struct Instance {
114 pub module_name: String,
115 pub instance_name: String,
116 pub connections: Vec<Connection>,
117}
118
119#[derive(Debug)]
120pub struct Connection {
121 pub port: String,
122 pub expr: Expr,
123}
124
125#[derive(Debug)]
126pub enum VarExpr {
127 Ident(String),
128 RangeSelect { base: String, range: Range },
129}
130
131#[derive(Debug, PartialEq, Eq)]
132pub enum Expr {
133 Ident(String),
134 Number(i32),
135 Value(Value),
136 RangeSelect { base: String, range: Range },
137 BinOp { op: BinOpKind, lhs: Box<Expr>, rhs: Box<Expr> },
138 UnaryOp { op: UnaryOpKind, expr: Box<Expr> },
139}
140
141#[derive(Debug)]
142pub enum ConstExpr {
143 Number(i32),
144 Value(Value),
145}
146
147#[derive(Debug, PartialEq, Eq)]
148pub enum BinOpKind {
149 Add,
150 Sub,
151 Mul,
152 And,
153 Or,
154 Eq,
155 Neq,
156 Lt,
157 Gt,
158}
159
160#[derive(Debug, PartialEq, Eq)]
161pub enum UnaryOpKind {
162 Not,
163 Neg,
164}
165
166#[derive(Debug)]
168pub enum Stmt {
169 BlockingAssign { lhs: VarExpr, rhs: Expr },
170 NonBlockingAssign { lhs: VarExpr, rhs: Expr },
171 If { cond: Expr, then_body: Vec<Stmt>, else_body: Option<Vec<Stmt>> },
172 Case { expr: Expr, items: Vec<(Expr, Vec<Stmt>)>, default: Option<Vec<Stmt>> },
173}
174
175#[derive(Debug, PartialEq, Eq)]
176pub enum ValueBase {
177 Bin,
178 Oct,
179 Dec,
180 Hex,
181}
182
183pub enum ValueBit {
184 X,
185 Z,
186 One,
187 Zero,
188}
189
190#[derive(Debug, PartialEq, Eq)]
191pub struct Value {
192 pub len: usize,
193 pub base: ValueBase,
194 pub bits: String,
195}