Advertisement
Guest User

Display_optimised

a guest
May 2nd, 2017
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // --------------------------------------------------------------------
  2. // Copyright (c) 2007 by Terasic Technologies Inc.
  3. // --------------------------------------------------------------------
  4. //
  5. // Permission:
  6. //
  7. //   Terasic grants permission to use and modify this code for use
  8. //   in synthesis for all Terasic Development Boards and Altera Development
  9. //   Kits made by Terasic.  Other use of this code, including the selling
  10. //   ,duplication, or modification of any portion is strictly prohibited.
  11. //
  12. // Disclaimer:
  13. //
  14. //   This VHDL/Verilog or C/C++ source code is intended as a design reference
  15. //   which illustrates how these types of functions can be implemented.
  16. //   It is the user's responsibility to verify their design for
  17. //   consistency and functionality through the use of formal
  18. //   verification methods.  Terasic provides no warranty regarding the use
  19. //   or functionality of this code.
  20. //
  21. // --------------------------------------------------------------------
  22. //          
  23. //                     Terasic Technologies Inc
  24. //                     356 Fu-Shin E. Rd Sec. 1. JhuBei City,
  25. //                     HsinChu County, Taiwan
  26. //                     302
  27. //
  28. //                     web: http://www.terasic.com/
  29. //                     email: support@terasic.com
  30. //
  31. // --------------------------------------------------------------------
  32. //
  33. // Revision History :
  34. // --------------------------------------------------------------------
  35. //  Ver  :| Author                :| Mod. Date :| Changes Made:
  36. //  V1.0 :| Johnny Fan               :| 07/06/30  :| Initial Revision
  37. //   V2.0 :| Charlotte Frenkel      :| 14/08/03  :| Adaptation for ELEC2103 project
  38. //  V3.0 :| Ludovic Moreau            :| 17/02/06  :| Adaptation for ELEC2103 project
  39. // --------------------------------------------------------------------
  40.  
  41. module mtl_display_controller(
  42.     // Host Side
  43.     iCLK,               // Input LCD control clock
  44.     iRST_n,                 // Input system reset
  45.     iColorData,         // Input hardcoded color data
  46.     oNewFrame,          // Output signal being a pulse when a new frame of the LCD begins
  47.     oEndFrame,          // Output signal being a pulse when a frame of the LCD ends
  48.     // LCD Side
  49.     oHD,                    // Output LCD horizontal sync
  50.     oVD,                    // Output LCD vertical sync
  51.     oLCD_R,             // Output LCD red color data
  52.     oLCD_G,           // Output LCD green color data  
  53.     oLCD_B,            // Output LCD blue color data
  54.    
  55.     S1, S2, S3, S4, S5, S6, S7, S8, S9, S10,code,pos,code2,
  56.     ROM_address, ROM_data, ROM_address2, ROM_data2, point_marker
  57. );
  58.                        
  59. //============================================================================
  60. // PARAMETER declarations
  61. //============================================================================
  62.  
  63. // All these parameters are given in the MTL datasheet, section 3.2,
  64. // available in the project file folder
  65. parameter H_LINE = 1056;
  66. parameter V_LINE = 525;
  67. parameter Horizontal_Blank = 46;          // H_SYNC + H_Back_Porch
  68. parameter Horizontal_Front_Porch = 210;
  69. parameter Vertical_Blank = 23;             // V_SYNC + V_BACK_PORCH
  70. parameter Vertical_Front_Porch = 22;
  71.  
  72. //===========================================================================
  73. // PORT declarations
  74. //===========================================================================
  75.  
  76. input               iCLK;  
  77. input               iRST_n;
  78. input  [23:0]  iColorData;
  79. output          oNewFrame;
  80. output          oEndFrame;
  81. output          oHD;
  82. output          oVD;
  83. output [7:0]    oLCD_R;    
  84. output [7:0]    oLCD_G;
  85. output [7:0]    oLCD_B;
  86. input [23:0]   S1, S2, S3, S4, S5, S6, S7, S8, S9, S10;
  87. input [31:0] code,pos, code2, point_marker;
  88. output [13:0] ROM_address;
  89. input [31:0] ROM_data;
  90. output [14:0] ROM_address2;
  91. input [7:0] ROM_data2;
  92. //=============================================================================
  93. // REG/WIRE declarations
  94. //=============================================================================
  95.  
  96. reg  [10:0] x_cnt, x_next2;  
  97. reg  [9:0]  y_cnt, y_next2;
  98. wire [7:0]  read_red;
  99. wire [7:0]  read_green;
  100. wire [7:0]  read_blue;
  101. wire            display_area, display_area_prev;
  102. reg         mhd;
  103. reg         mvd;
  104.  
  105.  
  106. //=============================================================================
  107. // Structural coding
  108. //=============================================================================
  109.  
  110.  
  111. //--- Assigning the right color data as a function -------------------------
  112. //--- of the current pixel position ----------------------------------------
  113.                
  114. // This signal indicates the LCD active display area shifted back from
  115. // 1 pixel in the x direction. This accounts for the 1-cycle delay
  116. // in the sequential logic.
  117. assign  display_area = ((x_cnt>(Horizontal_Blank-2)&&
  118.                         (x_cnt<(H_LINE-Horizontal_Front_Porch-1))&&
  119.                         (y_cnt>(Vertical_Blank-1))&&
  120.                         (y_cnt<(V_LINE-Vertical_Front_Porch))));
  121.  
  122. // This signal indicates the same LCD active display area, now shifted
  123. // back from 2 pixels in the x direction, again for sequential delays.
  124. assign  display_area_prev = ((x_cnt>(Horizontal_Blank-3)&&
  125.                         (x_cnt<(H_LINE-Horizontal_Front_Porch-2))&&
  126.                         (y_cnt>(Vertical_Blank-1))&&
  127.                         (y_cnt<(V_LINE-Vertical_Front_Porch))));   
  128.                        
  129.  
  130.  
  131. // -------- GRAPHIC DRIVER ------------
  132.  
  133. logic Display_S1, Display_S2, Display_S3, Display_S4, Display_S5, Display_S6, Display_S7, Display_S8, Display_S9, Display_S10;
  134. logic Display_next_S1, Display_next_S2, Display_next_S3, Display_next_S4, Display_next_S5, Display_next_S6, Display_next_S7, Display_next_S8, Display_next_S9, Display_next_S10;
  135. logic [23:0] Colors_S1, Colors_S2, Colors_S3, Colors_S4, Colors_S5, Colors_S6, Colors_S7, Colors_S8, Colors_S9, Colors_S10;
  136. logic [3:0] ROM_addr_select;
  137. logic [13:0] ROM_address_S1, ROM_address_S2, ROM_address_S3, ROM_address_S4, ROM_address_S5, ROM_address_S6, ROM_address_S7, ROM_address_S8, ROM_address_S9, ROM_address_S10;
  138. logic TDU, Go1, Go2, C1, C2, C3, C4, C5, C6, C7, C8, C9, F1, F2;
  139. logic [2:0] CondStart;
  140. assign CondStart = point_marker[29:27];
  141. logic Display_SC0, Display_SC1, Display_SC2, Display_SC01, Display_SC11, Display_SC21;
  142. logic [13:0] N_code, E_code, W_code, G_code, A_code, M_code, Y_code, O_code, U_code, I_code, L_code, S_code, Plus_code, Moins_code, Un_code, Deux_code, Cinq_code;
  143.  
  144.  
  145. logic [18:0] pos_ed, pos_ed1, pos_ed2, pos_ed3, pos_ed4, pos_ed5, pos_ed6, pos_ed7, pos_ed8, pos_ed9, pos_ed10, pos_ed11, pos_ed12, pos_ed13, pos_ed14, pos_ed15;
  146. logic Disp_dr1, Disp_dr2, Disp_dr3, Disp_dr4, Disp_dr5, Disp_dr6, Disp_dr7, Disp_dr8, Disp_dr9, Disp_dr10, Disp_dr11, Disp_dr12, Disp_dr13, Disp_dr14, Disp_dr15;
  147.  
  148. //logic Display_pp1, Display_pp2, Display_pp3, Display_pp4, Display_pp5, Display_pp6, Display_pp7, Display_pp8, Display_pp9;
  149. //logic Display_doigt;
  150.  
  151. always @(posedge iCLK or negedge iRST_n)
  152. if(~iRST_n)         pos_ed <= 0;
  153. else if (oEndFrame && pos[0])   pos_ed <=  pos[19:1];       // Update the color displayed between
  154. else                pos_ed <= pos_ed;
  155.  
  156. logic dCLK;
  157. downclock d(iCLK, dCLK);
  158. always @(posedge dCLK or negedge iRST_n)
  159. if(~iRST_n) begin
  160. pos_ed1 <= 0;
  161. pos_ed2 <= 0;
  162. pos_ed3 <= 0;
  163. pos_ed4 <= 0;
  164. pos_ed5 <= 0;
  165. pos_ed6 <= 0;
  166. /*pos_ed7 <= 0;
  167. pos_ed8 <= 0;
  168. pos_ed9 <= 0;
  169. pos_ed10 <= 0;
  170. pos_ed11 <= 0;
  171. pos_ed12 <= 0;
  172. pos_ed13 <= 0;
  173. pos_ed14 <= 0;
  174. pos_ed15 <= 0;*/
  175. end
  176. else begin
  177. pos_ed1 <= pos_ed;
  178. pos_ed2 <= pos_ed1;
  179. pos_ed3 <= pos_ed2;
  180. pos_ed4 <= pos_ed3;
  181. pos_ed5 <= pos_ed4;
  182. pos_ed6 <= pos_ed5;
  183. /*pos_ed7 <= pos_ed6;
  184. pos_ed8 <= pos_ed7;
  185. pos_ed9 <= pos_ed8;
  186. pos_ed10 <= pos_ed9;
  187. pos_ed11 <= pos_ed10;
  188. pos_ed12 <= pos_ed11;
  189. pos_ed13 <= pos_ed12;
  190. pos_ed14 <= pos_ed13;
  191. pos_ed15 <= pos_ed14;
  192.  
  193. Display_pp2 <= Display_pp1;
  194. Display_pp3 <= Display_pp2;
  195. Display_pp4 <= Display_pp3;
  196. Display_pp5 <= Display_pp4;
  197. Display_pp6 <= Display_pp5;
  198. Display_pp7 <= Display_pp6;
  199. Display_pp8 <= Display_pp7;
  200. Display_pp9 <= Display_pp8;
  201. */
  202. end
  203.  
  204. //assign Display_doigt =  ((x_cnt - pos_ed[9:0])*(x_cnt - pos_ed[9:0]) + (y_cnt - pos_ed[18:10])*(y_cnt - pos_ed[18:10])<25);*/
  205.  
  206. //assign Display_pp1 =  ((pos_ed1[9:0] - pos_ed[9:0])*(pos_ed1[9:0] - pos_ed[9:0]) + (pos_ed1[18:10] - pos_ed[18:10])*(pos_ed1[18:10] - pos_ed[18:10])<2500);
  207.  
  208. Droite dr1(x_cnt, y_cnt, pos_ed[9:0]+44, pos_ed[18:10]+22, pos_ed1[9:0]+44, pos_ed1[18:10]+22, 3, Disp_dr1);
  209. Droite dr2(x_cnt, y_cnt, pos_ed1[9:0]+44, pos_ed1[18:10]+22, pos_ed2[9:0]+44, pos_ed2[18:10]+22, 3, Disp_dr2);
  210. Droite dr3(x_cnt, y_cnt, pos_ed2[9:0]+44, pos_ed2[18:10]+22, pos_ed3[9:0]+44, pos_ed3[18:10]+22, 3, Disp_dr3);
  211. Droite dr4(x_cnt, y_cnt, pos_ed3[9:0]+44, pos_ed3[18:10]+22, pos_ed4[9:0]+44, pos_ed4[18:10]+22, 3, Disp_dr4);
  212. Droite dr5(x_cnt, y_cnt, pos_ed4[9:0]+44, pos_ed4[18:10]+22, pos_ed5[9:0]+44, pos_ed5[18:10]+22, 3, Disp_dr5);
  213. Droite dr6(x_cnt, y_cnt, pos_ed5[9:0]+44, pos_ed5[18:10]+22, pos_ed6[9:0]+44, pos_ed6[18:10]+22, 3, Disp_dr6);
  214. /*Droite dr7(x_cnt, y_cnt, pos_ed6[9:0]+44, pos_ed6[18:10]+22, pos_ed7[9:0]+44, pos_ed7[18:10]+22, 3, Disp_dr7);
  215. Droite dr8(x_cnt, y_cnt, pos_ed7[9:0]+44, pos_ed7[18:10]+22, pos_ed8[9:0]+44, pos_ed8[18:10]+22, 3, Disp_dr8);
  216. Droite dr9(x_cnt, y_cnt, pos_ed8[9:0]+44, pos_ed8[18:10]+22, pos_ed9[9:0]+44, pos_ed9[18:10]+22, 3, Disp_dr9);
  217. Droite dr10(x_cnt, y_cnt, pos_ed9[9:0]+44, pos_ed9[18:10]+22, pos_ed10[9:0]+44, pos_ed10[18:10]+22, 3, Disp_dr10);
  218. Droite dr11(x_cnt, y_cnt, pos_ed10[9:0]+44, pos_ed10[18:10]+22, pos_ed11[9:0]+44, pos_ed11[18:10]+22, 3, Disp_dr11);
  219. Droite dr12(x_cnt, y_cnt, pos_ed11[9:0]+44, pos_ed11[18:10]+22, pos_ed12[9:0]+44, pos_ed12[18:10]+22, 3, Disp_dr12);
  220. Droite dr13(x_cnt, y_cnt, pos_ed12[9:0]+44, pos_ed12[18:10]+22, pos_ed13[9:0]+44, pos_ed13[18:10]+22, 3, Disp_dr13);
  221. Droite dr14(x_cnt, y_cnt, pos_ed13[9:0]+44, pos_ed13[18:10]+22, pos_ed14[9:0]+44, pos_ed14[18:10]+22, 3, Disp_dr14);
  222. Droite dr15(x_cnt, y_cnt, pos_ed14[9:0]+44, pos_ed14[18:10]+22, pos_ed15[9:0]+44, pos_ed15[18:10]+22, 3, Disp_dr15);
  223. */
  224.  
  225. // Image logic
  226. assign ROM_addr_select = Display_next_S1 ? 4'd1 : (Display_next_S2 ? 4'd2 : (Display_next_S3 ? 4'd3 :
  227.                                 (Display_next_S4 ? 4'd4 : (Display_next_S5 ? 4'd5 : (Display_next_S6 ? 4'd6 :
  228.                                 (Display_next_S7 ? 4'd7 : (Display_next_S8 ? 4'd8 : (Display_next_S9 ? 4'd9 : 4'd10))))))));
  229.        
  230. mux15 MUX(iCLK, ROM_addr_select, ROM_address, ROM_address_S1, ROM_address_S2, ROM_address_S3, ROM_address_S4, ROM_address_S5, ROM_address_S6,
  231.         ROM_address_S7, ROM_address_S8, ROM_address_S9, ROM_address_S10);
  232. //assign ROM_address = ROM_address_S1;
  233.  
  234.  
  235. // 3-2-1-GO
  236.  
  237. DisplayBigDigit Dispdig1(x_cnt, y_cnt, (14'b11001111 & {14 {CondStart ==3'd3}})|(14'b11011011& {14 {CondStart ==3'd2}})|(14'b1001000000000 & {14 {CondStart ==3'd1}}), 11'd514, 10'd132, TDU);
  238. DisplayBigDigit Dispdig2(x_cnt, y_cnt, 14'b00000010111101, 11'd594, 10'd132, Go1);
  239. DisplayBigDigit Dispdig3(x_cnt, y_cnt, 14'b111111, 11'd434, 10'd132, Go2);
  240.  
  241. // New Game - You win - You Lose
  242. Decode dec1(6'd23, N_code);
  243. Decode dec2(6'd14 , E_code);
  244. Decode dec3(6'd32 , W_code);
  245. Decode dec4(6'd16 , G_code);
  246. Decode dec5(6'd10 , A_code);
  247. Decode dec6(6'd22 , M_code);
  248. Decode dec7(6'd34, Y_code);
  249. Decode dec8(6'd24 , O_code);
  250. Decode dec9(6'd30 , U_code);
  251. Decode dec10(6'd18 , I_code);
  252. Decode dec11(6'd21 , L_code);
  253. Decode dec12(6'd28 , S_code);
  254. Decode dec13(6'd36 , Plus_code);
  255. Decode dec14(6'd37 , Moins_code);
  256. Decode dec15(6'd1 , Un_code);
  257. Decode dec16(6'd2 , Deux_code);
  258. Decode dec17(6'd5 , Cinq_code);
  259.  
  260. DisplayDigit Dispdig4(x_cnt, y_cnt, (N_code & {14 {point_marker[24]}})|(Y_code & ({14 {point_marker[25]}}|{14 {point_marker[26]}})), 11'd570, 10'd236, C1);
  261. DisplayDigit Dispdig5(x_cnt, y_cnt, (E_code & {14 {point_marker[24]}})|(O_code & ({14 {point_marker[25]}}|{14 {point_marker[26]}})), 11'd538, 10'd236, C2);
  262. DisplayDigit Dispdig6(x_cnt, y_cnt, (W_code & {14 {point_marker[24]}})|(U_code & ({14 {point_marker[25]}}|{14 {point_marker[26]}})), 11'd506, 10'd236, C3);
  263. DisplayDigit Dispdig7(x_cnt, y_cnt, (G_code & {14 {point_marker[24]}})|(14'd0 & {14 {point_marker[25]}})|(L_code & {14 {point_marker[26]}}), 11'd442, 10'd236, C4);
  264. DisplayDigit Dispdig8(x_cnt, y_cnt, (A_code & {14 {point_marker[24]}})|(W_code & {14 {point_marker[25]}})|(O_code & {14 {point_marker[26]}}), 11'd410, 10'd236, C5);
  265. DisplayDigit Dispdig9(x_cnt, y_cnt,(M_code & {14 {point_marker[24]}})|(I_code & {14 {point_marker[25]}})|(S_code & {14 {point_marker[26]}}), 11'd378, 10'd236, C6);
  266. DisplayDigit Dispdig10(x_cnt, y_cnt,(E_code & {14 {point_marker[24]}})|(N_code & {14 {point_marker[25]}})|(E_code & {14 {point_marker[26]}}), 11'd346, 10'd236, C7);
  267.  
  268. DisplayDigit Dispdig11(x_cnt, y_cnt,(Moins_code & {14{point_marker[22:21]==2'd0}})|(Plus_code & {14{point_marker[22:21]!=2'd0}}), point_marker[10:0], point_marker[20:11], C8);
  269. DisplayDigit Dispdig12(x_cnt, y_cnt,(Un_code & {14{point_marker[22:21]==2'd1}})|(Deux_code & {14{point_marker[22:21]==2'd2}})|(Cinq_code & {14{point_marker[22:21]==2'd0|point_marker[22:21]==2'd3}}), point_marker[10:0]-11'd32, point_marker[20:11], C9);
  270.  
  271. Rectangle Font(x_cnt, y_cnt, 9'd270, 9'd60, 11'd579, 10'd232, F1);
  272.  
  273. // Sphere Logic
  274. DisplaySphere DS1(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  275.                         S1, Display_S1, Display_next_S1, Colors_S1, ROM_address_S1);
  276.                        
  277. DisplaySphere DS2(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  278.                         S2, Display_S2, Display_next_S2, Colors_S2, ROM_address_S2);
  279.                        
  280. DisplaySphere DS3(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  281.                         S3, Display_S3, Display_next_S3, Colors_S3, ROM_address_S3);
  282.                        
  283. DisplaySphere DS4(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  284.                         S4, Display_S4, Display_next_S4, Colors_S4, ROM_address_S4);
  285.                        
  286. DisplaySphere DS5(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  287.                         S5, Display_S5, Display_next_S5, Colors_S5, ROM_address_S5);
  288.                        
  289. DisplaySphere DS6(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  290.                         S6, Display_S6, Display_next_S6, Colors_S6, ROM_address_S6);
  291.                        
  292. DisplaySphere DS7(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  293.                         S7, Display_S7, Display_next_S7, Colors_S7, ROM_address_S7);
  294.                        
  295. DisplaySphere DS8(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  296.                         S8, Display_S8, Display_next_S8, Colors_S8, ROM_address_S8);
  297.                        
  298. DisplaySphere DS9(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  299.                         S9, Display_S9, Display_next_S9, Colors_S9, ROM_address_S9);
  300.                        
  301. DisplaySphere DS10(iCLK, x_cnt, y_cnt, x_next2, y_next2, ROM_data,
  302.                         S10, Display_S10, Display_next_S10, Colors_S10, ROM_address_S10);
  303. // Score Logic                     
  304. DisplayScore DSSC0(x_cnt, y_cnt, code[20:14],100, 460,Display_SC0);
  305.  
  306. DisplayScore DSSC1(x_cnt, y_cnt, code[13:7],85, 460, Display_SC1);
  307.  
  308. DisplayScore DSSC2(x_cnt, y_cnt, code[6:0],70,460, Display_SC2);
  309.  
  310. DisplayScore DSSC01(x_cnt, y_cnt, code2[20:14],830, 460,Display_SC01);
  311.  
  312. DisplayScore DSSC11(x_cnt, y_cnt, code2[13:7],815, 460,Display_SC11);
  313.  
  314. DisplayScore DSSC21(x_cnt, y_cnt, code2[6:0],800, 460,Display_SC21);
  315.  
  316.  
  317. assign ROM_address2 = ((15'd844 >> 2) - x_cnt[10:2]) + ((15'd502 >> 3) - y_cnt[9:3]) * 8'd200;
  318. //assign ROM_address2 = ((844 >> 3) - x_cnt[10:3]) + ((502 >> 3) - y_cnt[9:3]) * 7'd100; // upscaling 8x
  319. //assign ROM_address2 = (((846-x_cnt) >>1) /3)+ (((502-y_cnt)>>1) /3)*133; // upscaling 6x
  320.  
  321. // Assigns the right color data.
  322. always_ff @(posedge iCLK) begin
  323.     // If the screen is reset, put at zero the color signals.
  324.     if (!iRST_n) begin
  325.         read_red    <= 8'b0;
  326.         read_green  <= 8'b0;
  327.         read_blue   <= 8'b0;
  328.     // If we are not in the active display area...
  329.     end else if (~display_area) begin
  330.         read_red    <= 8'b0;
  331.         read_green  <= 8'b0;
  332.         read_blue   <= 8'b0;
  333.     end else if (Display_SC0 || Display_SC1 || Display_SC2) begin // if we are in the own score area
  334.         read_red    <= 8'h00;
  335.         read_green  <= 8'hFF;
  336.         read_blue   <= 8'h00;
  337.     end else if (Display_SC01 || Display_SC11 || Display_SC21) begin // if we are in the oppponent score area
  338.         read_red    <= 8'hFF;
  339.         read_green  <= 8'h00;
  340.         read_blue   <= 8'h00;
  341.     end else if (Disp_dr1 || Disp_dr2 || Disp_dr3 || Disp_dr4 || Disp_dr5 || Disp_dr6 /*| Disp_dr7 | Disp_dr8 | Disp_dr9 */) begin // if we are in the Trace zone
  342.         read_red    <= 8'h00;
  343.         read_green  <= 8'h00;
  344.         read_blue   <= 8'hFF;
  345.     end else if ((TDU & (CondStart==3|CondStart==2|CondStart==1)) | (Go1 & CondStart ==4) | (Go2 & CondStart ==4)) begin // if we are in 3-2-1-Go zone
  346.         read_red    <= 8'hFF;
  347.         read_green  <= 8'h00;
  348.         read_blue   <= 8'h00;
  349.     end else if (point_marker[24] & (C1|C2|C3|C4|C5|C6|C7)) begin // if we are in New Game Zone
  350.         read_red    <= 8'h00;
  351.         read_green  <= 8'h00;
  352.         read_blue   <= 8'h00;
  353.     end else if (point_marker[25] & (C1|C2|C3|C4|C5|C6|C7)) begin // if we are in You Win zone
  354.         read_red    <= 8'h00;
  355.         read_green  <= 8'h00;
  356.         read_blue   <= 8'h00;
  357.     end else if (point_marker[26] & (C1|C2|C3|C4|C5|C6|C7)) begin // if we are in You Lose zone
  358.         read_red    <= 8'h00;
  359.         read_green  <= 8'h00;
  360.         read_blue   <= 8'h00;
  361.     end else if ((point_marker[24] | point_marker[25] | point_marker[26]) & F1) begin // if we are in Font zone
  362.         read_red    <= 8'hFF;
  363.         read_green  <= 8'hFF;
  364.         read_blue   <= 8'h00;
  365.     end else if ((C8|C9) & point_marker[23]) begin // if we are in Font zone
  366.         read_red    <= 8'hFF;
  367.         read_green  <= 8'h00;
  368.         read_blue   <= 8'h00;
  369.     end else if (Display_S1) begin // if we are in the sphere area
  370.         read_red    <= Colors_S1[23:16];
  371.         read_green  <= Colors_S1[15:8];
  372.         read_blue   <= Colors_S1[7:0];
  373.     end else if (Display_S2) begin // if we are in the sphere area
  374.         read_red    <= Colors_S2[23:16];
  375.         read_green  <= Colors_S2[15:8];
  376.         read_blue   <= Colors_S2[7:0];
  377.     end else if (Display_S3) begin // if we are in the sphere area
  378.         read_red    <= Colors_S3[23:16];
  379.         read_green  <= Colors_S3[15:8];
  380.         read_blue   <= Colors_S3[7:0];
  381.     end else if (Display_S4) begin // if we are in the sphere area
  382.         read_red    <= Colors_S4[23:16];
  383.         read_green  <= Colors_S4[15:8];
  384.         read_blue   <= Colors_S4[7:0];
  385.     end else if (Display_S5) begin // if we are in the sphere area
  386.         read_red    <= Colors_S5[23:16];
  387.         read_green  <= Colors_S5[15:8];
  388.         read_blue   <= Colors_S5[7:0];
  389.     end else if (Display_S6) begin // if we are in the sphere area
  390.         read_red    <= Colors_S6[23:16];
  391.         read_green  <= Colors_S6[15:8];
  392.         read_blue   <= Colors_S6[7:0];
  393.     end else if (Display_S7) begin // if we are in the sphere area
  394.         read_red    <= Colors_S7[23:16];
  395.         read_green  <= Colors_S7[15:8];
  396.         read_blue   <= Colors_S7[7:0];
  397.     end else if (Display_S8) begin // if we are in the sphere area
  398.         read_red    <= Colors_S8[23:16];
  399.         read_green  <= Colors_S8[15:8];
  400.         read_blue   <= Colors_S8[7:0];
  401.     end else if (Display_S9) begin // if we are in the sphere area
  402.         read_red    <= Colors_S9[23:16];
  403.         read_green  <= Colors_S9[15:8];
  404.         read_blue   <= Colors_S9[7:0];
  405.     end else if (Display_S10) begin // if we are in the sphere area
  406.         read_red    <= Colors_S10[23:16];
  407.         read_green  <= Colors_S10[15:8];
  408.         read_blue   <= Colors_S10[7:0];
  409.     end else if (code[21]) begin // if we are freezed
  410.         read_red    <= 8'hC0;
  411.         read_green  <= 8'hC0;
  412.         read_blue   <= 8'hC0;
  413.     end else begin // if we are in the active display area
  414.         read_red    <= ROM_data2;
  415.         read_green  <= ROM_data2;
  416.         read_blue   <= ROM_data2;
  417.     end
  418. end
  419.  
  420.  
  421. // -------- END GRAPHIC DRIVER -------
  422.  
  423. //--- Keeping track of x and y positions of the current pixel ------------------
  424. //--- and generating the horiz. and vert. sync. signals ------------------------
  425.  
  426. always@(posedge iCLK or negedge iRST_n) begin
  427.     if (!iRST_n)
  428.     begin
  429.         x_cnt <= 11'd0;
  430.        x_next2 <= 11'd2;   
  431.         mhd  <= 1'd0;  
  432.     end
  433.     else if (x_cnt == (H_LINE-1))
  434.     begin
  435.         x_cnt <= 11'd0;
  436.         x_next2 <= 11'd2;
  437.         mhd  <= 1'd0;
  438.     end    
  439.     else
  440.     begin
  441.         x_cnt <= x_cnt + 11'd1;
  442.         if (x_next2 == (H_LINE-1))
  443.             x_next2 <= 11'd0;
  444.         else
  445.             x_next2 <= x_next2 + 11'd1;
  446.         mhd  <= 1'd1;
  447.     end
  448. end
  449.  
  450. always@(posedge iCLK or negedge iRST_n) begin
  451.     if (!iRST_n) begin
  452.         y_cnt <= 10'd0;
  453.     end
  454.     else if (x_cnt == (H_LINE-1))
  455.     begin
  456.         if (y_cnt == (V_LINE-1))
  457.             y_cnt <= 10'd0;
  458.         else
  459.             y_cnt <= y_cnt + 10'd1;
  460.     end
  461. end
  462.  
  463. always@(posedge iCLK or negedge iRST_n) begin
  464.     if (!iRST_n) begin
  465.         y_next2 <= 10'd2;
  466.     end
  467.     else if (x_next2 == (H_LINE-1))
  468.     begin
  469.         if (y_next2 == (V_LINE-1))
  470.             y_next2 <= 10'd0;
  471.         else
  472.             y_next2 <= y_next2 + 10'd1;
  473.     end
  474. end
  475.  
  476.  
  477. always@(posedge iCLK  or negedge iRST_n) begin
  478.     if (!iRST_n)
  479.         mvd  <= 1'b1;
  480.     else if (y_cnt == 10'd0)
  481.         mvd  <= 1'b0;
  482.     else
  483.         mvd  <= 1'b1;
  484. end
  485.  
  486. assign oNewFrame = ((x_cnt == 11'd0)   && (y_cnt == 10'd0)  ); 
  487. assign oEndFrame = ((x_cnt == 11'd846) && (y_cnt == 10'd503)); 
  488.    
  489. //--- Assigning synchronously the color and sync. signals ------------------
  490.  
  491. always@(posedge iCLK or negedge iRST_n) begin
  492.     if (!iRST_n)
  493.         begin
  494.             oHD <= 1'd0;
  495.             oVD <= 1'd0;
  496.             oLCD_R <= 8'd0;
  497.             oLCD_G <= 8'd0;
  498.             oLCD_B <= 8'd0;
  499.         end
  500.     else
  501.         begin
  502.             oHD <= mhd;
  503.             oVD <= mvd;
  504.             oLCD_R <= read_red;
  505.             oLCD_G <= read_green;
  506.             oLCD_B <= read_blue;
  507.         end    
  508. end
  509.  
  510.                            
  511. endmodule
  512.  
  513.  
  514. // [23:0] sphere : {enabled, color:2, y_center:10, x_center:11}
  515. module DisplaySphere(input iCLK, input [10:0] x, input [9:0] y, input [10:0] x_next, input [9:0] y_next, input [31:0] ROM_data, input [23:0] sphere,
  516.                             output oDisplay, output oDisplay_next, output [23:0] oColors, output [13:0] ROM_address);
  517.    
  518.     logic [31:0] radius;
  519.     assign radius = 31'd30;
  520.    
  521.     logic [7:0] red, green, blue;
  522.    
  523.     logic [10:0] x_center;
  524.     logic [9:0] y_center;
  525.     logic [1:0] color;
  526.    
  527.     assign x_center = sphere[10:0];
  528.     assign y_center = sphere[20:11];
  529.     assign color    = sphere[22:21];
  530.    
  531.    assign oDisplay = (sphere[23]) && (ROM_data[23:0] != 24'hFFFFFF) && ((x - x_center)*(x - x_center) + (y - y_center)*(y - y_center)<radius*radius);
  532.    assign oDisplay_next = (sphere[23]) && ((x_next - x_center)*(x_next - x_center) + (y_next - y_center)*(y_next - y_center)<radius*radius);   
  533.    
  534.    
  535.     // Preparing signed logic registers
  536.     logic [13:0] pos_mem, ROM_offset;
  537.     logic [13:0] frame_dist_x, frame_dist_y;
  538.  
  539.     logic sr1, sr2;
  540.     Substraction #(32)(x_center+radius, x, 1'b0, 1'b0, frame_dist_x, sr1);
  541.     Substraction #(32)(y_center+radius, y, 1'b0, 1'b0, frame_dist_y, sr2);
  542.     assign pos_mem = frame_dist_x + frame_dist_y*14'd60;
  543.    
  544.     logic [23:0] rColors;
  545.    
  546.     always_comb
  547.         if (color == 2'd0)
  548.             ROM_offset = 14'd0;
  549.         else if (color == 2'd1)
  550.             ROM_offset = 14'd3600;
  551.         else if (color == 2'd2)
  552.             ROM_offset = 14'd7200;
  553.         else
  554.             ROM_offset = 14'd10800;
  555.    
  556.     assign oColors = rColors;
  557.     assign ROM_address = pos_mem + ROM_offset;// + (color == 2'd0) ? 14'd0 : (color == 2'd1 ? 14'd3600 : (color == 2'd2 ? 14'd7200 : (color == 2'd3 ? 14'd10800 : 14'd0)));
  558.    
  559.     always_ff @(posedge iCLK) begin
  560.         if (oDisplay_next) begin
  561.             rColors <= ROM_data[23:0];
  562.         end
  563.        
  564.     end
  565.    
  566.  
  567. endmodule
  568. // [23:0] sphere : {enabled, color:2, y_center:10, x_center:11}
  569. module DisplayScore(input [10:0] x, input [9:0] y, input [6:0] code, input [10:0] x_init, input [9:0] y_init, output oDisplay);
  570. logic D1, D2, D3, D4, D5, D6, D7;
  571.  
  572. Rectangle R1(x, y, 9'd10, 9'd2, x_init-11'd2, y_init+10'd24, D1);
  573. Rectangle R2(x, y, 9'd2, 9'd10, x_init-11'd12, y_init+10'd14, D2); 
  574. Rectangle R3(x, y, 9'd2, 9'd10, x_init-11'd12, y_init+10'd2, D3);      
  575. Rectangle R4(x, y, 9'd10, 9'd2, x_init-11'd2, y_init, D4);
  576. Rectangle R5(x, y, 9'd2, 9'd10, x_init, y_init+10'd2, D5);     
  577. Rectangle R6(x, y, 9'd2, 9'd10, x_init, y_init+10'd14, D6);
  578. Rectangle R7(x, y, 9'd10, 9'd2, x_init-11'd2, y_init+10'd12, D7);  
  579.  
  580.  
  581.    
  582.    assign oDisplay =    (code[0] && D1)||
  583.                         (code[1] && D2)||
  584.                         (code[2] && D3)||
  585.                         (code[3] && D4)||
  586.                         (code[4] && D5)||
  587.                         (code[5] && D6)||
  588.                         (code[6] && D7);   
  589. endmodule
  590.  
  591. module mux15 (input clk,
  592.               input [3:0] select,
  593.               output [13:0] out,
  594.               input [13:0] S1,
  595.               input [13:0] S2,
  596.               input [13:0] S3,
  597.               input [13:0] S4,
  598.               input [13:0] S5,
  599.               input [13:0] S6,
  600.               input [13:0] S7,
  601.               input [13:0] S8,
  602.               input [13:0] S9,
  603.               input [13:0] S10);
  604.     reg [13:0] reg_out;
  605.     assign out = reg_out;
  606.     always @(posedge clk) begin
  607.         case (select)
  608.             4'd1    : reg_out <= S1;
  609.             4'd2    : reg_out <= S2;
  610.             4'd3    : reg_out <= S3;
  611.             4'd4    : reg_out <= S4;
  612.             4'd5    : reg_out <= S5;
  613.             4'd6    : reg_out <= S6;
  614.             4'd7    : reg_out <= S7;
  615.             4'd8    : reg_out <= S8;
  616.             4'd9    : reg_out <= S9;
  617.             4'd10   : reg_out <= S10;
  618.             default : reg_out <= 14'd0;
  619.         endcase
  620.     end
  621. endmodule
  622.  
  623. module Substraction #(parameter width = 8)(input [width-1:0] a, input [width-1:0] b, input  sa, input sb, output [width-1:0] result, output sr);
  624.     assign result = (sa==sb) ?  ((a>b) ? a-b : b-a): a+b;
  625.     assign sr = (sa==sb) ? ((a>b) ? sa : ~sa): sa;
  626. endmodule
  627.  
  628. module Product(input [10:0] a, input [9:0] b, input  sa, input sb, output [20:0] result, output sr);
  629.     assign result = a*b;
  630.     assign sr = sa^sb;
  631. endmodule
  632.  
  633. module Droite(input [10:0] x, input [9:0] y, input [10:0] x1, input [9:0] y1, input [10:0] x2, input [9:0] y2, input [3:0] width, output oDisplay);
  634. logic sdx, sdy, sdpx, sdpy, sp1, sp2, sdp;
  635. logic [31:0] widthsqr;
  636. logic [10:0] dx, dpx;
  637. logic [9:0] dy, dpy;
  638. logic [20:0] p1, p2;
  639. logic [31:0] dp;
  640. Substraction #(11) Sub1 (x,x2,1'b0,1'b0,dx,sdx);
  641. Substraction #(10) Sub2 (y2,y1,1'b0,1'b0,dpy,sdpy);
  642. Substraction #(10) Sub3 (y,y2,1'b0,1'b0,dy,sdy);
  643. Substraction #(11) Sub4 (x2,x1,1'b0,1'b0,dpx,sdpx);
  644. Product Prod1 (dx,dpy,sdx,sdpy,p1,sp1);
  645. Product Prod2 (dpx,dy,sdpx,sdy,p2,sp2);
  646. Substraction #(32) Sub5 (p1,p2,sp1,sp2,dp,sdp);
  647. assign widthsqr = width*width;
  648. assign oDisplay = ((dp*dp) <= (widthsqr*(dpx*dpx+dpy*dpy))) && ((x2>x1) ? (x1<x+width && x<x2+width):(x2<x+width && x<x1+width))&& ((y2>y1) ? (y1<y+width && y<y2+width):(y2<y+width && y<y1+width));
  649. endmodule
  650.  
  651. module DisplayDigit(input [10:0] x, input [9:0] y, input [13:0] code, input [10:0] x_init, input [9:0] y_init, output oDisplay);
  652. logic D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14;
  653.  
  654.  
  655. Rectangle R1(x, y, 9'd20, 9'd4, x_init-11'd4, y_init+10'd48, D1);
  656. Rectangle R2(x, y, 9'd4, 9'd20, x_init-11'd24, y_init+10'd28, D2); 
  657. Rectangle R3(x, y, 9'd4, 9'd20, x_init-11'd24, y_init+10'd4, D3);      
  658. Rectangle R4(x, y, 9'd20, 9'd4, x_init-11'd4, y_init, D4);
  659. Rectangle R5(x, y, 9'd4, 9'd20, x_init, y_init+10'd4, D5);     
  660. Rectangle R6(x, y, 9'd4, 9'd20, x_init, y_init+10'd28, D6);
  661. Rectangle R7(x, y, 9'd8, 9'd4, x_init-11'd4, y_init+10'd24, D7);
  662. Rectangle R8(x, y, 9'd8, 9'd4, x_init-11'd16, y_init+10'd24, D8);
  663. Droite dr9(x, y, x_init-11'd6, y_init+10'd46, x_init-11'd10, y_init+10'd30, 4'd1, D9);
  664. Rectangle R10(x, y, 9'd4, 9'd20, x_init-11'd12, y_init+10'd28, D10);
  665. Droite dr11(x, y, x_init-11'd22, y_init+10'd46, x_init-11'd18, y_init+10'd30, 4'd1, D11);
  666. Droite dr12(x, y, x_init-11'd22, y_init+10'd6, x_init-11'd18, y_init+10'd22, 4'd1, D12);
  667. Rectangle R13(x, y, 9'd4, 9'd20, x_init-11'd12, y_init+10'd4, D13);
  668. Droite dr14(x, y, x_init-11'd6, y_init+10'd6, x_init-11'd10, y_init+10'd22, 4'd1, D14);
  669.    
  670.    assign oDisplay =    (code[0] && D1)||
  671.                         (code[1] && D2)||
  672.                         (code[2] && D3)||
  673.                         (code[3] && D4)||
  674.                         (code[4] && D5)||
  675.                         (code[5] && D6)||
  676.                         (code[6] && D7)||
  677.                         (code[7] && D8)||
  678.                         (code[8] && D9)||
  679.                         (code[9] && D10)||
  680.                         (code[10] && D11)||
  681.                         (code[11] && D12)||
  682.                         (code[12] && D13)||
  683.                         (code[13] && D14);
  684.                        
  685. endmodule
  686.  
  687. module Rectangle(input [10:0] x, input [9:0] y, input [8:0] width, input[8:0] height, input [10:0] x_init, input [9:0] y_init, output oDisplay);
  688. assign oDisplay =    (x<x_init) && (x>x_init-width) && (y>y_init) && (y<y_init+height);
  689. endmodule
  690.  
  691. module downclock (input wire input_clk, output reg output_clk);
  692.     reg [31:0] cnt = 31'b0;
  693.    
  694.     always @(posedge input_clk) begin
  695.         if (cnt < 31'd250000)
  696.             output_clk <= 1'b0;
  697.         else if (cnt < 31'd500000)
  698.             output_clk <= 1'b1;
  699.        
  700.         cnt = cnt + 1'b1;
  701.         if (cnt == 31'd500000)
  702.             cnt = 1'b0;
  703.     end
  704. endmodule
  705.  
  706.  
  707. module DisplayBigDigit(input [10:0] x, input [9:0] y, input [13:0] code, input [10:0] x_init, input [9:0] y_init, output oDisplay);
  708. logic D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14;
  709.  
  710. Rectangle R1(x, y, 9'd100, 9'd20, x_init-11'd20, y_init+10'd240, D1);
  711. Rectangle R2(x, y, 9'd20, 9'd100, x_init-11'd120, y_init+10'd140, D2); 
  712. Rectangle R3(x, y, 9'd20, 9'd100, x_init-11'd120, y_init+10'd20, D3);      
  713. Rectangle R4(x, y, 9'd100, 9'd20, x_init-11'd20, y_init, D4);
  714. Rectangle R5(x, y, 9'd20, 9'd100, x_init, y_init+10'd20, D5);      
  715. Rectangle R6(x, y, 9'd20, 9'd100, x_init, y_init+10'd140, D6);
  716. Rectangle R7(x, y, 9'd40, 9'd20, x_init-11'd20, y_init+10'd120, D7);
  717. Rectangle R8(x, y, 9'd40, 9'd20, x_init-11'd80, y_init+10'd120, D8);
  718. Droite dr9(x, y, x_init-11'd20, y_init+10'd240, x_init-11'd60, y_init+10'd140, 4'd10, D9);
  719. Rectangle R10(x, y, 9'd20, 9'd100, x_init-11'd60, y_init+10'd140, D10);
  720. Droite dr11(x, y, x_init-11'd120, y_init+10'd240, x_init-11'd80, y_init+10'd140, 4'd10, D11);
  721. Droite dr12(x, y, x_init-11'd120, y_init+10'd20, x_init-11'd80, y_init+10'd120, 4'd10, D12);
  722. Rectangle R13(x, y, 9'd20, 9'd100, x_init-11'd60, y_init+10'd20, D13);
  723. Droite dr14(x, y, x_init-11'd20, y_init+10'd20, x_init-11'd60, y_init+10'd120, 4'd10, D14);
  724.  
  725.    
  726.    assign oDisplay =    (code[0] && D1)||
  727.                         (code[1] && D2)||
  728.                         (code[2] && D3)||
  729.                         (code[3] && D4)||
  730.                         (code[4] && D5)||
  731.                         (code[5] && D6)||
  732.                         (code[6] && D7)||
  733.                         (code[7] && D8)||
  734.                         (code[8] && D9)||
  735.                         (code[9] && D10)||
  736.                         (code[10] && D11)||
  737.                         (code[11] && D12)||
  738.                         (code[12] && D13)||
  739.                         (code[13] && D14);
  740.                        
  741. endmodule
  742.  
  743. module Decode (input [5:0] let , output [13:0] message);
  744. always_comb
  745. case(let)
  746. 6'd0: message <= 14'b111111; //0
  747. 6'd1: message <= 14'b110; //1
  748. 6'd2: message <= 14'b11011011; //2
  749. 6'd3: message <= 14'b11001111; //3
  750. 6'd4: message <= 14'b11100110; //4
  751. 6'd5: message <= 14'b11101101; //5
  752. 6'd6: message <= 14'b11111101; //6
  753. 6'd7: message <= 14'b111; //7
  754. 6'd8: message <= 14'b11111111; //8
  755. 6'd9: message <= 14'b11101111; //9
  756. 6'd10: message <= 14'b11110111; //A
  757. 6'd11: message <= 14'b11111100; //B
  758. 6'd12: message <= 14'b111001; //C
  759. 6'd13: message <= 14'b11011110; //D
  760. 6'd14: message <= 14'b11111001; //E
  761. 6'd15: message <= 14'b1110001; //F
  762. 6'd16: message <= 14'b10111101; //G
  763. 6'd17: message <= 14'b11110110; //H
  764. 6'd18: message <= 14'b1001000000000; //I
  765. 6'd19: message <= 14'b11110; //J
  766. 6'd20: message <= 14'b110001110000; //K
  767. 6'd21: message <= 14'b111000; //L
  768. 6'd22: message <= 14'b10100110110; //M
  769. 6'd23: message <= 14'b100100110110; //N
  770. 6'd24: message <= 14'b111111; //O
  771. 6'd25: message <= 14'b11110011; //P
  772. 6'd26: message <= 14'b100000111111; //Q
  773. 6'd27: message <= 14'b100011110011; //R
  774. 6'd28: message <= 14'b11101101; //S
  775. 6'd29: message <= 14'b1001000000001; //T
  776. 6'd30: message <= 14'b111110; //U
  777. 6'd31: message <= 14'b10010000110000; //V
  778. 6'd32: message <= 14'b10100000110110; //W
  779. 6'd33: message <= 14'b10110100000000; //X
  780. 6'd34: message <= 14'b1010100000000; //Y
  781. 6'd35: message <= 14'b10010000001001; //Z
  782. 6'd36: message <= 14'b1001011000000; //+
  783. 6'd37: message <= 14'b11000000; //-
  784. default: message <= 14'b0; // nothing
  785. endcase
  786. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement