搜档网
当前位置:搜档网 › Verilog程序代码集

Verilog程序代码集

Verilog程序代码集
Verilog程序代码集

1.全加器

Sum=A⊕B⊕Cin

Count=AB+Cin(A+B)

①数据流

module adder(a,b,Cin,Sum,Count); input [2:0]a,b;

input Cin;

output [2:0] Sum;

output Count;

assign {Count,Sum}=a+b+Cin; endmodule

②行为描述always语句

module adder(a,b,c,Cin,Sum,Count); input [4:0] a,b;

input Cin;

output reg [4:0] Sum;

output reg Count;

reg T1,T2,T3;

always@(a or b or Cin)

begin

Sum=a^b^Cin;

T1=A&B;

T2=Cin&A;

T3=Cin&B;

Count=T1|T2|T3;

end

endmodule

③结构体

module adder (a,b,Cin,Sum,Count);input a,b,Cin;

output Sum,Count;

Xor a1(s1,a1,b);

Xor a2(Sum,s1,Cin);

and a3(T1,a,b);

or a4(T2,a,b);

and a5(T3,Cin,T2);

or a6(Count,T1,T3);

Endmodule

2.数值比较器

①判断两值是否相等

module compare(a,b,equal);

input [7:0] a,b;

output equal;

assign equal=(a==b)?|0; ②谁大谁输出

module compare(a,b,out);

input [7:0] a,b;

output reg[7:0] out;

always@(a or b)

begin

if (a>b) out<=a;

else if (a==b) out<=a;

else out<=b;

end

endmodule

③输出参数

module compare(a.b.xgy,xsy,xey);

input [7:0] x,y;

output reg xgy,xsy,xey;

always@(x or y)

begin

if (x==y) xey=1;

else xey=0;

if (x>y) begin xgy=1;xsy=0;end

else if (x

endmodule

3.编码器(4-2 8-3 16-4编码)

①case语句8-3编码(优先)

module code (in ,out);

input [7:0] in;

output reg [2:0] out;

always@(in)

case x(in)

begin f=1;8?b1xxxxxxx:out=3?b111;end begin f=1;8?b01xxxxxx:out=3?b110;end begin f=1;8?b001xxxxx:out=3?b101;end begin f=1;8?b0001xxxx:out=3?b100;end begin f=1;8?b00001xxx:out=3?b011;end begin f=1;8?b000001xx:out=3?b010;end begin f=1;8?b0000001x:out=3?b001;end begin f=1;8?b00000001:out=3?b000;end default:begin f=0:out=3?b000;end endcase

endmodule

②if-else语句(4-2优先编码)

module code(in,out);

input[3:0] in;

output reg [1:0] out;

always@(in)

if (in[3]==1):begin f=1;out=2?b11;end else if (in[2]==1):begin f=1;out=2?b10;end else if (in[1]==1):begin f=1;out=2?b01;end else if (in[0]==1):begin f=1;out=2?b00;end else begin f=0;out=2?b00;end endmodule

4.多路选择器

①行为描述(4选1)

module choice(A,B,C,D,ncs addr out);

input [7:0]A,B,C,D;

input ncs;

input [1:0] addr ;

output reg[7:0] out;

always@(A or B or C or D or ncs or addr) begin

if (!ncs)

case(addr)

2?b00:out=A;

2?b01:out=B;

2?b10:out=C;

2?b11:out=D;

endcase

else out=0;

end

endmodule

5.设计一个4位双向移位寄存器。module shift(sout,out,clk,in,sin,d,load); input clk,sin,d,load;input[3:0]in;

output sout;output[3:0]out;

reg[3:0]out;

always @(posedge clk)

if(load) out<=in;

else

if(d==0)

begin

out<=out>>1;out[3]<=sin;sout<=out[0];end else

begin

out<=out<<1;out[0]<=sin;sout<=out[3];end 6.11111010000序列检测器module shift(q,s,d,clk);

output[11:0] q;

output s;

input d;

input clk;

reg[11:0] q;

reg s;

always @(posedge clk)

begin

q<=q<<1;q[0]<=d;

end

always @(posedge clk )

if(q==12'b 11111010000)

s<=1;

else

s<=0;

Endmodule

7.计数器

①计数分频器

8分频占空比1:1

module div(clk8,clk,rst);

input clk,rst;

output reg clk8;

reg [2:0] count;

always@(posedge clk)

begin

if (rst) begin

clk8<=0;count<=0;end else begin

if (count==7) count<=0;

else count<=count+1;

if (count<=3) clk_8<=0;

else clk_8<=1;

end

end

endmodule

②预置数计数

module setcount(clk,rst,F,out);

input clk,rst,F;

output reg out;reg[3:0]count; always@(posedge clk or posedge rst) begin

if (rst) begin out<=0;count<=0;end else if(F==1) count<=10;

else if (count>=15) begin

Count<=o; out<=1;end

else begin

count<=count+1; out<=0;end end

endmodule

8.触发器

T=1时翻转

①T触发器

T=0时保持翻转

module D_FF(T,clk,rst,Q,Q n);

input T,clk,rst;

output reg Q, Q n ;

always@(posedge clk or posedge rst) if (rst) begin

Q<=0;Q n<=1;end

else if(T) begin

Q<=~Q Q n<=~Q n ; end

else begin

Q<=Q; Q n<=Q n ; end endmodule

②D触发器Q n+1=D(保持D)module D_FF(D,clk,rst,Q,Q n);

input D,clk,rst;

output reg Q,Q n;

always@(posedge clk or posedge rst) ef (rst) begin Q<=0; Q n<=1;end else begin Q<=D; Q n<=~D;end endmodule ③JK触发器

module JK_FF(J,K,clk,rst,Q,Q n);

input J,K,clk,rst;

output Q,Q n ;

always@(posedge clk or posedge rst)

if(rst) begin

Q<=0; Q n<=1 end

else if (J==1&&K==0) begin

Q<=1; Q n<=0; end

else if ((J==0&&K==1) begin

Q<=0; Q n<=1; end

else if ((J==0&&K==0) begin

Q<=Q ; Q n<=Q n ; end

else begin

Q<=~Q ; Q n =~Q n ; end

endmodule

9.测试程序(异或门测试)

… timescale 1ns/1ps

module test;

reg c,a,b;

always@(a or b)

c=a^b;

initial begin

a=0; b=0;

#10 b=1;

#10 a=1;

#10 b=0; end

always@(a or b or c)

$display(“a=%d,b=%d,c=%d,a,b,c)

endmodule

J=1 K=0时Q=1 置1

J=0 K=1时Q=0 置0

J=K=0时Q=Q 保持

J=K=1时Q=~Q 取反转

10.二选一测试程序

…include “muxtwo.v”

module t;

reg ain, bin, select;

reg clock;

wire outw;

initial begin

ain=0 ;bin=0; select=0; clock=0; end

always #50 clock =~clock;

always @(posedge clock)

begin

ain={$random}%2;

#3?bin={$random}%2; end

always #1000 select=~select;

muxtwo

m(.out(outw), .a(ain), .b(bin) .sl(select)); endmodule

11.自动售货机

module sale(

input clk,

input rst,

input k1,

input k2,

input k5,

input k,

output reg out,

output reg [2:0] out0);

reg [2:0] state;

parameter

s0=3'b000,

s1=3'b001,

s2=3'b010,

s3=3'b011,

s4=3'b100;

always @(posedge clk or posedge rst)

begin

if(rst) begin state<=s0;out<=0;out0<=0; end

else

case(state)

s0: if(k1==1)begin

state<=s1; out<=0; out0<=0;end

else if(k2==1)begin out<=0; out0<=0;end

else if(k5==1)begin

state<=s0;

out<=1; out0<=0;end

else if(k==1) begin

out<=0; out0<=0; end

s1: if(k1==1)begin

state<=s2;out<=0;out0<=0;end

else if(k2==1)begin state<=s3;out<=0;out0<=0;end

else if(k5==1)begin state<=s1;out<=1;out0<=0;end

else if(k==1) begin

out<=0; out0<=1; end

s2: if(k1==1)begin

state<=s3;out<=0;out0<=0;end

else if(k2==1)begin state<=s4;out<=0;out0<=0;end

else if(k5==1)begin state<=s2;out<=1;out0<=0;end

else if(k==1) begin

out<=0; out0<=2; end

s3: if(k1==1)begin

state<=s4;out<=0;out0<=0;end

else if(k2==1)begin

state<=s0;out<=1;out0<=0;end

else if(k5==1)begin state<=s3;out<=1;out0<=3;end

else if(k==1) begin

out<=0; out0<=3; end

s4: if(k1==1)begin

state<=s0;out<=1;out0<=0;end

else if(k2==1)begin

state<=s1;out<=1;out0<=0;end

else if(k5==1)begin

state<=s4;out<=1;out0<=0;end

else if(k==1) begin

out<=0;

out0<=4; end

endcase

end

AD转换verilog程序

// 16-bit Analogue-Digital Converter // // +-----------------------------+ // | Copyright 1996 DOULOS | // | designer : Tim Pagden | // | opened: 7 Jun 1996 | // +-----------------------------+ `timescale 1 ns / 1 ps module ADC_16bit (analog_in,digital_out); parameter conversion_time = 25.0, // conversion_time in ns // (see `timescale above) charge_limit = 1000000; // = 1 million input[63:0] analog_in; // double-precision representation of a real-valued input port; a fix that enables // analog wires between modules to be coped with in Verilog. // Think of input[63:0] as the equivalent of MAST's electrical. output[15:0] digital_out; reg[15:0] delayed_digitized_signal; reg[15:0] old_analog,current_analog; reg[4:0] changed_bits; reg[19:0] charge; reg charge_ovr; reg reset_charge; /* SIGNALS:- analog_in = 64-bit representation of a real-valued signal analog_signal = real valued signal recovered from analog_in analog_limited = analog_signal, limited to the real-valued input range of the ADC digital_out = digitized 16bit 2's complement quantization of analog_limited */ /* function to convert analog_in to digitized_2s_comp_signal. Takes analog_in values from (+10.0 v - 1LSB) to -10.0 v and converts them to values from +32767 to -32768 respectively */

VerilogHDL经典程序非常适合新手

一、2线-4线译码器 module counter4(q1,q0,ncr,cp); input cp,ncr; output q1,q0; reg q1,q0; always@(posedge cp or negedge ncr) begin if(~ncr){q1,q0}<=2'b00; else{q1,q0}<={q1,q0}+1'b1; end endmodule 二、4选1数据选择器 module selector4_1(i0,i1,i2,i3,a1,a0,y); input i0,i1,i2,i3,a1,a0; output y; reg y; always@(a1or a0) begin case({a1,a0}) 2'b00:y=i0; 2'b01:y=i1; 2'b10:y=i2; 2'b11:y=i3; default:y=0; 一、2线-4线译码器 module counter4(q1,q0,ncr,cp); input cp,ncr; output q1,q0; reg q1,q0; always@(posedge cp or negedge ncr) begin if(~ncr){q1,q0}<=2'b00; else{q1,q0}<={q1,q0}+1'b1; end endmodule 二、4选1数据选择器 module selector4_1(i0,i1,i2,i3,a1,a0,y); input i0,i1,i2,i3,a1,a0; output y; reg y; always@(a1or a0) begin case({a1,a0}) 2'b00:y=i0;

Verilog HDL编程举例

设计示范和上机习题 练习一.简单的组合逻辑设计 //(方法一): //---------------文件名compare.v ----------------- module compare(equal,a,b); input a,b; output equal; assign equal = (a==b)? 1 : 0; //a等于b时,equal输出为1;a不等于b时,equal输出为0。endmodule //(方法二): module compare(equal,a,b); input a,b; output equal; reg equal; always @(a or b) if(a==b) //a等于b时,equal输出为1; equal =1; else //a不等于b时,equal输出为0。 equal = 0; //思考:如果不写else 部分会产生什么逻辑?

endmodule //------------------------------------------------------------- //----------测试模块源代码(方法之一): `timescale 1ns/1ns // 定义时间单位。 `include "./compare.v" // 包含模块文件。在有的仿真调试环境中并不需要此语句。 //而需要从调试环境的菜单中键入有关模块文件的路径和名称 module t; reg a,b; wire equal; initial // initial常用于仿真时信号的给出。 begin a=0; b=0; #100 a=0; b=1; #100 a=1; b=1; #100 a=1; b=0; #100 a=0; b=0; #100 $stop; //系统任务,暂停仿真以便观察仿真波

verilog程序-60进制计数器

module count60_dongtai_LED ( input clk, input rest_n, output reg [2:0] sel, //位选 output reg [6:0] display ); reg [15:0] count_clk; // 分频计数器,最大2^16=64K分频 reg [5:0] sum_num; //计数缓存器,2^6=64 reg [3:0] g_bit; //个位 reg [3:0] s_bit; //十位 reg [3:0] disp_temp; //分频 always @ (posedge clk or negedge rest_n) begin if(rest_n ==0) begin count_clk=16'b0; end else begin if(count_clk==16'hffff) begin count_clk=16'b0; end else begin count_clk=count_clk+1'b1; end end end // 60进制计数 always @ (negedge count_clk[3] or negedge rest_n) begin // clk_clk[3] 对"clk" 16分频if(rest_n ==0) begin g_bit=4'b0; s_bit=4'b0; sum_num=6'b0; end else begin if (sum_num==6'd59) begin sum_num=6'b0; end else begin sum_num=sum_num+1'b1; end end s_bit=(sum_num/10)%10;

Verilog程序(汉字点阵显示

中国石油大学 数电课程设计报告题目: 学院: 班级: 姓名: 学号: 日期: 2012 年 12月

摘要 设计要求: 利用EDA/SOPC 实验开发平台提供的16*16点阵LED以及EPC235核心板,实现循环显示“中国石油大学”这6个汉字(左移或者右移均可)。 (1)手动生成“中国石油大学”这6个汉字在16*16点阵LED 上的6个字模(即控制某些LED亮,某些LED灭)。 (2)实现循环显示“中国石油大学”这6个汉字(左移或者右移均可)。 (3)拓展要求:自主设计(如控制循环速度,方向)。 关键词: 扫描分频,控制速度,点阵,点阵汉字显示,

设计原理及方案: 1、16*16点阵LED内部结构如下图所示。 2、总体设计框图: 3、各子模块的设计: (1)、分频,扫描: module fenpin (clk_50Mhz,clk_4hz,k2,k3); input clk_50Mhz,k2,k3; // 输入端口声明

output clk_4hz; // 输出端口声明reg[24:0] count,ccount; reg clk_4hz; always @(posedge clk_50Mhz) begin if ((k2==0) && (k3==0)) ccount<=500000000; if ((k2==0) && (k3==1)) ccount<=100000000; if ((k2==1) && (k3==0)) ccount<=50000000; if ((k2==1) && (k3==1)) ccount<=10000000; if(count

Verilog的135个经典设计实例

【例3.1】4位全加器 module adder4(cout,sum,ina,inb,cin); output[3:0] sum; output cout; input[3:0] ina,inb; input cin; assign {cout,sum}=ina+inb+cin; endmodule 【例3.2】4位计数器 module count4(out,reset,clk); output[3:0] out; input reset,clk; reg[3:0] out; always @(posedge clk) begin if (reset) out<=0; //同步复位 else out<=out+1; //计数 end endmodule 【例3.3】4位全加器的仿真程序 `timescale 1ns/1ns `include "adder4.v" module adder_tp; //测试模块的名字 reg[3:0] a,b; //测试输入信号定义为reg型 reg cin; wire[3:0] sum; //测试输出信号定义为wire型 wire cout; integer i,j; adder4 adder(sum,cout,a,b,cin); //调用测试对象 always #5 cin=~cin; //设定cin的取值 initial begin a=0;b=0;cin=0; for(i=1;i<16;i=i+1) #10 a=i; //设定a的取值 end - 1 -

initial begin for(j=1;j<16;j=j+1) #10 b=j; //设定b的取值 end initial//定义结果显示格式 begin $monitor($time,,,"%d + %d + %b={%b,%d}",a,b,cin,cout,sum); #160 $finish; end endmodule 【例3.4】4位计数器的仿真程序 `timescale 1ns/1ns `include "count4.v" module coun4_tp; reg clk,reset; //测试输入信号定义为reg型 wire[3:0] out; //测试输出信号定义为wire型 parameter DELY=100; count4 mycount(out,reset,clk); //调用测试对象 always #(DELY/2) clk = ~clk; //产生时钟波形 initial begin//激励信号定义 clk =0; reset=0; #DELY reset=1; #DELY reset=0; #(DELY*20) $finish; end //定义结果显示格式 initial $monitor($time,,,"clk=%d reset=%d out=%d", clk, reset,out); endmodule 【例3.5】“与-或-非”门电路 module AOI(A,B,C,D,F); //模块名为AOI(端口列表A,B,C,D,F) input A,B,C,D; //模块的输入端口为A,B,C,D output F; //模块的输出端口为F - 2 -

ADDA等一些芯片的verilog程序

/* AD0809 module v1.0 work up to 5M sample = 25us 40khz for normal clk = 2.5M sample = 30us 33khz */ module ad0809( clkin, adclk, eoc, st, ale, datain, oe, dataout ); input clkin; input eoc; input [7:0]datain; output st; output ale; output oe; output adclk; output [7:0]dataout; reg adclk; reg [7:0]dataout; reg st; reg oe; reg ale; //frequence divider for AD parameter Div_adclk = 8'd9;//(9+1)*2=20 adclk=2.5M parameter Div_clk_state = 4'd4;//(4+1)*2=10 clk_state=5M

reg [8:0]div_cnt_ad;//frequence div cnt reg [3:0]div_cnt_state; reg clk_state; always@(negedge clkin)begin if(div_cnt_ad != Div_adclk) div_cnt_ad <= div_cnt_ad + 1'b1; else begin div_cnt_ad <= 0; adclk <= ~adclk; end if(div_cnt_state != Div_clk_state) div_cnt_state <= div_cnt_state + 1'b1; else begin div_cnt_state <= 0; clk_state <= ~clk_state; end end /*AD convert*/ reg [3:0]state; reg [7:0]delay; initial begin state <= 4'd0; end always@(negedge clk_state)begin case(state) 4'd0:begin //clear all st <= 1'b0; oe <= 1'b0; ale <= 1'b0;

几个常用的Verilog小程序

几个常用的Verilog小程序 (1) 8位串并转换 module haidaoqi(clk,rst,din,dout); input clk,rst,din; output [7:0] dout; reg[7:0] data,dout; reg[2:0] cnt; always@(posedge clk or posedge rst) if(rst) //复位高有效 data <=8'b0; else data<= {data[7:0],din};//din是输入串行数据,假设输入数据高位在前 //这是一个移位寄存器 always@(posedge clk or posedge rst) if(rst) cnt <= 3'b0; else if(cnt == 3'b111) cnt <= 3'b0; else cnt <= cnt +1;//计数器,用来计算移位次数,移位8次在以

后产生一个有效数据 always@(posedge clk or posedge rst) if(rst) dout <= 8'b0; else if(cnt == 3'b111) dout <= data;//如果计数器记到7,那么输出一个有效的8位数据 else dout <= 0; endmodule (2) 8 位数据寄存器 module reg8(out_data,in_data,clk,clr); output[7:0] out_data; input[7:0] in_data; input clk,clr; reg[7:0] out_data; always @(posedge clk or posedge clr) begin

verilog语言基本程序

1.设计一个产生010*******的序列信号发生器。module generator(out,clk,state); output [3:0]state; output out; input clk; reg [3:0]state; reg out; always@(posedge clk) begin if(state==9) state<=0; else state<=state+1; case(state) 4'b0000:out<=0; 4'b0001:out<=1; 4'b0010:out<=0; 4'b0011:out<=0; 4'b0100:out<=1; 4'b0101:out<=0; 4'b0110:out<=0; 4'b0111:out<=1;

4'b1000:out<=1; 4'b1001:out<=1; default:out<=0; endcase end endmodule 2.设计一个8位的加减计数器。时钟信号为clk,同步清零信号为rst。Clk上升沿时刻,当inc=1计数器加二,其他情况计数器保持不变。 module counter(rst,clk,q,inc,dec); input rst,clk,inc,dec; output [7:0]q; reg [7:0]q; always@(posedge clk) if(rst) q<=0; else case({inc,dec}) 2'b10:begin if(q==254) q<=0;else q<=q+2;end// 2'b01:begin if(q==0) q<=254;else q<=q-2;end// default:q<=q; endcase

Verilog程序解析

module antitwitter (clock,keyin,keyout; input clock,keyin; output keyout; reg [3:0] count; reg keyout; always @(posedge clock begin if ( keyin == 1 begin count <= count +4'h1; if (count <= 8 keyout<= 1'b0; else begin keyout <= keyin; count <= 4'h9; end end else begin

count <= 4'h0; keyout <= 1'b0; end end endmodule module discode38(g1,g2a,g2b,a,b,c,y; input g1,g2a,g2b,a,b,c; output [7:0] y; reg [7:0] y; always @(g1 or g2a or g2b or a or b or c // 当输入信号有变化时,执行块语句begin if ( (g1==1&&(g2a==0&&(g2b==0 // 门控信号满足条件时,输出有效begin case ( {c,b,a} 3'b000 : y <=8'b11111110; 3'b001 : y <=8'b11111101; 3'b010 : y <=8'b11111011; 3'b011 : y <=8'b11110111; 3'b100 : y <=8'b11101111; 3'b101 : y <=8'b11011111;

ADC0809驱动FPGA实现的verilog程序

/*FPGA实现的程序:(verilog) 贴子回复于:2008-4-27 15:26:01*/ module AD0809(clk500K, //脉宽(至少100ns) rst_n, EOC, //约100us后EOC变为高电平转换结束 START, //启动信号,上升沿有效(至少100ns) OE, //高电平打开三态缓冲器输出转换数据 ALE, //高电平有效,选择信道口 ADDA, //因为ADDB,ADDC都接地了,这里只有ADDA为变量 DATA, //转换数据 DATA_R); output START,OE,ALE,ADDA; input EOC,clk500K,rst_n; input[7:0] DATA; output[7:0] DATA_R; reg START,OE,ALE,ADDA; reg[7:0] DATA_R; reg[4:0] CS,NS; parameter IDLE=5'b00001,START_H=5'b00010,START_L=5'b00100,CHECK_END=5'b01000,GET_DATA=5'b100 00; always @(posedge clk500K) case(CS) IDLE: NS=START_H; START_H: NS=START_L; START_L: NS=CHECK_END; CHECK_END: if(EOC) NS=GET_DATA; else NS=CHECK_END; GET_DATA: NS=IDLE; default: NS=IDLE; endcase always @(posedge clk500K) if(!rst_n) CS<=IDLE;

常见面试笔试题-verilog程序库

加减法 module addsub ( input [7:0] dataa, input [7:0] datab, input add_sub, // if this is 1, add; else subtract input clk, output reg [8:0] result); always @ (posedge clk) begin if (add_sub) result <= dataa + datab; //or "assign {cout,sum}=dataa+datab;" else result <= dataa - datab; end endmodule 四位的全加法器. module add4(cout,sum,a,b,cin) input[3:0]a,b;input cin; output [3:0] sum; output cout; assign {cout,sum}=a+b+cin; endmodule 补码不仅可以执行正值和负值转换,其实补码存在的意义,就是避免计算机去做减法的操作。 1101 -3 补 + 1000 8 01015 假设-3 + 8,只要将-3 转为补码形式,亦即0011 => 1101,然后和8,亦即1000 相加 就会得到5,亦即0101。至于溢出的最高位可以无视掉。 乘法器 module mult(outcome,a,b); parameter SIZE=8; input[SIZE:1] a,b; output reg[2*SIZE:1] outcome; integer i; always @(a or b) begin outcome<=0; for(i=0,i<=SIZE;i=i+1) if(b[i]) outcome<=outcome+(a<<(i-1)); end endmodule 另一种乘法器。在初始化之际,取乘数和被乘数的正负关系,然后取被乘数和乘数的正值。输出结果根据正负关系取得。 else if( Start_Sig ) case( i ) 0: begin

Verilog程序例子

modul e and1(a, b, c); input a; input b; output c; assign c = a & b; endmodul e `timescale 1ns/1ns modul e t; reg a; reg b; wire c; and1 t(a,b,c); initial begin a=0; b=0; #100 a = 1; b = 0; #100 a = 0; b = 1; #100 a = 0; b = 0; #100 a = 1; b = 1; #100 $stop;

end initial $monitor("a = %d, ", a, " b = %d, ", b, "c = %d\n", c); endmodul e modul e add(a, b, in, c, out); input a; input b; input in; output c; output out; assign {out, c} = a + b + in; endmodul e `timescal e 1ns/1ns modul e count_t; reg clk; reg a; reg b; reg in; wire c; wire out; ad d process(a, b, in, c, out);

initial clk = 0; always forever #5 clk = ~clk; initial begin a = 0; b = 0; in = 0; #10 a = 0; b = 0; in = 1; #10 a = 0; b = 1; in = 0; #10 a = 0; b = 1; in = 1; #10 a = 1; b = 0; in = 0; #10 a = 1; b = 0; in = 1; #10 a = 1; b = 1; in = 0; #10 a = 1; b = 1; in = 1; end initial begin #200 $finish; end initial $monitor(" out = %d, c = %d\n", out, c); endmodul e modul e compare (equal, a, b); input a,b; output equal; assign equal= (a==b)?1:0; endmodul e `timescal e 1ns/1ns modul e t; reg a, b; wire equal; initial begin a=0; b=0; #100 a = 1; b = 0;

Verilog的150个经典设计实例

module adder4(cout,sum,ina,inb,cin); output[3:0] sum; output cout; input[3:0] ina,inb; input cin; assign {cout,sum}=ina+inb+cin; endmodule 【例3.2】4位计数器 module count4(out,reset,clk); output[3:0] out; input reset,clk; reg[3:0] out; always @(posedge clk) begin if (reset) out<=0; //同步复位 else out<=out+1; //计数 end endmodule 【例3.3】4位全加器的仿真程序 `timescale 1ns/1ns `include "adder4.v" module adder_tp; //测试模块的名字 reg[3:0] a,b; //测试输入信号定义为reg型 reg cin; wire[3:0] sum; //测试输出信号定义为wire型 wire cout; integer i,j; adder4 adder(sum,cout,a,b,cin); //调用测试对象 always #5 cin=~cin; //设定cin的取值 initial begin a=0;b=0;cin=0; for(i=1;i<16;i=i+1) #10 a=i; //设定a的取值 end - 1 -

initial begin for(j=1;j<16;j=j+1) #10 b=j; //设定b的取值 end initial//定义结果显示格式 begin $monitor($time,,,"%d + %d + %b={%b,%d}",a,b,cin,cout,sum); #160 $finish; end endmodule 【例3.4】4位计数器的仿真程序 `timescale 1ns/1ns `include "count4.v" module coun4_tp; reg clk,reset; //测试输入信号定义为reg型 wire[3:0] out; //测试输出信号定义为wire型 parameter DELY=100; count4 mycount(out,reset,clk); //调用测试对象 always #(DELY/2) clk = ~clk; //产生时钟波形 initial begin//激励信号定义 clk =0; reset=0; #DELY reset=1; #DELY reset=0; #(DELY*20) $finish; end //定义结果显示格式 initial $monitor($time,,,"clk=%d reset=%d out=%d", clk, reset,out); endmodule 【例3.5】“与-或-非”门电路 module AOI(A,B,C,D,F); //模块名为AOI(端口列表A,B,C,D,F) input A,B,C,D; //模块的输入端口为A,B,C,D output F; //模块的输出端口为F - 2 -

FPGA实验及基本程序汇总(Verilog)

一、填空题: 1、 FPGA结构一般分为三部分:可编程逻辑块(CLB)、可编程I/O模块和可编程内部连线。 2、 CPLD的内部连线为连续式布线互连结构,任意一对输入、输出端之间的延时是固定;FPGA的内部连线为分段式布线互连结构,各功能单元间的延时不定(不可预测)。 3、大规模可编程器件主要有CPLD和FPGA两类,其中CPLD通过可编程乘积项逻辑实现其逻辑功能。基于SRAM的FPGA器件,每次上电后必须进行一次配置。FPGA内部阵列的配置一般采用在电路可重构技术,编程数据保存在静态存储器(SRAM) ,掉电易失。 4、目前世界上有十几家生产CPLD/FPGA的公司,最大的两家是:Altera,Xilinx。 5、硬件描述语言(HDL)是EDA技术的重要组成部分,是电子系统硬件行为描述、结构描述、数据流描述的语言,它的种类很多,如VHDL、Verilog HDL、AHDL 6、 WHEN_ELSE条件信号赋值语句和 IF_ELSE顺序语句的异同: * WHEN_ELSE条件信号赋值语句中无标点,只有最后有分号;必须成对出现;是并行语句,必须放在结构体中。 * IF_ELSE顺序语句中有分号;是顺序语句,必须放在进程中 7、可编程逻辑器件设计输入有原理图输入、硬件描述语言输入和波形输入三种方式。原理图输入方式是一种最直接的设计描述方式,波形设计输入适用于时序逻辑和有重复性的逻辑函数。 硬件描述语言的突出优点是: * 语言与工艺的无关性;语言的公开可利用性,便于实现大规模系统的设计; * 具有很强逻辑描述和仿真功能,而且输入效率高,在不同设计输入库之间的转换非常方便,用不着对底层的电路和PLD结构的熟悉。 8、用VHDL/Veilog HDL语言开发可编程逻辑电路的完整流程:文本编辑→功能仿真→逻辑综合→布局布线→时序仿真。 *所谓综合,就是根据设计功能和实现该设计的约束条件(如面积、速度、功耗和成本等),将设计输入转换成满足要求的电路设计方案,该方案必须同时满足与其的功能和约束条件。综合的过程也是设计目标的优化过程,其目的是将多个模块化设计文件合并为一个网表文件,供布局布线使用,网表中包含了目标器件中的逻辑单元和互连的信息。 *布局布线就是根据设计者指定的约束条件(如面积、延时、时钟等)、目标器件的结构资源和工艺特性,以最优的方式对逻辑元件布局,并准确地实现元件间的互连,完成实现方案(网表)到使实际目标器件(FPGA或CPLD)的变换。 9、基于EDA软件的FPGA / CPLD设计流程为:原理图/HDL文本输入→功能仿真→综合→适配→时序仿真→编程下载→硬件测试。 * 综合是EDA设计的关键步骤,综合就是将电路的高级语言转换成低级的,可与FPGA/CPLD相映射的功能网表文件。为实现系统的速度、面积、性能的要求,需要对综合加以约束,称为综合约束。 10、构成一个完整的VHDL语言程序的五个基本结构: 实体(ENTITY)、结构体(ARCHITECURE)、配置(CONFIGURATION) 、库(LIBRARY) 、程序包 (PACKAGE) 。 *实体的由实体说明和结构体两部分组成。实体说明部分用于描述所设计系统的外部端口信号和参数的属性和设置,而结构体部分则定义了设计单元的具体功能、行为、数据流程或内部结构。 *结构体的三种描述方式,即行为级描述、数据流级描述和结构级描述。 *结构体通常由结构体名称、定义语句和并行处理语句构成。 *程序包用于存放各设计模块能共享的数据类型、常数、子程序等。 *库用于存放已编译的实体、结构体、程序包和配置,可以通过其目录进行查询和调用。在VHDL语言中,可以存在多个不同的库,但是库与库之间是独立的,不能互相嵌套。它可由用户生成或由ASIC芯片制造商提供,以便于在设计中为大家所共享。 *库用于存放已编译的实体、结构体、程序包和配置,可以通过其目录进行查询和调用。在VHDL语言中,可以存在多个不同的库,但是库与库之间是独立的,不能互相嵌套。它可由用户生成或由ASIC芯片制造商提供,以便于在设计中为大家所共享。 常用库:

Verilog程序代码集

1.全加器 Sum=A⊕B⊕Cin Count=AB+Cin(A+B) ①数据流 module adder(a,b,Cin,Sum,Count); input [2:0]a,b; input Cin; output [2:0] Sum; output Count; assign {Count,Sum}=a+b+Cin; endmodule ②行为描述always语句 module adder(a,b,c,Cin,Sum,Count); input [4:0] a,b; input Cin; output reg [4:0] Sum; output reg Count; reg T1,T2,T3; always@(a or b or Cin) begin Sum=a^b^Cin; T1=A&B; T2=Cin&A; T3=Cin&B; Count=T1|T2|T3; end endmodule ③结构体 module adder (a,b,Cin,Sum,Count);input a,b,Cin; output Sum,Count; Xor a1(s1,a1,b); Xor a2(Sum,s1,Cin); and a3(T1,a,b); or a4(T2,a,b); and a5(T3,Cin,T2); or a6(Count,T1,T3); Endmodule 2.数值比较器 ①判断两值是否相等 module compare(a,b,equal); input [7:0] a,b; output equal; assign equal=(a==b)?|0; ②谁大谁输出 module compare(a,b,out); input [7:0] a,b; output reg[7:0] out; always@(a or b) begin if (a>b) out<=a; else if (a==b) out<=a; else out<=b; end endmodule ③输出参数 module compare(a.b.xgy,xsy,xey); input [7:0] x,y; output reg xgy,xsy,xey; always@(x or y) begin if (x==y) xey=1; else xey=0; if (x>y) begin xgy=1;xsy=0;end else if (x

Verilog实例代码

- 1 - 【例3.1】4位全加器 module adder4(cout,sum,ina,inb,cin); output [3:0] sum; output cout; input [3:0] ina,inb; input cin; assign {cout,sum}=ina+inb+cin; endmodule 【例3.2】4位计数器 module count4(out,reset,clk); output [3:0] out; input reset,clk; reg [3:0] out; always @(posedge clk) begin if (reset) out<=0; //同步复位 else out<=out+1; //计数 end endmodule 【例3.3】4位全加器的仿真程序 `timescale 1ns/1ns `include "adder4.v" module adder_tp; //测试模块的名字 reg [3:0] a,b; //测试输入信号定义为reg 型 reg cin; wire [3:0] sum; //测试输出信号定义为wire 型 wire cout; integer i,j; adder4 adder(sum,cout,a,b,cin); //调用测试对象 always #5 cin=~cin; //设定cin 的取值 initial begin a=0;b=0;cin=0; for (i=1;i<16;i=i+1) #10 a=i; //设定a 的取值 end

initial begin for(j=1;j<16;j=j+1) #10 b=j; //设定b的取值 end initial//定义结果显示格式 begin $monitor($time,,,"%d + %d + %b={%b,%d}",a,b,cin,cout,sum); #160 $finish; end endmodule 【例3.4】4位计数器的仿真程序 `timescale 1ns/1ns `include "count4.v" module coun4_tp; reg clk,reset; //测试输入信号定义为reg型 wire[3:0] out; //测试输出信号定义为wire型 parameter DELY=100; always #(DELY/2) clk = ~clk; //产生时钟波形 initial begin//激励信号定义 clk =0; reset=0; #DELY reset=1; #DELY reset=0; #(DELY*20) $finish; end //定义结果显示格式 initial $monitor($time,,,"clk=%d reset=%d out=%d", clk, reset,out); endmodule 【例3.5】“与-或-非”门电路 module AOI(A,B,C,D,F); //模块名为AOI(端口列表A,B,C,D,F) input A,B,C,D; //模块的输入端口为A,B,C,D output F; //模块的输出端口为F - 2 -

Verilog及FPGA学习经典程序(一)

目录 1.四位全加器 (2) 2.四位计数器 (2) 3.四位全加器仿真程序 (2) 4.四位计数器仿真程序 (3) 5.“与-或-非”门电路 (4) 6.用case 语句描述的4 选1 数据选择器 (4) 7.同步置数、同步清零的计数器 (5) 8.用always 过程语句描述的简单算术逻辑单元 (5) 9.用initial 过程语句对测试变量A、B、C 赋值 (6) 10.用begin-end 串行块产生信号波形 (7) 11.用fork-join 并行块产生信号波形 (7) 12.持续赋值方式定义的2 选1 多路选择器 (8) 13.阻塞赋值方式定义的2 选1 多路选择器 (8) 14.非阻塞赋值 (9) 15.阻塞赋值 (9) 16.模为60 的BCD 码加法计数器 (9) 17.BCD 码—七段数码管显示译码器 (10) 18.用casez 描述的数据选择器 (11) 19.隐含锁存器举例 (12) 20.用for 语句描述的七人投票表决器 (12) 21.用for 语句实现2 个8 位数相乘 (13) 22.用repeat 实现8 位二进制数的乘法 (13) 23.同一循环的不同实现方式 (14) 24.使用了`include 语句的16 位加法器 (15) 25.条件编译举例 (16) 1

1.四位全加器 module adder4(cout,sum,ina,inb,cin); output[3:0] sum; output cout; input[3:0] ina,inb; input cin; assign {cout,sum}=ina+inb+cin; endmodule 2.四位计数器 module count4(out,reset,clk); output[3:0] out; input reset,clk; reg[3:0] out; always @(posedge clk) begin if (reset) out<=0; //同步复位 else out<=out+1; //计数 end endmodule 3.四位全加器仿真程序 `timescale 1ns/1ns `include "adder4.v" module adder_tp; //测试模块的名字 reg[3:0] a,b; //测试输入信号定义为reg型reg cin; wire[3:0] sum; //测试输出信号定义为wire型wire cout; integer i,j; 2

相关主题