列表

详情


VL51. 可置位计数器

描述

请编写一个十六进制计数器模块,计数器输出信号递增每次到达0,给出指示信号zero,当置位信号set 有效时,将当前输出置为输入的数值set_num。
模块的接口信号图如下:
     
模块的时序图如下:
 
请使用Verilog HDL实现以上功能,并编写testbench验证模块的功能

输入描述

clk:时钟信号
rst_n:复位信号,低电平有效
set:置位指示信号,当该信号有效时,表示将输出信号强制置为set_num
set_num:4比特信号,当set信号有效时,将该信号的数字赋予输出信号number

输出描述

zero:过零指示信号,当number计数到0时,该信号为1,其余时刻为0
number:4比特位宽,表示计数器的当前读数

原站题解

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,
	input set,
	input [3:0] set_num,
	output reg [3:0]number,
	output reg zero
	);
    
    reg      [3:0]   number_reg        ;
    always@(posedge clk or negedge rst_n)   
        begin
            if(rst_n==1'd0)
              number_reg <=4'd0            ;
            else if(set==1'd1)
              number_reg<= set_num         ;
            else 
              number_reg<=  number_reg + 1'd1  ;  
            
        end  
  always@(posedge clk or negedge rst_n)   
        begin
            if(rst_n==1'd0)
              number <=4'd0            ;
            else 
              number<=  number_reg         ;              
        end 
                
    always@(*)
        begin
           if(rst_n==1'd0)
              zero <= 1'd0          ;
            else if(number==4'd0)
               zero <= 1'd1         ; 
            else 
                zero <= 1'd0        ; 
        end
    
    
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,
	input set,
	input [3:0] set_num,
	output reg [3:0]number,
	output reg zero
	);
    
    reg [3:0] num;
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            num<=0;
        else if(set)
            num<=set_num;
        else 
            num<=num+1'b1;
    end
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            zero<=0;
        else if(num==0)
            zero<=1;
        else
            zero<=0;
    end
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            number<=0;
        else
            number<=num;
    end
   
    
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,
	input set,
	input [3:0] set_num,
	output reg [3:0]number,
	output reg zero
	);
    
    reg    [3:0]    number_reg;
    
    always@(*) begin
        if(!rst_n)
            zero = 1'b0;
        else if(number == 4'd0)
            zero = 1'b1;
        else
            zero = 1'b0;        
    end
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            number_reg    <= 4'd0;
        else if(set)
            number_reg    <= set_num;
        else
            number_reg    <= number_reg + 1'b1;        
    end
    
    always@(posedge clk or negedge rst_n) begin
        if(!rst_n)
            number    <= 4'd0;
        else
            number    <= number_reg;        
    end
    
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,
	input set,
	input [3:0] set_num,
	output reg [3:0]number,
	output reg zero
	);
    
    reg [3:0] num_reg;
    always @(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            num_reg <= 4'd0;
            zero <= 1'b0;
        end
        else begin
            if(set) begin
                num_reg <= set_num;
            end
            else begin
                num_reg <= num_reg + 1'b1;
            end
            
            zero <= (num_reg==4'd0)? 1'b1: 1'b0;
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if(!rst_n) begin
            number <= 4'd0;
        end
        else begin
            number <= num_reg;
        end
    end
    
    
    
endmodule

Verilog 解法, 执行用时: 0ms, 内存消耗: 0KB, 提交时间: 2022-08-06

`timescale 1ns/1ns

module count_module(
	input clk,
	input rst_n,
	input set,
	input [3:0] set_num,
	output reg [3:0]number,
	output reg zero
	);
reg [3:0] number_reg;

always @(posedge clk or negedge rst_n) 
begin
   if(!rst_n)
     number_reg<=0;
   else if(set)
     number_reg<=set_num;
   else if(number_reg==4'hF)
     number_reg<=0;
   else 
     number_reg<=number_reg+1;
end

always @(posedge clk or negedge rst_n) 
   if(!rst_n)
     number<=0;
   else 
     number<=number_reg;

always @(posedge clk or negedge rst_n) 
   if(!rst_n)
     zero<=0;
   else if(number_reg==0)
     zero<=1;
   else 
     zero<=0;

endmodule

上一题