FSX.v 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // LVDS Converter
  38. wire [3:0] TMDS;
  39. lvds lvdsConverter(
  40. .datain (TMDS),
  41. .dataout (TMDS_n),
  42. .dataout_b (TMDS_p) // Reversed because of a LVDS polarity swap on the V3 PCB
  43. );
  44. wire [11:0] h_count_hdmi;
  45. wire [11:0] v_count_hdmi;
  46. wire hsync_hdmi;
  47. wire vsync_hdmi;
  48. wire csync;
  49. wire blank_hdmi;
  50. wire frameDrawn_hdmi;
  51. TimingGenerator timingGenerator(
  52. // Clock
  53. .clkPixel(clkPixel),
  54. // Position counters
  55. .h_count(h_count_hdmi),
  56. .v_count(v_count_hdmi),
  57. // Video signals
  58. .hsync(hsync_hdmi),
  59. .vsync(vsync_hdmi),
  60. .csync(csync),
  61. .blank(blank_hdmi),
  62. // Interrupt signal
  63. .frameDrawn(frameDrawn_hdmi)
  64. );
  65. wire [2:0] r_ntsc;
  66. wire [2:0] g_ntsc;
  67. wire [1:0] b_ntsc;
  68. wire frameDrawn_ntsc;
  69. wire [11:0] h_count_ntsc;
  70. wire [11:0] v_count_ntsc;
  71. wire hsync_ntsc;
  72. wire vsync_ntsc;
  73. wire blank_ntsc;
  74. RGB332toNTSC rgb2ntsc(
  75. .clk(clk14), //14.318MHz
  76. .clkColor(clk114), //114.5454MHz
  77. .r(r_ntsc),
  78. .g(g_ntsc),
  79. .b(b_ntsc),
  80. .hcount(h_count_ntsc),
  81. .vcount(v_count_ntsc),
  82. .hs(hsync_ntsc),
  83. .vs(vsync_ntsc),
  84. .blank(blank_ntsc),
  85. .composite(composite), // video output signal
  86. .frameDrawn(frameDrawn_ntsc) // interrupt signal
  87. );
  88. wire hsync;
  89. wire vsync;
  90. wire blank;
  91. wire [11:0] h_count;
  92. wire [11:0] v_count;
  93. assign frameDrawn = (selectOutput == 1'b1) ? frameDrawn_hdmi : frameDrawn_ntsc;
  94. assign hsync = (selectOutput == 1'b1) ? hsync_hdmi : hsync_ntsc;
  95. assign vsync = (selectOutput == 1'b1) ? vsync_hdmi : ~vsync_ntsc; // ntsc vsync is inverted
  96. assign blank = (selectOutput == 1'b1) ? blank_hdmi : blank_ntsc;
  97. assign h_count = (selectOutput == 1'b1) ? h_count_hdmi : h_count_ntsc;
  98. assign v_count = (selectOutput == 1'b1) ? v_count_hdmi : v_count_ntsc;
  99. wire [2:0] BGW_r;
  100. wire [2:0] BGW_g;
  101. wire [1:0] BGW_b;
  102. BGWrenderer bgwrenderer(
  103. // Video I/O
  104. .clk(clkMuxOut),
  105. .hs(hsync),
  106. .vs(vsync),
  107. .blank(blank),
  108. .scale2x(selectOutput),
  109. // Output colors
  110. .r(BGW_r),
  111. .g(BGW_g),
  112. .b(BGW_b),
  113. .h_count(h_count), // line position in pixels including blanking
  114. .v_count(v_count), // frame position in lines including blanking
  115. // VRAM32
  116. .vram32_addr(vram32_addr),
  117. .vram32_q(vram32_q),
  118. // VRAM8
  119. .vram8_addr(vram8_addr),
  120. .vram8_q(vram8_q)
  121. );
  122. wire [2:0] PX_r;
  123. wire [2:0] PX_g;
  124. wire [1:0] PX_b;
  125. PixelEngine pixelEngine(
  126. // Video I/O
  127. .clk(clkMuxOut),
  128. .hs(hsync),
  129. .vs(vsync),
  130. .blank(blank),
  131. .scale2x(selectOutput),
  132. // Output colors
  133. .r(PX_r),
  134. .g(PX_g),
  135. .b(PX_b),
  136. .h_count(h_count), // line position in pixels including blanking
  137. .v_count(v_count), // frame position in lines including blanking
  138. // VRAM
  139. .vram_addr(vramPX_addr),
  140. .vram_q(vramPX_q)
  141. );
  142. // Give priority to pixel plane if bgw plane is black
  143. wire pxPriority = (BGW_r == 3'd0 && BGW_g == 3'd0 && BGW_b == 2'd0);
  144. wire [2:0] rendered_r;
  145. wire [2:0] rendered_g;
  146. wire [1:0] rendered_b;
  147. assign rendered_r = (pxPriority) ? PX_r: BGW_r;
  148. assign rendered_g = (pxPriority) ? PX_g: BGW_g;
  149. assign rendered_b = (pxPriority) ? PX_b : BGW_b;
  150. assign r_ntsc = (!selectOutput) ? rendered_r : 3'd0;
  151. assign g_ntsc = (!selectOutput) ? rendered_g : 3'd0;
  152. assign b_ntsc = (!selectOutput) ? rendered_b : 2'd0;
  153. wire [2:0] r_hdmi;
  154. wire [2:0] g_hdmi;
  155. wire [1:0] b_hdmi;
  156. assign r_hdmi = (selectOutput) ? rendered_r : 3'd0;
  157. assign g_hdmi = (selectOutput) ? rendered_g : 3'd0;
  158. assign b_hdmi = (selectOutput) ? rendered_b : 2'd0;
  159. wire [7:0] rByte;
  160. wire [7:0] gByte;
  161. wire [7:0] bByte;
  162. assign rByte = (r_hdmi == 3'd0) ? {r_hdmi, 5'b00000} : {r_hdmi, 5'b11111};
  163. assign gByte = (g_hdmi == 3'd0) ? {g_hdmi, 5'b00000} : {g_hdmi, 5'b11111};
  164. assign bByte = (b_hdmi == 2'd0) ? {b_hdmi, 6'b000000} : {b_hdmi, 6'b111111};
  165. // Convert VGA signal to HDMI signals
  166. RGB2HDMI rgb2hdmi(
  167. .clkTMDS(clkTMDShalf),
  168. .clkRGB (clkPixel),
  169. .rRGB (rByte),
  170. .gRGB (gByte),
  171. .bRGB (bByte),
  172. .blk (blank_hdmi),
  173. .hs (hsync_hdmi),
  174. .vs (vsync_hdmi),
  175. .bTMDS (TMDS[0]),
  176. .gTMDS (TMDS[1]),
  177. .rTMDS (TMDS[2]),
  178. .cTMDS (TMDS[3])
  179. );
  180. // Image file generator for simulation
  181. integer file;
  182. integer framecounter = 0;
  183. // HDMI
  184. always @(negedge vsync_hdmi)
  185. begin
  186. if (selectOutput == 1'b1)
  187. begin
  188. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC6/Verilog/output/frame%0d.ppm", framecounter), "w");
  189. $fwrite(file, "P3\n");
  190. $fwrite(file, "640 480\n");
  191. $fwrite(file, "255\n");
  192. framecounter = framecounter + 1;
  193. end
  194. end
  195. always @(posedge clkPixel)
  196. begin
  197. if (selectOutput == 1'b1)
  198. begin
  199. if (~blank_hdmi)
  200. begin
  201. $fwrite(file, "%d %d %d\n", rByte, gByte, bByte);
  202. end
  203. end
  204. end
  205. wire [7:0] rByte_ntsc;
  206. wire [7:0] gByte_ntsc;
  207. wire [7:0] bByte_ntsc;
  208. assign rByte_ntsc = (r_ntsc == 3'd0) ? {r_ntsc, 5'b00000} : {r_ntsc, 5'b11111};
  209. assign gByte_ntsc = (g_ntsc == 3'd0) ? {g_ntsc, 5'b00000} : {g_ntsc, 5'b11111};
  210. assign bByte_ntsc = (b_ntsc == 2'd0) ? {b_ntsc, 6'b000000} : {b_ntsc, 6'b111111};
  211. // NTSC
  212. always @(negedge vsync_ntsc)
  213. begin
  214. if (selectOutput == 1'b0)
  215. begin
  216. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC6/Verilog/output/frame%0d.ppm", framecounter), "w");
  217. $fwrite(file, "P3\n");
  218. $fwrite(file, "320 240\n");
  219. $fwrite(file, "255\n");
  220. framecounter = framecounter + 1;
  221. end
  222. end
  223. always @(posedge clkPixel)
  224. begin
  225. if (selectOutput == 1'b0)
  226. begin
  227. if (~blank_ntsc)
  228. begin
  229. $fwrite(file, "%d %d %d\n", rByte_ntsc, gByte_ntsc, bByte_ntsc);
  230. end
  231. end
  232. end
  233. endmodule