一、宏的三种定义方法
有时候编辑代码时候需要宏定义,那么如何进行宏定义呢?这里用以下代码介绍了三种方法
module fsm_moore(
//input signals
input clk,
input rst_n,
//output signals
output reg [3:0] dout,
output reg [2:0] q
);
parameter idle =2'b00,
s1 =2'b01,
s2 =2'b10,
s3 =2'b11;
reg [1:0] state,next;
always@(posedge clk or negedge rst_n)
begin
if(rst_n==1'b0)
state<= idle;
else
state<= next;
end
always@(*)
begin
next=idle;
dout=4'd0;
case(state)
idle:begin
dout=4'd0;
next=s1;
end
s1: begin
dout=4'd1;
next=s2;
end
s2: begin
dout=4'd2;
next=s3;
end
s3: begin
dout=4'd9;
next=idle;
end
endcase
end
always@(posedge clk or negedge rst_n)
begin
if(rst_n==1'b0)
q<=3'd0;
`ifdef INC_COUNTER
else
q<=q+1'b1;
`else
else
q<=q-1'b1;
`endif
end
endmodule
1.1 直接在代码外部定义宏
`define INC_COUNTER
module fsm_moore(
//input signals
input clk,
input rst_n,
//output signals
output reg [3:0] dout,
output reg [2:0] q
);
...
endmodule
宏定义了INC_COUNTER,计数器部分就会从减法变为加法。
1.2 在创建的.vh文件中定义宏
- 创建global_define.vh 命令:touch global_define.vh
- 打开global_define.vh文件 命令:gvim global_define.vh
- 定义`define INC_COUNTER
- 最后在源代码 fsm_moore.v中要 `include"global_define.vh"进来
- 最后编译的时候还要加+incdir+inc_dir,告诉编译软件global_define.vh的路径,假如global_define.vh在前级的rtl文件夹中,写成 +incdir. ./rtl
global_define.vh 文件
`define INC_COUNTER
fsm_moore.v 文件:
`include "global_define.vh"
module fsm_moore(
//input signals
input clk,
input rst_n,
//output signals
output reg [3:0] dout,
output reg [2:0] q
);
...
endmodule
1.3 直接在编译命令中定义宏
vcs ../rtl/fsm_moore.v ../tb/fsm_top.v +v2k -debug_all -l compile.log +define+INC_COUNTER