1
0

FSX.v 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // 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 [7:0] PX_r;
  134. wire [7:0] PX_g;
  135. wire [7: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 [7:0] BGW_r_Byte;
  156. wire [7:0] BGW_g_Byte;
  157. wire [7:0] BGW_b_Byte;
  158. assign BGW_r_Byte = (BGW_r == 3'd0) ? {BGW_r, 5'b00000} : {BGW_r, 5'b11111};
  159. assign BGW_g_Byte = (BGW_g == 3'd0) ? {BGW_g, 5'b00000} : {BGW_g, 5'b11111};
  160. assign BGW_b_Byte = (BGW_b == 2'd0) ? {BGW_b, 6'b000000} : {BGW_b, 6'b111111};
  161. wire [7:0] rByte;
  162. wire [7:0] gByte;
  163. wire [7:0] bByte;
  164. assign rByte = (pxPriority) ? PX_r: BGW_r_Byte;
  165. assign gByte = (pxPriority) ? PX_g: BGW_g_Byte;
  166. assign bByte = (pxPriority) ? PX_b : BGW_b_Byte;
  167. // Convert VGA signal to HDMI signals
  168. RGB2HDMI rgb2hdmi(
  169. .clkTMDS(clkTMDShalf),
  170. .clkRGB (clkPixel),
  171. .rRGB (rByte),
  172. .gRGB (gByte),
  173. .bRGB (bByte),
  174. .blk (blank_hdmi),
  175. .hs (hsync_hdmi),
  176. .vs (vsync_hdmi),
  177. .bTMDS (TMDS[0]),
  178. .gTMDS (TMDS[1]),
  179. .rTMDS (TMDS[2]),
  180. .cTMDS (TMDS[3])
  181. );
  182. // Image file generator for simulation
  183. integer file;
  184. integer framecounter = 0;
  185. // HDMI
  186. always @(negedge vsync_hdmi)
  187. begin
  188. if (selectOutput == 1'b1)
  189. begin
  190. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC6/Verilog/output/frame%0d.ppm", framecounter), "w");
  191. $fwrite(file, "P3\n");
  192. $fwrite(file, "640 480\n");
  193. $fwrite(file, "255\n");
  194. framecounter = framecounter + 1;
  195. end
  196. end
  197. always @(posedge clkPixel)
  198. begin
  199. if (selectOutput == 1'b1)
  200. begin
  201. if (~blank_hdmi)
  202. begin
  203. $fwrite(file, "%d %d %d\n", rByte, gByte, bByte);
  204. end
  205. end
  206. end
  207. /*
  208. wire [7:0] rByte_ntsc;
  209. wire [7:0] gByte_ntsc;
  210. wire [7:0] bByte_ntsc;
  211. assign rByte_ntsc = (r_ntsc == 3'd0) ? {r_ntsc, 5'b00000} : {r_ntsc, 5'b11111};
  212. assign gByte_ntsc = (g_ntsc == 3'd0) ? {g_ntsc, 5'b00000} : {g_ntsc, 5'b11111};
  213. assign bByte_ntsc = (b_ntsc == 2'd0) ? {b_ntsc, 6'b000000} : {b_ntsc, 6'b111111};
  214. // NTSC
  215. always @(negedge vsync_ntsc)
  216. begin
  217. if (selectOutput == 1'b0)
  218. begin
  219. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC6/Verilog/output/frame%0d.ppm", framecounter), "w");
  220. $fwrite(file, "P3\n");
  221. $fwrite(file, "320 240\n");
  222. $fwrite(file, "255\n");
  223. framecounter = framecounter + 1;
  224. end
  225. end
  226. always @(posedge clkPixel)
  227. begin
  228. if (selectOutput == 1'b0)
  229. begin
  230. if (~blank_ntsc)
  231. begin
  232. $fwrite(file, "%d %d %d\n", rByte_ntsc, gByte_ntsc, bByte_ntsc);
  233. end
  234. end
  235. end
  236. */
  237. endmodule