verilog语法实例学习(9)

常用的时序电路介绍


寄存器

     一个触发器可以存储一位数据,由n个触发器组成的电路可以存储n位数据,我们把这一组触发器叫做寄存器。寄存器中每个触发器共用同一个时钟。

     下面是n位寄存器的代码,我们通过一个参数定义n,在实例化时传入参数n。


module regne (D, clk,Rst_n,E,Q);
  parameter n=4;
  input [n-1:0] D;
  input clk;
  input Rst_n; //复位信号
  input E; //使能信号

  output reg [n-1:0] Q;

  always @(posedge clk,negedge Rst_n)
    if(Rst_n==0)
	   Q <= 0;
	 else if(E)
	   Q <= D;

endmodule
`timescale 1ns/1ns
`define clock_period 20

module regne_tb;
  reg [7:0] D;
  wire [7:0] Q;
  reg clk;
  reg Rst_n;
  reg E;

  regne #(.n(8)) regne(.D(D),.clk(clk),.Rst_n(Rst_n),.E(E),.Q(Q));
  always # (`clock_period/2) clk = ~clk;

  initial
  begin
    D = 4'b01010101;
	 clk = 1'b0;
	 Rst_n = 1'b1;
	 E = 1'b1;
	 #(`clock_period)
	 Rst_n = 1'b0;
	 D = 4'b10101010;
	 #(`clock_period*2)
	 E = 1'b0;
	 Rst_n = 1'b1;
	 D = 4'b00010001;
	 #(`clock_period*4)
	 E = 1'b1;
	 D = 4'b1111;
	 #(`clock_period*2)
	 D = 4'b10111011;
	 #(`clock_period*2)
	 D = 4'b10011001;
	 #(`clock_period*2)
    $stop;
  end

endmodule

image_thumb11

移位寄存器

把串行的输入存储到一个寄存器,可以选择时机并行输出。

/*串行输入,并行输出寄存器
从高位输入,即从右向左输入*/
module shiftn(R,L,w,clk,Q);
  parameter n=8;
  input [n-1:0] R;//初始值
  input L; //load信号
  input w; //移入信号
  input clk;//时钟信号
  output reg [n-1:0] Q;
  integer k;

  always @(posedge clk)
  begin
    if(L)
	    Q <=R;
	 else
	 begin
	   for(k=0; k<n-1; k=k+1)
		  Q[k] <= Q[k+1];
	   Q[n-1] <= w;
	 end

  end


endmodule
`timescale 1ns/1ns
`define clock_period 20

module shiftn_tb;

  reg [7:0] R;
  reg L;
  reg w;
  reg clk;
  wire [7:0] Q;

  shiftn #(.n(8)) shitn0(.R(R),.L(L),.w(w),.clk(clk),.Q(Q));
  initial clk = 0;
  always #(`clock_period/2) clk = ~clk;

  initial
  begin
    R = 8'b00000000;
	 L = 1'b1;
	 w = 1'b0;
	 #(`clock_period)
	 L = 1'b0;
	 w = 1'b1;
	 #(`clock_period)
	 w = 1'b0;
	 #(`clock_period)
	 w = 1'b1;
	 #(`clock_period)
	 w = 1'b1;
	 #(`clock_period)
	 w = 1'b1;
	 #(`clock_period)
	 w = 1'b1;
	 #(`clock_period)
	 w = 1'b1;
	 #(`clock_period)
	 w = 1'b1;
	 #(`clock_period)
	 $stop;
  end

endmodule
View Code


image_thumb14

我们也可以使用拼接符操作代替for循环,实现同样的功能,而且语法更加简单。

/*串行输入,并行输出寄存器
从高位输入,即从右向左输入*/
module shiftn(R,L,w,clk,Q);
  parameter n=8;
  input [n-1:0] R;//初始值
  input L; //load信号
  input w; //移入信号
  input clk;//时钟信号
  output reg [n-1:0] Q;


  always @(posedge clk)
  begin
    if(L)
	    Q <=R;
	 else
	 begin

     Q = {w,Q[n-1:1]};
	 end

  end


endmodule

猜你喜欢

转载自www.cnblogs.com/mikewolf2002/p/10197735.html