FSX.v 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. //Interrupt signal
  32. output frameDrawn
  33. );
  34. // LVDS Converter
  35. wire [3:0] TMDS;
  36. lvds lvdsConverter(
  37. .datain (TMDS),
  38. .dataout (TMDS_n),
  39. .dataout_b (TMDS_p) // Reversed because of a LVDS polarity swap on the V3 PCB
  40. );
  41. wire [11:0] h_count_hdmi;
  42. wire [11:0] v_count_hdmi;
  43. wire hsync_hdmi;
  44. wire vsync_hdmi;
  45. wire csync;
  46. wire blank_hdmi;
  47. wire frameDrawn_hdmi;
  48. TimingGenerator timingGenerator(
  49. // Clock
  50. .clkPixel(clkPixel),
  51. // Position counters
  52. .h_count(h_count_hdmi),
  53. .v_count(v_count_hdmi),
  54. // Video signals
  55. .hsync(hsync_hdmi),
  56. .vsync(vsync_hdmi),
  57. .csync(csync),
  58. .blank(blank_hdmi),
  59. // Interrupt signal
  60. .frameDrawn(frameDrawn_hdmi)
  61. );
  62. wire [2:0] r_ntsc;
  63. wire [2:0] g_ntsc;
  64. wire [1:0] b_ntsc;
  65. wire frameDrawn_ntsc;
  66. wire [11:0] h_count_ntsc;
  67. wire [11:0] v_count_ntsc;
  68. wire hsync_ntsc;
  69. wire vsync_ntsc;
  70. wire blank_ntsc;
  71. RGB332toNTSC rgb2ntsc(
  72. .clk(clk14), //14.318MHz
  73. .clkColor(clk114), //114.5454MHz
  74. .r(r_ntsc),
  75. .g(g_ntsc),
  76. .b(b_ntsc),
  77. .hcount(h_count_ntsc),
  78. .vcount(v_count_ntsc),
  79. .hs(hsync_ntsc),
  80. .vs(vsync_ntsc),
  81. .blank(blank_ntsc),
  82. .composite(composite), // video output signal
  83. .frameDrawn(frameDrawn_ntsc) // interrupt signal
  84. );
  85. wire hsync;
  86. wire vsync;
  87. wire blank;
  88. wire [11:0] h_count;
  89. wire [11:0] v_count;
  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. wire [2:0] BGW_r;
  97. wire [2:0] BGW_g;
  98. wire [1:0] BGW_b;
  99. BGWrenderer bgwrenderer(
  100. // Video I/O
  101. .clk(clkMuxOut),
  102. .hs(hsync),
  103. .vs(vsync),
  104. .blank(blank),
  105. .scale2x(selectOutput),
  106. // Output colors
  107. .r(BGW_r),
  108. .g(BGW_g),
  109. .b(BGW_b),
  110. .h_count(h_count), // line position in pixels including blanking
  111. .v_count(v_count), // frame position in lines including blanking
  112. // VRAM32
  113. .vram32_addr(vram32_addr),
  114. .vram32_q(vram32_q),
  115. // VRAM8
  116. .vram8_addr(vram8_addr),
  117. .vram8_q(vram8_q)
  118. );
  119. assign r_ntsc = (!selectOutput) ? BGW_r : 3'd0;
  120. assign g_ntsc = (!selectOutput) ? BGW_g : 3'd0;
  121. assign b_ntsc = (!selectOutput) ? BGW_b : 2'd0;
  122. wire [2:0] r_hdmi;
  123. wire [2:0] g_hdmi;
  124. wire [1:0] b_hdmi;
  125. assign r_hdmi = (selectOutput) ? BGW_r : 3'd0;
  126. assign g_hdmi = (selectOutput) ? BGW_g : 3'd0;
  127. assign b_hdmi = (selectOutput) ? BGW_b : 2'd0;
  128. wire [7:0] rByte;
  129. wire [7:0] gByte;
  130. wire [7:0] bByte;
  131. assign rByte = (r_hdmi == 3'd0) ? {r_hdmi, 5'b00000} : {r_hdmi, 5'b11111};
  132. assign gByte = (g_hdmi == 3'd0) ? {g_hdmi, 5'b00000} : {g_hdmi, 5'b11111};
  133. assign bByte = (b_hdmi == 2'd0) ? {b_hdmi, 6'b000000} : {b_hdmi, 6'b111111};
  134. // Convert VGA signal to HDMI signals
  135. RGB2HDMI rgb2hdmi(
  136. .clkTMDS(clkTMDShalf),
  137. .clkRGB (clkPixel),
  138. .rRGB (rByte),
  139. .gRGB (gByte),
  140. .bRGB (bByte),
  141. .blk (blank_hdmi),
  142. .hs (hsync_hdmi),
  143. .vs (vsync_hdmi),
  144. .bTMDS (TMDS[0]),
  145. .gTMDS (TMDS[1]),
  146. .rTMDS (TMDS[2]),
  147. .cTMDS (TMDS[3])
  148. );
  149. // Image file generator for simulation
  150. integer file;
  151. integer framecounter = 0;
  152. always @(negedge vsync_hdmi)
  153. begin
  154. file = $fopen($sformatf("/home/bart/Documents/FPGA/FPGC6/Verilog/output/frame%0d.ppm", framecounter), "w");
  155. $fwrite(file, "P3\n");
  156. $fwrite(file, "640 480\n");
  157. $fwrite(file, "255\n");
  158. framecounter = framecounter + 1;
  159. end
  160. always @(posedge clkPixel)
  161. begin
  162. if (~blank_hdmi)
  163. begin
  164. $fwrite(file, "%d %d %d\n", rByte, gByte, bByte);
  165. end
  166. end
  167. endmodule