`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Megkapja a bill_kod-ot, a clk_d-t és a folyamatot,
// és ha a folyamat 1, akkor engedélyezzük a működését 
//
//////////////////////////////////////////////////////////////////////////////////
module billrol_iranyitott(
		input clk, rst,
		input wire [7:0] bill_kod,
		input wire [2:0] folyamat,
		input wire clk_d,
		
		input [15:0] kit1_mtt,
		input [15:0] kit2_mtt, 
		input [15:0] kit3_mtt, 
		input [15:0] kit4_mtt, 
		input [15:0] kit5_mtt, 
		input [15:0] kit6_mtt, 
		
		input [15:0] kit1_mki,
		input [15:0] kit2_mki, 
		input [15:0] kit3_mki, 
		input [15:0] kit4_mki, 
		input [15:0] kit5_mki, 
		input [15:0] kit6_mki, 
		
		output reg [15:0] kit1_b = 16'd46_000,
		output reg [15:0] kit2_b = 16'd24_000,
		output reg [15:0] kit3_b = 16'd30_000,
		output reg [15:0] kit4_b = 16'd24_000,
		output reg [15:0] kit5_b = 16'd24_000,
		output reg [15:0] kit6_b = 16'd24_000
    );

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
	
//localparam NYIT = 16'd46_000;
//localparam ZAR  = 16'd32_000;	
	
	always @(posedge clk)
	begin
		if(rst || folyamat!= 3'd1)
		begin
			kit1_b <= (folyamat == 3'd3) ? kit1_mtt : 
						 ( (folyamat == 3'd4) ? kit1_mki : 
						 ( (folyamat == 3'd5) ? kit1_b : 16'd46_000));
			kit2_b <= (folyamat == 3'd3) ? kit2_mtt : 
						 ( (folyamat == 3'd4) ? kit2_mki : 
						 ( (folyamat == 3'd5) ? kit2_b : 16'd24_000));
			kit3_b <= (folyamat == 3'd3) ? kit3_mtt : 
						 ( (folyamat == 3'd4) ? kit3_mki : 
						 ( (folyamat == 3'd5) ? kit3_b : 16'd30_000));
			kit4_b <= (folyamat == 3'd3) ? kit4_mtt : 
						 ( (folyamat == 3'd4) ? kit4_mki : 
						 ( (folyamat == 3'd5) ? kit4_b : 16'd24_000));
			kit5_b <= (folyamat == 3'd3) ? kit5_mtt : 
						 ( (folyamat == 3'd4) ? kit5_mki : 
						 ( (folyamat == 3'd5) ? kit5_b : 16'd24_000));
			kit6_b <= (folyamat == 3'd3) ? kit6_mtt : 
						 ( (folyamat == 3'd4) ? kit6_mki : 
						 ( (folyamat == 3'd5) ? kit6_b : 16'd24_000));
		end
		else if((folyamat == 3'd1) && (mehet_billrol) )
			case(bill_kod)
//felső rész
				8'h15:  // Q -- servo1 - szét - kod: 10
				begin
//					if(kit1_b == ZAR)
//						kit1_b <= NYIT;
					if(kit1_b != 16'd46_000)
						kit1_b <= 16'd46_000;
						//kit1_b <= kit1_b + 14000;
				end
				8'h24:  // E -- servo1 - össze - kod: 11
				begin
//					if(kit1_b == NYIT)
//						kit1_b <= ZAR;
					if(kit1_b != 16'd32_000)
						kit1_b <= 16'd32_000;
						//kit1_b <= kit1_b - 14000;
				end
				8'h1B:  // S -- servo2 - le - kod: 20		
				begin
					if(kit2_b > 16'd8_000) 
						kit2_b <= kit2_b - 2000;
				end
				8'h1D:  // W -- servo2 - fel - kod: 21
				begin
					if(kit2_b < 16'd30_000)
						kit2_b <= kit2_b + 2000;
				end
				8'h1C:  // A -- servo3 - balra - kod: 30
				begin
					if(kit3_b > 16'd8_000) 
						kit3_b <= kit3_b - 2000;
				end
				8'h23:  // D -- servo3 - jobbra - kod: 31
				begin
					if(kit3_b < 16'd38_000) 
						kit3_b <= kit3_b + 2000;
				end
//alsó rész
				8'h42 :  //K -- servo4 - le - kod: 40
				begin
					if(kit4_b < 16'd34_000)
						kit4_b <= kit4_b + 500;
				end
				8'h43 :  //I -- servo4 - fel - kod: 41
				begin
					if(kit4_b > 16'd10_000)
						kit4_b <= kit4_b - 1500;
				end
				8'h3C : //U -- servo5 - le - kod: 50
				begin
					if(kit5_b > 16'd8_000)
						kit5_b <= kit5_b - 500;
				end
				8'h44 : //O -- servo5 - fel - kod: 51
				begin
					if(kit5_b < 16'd38_000)
						kit5_b <= kit5_b + 500;
				end
				8'h3B : //J -- servo6 - balra - kod: 60
				begin
					if(kit6_b < 16'd40_000)
						kit6_b <= kit6_b + 500;
				end
				8'h4B : //L -- servo6 - jobbra - kod: 61
				begin
					if(kit6_b > 16'd8_000)
						kit6_b <= kit6_b - 500;
				end
				default: ;
			endcase


	end//allways-t zárja

endmodule

