`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
//
// Megkapja a memoria.v-től a kiolvasott_kod-ot, és a kitöltési tényezőt annak megfelelően változtatja, 
// így visszajátszható a letárolt mozgás
//////////////////////////////////////////////////////////////////////////////////
module mem_kiolvas(
		input clk, r_en, rst,
		input [7:0] kiolvasott_kod,
		input kiolv_vege,
	
		output reg [15:0] kit1_mki = 16'd46_000,
		output reg [15:0] kit2_mki = 16'd24_000,
		output reg [15:0] kit3_mki = 16'd30_000, 
		output reg [15:0] kit4_mki = 16'd24_000,
		output reg [15:0] kit5_mki = 16'd24_000,
		output reg [15:0] kit6_mki = 16'd24_000
    );

reg mehet = 0;
reg d = 0;

	always @(posedge clk)
	begin
		if( (r_en == 1) && (mehet == 0) && (d == 0))
		begin
			mehet <= 1;
			d <= 1;
		end
		else if( (r_en == 1) && (mehet == 1) )
			mehet <= 0;
		else if(r_en == 0)
		begin
			mehet <= 0;
			d <= 0;
		end
	end

	always @(posedge clk)
	begin
		if(kiolv_vege || rst)
		begin
			kit1_mki <= 16'd46_000;
			kit2_mki <= 16'd24_000;
			kit3_mki <= 16'd30_000;
			kit4_mki <= 16'd24_000;
			kit5_mki <= 16'd24_000;
			kit6_mki <= 16'd24_000;
		end	
		
		else if(mehet)
		begin
		case(kiolvasott_kod[2:0])//x_xxxx_xxx
				3'b001: kit1_mki <= ((kiolvasott_kod[7]) ? (kit1_mki + 14000) : (kit1_mki - 14000)) ;
				3'b010: kit2_mki <= ((kiolvasott_kod[7]) ? (kit2_mki +  2000) : (kit2_mki -  2000)) ;
				3'b011: kit3_mki <= ((kiolvasott_kod[7]) ? (kit3_mki +  2000) : (kit3_mki -  2000)) ;
				3'b100: kit4_mki <= ((kiolvasott_kod[7]) ? (kit4_mki +   500) : (kit4_mki -  1500)) ;
				3'b101: kit5_mki <= ((kiolvasott_kod[7]) ? (kit5_mki +   500) : (kit5_mki -   500)) ;
				3'b110: kit6_mki <= ((kiolvasott_kod[7]) ? (kit6_mki +   500) : (kit6_mki -   500)) ;
				default: ;
		endcase
		
		
		end
	end

endmodule


