FSX.v 7.1 KB

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