working generator and simple tb

This commit is contained in:
Zer0Nu11
2026-06-09 13:08:51 +03:00
parent c8e11a2a1f
commit 9c74fe91e8
2 changed files with 63 additions and 66 deletions

View File

@ -26,7 +26,7 @@ module generator
logic [15:0] cnt_pulse_num; logic [15:0] cnt_pulse_num;
logic [31:0] cnt_pulse_period; logic [31:0] cnt_pulse_period;
logic enable; logic enable, synced;
always @(posedge clk_dac) begin always @(posedge clk_dac) begin
if (rst) begin if (rst) begin
@ -39,52 +39,47 @@ module generator
dac_out <= ZERO_LEVEL; dac_out <= ZERO_LEVEL;
done <= 0; done <= 0;
enable <= 0; enable <= 0;
synced <= 0;
end end
else begin else begin
// wait start for updating registers
if (start & !enable) begin if (start & !enable) begin
enable <= 1; enable <= 1;
pulse_width_reg <= pulse_width;
sample_req <= 1; pulse_period_reg <= pulse_period;
pulse_num_reg <= pulse_num;
pulse_width_reg <= pulse_width; pulse_height_reg <= pulse_height;
pulse_period_reg <= pulse_period; end
pulse_num_reg <= pulse_num; // main work cycle
pulse_height_reg <= pulse_height;
end
if (enable) begin if (enable) begin
if (cnt_pulse_num != pulse_num_reg) begin
if (!sample_req && (cnt_period == 0)) begin // wait for synchronization with sampler
pulse_height_out_reg <= ZERO_LEVEL; if (!synced) begin
if (sample_done) begin done <= 1;
sample_req <= 1'b0; if (request) begin
end synced <= 1;
done <= 0;
if (!sample_done) begin
if (cnt_pulse_num == pulse_num_reg - 1) begin
enable <= 1'b0;
end end
else begin end
cnt_pulse_num <= cnt_pulse_num + 1; else begin
sample_req <= 1'b1; if (cnt_pulse_period != pulse_period_reg) begin
cnt_period <= 1; if (cnt_pulse_period < pulse_width_reg)
dac_out <= pulse_height_reg;
else
dac_out <= ZERO_LEVEL;
cnt_pulse_period <= cnt_pulse_period + 1;
end
else if (cnt_pulse_period == pulse_period_reg) begin
cnt_pulse_num <= cnt_pulse_num + 1;
cnt_pulse_period <= 0;
synced <= 0;
dac_out <= ZERO_LEVEL;
end end
end end
end end
else begin else if (cnt_pulse_num == pulse_num_reg) begin
cnt_pulse_num <= 0;
if (cnt_period < pulse_width_reg - 1) begin enable <= 0;
pulse_height_out_reg <= pulse_height_reg;
end else begin
pulse_height_out_reg <= ZERO_LEVEL;
end
if (cnt_period == pulse_period_reg - 1) begin
cnt_period <= 0;
end else begin
cnt_period <= cnt_period + 1;
end
if (sample_req && sample_done) begin
sample_req <= 0;
end
end end
end end
end end
@ -92,8 +87,8 @@ module generator
// Gated DAC write signal from DAC clock. Needed for posedge // Gated DAC write signal from DAC clock. Needed for posedge
OBUF OBUF_pulse_clk ( OBUF OBUF_pulse_clk (
.I(clk_in & enable), .I(clk_dac & enable),
.O(pulse) .O(dac_wrt)
); );
endmodule endmodule

View File

@ -15,26 +15,27 @@ module generator_tb;
logic [DATA_WIDTH-1:0] pulse_height; logic [DATA_WIDTH-1:0] pulse_height;
logic [15:0] pulse_num; logic [15:0] pulse_num;
logic pulse; wire pulse;
logic [DATA_WIDTH-1:0] pulse_height_out; wire [DATA_WIDTH-1:0] dac_out;
logic sample_done, sample_req; wire sample_done;
logic sample_req;
// DUT // DUT
generator #( generator #(
.DATA_WIDTH(DATA_WIDTH), .DATA_WIDTH(DATA_WIDTH),
.ZERO_LEVEL(ZERO_LEVEL) .ZERO_LEVEL(ZERO_LEVEL)
) dut ( ) dut (
.clk_in(clk), .clk_dac(clk),
.rst(rst), .rst(rst),
.start(start), .start(start),
.pulse_width(pulse_width), .pulse_width(pulse_width),
.pulse_period(pulse_period), .pulse_period(pulse_period),
.pulse_height(pulse_height), .pulse_height(pulse_height),
.pulse_num(pulse_num), .pulse_num(pulse_num),
.pulse(pulse), .dac_wrt(pulse),
.pulse_height_out(pulse_height_out), .dac_out(dac_out),
.sample_done(sample_done), .done(sample_done),
.sample_req(sample_req) .request(sample_req)
); );
// Clock // Clock
@ -53,33 +54,33 @@ module generator_tb;
pulse_period = 0; pulse_period = 0;
pulse_height = 0; pulse_height = 0;
pulse_num = 0; pulse_num = 0;
sample_done = 0; sample_req = 0;
repeat(5) @(posedge clk); repeat(5) @(posedge clk);
rst = 0; rst = 0;
// --- Test 1 ---
// 3 clk 1, 5 clk 0, 4 pulses
repeat(2) @(posedge clk); repeat(2) @(posedge clk);
pulse_width = 1; pulse_width = 0;
pulse_period = 20; pulse_period = 20;
pulse_num = 4; pulse_num = 4;
pulse_height = 14'h3FF; pulse_height = 1024;
// startup signal
#1; #1;
start = 1; start = 1;
repeat(1) @(posedge clk);
start = 0;
wait(sample_req == 1);
@(posedge clk); @(posedge clk);
#1; #1;
sample_done = 1; start = 0;
wait(sample_req == 0)
sample_done = 0;
repeat(pulse_period*pulse_num+10) @(posedge clk); repeat(pulse_num) begin
// syncronization
wait(sample_done == 1);
repeat(4) @(posedge clk);
sample_req = 1;
repeat(2) @(posedge clk);
sample_req = 0;
end
wait(sample_done == 1);
// // --- Test 2 --- // // --- Test 2 ---
// $display("\n--- SECOND RUN ---\n"); // $display("\n--- SECOND RUN ---\n");
@ -115,8 +116,9 @@ module generator_tb;
// $display("\n=== TEST FINISHED ==="); // $display("\n=== TEST FINISHED ===");
// $finish; #100;
end $finish;
end
// // Display // // Display
// always @(posedge clk) begin // always @(posedge clk) begin