FPGA 將1bit的flag量轉化為狀態保持的開關量(verilog)
阿新 • • 發佈:2020-12-23
技術標籤:FPGA從硬體描述到刪核跑路fpgaVerilogverilog
計數器A,受兩個1bit的flag控制,分別為:start控制計數開始,stop控制暫停計數。
解決方法:利用兩個flag生成一個具有開關性質的量,當開關開的時候開始計數,開關關的時候暫停計數。
主模組:
module flag2switch(
input clk,
input rst,
input start_flag,
input stop_flag
);
reg switch;
[email protected](*) begin
if (!rst)
switch <= 1'b0;
else if(start_flag)
switch <= 1'b1;
else if (stop_flag)
switch <= 1'b0;
else
switch <= switch;
end
reg [12:0] cnt;
[email protected](posedge clk or negedge rst ) begin
if(!rst)
cnt <= 13'd0;
else if (switch )
cnt <= cnt +1'b1;
else
cnt <= cnt;
end
endmodule
testbench:
`timescale 1ns/1ns
`define clk_period 20
module tb;
reg Clk;/*系統時鐘*/
reg Rst_n;/*系統復位*/
reg start;
reg stop;
flag2switch f1(
.clk(Clk),
.rst(Rst_n),
.start_flag(start),
.stop_flag(stop)
);
initial Clk = 0;
always #10 Clk = ~Clk;
initial begin
Rst_n = 1'b0;
start = 1'b0;
stop = 1'b0;
#(`clk_period*20)
Rst_n = 1'b1;
#(`clk_period)
start = 1'b1;
#(`clk_period)
start = 1'b0;
#(`clk_period*8)
stop = 1'b1;
#(`clk_period)
stop = 1'b0;
#(`clk_period*8)
start = 1'b1;
#(`clk_period)
start = 1'b0;
#(`clk_period*10)
$stop;
end
endmodule