verilog编程题-计数器

题目

  1. 编写一个0 1 2 3 3 2 1 0输出的循环计数器
  2. 编写一个0 1 2 3 4 5 6 6 5 4 3 2 1 0输出的循环计数器

代码

DUT


//function: count 0 1 2 3 3 2 1 0
module Count(
	input wire clk,
	input wire rst_n,
	output wire [1:0] out
	);

	reg [2:0] count;

	always_ff @(posedge clk or negedge rst_n) begin 
		if(~rst_n) begin
			 count <= 0;
		end else  begin
			 count <= count + 1'b1 ;
		end
	end

	assign out = count[2] ? ~count[1:0] : count[1:0];

endmodule
//function2: count 0 1 2 3 4 5 6 6 5 4 3 2 1 0
module Count6(
	input wire clk,
	input wire rst_n,
	output wire [2:0] out
	);
	reg [2:0] count1;
	reg [2:0] count2;
	reg flag;

	always_ff @(posedge clk or negedge rst_n) begin : proc_count1
		if(~rst_n) begin
			count1 <= 'd0;
		end else if (count1 == 'd6)begin
			count1 <= 'd0;
		end else begin
			count1 <= count1 + 1'b1 ;
		end
	end
	always_ff @(posedge clk or negedge rst_n) begin : proc_flag
		if(~rst_n) begin
			flag <= 0;
		end else if (count1=='d6) begin
			flag <= ~flag;
		end
		else
			flag <= flag;
	end

	always_ff @(posedge clk or negedge rst_n) begin : proc_count2
		if(~rst_n) begin
			count2 <= 'd6;
		end else if (count2 == 'd0)begin
			count2 <= 'd6;
		end else begin
			count2 <= count2 - 1'b1;
		end
	end

	assign out = flag ? count2 : count1;

endmodule

TB


module tb_Count();

	reg clk, rst_n;
	wire [1:0] out;
	wire [2:0] out2;

	initial begin 
		clk = 'b0;
		rst_n = 'b0;
		# 10
		rst_n = 'b1;
	end

	always #5 clk = ~clk;

	Count dut1(
		clk,
		rst_n,
		out
		);
	Count6 dut2(
		clk,
		rst_n,
		out2
		);

endmodule

仿真

在这里插入图片描述

小结

这是华为的一道手撕代码题,一开始写的时候一直想用一个计数器完成,然后加一个flag进行判断什么时候加什么时候减,但是因为最大值或者最小值重复了一次,flag就很不好判断。反正就是被局限住了,最后想到可以用两个计数器就方便很多。欢迎大家在评论区提出更好的解决方案。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值