// Raster video timing generator. // Each line is H_ACTIVE pixels of picture, then the front porch, the // horizontal sync pulse and the back porch. Each frame is V_ACTIVE lines, // then the vertical front porch, sync and back porch (in whole lines). // The defaults are 640x480 at 60 Hz (pixel clock 25.175 MHz), with the // VESA DMT borders folded into the porches. H_POL/V_POL give the level of // the sync pulse: 0 = negative (active low), 1 = positive (active high). // All outputs are registered and change together, one clock after the // counters; x and y are the pixel coordinates when de is high. `timescale 1ns / 1ps module video_timing #( parameter int H_ACTIVE = 640, parameter int H_FP = 16, parameter int H_SYNC = 96, parameter int H_BP = 48, parameter int V_ACTIVE = 480, parameter int V_FP = 10, parameter int V_SYNC = 2, parameter int V_BP = 33, parameter logic H_POL = 1'b0, parameter logic V_POL = 1'b0, localparam int H_TOTAL = H_ACTIVE + H_FP + H_SYNC + H_BP, localparam int V_TOTAL = V_ACTIVE + V_FP + V_SYNC + V_BP, localparam int XW = $clog2(H_TOTAL), localparam int YW = $clog2(V_TOTAL) ) ( input logic clk, // pixel clock input logic rst, output logic hsync, output logic vsync, output logic de, // data enable: high for visible pixels output logic frame_start, // one pulse with the first pixel of a frame output logic [XW-1:0] x, output logic [YW-1:0] y ); logic [XW-1:0] h_cnt; logic [YW-1:0] v_cnt; always_ff @(posedge clk) begin if (rst) begin h_cnt <= '0; v_cnt <= '0; end else if (h_cnt == XW'(H_TOTAL - 1)) begin h_cnt <= '0; v_cnt <= (v_cnt == YW'(V_TOTAL - 1)) ? '0 : v_cnt + 1'b1; end else begin h_cnt <= h_cnt + 1'b1; end end logic h_act, v_act, h_sync_on, v_sync_on; assign h_act = (h_cnt < XW'(H_ACTIVE)); assign v_act = (v_cnt < YW'(V_ACTIVE)); assign h_sync_on = (h_cnt >= XW'(H_ACTIVE + H_FP)) && (h_cnt < XW'(H_ACTIVE + H_FP + H_SYNC)); assign v_sync_on = (v_cnt >= YW'(V_ACTIVE + V_FP)) && (v_cnt < YW'(V_ACTIVE + V_FP + V_SYNC)); always_ff @(posedge clk) begin if (rst) begin hsync <= ~H_POL; vsync <= ~V_POL; de <= 1'b0; frame_start <= 1'b0; x <= '0; y <= '0; end else begin hsync <= h_sync_on ? H_POL : ~H_POL; vsync <= v_sync_on ? V_POL : ~V_POL; de <= h_act && v_act; frame_start <= (h_cnt == '0) && (v_cnt == '0); x <= h_cnt; y <= v_cnt; end end endmodule