|
module top (clk, rst_n, key, r, g, b, sel,seg,hsync, vsync, VGA_CLK, VGA_BLANK, VGA_SYNC);
input clk, rst_n;
input [2:0] key;
output [2:0] sel;
output [7:0] seg;
output [2:0] r, g;
output [1:0] b;
output hsync, vsync;
output VGA_CLK;
output VGA_BLANK, VGA_SYNC;
assign VGA_BLANK = 1'b1;
assign VGA_SYNC = 1'b0;
wire [2:0] key_out;
wire [7:0] rgb;
wire [7:0] addr;
wire [7:0] sine_q, square_q,triangle_q;
wire [7:0] o_wave;
wire [23:0] data;
//parameter T20ms = 800_000;
pll pll(
.areset(~rst_n),
.inclk0(clk),
.c0(VGA_CLK)
); // VGA_CLK is 40M clock
jitter_key k0(
.clk(VGA_CLK),
.rst_n(rst_n),
.key_in(key[0]),
.key_out(key_out[0])
);
jitter_key k1(
.clk(VGA_CLK),
.rst_n(rst_n),
.key_in(key[1]),
.key_out(key_out[1])
);
jitter_key k2(
.clk(VGA_CLK),
.rst_n(rst_n),
.key_in(key[2]),
.key_out(key_out[2])
);
sin_8x256 sin_8x256_inst (
.address ( addr ),
.clock ( VGA_CLK ),
.q ( sine_q )
);
squ_8x256 squ_8x256_inst (
.address ( addr ),
.clock ( VGA_CLK ),
.q ( square_q )
);
tri_8x256 tri_8x256_inst (
.address ( addr ),
.clock ( VGA_CLK ),
.q ( triangle_q )
);
dds_controller ctrl(
.clk(VGA_CLK),
.rst_n(rst_n),
.key_out(key_out),
.addr(addr),
.sine_q(sine_q),
.square_q(square_q),
.triangle_q(triangle_q),
.o_wave(o_wave),
.data(data)
);
seg7 seg7(
.clk (clk),
.rst_n(rst_n),
.data (data),
.sel (sel),
.seg (seg)
);
vga_dds vga_dds (
.clk(VGA_CLK),
.rst_n(rst_n),
.key_out(key_out),
.wave(o_wave),
.rgb8(rgb),
.hsync(hsync),
.vsync(vsync)
);
rgb8 rgb8 (
.rgb8(rgb),
.r(r),
.g(g),
.b(b)
);
endmodule
下面是tb
`timescale 1ns/1ps
module top_tb;
reg clk, rst_n;
reg [2:0] key;
wire [2:0] sel;
wire [7:0] seg;
wire [2:0] r, g;
wire [1:0] b;
wire hsync, vsync;
wire VGA_CLK;
wire VGA_BLANK, VGA_SYNC;
reg [7:0] seg_monitor;
top dut (
.clk(clk),
.rst_n(rst_n),
.key(key),
.sel(sel),
.seg(seg),
.r(r),
.g(g),
.b(b),
.hsync(hsync),
.vsync(vsync),
.VGA_CLK(VGA_CLK),
.VGA_BLANK(VGA_BLANK),
.VGA_SYNC(VGA_SYNC)
);
initial
begin
clk = 1;
rst_n = 0;
key=3'b111;
#200.1
rst_n = 1;
#28000 key = 3'b101; #2000 key = 3'b111;//key[1],切换频率
#28000 key = 3'b101; #2000 key = 3'b111;
/*#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;*/
#28000 key = 3'b011; #2000 key = 3'b011;//key[2],切换振幅
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b110; #2000 key = 3'b111;//key[0],切换波形
#28000 key = 3'b101; #2000 key = 3'b111;//key[1],切换频率
#28000 key = 3'b101; #2000 key = 3'b111;
/*#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;*/
#28000 key = 3'b011; #2000 key = 3'b011;//key[2],切换振幅
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b110; #2000 key = 3'b111;//key[0],切换波形
#28000 key = 3'b101; #2000 key = 3'b111;//key[1],切换频率
#28000 key = 3'b101; #2000 key = 3'b111;
/*#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;
#28000 key = 3'b101; #2000 key = 3'b111;*/
#28000 key = 3'b011; #2000 key = 3'b011;//key[2],切换振幅
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b011; #2000 key = 3'b011;
#28000 key = 3'b011; #2000 key = 3'b011;
#30_000 $stop;
end
always #10 clk = ~clk;
always @(*)
begin
case(seg)
8'b1100_0000 : seg_monitor = "0" ;//0
8'b1111_1001 : seg_monitor = "1" ;//1
8'b1010_0100 : seg_monitor = "2" ;//2
8'b1011_0000 : seg_monitor = "3" ;//3
8'b1001_1001 : seg_monitor = "4" ;//4
8'b1001_0010 : seg_monitor = "5" ;//5
8'b1000_0010 : seg_monitor = "6" ;//6
8'b1111_1000 : seg_monitor = "7" ;//7
8'b1000_0000 : seg_monitor = "8" ;//8
8'b1001_0000 : seg_monitor = "9" ;//9
8'b1000_1000 : seg_monitor = "A" ;//A
8'b1000_0011 : seg_monitor = "b" ;//b
8'b1100_0110 : seg_monitor = "C" ;//C
8'b1010_0001 : seg_monitor = "d" ;//d
8'b1000_0110 : seg_monitor = "E" ;//E
8'b1000_1110 : seg_monitor = "F" ;//F
default : seg_monitor = "X" ;//null
endcase
end
endmodule
最后是dds_controller
module dds_controller(
input wire clk,
input wire rst_n,
input wire [2:0] key_out,
input wire [7:0] sine_q,
input wire [7:0] square_q,
input wire [7:0] triangle_q,
output wire [7:0] addr,
output reg [7:0] o_wave,
output wire [23:0] data
);
parameter phase = 8'd64;//初始相位
//parameter Freq1K = 107374;// 40Mhz=VGA_clk,频率控制字1k/40Mhz*(2^32)=107374
parameter Freq500 = 53687;// 40Mhz=VGA_clk,频率控制字500/40Mhz*(2^32)=53687
reg [3:0] wave_cnt;//切换波形计数器
reg [3:0] fren_cnt;//切换频率计数器
reg [3:0] ampl_cnt;//切换振幅计数器
reg [31:0] addr_cnt;//相位累加器位数N=32
reg [31:0] freq;//构造的输出目标频率
reg [7:0] m_wave;//构造的中间波形
//数码管显示
assign data = {4'hA,wave_cnt,4'hB,fren_cnt,4'hC,ampl_cnt};
//相位累加
always @(posedge clk or negedge rst_n)
if(rst_n==1'b0)
begin
addr_cnt[31:24] <= phase;
addr_cnt[23:0] <= 24'd0;
end
else
addr_cnt <= addr_cnt + freq;
assign addr = addr_cnt[31:24];
//切换波形wave_cnt
always @(posedge clk or negedge rst_n)
if(rst_n==1'b0)
wave_cnt <= 4'd0;
else if(key_out[0]==1'b0)
wave_cnt <= wave_cnt;
else if(wave_cnt<4'd2)
wave_cnt <= wave_cnt + 4'd1;
else
wave_cnt <= 4'd0;
always @(*)
case(wave_cnt)
0 : m_wave = sine_q;
1 : m_wave = square_q;
2 : m_wave = triangle_q;
default : ;
endcase
//切换频率fren_cnt
always @(posedge clk or negedge rst_n)
if(rst_n==1'b0)
fren_cnt <= 4'd0;
else if(key_out[1]==1'b0)
fren_cnt <= fren_cnt;
else if(fren_cnt<10)
fren_cnt <= fren_cnt + 4'd1;
else
fren_cnt <= 4'd0;
always @(*)
case(fren_cnt)
0 : freq = Freq500 *74;//37Khz
1 : freq = Freq500 *75;//37.5Khz
/*2 : freq = Freq1K *38;//38Khz
3 : freq = Freq1K *38.5;//38.5Khz
4 : freq = Freq1K *39;//39Khz
5 : freq = Freq1K *39.5;//39.5Khz
6 : freq = Freq1K *40;//40Khz
7 : freq = Freq1K *35;//35Khz
8 : freq = Freq1K *35.5;//35.5Khz
9 : freq = Freq1K *36;//36Khz
10: freq = Freq1K *36.5;//36.5Khz*/
default : ;
endcase
//切换振幅ampl_cnt
always @(posedge clk or negedge rst_n)
if(rst_n==1'b0)
ampl_cnt <= 4'd3;
else if(key_out[2]==1'b0)
ampl_cnt <= ampl_cnt;
else if(ampl_cnt<3)
ampl_cnt <= ampl_cnt + 4'd1;
else
ampl_cnt <= 4'd0;
always @(*)
case(ampl_cnt)
0 : o_wave = (m_wave>>2) + 8'd96;//ampl*1/4,middle value=128
1 : o_wave = (m_wave>>1) + 8'd64;
2 : o_wave = (m_wave>>2) +(m_wave>>1) + 8'd32;
3 : o_wave = o_wave;
default : ;
endcase
endmodule |
|