`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 19.06.2017 18:05:00
// Design Name: 
// Module Name: lab_6
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module lab_6(
	input CLK100MHZ,
	input SW0,
	input PS2_CLK,
	input PS2_DATA,
	input CPU_RESETN,
	input BTNC,
	output VGA_HS,
	output VGA_VS,
	output [3:0] VGA_R,
	output [3:0] VGA_G,
	output [3:0] VGA_B,
	output [5:0] LED,
	output JA1
	);
	
	localparam CUADRILLA_XI = 		212;
	localparam CUADRILLA_XF = 		812;
	
	localparam CUADRILLA_YI = 		184;
	localparam CUADRILLA_YF = 		584;
	
	wire [10:0]vc_visible,hc_visible;
	wire CLK82MHZ;
	
	clk_wiz_0 inst(
		// Clock out ports  
		.clk_out1(CLK82MHZ),
		.clk_out2(clock100),
		// Status and control signals               
		.reset(1'b0), 
		//.locked(locked),
		// Clock in ports
		.clk_in1(CLK100MHZ)
		
		);
		
	//driver_vga_640x480 m_driver(CLK82MHZ, VGA_HS, VGA_VS,hc_visible,vc_visible);
	driver_vga_1024x768 m_driver(CLK82MHZ, VGA_HS, VGA_VS, hc_visible, vc_visible);
	kbd_ms m_kd(CLK82MHZ, 1'b0, PS2_DATA, PS2_CLK, data, data_type, kbs_tot, parity_error);
	
	wire kbs_tot;
	wire [7:0] data;
	wire [2:0] data_type;
	wire parity_error;
	
	wire [10:0]hc_template, vc_template;
	wire [2:0]matrix_x;
	wire [1:0]matrix_y;
	wire lines;
	wire [63:0] digitos;
	wire [2:0] control;
	wire modo;
	wire [3:0] flags;
	wire [11:0] overflow,carry,zero,negative;
	wire clock100;
	
	template_6x4_600x400 template_1(CLK82MHZ, hc_visible, vc_visible, matrix_x, matrix_y, lines);
	
	reg [11:0]VGA_COLOR;
	
	wire in_sq, dr;
//	hello_world_text_square m_hw(CLK82MHZ, 1'b0, hc_visible, vc_visible, in_sq, dr);
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	localparam ARRIBA=5'd19;
	localparam ABAJO=5'd20;
	localparam IZQUIERDA=5'd17;
	localparam DERECHA=5'd18;
	localparam SUMA=5'd21;
	localparam RESTA=5'd22;
	localparam MUL=5'd23;
	localparam AND=5'd24;
	localparam OR=5'd25;
	
	wire in_sq_0,in_sq_1,in_sq_2,in_sq_3,in_sq_4,in_sq_5,in_sq_6,in_sq_7,in_sq_8,in_sq_9;
	wire in_sq_a,in_sq_b,in_sq_c,in_sq_d,in_sq_e,in_sq_f,in_sq_cuadro;
	wire in_sq_mas,in_sq_menos,in_sq_mul,in_sq_and,in_sq_or,in_sq_E1,in_sq_E2,in_sq_E3,in_sq_X,in_sq_C,in_sq_hex,in_sq_dec;
	wire in_sq_cuadro0,in_sq_cuadro1,in_sq_cuadro2,in_sq_cuadro3;
	wire in_ch_0,in_ch_1,in_ch_2,in_ch_3,in_ch_4,in_ch_5,in_ch_6,in_ch_7,in_ch_8,in_ch_9;
    wire in_ch_a,in_ch_b,in_ch_c,in_ch_d,in_ch_e,in_ch_f,in_ch_cuadro;
    wire in_ch_mas,in_ch_menos,in_ch_mul,in_ch_and,in_ch_or,in_ch_E1,in_ch_E2,in_ch_E3,in_ch_X,in_ch_C,in_ch_hex,in_ch_dec;
    wire [25:0] SQ;
    wire [25:0] CHAR;
    wire [4:0] tecla;
    wire reset;
    wire [7:0] valor_cursor;
    wire [63:0] resultado;
    wire [7:0] tx_data;
    wire trigger;
    wire tx_start;
    wire [15:0] RES;
    wire clear;
    
    reg [11:0] color_hex; 
    reg [11:0] color_dec;
    reg [2:0] visor_x,visor_x_next;
    reg [1:0] visor_y,visor_y_next;
    
    assign reset=~CPU_RESETN;
    
    assign SQ={in_sq_0,in_sq_1,in_sq_2,in_sq_3,in_sq_4,in_sq_5,in_sq_6,in_sq_7,in_sq_8,in_sq_9,
               in_sq_a,in_sq_b,in_sq_c,in_sq_d,in_sq_e,in_sq_f,in_sq_mas,in_sq_menos,in_sq_mul,in_sq_and,in_sq_or,in_sq_E1,in_sq_E2,in_sq_E3,in_sq_X,in_sq_C};
               
    assign CHAR={   in_ch_0,in_ch_1,in_ch_2,in_ch_3,in_ch_4,in_ch_5,in_ch_6,in_ch_7,in_ch_8,in_ch_9,  
                     in_ch_a,in_ch_b,in_ch_c,in_ch_d,in_ch_e,in_ch_f,                                
                     in_ch_mas,in_ch_menos,in_ch_mul,in_ch_and,in_ch_or,in_ch_E1,in_ch_E2,in_ch_E3,in_ch_X,in_ch_C};                   
	
	assign LED[5:2]=flags;
	
	show_one_char
	#( .MENU_X_LOCATION (11'd253),
	   .MENU_Y_LOCATION (11'd222)
	) show_0
	(  
	   .clk(CLK82MHZ),
	   .rst(1'b0),
	   .hc_visible(hc_visible),
	   .vc_visible(vc_visible),
	   .the_char({8'd0}),
	   .in_square(in_sq_0),
	   .in_character(in_ch_0)
	);
	
	show_one_char
        #( .MENU_X_LOCATION (11'd353),
           .MENU_Y_LOCATION (11'd222)
        ) show_1
        (  
           .clk(CLK82MHZ),
           .rst(1'b0),
           .hc_visible(hc_visible),
           .vc_visible(vc_visible),
           .the_char({8'd1}),
           .in_square(in_sq_1),
           .in_character(in_ch_1)
        );
      show_one_char
            #( .MENU_X_LOCATION (11'd453),
               .MENU_Y_LOCATION (11'd222)
            ) show_2
            (  
               .clk(CLK82MHZ),
               .rst(1'b0),
               .hc_visible(hc_visible),
               .vc_visible(vc_visible),
               .the_char({8'd2}),
               .in_square(in_sq_2),
               .in_character(in_ch_2)
            );
      show_one_char
                #( .MENU_X_LOCATION (11'd553),
                   .MENU_Y_LOCATION (11'd222)
                ) show_3
                (  
                   .clk(CLK82MHZ),
                   .rst(1'b0),
                   .hc_visible(hc_visible),
                   .vc_visible(vc_visible),
                   .the_char({8'd3}),
                   .in_square(in_sq_3),
                   .in_character(in_ch_3)
                );
                show_one_char
                    #( .MENU_X_LOCATION (11'd253),
                       .MENU_Y_LOCATION (11'd322)
                    ) show_4
                    (  
                       .clk(CLK82MHZ),
                       .rst(1'b0),
                       .hc_visible(hc_visible),
                       .vc_visible(vc_visible),
                       .the_char({8'd4}),
                       .in_square(in_sq_4),
                       .in_character(in_ch_4)
                    );
                    show_one_char
                        #( .MENU_X_LOCATION (11'd353),
                           .MENU_Y_LOCATION (11'd322)
                        ) show_5
                        (  
                           .clk(CLK82MHZ),
                           .rst(1'b0),
                           .hc_visible(hc_visible),
                           .vc_visible(vc_visible),
                           .the_char({8'd5}),
                           .in_square(in_sq_5),
                           .in_character(in_ch_5)
                        );
                        show_one_char
                            #( .MENU_X_LOCATION (11'd453),
                               .MENU_Y_LOCATION (11'd322)
                            ) show_6
                            (  
                               .clk(CLK82MHZ),
                               .rst(1'b0),
                               .hc_visible(hc_visible),
                               .vc_visible(vc_visible),
                               .the_char({8'd6}),
                               .in_square(in_sq_6),
                               .in_character(in_ch_6)
                            );
                            show_one_char
                                #( .MENU_X_LOCATION (11'd553),
                                   .MENU_Y_LOCATION (11'd322)
                                ) show_7
                                (  
                                   .clk(CLK82MHZ),
                                   .rst(1'b0),
                                   .hc_visible(hc_visible),
                                   .vc_visible(vc_visible),
                                   .the_char({8'd7}),
                                   .in_square(in_sq_7),
                                   .in_character(in_ch_7)
                                );
                                show_one_char
                                    #( .MENU_X_LOCATION (11'd253),
                                       .MENU_Y_LOCATION (11'd422)
                                    ) show_8
                                    (  
                                       .clk(CLK82MHZ),
                                       .rst(1'b0),
                                       .hc_visible(hc_visible),
                                       .vc_visible(vc_visible),
                                       .the_char({8'd8}),
                                       .in_square(in_sq_8),
                                       .in_character(in_ch_8)
                                    );
                                    show_one_char
                                        #( .MENU_X_LOCATION (11'd353),
                                           .MENU_Y_LOCATION (11'd422)
                                        ) show_9
                                        (  
                                           .clk(CLK82MHZ),
                                           .rst(1'b0),
                                           .hc_visible(hc_visible),
                                           .vc_visible(vc_visible),
                                           .the_char({8'd9}),
                                           .in_square(in_sq_9),
                                           .in_character(in_ch_9)
                                        );
                                        show_one_char
                                            #( .MENU_X_LOCATION (11'd453),
                                               .MENU_Y_LOCATION (11'd422)
                                            ) show_a
                                            (  
                                               .clk(CLK82MHZ),
                                               .rst(1'b0),
                                               .hc_visible(hc_visible),
                                               .vc_visible(vc_visible),
                                               .the_char(8'd10),
                                               .in_square(in_sq_a),
                                               .in_character(in_ch_a)
                                            );
                                            show_one_char
                                                #( .MENU_X_LOCATION (11'd553),
                                                   .MENU_Y_LOCATION (11'd422)
                                                ) show_b
                                                (  
                                                   .clk(CLK82MHZ),
                                                   .rst(1'b0),
                                                   .hc_visible(hc_visible),
                                                   .vc_visible(vc_visible),
                                                   .the_char(8'd11),
                                                   .in_square(in_sq_b),
                                                   .in_character(in_ch_b)
                                                );
                                                show_one_char
                                                    #( .MENU_X_LOCATION (11'd253),
                                                       .MENU_Y_LOCATION (11'd522)
                                                    ) show_c
                                                    (  
                                                       .clk(CLK82MHZ),
                                                       .rst(1'b0),
                                                       .hc_visible(hc_visible),
                                                       .vc_visible(vc_visible),
                                                       .the_char(8'd12),
                                                       .in_square(in_sq_c),
                                                       .in_character(in_ch_c)
                                                    );
                                                    show_one_char
                                                        #( .MENU_X_LOCATION (11'd353),
                                                           .MENU_Y_LOCATION (11'd522)
                                                        ) show_d
                                                        (  
                                                           .clk(CLK82MHZ),
                                                           .rst(1'b0),
                                                           .hc_visible(hc_visible),
                                                           .vc_visible(vc_visible),
                                                           .the_char(8'd13),
                                                           .in_square(in_sq_d),
                                                           .in_character(in_ch_d)
                                                        );
                                                        show_one_char
                                                            #( .MENU_X_LOCATION (11'd453),
                                                               .MENU_Y_LOCATION (11'd522)
                                                            ) show_e
                                                            (  
                                                               .clk(CLK82MHZ),
                                                               .rst(1'b0),
                                                               .hc_visible(hc_visible),
                                                               .vc_visible(vc_visible),
                                                               .the_char(8'd14),
                                                               .in_square(in_sq_e),
                                                               .in_character(in_ch_e)
                                                            );
                                                            show_one_char
                                                                #( .MENU_X_LOCATION (11'd553),
                                                                   .MENU_Y_LOCATION (11'd522)
                                                                ) show_f
                                                                (  
                                                                   .clk(CLK82MHZ),
                                                                   .rst(1'b0),
                                                                   .hc_visible(hc_visible),
                                                                   .vc_visible(vc_visible),
                                                                   .the_char(8'd15),
                                                                   .in_square(in_sq_f),
                                                                   .in_character(in_ch_f)
                                                                );
                                                                show_one_char
                                                                    #( .MENU_X_LOCATION (11'd653),
                                                                       .MENU_Y_LOCATION (11'd222)
                                                                    ) show_mas
                                                                    (  
                                                                       .clk(CLK82MHZ),
                                                                       .rst(1'b0),
                                                                       .hc_visible(hc_visible),
                                                                       .vc_visible(vc_visible),
                                                                       .the_char("+"),
                                                                       .in_square(in_sq_mas),
                                                                       .in_character(in_ch_mas)
                                                                    );
                                                                    show_one_char
                                                                        #( .MENU_X_LOCATION (11'd753),
                                                                           .MENU_Y_LOCATION (11'd222)
                                                                        ) show_menos
                                                                        (  
                                                                           .clk(CLK82MHZ),
                                                                           .rst(1'b0),
                                                                           .hc_visible(hc_visible),
                                                                           .vc_visible(vc_visible),
                                                                           .the_char("-"),
                                                                           .in_square(in_sq_menos),
                                                                           .in_character(in_ch_menos)
                                                                        );
                                                                        show_one_char
                                                                            #( .MENU_X_LOCATION (11'd653),
                                                                               .MENU_Y_LOCATION (11'd322)
                                                                            ) show_mul
                                                                            (  
                                                                               .clk(CLK82MHZ),
                                                                               .rst(1'b0),
                                                                               .hc_visible(hc_visible),
                                                                               .vc_visible(vc_visible),
                                                                               .the_char("*"),
                                                                               .in_square(in_sq_mul),
                                                                               .in_character(in_ch_mul)
                                                                            );
                                                                            show_one_char
                                                                                #( .MENU_X_LOCATION (11'd653),
                                                                                   .MENU_Y_LOCATION (11'd422)
                                                                                ) show_and
                                                                                (  
                                                                                   .clk(CLK82MHZ),
                                                                                   .rst(1'b0),
                                                                                   .hc_visible(hc_visible),
                                                                                   .vc_visible(vc_visible),
                                                                                   .the_char("&"),
                                                                                   .in_square(in_sq_and),
                                                                                   .in_character(in_ch_and)
                                                                                );
       show_one_char
           #( .MENU_X_LOCATION (11'd753),
              .MENU_Y_LOCATION (11'd322)
           ) show_or
           (  
              .clk(CLK82MHZ),
              .rst(1'b0),
              .hc_visible(hc_visible),
              .vc_visible(vc_visible),
              .the_char("|"),
              .in_square(in_sq_or),
              .in_character(in_ch_or)
           );
           
           show_one_char
                      #( .MENU_X_LOCATION (11'd610),
                         .MENU_Y_LOCATION (11'd522)
                      ) show_E1
                      (  
                         .clk(CLK82MHZ),
                         .rst(1'b0),
                         .hc_visible(hc_visible),
                         .vc_visible(vc_visible),
                         .the_char(8'd201),
                         .in_square(in_sq_E1),
                         .in_character(in_ch_E1)
                      );
                      
          show_one_char
                                 #( .MENU_X_LOCATION (11'd640),
                                    .MENU_Y_LOCATION (11'd522)
                                 ) show_X
                                 (  
                                    .clk(CLK82MHZ),
                                    .rst(1'b0),
                                    .hc_visible(hc_visible),
                                    .vc_visible(vc_visible),
                                    .the_char(8'd220),
                                    .in_square(in_sq_X),
                                    .in_character(in_ch_X)
                                 );
           show_one_char
                                            #( .MENU_X_LOCATION (11'd670),
                                               .MENU_Y_LOCATION (11'd522)
                                            ) show_E2
                                            (  
                                               .clk(CLK82MHZ),
                                               .rst(1'b0),
                                               .hc_visible(hc_visible),
                                               .vc_visible(vc_visible),
                                               .the_char(8'd201),
                                               .in_square(in_sq_E2),
                                               .in_character(in_ch_E2)
                                            );
          show_one_char
                                                       #( .MENU_X_LOCATION (11'd720),
                                                          .MENU_Y_LOCATION (11'd422)
                                                       ) show_C
                                                       (  
                                                          .clk(CLK82MHZ),
                                                          .rst(1'b0),
                                                          .hc_visible(hc_visible),
                                                          .vc_visible(vc_visible),
                                                          .the_char(8'd199),
                                                          .in_square(in_sq_C),
                                                          .in_character(in_ch_C)
                                                       );
           show_one_char
                                                                  #( .MENU_X_LOCATION (11'd770),
                                                                     .MENU_Y_LOCATION (11'd422)
                                                                  ) show_E3
                                                                  (  
                                                                     .clk(CLK82MHZ),
                                                                     .rst(1'b0),
                                                                     .hc_visible(hc_visible),
                                                                     .vc_visible(vc_visible),
                                                                     .the_char(8'd201),
                                                                     .in_square(in_sq_E3),
                                                                     .in_character(in_ch_E3)
                                                                  );
             
                                                                                    
   show_one_line
   #(.MENU_X_LOCATION (11'd212),
     .MENU_Y_LOCATION (11'd112) 
    ) show_cuadro
   (
   .clk(CLK82MHZ),
   .rst(1'b0),
   .hc_visible(hc_visible),
   .vc_visible(vc_visible),
   .the_line({"  ",resultado}),
   .in_square(in_sq_cuadro),
   .in_character(in_ch_cuadro)
   );
   
    show_one_line2
     #(.MENU_X_LOCATION (11'd632),
       .MENU_Y_LOCATION (11'd112) 
      ) show_hex
     (
     .clk(CLK82MHZ),
     .rst(1'b0),
     .hc_visible(hc_visible),
     .vc_visible(vc_visible),
     .the_line({8'd204,8'd201,8'd220}),
     .in_square(in_sq_hex),
     .in_character(in_ch_hex)
     );
     
     show_one_line2
          #(.MENU_X_LOCATION (11'd632),
            .MENU_Y_LOCATION (11'd92) 
           ) show_dec
          (
          .clk(CLK82MHZ),
          .rst(1'b0),
          .hc_visible(hc_visible),
          .vc_visible(vc_visible),
          .the_line({8'd200,8'd201,8'd199}),
          .in_square(in_sq_dec),
          .in_character(in_ch_dec)
          );
     
     show_one_char2
    #( .MENU_X_LOCATION (11'd696),
       .MENU_Y_LOCATION (11'd112)
    ) show_cuadro0
    (  
       .clk(CLK82MHZ),
       .rst(1'b0),
       .hc_visible(hc_visible),
       .vc_visible(vc_visible),
       .the_char(" "),
       .in_square(in_sq_cuadro0),
       .in_character()
    );
    show_one_char2
    #( .MENU_X_LOCATION (11'd696),
       .MENU_Y_LOCATION (11'd127)
    ) show_cuadro1
    (  
       .clk(CLK82MHZ),
       .rst(1'b0),
       .hc_visible(hc_visible),
       .vc_visible(vc_visible),
       .the_char(" "),
       .in_square(in_sq_cuadro1),
       .in_character()
    );
   show_one_char2
    #( .MENU_X_LOCATION (11'd696),
       .MENU_Y_LOCATION (11'd142)
    ) show_cuadro2
    (  
       .clk(CLK82MHZ),
       .rst(1'b0),
       .hc_visible(hc_visible),
       .vc_visible(vc_visible),
       .the_char(" "),
       .in_square(in_sq_cuadro2),
       .in_character()
    );
  show_one_char2
      #( .MENU_X_LOCATION (11'd696),
         .MENU_Y_LOCATION (11'd157)
      ) show_cuadro3
      (  
         .clk(CLK82MHZ),
         .rst(1'b0),
         .hc_visible(hc_visible),
         .vc_visible(vc_visible),
         .the_char(" "),
         .in_square(in_sq_cuadro3),
         .in_character()
      );
    
   mostrar_numeros_pantalla num_pant(
   .clock(CLK82MHZ),
   .reset(reset),
   .visor_x(visor_x),
   .visor_y(visor_y),
   .ps2_data(PS2_DATA),
   .ps2_clk(PS2_CLK),
   .digitos(digitos),
   .valor_cursor(valor_cursor),
   .modo(modo),
   .tecla(tecla),
   .clear(clear)
   );
    
   Calculadora_monitor calc(
   .clock(CLK82MHZ),
   .reset(reset),
   .cifras(digitos),
   .kbs_tot(kbs_tot),
   .valor_cursor(valor_cursor),
   .data_type(data_type),
   .control(control),
   .resultado({resultado}),
   .flags(flags),
   .leds(LED[1:0]),
   .switch(SW0),
   .modo(modo),
   .RES(RES),
   .clear(clear)
   );
    
     teclado tec(
     .ps2_data(data),
     .val(tecla),
     .control(control),
     .leds()
     );
     
     flags_monitor(
     .flag(flags),
     .overflow(overflow),
     .carry(carry),
     .zero(zero),
     .negative(negative)
     );
     
     uart_basic uart_basic_inst (
                     .clk(clock100),
                     .reset(reset),
                     .rx(),
                     .rx_data(),
                     .rx_ready(),
                     .tx(JA1),
                     .tx_start(tx_start),
                     .tx_data(tx_data),
                     .tx_busy()
                 );
                 
         TX_control_slave TX_control_slave_inst(
                         .clock (clock100),             
                         .reset (reset),             
                         .trigger (trigger),           
                         .dataIn16 (RES),   
                         .tx_data (tx_data),
                         .tx_start (tx_start),     
                         .busy ()          
                     );
        
        PushButton_Debouncer BD1(
                               .clk (CLK82MHZ),
                               .PB (BTNC),
                               .PB_state (),
                               .PB_down (trigger),
                               .PB_up ()
                               );
                     

     
     always @(*)begin
     
     end
     
     always @(*)begin
     visor_x_next=visor_x;
     visor_y_next = visor_y;
            case({kbs_tot,tecla,data_type})
            {1'b1,ARRIBA,3'd1}:     visor_y_next=visor_y - 2'd1;
            {1'b1,ABAJO,3'd1}:      visor_y_next=visor_y + 2'd1;
            {1'b1,IZQUIERDA,3'd1}: begin
                            if(visor_x==3'b0)
                                visor_x_next=3'd5;
                            else
                                visor_x_next=visor_x-3'd1;
                        end
            {1'b1,DERECHA,3'd1}: begin
                            if(visor_x==3'd5)
                                 visor_x_next=3'b0;
                            else
                                 visor_x_next=visor_x + 3'd1;
            end 
            {1'b1,SUMA,3'd1}:begin
                            visor_x_next=3'd4;
                            visor_y_next=2'd0;
            end             
            {1'b1,RESTA,3'd1}: begin
                            visor_x_next=3'd5;
                            visor_y_next=2'd0;
            
            end
            {1'b1,MUL,3'd1}:begin
                            visor_x_next=3'd4;
                            visor_y_next=2'd1;
            end
            {1'b1,AND,3'd1}:begin
                            visor_x_next=3'd4;
                            visor_y_next=2'd2;
            end
            {1'b1,OR,3'd1}:begin
                            visor_x_next=3'd5;
                            visor_y_next=2'd1;
            end
            {1'b1,5'd0,3'd1}:begin
                            visor_x_next=3'd0;
                            visor_y_next=3'd0;
            end
            {1'b1,5'd1,3'd1}:begin
                            visor_x_next=3'd1; 
                            visor_y_next=3'd0;      
            end
            {1'b1,5'd2,3'd1}:begin
                            visor_x_next=3'd2;
                            visor_y_next=3'd0;
                            
            end
            {1'b1,5'd3,3'd1}:begin
                            visor_x_next=3'd3;
                            visor_y_next=3'd0;
                                            
            end
            {1'b1,5'd4,3'd1}:begin
                            visor_x_next=3'd0;
                            visor_y_next=3'd1;
            
            end
            {1'b1,5'd5,3'd1}:begin
                            visor_x_next=3'd1;
                            visor_y_next=3'd1;
                            
            end
            {1'b1,5'd6,3'd1}:begin
                            visor_x_next=3'd2;
                            visor_y_next=3'd1;
            end
            {1'b1,5'd7,3'd1}:begin
                            visor_x_next=3'd3;
                            visor_y_next=3'd1;
            
            end
            {1'b1,5'd8,3'd1}:begin
                            visor_x_next=3'd0;
                            visor_y_next=3'd2;
            
            end
            {1'b1,5'd9,3'd1}:begin
                            visor_x_next=3'd1;
                            visor_y_next=3'd2;
            
            end
            {1'b1,5'd10,3'd1}:begin
                            visor_x_next=3'd2;
                            visor_y_next=3'd2;
            
            end
            {1'b1,5'd11,3'd1}:begin
                            visor_x_next=3'd3;
                            visor_y_next=3'd2;
            end
            {1'b1,5'd12,3'd1}:begin
                            visor_x_next=3'd0;
                            visor_y_next=3'd3;
            
            end
            {1'b1,5'd13,3'd1}:begin
                            visor_x_next=3'd1;
                            visor_y_next=3'd3;
            
            end
            {1'b1,5'd14,3'd1}:begin
                            visor_x_next=3'd2;
                            visor_y_next=3'd3;
            
            end
            {1'b1,5'd15,3'd1}:begin
                            visor_x_next=3'd3;
                            visor_y_next=3'd3;
            end
                     
            endcase
            
     end
     
     
     always @(*) begin
            if(modo==1'b1) begin
                    color_hex=12'h000;
                    color_dec=12'hF00;
            end
            else begin
                    color_hex=12'hF00;
                    color_dec=12'h000;
            end
            
     end
     
     always @(posedge CLK82MHZ) begin
                visor_x <= visor_x_next;
                visor_y <= visor_y_next;
            
                
     end
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	always@(*)
		if((hc_visible != 0) && (vc_visible != 0))
		begin
			if(dr == 1'b1) begin
				VGA_COLOR = {12'h555}; //Fondo cuadro hello world
		    end
			else if (in_sq == 1'b1) begin
				VGA_COLOR = {12'hFFF}; //Letras hello world
			end
			else if((hc_visible > CUADRILLA_XI) && (hc_visible <= CUADRILLA_XF) && (vc_visible > CUADRILLA_YI) && (vc_visible <= CUADRILLA_YF)) begin
				if((lines==1'b1) | (CHAR !=26'b0) )
					VGA_COLOR = {12'h000}; //Color lineas y caracteres
				else if ((visor_x==matrix_x)&(visor_y==matrix_y))
				    VGA_COLOR = {4'b1111,4'b0,4'b0}; //Color visor
				else
					VGA_COLOR = {4'hC, {2'b0, matrix_x} + {2'b00, matrix_y}, 4'hF}; //Fondo teclas calcuadora
		end else if (in_ch_cuadro ==1'b1) begin
			     VGA_COLOR = {4'h9,4'hF,4'hF};
			end
			else if (in_sq_cuadro == 1'b1) begin
                 VGA_COLOR ={12'h94F};
            end
            else if (in_ch_hex)
                 VGA_COLOR = color_hex;
            else if (in_ch_dec)
                 VGA_COLOR = color_dec;
            else if (in_sq_cuadro0)
                 VGA_COLOR = overflow;
            else if (in_sq_cuadro1)
                 VGA_COLOR = carry;
            else if (in_sq_cuadro2)   
                 VGA_COLOR = zero;
            else if (in_sq_cuadro3)
                 VGA_COLOR = negative;
                 
			else begin
				VGA_COLOR = {12'hCAF}; //Color Fondo pantallaC
		    end
		end
		else
			VGA_COLOR = {12'd0};

	assign {VGA_R, VGA_G, VGA_B} = VGA_COLOR;
	
	
	
endmodule
