FSX.v 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 [23: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 [7:0] PX_r;
  127. wire [7:0] PX_g;
  128. wire [7: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 [7:0] BGW_r_Byte;
  149. wire [7:0] BGW_g_Byte;
  150. wire [7:0] BGW_b_Byte;
  151. assign BGW_r_Byte = (BGW_r == 3'd0) ? {BGW_r, 5'b00000} : {BGW_r, 5'b11111};
  152. assign BGW_g_Byte = (BGW_g == 3'd0) ? {BGW_g, 5'b00000} : {BGW_g, 5'b11111};
  153. assign BGW_b_Byte = (BGW_b == 2'd0) ? {BGW_b, 6'b000000} : {BGW_b, 6'b111111};
  154. wire [7:0] rByte;
  155. wire [7:0] gByte;
  156. wire [7:0] bByte;
  157. assign rByte = (pxPriority) ? PX_r: BGW_r_Byte;
  158. assign gByte = (pxPriority) ? PX_g: BGW_g_Byte;
  159. assign bByte = (pxPriority) ? PX_b : BGW_b_Byte;
  160. // Convert VGA signal to HDMI signals
  161. RGB2HDMI rgb2hdmi(
  162. .clkTMDS(clkTMDShalf),
  163. .clkRGB (clkPixel),
  164. .rRGB (rByte),
  165. .gRGB (gByte),
  166. .bRGB (bByte),
  167. .blk (blank_hdmi),
  168. .hs (hsync_hdmi),
  169. .vs (vsync_hdmi),
  170. .bTMDS (TMDS_p[0]),
  171. .gTMDS (TMDS_p[1]),
  172. .rTMDS (TMDS_p[2]),
  173. .cTMDS (TMDS_p[3]),
  174. .bTMDSn (TMDS_n[0]),
  175. .gTMDSn (TMDS_n[1]),
  176. .rTMDSn (TMDS_n[2]),
  177. .cTMDSn (TMDS_n[3])
  178. );
  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. */
  206. /*
  207. wire [7:0] rByte_ntsc;
  208. wire [7:0] gByte_ntsc;
  209. wire [7:0] bByte_ntsc;
  210. assign rByte_ntsc = (r_ntsc == 3'd0) ? {r_ntsc, 5'b00000} : {r_ntsc, 5'b11111};
  211. assign gByte_ntsc = (g_ntsc == 3'd0) ? {g_ntsc, 5'b00000} : {g_ntsc, 5'b11111};
  212. assign bByte_ntsc = (b_ntsc == 2'd0) ? {b_ntsc, 6'b000000} : {b_ntsc, 6'b111111};
  213. // NTSC
  214. always @(negedge vsync_ntsc)
  215. begin
  216. if (selectOutput == 1'b0)
  217. begin
  218. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC6/Verilog/output/frame%0d.ppm", framecounter), "w");
  219. $fwrite(file, "P3\n");
  220. $fwrite(file, "320 240\n");
  221. $fwrite(file, "255\n");
  222. framecounter = framecounter + 1;
  223. end
  224. end
  225. always @(posedge clkPixel)
  226. begin
  227. if (selectOutput == 1'b0)
  228. begin
  229. if (~blank_ntsc)
  230. begin
  231. $fwrite(file, "%d %d %d\n", rByte_ntsc, gByte_ntsc, bByte_ntsc);
  232. end
  233. end
  234. end
  235. */
  236. endmodule