// sram_test.v module sram_test(clk,reset,write,read,writeaddr,writedata,readaddr,readdata, sram_oe,sram_ce,sram_lb,sram_we,sram_addr,sram_data); input clk; input reset; input write; input [17:0] writeaddr; input [15:0] writedata; input read; input [17:0] readaddr; output [15:0] readdata; output sram_oe; output sram_ce; output sram_lb; output sram_we; output [17:0] sram_addr; inout [15:0] sram_data; reg sram_oe; reg sram_ce; reg sram_lb; reg sram_we; reg [17:0] sram_addr; reg [15:0] readdata; reg [3:0] presState; reg [3:0] nextState; reg [15:0] wdata_reg; reg [17:0] waddr_reg; reg [15:0] data_reg0; reg [17:0] raddr_reg; parameter stIdle = 4'b0000; parameter stWrite1 = 4'b0001; parameter stWrite2 = 4'b0010; parameter stWrite3 = 4'b0011; parameter stWrite4 = 4'b0100; parameter stRead1 = 4'b0101; parameter stRead2 = 4'b0110; parameter stRead3 = 4'b0111; parameter stRead4 = 4'b1000; always @(posedge clk) begin if(!reset) begin waddr_reg <= 18'b0; wdata_reg <= 16'b0; end else begin if(write) begin waddr_reg <= writeaddr; wdata_reg <= writedata; end end end always @(posedge clk) begin if(!reset) begin raddr_reg <= 18'b0; end else begin if(read) begin raddr_reg <= readaddr; end end end always @(posedge clk) begin if(!reset) begin presState <= stIdle; end else begin presState <= nextState; end end always @(*) begin case(presState) stIdle: begin if(write == 1'b1) begin nextState = stWrite1; end else if(read == 1'b1) begin nextState = stRead1; end else begin nextState = stIdle; end end stWrite1: begin nextState = stWrite2; end stWrite2: begin nextState = stWrite3; end stWrite3: begin nextState = stWrite4; end stWrite4: begin nextState = stIdle; end stRead1: begin nextState = stRead2; end stRead2: begin nextState = stRead3; end
stRead3: begin nextState = stRead4; end stRead4: begin nextState = stIdle; end default: begin nextState = stIdle; end endcase end always @(posedge clk) begin if(!reset) begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b1111; end else begin case(nextState) stIdle: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b1111; end stWrite1: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0110; end stWrite2: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0100; end stWrite3: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0100; end stWrite4: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0110; end stRead1: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0110; end stRead2: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0010; end stRead3: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0010; end stRead4: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b0110; end default: begin {sram_ce,sram_oe,sram_we,sram_lb} <= 4'b1111; end endcase end end
always @(posedge clk) begin if(nextState == stRead3) begin readdata <= sram_data; end end always @(posedge clk) begin if(nextState == stWrite1) begin sram_addr <= waddr_reg; end else if(nextState == stRead1) begin sram_addr <= raddr_reg; end end always @(posedge clk) begin if(nextState == stWrite3) begin data_reg0 <= wdata_reg; end end assign sram_data = sram_we ? 16'hzzzz : data_reg0;