第一種:
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;
always@(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
);
always@(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的輸入時鐘信號,但是寫不來異步時許……
感覺第三種資源佔用較好