当前位置:网站首页>Learn TB writing method with SPI simulation file
Learn TB writing method with SPI simulation file
2022-07-22 18:08:00 【IC learners】
Content
Instantiation and reset and the generation of clock signals are commonplace , It mainly depends on task Generation and invocation of , Test with results .
task
//==========================================
//driver task
task spi_cmd ;
input [15:0] data_send ;
begin
wait(ready) ;
@(posedge clk_200mhz) ;
# 0.7 ;
tx_data = data_send ;
tx_data_en = 1'b1 ;
@(posedge clk_200mhz) ;
# 0.7 ;
tx_data_en = 1'b0 ;
tx_data = 'b0 ;
wait(ready) ;
end
endtask // spi_rw
Defines his class , The name is spi_cmd
With input parameters , Written in begin front
wait Said wait ready High level
@(posedge clk_200mhz) ;
Wait for the rising edge of the clock
#0.7 ; Analog clock delay
Send the start signal and corresponding data
Wait for the next clock edge to send the end signal
The last wait ready High level did not understand , Maybe it's for robustness
Last endtask end
In the middle of the begin and end Make a bold guess , Don't let it be
//driver
initial begin
tx_data = 16'b0 ;
tx_data_en = 1'b0 ;
//(1) wr address: 100-102
#133.7 ; spi_cmd({1'b1, 7'd100, 8'hAA}) ;
#501.3 ; spi_cmd({1'b1, 7'd101, 8'h55}) ;
#501.3 ; spi_cmd({1'b1, 7'd102, 8'hA5}) ;
//(2) rd address: 102-100
#2001.3 ; spi_cmd({1'b0, 7'd102, 8'h0}) ;
#501.3 ; spi_cmd({1'b0, 7'd101, 8'h0}) ;
#501.3 ; spi_cmd({1'b0, 7'd100, 8'h0}) ;
end
call task
tx Two lines are to assign initial values
task The call format of is similar to c Functions with parameters in language
# The semicolon after the time can be added or not
Result verification
//==========================================
//finish
reg err_flag ;
initial begin
err_flag = 0 ;
#100;
//1st read
@(posedge rdata_valid) ;
@(negedge clk_200mhz) ;
if (rdata != 8'ha5) err_flag |= 1;
//2nd read
@(posedge rdata_valid) ;
@(negedge clk_200mhz) ;
if (rdata != 8'h55) err_flag |= 1;
//3rd 3read
@(posedge rdata_valid) ;
@(negedge clk_200mhz) ;
if (rdata != 8'haa) err_flag |= 1;
#13.7 ;
$display("-------------------------");
if (err_flag !== 0) begin
$display("Simulation Failed!");
end
else begin
$display("Simulation Succeed!");
end
$display();
$display();
#1000 ;
$finish ;
end
Define the error flag and assign the initial value
Wait for a valid signal sign
Wait for clock edge ==( I don't know why I use the falling edge )==
Judge whether the data is consistent with the input data
Repeat
The lower part is output
Source code
`timescale 1ns/1ps
module test ;
reg clk_200mhz ;
reg rstn ;
reg [15:0] tx_data ;
reg tx_data_en ;
wire sclk, csn, mosi, miso ;
wire [7:0] rdata ;
wire rdata_valid ;
wire ready ;
//==========================================
// clk and reset
initial begin
clk_200mhz = 0 ;
rstn = 0 ;
#11.3 rstn = 1 ;
end
always #(2.5) clk_200mhz = ~clk_200mhz ;
//==========================================
//driver task
task spi_cmd ;
input [15:0] data_send ;
begin
wait(ready) ;
@(posedge clk_200mhz) ;
# 0.7 ;
tx_data = data_send ;
tx_data_en = 1'b1 ;
@(posedge clk_200mhz) ;
# 0.7 ;
tx_data_en = 1'b0 ;
tx_data = 'b0 ;
wait(ready) ;
end
endtask // spi_rw
//==========================================
//driver
initial begin
tx_data = 16'b0 ;
tx_data_en = 1'b0 ;
//(1) wr address: 100-102
#133.7 ; spi_cmd({1'b1, 7'd100, 8'hAA}) ;
#501.3 ; spi_cmd({1'b1, 7'd101, 8'h55}) ;
#501.3 ; spi_cmd({1'b1, 7'd102, 8'hA5}) ;
//(2) rd address: 102-100
#2001.3 ; spi_cmd({1'b0, 7'd102, 8'h0}) ;
#501.3 ; spi_cmd({1'b0, 7'd101, 8'h0}) ;
#501.3 ; spi_cmd({1'b0, 7'd100, 8'h0}) ;
end
//finish
reg err_flag ;
initial begin
err_flag = 0 ;
#100;
//1st read
@(posedge rdata_valid) ;
@(negedge clk_200mhz) ;
if (rdata != 8'ha5) err_flag |= 1;
//2nd read
@(posedge rdata_valid) ;
@(negedge clk_200mhz) ;
if (rdata != 8'h55) err_flag |= 1;
//3rd 3read
@(posedge rdata_valid) ;
@(negedge clk_200mhz) ;
if (rdata != 8'haa) err_flag |= 1;
#13.7 ;
$display("-------------------------");
if (err_flag !== 0) begin
$display("Simulation Failed!");
end
else begin
$display("Simulation Succeed!");
end
$display();
$display();
#1000 ;
$finish ;
end
spi_master u_spi_master (
.rstn (rstn),
.clk (clk_200mhz),
//parallel
.tx_data (tx_data),
.tx_data_en (tx_data_en),
.ready (ready),
//spi intf
.sclk (sclk),
.csn (csn),
.mosi (mosi),
.miso (miso),
.rdata (rdata),
.rdata_valid (rdata_valid)
);
spi_slave u_spi_slave (
.sclk (sclk),
.csn (csn),
.mosi (mosi),
.miso (miso));
endmodule
边栏推荐
猜你喜欢
毕业985,工作996,也躲不开中年危机
Web3 sharing
智汇华云 | 集群日志动态采集方案
(11) 51 Single Chip Microcomputer -- realize the storage of stopwatch data with AT24C02 (attached with achievement display)
日期类的理解学习
RK3399平台开发系列讲解(内存篇)15.33、为什么可用内存会远超物理内存?
数据高效治理如何开展,指标管理与数据溯源来帮忙!
[case sharing] configure the routing penetration function of IS-IS
学IT,你后悔了么?
[opencv introduction practice] use the front camera of the computer for face detection
随机推荐
Branch and loop statements
会议OA项目之会议发布功能
02-线性结构2 一元多项式的乘法与加法运算(链表求解)
subprocess
MySQL constraints
How does boss direct hire write an excellent resume?
C language branch structure and loop structure (1)
等额本金递增还款/等额本金递减按揭房贷还款计算器
微信小程序学习五种页面跳转方法
pushgateway安装及Prometheus配置
方法论(一):如何快速学习一门编程语言
How to declare the default value in go language structure and how to convert it into JSON data
网络之物理层
Bigder:37/100 一个误操作
MySQL数据库结合项目实战SQL优化总结
How Allegro imports pictures such as high-definition logo, QR code, anti-static logo and Chinese characters
Unity TextMeshPro命名空间引用及组件获取
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
数据高效治理如何开展,指标管理与数据溯源来帮忙!
智汇华云 | 集群日志动态采集方案