1
0

FSX.v 7.2 KB

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