Test Bench基础知识笔记

时间:2022-10-01 08:02:31

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

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

写的一个Testbench示例

`timescale  ps/  ps
module SendTest_vlg_tst();
// constants
// general purpose registers
reg eachvec;
// test vector input registers
reg [:] RcvWrAddr;
reg [:] RcvWrData;
reg RcvWrEn;
reg Test;
reg clk;
reg rst;
// wires
wire ARPBit;
wire [:] AppData;
wire AppDataFlag;
wire [:] ClientCMD;
wire ICMPBit;
wire [:] RCAckNum;
wire [:] RCWinSize;
wire RcvDone;
wire [:] ReSend;
wire [:] SSeqNum;
wire SendBusy;
wire [:] SendLength;
wire SendRdClk;
wire SendStart;
wire TCPBit;
wire [:] TCPCount;
wire [:] TCPState;
wire [:] TEncoderState;
wire [:] TRAMDATAIN;
wire [:] TRAMDATAOUT;
wire [:] TRcvDecoderStatus;
wire [:] TTestStatus1;
wire UDPBit;
wire [:] 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=;
parameter RSTTIME=;
initial clk=;
initial forever #(CLKTIME/) clk=~clk;
initial rst=;
initial #RSTTIME rst=; reg [:] CTCPFLAG;
reg [:] CSeqNum;
reg [:] CAckNum;
reg [:] CWinSize;
parameter ACK='b010000;
parameter SYN='b000010;
parameter FIN='b000001;
parameter PUSH='b001000; task SendTCP;
begin
@(negedge clk);//以太网目标MAC地址
RcvWrEn<='b1;
RcvWrAddr<='d0;
RcvWrData<='h00E0;
@(negedge clk); //
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h3DF4;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='hDDF7;
@(negedge clk); //以太网源MAC地址
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='hF0DE;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='hF1B5;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h5EF8;
@(negedge clk);//帧类型IP:0X0800
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0800;
@(negedge clk); //IP帧头
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h4500;
@(negedge clk); //total length
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h40;
@(negedge clk); //Identification
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h4006;
@(negedge clk); //check
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0;
@(negedge clk); //以太网源IP
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h3b45;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h40a9;
@(negedge clk); //以太网目的IP
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h3b45;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h4002;
@(negedge clk); //TCP帧头
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h001E;//src port 30
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h001e;//dest port
@(negedge clk); //seq
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CSeqNum[:];
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CSeqNum[:];
@(negedge clk); //ack
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CAckNum[:];
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CAckNum[:];
@(negedge clk); //
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<={'d5,6'd0,CTCPFLAG};
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CWinSize;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='d0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='d0;
@(negedge clk);
RcvWrEn<='b0;
RcvWrAddr<='d0;
RcvWrData<='d0;
@(negedge RcvDone);
end
endtask task SendT;
begin
//客户机发送'T'//////////////////////////////////////////////////////////////////
@(negedge clk);//以太网目标MAC地址
RcvWrEn<='b1;
RcvWrAddr<='d0;
RcvWrData<='h00E0;
@(negedge clk); //
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h3DF4;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='hDDF7;
@(negedge clk); //以太网源MAC地址
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='hF0DE;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='hF1B5;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h5EF8;
@(negedge clk);//帧类型IP:0X0800
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0800;
@(negedge clk); //IP帧头
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h4500;
@(negedge clk); //total length
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h29;//16'd41
@(negedge clk); //Identification
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h4006;
@(negedge clk); //check
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h0;
@(negedge clk); //以太网源IP
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h3b45;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h40a9;
@(negedge clk); //以太网目的IP
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h3b45;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h4002;
@(negedge clk); //TCP帧头
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h001E;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h001E;
@(negedge clk); //seq
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CSeqNum[:];
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CSeqNum[:];
@(negedge clk); //ack
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CAckNum[:];
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CAckNum[:];
@(negedge clk); //
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<={'d5,6'd0,CTCPFLAG};
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<=CWinSize;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='d0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='d0;
@(negedge clk);
RcvWrAddr<=RcvWrAddr+'b1;
RcvWrData<='h5400;//'T'
@(negedge clk);
RcvWrEn<='b0;
RcvWrAddr<='d0;
RcvWrData<='d0;
@(negedge RcvDone);
end
endtask initial
begin
RcvWrEn<='b0;
RcvWrAddr<='d0;
RcvWrData<='d0;
//写SYN请求//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d0;
CAckNum<='d0;
CTCPFLAG<=SYN;
CWinSize<='d65535;
SendTCP();
wait(SendBusy==);
wait(SendBusy==);
//回应SYN+ACK//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
//发送ACK1//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
//发送ACK2//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
//发送ACK3//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
//发送ACK4//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
//发送ACK5//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
//发送ACK6//////////////////////////////////////////////////////////////////
repeat()@(negedge clk);
CSeqNum<='d1;
CAckNum<='d1;
CTCPFLAG<=ACK;
CWinSize<='d65535;
SendTCP();
end endmodule