FSX.v 6.7 KB

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