原创 【数字IC】高级数字 IC 设计(3)VGA 彩色条显示设计(480P)

2023-3-18 21:29 659 4 4 分类: FPGA/CPLD 文集: 数字IC

本文分享一下VGA显示的代码范例,480P满足一般需求。

通过代码注释可以快速了解到寄存器的使用和水平竖直像素定位的方式和方法,适合初学者入门学习

 

  1. module VGA(
  2. input clk, // pixel clock
  3. input rst_n,
  4. output hs,
  5. output vs,
  6. output de,
  7. output [23:0] RGB
  8. );
  9. // 640x480 25.175Mhz
  10. parameter H_ACTIVE = 16'd640;
  11. parameter H_FP = 16'd16 ;
  12. parameter H_SYNC = 16'd96 ;
  13. parameter H_BP = 16'd48 ;
  14. parameter V_ACTIVE = 16'd480;
  15. parameter V_FP = 16'd10 ;
  16. parameter V_SYNC = 16'd2 ;
  17. parameter V_BP = 16'd33 ;
  18. parameter HS_POL = 1'b0 ;
  19. parameter VS_POL = 1'b0 ;
  20. parameter H_TOTAL = H_ACTIVE + H_FP + H_SYNC + H_BP; // horizontal total time (pixels)
  21. parameter V_TOTAL = V_ACTIVE + V_FP + V_SYNC + V_BP; // vertical total time (lines)
  22. reg hs_reg; // horizontal sync register
  23. reg vs_reg; // vertical sync register
  24. reg hs_reg_d0; // delay 1 clock of 'hs_reg'
  25. reg vs_reg_d0; // delay 1 clock of 'vs_reg'
  26. reg [11:0] h_cnt; // horizontal counter
  27. reg [11:0] v_cnt; // vertical counter
  28. reg [11:0] active_x; // video x position
  29. reg [11:0] active_y; // video y position
  30. reg [23:0] rgb_reg; // output color bar
  31. reg h_active; // horizontal video active
  32. reg v_active; // vertical video active
  33. wire video_active; // video active(horizontal active and vertical active)
  34. reg video_active_d0; // delay 1 clock of video_active
  35. assign hs = hs_reg_d0;
  36. assign vs = vs_reg_d0;
  37. assign video_active = h_active & v_active;
  38. assign de = video_active_d0;
  39. assign RGB = rgb_reg;
  40. always@(posedge clk or negedge rst_n) begin
  41. if(!rst_n)
  42. begin
  43. hs_reg_d0 <= 1'b0;
  44. vs_reg_d0 <= 1'b0;
  45. video_active_d0 <= 1'b0;
  46. end
  47. else
  48. begin
  49. hs_reg_d0 <= hs_reg;
  50. vs_reg_d0 <= vs_reg;
  51. video_active_d0 <= video_active;
  52. end
  53. end
  54. always@(posedge clk or negedge rst_n) begin
  55. if(!rst_n)
  56. h_cnt <= 12'd0;
  57. else if(h_cnt == H_TOTAL - 1) // horizontal counter maximum value
  58. h_cnt <= 12'd0;
  59. else
  60. h_cnt <= h_cnt + 12'd1;
  61. end
  62. always@(posedge clk or negedge rst_n) begin
  63. if(!rst_n)
  64. active_x <= 12'd0;
  65. else if(h_cnt >= H_FP + H_SYNC + H_BP - 1) // horizontal video active
  66. active_x <= h_cnt - (H_FP[11:0] + H_SYNC[11:0] + H_BP[11:0] - 12'd1);
  67. else
  68. active_x <= active_x;
  69. end
  70. always@(posedge clk or negedge rst_n) begin
  71. if(!rst_n)
  72. v_cnt <= 12'd0;
  73. else if(h_cnt == H_FP - 1) // horizontal sync time
  74. if(v_cnt == V_TOTAL - 1) // vertical counter maximum value
  75. v_cnt <= 12'd0;
  76. else
  77. v_cnt <= v_cnt + 12'd1;
  78. else
  79. v_cnt <= v_cnt;
  80. end
  81. always@(posedge clk or negedge rst_n) begin
  82. if(!rst_n)
  83. hs_reg <= 1'b0;
  84. else if(h_cnt == H_FP - 1) // horizontal sync begin
  85. hs_reg <= HS_POL;
  86. else if(h_cnt == H_FP + H_SYNC - 1) // horizontal sync end
  87. hs_reg <= ~hs_reg;
  88. else
  89. hs_reg <= hs_reg;
  90. end
  91. always@(posedge clk or negedge rst_n) begin
  92. if(!rst_n)
  93. h_active <= 1'b0;
  94. else if(h_cnt == H_FP + H_SYNC + H_BP - 1) // horizontal active begin
  95. h_active <= 1'b1;
  96. else if(h_cnt == H_TOTAL - 1) // horizontal active end
  97. h_active <= 1'b0;
  98. else
  99. h_active <= h_active;
  100. end
  101. always@(posedge clk or negedge rst_n) begin
  102. if(!rst_n)
  103. vs_reg <= 1'd0;
  104. else if((v_cnt == V_FP - 1) && (h_cnt == H_FP - 1)) // vertical sync begin
  105. vs_reg <= HS_POL;
  106. else if((v_cnt == V_FP + V_SYNC - 1) && (h_cnt == H_FP - 1)) // vertical sync end
  107. vs_reg <= ~vs_reg;
  108. else
  109. vs_reg <= vs_reg;
  110. end
  111. always@(posedge clk or negedge rst_n) begin
  112. if(!rst_n)
  113. v_active <= 1'd0;
  114. else if((v_cnt == V_FP + V_SYNC + V_BP - 1) && (h_cnt == H_FP - 1)) // vertical active begin
  115. v_active <= 1'b1;
  116. else if((v_cnt == V_TOTAL - 1) && (h_cnt == H_FP - 1)) // vertical active end
  117. v_active <= 1'b0;
  118. else
  119. v_active <= v_active;
  120. end
  121. always@(posedge clk or negedge rst_n) begin
  122. if(!rst_n)
  123. rgb_reg <= 24'h000;
  124. else if(video_active) begin
  125. if(active_x == 12'd0)
  126. rgb_reg <= 24'hF00;
  127. else if(active_x == 12'd120)
  128. rgb_reg <= 24'h0F0;
  129. else if(active_x == 12'd240)
  130. rgb_reg <= 24'h00F;
  131. else if(active_x == 12'd360)
  132. rgb_reg <= 24'h0F0;
  133. else if(active_x == 12'd410)
  134. rgb_reg <= 24'hF00;
  135. else
  136. rgb_reg <= rgb_reg;
  137. end
  138. else
  139. rgb_reg <= 24'h000;
  140. end
  141. endmodule

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
4
关闭 站长推荐上一条 /3 下一条