generator quick fix

This commit is contained in:
2026-05-29 18:01:31 +03:00
parent 906d5090cd
commit 0a68a753be
2 changed files with 58 additions and 44 deletions

View File

@ -43,7 +43,8 @@ module generator
cnt_pulse_num <= '0; cnt_pulse_num <= '0;
cnt_period <= '0; cnt_period <= '0;
sample_req <= 0; sample_req <= 0;
end else begin end
else begin
if (start & !enable) begin if (start & !enable) begin
enable <= 1'b1; enable <= 1'b1;
cnt_pulse_num <= '0; cnt_pulse_num <= '0;
@ -77,12 +78,12 @@ module generator
end end
else begin else begin
if (cnt_period <= pulse_width_reg) begin if (cnt_period < pulse_width_reg - 1) begin
pulse_height_out_reg <= pulse_height_reg; pulse_height_out_reg <= pulse_height_reg;
end else begin end else begin
pulse_height_out_reg <= ZERO_LEVEL; pulse_height_out_reg <= ZERO_LEVEL;
end end
if (cnt_period == pulse_period_reg) begin if (cnt_period == pulse_period_reg - 1) begin
cnt_period <= 0; cnt_period <= 0;
end else begin end else begin
cnt_period <= cnt_period + 1; cnt_period <= cnt_period + 1;

View File

@ -3,8 +3,8 @@
module generator_tb; module generator_tb;
parameter DATA_WIDTH = 14; parameter DATA_WIDTH = 14;
parameter ZERO_LEVEL = 8192; parameter ZERO_LEVEL = 0;
parameter CLK_PERIOD = 16; parameter CLK_PERIOD = 8;
logic clk; logic clk;
logic rst; logic rst;
@ -17,10 +17,12 @@ module generator_tb;
logic pulse; logic pulse;
logic [DATA_WIDTH-1:0] pulse_height_out; logic [DATA_WIDTH-1:0] pulse_height_out;
logic sample_done, sample_req;
// DUT // DUT
generator #( generator #(
.DATA_WIDTH(DATA_WIDTH) .DATA_WIDTH(DATA_WIDTH),
.ZERO_LEVEL(ZERO_LEVEL)
) dut ( ) dut (
.clk_in(clk), .clk_in(clk),
.rst(rst), .rst(rst),
@ -30,7 +32,9 @@ module generator_tb;
.pulse_height(pulse_height), .pulse_height(pulse_height),
.pulse_num(pulse_num), .pulse_num(pulse_num),
.pulse(pulse), .pulse(pulse),
.pulse_height_out(pulse_height_out) .pulse_height_out(pulse_height_out),
.sample_done(sample_done),
.sample_req(sample_req)
); );
// Clock // Clock
@ -49,6 +53,7 @@ module generator_tb;
pulse_period = 0; pulse_period = 0;
pulse_height = 0; pulse_height = 0;
pulse_num = 0; pulse_num = 0;
sample_done = 0;
repeat(5) @(posedge clk); repeat(5) @(posedge clk);
rst = 0; rst = 0;
@ -56,59 +61,67 @@ module generator_tb;
// --- Test 1 --- // --- Test 1 ---
// 3 clk 1, 5 clk 0, 4 pulses // 3 clk 1, 5 clk 0, 4 pulses
repeat(2) @(posedge clk); repeat(2) @(posedge clk);
pulse_width = 3; pulse_width = 1;
pulse_period = 8; pulse_period = 20;
pulse_num = 4; pulse_num = 4;
pulse_height = 14'h3FF; pulse_height = 14'h3FF;
#1;
start = 1; start = 1;
repeat(1) @(posedge clk); repeat(1) @(posedge clk);
start = 0; start = 0;
repeat(50) @(posedge clk); wait(sample_req == 1);
// --- Test 2 ---
$display("\n--- SECOND RUN ---\n");
@(posedge clk); @(posedge clk);
pulse_width = 2; #1;
pulse_period = 5; sample_done = 1;
pulse_num = 3; wait(sample_req == 0)
pulse_height = 14'h155; sample_done = 0;
start = 1;
@(posedge clk); repeat(pulse_period*pulse_num+10) @(posedge clk);
start = 0;
repeat(40) @(posedge clk); // // --- Test 2 ---
// $display("\n--- SECOND RUN ---\n");
pulse_width = 3; // @(posedge clk);
pulse_period = 8; // pulse_width = 2;
pulse_num = 4; // pulse_period = 5;
pulse_height = 14'h3FF; // pulse_num = 3;
start = 1; // pulse_height = 14'h155;
// start = 1;
repeat(1) @(posedge clk); // @(posedge clk);
start = 0; // start = 0;
repeat(5) @(posedge clk); // repeat(40) @(posedge clk);
start = 1;
pulse_height = 14'h155;
repeat(1) @(posedge clk); // pulse_width = 3;
start = 0; // pulse_period = 8;
// pulse_num = 4;
// pulse_height = 14'h3FF;
// start = 1;
repeat(50) @(posedge clk); // repeat(1) @(posedge clk);
// start = 0;
// repeat(5) @(posedge clk);
// start = 1;
// pulse_height = 14'h155;
// repeat(1) @(posedge clk);
// start = 0;
// repeat(50) @(posedge clk);
$display("\n=== TEST FINISHED ==="); // $display("\n=== TEST FINISHED ===");
$finish; // $finish;
end end
// Display // // Display
always @(posedge clk) begin // always @(posedge clk) begin
$display("t=%0t | pulse=%0b | height=%h", // $display("t=%0t | pulse=%0b | height=%h",
$time, pulse, pulse_height_out); // $time, pulse, pulse_height_out);
end // end
endmodule endmodule