// Self-checking testbench for video_timing.sv. // Runs 640x480@60 and 1280x720@60 (VESA DMT values) for two frames each // and measures every interval from the outputs alone. `timescale 1ns / 1ps module timing_monitor #( parameter string NAME = "mode", parameter int H_ACTIVE = 640, H_FP = 16, H_SYNC = 96, H_BP = 48, parameter int V_ACTIVE = 480, V_FP = 10, V_SYNC = 2, V_BP = 33, parameter logic H_POL = 1'b0, V_POL = 1'b0 ) ( input logic clk, input logic hsync, input logic vsync, input logic de, input logic frame_start, output logic done, output int errors ); localparam int H_TOTAL = H_ACTIVE + H_FP + H_SYNC + H_BP; localparam int V_TOTAL = V_ACTIVE + V_FP + V_SYNC + V_BP; logic hs, vs, hs_q, vs_q, de_q; // syncs converted to active-high assign hs = (hsync == H_POL); assign vs = (vsync == V_POL); int frames = 0, cyc = 0, abs_cyc = 0, hs_len = 0, vs_len = 0, de_len = 0; int since_de_fall = 0, since_hs_fall = 0, last_hs_rise = -1; int lines = 0, de_lines = 0, hs_in_vs = 0; realtime t_frame = 0; task automatic check(input bit ok, input string what, input int got, input int exp); if (!ok) begin if (errors < 10) $display("%s: ERROR %s: got %0d, expected %0d", NAME, what, got, exp); errors++; end endtask initial begin done = 1'b0; errors = 0; end always @(posedge clk) begin hs_q <= hs; vs_q <= vs; de_q <= de; if (frame_start) begin if (frames > 0) begin check(cyc == H_TOTAL * V_TOTAL, "cycles per frame", cyc, H_TOTAL * V_TOTAL); check(lines == V_TOTAL, "hsync pulses per frame", lines, V_TOTAL); check(de_lines == V_ACTIVE, "lines with DE", de_lines, V_ACTIVE); $display("%s: frame %0d: %0d x %0d active, %0d x %0d total, frame period %.3f ms", NAME, frames, H_ACTIVE, de_lines, H_TOTAL, lines, ($realtime - t_frame) / 1e6); end check(!hs && !vs, "sync idle at the first pixel", 0, 0); frames <= frames + 1; cyc = 0; lines = 0; de_lines = 0; t_frame = $realtime; if (frames == 2) done <= 1'b1; end cyc++; abs_cyc++; // Horizontal: DE run length, front porch, sync width, back porch. if (de) de_len++; if (de_q && !de) begin check(de_len == H_ACTIVE, "DE pixels per line", de_len, H_ACTIVE); de_len = 0; de_lines++; since_de_fall = 0; end since_de_fall++; if (hs) hs_len++; if (hs && !hs_q) begin if (frames > 0 && de_lines > 0 && lines < V_ACTIVE) check(since_de_fall == H_FP + 1, "front porch", since_de_fall - 1, H_FP); if (last_hs_rise >= 0) check(abs_cyc - last_hs_rise == H_TOTAL, "line period", abs_cyc - last_hs_rise, H_TOTAL); last_hs_rise = abs_cyc; lines++; if (vs) hs_in_vs++; end if (hs_q && !hs) begin check(hs_len == H_SYNC, "hsync width", hs_len, H_SYNC); hs_len = 0; since_hs_fall = 0; end since_hs_fall++; if (de && !de_q && frames > 0) check(since_hs_fall == H_BP + 1, "back porch", since_hs_fall - 1, H_BP); // Vertical: front porch in lines and sync width. if (vs && !vs_q && frames > 0) begin check(lines == V_ACTIVE + V_FP, "lines before vsync", lines, V_ACTIVE + V_FP); hs_in_vs = 0; end if (vs) vs_len++; if (vs_q && !vs && frames > 0) begin check(vs_len == V_SYNC * H_TOTAL, "vsync width in clocks", vs_len, V_SYNC * H_TOTAL); check(hs_in_vs == V_SYNC, "hsync pulses during vsync", hs_in_vs, V_SYNC); end if (!vs) vs_len = 0; end endmodule module tb_video_timing; logic clk25 = 1'b0, clk74 = 1'b0, rst = 1'b1; always #19.861 clk25 = ~clk25; // 25.175 MHz always #6.734 clk74 = ~clk74; // 74.25 MHz logic hs_a, vs_a, de_a, fs_a, hs_b, vs_b, de_b, fs_b; logic [9:0] x_a, y_a, y_b; logic [10:0] x_b; logic done_a, done_b; int err_a, err_b; video_timing u_vga (.clk(clk25), .rst(rst), .hsync(hs_a), .vsync(vs_a), .de(de_a), .frame_start(fs_a), .x(x_a), .y(y_a)); timing_monitor #(.NAME("640x480@60")) u_mon_a ( .clk(clk25), .hsync(hs_a), .vsync(vs_a), .de(de_a), .frame_start(fs_a), .done(done_a), .errors(err_a)); video_timing #(.H_ACTIVE(1280), .H_FP(110), .H_SYNC(40), .H_BP(220), .V_ACTIVE(720), .V_FP(5), .V_SYNC(5), .V_BP(20), .H_POL(1'b1), .V_POL(1'b1)) u_720p ( .clk(clk74), .rst(rst), .hsync(hs_b), .vsync(vs_b), .de(de_b), .frame_start(fs_b), .x(x_b), .y(y_b)); timing_monitor #(.NAME("1280x720@60"), .H_ACTIVE(1280), .H_FP(110), .H_SYNC(40), .H_BP(220), .V_ACTIVE(720), .V_FP(5), .V_SYNC(5), .V_BP(20), .H_POL(1'b1), .V_POL(1'b1)) u_mon_b ( .clk(clk74), .hsync(hs_b), .vsync(vs_b), .de(de_b), .frame_start(fs_b), .done(done_b), .errors(err_b)); // x and y must count the visible pixels while DE is high. int xy_err = 0; always @(posedge clk25) if (de_a && (x_a >= 640 || y_a >= 480)) xy_err++; always @(posedge clk74) if (de_b && (x_b >= 1280 || y_b >= 720)) xy_err++; initial begin repeat (4) @(posedge clk25); rst = 1'b0; wait (done_a && done_b); if (err_a == 0 && err_b == 0 && xy_err == 0) $display("PASS"); else $display("FAIL: %0d + %0d + %0d errors", err_a, err_b, xy_err); $finish; end endmodule