verilog 常用系统函数及例子
1.打开文件
integer file_id;
file_id = fopen("file_path/file_name");
2.写入文件:$fmonitor,$fwrite,$fdisplay,$fstrobe
//$fmonitor只要有变化就一直记录
$fmonitor(file_id, "%format_char", parameter);
$fmonitor(file_id, "%m: %t in1=%d o1=%h", $time, in1, o1);
//$fwrite需要触发条件才记录
$fwrite(file_id, "%format_char", parameter);
//$fdisplay需要触发条件才记录
$fdisplay(file_id, "%format_char", parameter);
$fstrobe();
3.读取文件:$fread
integer file_id;
file_id = $fread("file_path/file_name", "r");
4.关闭文件:$fclose
$fclose(fjile_id);
5.由文件设定存储器初值:$readmemh,$readmemb
$readmemh("file_name", memory_name"); //初始化数据为十六进制
$readmemb("file_name", memory_name"); //初始化数据为二进制
6、文件显示:$monitor,$write,$display
$display,$write用于输出信息
$display("rvel = %h hex %d decimal",rvel,rvel);
$monitor($time, ,"rxd = %b txd = %b",rxd ,txd)
6、文件定位
$fseek,文件定位,可以从任意点对文件进行操作;
$fscanf,对文件一行进行读写。
7、退出仿真器$finish
8、随机数据产生:$random
下面是一些常见的应用:
1、读写文件
`timescale 1 ns/1 ns
module FileIO_tb;
integer fp_r, fp_w, cnt;
reg [7:0] reg1, reg2, reg3;
initial begin
fp_r = $fopen("data_in.txt", "r");
fp_w = $fopen("data_out.txt", "w");
while(!$feof(fp_r)) begin
cnt = $fscanf(fp_r, "%d %d %d", reg1, reg2, reg3);
$display("%d %d %d", reg1, reg2, reg3);
$fwrite(fp_w, "%d %d %d\n", reg3, reg2, reg1);
end
$fclose(fp_r);
$fclose(fp_w);
end
endmodule
2、
integer file, char;
reg eof;
initial begin
file = $fopenr("myfile.txt");
eof = 0;
while (eof == 0) begin
char = $fgetc(file);
eof = $feof (file);
$display ("%s", char);
end
end
3、文件处理定位
`define SEEK_SET 0
`define SEEK_CUR 1
`define SEEK_END 2
integer file, offset, position, r;
r = $fseek(file, 0, `SEEK_SET);
r = $fseek(file, 0, `SEEK_CUR);
r = $fseek(file, 0, `SEEK_END);
r = $fseek(file, position, `SEEK_SET);
4、
integer r, file, start, count;
reg [15:0] mem[0:10], r16;
r = $fread(file, mem[0], start, count);
r = $fread(file, r16);
5、
integer file, position;
position = $ftell(file);
6、
integer file, r, a, b;
reg [80*8:1] string;
file = $fopenw("output.log");
r = $sformat(string, "Formatted %d %x", a, b);
r = $sprintf(string, "Formatted %d %x", a, b);
r = $fprintf(file, "Formatted %d %x", a, b);
7、
integer file, r;
file = $fopenw("output.log");
r = $fflush(file);
8、
// This is a pattern file - read_pattern.pat
// time bin dec hex
10: 001 1 1
20.0: 010 20 020
50.02: 111 5 FFF
62.345: 100 4 DEADBEEF
75.789: XXX 2 ZzZzZzZz
`timescale 1ns / 10 ps
`define EOF 32'hFFFF_FFFF
`define NULL 0
`define MAX_LINE_LENGTH 1000
module read_pattern;
integer file, c, r;
reg [3:0] bin;
reg [31:0] dec, hex;
real real_time;
reg [8*`MAX_LINE_LENGTH:0] line;
initial
begin : file_block
$timeformat(-9, 3, "ns", 6);
$display("time bin decimal hex");
file = $fopenr("read_pattern.pat");
if (file == `NULL) // If error opening file
disable file_block; // Just quit
c = $fgetc(file);
while (c != `EOF)
begin
if (c == "/")
r = $fgets(line, `MAX_LINE_LENGTH, file);
else
begin
// Push the character back to the file then read the next time
r = $ungetc(c, file);
r = $fscanf(file," %f:\n", real_time);
// Wait until the absolute time in the file, then read stimulus
if ($realtime > real_time)
$display("Error - absolute time in file is out of order - %t",
real_time);
else
#(real_time - $realtime)
r = $fscanf(file," %b %d %h\n",bin,dec,hex);
end // if c else
c = $fgetc(file);
end // while not EOF
r = $fcloser(file);
end // initial
// Display changes to the signals
always @(bin or dec or hex)
$display("%t %b %d %h", $realtime, bin, dec, hex);
endmodule // read_pattern
9、自动比较输出结果
`define EOF 32'hFFFF_FFFF
`define NULL 0
`define MAX_LINE_LENGTH 1000
module compare;
integer file, r;
reg a, b, expect, clock;
wire out;
reg [`MAX_LINE_LENGTH*8:1];
parameter cycle = 20;
initial
begin : file_block
$display("Time Stim Expect Output");
clock = 0;
file = $fopenr("compare.pat");
if (file == `NULL)
disable file_block;
r = $fgets(line, MAX_LINE_LENGTH, file); // Skip comments
r = $fgets(line, MAX_LINE_LENGTH, file);
while (!$feof(file))
begin
// Wait until rising clock, read stimulus
@(posedge clock)
r = $fscanf(file, " %b %b %b\n", a, b, expect);
// Wait just before the end of cycle to do compare
#(cycle - 1)
$display("%d %b %b %b %b", $stime, a, b, expect, out);
$strobe_compare(expect, out);
end // while not EOF
r = $fcloser(file);
$stop;
end // initial
always #(cycle / 2) clock = !clock; // Clock generator
and #4 (out, a, b); // Circuit under test
endmodule // compare
10、从文件中读数据到mem(这个好像一般人用的最多了)
`define EOF 32'HFFFF_FFFF
`define MEM_SIZE 200_000
module load_mem;
integer file, i;
reg [7:0] mem[0:`MEM_SIZE];
reg [80*8:1] file_name;
initial
begin
file_name = "data.bin";
file = $fopenr(file_name);
i = $fread(file, mem[0]);
$display("Loaded %0d entries \n", i);
i = $fcloser(file);
$stop;
end endmodule // load_mem
verilog 常用系统函数及例子的更多相关文章
- Delphi常用系统函数总结
Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...
- ylb:SQLServer常用系统函数-字符串函数、配置函数、系统统计函数
原文:ylb:SQLServer常用系统函数-字符串函数.配置函数.系统统计函数 ylbtech-SQL Server:SQL Server-SQLServer常用系统函数 -- ========== ...
- Linux常用系统函数
Linux常用系统函数 一.进程控制 fork 创建一个新进程clone 按指定条件创建子进程execve 运行可执行文件exit 中止进程_exit 立即中止当前进程getdtablesize 进程 ...
- 【SQL Server】SQL常用系统函数
SQL常用系统函数 函数类型 函数表达式 功能 应用举例 字符串函数 SubString(表达式,起始,长度) 取子串 SubString('ABCDEFG',3,4) Right(表达式,长度) 右 ...
- Verilog学习笔记基本语法篇(十一)········ 常用系统函数
1)系统任务:$monitor 格式: $monitor(p1,p2,p3...pn); $monitor; $monitoron; $monitoroff; 任务$monitor提供了监控输出列 ...
- Win32汇编常用系统函数
汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操作码,用地 ...
- Verilog学习笔记基本语法篇(十)········ 常用系统函数
$display 和 $write 任务 格式: $display (p1,p2,...,pn); $write (p1,p2,..,pn); 这两个函数和系统的任务作用是用来输出信息,即将参数p2到 ...
- oracle常用系统函数
一.字符类函数 字符类函数是专门用于字符处理的函数,处理的对象可以是字符或者字符串常量,也可以是字符类型的列. 1.ASCII(c)和CHR(i) ASCII(c)函数用于返回一个字符的ASCII码, ...
- SQL Server 2008 R2 常用系统函数学习
/******************************************* * 聚合函数 *******************************************/ SEL ...
随机推荐
- nginx-rrd监控nginx访问数
一 .查看已安装的nginx是否包含stub_status模块 /usr/local/nginx/sbin/nginx -V nginx version: Nginx/1.2.0 configure ...
- 延期(deferred)的承诺(promise) — jq异步编程浅析
引子 相信各位developers对js中的异步概念不会陌生,异步操作后的逻辑由回调函数来执行,回调函数(callback function)顾名思义就是“回头调用的函数”,函数体事先已定义好,在未来 ...
- C# System.Attribute(验证类)
本文以一个项目中通用的验证类来举例说明如何使用自定义Attribute来扩展元数据. 在项目中,我们为了保证各个层次之间的松藕合,通常把在各个层次之间传递数据的封装在一个称为实体类的类中,比如Act ...
- Android 再按一次退出程序
实现代码: private long exitTime = 0; /** * 捕捉返回事件按钮 * * 因为此 Activity 继承 TabActivity 用 onKeyDown 无响应,所以改用 ...
- MongoDB之二(增删查改)
一: Insert操作 上一篇也说过,文档是采用“K-V”格式存储的,如果大家对JSON比较熟悉的话,我相信学mongodb是手到擒来,我们知道JSON里面Value 可能是“字符串”,可能是“数组” ...
- MySQL运行原理与基础架构
1.MySQL基础 MySQL是一个开放源代码的关系数据库管理系统.原开发者为瑞典的MySQL AB公司,最早是在2001年MySQL3.23进入到管理员的视野并在之后获得广泛的应用. 2008年My ...
- boost总结之any
boost中any库相对variant简单,any可以不限定类型,variant中对我们事先会定义好我们所需的类型,但是any无此限制,any的类型检测是在run time时. boost::an ...
- xcodebuild导出ipa方法
xcode 5.x版本导出ipa是不需要开发者账号,而xcode6以后导出ipa必须要求选择开发者team,无法绕开,但我们使用xcodebuild命令行可以无视这个限制 环境: mac osx 10 ...
- Fatjar成功安装记录
1.FatJar安装方式 (1)在线安装 具体网址http://kurucz-grafika.de/fatjar (2)离线安装 将fatjar的jar放到plugins文件夹中,重启 2. 安装失败 ...
- NOIP2015 运输计划(二分+LCA+差分)
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 308 Solved: 208[Submit][Status] ...