• Verilog HDL经典电路设计


    1.1 全加器的设计 

    description:example of a one-bit full add

    1. module FULLADDR(Cout,Sun,Ain,Bin,Cin);
    2. input Ain,Bin,Cin;
    3. output Sun,Cout;
    4. wire Sum;
    5. wire Count;
    6. assign Sum=Ain^Bin^Cin;
    7. assign Cout=(Ain&Bin)|(Bin&Cin)|(Ain&Cin);
    8. endmodule

    1.2 数据通路

    1.2.1 四选一的多路选择器

    description::example of a mux4-1

    1. module MUX(C,D,E,F,S,Mux_out);
    2. input C,D,E,F,; //input
    3. input [1:0] s; //select control
    4. output Mux_out; //result
    5. reg Mux_out; //mux
    6. always@(C or D or E or F or S)
    7. begin
    8. case(S)
    9. 2`b00:Mux_out=C;
    10. 2`b01:Mux_out=D;
    11. 2`b10:Mux_out=f;
    12. default:Mux_out=S;
    13. endcase
    14. end
    15. endmodule

    1.2.2 译码器

    description:example of 3-8decoder

    1. module DECODE(Ain,En,Yout);
    2. input En; //input
    3. input [2:0] Ain; //input code
    4. output [7:0] Yout;
    5. reg [7:0] Yout;
    6. always@(En or Ain)
    7. begin
    8. if(!En)
    9. Yout=8`0;
    10. else
    11. case(Ain)
    12. 3`b000:Yout=8`b0000_0001;
    13. 3`b001:Yout=8`b0000_0010;
    14. 3`b010:Yout=8`b0000_0100;
    15. 3`b011:Yout=8`b0000_1000;
    16. 3`b100:Yout=8`b0001_0000;
    17. 3`b101:Yout=8`b0010_0000;
    18. 3`b110:Yout=8`b0100_0000;
    19. 3`b111:Yout=8`b1000_0000;
    20. default:Yout=8`b0000_0000;
    21. endcase
    22. end
    23. endmodule

    1.2.3 优先编码器

    description:example of Priority encoder

    1. module PRIO_ENCODE(Cin,Din,Ein,Fin,Sin,Pout);
    2. input Cin,Din,Ein,Fin; //input signals
    3. input [1:0] Sin; //input select contorl
    4. output Pout; //output select result
    5. reg Pout; //Pout assignment
    6. always@(Sin or Cin or Din or Ein or Fin)
    7. begin
    8. if(Sin==2`b00)
    9. Pout=Cin;
    10. else if(Sin==2`b01)
    11. Pout=Din;
    12. else if(Sin==2`b10)
    13. Pout=Ein;
    14. else
    15. Pout=Fin;
    16. end
    17. endmodule //module prio_encode

    1.3 计数器

    description:example of a counter with enable

    1. module COUNT_EN(En,Clock,Reset,Out);
    2. parameter Width =8;
    3. parameter U_DLY=1;
    4. input Clock,Reset,En;
    5. output [Width-1:0] Out;
    6. reg [Width-1:0] Out;
    7. always@(posedge Clock or negedge Reset)
    8. if(!Reset)
    9. Out<=8`b0;
    10. else if(En)
    11. Out<=#U_DLY Out+1;
    12. endmodule

    1.4 算术操作

    description:example of a arithmetic include +-*/

    1. module ARITHMETIC(A,B,Q1,Q2,Q3,Q4);
    2. input [3:0] A,B; //input opeartor
    3. output [4:0] Q1; //output sum,with carry bit
    4. output [3:0] Q2; //output sutract result
    5. output [3:0] Q3; //output quotion
    6. output [7:0] Q4; //product
    7. reg [4:0] Q1;
    8. reg [3:0] Q2,Q3;
    9. reg [7:0] Q4; //arithmetic operate
    10. always@(A or B)
    11. begin
    12. Q1=A+B;
    13. Q2=A-B;
    14. Q3=A/2;
    15. Q4=A*B;
    16. end
    17. endmodule

    1.5 逻辑操作

    description:example of a relational operate

    1. module RELATIONAL(A,B,Q1,Q2,Q3,Q4);
    2. input [3:0] A,b; //operator
    3. output Q1,Q2,Q3,Q4; //result
    4. reg Q1,Q2,Q3,Q4; //compare
    5. always@(A or B)
    6. begin
    7. Q1=A>B;
    8. Q2=A
    9. Q3=A>=B;
    10. if(A<=B)
    11. Q4=1;
    12. else
    13. Q4=0;
    14. end
    15. endmodule

    1.6 移位操作

    description:example of a shifter 

    1. module SHIFT(Data,Q1,Q2);
    2. input [3:0] Data;
    3. output [3:0] Q1,Q2;
    4. parameter B=2;
    5. reg [3:0] Q1,Q2;
    6. always@(Data)
    7. bagin
    8. Q1=Data<
    9. Q2=Data>>B;
    10. end
    11. endmodule

    1.7 时序器件

    一个时序器件(指触发器或锁存器)就是一个一位存储器。锁存器是电平敏感存储器件,触发器是沿触发存储器件。

    触发器也称寄存器,在程序中体现为对上升沿或下降沿的探测,Verilog中如下方式表示

    (posedge Clk)——————上升沿

    (negedge Clk)——————下降沿

    下面给出不同类型触发器的描述

    1.7.1 上升沿触发的触发器

    description:example of a rising flip-flop

    1. module DFF(Data, Clk,Q);
    2. input Data,Clk;
    3. output Q;
    4. reg Q;
    5. always@(posedge Clk)
    6. Q <= Data;
    7. endmodule

    1.7.2 带异步复位,上升沿触发的触发器

    description:example of a rising edge flip-flop with asynchronous reset

    1. module DFF_ASYNC_RST(Data,Clk,Rest,Q);
    2. input Data,Clk,Reset;
    3. output Q;
    4. parameter U_DLY=1;
    5. reg Q;
    6. always@(posedge Clk or negedge Reset)
    7. if(~Reset)
    8. Q <= #U_DLY 1`b0;
    9. else
    10. Q <= #U_DLY Data;
    11. endmodule

    1.7.3 带异步置位,上升沿触发的触发器

    description:example of a rising edge flip-flop with asynchronous preset 

    1. module DFF_ASYNC_PRE(Data,Clk,Preset,Q);
    2. input Data,Clk,Preset;
    3. output Q;
    4. parameter U_DLY=1;
    5. reg Q;
    6. always@(posedge Clk or negedge Preset)
    7. if(~Prest)
    8. Q <= #U-DLY 1`b1;
    9. else
    10. Q <= #U_CLY Data;
    11. endmodule

    1.7.4 带异步复位和置位,上升沿触发的触发器

    description:example of a rising edge flip-flop with asynchronous reset  and preset

    1. module DFF_ASYNC(Data,Clk,Reset,Prest,Q);
    2. input Data,Clk,Reset,Preset;
    3. output Q;
    4. parameter U_DLY=1;
    5. reg Q;
    6. always@(posedge Clk or negedge Reset or posedge Preset)
    7. if(~Reset)
    8. Q <= 1`b0;
    9. else if(Preset)
    10. Q <= 1`b1;
    11. else
    12. Q <= #U_DLY Data;
    13. endmodule

    1.7.5 带同步复位,上升沿触发的触发器

    description:example of a rising edge flip-flop with synchronous reset

    1. module DFF_SYNC_RST(Data,Clk,Reset,Q);
    2. input Data,ClK,Reset;
    3. output Q;
    4. parameter U_DLY=1;
    5. reg Q;
    6. always@(posedge Clk)
    7. if(~Reset)
    8. Q <= #U_DLY 1`b0;
    9. else
    10. Q <= #U_DLY Data;
    11. endmodule

    1.7.6 带同步置位,上升沿触发的触发器

    description:example of a rising edge Flip-Flop with synchronous preset

    1. module DFF_SYNC_PRE(Data,Clk,Preset,Q);
    2. input Data,Clk,Preset;
    3. output Q;
    4. parameter U_DLY=1;
    5. reg Q;
    6. always@(posedge Clk)
    7. if(~Preset)
    8. Q <= #U_DLY 1`b1;
    9. else
    10. Q <= #U_DLY Data
    11. endmodule

    1.7.7 带异步复位和时钟使能,上升沿触发的触发器

    description:example of a Rising Edge Flip-Flop with Asynchronous Reset

    1. module DFF_CK_EN(Data,Clk,Reset,En,Q);
    2. input Data,Clk,Reset,En;
    3. output Q;
    4. parameter U_DLY=1;
    5. reg Q;
    6. always@(posedge Clk or negedge Reset)
    7. if(~Reset)
    8. Q <= 1`b0;
    9. else if(En)
    10. Q <= #U_DLY Data;
    11. endmodule

    1.8 ALU

    description:example of a 4-bit Carry Look Ahead ALU

    1. module ALU(A,B,Cin,Sum,Cout,Operate,Mode); //input signals
    2. input [3:0] A,b; //two operands of ALU
    3. input Cin; //carry in at the LSB
    4. input [3:0] Operate; //determine f(.) of sum=f(a,b)
    5. input Mode; //arithmetic(Mode=1`b1) or logic operation(Mode=1`b0)
    6. output [3:0] Sum; //result of ALU
    7. output Cout; //carry produced by ALU operation
    8. //carry generation bits and propogation bits.
    9. wire [3:0] G,P;
    10. //carry bits ;
    11. reg [2:0] C;
    12. //funtion for carry generation:
    13. function gen
    14. input A,B;
    15. input [1:0] Oper;
    16. begin
    17. case(Oper)
    18. 2`b00:gen=A;
    19. 2`b01:gen=A&B;
    20. 2`b10:gen=A&(~B);
    21. 2`b11:gen=1`b0;
    22. endcase
    23. end
    24. endfunction
    25. //function for carry propergation:
    26. function prop
    27. input A,B;
    28. input [1:0] Oper;
    29. begin
    30. case(Oper)
    31. 2`b00:prop=1;
    32. 2`b01:prop=A|(~B);
    33. 2`b10:prop=A|B;
    34. 2`b11:prop=A;
    35. endcase
    36. end
    37. endfunction
    38. //producing carry generation bits;
    39. assign G[0]=gen(A[0],B[0],Oper[1:0]);
    40. assign G[1]=gen(A[1],B[1],Oper[1:0]);
    41. assign G[2]=gen(A[2],B[2],Oper[1:0]);
    42. assign G[3]=gen(A[3],B[2],Oper[1:0]);
    43. //producing carry propogation bits
    44. assign P[0]=pro(A[0],B[0],Oper[3:2]);
    45. assign P[1]=Pro(A[1],B[1],Oper[3:2]);
    46. assign P[2]=Pro(A[2],B[2],Oper[3:2]);
    47. assign P[3]=Pro(A[3],B[3],Oper[3:2]);
    48. //producing carry bits with carry-look -ahead;
    49. always@(G or P or Cin,Mode)
    50. begin
    51. if(Mode)begin
    52. C[0]=G[0]|P[0] & Cin;
    53. C[1]=G[1]|P[1] & G[0]|P[1] & P[0] & Cin;
    54. C[2]=G[2]|P[2] & G[1]|P[2] & P[1] & G[0]|P[2] & P[1] & P[0] & Cin;
    55. Cout=G[3]|P[3] & G[2]|P[3] & P[2] & G[1]|P[3] & P[2] & P[1] & G[0]|P[3] & P[2] & P[1] & P[0] & Cin;
    56. end
    57. elsebegin
    58. C[0]=1`b0;
    59. C[1]=1`b0;
    60. C[2]=1`b0;
    61. Cout=1`b0;
    62. end
    63. end
    64. //calculate the operation results;
    65. assign Sum[0]=(~G[0]&P[0])^Cin;
    66. assign Sum[1]=(~G[1]&P[1])^C[0];
    67. assign Sum[2]=(~G[2]&P[2])^C[1];
    68. assign Sum[3]=(~G[3]&P[3])^C[2];
    69. endmodule

  • 相关阅读:
    数据结构-单链表
    easyexcel导入导出百万条数据思路分析
    MySQL定期删除旧数据的过程和事件
    酷开系统更多惊喜,尽享畅快观影之旅
    Git常用指令(基础)
    尚硅谷Vue系列教程学习笔记(3)
    C++总结(8):STL容器适配器之stack、queue、priority_queue详解
    【Vue】使用单文件组件编写 TodoList
    vue实现列表数据分页
    面试20个人居然没有一个写出数组扁平化?如何手写flat函数
  • 原文地址:https://blog.csdn.net/m0_61687959/article/details/125806542