`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////////
module mem_tarol_torol(
		input wire clk, 
		input wire clk_d,
		input wire [7:0] bill_kod,
		input wire [2:0] folyamat,
		input wire torles_vege,
		
		output reg [7:0] beirando_kod = 8'd0,
		
		output reg tarolas_eng = 0,
		output reg [7:0] mem_counter = 8'd200,
		
		output reg [15:0] kit1_mtt = 16'd46_000,
		output reg [15:0] kit2_mtt = 16'd24_000,
		output reg [15:0] kit3_mtt = 16'd30_000,
		output reg [15:0] kit4_mtt = 16'd24_000,
		output reg [15:0] kit5_mtt = 16'd24_000,
		output reg [15:0] kit6_mtt = 16'd24_000
    );

//#################################
// Az engedélyezés 1 órajelciklus lesz
//
reg mehet_billrol = 0;
reg d = 0;

	always @(posedge clk)
	begin
		if( (clk_d == 1) && (mehet_billrol == 0) && (d == 0))
		begin
			mehet_billrol <= 1;
			d <= 1;
		end
		else if( (clk_d == 1) && (mehet_billrol == 1) )
			mehet_billrol <= 0;
		else if(clk_d == 0)
		begin
			mehet_billrol <= 0;
			d <= 0;
		end
	end
//########################################

/// Tárolás
// átadja a beirando_kod -ot és a tarolas_eng-et a memoria.v-nek

	always @(posedge clk)
	begin
		if(torles_vege)
		begin
			mem_counter <= 8'd200;
			
			kit1_mtt <= 16'd46_000;
			kit2_mtt <= 16'd24_000;
			kit3_mtt <= 16'd30_000;
			kit4_mtt <= 16'd24_000;
			kit5_mtt <= 16'd24_000;
			kit6_mtt <= 16'd24_000;
		end
		
		else if(folyamat==3'd3 && mehet_billrol && mem_counter!=8'd0 && tarolas_eng==0)
		begin
			case(bill_kod)
//felső rész
				8'h15:  // Q -- servo1 - szét - kod: 1_1000_001
				begin
					if(kit1_mtt != 16'd46_000)
					begin
						kit1_mtt <= 16'd46_000;
						//kit1_mtt <= kit1_mtt + 14000;
						
						beirando_kod <= 8'b1_1000_001; // 1-es szervó + 14000
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h24:  // E -- servo1 - össze - kod: 0_1000_001
				begin
					if(kit1_mtt != 16'd32_000)
					begin
						kit1_mtt <= 16'd32_000;
						//kit1_mtt <= kit1_mtt - 14000;
						
						beirando_kod <= 8'b0_1000_001; // 1-es szervó - 14000
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h1B:  // S -- servo2 - le - kod: 0_0100_010		
				begin
					if(kit2_mtt > 16'd8_000)
					begin
						kit2_mtt <= kit2_mtt - 2000;
						
						beirando_kod <= 8'b0_0100_010; // 2-es szervó - 2000
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h1D:  // W -- servo2 - fel - kod: 1_0100_010
				begin
					if(kit2_mtt < 16'd30_000)
					begin
						kit2_mtt <= kit2_mtt + 2000;
						
						beirando_kod <= 8'b1_0100_010; // 2-es szervó + 2000
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h1C:  // A -- servo3 - balra - kod: 0_0100_011
				begin
					if(kit3_mtt > 16'd8_000) 
					begin
						kit3_mtt <= kit3_mtt - 2000;
						
						beirando_kod <= 8'b0_0100_011; // 3-as szervó - 2000
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h23:  // D -- servo3 - jobbra - kod: 1_0100_011
				begin
					if(kit3_mtt < 16'd38_000) 
					begin
						kit3_mtt <= kit3_mtt + 2000;
						
						beirando_kod <= 8'b1_0100_011; // 3-as szervó + 2000
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
//alsó rész
				8'h42 :  //K -- servo4 - le - kod: 1_0001_100
				begin
					if(kit4_mtt < 16'd34_000)
					begin
						kit4_mtt <= kit4_mtt + 500;
						
						beirando_kod <= 8'b1_0001_100; // 4-es szervó + 500
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h43 :  //I -- servo4 - fel - kod: 0_0010_100
				begin
					if(kit4_mtt > 16'd10_000)
					begin
						kit4_mtt <= kit4_mtt - 1500;
						
						beirando_kod <= 8'b0_0010_100; // 4-es szervó - 1500
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h3C : //U -- servo5 - le - kod: 0_0001_101
				begin
					if(kit5_mtt > 16'd8_000)
					begin
						kit5_mtt <= kit5_mtt - 500;
						
						beirando_kod <= 8'b0_0001_101; // 5-ös szervó - 500
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h44 : //O -- servo5 - fel - kod: 1_0001_101
				begin
					if(kit5_mtt < 16'd38_000)
					begin
						kit5_mtt <= kit5_mtt + 500;
						
						beirando_kod <= 8'b1_0001_101; // 5-ös szervó + 500
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h3B : //J -- servo6 - balra - kod: 1_0001_110
				begin
					if(kit6_mtt < 16'd40_000)
					begin
						kit6_mtt <= kit6_mtt + 500;
						
						beirando_kod <= 8'b1_0001_110; // 6-os szervó + 500
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				8'h4B : //L -- servo6 - jobbra - kod: 0_0001_110
				begin
					if(kit6_mtt > 16'd8_000)
					begin
						kit6_mtt <= kit6_mtt - 500;
						
						beirando_kod <= 8'b0_0001_110; // 6-os szervó + 500
						mem_counter <= mem_counter - 1;
						tarolas_eng <= 1;
					end
				end
				default: ;
			endcase
		end//if-et zárja
		
		else 
		begin
			tarolas_eng <= 0;
		end
	end//always-t zárja

endmodule


