Verilog Language

1 Basics

1.1 Wire

module top_module( input in, output out );
assign out = in; endmodule

1.2 Wire4

module top_module(
input a,b,c,
output w,x,y,z ); assign w = a;
assign x = b;
assign y = b;
assign z = c; endmodule

1.3 Notgate

module top_module( input in, output out );

    assign out =~in;
endmodule

1.4 Andgate

module top_module(
input a,
input b,
output out ); assign out = a&b;
endmodule

1.5 Norgate

module top_module(
input a,
input b,
output out ); assign out = ~(a|b);
endmodule

1.6 Xnorgate

module top_module(
input a,
input b,
output out ); assign out = ~((~a&b)|(a&~b));
endmodule

1.7 Declaring wires

`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n ); wire e,f;
wire out_0; assign e = a&b;
assign f = c&d; assign out_0 = e|f; assign out_n = ~out_0;
assign out = out_0; endmodule

1.8 7458

module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y ); wire a,b,c,d; assign a = p1c&p1b&p1a;
assign b = p2a&p2b;
assign c = p2c&p2d;
assign d = p1f&p1e&p1d; assign p2y = b|c;
assign p1y = a|d; endmodule

2 Vector

2.1 Vector0

module top_module (
input wire [2:0] vec,
output wire [2:0] outv,
output wire o2,
output wire o1,
output wire o0 ); // Module body starts after module declaration assign outv = vec;
assign o2 = vec[2];
assign o1 = vec[1];
assign o0 = vec[0]; endmodule

2.2 Vector1

`default_nettype none     // Disable implicit nets. Reduces some types of bugs.
module top_module(
input wire [15:0] in,
output wire [7:0] out_hi,
output wire [7:0] out_lo ); assign out_hi = in[15:8];
assign out_lo = in[7:0];
endmodule

2.3 Vector2

module top_module(
input [31:0] in,
output [31:0] out );// // assign out[31:24] = ...;
assign out[31:24] = in[7:0];
assign out[23:16] = in[15:8];
assign out[15:8] = in[23:16];
assign out[7:0] = in[31:24]; endmodule

2.4 Vectorgates

module top_module(
input [2:0] a,
input [2:0] b,
output [2:0] out_or_bitwise,
output out_or_logical,
output [5:0] out_not
); assign out_or_bitwise = a|b;
assign out_or_logical = a||b;
assign out_not[5:3] = ~b;
assign out_not[2:0] = ~a; endmodule

2.5 Gates4

module top_module(
input [3:0] in,
output out_and,
output out_or,
output out_xor
); assign out_and = in[3]&in[2]&in[1]&in[0];
assign out_or = in[3]|in[2]|in[1]|in[0];
assign out_xor = in[3]^in[2]^in[1]^in[0]; endmodule

2.6 Vector3

module top_module (
input [4:0] a, b, c, d, e, f,
output [7:0] w, x, y, z );// reg[1:0] t;
initial
begin
t = 2'b11;
end
// assign { ... } = { ... };
assign w[7:0] = {a[4:0],b[4:2]};
assign x[7:0] = {b[1:0],c[4:0],d[4]};
assign y[7:0] = {d[3:0],e[4:1]};
assign z[7:0] = {e[0],f[4:0],t}; endmodule

2.7 Vectorr

module top_module(
input [7:0] in,
output [7:0] out
);
assign out = {in[0],in[1],in[2],in[3],in[4],in[5],in[6],in[7]}; endmodule

2.8 Vector4

module top_module (
input [7:0] in,
output [31:0] out );// // assign out = { replicate-sign-bit , the-input };
assign out = {{24{in[7]}},in}; endmodule

2.9 Vector5

module top_module (
input a, b, c, d, e,
output [24:0] out );// // The output is XNOR of two vectors created by
// concatenating and replicating the five inputs.
// assign out = ~{ ... } ^ { ... };
assign out[24:20] = ~{5{a}}^{a,b,c,d,e};
assign out[19:15] = ~{5{b}}^{a,b,c,d,e};
assign out[14:10] = ~{5{c}}^{a,b,c,d,e};
assign out[9:5] = ~{5{d}}^{a,b,c,d,e};
assign out[4:0] = ~{5{e}}^{a,b,c,d,e}; endmodule

3 Modules:Hierarchy

3.1 Module

module top_module ( input a, input b, output out );
mod_a U1(a,b,out);
endmodule

3.2 Module pos

module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a U1(out1,out2,a,b,c,d); endmodule

3.3 Module name

module top_module (
input a,
input b,
input c,
input d,
output out1,
output out2
);
mod_a U1(.out1(out1),.out2(out2),.in1(a),.in2(b),.in3(c),.in4(d)); endmodule

3.4 Module shift

module top_module ( input clk, input d, output q );
wire d1,d2;
my_dff U1(clk,d,d1);
my_dff U2(clk,d1,d2);
my_dff U3(clk,d2,q); endmodule

3.5 Module shift8

module top_module (
input clk,
input [7:0] d,
input [1:0] sel,
output [7:0] q
);
wire [7:0] q0,q1,q2; my_dff8 U0(clk,d,q0);
my_dff8 U1(clk,q0,q1);
my_dff8 U8(clk,q1,q2); always @(sel)begin
case(sel)
2'b00:
q = d;
2'b01:
q = q0;
2'b10:
q = q1;
2'b11:
q = q2;
default:
q = 0;
endcase
end endmodule

3.6 Module add

module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout1;
add16 U1( a[15:0], b[15:0], 0, sum[15:0], cout1 );
add16 U2( a[31:16], b[31:16], cout1, sum[31:16], ); endmodule

3.6 Module fadd

module top_module (
input [31:0] a,
input [31:0] b,
output [31:0] sum
);//
wire cout1;
add16 U1( a[15:0], b[15:0], 0, sum[15:0], cout1 );
add16 U2( a[31:16], b[31:16], cout1, sum[31:16], ); endmodule module add1 ( input a, input b, input cin, output sum, output cout ); // Full adder module here
assign sum = a^b^cin;
assign cout = (a&b)|(a&cin)|(b&cin); endmodule

3.7 Module cseladd

module top_module(
input [31:0] a,
input [31:0] b,
output [31:0] sum
);
wire cout1;
wire [15:0] sum0,sum1,sum_l,sum_h;
add16 U1( a[15:0], b[15:0], 0, sum_l, cout1 );
add16 U2( a[31:16], b[31:16], 0,sum0, );
add16 U3( a[31:16], b[31:16], 1,sum1, );
always @(*)begin
if(cout1)
begin
sum_h = sum1;
end
else
begin
sum_h = sum0;
end
end
assign sum = {sum_h,sum_l}; endmodule

3.8 Module addsub

module top_module(
input [31:0] a,
input [31:0] b,
input sub,
output [31:0] sum
);
wire cout1;
wire [31:0] b1;
always @(*) begin
if(sub)
b1 = ~b;
else
b1 = b;
end
add16 U1( a[15:0], b1[15:0], sub, sum[15:0], cout1 );
add16 U2( a[31:16], b1[31:16], cout1, sum[31:16], ); endmodule

4 Procedures

4.1 Alwaysblock1

// synthesis verilog_input_version verilog_2001
module top_module(
input a,
input b,
output wire out_assign,
output reg out_alwaysblock
);
assign out_assign = a&b;
always @(*)begin
out_alwaysblock = a&b;
end endmodule

4.2 Alwaysblock2

// synthesis verilog_input_version verilog_2001
module top_module(
input clk,
input a,
input b,
output wire out_assign,
output reg out_always_comb,
output reg out_always_ff );
assign out_assign = a^b;
always @(*)begin
out_always_comb = a^b;
end
always @(posedge clk)begin
out_always_ff = a^b;
end endmodule

4.3 Always if

// synthesis verilog_input_version verilog_2001
module top_module(
input a,
input b,
input sel_b1,
input sel_b2,
output wire out_assign,
output reg out_always );
assign out_assign = (sel_b1&sel_b2)? b:a;
wire [1:0] sel;
assign sel = {sel_b1,sel_b2}; always @(*)begin
case(sel)
0:out_always = a;
1:out_always = a;
2:out_always = a;
3:out_always = b;
endcase
end endmodule

4.4 Always if2

// synthesis verilog_input_version verilog_2001
module top_module (
input cpu_overheated,
output reg shut_off_computer,
input arrived,
input gas_tank_empty,
output reg keep_driving ); // always @(*) begin
if (cpu_overheated)
shut_off_computer = 1;
else
shut_off_computer = 0;
end always @(*) begin
if (~arrived)
keep_driving = ~gas_tank_empty;
else
keep_driving = 0;
end endmodule

4.5 Always case

// synthesis verilog_input_version verilog_2001
module top_module (
input [2:0] sel,
input [3:0] data0,
input [3:0] data1,
input [3:0] data2,
input [3:0] data3,
input [3:0] data4,
input [3:0] data5,
output reg [3:0] out );// always@(*) begin // This is a combinational circuit
case(sel)
3'b000:out = data0;
3'b001:out = data1;
3'b010:out = data2;
3'b011:out = data3;
3'b100:out = data4;
3'b101:out = data5;
default:out = 0;
endcase
end endmodule

4.6 Always case2

// synthesis verilog_input_version verilog_2001
module top_module (
input [3:0] in,
output reg [1:0] pos );
always @(*) begin
case(in)
4'b0000 : pos = 0;
4'b0001 : pos = 0;
4'b0010 : pos = 1;
4'b0011 : pos = 0;
4'b0100 : pos = 2;
4'b0101 : pos = 0;
4'b0110 : pos = 1;
4'b0111 : pos = 0;
4'b1000 : pos = 3;
4'b1001 : pos = 0;
4'b1010 : pos = 1;
4'b1011 : pos = 0;
4'b1100 : pos = 2;
4'b1101 : pos = 0;
4'b1110 : pos = 1;
4'b1111 : pos = 0;
endcase
end endmodule

4.7 Always casez

// synthesis verilog_input_version verilog_2001
module top_module (
input [7:0] in,
output reg [2:0] pos );
always @(*)begin
casez(in)
8'bzzzzzzz1:pos=0;
8'bzzzzzz1z:pos=1;
8'bzzzzz1zz:pos=2;
8'bzzzz1zzz:pos=3;
8'bzzz1zzzz:pos=4;
8'bzz1zzzzz:pos=5;
8'bz1zzzzzz:pos=6;
8'b1zzzzzzz:pos=7;
default:pos=0;
endcase
end endmodule

4.8 Always nolatches

// synthesis verilog_input_version verilog_2001
module top_module (
input [15:0] scancode,
output reg left,
output reg down,
output reg right,
output reg up ); always @(*)begin
up = 1'b0; down = 1'b0; left = 1'b0; right = 1'b0;
case (scancode)
16'he06b:left = 1;
16'he072:down = 1;
16'he074:right = 1;
16'he075:up = 1;
default: ;
endcase
end endmodule

5 More Verilog Features

5.1 Conditional

module top_module (
input [7:0] a, b, c, d,
output [7:0] min);// // assign intermediate_result1 = compare? true: false;
wire [7:0] min1,min2;
assign min1 =(a<b)? a: b;
assign min2 =(min1<c)? min1: c;
assign min =(min2<d)? min2: d; endmodule

5.2 Reduction

module top_module (
input [7:0] in,
output parity);
assign parity = ^in[7:0]; endmodule

5.3 Gates100

module top_module(
input [99:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_and = &in[99:0];
assign out_or = |in[99:0];
assign out_xor = ^in[99:0]; endmodule

5.4 Vector100r

module top_module(
input [99:0] in,
output [99:0] out
);
integer i;
always @(*)begin
for(i = 0;i<100;i++)
out[i] = in[99-i];
end endmodule

5.5 Popcount255

module top_module(
input [254:0] in,
output [7:0] out );
integer i;
always @(*)begin
out = 0;
for(i=0;i<255;i++)
if(in[i]==1) out++;
else out = out;
end endmodule

5.6 Adder100i

module top_module(
input [99:0] a, b,
input cin,
output [99:0] cout,
output [99:0] sum );
integer i; add8 L1(a[7:0],b[7:0],cin,sum[7:0],cout[7:0]);
add8 L2(a[15:8],b[15:8],cout[7],sum[15:8],cout[15:8]);
add8 L3(a[23:16],b[23:16],cout[15],sum[23:16],cout[23:16]);
add8 L4(a[31:24],b[31:24],cout[23],sum[31:24],cout[31:24]);
add8 L5(a[39:32],b[39:32],cout[31],sum[39:32],cout[39:32]);
add8 L6(a[47:40],b[47:40],cout[39],sum[47:40],cout[47:40]);
add8 L7(a[55:48],b[55:48],cout[47],sum[55:48],cout[55:48]);
add8 L8(a[63:56],b[63:56],cout[55],sum[63:56],cout[63:56]);
add8 L9(a[71:64],b[71:64],cout[63],sum[71:64],cout[71:64]);
add8 L10(a[79:72],b[79:72],cout[71],sum[79:72],cout[79:72]);
add8 L11(a[87:80],b[87:80],cout[79],sum[87:80],cout[87:80]);
add8 L12(a[95:88],b[95:88],cout[87],sum[95:88],cout[95:88]); add1 P1(a[96],b[96],cout[95],sum[96],cout[96]);
add1 P2(a[97],b[97],cout[96],sum[97],cout[97]);
add1 P3(a[98],b[98],cout[97],sum[98],cout[98]);
add1 P4(a[99],b[99],cout[98],sum[99],cout[99]); endmodule module add1 ( input a, input b, input cin, output sum, output cout ); // Full adder module here
assign sum = a^b^cin;
assign cout = (a&b)|(a&cin)|(b&cin);
endmodule module add8 ( input [7:0]a, input [7:0] b, input cin ,output [7:0] sum,output [7:0] cout);
add1 U1(a[0],b[0],cin,sum[0],cout[0]);
add1 U2(a[1],b[1],cout[0],sum[1],cout[1]);
add1 U3(a[2],b[2],cout[1],sum[2],cout[2]);
add1 U4(a[3],b[3],cout[2],sum[3],cout[3]);
add1 U5(a[4],b[4],cout[3],sum[4],cout[4]);
add1 U6(a[5],b[5],cout[4],sum[5],cout[5]);
add1 U7(a[6],b[6],cout[5],sum[6],cout[6]);
add1 U8(a[7],b[7],cout[6],sum[7],cout[7]);
endmodule

5.8 Bcdadd100

module top_module(
input [399:0] a, b,
input cin,
output cout,
output [399:0] sum );
wire [399:0] cout_tmp;
bcd_fadd U1(.a(a[3:0]), .b(b[3:0]), .cin(cin), .cout(cout_tmp[0]),.sum(sum[3:0]));
generate
genvar i;
for(i = 4; i < 400; i=i+4) begin : adder
bcd_fadd fadd(.a(a[i+3:i]), .b(b[i+3:i]), .cin(cout_tmp[i-4]), .cout(cout_tmp[i]),.sum(sum[i+3:i]));
end
endgenerate
assign cout = cout_tmp[396]; endmodule

HDLBits答案——Verilog Language的更多相关文章

  1. Verilog Tips and Interview Questions

    Verilog Interiew Quetions Collection :  What is the difference between $display and $monitor and $wr ...

  2. JS应用,表单上的一些东西

    例: <body> <form>我的生日是哪一年? <input type="text" value="" id="t1 ...

  3. 中国澳门sinox很多平台CAD制图、PCB电路板、IC我知道了、HDL硬件描述语言叙述、电路仿真和设计软件,元素分析表

    中国澳门sinox很多平台CAD制图.PCB电路板.IC我知道了.HDL硬件描述语言叙述.电路仿真和设计软件,元素分析表,可打开眼世界. 最近的研究sinox执行windows版protel,powe ...

  4. 《JavaWeb程序开发入门》课后题

    第一章 1.请编写一个格式良好的XML文档,要求包含足球队一支,队名为Madrid,球员5人:Ronaldo.Casillas.Ramos.Modric.Benzema:篮球队一支,队名为Lakers ...

  5. SDRAM interface slashes pin count

    Many designs need deep buffering but don't require ultrahigh-memory bandwidth. Examples include imag ...

  6. 3分钟了解GPT Bert与XLNet的差异

    译者 | Arno 来源 | Medium XLNet是一种新的预训练模型,在20项任务中表现优于BERT,且有大幅度的提升. 这是什么原因呢? 在不了解机器学习的情况下,不难估计我们捕获的上下文越多 ...

  7. 学会使用Hdlbits网页版Verilog代码仿真验证平台

    给大家推荐一款网页版的 Verilog代码编辑仿真验证平台,这个平台是国外的一家开源FPGA学习网站,通过“https://hdlbits.01xz.net/wiki/Main_Page” 地址链接进 ...

  8. sublime text3 verilog代码编写高级操作篇

    2018.10.21 好久没写博客了,这段时间一直在学习一直在沉淀,然而发现学的越多会的更少,只能快马加鞭吧! 博主从大一暑假接触FPGA,到现在快一年半了,时间恍逝.刚开始入门也是用的quartus ...

  9. 【Java EE 学习 73】【数据采集系统第五天】【参与调查】【导航处理】【答案回显】【保存答案】

    一.参与调查的流程 单击导航栏上的“参与调查”按钮->EntrySurveyAction做出相应,找到所有的Survey对象并转发到显示所有survey对象的页面上供用户选择->用户单击其 ...

  10. java面试题及答案(转载)

    JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...

随机推荐

  1. Java 将Excel转为UOS

    以.uos为后缀的文件,表示Uniform Office Spreadsheet文件,是一种国产的办公文件格式,该格式以统一办公格式(UOF)创建,使用XML和压缩保存电子表格.既有的Excel表格文 ...

  2. Linux下进行Oracle数据库安装

    一般来说我们Windows下进行安装Oracle都很简单,但Linux下却要输入很多命令,以下的安装步骤是本人经过多次安装Linux下的Oracle经验,希望能帮到大家 一.在Linux服务器上创建o ...

  3. Jmeter处理响应报文中文乱码

    Jmeter在访问发送请求的时候,响应内容如果有中文可能会显示乱码,原因应该是响应页面没有做编码处理,jmeter默认按照ISO-8859-1编码格式进行解析.而我们的响应报文却是utf-8的格式,所 ...

  4. [GWCTF 2019]我有一个数据库 WP

    打开环境访问看到 提示我有一个数据库,但里面什么都没有,于是拿dirsearch跑了一下,没有出结果 但是有数据库嘛,那么试试常见的几个加上phpmyadmin 试试 于是看到了版本是4.8.1 拿到 ...

  5. 第四章:Django表单

    一.HTML表单概述 Django开发的是动态Web服务,而非单纯提供静态页面.动态服务的本质在于和用户进行互动,接收用户的输入,根据输入的不同,返回不同的内容给用户.返回数据是我们服务器后端做的,而 ...

  6. Deployment控制器(pod)更新策略

    最小就绪时间: 配置时,用户可以使用Deplpoyment控制器的spec.minReadySeconds属性来控制应用升级的速度.新旧更替过程中,新创建的Pod对象一旦成功响应就绪探测即被视作可用, ...

  7. 3.在 Kubernetes 上安装 Gitlab CI Runner

    结合文章:1. 在 Kubernetes 上安装 Gitlab ,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13852854.html 总结: 结合开头的文章 ...

  8. 5_项目实战MyShop

    一. 网上商城 1.1 商城类别 B2B 商家对商家 B2C 商家对客户 C2C 客户对客户 O2O 线上线下相结合 1.2 商城常见模块 后台常见功能模块 商品管理 包括后台商品库存管理, 上货, ...

  9. 【前端必会】不知道webpack插件? webpack插件源码分析BannerPlugin

    背景 不知道webpack插件是怎么回事,除了官方的文档外,还有一个很直观的方式,就是看源码. 看源码是一个挖宝的行动,也是一次冒险,我们可以找一些代码量不是很大的源码 比如webpack插件,我们就 ...

  10. 企业MES系统与ERP信息集成要素有哪些?

    关于要讲明企业MES系统与ERP信息集成要素有哪些,得先弄清楚他们之间的关系:从工厂的管理来说,ERP在上MES在下,ERP统领企业全局包括MES,为管理层服务,重心在于企业决策,ERP对企业宏观管理 ...