一些VERILOG例题

  • 格式:doc
  • 大小:4.81 MB
  • 文档页数:23

1 Verilog HDL描述例子 1. 组合电路的例子 例1 4选1数据选择器(if叙述)。 module mux (a, b, c, d, s, o); input a,b,c,d; input [1:0] s; output o; reg o; always @(a or b or c or d or s) begin if (s == 2'b00) o = a; else if (s == 2'b01) o = b; else if (s == 2'b10) o = c; else o = d; end endmodule

例2具有三态缓冲4选1数据选择器。 module mux (a, b, c, d, s, o); input a,b,c,d; input [3:0] s; output o; assign o = s[3] ? a :1'bz; assign o = s[2] ? b :1'bz; assign o = s[1] ? c :1'bz; assign o = s[0] ? d :1'bz; endmodule

例3 采用case语句描述的3-8译码器,输出高电平有效。 module mux (sel, res); input [2:0] sel; output [7:0] res; reg [7:0] res; always @(sel or res) begin case (sel) 3'b000 : res = 8'b00000001; 3'b001 : res = 8'b00000010; 3'b010 : res = 8'b00000100; 3'b011 : res = 8'b00001000; 3'b100 : res = 8'b00010000; 3'b101 : res = 8'b00100000; 3'b110 : res = 8'b01000000; 2

default : res = 8'b10000000; endcase end endmodule

例4 进位输入与输出的8位加法器 module adder(A, B, CI, SUM, CO); input CI; input [7:0] A; input [7:0] B; output [7:0] SUM; output CO; wire [8:0] tmp; assign tmp = A + B + CI; assign SUM = tmp [7:0]; assign CO = tmp [8]; endmodule

例5 8位比较器 module compar(A, B, CMP); input [7:0] A; input [7:0] B; output CMP; assign CMP = A >= B ? 1'b1 : 1'b0; endmodule

2 时序电路的例子 例1 上升沿触发的具有异步置位与时钟使能端的4位寄存器 module li5 (C, D, CE, PRE, Q); input C, CE, PRE; input [3:0] D; output [3:0] Q; reg [3:0] Q; always @(posedge C or posedge PRE) begin if (PRE) Q = 4'b1111; else if (CE) Q = D; end endmodule

例2 具有清除端的4位加法计数器。 module li7 (C, CLR, Q); input C, CLR; output [3:0] Q; reg [3:0] tmp; 3

always @(posedge C or posedge CLR) begin if (CLR) tmp = 4'b0000; else tmp = tmp + 1'b1; end assign Q = tmp; endmodule

例3 具有同步置位的4位减法计数器。 module li8 (C, S, Q); input C, S; output [3:0] Q; reg [3:0] tmp;

always @(posedge C) begin if (S) tmp = 4'b1111; else tmp = tmp - 1'b1; end assign Q = tmp; endmodule

例4 具有异步置位的4位加法计数器。 module li9 (C, ALOAD, D, Q); input C, ALOAD; input [3:0] D; output [3:0] Q; reg [3:0] tmp;

always @(posedge C or posedge ALOAD) begin if (ALOAD) tmp = D; else tmp = tmp + 1'b1; end assign Q = tmp; endmodule

例5 同步置入一个常数的4位加法计数器。 module lia10 (C, SLOAD, Q); input C, SLOAD; output [3:0] Q; reg [3:0] tmp;

always @(posedge C) begin if (SLOAD) tmp = 4'b1010; else tmp = tmp + 1'b1; end assign Q = tmp; 4

endmodule 例6 具有异步清除端的4位加减计数器。 module lia11 (C, CLR, UP_DOWN, Q); input C, CLR, UP_DOWN; output [3:0] Q; reg [3:0] tmp;

always @(posedge C or posedge CLR) begin if (CLR) tmp = 4'b0000; else if (UP_DOWN) tmp = tmp + 1'b1; else tmp = tmp - 1'b1; end assign Q = tmp; endmodule

例7 控制信号t控制置数与减法计数的100进制计数器。 module count(clk,t,td,qq1,qq2); //t控制置数与计数,td减计数到0时,td=1 input t,clk; output td; output [3:0] qq1,qq2; //qq2是高4位,qq1是低4位 wire [3:0] qq1,qq2; reg td; reg [7:0] qq; assign{qq2,qq1}=qq; always @ (posedge clk or negedge t) begin if (t==0) begin qq<=99; td<=0; end else if ((t==1) && (qq >=1)) begin qq <=qq-1; td<=0; end else td <=1; end endmodule

例8 正沿触发、串入、串出的8位左移移位寄存器 module shift (C, SI, SO); input C,SI; output SO; reg [7:0] tmp; always @(posedge C) begin tmp = tmp << 1; tmp[0] = SI; end assign SO = tmp[7]; endmodule 5

例9 负沿触发,串入、串出、左移、具有时钟使能的移位寄存器 module shift (C, CE, SI, SO); input C,SI, CE; output SO; reg [7:0] tmp; always @(negedge C) begin if (CE) begin tmp = tmp << 1; tmp[0] = SI; end end assign SO = tmp[7]; endmodule

例10正沿触发,串入、串出、左移、具有异步清除的移位寄存器 module shift (C, CLR, SI, SO); input C,SI,CLR; output SO; reg [7:0] tmp; always @(posedge C or posedge CLR) begin if (CLR) tmp = 8'b00000000; else begin tmp = {tmp[6:0], SI}; end end assign SO = tmp[7]; endmodule

例11正沿触发,串入、串出、左移、具有同步置位的移位寄存器 module shift (C, S, SI, SO); input C,SI,S; output SO; reg [7:0] tmp; always @(posedge C) begin if (S) tmp = 8'b11111111; else begin tmp = {tmp[6:0], SI}; //移位