新闻  |   论坛  |   博客  |   在线研讨会
PSP_FPGA_RAM控制源码
0750long | 2009-04-18 11:34:13    阅读:2960   发布文章
PSP_FPGA_RAM控制源码

 

module PSP(clk,HS,VS,DISP,LED,pspclk,pspdata,CE,OE,WE,BH,BL,ADDER,WR,wrdata,RamData,RST,RS);
    input clk,WR,RST,RS;
    input [15:0] wrdata;
    inout [15:0] RamData;
    output HS,VS,DISP,LED,pspclk,CE,OE,WE,BH,BL,ADDER,pspdata;
///////////////////////////////////////////////////////////////////////////////////////////////////////////   
    reg [23:0] pspdata;   
    wire [17:0] ADDER;                     //RAM地址输出寄存器
   
   
   
    reg [9:0]J1;                   //HS计数器:
    reg [8:0]J2;                   //VS计数器:
    reg HS="1";                      //PSP屏行扫描
    reg VS="1";                      //PSP屏帧扫描
    reg PSPEN="0";                   //psp开始工作标志位
    reg DISP="0";                    //PSP开显示,1开,0关
    reg [4:0] i;                  //分频时的计数器
    reg pspclk;                    //psp屏时钟
    reg [2:0] pspdataChoose;      //向PSP发数据时确定是第一位数据还是第二位数据

    //reg pspdataen="1";
////////////////////////////////////////////////////////////////////////////////////////////////////////////
    reg LED="1";                      //检测系统时钟工作正常的LED
    reg [1:0] led;
    reg [21:0] sysen;               //系统上电延时标志位
/////////////////////////////////////////////////////////////////////////////////////////////////////////     
    reg addstart;
    reg RamEN="0";
    reg [15:0] outdata;             //临时数据区为inout中的OUT 输出数据
    reg [4:0]  start;               //开始控制读写RAM和PSP数据发送计数器
    reg [15:0] RamA0;
    reg [15:0] RamB0;
    reg [15:0] RamC0;
    reg [15:0] RamA1;
    reg [15:0] RamB1;
    reg [15:0] RamC1;
    reg CE="0";
    reg OE,WE,BH,BL;                      //RAM控制位
    reg [15:0] wridata;                   //外部输入的数据临时存放寄存器
    reg [17:0] WADDER;                    //写入数据时的RAM地址
    reg [17:0] RADDER;                     //读RAM数据时的地址
    reg oe;reg WRoe;
    reg RamStart="0";
    reg WADDEREN;
    assign RamData="oe"? outdata:16'hzzzz;
    assign ADDER="WRoe"? WADDER:RADDER;
///////////////////////////////////////////////////////////////////////////////////////////////////////////  
always @(posedge clk)              //系统上电延时函数
begin
    sysen<=sysen+1;
    if(sysen>4000000)//4000000)                   //大概是74MS
      begin
           PSPEN="1";
      end
end
////////////////////////////////////////////////////////////////////////////////////////////////////////////
always @(posedge clk)             //时钟分频函数,输出PSP时钟
begin
if(PSPEN)                  //
begin
   if(i==2)
     begin
      i<=0;
      pspclk<=~pspclk;
     end
   else
      i<=i+1;
  end
end
////////////////////////////////////////////////////////////////////////////////////////////////////////////
always @(posedge clk)              //开始控制读写RAM和PSP数据发送函数
begin
  if(RamEN)
   begin
      BH="0";BL=0;
       case(start)
      0:
         begin
           OE="1";WE=0;CE=0;
           oe="1";WRoe=1;
           start="1"; 
         end    
      1:
       begin
           outdata="wridata";
           start="2";
       end 
      2:
       begin
           oe="0";WRoe=0;
           OE="0";CE=0;WE=1;
           start="3";
       end             
     3:
         begin
          RADDER<=RADDER+1;                        //adda0           
          start="4";
         end

      4:
         begin
           RamA0<=RamData;                       //dataa0
           start="5";             
         end

     5: 
         begin                               //addb0
           RADDER<=RADDER+1;    
           start="6"; 
         end
      6:
         begin                               //datab0
           RamB0<=RamData;   
           start="7";
         end

      7: begin 
           RADDER<=RADDER+1;   
           start="8";
          end
      8:
         begin
           RamC0<=RamData;                   //datac0
           start="9";
         end
      9:
         begin
           RADDER<=RADDER+1;    
           start="10";
         end
     10:
         begin                             //dataa1
          RamA1<=RamData;
          start="11";
         end
      11:                                  //addb1
         begin 
           RADDER<=RADDER+1;    
           start="12"; 
         end
     12:
        begin
           RamB1<=RamData;               //databb1
           start="13"; 
        end
     13:
        begin
           RADDER<=RADDER+1;   
           start="14";
         end
     14:
       begin
          RamC1<=RamData;               //datac1
          start="15";
       end
     15:
       begin
           CE="1";
        if(addstart==1)
           begin
              RADDER="0";
           end
           start="16";
       end
     16:
       begin
           OE="1";WE=0;CE=0;
           oe="1";WRoe=1;
           start="17";
 
       end

    17:
       begin
          outdata="wridata";
          start="18";
       end
    18:
       begin
          start="19";
       end
    19:
       begin
          outdata="wridata";
          start="20";
       end
    20:
       begin
           oe="0"; CE="1";
           start="21";
       end
   
    21:
       begin
           start="22";
       end

    22:
       begin
           start="23";
       end
    23:
       begin
           start="0";
       end
      endcase
   end
end  
////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
always @(posedge clk )
begin
  if(RST==0)
     begin
      WADDEREN="0";
     end
  else
      WADDEREN="1";
end
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////
always @(negedge WR )                         //WR下降沿时读入外部数据
begin
   case(RS)                                   //RS=0;cmd
    1:                                        //RS=1;DATA
     begin
       if(WADDER==196560) //195840                           
         begin
           WADDER="1";
          end
        else  
         WADDER="WADDER"+1;

       if(WADDER<=196560)
         begin
          wridata<=wrdata;
         end
     end
    0:
      begin
        WADDER<=wrdata;
      end
    endcase
end
///////////////////////////////////////////////////////////////////////////////////////////////////////////        
always@(posedge pspclk)                      //将colordata的数据发送给PSP
begin
      if(J1==525)
         begin
           J1=1;
          end
        else 
            J1=J1+1;
   if(J1<=41)
        begin
           HS="0";
        end
    else if(J1<=43)
        begin
          HS="1";
          RamEN="1";
          pspdata='hzzzzzz;
        end
    else if(J1<=523)
       begin
           if(J1==522)
              begin
                 RamEN="0";
               end
             case(pspdataChoose)
                 0:
                  begin
                    pspdata[23:8]<=RamA0[15:0];
                    pspdata[7:0] <=RamB0[15:8];
                    pspdataChoose="1";
                    
                 end
                 1:
                   begin
                    pspdata[23:16]<=RamB0[7:0];
                    pspdata[15:0] <=RamC0[15:0];
                    pspdataChoose="2";
                   end
                2:
                  begin
                    pspdata[23:8]<=RamA1[15:0];
                    pspdata[7:0] <=RamB1[15:8];
                    pspdataChoose="3"; 
                  end
                3:
                  begin
                    pspdata[23:16]<=RamB1[7:0];
                    pspdata[15:0] <=RamC1[15:0];
                    pspdataChoose="0";
                end
               endcase
    end 
   
     else if(J1<=525)
          begin
             pspdata='hzzzzzz;
          end  
    
end   
///////////////////////////////////////////////////////////////////////////////////////////////////////////   
always@(posedge HS)                 //PSP屏帧扫描函数
begin
   if(J2==286)
     begin
       J2=1;
     end
    else
      J2=J2+1;
  if(J2<=10)
    begin
        DISP="1";
        VS="0";
     end
  else if(J2<=12)
     begin
        addstart="1";
        VS="1";
     end
  else if(J2<=286)
      begin
        addstart="0";
      end   
  /*else if(J2<=286)
     begin
     end*/
end       
////////////////////////////////////////////////////////////////////////////////////////////////////////////
always @(posedge VS)                   //LED闪烁函数,为了检测系统时钟是否工作正常
begin     
    led<=led+1;
    if(led==3)
        begin
             LED=~LED;
             led<=0;
        end
end   
endmodule

参与讨论
登录后参与讨论
推荐文章
最近访客