DE2_115_SRAM的小应用

大家都知道DE2_115_SRAM没有给出API  这里给出API 下面的历程是实现SRAM一个读写的小实验

板子上的KEY0作为复位信号 KEY1作为写信号 KEY2作为读信号

SW作为地址输入  数码管显示相应地址的数据

代码如下:

顶层模块:

module DE2_115_TV

    (  

 //////// CLOCK //////////   

           CLOCK_50,   

           CLOCK2_50,      

//////// KEY //////////   

           KEY,

  //////// SW //////////   

           SW,

  //////// SEG7 //////////   

          HEX0,

          HEX1,

          HEX2,  

          HEX3,

  //////// SRAM //////////   

          SRAM_ADDR,   

          SRAM_CE_N,   

          SRAM_DQ,  

          SRAM_LB_N,   

          SRAM_OE_N,  

          SRAM_UB_N,   

          SRAM_WE_N,   

     );  

//////////// CLOCK //////////

input              CLOCK_50;

input              CLOCK2_50;

//////////// KEY //////////

input       [3:0]  KEY;

//////////// SW //////////

input      [17:0]  SW;

//////////// SEG7 //////////

output       [6:0]  HEX0;

output       [6:0]  HEX1;

output       [6:0]  HEX2;

output       [6:0]  HEX3;

//////////// SRAM //////////

output      [19:0]  SRAM_ADDR;

output              SRAM_CE_N;

inout      [15:0]     SRAM_DQ;

output              SRAM_LB_N;

output              SRAM_OE_N;

output              SRAM_UB_N;

output              SRAM_WE_N;

// 7 segment LUT

SEG7_LUT_8    u0 

   ( 

      .oSEG0(HEX0),        

      .oSEG1(HEX1),        

      .oSEG2(HEX2),        

       .oSEG3(HEX3),       

       .iDIG(SRAM_odata)       

     );

wire[15:0] SRAM_odata;               

SRAM_controller   u1

   (     

        .iCLK(CLOCK_50),   

        .iRST_N(KEY[0]),      

        .iWR(KEY[1]),   

        .iRD(KEY[2]),   

        .iWR_ADDR(0),    

        .iWR_MAX_ADDR(1000000),   

        .iRD_ADDR(SW),   

        .oRD_DATA(SRAM_odata),      

        .SRAM_DQ(SRAM_DQ),   

        .SRAM_ADDR(SRAM_ADDR),   

        .SRAM_CE_N(SRAM_CE_N),   

        .SRAM_OE_N(SRAM_OE_N),   

        .SRAM_WE_N(SRAM_WE_N),   

        .SRAM_UB_N(SRAM_UB_N),   

        .SRAM_LB_N(SRAM_LB_N)  

   );               

endmodule

两个底层模块:

数码管显示模块:

次数忽略

SRAM控制模块:

module SRAM_controller   

    (     

           iCLK,   

          iRST_N,   

          iWR,   

          iRD,   

          iWR_ADDR,   

          iWR_MAX_ADDR,   

          iRD_ADDR,   

          oRD_DATA,   

          SRAM_DQ,   

          SRAM_ADDR,   

          SRAM_CE_N,   

          SRAM_OE_N,   

          SRAM_WE_N,   

          SRAM_UB_N,   

          SRAM_LB_N  

   );

input                             iCLK;

input                             iRST_N,

input                             iWR,

iRD;

input     [17:0]              iWR_ADDR,

input     [17:0]              iWR_MAX_ADDR,

input     [17:0]              iRD_ADDR;

output   [15:0]              oRD_DATA;

reg       [15:0] ]            oRD_DATA

inout     [15:0]              SRAM_DQ;

output                          SRAM_CE_N

output                          SRAM_OE_N

output                          SRAM_WE_N

output                          SRAM_UB_N

output                          SRAM_LB_N;

output   [17:0]              SRAM_ADDR;

reg                              SRAM_WE_N;

reg       [15:0]              SRAM_DQ;

reg       [17:0]              SRAM_ADDR;

assign                          SRAM_CE_N = 0;        //SRAM is always worked

assign                          SRAM_UB_N = 0;        //upper byte is available

assign                          SRAM_LB_N = 0;        //lower byte is available assign SRAM_OE_N = 0;//output enable (not effect write)

reg      [15:0]               tmp_data;

reg      [17:0]               rWR_ADDR,rWR_MAX_ADDR;

always @(posedge iCLK or negedge iRST_N)

begin   

        if(!iRST_N)    

            begin    

                    rWR_ADDR <= iWR_ADDR;    

                    rWR_MAX_ADDR <= iWR_MAX_ADDR;    

                    tmp_data <= 0;    

                    SRAM_WE_N <= 0;   

            end  

       else    

           begin     

                    //write process    

                    if ((iWR==1) && (rWR_ADDR<iWR_MAX_ADDR))      

                        begin       

                                SRAM_WE_N <= 0;      

                                rWR_ADDR<=rWR_ADDR+1'b1;      

                                tmp_data <= tmp_data + 1'b1;      

                                SRAM_DQ <= tmp_data;     

                                SRAM_ADDR <= rWR_ADDR;     

                       end   

                   else      

                        begin       

                              SRAM_WE_N <= 1;               //write disable     

                              tmp_data <= 0;     

                              rWR_ADDR <= 0;     

                        end    

                   //read process   

                   if (iRD==1)     

                        begin        

                               SRAM_DQ <= 16'hzzzz;      //SRAM_DQ must be set to z before read      

                               SRAM_ADDR <= iRD_ADDR;      

                               oRD_DATA<=SRAM_DQ;     

                        end   

          end

end

endmodule

通过数码管显示的数据和signaiTab ii的数据对照  结果一致,注意KEY[1]按键时间不能太短,否则数据可能写不进去,SRAM读出的数据是以前的数据

原文地址:https://www.cnblogs.com/Arthur339432/p/2591163.html