Test Bench基础知识笔记

学的内容多了,好多指令和用法都容易遗忘和混淆,出现这种情况就需要勤记笔记,忘记了多翻阅几次,基本上就能完全记住了。

[`timescale 1ns/1ps]前一个1ns表示时延时间,后一个1ps表示时延精度。

写的一个Testbench示例

`timescale 1 ps/ 1 ps
module SendTest_vlg_tst();
// constants                                           
// general purpose registers
reg eachvec;
// test vector input registers
reg [9:0] RcvWrAddr;
reg [15:0] RcvWrData;
reg RcvWrEn;
reg Test;
reg clk;
reg rst;
// wires                                               
wire ARPBit;
wire [15:0]  AppData;
wire AppDataFlag;
wire [7:0]  ClientCMD;
wire ICMPBit;
wire [31:0]  RCAckNum;
wire [15:0]  RCWinSize;
wire RcvDone;
wire [1:0]  ReSend;
wire [31:0]  SSeqNum;
wire SendBusy;
wire [15:0]  SendLength;
wire SendRdClk;
wire SendStart;
wire TCPBit;
wire [15:0]  TCPCount;
wire [3:0]  TCPState;
wire [15:0]  TEncoderState;
wire [15:0]  TRAMDATAIN;
wire [15:0]  TRAMDATAOUT;
wire [15:0]  TRcvDecoderStatus;
wire [15:0]  TTestStatus1;
wire UDPBit;
wire [15:0]  Value;

// assign statements (if any)                          
SendTest i1 (
// port map - connection between master ports and signals/registers   
    .ARPBit(ARPBit),
    .AppData(AppData),
    .AppDataFlag(AppDataFlag),
    .ClientCMD(ClientCMD),
    .ICMPBit(ICMPBit),
    .RCAckNum(RCAckNum),
    .RCWinSize(RCWinSize),
    .RcvDone(RcvDone),
    .RcvWrAddr(RcvWrAddr),
    .RcvWrData(RcvWrData),
    .RcvWrEn(RcvWrEn),
    .ReSend(ReSend),
    .SSeqNum(SSeqNum),
    .SendBusy(SendBusy),
    .SendLength(SendLength),
    .SendRdClk(SendRdClk),
    .SendStart(SendStart),
    .TCPBit(TCPBit),
    .TCPCount(TCPCount),
    .TCPState(TCPState),
    .TEncoderState(TEncoderState),
    .TRAMDATAIN(TRAMDATAIN),
    .TRAMDATAOUT(TRAMDATAOUT),
    .TRcvDecoderStatus(TRcvDecoderStatus),
    .TTestStatus1(TTestStatus1),
    .Test(Test),
    .UDPBit(UDPBit),
    .Value(Value),
    .clk(clk),
    .rst(rst)
);
parameter CLKTIME=10;
parameter RSTTIME=20;
initial clk=0;
initial forever #(CLKTIME/2) clk=~clk;
initial rst=0;
initial #RSTTIME rst=1;                                                   

reg [5:0] CTCPFLAG;
reg [31:0] CSeqNum;
reg [31:0] CAckNum; 
reg [15:0] CWinSize;
parameter ACK=6'b010000;
parameter SYN=6'b000010;
parameter FIN=6'b000001;
parameter PUSH=6'b001000;

task SendTCP;
    begin
        @(negedge clk);//以太网目标MAC地址
            RcvWrEn<=1'b1;
            RcvWrAddr<=10'd0;
            RcvWrData<=16'h00E0;
        @(negedge clk);    //203
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h3DF4;
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'hDDF7;
        @(negedge clk);    //以太网源MAC地址
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'hF0DE;    
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'hF1B5;
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h5EF8;    
        @(negedge clk);//帧类型IP:0X0800
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h0800;
        @(negedge clk);    //IP帧头    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h4500;
        @(negedge clk);    //total length
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h40;
        @(negedge clk);    //Identification
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h0;    
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h0;
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h4006;
        @(negedge clk);    //check
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h0;
        @(negedge clk);    //以太网源IP
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h3b45;
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h40a9;
        @(negedge clk);    //以太网目的IP
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h3b45;
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h4002;
        @(negedge clk);    //TCP帧头
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h001E;//src port 30
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'h001e;//dest port
        @(negedge clk);    //seq
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=CSeqNum[31:16];
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=CSeqNum[15:0];
        @(negedge clk);    //ack
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=CAckNum[31:16];
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=CAckNum[15:0];
        @(negedge clk);    //
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<={4'd5,6'd0,CTCPFLAG};
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=CWinSize;
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'd0;    
        @(negedge clk);    
            RcvWrAddr<=RcvWrAddr+1'b1;
            RcvWrData<=16'd0;            
        @(negedge clk);    
            RcvWrEn<=1'b0;
            RcvWrAddr<=10'd0;
            RcvWrData<=16'd0;    
        @(negedge RcvDone);
        end
endtask

task SendT;
    begin
    //客户机发送'T'//////////////////////////////////////////////////////////////////
    @(negedge clk);//以太网目标MAC地址
        RcvWrEn<=1'b1;
        RcvWrAddr<=10'd0;
        RcvWrData<=16'h00E0;
    @(negedge clk);    //203
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h3DF4;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'hDDF7;
    @(negedge clk);    //以太网源MAC地址
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'hF0DE;    
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'hF1B5;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h5EF8;    
    @(negedge clk);//帧类型IP:0X0800
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h0800;
    @(negedge clk);    //IP帧头    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h4500;
    @(negedge clk);    //total length
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h29;//16'd41
    @(negedge clk);    //Identification
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h0;    
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h0;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h4006;
    @(negedge clk);    //check
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h0;
    @(negedge clk);    //以太网源IP
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h3b45;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h40a9;
    @(negedge clk);    //以太网目的IP
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h3b45;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h4002;
    @(negedge clk);    //TCP帧头
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h001E;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h001E;
    @(negedge clk);    //seq
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=CSeqNum[31:16];
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=CSeqNum[15:0];
    @(negedge clk);    //ack
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=CAckNum[31:16];
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=CAckNum[15:0];
    @(negedge clk);    //
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<={4'd5,6'd0,CTCPFLAG};
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=CWinSize;
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'd0;    
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'd0;        
    @(negedge clk);    
        RcvWrAddr<=RcvWrAddr+1'b1;
        RcvWrData<=16'h5400;//'T'            
    @(negedge clk);    
        RcvWrEn<=1'b0;
        RcvWrAddr<=10'd0;
        RcvWrData<=16'd0;    
    @(negedge RcvDone);    
    end
endtask

initial
begin 
    RcvWrEn<=1'b0;
    RcvWrAddr<=10'd0;
    RcvWrData<=16'd0;        
//写SYN请求//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd0;
    CAckNum<=32'd0;
    CTCPFLAG<=SYN;
    CWinSize<=16'd65535;
    SendTCP();
    wait(SendBusy==1);
    wait(SendBusy==0);
//回应SYN+ACK//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();    
//发送ACK1//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();
//发送ACK2//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();
//发送ACK3//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();
//发送ACK4//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();
//发送ACK5//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();
//发送ACK6//////////////////////////////////////////////////////////////////
    repeat(10)@(negedge clk);
    CSeqNum<=32'd1;
    CAckNum<=32'd1;
    CTCPFLAG<=ACK;
    CWinSize<=16'd65535;
    SendTCP();
end
        
endmodule

 

posted on 2016-04-07 11:00  万好好  阅读(491)  评论(0编辑  收藏  举报

导航