集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 1073|回复: 1

Verilog HDL 基本语法回顾

[复制链接]
fpga_feixiang 发表于 2019-2-17 14:30:33 | 显示全部楼层 |阅读模式
module的结构
/*
VHDL是由模块组成,嵌在module endmodule之间,其他语句均由 ';'  结束
*/
module add(a,b,c,sum,count);  //模块端口定义
    input [2:0] a,b;        
    input cin;
    output [2:0] sum;      
    output count;           //IO 定义
        //内部变量定义
        assign {count,sum} = a + b + cin;  // 功能定义
endmodule
1
2
3
4
5
6
7
8
9
10
11
功能块
常用的是 assign(组合逻辑电路)和 always(时序电路)

//简单的计数器   
module count (clk,reset,x,num);
input reset,clk,x;
output [1:0]num;
reg [1:0]num;
always @ (posedge clk)   //上升沿
begin
if(reset)
    num=2'b00;
else
    case (num)
        2'b00 : if(x==0) num<=2'b01;
                else num<=2'b11;
        2'b01 : if(x==0)  num<=2'b10;
                else num<=2'b00;
        2'b10 : if(x==0) num<=2'b11;
                else num<=2'b01;
        2'b11 : if(x==0) num<=2'b00;
                else num<=2'b10;
        endcase
end
endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
assign和 always 块的逻辑功能是同时进行(并行)的,always内的逻辑是顺序执行的.

Verilog HDL中没有{ } 以 begin end 取代

数据类型
数字的表达方式
<位宽>’<进制><对应的进制数字>

不同位宽应分别定义

8'b0001_0101 //位宽为8位的二进制数字表示,使用下划线可以提高可读性(对数字的大小没有影响)
8'h15       //位宽为8位的十六进制数字表示
1
2
变量类型
wire型
wire 类型用以 assign 操作的组合逻辑信号

默认为wire型

wire a;  //1位的wire型变量
wire[7:0] b; //8位的wire型变量
wire[7:0] c,d; //两个8位的wire型变量
1
2
3
寄存器类型 reg型
寄存器类型是对数据存储单元的抽象,在 always 块内定义的变量都必须是reg型,可以这么理解:reg型就是在always内使用的变量类型,并不一定是寄存器或者触发器的输出

运算符
基本算术运算符: + - * / %
关系运算符: > < >= <= ==
赋值运算符: =  <=
逻辑运算符: && || !(非)
位运算符: & | ~(反) `(亦或)
拼接符: { }
1
2
3
4
5
6
非阻塞赋值<= : 块结束后,才完成赋值;值不是立即改变的;在always块中常使用此种方法。

阻塞赋值 : 立即完成赋值,赋值结束后才能结束块。可能会产生意想不到的错误。

基本流程语句
条件语句
if else
1
case
case(变量)
    变量值1:
            begin
                执行语句
            end
    变量值2:
    default:
endcase
1
2
3
4
5
6
7
8
循环语句
for()
1
Exemple
//模4计数器
module counter4(X,clk,Z,L);
    input X,clk;
    output Z,L;
    reg[1:0] L;
    parameter S0=2'b00,S1=2'b01,S2=2'b10,S3=2'b11;
    always @(posedge clk)
        begin
            if(X==1)
                case(L)
                S0: L<=S1;
                S1: L<=S2;
                S2: L<=S3;
                S3: L<=S0;
                endcase
            else
                case(L)
                S0: L<=S3;      /*仿真时间、always语句和assign的位置*/
                S1: L<=S0;
                S2: L<=S1;
                S3: L<=S2;              
                endcase
        end
    assign Z=((X==1&&L==S3)?1:0)|((X==0&&L==S0)?1:0);
endmodule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//010序列检查器
module test010(in,out,state,clk,reset);
input in,clk,reset;
output out;
output[2:0]state;
reg[2:0]state;
reg out;
parameter s0='d0,s1='d1,s2='d2;
always @(posedge clk)
  begin
    if(reset) begin state<=s0; out<=0; end      /*控制in的输入*/
    else case(state)
    s0:begin
       if(in==0) begin state<=s1; out<=0; end
       else begin state<=s0; out<=0; end
    end
    s1:begin
      if(in==1) begin state<=s2; out<=0; end
      else begin state<=s0; out<=0; end
    end        
    s2:begin
      if(in==0) begin state<=s0; out<=1; end
      else begin state<=s0; out<=0; end
    end
    default: state<=s0;
    endcase
  end
endmodule
---------------------
zhangyukun 发表于 2019-2-18 09:14:55 | 显示全部楼层
Verilog HDL 基本语法回顾
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|集成电路技术分享 ( 京ICP备20003123号-1 )

GMT+8, 2024-4-26 19:20 , Processed in 0.065929 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表