-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFT_Buffer.sv.bak
105 lines (82 loc) · 2.26 KB
/
FFT_Buffer.sv.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module FFT_Buffer #(parameter N = 1024, data_width = 24, fp_width = 32, mag_width = 9, MAX_X = 640, MAX_Y = 480)
(
input logic clk, rst, ItoF_done, take_fft_sample,
input logic [fp_width - 1:0] data_in,
output logic [fp_width - 1:0] data_out,
output logic start_fft
);
integer n;
integer buf_ctr;
logic [fp_width - 1:0] audio_buffer [0:N - 1];
logic buffer_loaded, buffer_emptied;
enum {init, load_buf, unload_buf} ps, ns;
assign buffer_loaded = (ps == load_buf) && (n == N - 1);
assign buffer_emptied = (ps == unload_buf) && (n == 0);
always_comb begin
case (ps)
init: if (take_fft_sample) ns = load_buf;
else ns = init;
load_buf: if (buffer_loaded) ns = unload_buf;
else ns = load_buf;
unload_buf: if (buffer_emptied) ns = init;
else ns = unload_buf;
endcase
end
always_ff @(posedge clk) begin
if (rst) begin
ps <= init;
end else begin
ps <= ns;
end
end
always_ff @(posedge clk) begin
if (ps == init) begin
n <= 0;
end
if ((ps == load_buf) && ItoF_done) begin
audio_buffer[0] <= data_in;
n <= n + 1'b1;
for (buf_ctr = 1; buf_ctr < N; buf_ctr++) begin
audio_buffer[buf_ctr] <= audio_buffer[buf_ctr - 1];
end
end
if (ps == unload_buf) begin
start_fft <= 1'b1;
data_out <= audio_buffer[N - 1];
n <= n - 1'b1;
for (buf_ctr = 1; buf_ctr < N; buf_ctr++) begin
audio_buffer[buf_ctr] <= audio_buffer[buf_ctr - 1];
end
end
if (start_fft) begin
start_fft <= 1'b0;
end
end
endmodule
`timescale 1 ps / 1 ps
module FFT_Buffer_testbench();
parameter N = 1024, data_width = 24, fp_width = 32, mag_width = 9, MAX_X = 640, MAX_Y = 480;
logic clk, rst, ItoF_done, take_fft_sample;
logic [fp_width - 1:0] data_in;
logic [fp_width - 1:0] data_out;
logic start_fft;
FFT_Buffer dut (.*);
// Set up the clock
integer ctr;
parameter CLOCK_PERIOD = 100;
initial begin
ctr <= 0;
clk <= 0;
forever #(CLOCK_PERIOD / 2) clk <= ~clk;
end
initial begin
rst <= 1'b1; @(posedge clk);
rst <= 1'b0; @(posedge clk);
ItoF_done <= 1'b0; @(posedge clk);
take_fft_sample <= 1'b1; @(posedge clk);
repeat (16 * N) begin
data_in <= ctr; ctr <= ctr + 1'b1; ItoF_done <= ~ItoF_done; @(posedge clk);
end
$stop;
end
endmodule