三种不同代码实现2位计数器的RTL比较
第一种:
module fenpinqi_reg(out,reset,clk);
output[1:0] out;
input reset,clk;
reg[1:0] out;
always @(posedge clk)
begin
if (reset)
out<=0; //同步复位
else
out<=out+1'b1; //计数
end
endmodule
波形为:
RTL为:
第二种:
//若要实现N分频,则state应设置为N/2-1
module fenpinqi(clk, clr, q0, state);
input clk, clr;
output reg q0;
output reg [1:0] state;
[email protected](posedge clk)
begin
if(clr)
begin
q0 <= 0;
state <=2'b00;
end
if(!clr)
begin
if(state == 2'b01)
begin
q0 <= ~q0;
state <= 2'b00;
end
else
begin
state <= state +2'b01;
q0 <= q0;
end
end
end
endmodule
波形为:
RTL:
第三种:
module jishuqi_d(
input clk,
input rst,
output reg q1,
output reg q2
);
[email protected](posedge clk)
begin
if(rst)
begin
q1 <= 0;
q2 <= 0;
end
else if(rst == 0)
begin
q1 <= ~q1;
end
if(q1)
begin
q2 <= ~q2;
end
else if(q1 == 0)
begin
q2 <= q2;
end
end
endmodule
波形为:
RTL:
第一种要用到一个加法器,一个多路选择器,一个寄存器
第二种是我自己想的一种写法,没想到资源占用严重,是不可取的
第三者也是我自己想的一种,本来想利用Q1的输出作为Q2的输入时钟信号,但是写不来异步时许……
感觉第三种资源占用较好