题意整理

       实现不同波形的输出可以使用case语句,以wave_choise作为case语句的cans.根据wave_choise的值输出不同的波形。以0为最低值,20为最高值为例(可以根据需要具体修改)。根据不同波形的需要,产生相应的数据:

       方波:产生占空比50%的方波,输出0保持20个时钟,输出20保持20个时钟。

       锯齿波:输出数据从0按照步长1递增至20,再从20跳变到0,再依次递增,以此循环。

       三角波:输出数据从0按照步长1递增至20,再从20递减到0,以此循环。

题解主体

       首先是方波的实现,较为简单,只需要设置一个计数器,使输出保持20个时钟为0,跳变为20,再保持20个时钟。依次循环。可以按照如下的过程实现:cnt每个时钟加一,当cnt=19时,将wave的值置为0,同时cnt置为0;当cnt=9时,将wave的值置为20。

                     reg [4:0]cnt;

                            if (cnt == 5'd19)

                                   begin    

                                          wave <= 5'd0;

                                          cnt <= 5'd0;

                                   end

                            else if(cnt == 5'd9)

                                   begin    

                                          wave <= 5'd20;

                                          cnt <= cnt + 1'd1;

                                   end

                            else

                                   begin

                                          wave <= wava;

                                          cnt <= cnt + 1'd1;

                                   end

       锯齿波的实现,即输出信号由0每次加一递增到20,然后从20跳变到0。可以按照如下的过程实现:当wave=20,将wave的值置为0,其余时刻,wave加一。

                            if (wave == 5'd20)

                                   begin

                                          wave <= 5'd0;

                                   end

                            else

                                   begin

                                          wave <= wava + 1'd1;

                                   end

       三角波的实现,类似于锯齿波,但当wave达到20时,不是跳变到0,而是以一为步长递减到0。可以设置一个指示信号up,当up的值是1时,wave每个时钟加一。当up的值为0时,则减一。

                            if (wave == 5'd20)

                                   begin

                                          wave <= wave - 1'd1;

                                          up = 1'd0;

                                   end

                            else if (wave = 5'd0)

                                   begin

                                          wave <= wava - 1'd1;

                                          up = 1'd1;

                                   end

                            else if (up)

                                   begin

                                          wave <= wava + 1'd1;

                                   end

                            else

                                   begin

                                          wave <= wava - 1'd1;

                                   end

参考答案

`timescale 1ns/1ns
module signal_generator(
	input clk,
	input rst_n,
	input [1:0] wave_choise,
	output reg [4:0]wave
	);

	reg [4:0]cnt;
	reg up;
	always @(posedge clk or negedge rst_n)
		if (!rst_n)
			begin 
				wave <= 5'd0;
				cnt <= 5'd0;
			end
		else case (wave_choise)
		2'b00:				//产生方波,保持0十个时钟,保持20十个时钟
			begin
				if (cnt == 5'd19)
					begin	
						wave <= 5'd0;
						cnt <= 5'd0;
					end
				else if(cnt == 5'd9)
					begin	
						wave <= 5'd20;
						cnt <= cnt + 1'd1;
					end
				else
					begin
						wave <= wave;
						cnt <= cnt + 1'd1;
					end
			end
		2'b01:				//产生锯齿,0递增到20
			begin
				if (wave == 5'd20)
					begin
						wave <= 5'd0;
					end
				else 
					begin
						wave <= wave + 1'd1;
					end
			end
		2'b10:
			begin
				if (wave == 5'd20)
					begin
						wave <= wave - 1'd1;
						up <= 1'd0;
					end
				else if (wave == 5'd0)
					begin
						wave <= wave - 1'd1;
						up <= 1'd1;
					end
				if (up)
					begin
						wave <= wave + 1'd1;
					end
				else 
					begin
						wave <= wave - 1'd1;
					end
			end
		default:
			begin
				wave <= 1'b0;
			end
	endcase
endmodule