Yosys 输入: RTL code ,输出:Netlist
module counter (clk, rst, en, count); // counter.v 作为例子
input clk, rst, en;
output reg [1:0] count;
always @(posedge clk)
if (rst)
count <= 2'd0;
else if (en)
count <= count + 2'd1;
endmodule
yosys> show
需要先安装xdot sudo install xdot
yosys> dump
TextRtlil Document 描述了语法,主要是对Verilog代码进行词法分析,我还没细看,应该是使用lex&yacc自动生成
yosys> read_verilog counter.v
read_verilog 使用Parser对RTL进行解析,生成AST(Abstract Syntax Tree 抽象语法树?但这明显是个图,不懂),并产生RTLIL

yosys> show

yosys> dump
dump 输出的内容很多,这里只截取了后半部分:

yosys> hierarchy -check -top counter
proc命令处理processverilog中的process(过程结构) 包括
initial和always
- 电路在上电后是浮动电平,无法实现
initial,所以initial不可综合,只能在仿真时使用- 实际上板中,使用 reset 来处理浮动电平
yosys> proc
这是一个指令集合,等价于如下一系列指令:
yosys> proc_clean
yosys> proc_rmdead
yosys> proc_init
yosys> proc_arst
yosys> proc_mux
yosys> proc_dlatch
yosys> proc_dff
| Command | Function | for counter.v |
|---|---|---|
proc_clean | remove empty parts of processes | 没有空的process,所以没效果 |
proc_rmdead | eliminate dead trees in decision trees / if-else switch会构成决策树,清除永远不会到达或者空的分支 | 没有永远不会到达的分支,所以没效果 |
proc_init | convert initial block to init attributes | 没效果,不懂 |
proc_arst | detect asynchronous resets / 检测异步复位 | 没有异步复位,所以没效果 |
proc_mux | convert decision trees to multiplexers / 用选择器来实现决策树 | 效果如下图所示 |
proc_dlatch | extract latches from processes and converts them to d-type latches / 将锁存器转换成D锁存器 | 没有锁存器?所以没效果,一般都用触发器 |
proc_dff | extract flip-flops from processes and converts them to d-type flip-flop cells / 将always block中的D触发器提取出来 | 效果如下图所示 |


if 和 else if 是有先后关系的,这里使用两个mux串行来实现

提示信息说的很清楚,这个命令做了哪些事;如果没起效果,就只输出一行
Executing ...
fsm命令处理有限自动状态机由于HDL的并行性,如果我们想分周期完成一个任务,可以设置多个使能信号来链接不同的模块(一个模块执行结束,输出下一个模块的使能信号),但比较麻烦。
如果使用FSM,一个模块就可以完成。
module test(input clk, rst, ctrl, output [3:0] Out);
reg [1:0] curState; // current state
reg [1:0] nextState; // next state
always @(posedge clk) begin // 时序逻辑,一般使用非阻塞赋值
if(rst) curState <= 0;
else curState <= nextState;
end
always @(curState or ctrl) begin // 组合逻辑,一般使用阻塞赋值
case (curState)
0: begin
Out = 1;
nextState = ctrl ? 1 : 2;
end
1: begin
Out = 2;
if (ctrl) begin
Out = 3;
nextState = 2;
end
end
2: begin
Out = 4;
if (ctrl) begin
Out = 5;
nextState = 3;
end
end
3: begin
if (!ctrl)
nextState = 2'b00;
end
default: ;
endcase
end
endmodule
extract and optimize finite state machines
opt命令进行优化