`timescale 1ns / 1ps

module r_top(
		input wire clk, rst,
		input wire ps2_data, ps2_clk,
		output wire [7:0] led,
		output [5:0] PWM_CH,
		
		output wire [7:0]	seg,
	   output wire	[3:0]	dig,
		output wire [4:0]	col,
		
		inout [7:0] sram_databus,
		output [16:0] sram_addr,
		output sram_csn, sram_oen, sram_wen		
    );

wire ps2_de_ready_in, r_en;
wire clk_d; //a ps2_decoder kimeneti clk-ja, ami 1, ha van érvényes ada, különben 0
wire torles_vege, tarolas_eng, kiolv_vege;

wire [1:0] start_stop; //első billnyomás

wire [2:0] folyamat;

wire [3:0] digit1, digit2, digit3, digit4;

wire [7:0] ps2_de_data_in;
wire [7:0] bill_kod; //a ps2_decoder kimenete, a leütött billentyű kódja
wire [7:0] mem_counter, kiolvasott_kod, beirando_kod, w_address, r_address;

wire [15:0] kit1_b, kit2_b, kit3_b, kit4_b, kit5_b, kit6_b;
wire [15:0] kit1_mtt, kit2_mtt, kit3_mtt, kit4_mtt, kit5_mtt, kit6_mtt;
wire [15:0] kit1_mki, kit2_mki, kit3_mki, kit4_mki, kit5_mki, kit6_mki;
wire [15:0] kt1, kt2, kt3, kt4, kt5, kt6;

ps2_receive ps2_r(.clk(clk), .rst(rst), .ps2_data(ps2_data), .ps2_clk(ps2_clk), .data_out(ps2_de_data_in), .ready_out(ps2_de_ready_in));

ps2_decoder ps2_de(.clk(clk), .rst(rst), .data_ready(ps2_de_ready_in), .data(ps2_de_data_in), .bill_kod(bill_kod), .clk_d(clk_d) , .start_stop(start_stop));

folyamat_valaszto f(.clk(clk), .rst(rst), .clk_d(clk_d), .bill_kod(bill_kod), .start_stop(start_stop), 
						  .torles_vege(torles_vege), .kiolv_vege(kiolv_vege), .folyamat(folyamat));

billrol_iranyitott bi(.clk(clk), .rst(rst), .bill_kod(bill_kod), .folyamat(folyamat), .clk_d(clk_d), 
							 .kit1_mtt(kit1_mtt), .kit2_mtt(kit2_mtt), .kit3_mtt(kit3_mtt), .kit4_mtt(kit4_mtt), .kit5_mtt(kit5_mtt), .kit6_mtt(kit6_mtt),
							 .kit1_mki(kit1_mki), .kit2_mki(kit2_mki), .kit3_mki(kit3_mki), .kit4_mki(kit4_mki), .kit5_mki(kit5_mki), .kit6_mki(kit6_mki),
						    .kit1_b(kit1_b), .kit2_b(kit2_b), .kit3_b(kit3_b), .kit4_b(kit4_b), .kit5_b(kit5_b), .kit6_b(kit6_b));

mem_tarol_torol mtt(.clk(clk), .clk_d(clk_d), .bill_kod(bill_kod), .folyamat(folyamat), .torles_vege(torles_vege),
							.beirando_kod(beirando_kod), .tarolas_eng(tarolas_eng), .mem_counter(mem_counter), 
							.kit1_mtt(kit1_mtt), .kit2_mtt(kit2_mtt), .kit3_mtt(kit3_mtt), .kit4_mtt(kit4_mtt), .kit5_mtt(kit5_mtt), .kit6_mtt(kit6_mtt));

memoria m(.clk(clk), .rst(rst), .folyamat(folyamat), .tarolas_eng(tarolas_eng), .beirando_kod(beirando_kod),
				.kiolvasott_kod(kiolvasott_kod), .sram_databus(sram_databus), .sram_addr(sram_addr), .r_address(r_address),
				.sram_csn(sram_csn), .sram_oen(sram_oen), .sram_wen(sram_wen), .kiolv_vege(kiolv_vege), .r_en(r_en),
				.torles_vege(torles_vege), .w_address(w_address));

mem_kiolvas mki(.clk(clk), .r_en(r_en), .rst(rst), .kiolvasott_kod(kiolvasott_kod), .kiolv_vege(kiolv_vege),
					.kit1_mki(kit1_mki), .kit2_mki(kit2_mki), .kit3_mki(kit3_mki), .kit4_mki(kit4_mki), .kit5_mki(kit5_mki), .kit6_mki(kit6_mki));


kitoltes_to_pwm k_to_pwm(.folyamat(folyamat), 
								 .kit1_b(kit1_b), .kit2_b(kit2_b), .kit3_b(kit3_b), .kit4_b(kit4_b), .kit5_b(kit5_b), .kit6_b(kit6_b),
								 .kit1_mtt(kit1_mtt), .kit2_mtt(kit2_mtt), .kit3_mtt(kit3_mtt), .kit4_mtt(kit4_mtt), .kit5_mtt(kit5_mtt), .kit6_mtt(kit6_mtt),
								 .kit1_mki(kit1_mki), .kit2_mki(kit2_mki), .kit3_mki(kit3_mki), .kit4_mki(kit4_mki), .kit5_mki(kit5_mki), .kit6_mki(kit6_mki),
								 .kit1(kt1), .kit2(kt2), .kit3(kt3), .kit4(kt4), .kit5(kt5), .kit6(kt6));

pwm_generator servo1(.clk(clk), .kitoltesi_tenyezo(kt1), .PWM_CH(PWM_CH[0]));
pwm_generator servo2(.clk(clk), .kitoltesi_tenyezo(kt2), .PWM_CH(PWM_CH[1]));
pwm_generator servo3(.clk(clk), .kitoltesi_tenyezo(kt3), .PWM_CH(PWM_CH[2]));
pwm_generator servo4(.clk(clk), .kitoltesi_tenyezo(kt4), .PWM_CH(PWM_CH[3]));
pwm_generator servo5(.clk(clk), .kitoltesi_tenyezo(kt5), .PWM_CH(PWM_CH[4]));
pwm_generator servo6(.clk(clk), .kitoltesi_tenyezo(kt6), .PWM_CH(PWM_CH[5]));


digit d(.clk(clk), .rst(rst), .folyamat(folyamat), .w_address(w_address) , .r_address(r_address),.digit1(digit1), .digit2(digit2), .digit3(digit3), .digit4(digit4));

kijelzo k(.clk(clk), .rst(rst), .folyamat(folyamat),  
			 .digit1(digit1), .digit2(digit2), .digit3(digit3), .digit4(digit4), .seg(seg), .dig(dig), .col(col));


assign led = (folyamat == 3'd4) ? kiolvasott_kod : ((folyamat == 3'd3) ? mem_counter : ((folyamat == 3'd2) ?  /*{2'b00,status_tp}*/8'd0 : bill_kod));


endmodule 

