FSX.v 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Graphical processor (Frame Synthesizer)
  3. * Generates video from VRAM
  4. */
  5. module FSX(
  6. //Clocks
  7. input clkPixel,
  8. input clkTMDShalf,
  9. input clk14,
  10. input clk114,
  11. input clkMuxOut,
  12. //HDMI
  13. output [3:0] TMDS_p,
  14. output [3:0] TMDS_n,
  15. //NTSC composite
  16. output [7:0] composite,
  17. //Select output method
  18. input selectOutput,
  19. //VRAM32
  20. output [13:0] vram32_addr,
  21. input [31:0] vram32_q,
  22. //VRAM322
  23. output [13:0] vram322_addr,
  24. input [31:0] vram322_q,
  25. //VRAM8
  26. output [13:0] vram8_addr,
  27. input [7:0] vram8_q,
  28. //VRAMSPR
  29. output [13:0] vramSPR_addr,
  30. input [8:0] vramSPR_q,
  31. //VRAMpixel
  32. output [16:0] vramPX_addr,
  33. input [7:0] vramPX_q,
  34. //Interrupt signal
  35. output frameDrawn
  36. );
  37. wire [11:0] h_count_hdmi;
  38. wire [11:0] v_count_hdmi;
  39. wire hsync_hdmi;
  40. wire vsync_hdmi;
  41. wire csync;
  42. wire blank_hdmi;
  43. wire frameDrawn_hdmi;
  44. TimingGenerator timingGenerator(
  45. // Clock
  46. .clkPixel(clkPixel),
  47. // Position counters
  48. .h_count(h_count_hdmi),
  49. .v_count(v_count_hdmi),
  50. // Video signals
  51. .hsync(hsync_hdmi),
  52. .vsync(vsync_hdmi),
  53. .csync(csync),
  54. .blank(blank_hdmi),
  55. // Interrupt signal
  56. .frameDrawn(frameDrawn_hdmi)
  57. );
  58. wire [2:0] r_ntsc;
  59. wire [2:0] g_ntsc;
  60. wire [1:0] b_ntsc;
  61. wire frameDrawn_ntsc;
  62. wire [11:0] h_count_ntsc;
  63. wire [11:0] v_count_ntsc;
  64. wire hsync_ntsc;
  65. wire vsync_ntsc;
  66. wire blank_ntsc;
  67. RGB332toNTSC rgb2ntsc(
  68. .clk(clk14), //14.318MHz
  69. .clkColor(clk114), //114.5454MHz
  70. .r(r_ntsc),
  71. .g(g_ntsc),
  72. .b(b_ntsc),
  73. .hcount(h_count_ntsc),
  74. .vcount(v_count_ntsc),
  75. .hs(hsync_ntsc),
  76. .vs(vsync_ntsc),
  77. .blank(blank_ntsc),
  78. .composite(composite), // video output signal
  79. .frameDrawn(frameDrawn_ntsc) // interrupt signal
  80. );
  81. wire hsync;
  82. wire vsync;
  83. wire blank;
  84. wire [11:0] h_count;
  85. wire [11:0] v_count;
  86. assign frameDrawn = (selectOutput == 1'b1) ? frameDrawn_hdmi : frameDrawn_ntsc;
  87. assign hsync = (selectOutput == 1'b1) ? hsync_hdmi : hsync_ntsc;
  88. assign vsync = (selectOutput == 1'b1) ? vsync_hdmi : ~vsync_ntsc; // ntsc vsync is inverted
  89. assign blank = (selectOutput == 1'b1) ? blank_hdmi : blank_ntsc;
  90. assign h_count = (selectOutput == 1'b1) ? h_count_hdmi : h_count_ntsc;
  91. assign v_count = (selectOutput == 1'b1) ? v_count_hdmi : v_count_ntsc;
  92. wire [2:0] BGW_r;
  93. wire [2:0] BGW_g;
  94. wire [1:0] BGW_b;
  95. BGWrenderer bgwrenderer(
  96. // Video I/O
  97. .clk(clkMuxOut),
  98. .hs(hsync),
  99. .vs(vsync),
  100. .blank(blank),
  101. .scale2x(selectOutput),
  102. // Output colors
  103. .r(BGW_r),
  104. .g(BGW_g),
  105. .b(BGW_b),
  106. .h_count(h_count), // line position in pixels including blanking
  107. .v_count(v_count), // frame position in lines including blanking
  108. // VRAM32
  109. .vram32_addr(vram32_addr),
  110. .vram32_q(vram32_q),
  111. // VRAM8
  112. .vram8_addr(vram8_addr),
  113. .vram8_q(vram8_q)
  114. );
  115. wire [2:0] PX_r;
  116. wire [2:0] PX_g;
  117. wire [1:0] PX_b;
  118. PixelEngine pixelEngine(
  119. // Video I/O
  120. .clk(clkMuxOut),
  121. .hs(hsync),
  122. .vs(vsync),
  123. .blank(blank),
  124. .scale2x(selectOutput),
  125. // Output colors
  126. .r(PX_r),
  127. .g(PX_g),
  128. .b(PX_b),
  129. .h_count(h_count), // line position in pixels including blanking
  130. .v_count(v_count), // frame position in lines including blanking
  131. // VRAM
  132. .vram_addr(vramPX_addr),
  133. .vram_q(vramPX_q)
  134. );
  135. // Give priority to pixel plane if bgw plane is black
  136. wire pxPriority = (BGW_r == 3'd0 && BGW_g == 3'd0 && BGW_b == 2'd0);
  137. wire [2:0] rendered_r;
  138. wire [2:0] rendered_g;
  139. wire [1:0] rendered_b;
  140. assign rendered_r = (pxPriority) ? PX_r: BGW_r;
  141. assign rendered_g = (pxPriority) ? PX_g: BGW_g;
  142. assign rendered_b = (pxPriority) ? PX_b : BGW_b;
  143. assign r_ntsc = (!selectOutput) ? rendered_r : 3'd0;
  144. assign g_ntsc = (!selectOutput) ? rendered_g : 3'd0;
  145. assign b_ntsc = (!selectOutput) ? rendered_b : 2'd0;
  146. wire [2:0] r_hdmi;
  147. wire [2:0] g_hdmi;
  148. wire [1:0] b_hdmi;
  149. assign r_hdmi = (selectOutput) ? rendered_r : 3'd0;
  150. assign g_hdmi = (selectOutput) ? rendered_g : 3'd0;
  151. assign b_hdmi = (selectOutput) ? rendered_b : 2'd0;
  152. wire [7:0] rByte;
  153. wire [7:0] gByte;
  154. wire [7:0] bByte;
  155. assign rByte = (r_hdmi == 3'd0) ? {r_hdmi, 5'b00000} : {r_hdmi, 5'b11111};
  156. assign gByte = (g_hdmi == 3'd0) ? {g_hdmi, 5'b00000} : {g_hdmi, 5'b11111};
  157. assign bByte = (b_hdmi == 2'd0) ? {b_hdmi, 6'b000000} : {b_hdmi, 6'b111111};
  158. // Convert VGA signal to HDMI signals
  159. RGB2HDMI rgb2hdmi(
  160. .clkTMDS(clkTMDShalf),
  161. .clkRGB (clkPixel),
  162. .rRGB (rByte),
  163. .gRGB (gByte),
  164. .bRGB (bByte),
  165. .blk (blank_hdmi),
  166. .hs (hsync_hdmi),
  167. .vs (vsync_hdmi),
  168. .bTMDS (TMDS_p[0]),
  169. .gTMDS (TMDS_p[1]),
  170. .rTMDS (TMDS_p[2]),
  171. .cTMDS (TMDS_p[3]),
  172. .bTMDSn (TMDS_n[0]),
  173. .gTMDSn (TMDS_n[1]),
  174. .rTMDSn (TMDS_n[2]),
  175. .cTMDSn (TMDS_n[3])
  176. );
  177. /*
  178. // Image file generator for simulation
  179. integer file;
  180. integer framecounter = 0;
  181. always @(negedge vsync_hdmi)
  182. begin
  183. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC5/Verilog/output/frame%0d.ppm", framecounter), "w");
  184. $fwrite(file, "P3\n");
  185. $fwrite(file, "640 480\n");
  186. $fwrite(file, "255\n");
  187. framecounter = framecounter + 1;
  188. end
  189. always @(posedge clkPixel)
  190. begin
  191. if (~blank_hdmi)
  192. begin
  193. $fwrite(file, "%d %d %d\n", rByte, gByte, bByte);
  194. end
  195. end
  196. */
  197. endmodule