Verilog Language

1 Basics

1.1 Wire

  1. module top_module( input in, output out );
  2. assign out = in;
  3. endmodule

1.2 Wire4

  1. module top_module(
  2. input a,b,c,
  3. output w,x,y,z );
  4. assign w = a;
  5. assign x = b;
  6. assign y = b;
  7. assign z = c;
  8. endmodule

1.3 Notgate

  1. module top_module( input in, output out );
  2. assign out =~in;
  3. endmodule

1.4 Andgate

  1. module top_module(
  2. input a,
  3. input b,
  4. output out );
  5. assign out = a&b;
  6. endmodule

1.5 Norgate

  1. module top_module(
  2. input a,
  3. input b,
  4. output out );
  5. assign out = ~(a|b);
  6. endmodule

1.6 Xnorgate

  1. module top_module(
  2. input a,
  3. input b,
  4. output out );
  5. assign out = ~((~a&b)|(a&~b));
  6. endmodule

1.7 Declaring wires

  1. `default_nettype none
  2. module top_module(
  3. input a,
  4. input b,
  5. input c,
  6. input d,
  7. output out,
  8. output out_n );
  9. wire e,f;
  10. wire out_0;
  11. assign e = a&b;
  12. assign f = c&d;
  13. assign out_0 = e|f;
  14. assign out_n = ~out_0;
  15. assign out = out_0;
  16. endmodule

1.8 7458

  1. module top_module (
  2. input p1a, p1b, p1c, p1d, p1e, p1f,
  3. output p1y,
  4. input p2a, p2b, p2c, p2d,
  5. output p2y );
  6. wire a,b,c,d;
  7. assign a = p1c&p1b&p1a;
  8. assign b = p2a&p2b;
  9. assign c = p2c&p2d;
  10. assign d = p1f&p1e&p1d;
  11. assign p2y = b|c;
  12. assign p1y = a|d;
  13. endmodule

2 Vector

2.1 Vector0

  1. module top_module (
  2. input wire [2:0] vec,
  3. output wire [2:0] outv,
  4. output wire o2,
  5. output wire o1,
  6. output wire o0 ); // Module body starts after module declaration
  7. assign outv = vec;
  8. assign o2 = vec[2];
  9. assign o1 = vec[1];
  10. assign o0 = vec[0];
  11. endmodule

2.2 Vector1

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

2.3 Vector2

  1. module top_module(
  2. input [31:0] in,
  3. output [31:0] out );//
  4. // assign out[31:24] = ...;
  5. assign out[31:24] = in[7:0];
  6. assign out[23:16] = in[15:8];
  7. assign out[15:8] = in[23:16];
  8. assign out[7:0] = in[31:24];
  9. endmodule

2.4 Vectorgates

  1. module top_module(
  2. input [2:0] a,
  3. input [2:0] b,
  4. output [2:0] out_or_bitwise,
  5. output out_or_logical,
  6. output [5:0] out_not
  7. );
  8. assign out_or_bitwise = a|b;
  9. assign out_or_logical = a||b;
  10. assign out_not[5:3] = ~b;
  11. assign out_not[2:0] = ~a;
  12. endmodule

2.5 Gates4

  1. module top_module(
  2. input [3:0] in,
  3. output out_and,
  4. output out_or,
  5. output out_xor
  6. );
  7. assign out_and = in[3]&in[2]&in[1]&in[0];
  8. assign out_or = in[3]|in[2]|in[1]|in[0];
  9. assign out_xor = in[3]^in[2]^in[1]^in[0];
  10. endmodule

2.6 Vector3

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

2.7 Vectorr

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

2.8 Vector4

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

2.9 Vector5

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

3 Modules:Hierarchy

3.1 Module

  1. module top_module ( input a, input b, output out );
  2. mod_a U1(a,b,out);
  3. endmodule

3.2 Module pos

  1. module top_module (
  2. input a,
  3. input b,
  4. input c,
  5. input d,
  6. output out1,
  7. output out2
  8. );
  9. mod_a U1(out1,out2,a,b,c,d);
  10. endmodule

3.3 Module name

  1. module top_module (
  2. input a,
  3. input b,
  4. input c,
  5. input d,
  6. output out1,
  7. output out2
  8. );
  9. mod_a U1(.out1(out1),.out2(out2),.in1(a),.in2(b),.in3(c),.in4(d));
  10. endmodule

3.4 Module shift

  1. module top_module ( input clk, input d, output q );
  2. wire d1,d2;
  3. my_dff U1(clk,d,d1);
  4. my_dff U2(clk,d1,d2);
  5. my_dff U3(clk,d2,q);
  6. endmodule

3.5 Module shift8

  1. module top_module (
  2. input clk,
  3. input [7:0] d,
  4. input [1:0] sel,
  5. output [7:0] q
  6. );
  7. wire [7:0] q0,q1,q2;
  8. my_dff8 U0(clk,d,q0);
  9. my_dff8 U1(clk,q0,q1);
  10. my_dff8 U8(clk,q1,q2);
  11. always @(sel)begin
  12. case(sel)
  13. 2'b00:
  14. q = d;
  15. 2'b01:
  16. q = q0;
  17. 2'b10:
  18. q = q1;
  19. 2'b11:
  20. q = q2;
  21. default:
  22. q = 0;
  23. endcase
  24. end
  25. endmodule

3.6 Module add

  1. module top_module(
  2. input [31:0] a,
  3. input [31:0] b,
  4. output [31:0] sum
  5. );
  6. wire cout1;
  7. add16 U1( a[15:0], b[15:0], 0, sum[15:0], cout1 );
  8. add16 U2( a[31:16], b[31:16], cout1, sum[31:16], );
  9. endmodule

3.6 Module fadd

  1. module top_module (
  2. input [31:0] a,
  3. input [31:0] b,
  4. output [31:0] sum
  5. );//
  6. wire cout1;
  7. add16 U1( a[15:0], b[15:0], 0, sum[15:0], cout1 );
  8. add16 U2( a[31:16], b[31:16], cout1, sum[31:16], );
  9. endmodule
  10. module add1 ( input a, input b, input cin, output sum, output cout );
  11. // Full adder module here
  12. assign sum = a^b^cin;
  13. assign cout = (a&b)|(a&cin)|(b&cin);
  14. endmodule

3.7 Module cseladd

  1. module top_module(
  2. input [31:0] a,
  3. input [31:0] b,
  4. output [31:0] sum
  5. );
  6. wire cout1;
  7. wire [15:0] sum0,sum1,sum_l,sum_h;
  8. add16 U1( a[15:0], b[15:0], 0, sum_l, cout1 );
  9. add16 U2( a[31:16], b[31:16], 0,sum0, );
  10. add16 U3( a[31:16], b[31:16], 1,sum1, );
  11. always @(*)begin
  12. if(cout1)
  13. begin
  14. sum_h = sum1;
  15. end
  16. else
  17. begin
  18. sum_h = sum0;
  19. end
  20. end
  21. assign sum = {sum_h,sum_l};
  22. endmodule

3.8 Module addsub

  1. module top_module(
  2. input [31:0] a,
  3. input [31:0] b,
  4. input sub,
  5. output [31:0] sum
  6. );
  7. wire cout1;
  8. wire [31:0] b1;
  9. always @(*) begin
  10. if(sub)
  11. b1 = ~b;
  12. else
  13. b1 = b;
  14. end
  15. add16 U1( a[15:0], b1[15:0], sub, sum[15:0], cout1 );
  16. add16 U2( a[31:16], b1[31:16], cout1, sum[31:16], );
  17. endmodule

4 Procedures

4.1 Alwaysblock1

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module(
  3. input a,
  4. input b,
  5. output wire out_assign,
  6. output reg out_alwaysblock
  7. );
  8. assign out_assign = a&b;
  9. always @(*)begin
  10. out_alwaysblock = a&b;
  11. end
  12. endmodule

4.2 Alwaysblock2

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module(
  3. input clk,
  4. input a,
  5. input b,
  6. output wire out_assign,
  7. output reg out_always_comb,
  8. output reg out_always_ff );
  9. assign out_assign = a^b;
  10. always @(*)begin
  11. out_always_comb = a^b;
  12. end
  13. always @(posedge clk)begin
  14. out_always_ff = a^b;
  15. end
  16. endmodule

4.3 Always if

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module(
  3. input a,
  4. input b,
  5. input sel_b1,
  6. input sel_b2,
  7. output wire out_assign,
  8. output reg out_always );
  9. assign out_assign = (sel_b1&sel_b2)? b:a;
  10. wire [1:0] sel;
  11. assign sel = {sel_b1,sel_b2};
  12. always @(*)begin
  13. case(sel)
  14. 0:out_always = a;
  15. 1:out_always = a;
  16. 2:out_always = a;
  17. 3:out_always = b;
  18. endcase
  19. end
  20. endmodule

4.4 Always if2

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module (
  3. input cpu_overheated,
  4. output reg shut_off_computer,
  5. input arrived,
  6. input gas_tank_empty,
  7. output reg keep_driving ); //
  8. always @(*) begin
  9. if (cpu_overheated)
  10. shut_off_computer = 1;
  11. else
  12. shut_off_computer = 0;
  13. end
  14. always @(*) begin
  15. if (~arrived)
  16. keep_driving = ~gas_tank_empty;
  17. else
  18. keep_driving = 0;
  19. end
  20. endmodule

4.5 Always case

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module (
  3. input [2:0] sel,
  4. input [3:0] data0,
  5. input [3:0] data1,
  6. input [3:0] data2,
  7. input [3:0] data3,
  8. input [3:0] data4,
  9. input [3:0] data5,
  10. output reg [3:0] out );//
  11. always@(*) begin // This is a combinational circuit
  12. case(sel)
  13. 3'b000:out = data0;
  14. 3'b001:out = data1;
  15. 3'b010:out = data2;
  16. 3'b011:out = data3;
  17. 3'b100:out = data4;
  18. 3'b101:out = data5;
  19. default:out = 0;
  20. endcase
  21. end
  22. endmodule

4.6 Always case2

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module (
  3. input [3:0] in,
  4. output reg [1:0] pos );
  5. always @(*) begin
  6. case(in)
  7. 4'b0000 : pos = 0;
  8. 4'b0001 : pos = 0;
  9. 4'b0010 : pos = 1;
  10. 4'b0011 : pos = 0;
  11. 4'b0100 : pos = 2;
  12. 4'b0101 : pos = 0;
  13. 4'b0110 : pos = 1;
  14. 4'b0111 : pos = 0;
  15. 4'b1000 : pos = 3;
  16. 4'b1001 : pos = 0;
  17. 4'b1010 : pos = 1;
  18. 4'b1011 : pos = 0;
  19. 4'b1100 : pos = 2;
  20. 4'b1101 : pos = 0;
  21. 4'b1110 : pos = 1;
  22. 4'b1111 : pos = 0;
  23. endcase
  24. end
  25. endmodule

4.7 Always casez

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

4.8 Always nolatches

  1. // synthesis verilog_input_version verilog_2001
  2. module top_module (
  3. input [15:0] scancode,
  4. output reg left,
  5. output reg down,
  6. output reg right,
  7. output reg up );
  8. always @(*)begin
  9. up = 1'b0; down = 1'b0; left = 1'b0; right = 1'b0;
  10. case (scancode)
  11. 16'he06b:left = 1;
  12. 16'he072:down = 1;
  13. 16'he074:right = 1;
  14. 16'he075:up = 1;
  15. default: ;
  16. endcase
  17. end
  18. endmodule

5 More Verilog Features

5.1 Conditional

  1. module top_module (
  2. input [7:0] a, b, c, d,
  3. output [7:0] min);//
  4. // assign intermediate_result1 = compare? true: false;
  5. wire [7:0] min1,min2;
  6. assign min1 =(a<b)? a: b;
  7. assign min2 =(min1<c)? min1: c;
  8. assign min =(min2<d)? min2: d;
  9. endmodule

5.2 Reduction

  1. module top_module (
  2. input [7:0] in,
  3. output parity);
  4. assign parity = ^in[7:0];
  5. endmodule

5.3 Gates100

  1. module top_module(
  2. input [99:0] in,
  3. output out_and,
  4. output out_or,
  5. output out_xor
  6. );
  7. assign out_and = &in[99:0];
  8. assign out_or = |in[99:0];
  9. assign out_xor = ^in[99:0];
  10. endmodule

5.4 Vector100r

  1. module top_module(
  2. input [99:0] in,
  3. output [99:0] out
  4. );
  5. integer i;
  6. always @(*)begin
  7. for(i = 0;i<100;i++)
  8. out[i] = in[99-i];
  9. end
  10. endmodule

5.5 Popcount255

  1. module top_module(
  2. input [254:0] in,
  3. output [7:0] out );
  4. integer i;
  5. always @(*)begin
  6. out = 0;
  7. for(i=0;i<255;i++)
  8. if(in[i]==1) out++;
  9. else out = out;
  10. end
  11. endmodule

5.6 Adder100i

  1. module top_module(
  2. input [99:0] a, b,
  3. input cin,
  4. output [99:0] cout,
  5. output [99:0] sum );
  6. integer i;
  7. add8 L1(a[7:0],b[7:0],cin,sum[7:0],cout[7:0]);
  8. add8 L2(a[15:8],b[15:8],cout[7],sum[15:8],cout[15:8]);
  9. add8 L3(a[23:16],b[23:16],cout[15],sum[23:16],cout[23:16]);
  10. add8 L4(a[31:24],b[31:24],cout[23],sum[31:24],cout[31:24]);
  11. add8 L5(a[39:32],b[39:32],cout[31],sum[39:32],cout[39:32]);
  12. add8 L6(a[47:40],b[47:40],cout[39],sum[47:40],cout[47:40]);
  13. add8 L7(a[55:48],b[55:48],cout[47],sum[55:48],cout[55:48]);
  14. add8 L8(a[63:56],b[63:56],cout[55],sum[63:56],cout[63:56]);
  15. add8 L9(a[71:64],b[71:64],cout[63],sum[71:64],cout[71:64]);
  16. add8 L10(a[79:72],b[79:72],cout[71],sum[79:72],cout[79:72]);
  17. add8 L11(a[87:80],b[87:80],cout[79],sum[87:80],cout[87:80]);
  18. add8 L12(a[95:88],b[95:88],cout[87],sum[95:88],cout[95:88]);
  19. add1 P1(a[96],b[96],cout[95],sum[96],cout[96]);
  20. add1 P2(a[97],b[97],cout[96],sum[97],cout[97]);
  21. add1 P3(a[98],b[98],cout[97],sum[98],cout[98]);
  22. add1 P4(a[99],b[99],cout[98],sum[99],cout[99]);
  23. endmodule
  24. module add1 ( input a, input b, input cin, output sum, output cout );
  25. // Full adder module here
  26. assign sum = a^b^cin;
  27. assign cout = (a&b)|(a&cin)|(b&cin);
  28. endmodule
  29. module add8 ( input [7:0]a, input [7:0] b, input cin ,output [7:0] sum,output [7:0] cout);
  30. add1 U1(a[0],b[0],cin,sum[0],cout[0]);
  31. add1 U2(a[1],b[1],cout[0],sum[1],cout[1]);
  32. add1 U3(a[2],b[2],cout[1],sum[2],cout[2]);
  33. add1 U4(a[3],b[3],cout[2],sum[3],cout[3]);
  34. add1 U5(a[4],b[4],cout[3],sum[4],cout[4]);
  35. add1 U6(a[5],b[5],cout[4],sum[5],cout[5]);
  36. add1 U7(a[6],b[6],cout[5],sum[6],cout[6]);
  37. add1 U8(a[7],b[7],cout[6],sum[7],cout[7]);
  38. endmodule

5.8 Bcdadd100

  1. module top_module(
  2. input [399:0] a, b,
  3. input cin,
  4. output cout,
  5. output [399:0] sum );
  6. wire [399:0] cout_tmp;
  7. bcd_fadd U1(.a(a[3:0]), .b(b[3:0]), .cin(cin), .cout(cout_tmp[0]),.sum(sum[3:0]));
  8. generate
  9. genvar i;
  10. for(i = 4; i < 400; i=i+4) begin : adder
  11. 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]));
  12. end
  13. endgenerate
  14. assign cout = cout_tmp[396];
  15. 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. KingbaseES V8R6集群维护之--修改数据库服务端口案例

    ​ 案例说明: 对于KingbaseES数据库单实例环境,只需要修改kingbase.conf文件的'port'参数即可,但是对于KingbaseES V8R6集群中涉及到多个配置文件的修改,并且在应 ...

  2. 巧用KingbaseES中的动态DDL

    概述 :在DBA的日常工作中,经常遇到一些需要基于数据库当前状态的实用程序查询的实例.比如一个逻辑复制的目标表,主键ID列与生成数据的序列不同步,这将导致插入新行是,会有主键冲突.要纠正这个问题,需要 ...

  3. 【读书笔记】C#高级编程 第十五章 反射

    (一)在运行期间处理和检查代码 自定义特性允许把自定义元数据与程序元素关联起来.反射是一个普通术语,它描述了在运行过程中检查和处理程序元素的功能.例如,反射允许完成的任务: 枚举类型的成员 实例化新对 ...

  4. 从零开始搭建gitea代码管理平台

    Gitea,一款极易搭建的Git自助服务.如其名,Git with a cup of tea.跨平台的开源服务,支持Linux.Windows.macOS和ARM平台.配置要求低,甚至可以运行在树莓派 ...

  5. C 语言 struct 第一个成员变量的妙用

    一.双重身份 如下定义了一个 School 结构体: typedef struct School { int a; int b; }SCHOOL_S; SCHOOL_S stSch; 下面我们来输出一 ...

  6. Windows Server体验之应用兼容性按需功能

    Windows Server默认仅能支持几个有图形界面的应用包括注册表编辑器regedit.记事本notepad.任务管理器taskmgr.时间设置control timedate.cpl.区域设置c ...

  7. 2022CSP-J初赛游记

    2022年9月16日: 下午,在学校集训,刘洋让我看了一下时间,突然发现,距离初赛就剩2天了...... 晚上,辅导班的老师对我们进行最后的培训,做了2套有道小图灵模拟题,但是做的不理想,很慌. 20 ...

  8. Typora Markdown 安装包

    下载地址: 链接:https://pan.baidu.com/s/1wy0Ik95AjM5WjSC3nzOzqA 提取码:f26j 复制这段内容后打开百度网盘手机App,操作更方便哦 已更新至最新版0 ...

  9. 跟羽夏学 Ghidra ——调试

    写在前面   此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇文章 ...

  10. Apollo 中配置String、Map和List和默认值

    摘要:在Apollo 中,配置String.Map和List等类型的信息,同时设置默认值. 综述   随着业务需求的变更,需要在Apollo中配置一个Map<String, List>类型 ...