代码规范有利于在项目和工程中的维护,养成习惯对后期的工作学习会有很大的帮助!
下面就看下各个情况的规范书写格式是什么
1.时序逻辑的规范写法:
always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_init<=1'b0;endelsebegintmp_init<=time_ram_init; endend
2.组合逻辑的规范写法:
always (*)beginif(delay<DELAY_TIME)beginmath=sec_low; endelseif(delay<DELAY_TIME*2)beginmath=sec_high; endelseif(delay<DELAY_TIME*3)beginmath=min_low; endelsebeginmath=min_high; endend
3.always的规范写法:(一个always只穷举一个变量的各种情况)
always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_a1<=0; endelseif(vld_in)begintmp_a1<=a; endelsebegintmp_a1<=tmp_a1; endendalways (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_b1<=0; endelseif(vld_in)begintmp_b1<=b; endelsebegintmp_b1<=tmp_b1; endend
4.阻塞赋值与非阻塞赋值
//时序逻辑用非阻塞always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_init<=1'b0;endelsebegintmp_init<=time_ram_init; endend//组合逻辑阻塞always (*)beginif(delay<DELAY_TIME)beginmath=sec_low; endelseif(delay<DELAY_TIME*2)beginmath=sec_high; endelseif(delay<DELAY_TIME*3)beginmath=min_low; endelsebeginmath=min_high; endend
5.对齐实例:begin 对齐 信号对齐
/*对齐示例*///信号对齐:inputclk ; inputrst_n ; inputtime_ram_init ; inputtime_ram_wr ; input [ONEDATA_W-1:0] time_ram_data ; input [ADRESS_W-1:0] time_rd_times ; inputtime_en ; outputtime_ram_ovful ; outputtime_vld_out ; output [NUM_W-1 :0] time_num_out ; regtime_ram_ovful ; regtime_vld_out ; reg [NUM_W-1:0] time_num_out ; reg [ADRESS_W-1:0] wr_adr ; reg [ADRESS_W-1:0] rd_adr ; regtmp_init1 ; regtmp_init2 ; regflag_init_wadr ; regtmp_wr1 ; regtmp_wr2 ; regwr_en ; reg [CNT_W-1:0] count ; reg [CNTOF_W-1:0] cnt_ovful ; reg [CNTVLD_W-1:0] cnt_vld ; reg [ONEDATA_W-1:0] data_ram [DATARAM_W-1:0] ; reg [CNTVLD_W-1:0] vld_cnt_tmp ; //begin end对齐:always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_init1<=1'b0;endelsebegintmp_init1<=time_ram_init; endend
6.信号的书写规范
/*参数、宏定义字母必须用大写*///参数的定义:parameterADRESS_W=11; parameterONEDATA_W=32; parameterNUM_W=8; parameterCNTOF_W=4; parameterCNTVLD_W=4; parameterCNT_W=20; parameterDATARAM_W=2048; //宏定义`defineOC8051_ALU_NOP4'b0000`defineOC8051_ALU_ADD4'b0001`defineOC8051_ALU_SUB4'b0010`defineOC8051_ALU_MUL4'b0011`defineOC8051_ALU_DIV4'b0100`defineOC8051_ALU_DA4'b0101`defineOC8051_ALU_NOT4'b0110
7.时钟复位信号使用示例
//时钟只能用在敏感列表,一个模块只能用一个时钟always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_init1<=1'b0;endelsebegintmp_init1<=time_ram_init; endend
//禁止信号做时钟,而要用改成时钟使能的方式;//正确的做法:always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begincounter<=1'b0;endelseif(counter==7)begincounter<=0; endelsebegincounter<=counter+1; endendalways (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)beginen<=0; endelseif(counter==7)beginen<=1; endelsebeginen<=0; endendalways (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_init1<=1'b0;endelseif(en==1)begintmp_init1<=time_ram_init; endelsebegintmp_init1<=tmp_init1; endend
8.reg和wire的使用
/*reg类型的使用*/regtmp_init1 ; regtmp_init2 ; /*时序逻辑*/always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)begintmp_init1<=1'b0;endelsebegintmp_init1<=time_ram_init; endend/*组合逻辑*/always (*)begintmp_init2=tmp_init1; end/*wire类型的使用*/wiresys_rst_n; wireclk_25m; system_ctrlu_system_ctrl( .clk (clk_50m),//input .sys_rst_n (sys_rst_n),//output .clk_c0 (clk_25m) //output);
9.输入输出的规范
/*不能用组合输出*///正确的做法modulekey(clk,rst_n,key,key_out); inputclk ; inputrst_n ; inputkey ; outputkey_out; regkey_out; ************always (posedgeclkornegedgerst_n)beginif(rst_n==1'b0)beginkey_out<=1'b0;endelseif(count==COUNT_TIME-1)beginkey_out<=1'b1;endelsebeginkey_out<=1'b0;endendendmodule//错误的做法modulekey(clk,rst_n,key,key_out); inputclk ; inputrst_n ; inputkey ; outputkey_out; regkey_out; ************always (*)beginkey_out=sw_5&(~sw_4); endendmodule