vid_tgen.v 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * vid_tgen.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Video Timing Generator
  7. *
  8. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  9. * All rights reserved.
  10. *
  11. * BSD 3-clause, see LICENSE.bsd
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * * Neither the name of the <organization> nor the
  21. * names of its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  28. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  31. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. `default_nettype none
  36. `ifndef SIM
  37. `define FORCE_REG // Yosys fuckery workaround :/
  38. `endif
  39. module vid_tgen #(
  40. parameter integer H_WIDTH = 12,
  41. parameter integer H_FP = 88 / 2,
  42. parameter integer H_SYNC = 44 / 2,
  43. parameter integer H_BP = 148 / 2,
  44. parameter integer H_ACTIVE = 1920 / 2,
  45. parameter integer V_WIDTH = 12,
  46. parameter integer V_FP = 4,
  47. parameter integer V_SYNC = 5,
  48. parameter integer V_BP = 36,
  49. parameter integer V_ACTIVE = 1080
  50. )(
  51. output reg vid_hsync,
  52. output reg vid_vsync,
  53. output reg vid_active,
  54. output reg vid_h_first,
  55. output reg vid_h_last,
  56. output reg vid_v_first,
  57. output reg vid_v_last,
  58. input wire clk,
  59. input wire rst
  60. );
  61. localparam Z_FP = 0;
  62. localparam Z_SYNC = 1;
  63. localparam Z_BP = 2;
  64. localparam Z_ACTIVE = 3;
  65. // Signals
  66. reg [1:0] h_zone;
  67. wire [H_WIDTH:0] h_dec;
  68. reg [H_WIDTH:0] h_mux;
  69. reg [H_WIDTH:0] h_cnt;
  70. reg h_first;
  71. wire h_last;
  72. reg [1:0] v_zone;
  73. wire [V_WIDTH:0] v_dec;
  74. reg [V_WIDTH:0] v_mux;
  75. `ifdef FORCE_REG
  76. wire [V_WIDTH:0] v_cnt;
  77. `else
  78. reg [V_WIDTH:0] v_cnt;
  79. `endif
  80. reg v_first;
  81. wire v_last;
  82. wire v_ce;
  83. reg v_ce_r;
  84. // Horizontal Counter
  85. assign h_dec = h_cnt - 1;
  86. assign h_last = h_cnt[H_WIDTH];
  87. always @(posedge clk or posedge rst)
  88. if (rst)
  89. h_first <= 1'b1;
  90. else
  91. h_first <= h_last;
  92. always @(posedge clk or posedge rst)
  93. if (rst)
  94. h_zone <= 2'b00;
  95. else
  96. h_zone <= h_zone + h_last;
  97. always @(*)
  98. begin
  99. h_mux = h_dec;
  100. if (h_last)
  101. case (h_zone)
  102. Z_FP: h_mux = H_SYNC - 2;
  103. Z_SYNC: h_mux = H_BP - 2;
  104. Z_BP: h_mux = H_ACTIVE - 2;
  105. Z_ACTIVE: h_mux = H_FP - 2;
  106. endcase
  107. end
  108. always @(posedge clk or posedge rst)
  109. if (rst)
  110. h_cnt <= 0;
  111. else
  112. h_cnt <= h_mux;
  113. // Vertical Counter
  114. assign v_dec = v_cnt - 1;
  115. assign v_last = v_cnt[V_WIDTH];
  116. assign v_ce = h_last & (h_zone == Z_ACTIVE);
  117. always @(posedge clk)
  118. v_ce_r <= v_ce;
  119. always @(posedge clk or posedge rst)
  120. if (rst)
  121. v_first <= 1'b1;
  122. else if (v_ce)
  123. v_first <= v_last;
  124. always @(posedge clk or posedge rst)
  125. if (rst)
  126. v_zone <= 2'b00;
  127. else if (v_ce)
  128. v_zone <= v_zone + v_last;
  129. always @(*)
  130. begin
  131. v_mux = v_dec;
  132. if (v_last)
  133. case (v_zone)
  134. Z_FP: v_mux = V_SYNC - 2;
  135. Z_SYNC: v_mux = V_BP - 2;
  136. Z_BP: v_mux = V_ACTIVE - 2;
  137. Z_ACTIVE: v_mux = V_FP - 2;
  138. endcase
  139. end
  140. `ifdef FORCE_REG
  141. dffer_n #(
  142. .WIDTH(V_WIDTH+1)
  143. ) v_cnt_I (
  144. .d(v_mux),
  145. .q(v_cnt),
  146. .ce(v_ce),
  147. .clk(clk),
  148. .rst(rst)
  149. );
  150. `else
  151. always @(posedge clk or posedge rst)
  152. if (rst)
  153. v_cnt <= 0;
  154. else if (v_ce)
  155. v_cnt <= v_mux;
  156. `endif
  157. // Active / Sync generation
  158. always @(posedge clk or posedge rst)
  159. if (rst) begin
  160. vid_hsync <= 1'b0;
  161. vid_vsync <= 1'b0;
  162. vid_active <= 1'b0;
  163. vid_h_first <= 1'b0;
  164. vid_h_last <= 1'b0;
  165. vid_v_first <= 1'b0;
  166. vid_v_last <= 1'b0;
  167. end else begin
  168. vid_hsync <= (h_zone == Z_SYNC);
  169. vid_vsync <= (v_zone == Z_SYNC);
  170. vid_active <= (h_zone == Z_ACTIVE) & (v_zone == Z_ACTIVE);
  171. vid_h_first <= (h_zone == Z_ACTIVE) & h_first;
  172. vid_h_last <= (h_zone == Z_ACTIVE) & h_last;
  173. vid_v_first <= (v_zone == Z_ACTIVE) & v_first;
  174. vid_v_last <= (v_zone == Z_ACTIVE) & v_last;
  175. end
  176. endmodule // vid_tgen