`timescale 1ns / 1ps
//////////////////////////////////
//!CS	  !WE   !OE	     MODE
// 1		X     X		Not Select
// 0     1     1     Output Disable
// 0     1     0     Read
// 0     0     X     Write
//////////////////////////////////

module memoria(
		input clk, rst,
		input [2:0] folyamat,
		input tarolas_eng,
		input [7:0] beirando_kod,
		output reg [7:0] kiolvasott_kod = 8'd0,
		
		output reg [7:0] w_address = 8'd0,
		output reg [7:0] r_address = 8'd0,
		
		inout [7:0] sram_databus,
		output reg [16:0] sram_addr = 17'd0,
		output sram_csn, sram_oen, sram_wen,
		output reg kiolv_vege,
		output clk_kiolv,
		output reg r_en = 0,
		output reg torles_vege = 0
    );

/////////////////////////////////////////////////////
// -- kiolvasás sebessége
  reg [23:0] counter;
   
   always @(posedge clk)
   begin
      if (rst || clk_kiolv)
         counter <= 24'd0;
      else
         counter <= counter + 1;
   end
   
   assign clk_kiolv = (counter == 24'd3999999); 
/////////////////////////////////////////////////////	 
//reg [7:0] r_address = 8'd0;
//reg [7:0] w_address = 8'd0;
//#################################
always @(posedge clk)
	begin
		if(clk_kiolv && (r_address == w_address))
			kiolv_vege <= 1;
		else
			kiolv_vege <= 0;	
	end
//##################################	 
	 
reg [7:0] beirando_adat = 8'd0;	 
reg we = 1; // 1 - Read - 0 - Write

assign sram_csn = 0;
assign sram_oen = 0;
assign sram_wen = we;

assign sram_databus = (!we) ? beirando_adat : 8'hz; 

reg [1:0] w_status = 2'd0; //2 1
reg r_status = 0;
reg [1:0] d_status = 2'd0;
reg t_en;
reg [7:0] d_address = 8'd0;

	always @(posedge clk)
	begin
		if(rst || kiolv_vege)
		begin
			r_address <= 8'd0;
			r_status <= 0;
			r_en <= 0;
		end
		
		else if(torles_vege)
		begin
			d_status <= 2'd0;
			d_address <= 8'd0;
			torles_vege <= 0;
			
			w_status <= 2'd0; 
			w_address <= 8'd0; //törlés után megint az elejéről kezdhetjük
		end
		
		else if(tarolas_eng)
			t_en <= 1;
		else if(t_en)
			case(w_status)
			2'd0://Write -- OE- low fixed
				begin
					we <= 0;
					sram_addr <= w_address;
					
					w_status <= 2'd1;
				end
			2'd1:
				begin
					beirando_adat <= beirando_kod;
					
					w_status <= 2'd2;
				end
			2'd2://írás vége
				begin
					we <= 1;
					w_status <= 2'd0;
					t_en <= 0;
					w_address <= w_address + 1;
				end
			default: ;
			endcase
		
		else if(folyamat==3'd4 && clk_kiolv && !kiolv_vege /*&& init_lefutott*/) //Read
			case(r_status)
			0:
				begin
					sram_addr <= r_address; //r_address és a clk_kiolv local
					r_en <= 0;
					r_status <= 1;
				end
			1:
				begin
					kiolvasott_kod <= sram_databus;
					r_en <= 1;
					r_status <= 0;
					r_address <= r_address + 1;
				end
			default: ;
			endcase
		
		else if((folyamat == 3'd5) && (torles_vege == 0))
			case(d_status)
			2'd0:
				begin
					we <= 0;
					sram_addr <= d_address;
					d_status <= 2'd1;
				end
			2'd1:
				begin
					beirando_adat <= 8'd0;
					d_status <= 2'd2;
				end	
			2'd2:
				begin
					we <= 1;
					d_status <= 0;
					
					if(d_address == w_address)
						torles_vege <= 1;
					else
						d_address <= d_address + 1;
				end	
			default: ;
			endcase
	end//always-t zárja

endmodule


