vid_tgen.v 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. `define FORCE_REG // Yosys fuckery workaround :/
  37. module vid_tgen #(
  38. parameter integer H_WIDTH = 12,
  39. parameter integer H_FP = 88 / 2,
  40. parameter integer H_SYNC = 44 / 2,
  41. parameter integer H_BP = 148 / 2,
  42. parameter integer H_ACTIVE = 1920 / 2,
  43. parameter integer V_WIDTH = 12,
  44. parameter integer V_FP = 4,
  45. parameter integer V_SYNC = 5,
  46. parameter integer V_BP = 36,
  47. parameter integer V_ACTIVE = 1080
  48. )(
  49. output reg vid_hsync,
  50. output reg vid_vsync,
  51. output reg vid_active,
  52. output reg vid_h_first,
  53. output reg vid_h_last,
  54. output reg vid_v_first,
  55. output reg vid_v_last,
  56. input wire clk,
  57. input wire rst
  58. );
  59. localparam Z_FP = 0;
  60. localparam Z_SYNC = 1;
  61. localparam Z_BP = 2;
  62. localparam Z_ACTIVE = 3;
  63. // Signals
  64. reg [1:0] h_zone;
  65. wire [H_WIDTH:0] h_dec;
  66. reg [H_WIDTH:0] h_mux;
  67. reg [H_WIDTH:0] h_cnt;
  68. reg h_first;
  69. wire h_last;
  70. reg [1:0] v_zone;
  71. wire [V_WIDTH:0] v_dec;
  72. reg [V_WIDTH:0] v_mux;
  73. `ifdef FORCE_REG
  74. wire [V_WIDTH:0] v_cnt;
  75. `else
  76. reg [V_WIDTH:0] v_cnt;
  77. `endif
  78. reg v_first;
  79. wire v_last;
  80. wire v_ce;
  81. reg v_ce_r;
  82. // Horizontal Counter
  83. assign h_dec = h_cnt - 1;
  84. assign h_last = h_cnt[H_WIDTH];
  85. always @(posedge clk or posedge rst)
  86. if (rst)
  87. h_first <= 1'b1;
  88. else
  89. h_first <= h_last;
  90. always @(posedge clk or posedge rst)
  91. if (rst)
  92. h_zone <= 2'b00;
  93. else
  94. h_zone <= h_zone + h_last;
  95. always @(*)
  96. begin
  97. h_mux = h_dec;
  98. if (h_last)
  99. case (h_zone)
  100. Z_FP: h_mux = H_SYNC - 2;
  101. Z_SYNC: h_mux = H_BP - 2;
  102. Z_BP: h_mux = H_ACTIVE - 2;
  103. Z_ACTIVE: h_mux = H_FP - 2;
  104. endcase
  105. end
  106. always @(posedge clk or posedge rst)
  107. if (rst)
  108. h_cnt <= 0;
  109. else
  110. h_cnt <= h_mux;
  111. // Vertical Counter
  112. assign v_dec = v_cnt - 1;
  113. assign v_last = v_cnt[V_WIDTH];
  114. assign v_ce = h_last & (h_zone == Z_ACTIVE);
  115. always @(posedge clk)
  116. v_ce_r <= v_ce;
  117. always @(posedge clk or posedge rst)
  118. if (rst)
  119. v_first <= 1'b1;
  120. else if (v_ce)
  121. v_first <= v_last;
  122. always @(posedge clk or posedge rst)
  123. if (rst)
  124. v_zone <= 2'b00;
  125. else if (v_ce)
  126. v_zone <= v_zone + v_last;
  127. always @(*)
  128. begin
  129. v_mux = v_dec;
  130. if (v_last)
  131. case (v_zone)
  132. Z_FP: v_mux = V_SYNC - 2;
  133. Z_SYNC: v_mux = V_BP - 2;
  134. Z_BP: v_mux = V_ACTIVE - 2;
  135. Z_ACTIVE: v_mux = V_FP - 2;
  136. endcase
  137. end
  138. `ifdef FORCE_REG
  139. dffer_n #(
  140. .WIDTH(V_WIDTH+1)
  141. ) v_cnt_I (
  142. .d(v_mux),
  143. .q(v_cnt),
  144. .ce(v_ce),
  145. .clk(clk),
  146. .rst(rst)
  147. );
  148. `else
  149. always @(posedge clk or posedge rst)
  150. if (rst)
  151. v_cnt <= 0;
  152. else if (v_ce)
  153. v_cnt <= v_mux;
  154. `endif
  155. // Active / Sync generation
  156. always @(posedge clk or posedge rst)
  157. if (rst) begin
  158. vid_hsync <= 1'b0;
  159. vid_vsync <= 1'b0;
  160. vid_active <= 1'b0;
  161. vid_h_first <= 1'b0;
  162. vid_h_last <= 1'b0;
  163. vid_v_first <= 1'b0;
  164. vid_v_last <= 1'b0;
  165. end else begin
  166. vid_hsync <= (h_zone == Z_SYNC);
  167. vid_vsync <= (v_zone == Z_SYNC);
  168. vid_active <= (h_zone == Z_ACTIVE) & (v_zone == Z_ACTIVE);
  169. vid_h_first <= (h_zone == Z_ACTIVE) & h_first;
  170. vid_h_last <= (h_zone == Z_ACTIVE) & h_last;
  171. vid_v_first <= (v_zone == Z_ACTIVE) & v_first;
  172. vid_v_last <= (v_zone == Z_ACTIVE) & v_last;
  173. end
  174. endmodule // vid_tgen