fpga_feixiang 发表于 2019-2-20 14:01:05

verilog 基础语法

1 模块结构
    端口: module 模块名(端口1, 端口2, 端口3)
    内容:
        I/O说明:
            input 端口名;
            output 端口名;
        内部信号:
            reg   r变量1,r变量2;
            wire w变量1,w变量2;
        功能定义:
            a. assign 连线
                assign a = b&c;
            b. 实例化其他元件
                and and_inst(q, a, b);
            c. always模块
                always @(posedge clk or posedge clr)
                begin
                    if(clr) 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q <= 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(en)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; q <= d;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end


2.数据类型 常量 变量
&nbsp; &nbsp; 常量:
&nbsp; &nbsp; &nbsp; &nbsp; 整数:<位宽 num'><进制 b|o|d|h><数字>,例如 4'b1010
&nbsp; &nbsp; &nbsp; &nbsp; x值(不定值)和z值(高阻值,也可用?代替)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x和z可以标识某一位或者某一个数字
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4'b10x0,4'bx,4'b101z,4'bz,4'b?
&nbsp; &nbsp; &nbsp; &nbsp; 负数:整数最前面加-
&nbsp; &nbsp; &nbsp; &nbsp; 下划线:分割数字部分,更加易读(8'b1000_1000)
&nbsp; &nbsp; 参数:parameter
&nbsp; &nbsp; &nbsp; &nbsp; parameter 参数名=表达式;
&nbsp; &nbsp; &nbsp; &nbsp; 表达式只能是数字或者定义过的参数
&nbsp; &nbsp; 变量:
&nbsp; &nbsp; &nbsp; &nbsp; wire型:wire 数据名;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wire表示信号,常用来表示assign关键字指定的组合逻辑信号
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wire型信号可以用作输入,输出
&nbsp; &nbsp; &nbsp; &nbsp; reg型:reg 数据名;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 对存储单元的抽象
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 常用来表示always模块内的指定信号,常代表触发器
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; always块内被赋值的每一个信号都必须定义为reg型
&nbsp; &nbsp; &nbsp; &nbsp; memory型:reg 存储器名;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reg 表示基本存储单元的大小
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 存储器名表示基本存储单元的个数,存储空间的容量
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 对存储器进行地址索引的表达式必须是常数表达式
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 一个n位寄存器可以在一条赋值语句里进行赋值,而一个完整的存储器不行
&nbsp; &nbsp; 运算符及表达式:
&nbsp; &nbsp; &nbsp; &nbsp; 基本运算符:+ - * / %
&nbsp; &nbsp; &nbsp; &nbsp; 位运算符:~ & | ^ ^~
&nbsp; &nbsp; &nbsp; &nbsp; 逻辑运算符:&& || !
&nbsp; &nbsp; &nbsp; &nbsp; 关系运算符:< > <= >=
&nbsp; &nbsp; &nbsp; &nbsp; 等式运算符:== != (不管x、z,结果可能是不定值)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; === !==(对参数的x、z都进行比较)
&nbsp; &nbsp; &nbsp; &nbsp; 移位运算符:<< >>
&nbsp; &nbsp; &nbsp; &nbsp; 位拼接运算符:{ },将几个信号拼接起来,例如{a,b,w,3'b100}
&nbsp; &nbsp; &nbsp; &nbsp; 缩减运算符:C =&B;C =|B;C =^B;
&nbsp; &nbsp; &nbsp; &nbsp; 优先级别:和c语言差不多,加括号
&nbsp; &nbsp; 赋值语句:
&nbsp; &nbsp; &nbsp; &nbsp; 1)非阻塞赋值方式(b <= a)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.块结束才完成赋值
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.b的值不是立刻就改变的
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.在可综合的模块中常用
&nbsp; &nbsp; &nbsp; &nbsp; 2)阻塞赋值方式(b = a)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.赋值语句执行完成后,块才结束
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.b的值在赋值语句执行后立刻改变
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.可能会产生意想不到的结果
&nbsp; &nbsp; &nbsp; &nbsp; 简单理解:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 非阻塞赋值用了多个触发器,每次时钟到达,所有触发器都触发一次
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 阻塞赋值连到同一个触发器上,时钟到达,导致所有寄存器被赋值


&nbsp; &nbsp; 块语句:
&nbsp; &nbsp; &nbsp; &nbsp; 顺序块:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)块内顺序执行
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)每条语句的延迟是相对于前一条语句的仿真时间(语句前#num)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)直到最后一句执行完,流程控制才跳出该块
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句n;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 或
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin:块名:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 块内声明;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句n;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; 并行块:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)块内是同时执行的
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)语句的延迟是相对于程序流程控制进入块内时的仿真时间
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)延迟时间是用来给赋值语句提供时序的
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4)时序最后的语句执行完,或者disable语句执行时,跳出程序块
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fork
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句n;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 或
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fork:块名:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 块内声明;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句n;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; 块名:可以给每一块取名,将名字加在begin和fork之后
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)可以在块内定义局部变量
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)可以被其他语句调用
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)在verilog中,所有变量静态(都有唯一地址)
&nbsp; &nbsp; &nbsp; &nbsp; 起始时间和结束时间:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 并行块和顺序块中有起始时间和结束时间
&nbsp; &nbsp; &nbsp; &nbsp; 条件语句:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)if...else 语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(表达式)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句|语句块
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语句|语句块
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)case语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case|casez(case?)|casex,最常用的casez
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case(控制表达式)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 分支表达式1:语句|语句块
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 分支表达式n:语句|语句块
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: 语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endcase
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 分支表达式的值的位宽必须相等,需要指明位宽
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用if时,最好也要使用else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用case时,最好用上default
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; 循环语句:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)forever&nbsp; 连续的执行语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forever&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 常用来生成周期型波形,用来作为仿真测试信号
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 不能独立写在程序中,必须写在initial块中
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)repeat&nbsp; &nbsp; 执行一条语句n次
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat(常量表达式)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)while:执行一条语句直到某个条件不满足(如果一开始条件不满足,则一次也不执行)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(表达式)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4)for
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a) 循环次数变量初始值
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b) 循环表达式判断
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c) 执行语句修正循环次数变量
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 使用方法和c语言基本一致
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(表达式1;表达式2;表达式3)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; 结构说明:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial语句:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial在仿真一开始就执行,但是只执行一次
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; always语句:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; always <控制时序> <语句>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;always 在仿真一开始就执行,always语句会不断重复执行,所以需要时序的控制
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 不加时序控制,会不停重复执行,形成仿真死锁
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //边沿触发
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; always @(postedge clock or postedge reset)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //电平触发
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; always @(postedge clock or postedge reset)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 边沿触发的always块常常用来描述时序逻辑
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 一个模块中可以有多个always块,他们并行执行
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; task语句:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 任务定义:&nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; task <任务名>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <端口及数据类型声明语句>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <语句1>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <语句n>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endtask
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 任务调用:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <任务名> (端口1,...端口n)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例子:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //定义
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; task mytask;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input a,b;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inout c;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output d,e;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <语句>&nbsp; &nbsp; //执行任务工作相应的语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c=foo1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d=foo2;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e=foo3;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endtask
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //调用
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mytask(v,w,x,y,z);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function语句:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function定义:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function <返回值的类型或范围> (函数名);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <端口说明语句>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <变量类型说明语句>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多条语句;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endfunction
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 函数调用:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <函数名> (<表达式1>,...,<表达式n>)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 函数的使用规则:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)函数定义中不能包含有任何的时间控制语句
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)函数不能启动任务
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)定义函数时至少有一个输入参数
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4)在函数的定义中必须有一条赋值语句给函数中的一个内部变量赋以函数的结果值
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 该函数变量具有和函数名相同的名字
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例子:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //函数定义
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function getbyte;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input address;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <说明语句>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getbyte = result_expression;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endfunction
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //函数调用:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; word = control ? {getbyte(mybyte),getbyte(mybyte)} : 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; 系统函数和任务:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; verilog语言中每个系统函数和任务前面都用一个标识符$来加以确认
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notice:参数为",,",表示空参数,输出时显示空格
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1)$display 和 $write
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 作用:按照指定格式输出,p1给出格式,p2...pn按序输出
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $display输出后会自动换行,$write不换行
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用法基本和c语言的printf一致
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 格式:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $display(p1,p2,...pn);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $write(p1,p2,...pn);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例子:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $display("rval=%h hex %d decimal",rval,rval);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2)系统任务 $monitor
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用处:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 提供了监控和输出参数列表中的表达式或变量值的功能
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 仿真器建立了一种机制,使得每当参数列表中变量或者表达式的值发生变化时,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 整个参数列表中变量或表达式的值都将输出显示。
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 任何时刻只能有一个$monitor起作用,因此需配合$monitoron和$monitoroff使用,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 把需要监视的模块用$monitoron打开,在监视完毕后及时用$monitoroff关闭
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 格式:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $monitor(p1,p2,...pn);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $monitor;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $monitoron;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $monitoroff;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例子:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $monitor($time,,"rxd=%b txd=%b",rxd,txd);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3)时间度量系统函数$time
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; verilog语言中支持两种时间函数$time和$realtime
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $time返回一个64比特值的整数来表示当前仿真时刻值
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 返回的总是时间尺度的倍数,并且会取整数
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例子:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `timescale 10ns/1ns
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; module test
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reg set;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter p=1.6;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $monitor($time,,"set=",set);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #p set=0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #p set=1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endmodule
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $realtime返回的时间数字是个一个实型数
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 和$time一样都是以时间尺度为单位
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4)系统任务 $finish
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $finish的作用是退出仿真器,返回主操作系统
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $finish有3个参数,输出的特征信息一次变多
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 参数:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0:不输出信息
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1:输出当前的仿真时刻和位置
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2:输出当前的仿真时刻和位置,在仿真过程中
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 所用的memory及CPU时间的统计
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5)系统任务 $stop
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 把EDA工具(如仿真器)设置成暂停模式,在仿真环境中给出一个交互式的
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 命令提示符,将控制权交给用户
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 和$finish一样,参数为0,1,2,数越大,信息越多
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6)系统任务 $readmemb,$readmemh
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 用来从文件中读取数据到寄存器
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 读取的文件有格式要求(具体查书)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 数据文件的每个被读取的数字都被存放到地址连续的存储器单元中
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 每个数据的存放地址在数据文件中进行说明
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 二进制数字读取
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $readmemb("<数据文件名>",<存储器名>);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $readmemb("<数据文件名>",<存储器名>,<起始地址>);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $readmemb("<数据文件名>",<存储器名>,<起始地址>,<结束地址>);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 16进制数字读取
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $readmemh("<数据文件名>",<存储器名>);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $readmemh("<数据文件名>",<存储器名>,<起始地址>);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $readmemh("<数据文件名>",<存储器名>,<起始地址>,<结束地址>);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 例子:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reg mem;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial $readmemh("mem.data",mem);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial $readmemh("mem.data",mem,16);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initial $readmemh("mem.data",mem,128,1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7)系统任务 $random
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 一个产生随机数的手段,函数返回一个32bit的随机数
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $random的一般用法 $random % num,返回一个在(-num+1,num-1)范围
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 内的随机数
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reg rand;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand = {$random} % 60; //利用拼接返回一个0到59之间的数
&nbsp; &nbsp; &nbsp; &nbsp; 编译预处理:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;宏定义 `define
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`define 标识符(宏名) 字符串内容(宏内容)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; “文件包含”处理 `include&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 一个源文件可以将另外一个源文件全部包含起来
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 时间尺度:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `timescale <时间单位>/<时间尺度>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 条件编译:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `ifdef
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `endif

大鹏 发表于 2019-2-21 09:25:00

verilog 基础语法

大鹏 发表于 2022-4-7 13:02:57

verilog 基础语法
页: [1]
查看完整版本: verilog 基础语法