URL
https://opencores.org/ocsvn/dblclockfft/dblclockfft/trunk
Subversion Repositories dblclockfft
Compare Revisions
- This comparison shows the changes necessary to convert path
/dblclockfft
- from Rev 13 to Rev 14
- ↔ Reverse comparison
Rev 13 → Rev 14
/trunk/bench/cpp/fft_tb.m
0,0 → 1,17
% Read the file |
fid = fopen('fft_tb.dbl','r'); |
raw = fread(fid, [2 inf], 'double'); |
fclose(fid); |
|
% Convert the raw doubles into complex values |
datc = raw(1,:)+j*raw(2,:); |
% Reshape the matrix into one line per FFT |
% Assume an FFT length of 2048 |
ftlen = 2048; |
ndat = reshape(datc, ftlen, length(datc)/ftlen); |
|
% Create a time axis, for use in plotting if desired |
tm = 0:(ftlen-1); |
|
% Now, the data from the test is ready for inspection |
|
/trunk/bench/cpp/ifft_tb.v
0,0 → 1,28
|
module ifft_tb(i_clk, i_rst, i_ce, i_left, i_right, o_left, o_right, o_sync); |
parameter IWIDTH=16, MIDWIDTH=22, OWIDTH=28; |
input i_clk, i_rst, i_ce; |
input [(2*IWIDTH-1):0] i_left, i_right; |
output wire [(2*OWIDTH-1):0] o_left, o_right; |
output wire o_sync; |
|
wire m_sync; |
wire [(2*MIDWIDTH-1):0] m_left, m_right; |
fftmain fft(i_clk, i_rst, i_ce, i_left, i_right, |
m_left, m_right, m_sync); |
|
wire w_syncd; |
reg r_syncd; |
always @(posedge i_clk) |
if (i_rst) |
r_syncd <= 1'b0; |
else |
r_syncd <= r_syncd || m_sync; |
assign w_syncd = r_syncd || m_sync; |
|
ifftmain ifft(i_clk, i_rst, (i_ce)&&(w_syncd), m_left, m_right, |
o_left, o_right, o_sync); |
|
|
endmodule |
|
/trunk/bench/cpp/fft_tb.cpp
3,7 → 3,7
// |
// Project: A Doubletime Pipelined FFT |
// |
// Purpose: A test-bench for the mail program, fftmain.v, of the double |
// Purpose: A test-bench for the main program, fftmain.v, of the double |
// clocked FFT. This file may be run autonomously (when |
// fully functional). If so, the last line output will either |
// read "SUCCESS" on success, or some other failure message |
137,12 → 137,12
long tv = m_data[i]; |
|
printf("OUT[%4d = %4x] = ", i, i); |
printf("%16lx = ", tv); |
printf("%12lx = ", tv); |
*dp = twos_complement(tv >> OWIDTH, OWIDTH); |
printf("%12.1f + ", *dp); |
printf("%10.1f + ", *dp); |
osq += (*dp) * (*dp); dp++; |
*dp = twos_complement(tv, OWIDTH); |
printf("%12.1f j", *dp); |
printf("%10.1f j", *dp); |
osq += (*dp) * (*dp); dp++; |
printf(" <-> %12.1f %12.1f\n", m_fft_buf[2*i], m_fft_buf[2*i+1]); |
} |
272,10 → 272,7
double rdata(int addr) { |
long ivl = m_data[addr & (FFTLEN-1)]; |
|
ivl = ivl >> 17; |
ivl &= ((1<<OWIDTH)-1); |
if (1 & (ivl>>(OWIDTH-1))) |
ivl |= (-1l << OWIDTH); |
ivl = twos_complement(ivl >> OWIDTH, OWIDTH); |
return (double)ivl; |
} |
|
282,10 → 279,7
double idata(int addr) { |
long ivl = m_data[addr & (FFTLEN-1)]; |
|
ivl = ivl; |
ivl &= ((1<<OWIDTH)-1); |
if (1 & (ivl>>(OWIDTH-1))) |
ivl |= (-1l << OWIDTH); |
ivl = twos_complement(ivl, OWIDTH); |
return (double)ivl; |
} |
|
/trunk/bench/cpp/ifft_tb.m
0,0 → 1,17
% Read the file |
fid = fopen('ifft_tb.dbl','r'); |
raw = fread(fid, [2 inf], 'double'); |
fclose(fid); |
|
% Convert the raw doubles into complex values |
datc = raw(1,:)+j*raw(2,:); |
% Reshape the matrix into one line per FFT |
% Assume an FFT length of 2048 |
ftlen = 2048; |
ndat = reshape(datc, ftlen, length(datc)/ftlen); |
|
% Create a time axis, for use in plotting if desired |
tm = 0:(ftlen-1); |
|
% Now, the data from the test is ready for inspection |
|
/trunk/bench/cpp/Makefile
1,7 → 1,9
all: mpy_tb dblrev_tb dblstage_tb qtrstage_tb fft_tb test |
|
OBJDR:= ../../sw/fft-core/obj_dir |
VINC := -I/usr/share/verilator/include -I$(OBJDR)/ |
VSRCD:= ../../sw/fft-core |
LCLDR:= obj_dir |
VINC := -I/usr/share/verilator/include -I$(OBJDR)/ -I$(LCLDR)/ |
MPYLB:= $(OBJDR)/Vshiftaddmpy__ALL.a |
DBLRV:= $(OBJDR)/Vdblreverse__ALL.a |
DBLSG:= $(OBJDR)/Vdblstage__ALL.a |
8,6 → 10,7
QTRSG:= $(OBJDR)/Vqtrstage__ALL.a |
BFLYL:= $(OBJDR)/Vbutterfly__ALL.a |
FFTLB:= $(OBJDR)/Vfftmain__ALL.a |
IFTLB:= $(LCLDR)/Vifft_tb__ALL.a |
STGLB:= $(OBJDR)/Vfftstage_o2048__ALL.a |
VERILATOR_ROOT := /usr/share/verilator |
|
32,9 → 35,16
fft_tb: fft_tb.cpp $(FFTLB) |
g++ -g $(VINC) $< $(FFTLB) $(VERILATOR_ROOT)/include/verilated.cpp -lfftw3 -o $@ |
|
ifft_tb: ifft_tb.cpp $(IFTLB) |
g++ -g $(VINC) $< $(IFTLB) $(VERILATOR_ROOT)/include/verilated.cpp -lfftw3 -o $@ |
$(IFTLB): $(LCLDR)/Vifft_tb.cpp |
cd $(LCLDR); make -f Vifft_tb.mk |
$(LCLDR)/Vifft_tb.cpp: ifft_tb.v $(VSRCD)/fftmain.v $(VSRCD)/ifftmain.v |
verilator -y $(VSRCD) -cc ifft_tb.v |
|
.PHONY: test |
test: mpy_tb dblrev_tb dblstage_tb qtrstage_tb butterfly_tb fftstage_o2048_tb |
test: fft_tb |
test: fft_tb ifft_tb |
./mpy_tb |
./dblrev_tb |
./dblstage_tb |
42,9 → 52,12
./butterfly_tb |
./fftstage_o2048_tb |
./fft_tb |
./ifft_tb |
|
.PHONY: clean |
clean: |
rm mpy_tb dblrev_tb dblstage_tb qtrstage_tb |
rm mpy_tb dblrev_tb dblstage_tb qtrstage_tb butterfly_tb |
rm fftstage_o2048_tb fft_tb ifft_tb |
rm -rf $(LCLDR) fft_tb.dbl ifft_tb.dbl |
|
include $(VERILATOR_ROOT)/include/verilated.mk |
/trunk/bench/cpp/ifft_tb.cpp
0,0 → 1,469
// |
// Filename: ifft_tb.cpp |
// |
// Project: A Doubletime Pipelined FFT |
// |
// Purpose: A test-bench for the combined work of both fftmain.v and |
// ifftmain.v. If they work together, in concert like they should, |
// then the operation of both in series should yield an identity. |
// This program attempts to check that identity with various |
// inputs given to it. |
// |
// This file has a variety of dependencies, not the least of which |
// are verilator, ifftmain.v and fftmain.v (both produced by |
// fftgen), but also on the ifft_tb.v verilog test bench found |
// within this directory. |
// |
// Creator: Dan Gisselquist, Ph.D. |
// Gisselquist Tecnology, LLC |
// |
/////////////////////////////////////////////////////////////////////////// |
// |
// Copyright (C) 2015, Gisselquist Technology, LLC |
// |
// This program is free software (firmware): you can redistribute it and/or |
// modify it under the terms of the GNU General Public License as published |
// by the Free Software Foundation, either version 3 of the License, or (at |
// your option) any later version. |
// |
// This program is distributed in the hope that it will be useful, but WITHOUT |
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
// for more details. |
// |
// You should have received a copy of the GNU General Public License along |
// with this program. (It's in the $(ROOT)/doc directory, run make with no |
// target there if the PDF file isn't present.) If not, see |
// <http://www.gnu.org/licenses/> for a copy. |
// |
// License: GPL, v3, as defined and found on www.gnu.org, |
// http://www.gnu.org/licenses/gpl.html |
// |
// |
/////////////////////////////////////////////////////////////////////////// |
#include <stdio.h> |
#include <math.h> |
#include <assert.h> |
|
#include "verilated.h" |
#include "Vifft_tb.h" |
|
#define LGWIDTH 11 |
#define IWIDTH 16 |
#define MWIDTH 22 |
#define OWIDTH 28 |
|
#define FFTLEN (1<<LGWIDTH) |
|
class IFFT_TB { |
public: |
Vifft_tb *m_tb; |
unsigned int m_log[8*FFTLEN]; |
long m_data[2*FFTLEN]; |
int m_iaddr, m_oaddr, m_offset; |
FILE *m_dumpfp; |
// double *m_tb_buf; |
// int m_ntest; |
bool m_syncd; |
|
IFFT_TB(void) { |
m_tb = new Vifft_tb; |
m_iaddr = m_oaddr = 0; |
m_dumpfp = NULL; |
|
m_syncd = false; |
// m_ntest = 0; |
} |
|
void tick(void) { |
m_tb->i_clk = 0; |
m_tb->eval(); |
m_tb->i_clk = 1; |
m_tb->eval(); |
} |
|
void reset(void) { |
m_tb->i_ce = 0; |
m_tb->i_rst = 1; |
tick(); |
m_tb->i_rst = 0; |
tick(); |
|
m_iaddr = m_oaddr = 0; |
m_syncd = false; |
} |
|
long twos_complement(const long val, const int bits) { |
long r; |
|
r = val & ((1l<<bits)-1); |
if (r & (1l << (bits-1))) |
r |= (-1l << bits); |
return r; |
} |
|
void checkresults(void) { |
/* |
double *dp, *sp; // Complex array |
double vout[FFTLEN*2]; |
double isq=0.0, osq = 0.0; |
long *lp; |
|
// Fill up our test array from the log array |
printf("%3d : CHECK: %8d %5x\n", m_ntest, m_iaddr, m_iaddr); |
dp = m_tb_buf; lp = &m_log[(m_iaddr-FFTLEN*3)&((4*FFTLEN-1)&(-FFTLEN))]; |
for(int i=0; i<FFTLEN; i++) { |
long tv = *lp++; |
|
dp[0] = twos_complement(tv >> IWIDTH, IWIDTH); |
dp[1] = twos_complement(tv, IWIDTH); |
|
printf("IN[%4d = %4x] = %9.1f %9.1f\n", |
i+((m_iaddr-FFTLEN*3)&((4*FFTLEN-1)&(-FFTLEN))), |
i+((m_iaddr-FFTLEN*3)&((4*FFTLEN-1)&(-FFTLEN))), |
dp[0], dp[1]); |
dp += 2; |
} |
|
// Let's measure ... are we the zero vector? If not, how close? |
dp = m_tb_buf; |
for(int i=0; i<FFTLEN; i++) |
isq += (*dp) * (*dp); |
|
fftw_execute(m_plan); |
|
// Let's load up the output we received into vout |
dp = vout; |
for(int i=0; i<FFTLEN; i++) { |
long tv = m_data[i]; |
|
printf("OUT[%4d = %4x] = ", i, i); |
printf("%16lx = ", tv); |
*dp = twos_complement(tv >> OWIDTH, OWIDTH); |
printf("%12.1f + ", *dp); |
osq += (*dp) * (*dp); dp++; |
*dp = twos_complement(tv, OWIDTH); |
printf("%12.1f j", *dp); |
osq += (*dp) * (*dp); dp++; |
printf(" <-> %12.1f %12.1f\n", m_tb_buf[2*i], m_fft_buf[2*i+1]); |
} |
|
|
// Let's figure out if there's a scale factor difference ... |
double scale = 0.0, wt = 0.0; |
sp = m_tb_buf; dp = vout; |
for(int i=0; i<FFTLEN*2; i++) { |
scale += (*sp) * (*dp++); |
wt += (*sp) * (*sp); sp++; |
} scale = scale / wt; |
|
if (wt == 0.0) scale = 1.0; |
|
double xisq = 0.0; |
sp = m_tb_buf; dp = vout; |
for(int i=0; i<FFTLEN*2; i++) { |
double vl = (*sp++) * scale - (*dp++); |
xisq += vl * vl; |
} |
|
printf("%3d : SCALE = %12.6f, WT = %18.1f, ISQ = %15.1f, ", |
m_ntest, scale, wt, isq); |
printf("OSQ = %18.1f, ", osq); |
printf("XISQ = %18.1f\n", xisq); |
m_ntest++; |
*/ |
} |
|
bool test(int lft, int rht) { |
m_tb->i_ce = 1; |
m_tb->i_rst = 0; |
m_tb->i_left = lft; |
m_tb->i_right = rht; |
|
m_log[(m_iaddr++)&(8*FFTLEN-1)] = lft; |
m_log[(m_iaddr++)&(8*FFTLEN-1)] = rht; |
|
tick(); |
|
if ((m_tb->o_sync)&&(!m_syncd)) { |
m_offset = m_iaddr; |
m_oaddr = 0; |
m_syncd = true; |
} |
|
m_data[(m_oaddr++)&(FFTLEN-1)] = m_tb->o_left; |
m_data[(m_oaddr++)&(FFTLEN-1)] = m_tb->o_right; |
|
if ((m_syncd)&&((m_oaddr&(FFTLEN-1)) == 0)) { |
dumpwrite(); |
// checkresults(); |
} |
|
return (m_tb->o_sync); |
} |
|
bool test(double lft_r, double lft_i, double rht_r, double rht_i) { |
int ilft, irht, ilft_r, ilft_i, irht_r, irht_i; |
|
assert(2*IWIDTH <= 32); |
ilft_r = (int)(lft_r) & ((1<<IWIDTH)-1); |
ilft_i = (int)(lft_i) & ((1<<IWIDTH)-1); |
irht_r = (int)(rht_r) & ((1<<IWIDTH)-1); |
irht_i = (int)(rht_i) & ((1<<IWIDTH)-1); |
|
ilft = (ilft_r << IWIDTH) | ilft_i; |
irht = (irht_r << IWIDTH) | irht_i; |
|
return test(ilft, irht); |
} |
|
double rdata(int addr) { |
long ivl = m_data[addr & (FFTLEN-1)]; |
|
ivl = twos_complement(ivl >> OWIDTH, OWIDTH); |
return (double)ivl; |
} |
|
double idata(int addr) { |
long ivl = m_data[addr & (FFTLEN-1)]; |
|
ivl = twos_complement(ivl, OWIDTH); |
return (double)ivl; |
} |
|
void dump(FILE *fp) { |
m_dumpfp = fp; |
} |
|
void dumpwrite(void) { |
if (!m_dumpfp) |
return; |
|
double *buf; |
|
buf = new double[FFTLEN * 2]; |
for(int i=0; i<FFTLEN; i++) { |
buf[i*2] = rdata(i); |
buf[i*2+1] = idata(i); |
} |
|
fwrite(buf, sizeof(double), FFTLEN*2, m_dumpfp); |
delete[] buf; |
} |
}; |
|
|
int main(int argc, char **argv, char **envp) { |
Verilated::commandArgs(argc, argv); |
IFFT_TB *tb = new IFFT_TB; |
FILE *fpout; |
|
fpout = fopen("ifft_tb.dbl", "w"); |
if (NULL == fpout) { |
fprintf(stderr, "Cannot write output file, fft_tb.dbl\n"); |
exit(-1); |
} |
|
tb->reset(); |
tb->dump(fpout); |
|
// 1 -> 0x0001 |
// 2 -> 0x0002 |
// 4 -> 0x0004 |
// 8 -> 0x0008 |
// 16 -> 0x0010 |
// 32 -> 0x0020 |
// 64 -> 0x0040 |
// 128 -> 0x0080 |
// 256 -> 0x0100 |
// 512 -> 0x0200 |
// 1024 -> 0x0400 |
// 2048 -> 0x0800 |
// 4096 -> 0x1000 |
// 8192 -> 0x2000 |
// 16384 -> 0x4000 |
for(int v=1; v<32768; v<<=1) for(int k=0; k<FFTLEN/2; k++) |
tb->test((double)v,0.0,(double)v,0.0); |
// 1 -> 0xffff |
// 2 -> 0xfffe |
// 4 -> 0xfffc |
// 8 -> 0xfff8 |
// 16 -> 0xfff0 |
// 32 -> 0xffe0 |
// 64 -> 0xffc0 |
// 128 -> 0xff80 |
// 256 -> 0xff00 |
// 512 -> 0xfe00 |
// 1024 -> 0xfc00 |
// 2048 -> 0xf800 |
// 4096 -> 0xf000 |
// 8192 -> 0xe000 |
// 16384 -> 0xc000 |
// 32768 -> 0x8000 |
for(int v=1; v<=32768; v<<=1) for(int k=0; k<FFTLEN/2; k++) |
tb->test(-(double)v,0.0,-(double)v,0.0); |
// 1 -> 0x000040 CORRECT!! |
// 2 -> 0x000080 |
// 4 -> 0x000100 |
// 8 -> 0x000200 |
// 16 -> 0x000400 |
// 32 -> 0x000800 |
// 64 -> 0x001000 |
// 128 -> 0x002000 |
// 256 -> 0x004000 |
// 512 -> 0x008000 |
// 1024 -> 0x010000 |
// 2048 -> 0x020000 |
// 4096 -> 0x040000 |
// 8192 -> 0x080000 |
// 16384 -> 0x100000 |
for(int v=1; v<32768; v<<=1) for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,(double)v,0.0,(double)v); |
// 1 -> 0x3fffc0 |
// 2 -> 0x3fff80 |
// 4 -> 0x3fff00 |
// 8 -> 0x3ffe00 |
// 16 -> 0x3ffc00 |
// 32 -> 0x3ff800 |
// 64 -> 0x3ff000 |
// 128 -> 0x3fe000 |
// 256 -> 0x3fc000 |
// 512 -> 0x3f8000 |
// 1024 -> 0x3f0000 |
// 2048 -> 0x3e0000 |
// 4096 -> 0x3c0000 |
// 8192 -> 0x380000 |
// 16384 -> 0x300000 |
for(int v=1; v<32768; v<<=1) for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,-(double)v,0.0,-(double)v); |
|
// 61. Now, how about the smallest alternating real signal |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(2.0,0.0,0.0,0.0); // Don't forget to expect a bias! |
// 62. Now, how about the smallest alternating imaginary signal |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,2.0,0.0,0.0); // Don't forget to expect a bias! |
// 63. Now, how about the smallest alternating real signal,2nd phase |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,0.0,2.0,0.0); // Don't forget to expect a bias! |
// 64.Now, how about the smallest alternating imaginary signal,2nd phase |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,0.0,0.0,2.0); // Don't forget to expect a bias! |
|
// 65. |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(32767.0,0.0,-32767.0,0.0); |
// 66. |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,-32767.0,0.0,32767.0); |
// 67. |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(-32768.0,-32768.0,-32768.0,-32768.0); |
// 68. |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,-32767.0,0.0,32767.0); |
// 69. |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(0.0,32767.0,0.0,-32767.0); |
// 70. |
for(int k=0; k<FFTLEN/2; k++) |
tb->test(-32768.0,-32768.0,-32768.0,-32768.0); |
|
// 71. Now let's go for an impulse (SUCCESS) |
tb->test(16384.0, 0.0, 0.0, 0.0); |
for(int k=0; k<FFTLEN/2-1; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
// 72. And another one on the next clock (FAILS, ugly) |
// Lot's of roundoff error, or some error in small bits |
tb->test(0.0, 0.0, 16384.0, 0.0); |
for(int k=0; k<FFTLEN/2-1; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
// 73. And an imaginary one on the second clock |
// Much roundoff error, as in last test |
tb->test(0.0, 0.0, 0.0, 16384.0); |
for(int k=0; k<FFTLEN/2-1; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
// 74. Likewise the next clock |
// Much roundoff error, as in last test |
tb->test(0.0,0.0,0.0,0.0); |
tb->test(16384.0, 0.0, 0.0, 0.0); |
for(int k=0; k<FFTLEN/2-2; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
// 75. And it's imaginary counterpart |
// Much roundoff error, as in last test |
tb->test(0.0,0.0,0.0,0.0); |
tb->test(0.0, 16384.0, 0.0, 0.0); |
for(int k=0; k<FFTLEN/2-2; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
// 76. Likewise the next clock |
// Much roundoff error, as in last test |
tb->test(0.0,0.0,0.0,0.0); |
tb->test(0.0, 0.0, 16384.0, 0.0); |
for(int k=0; k<FFTLEN/2-2; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
// 77. And it's imaginary counterpart |
// Much roundoff error, as in last test |
tb->test(0.0,0.0,0.0,0.0); |
tb->test(0.0, 0.0, 0.0, 16384.0); |
for(int k=0; k<FFTLEN/2-2; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
|
// 78. Now let's try some exponentials |
for(int k=0; k<FFTLEN/2; k++) { |
double cl, cr, sl, sr, W; |
W = - 2.0 * M_PI / FFTLEN; |
cl = cos(W * (2*k )) * 16383.0; |
sl = sin(W * (2*k )) * 16383.0; |
cr = cos(W * (2*k+1)) * 16383.0; |
sr = sin(W * (2*k+1)) * 16383.0; |
tb->test(cl, sl, cr, sr); |
} |
|
// 79. |
for(int k=0; k<FFTLEN/2; k++) { |
double cl, cr, sl, sr, W; |
W = - 2.0 * M_PI / FFTLEN * 5; |
cl = cos(W * (2*k )) * 16383.0; |
sl = sin(W * (2*k )) * 16383.0; |
cr = cos(W * (2*k+1)) * 16383.0; |
sr = sin(W * (2*k+1)) * 16383.0; |
tb->test(cl, sl, cr, sr); |
} |
|
// 80. |
for(int k=0; k<FFTLEN/2; k++) { |
double cl, cr, sl, sr, W; |
W = - 2.0 * M_PI / FFTLEN * 8; |
cl = cos(W * (2*k )) * 8190.0; |
sl = sin(W * (2*k )) * 8190.0; |
cr = cos(W * (2*k+1)) * 8190.0; |
sr = sin(W * (2*k+1)) * 8190.0; |
tb->test(cl, sl, cr, sr); |
} |
|
// 81. |
for(int k=0; k<FFTLEN/2; k++) { |
double cl, cr, sl, sr, W; |
W = - 2.0 * M_PI / FFTLEN * 25; |
cl = cos(W * (2*k )) * 4.0; |
sl = sin(W * (2*k )) * 4.0; |
cr = cos(W * (2*k+1)) * 4.0; |
sr = sin(W * (2*k+1)) * 4.0; |
tb->test(cl, sl, cr, sr); |
} |
|
// 19.--24. And finally, let's clear out our results / buffer |
for(int k=0; k<(FFTLEN/2) * 5; k++) |
tb->test(0.0,0.0,0.0,0.0); |
|
fclose(fpout); |
} |
|
|
/trunk/sw/fftgen.cpp
3,6 → 3,7
#include <unistd.h> |
#include <sys/stat.h> |
#include <string.h> |
#include <string> |
#include <math.h> |
#include <ctype.h> |
#include <assert.h> |
34,7 → 35,7
"//\n" |
"//\n" |
"///////////////////////////////////////////////////////////////////////////\n"; |
const char prjname[] = "A Doubletime Pipelined FFT\n"; |
const char prjname[] = "A Doubletime Pipelined FFT"; |
const char creator[] = "// Creator: Dan Gisselquist, Ph.D.\n" |
"// Gisselquist Tecnology, LLC\n"; |
|
54,16 → 55,25
return r; |
} |
|
int lgdelay(int nbits, int xtra) { |
int bflydelay(int nbits, int xtra) { |
int cbits = nbits + xtra; |
int delay = nbits + 2; |
int delay; |
if (nbits+1<cbits) |
delay = nbits+4; |
else |
delay = cbits+3; |
return lgval(delay); |
return delay; |
} |
|
int lgdelay(int nbits, int xtra) { |
// The butterfly code needs to compare a valid address, of this |
// many bits, with an address two greater. This guarantees we |
// have enough bits for that comparison. We'll also end up with |
// more storage space to look for these values, but without a |
// redesign that's just what we'll deal with. |
return lgval(bflydelay(nbits, xtra)+3); |
} |
|
void build_quarters(const char *fname) { |
FILE *fp = fopen(fname, "w"); |
if (NULL == fp) { |
100,12 → 110,14
"\tinput\t [(2*IWIDTH-1):0] i_data;\n" |
"\toutput\treg [(2*OWIDTH-1):0] o_data;\n" |
"\toutput\treg o_sync;\n" |
"\t\n" |
"\t\n"); |
fprintf(fp, |
"\treg\t wait_for_sync;\n" |
"\treg\t[2:0] pipeline;\n" |
"\n" |
"\treg\t[(IWIDTH):0] sum_r, sum_i, diff_r, diff_i;\n" |
"\twire\t[(IWIDTH):0] n_diff_i;\n" |
"\twire\t[(IWIDTH):0] n_diff_r, n_diff_i;\n" |
"\tassign n_diff_r = -diff_r;\n" |
"\tassign n_diff_i = -diff_i;\n" |
"\n" |
"\treg\t[(2*OWIDTH-1):0] ob_a;\n" |
125,7 → 137,8
"\tassign\ti_data_i = i_data[(IWIDTH-1):0];\n" |
"\n" |
"\treg [(2*OWIDTH-1):0] omem;\n" |
"\n" |
"\n"); |
fprintf(fp, |
"\twire [(IWIDTH-1):0] rnd;\n" |
"\tgenerate\n" |
"\tif ((ROUND)&&((IWIDTH+1-OWIDTH-SHIFT)>0))\n" |
172,10 → 185,10
"\t\t\t\t\tob_b_i <= diff_i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n" |
// "\t\t\t\t\tob_b_r <= { (OWIDTH) {1'b0} };\n" |
// "\t\t\t\t\tob_b_i <= { (OWIDTH) {1'b0} };\n" |
"\t\t\t\tend else if (~INVERSE) begin\n" |
"\t\t\t\tend else if (INVERSE==0) begin\n" |
"\t\t\t\t\t// on Odd, W = e^{-j2pi 1/4} = -j\n" |
"\t\t\t\t\tob_b_r <= diff_i[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n" |
"\t\t\t\t\tob_b_i <= diff_r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n" |
"\t\t\t\t\tob_b_i <= n_diff_r[(IWIDTH-SHIFT):(IWIDTH+1-OWIDTH-SHIFT)];\n" |
// "\t\t\t\t\tob_b_r <= { (OWIDTH) {1'b0} };\n" |
// "\t\t\t\t\tob_b_i <= { (OWIDTH) {1'b0} };\n" |
"\t\t\t\tend else begin\n" |
534,7 → 547,7
fclose(fp); |
} |
|
void build_butterfly(const char *fname) { |
void build_butterfly(const char *fname, int xtracbits) { |
FILE *fp = fopen(fname, "w"); |
if (NULL == fp) { |
fprintf(stderr, "Could not open \'%s\' for writing\n", fname); |
618,14 → 631,14
"module\tbutterfly(i_clk, i_rst, i_ce, i_coef, i_left, i_right, i_aux,\n" |
"\t\to_left, o_right, o_aux);\n" |
"\t// Public changeable parameters ...\n" |
"\tparameter IWIDTH=16,CWIDTH=IWIDTH+4,OWIDTH=IWIDTH+1;\n" |
"\tparameter IWIDTH=%d,CWIDTH=IWIDTH+%d,OWIDTH=IWIDTH+1;\n" |
"\t// Parameters specific to the core that should not be changed.\n" |
"\tparameter MPYDELAY=5'd20, // (IWIDTH+1 < CWIDTH)?(IWIDTH+4):(CWIDTH+3),\n" |
"\tparameter MPYDELAY=%d'd%d, // (IWIDTH+1 < CWIDTH)?(IWIDTH+4):(CWIDTH+3),\n" |
"\t\t\tSHIFT=0, ROUND=0;\n" |
"\t// The LGDELAY should be the base two log of the MPYDELAY. If\n" |
"\t// this value is fractional, then round up to the nearest\n" |
"\t// integer: LGDELAY=ceil(log(MPYDELAY)/log(2));\n" |
"\tparameter\tLGDELAY=5;\n" |
"\tparameter\tLGDELAY=%d;\n" |
"\tinput\t\ti_clk, i_rst, i_ce;\n" |
"\tinput\t\t[(2*CWIDTH-1):0] i_coef;\n" |
"\tinput\t\t[(2*IWIDTH-1):0] i_left, i_right;\n" |
632,7 → 645,9
"\tinput\t\ti_aux;\n" |
"\toutput\twire [(2*OWIDTH-1):0] o_left, o_right;\n" |
"\toutput\twire o_aux;\n" |
"\n" |
"\n", 16, xtracbits, lgdelay(16,xtracbits), |
bflydelay(16, xtracbits), lgdelay(16,xtracbits)); |
fprintf(fp, |
"\twire\t[(OWIDTH-1):0] o_left_r, o_left_i, o_right_r, o_right_i;\n" |
"\n" |
"\treg\t[(2*IWIDTH-1):0]\tr_left, r_right;\n" |
691,7 → 706,8
"\t\t\t// right side.\n" |
"\t\t\tfifo_left[fifo_addr] <= { r_aux_2, r_sum_r, r_sum_i };\n" |
"\t\t\tfifo_addr <= fifo_addr + 1;\n" |
"\t\t\tovalid <= (ovalid) || (fifo_addr > MPYDELAY+1);\n" |
"\n" |
"\t\t\tovalid <= (ovalid) || (fifo_addr > (MPYDELAY+1));\n" |
"\t\tend\n" |
"\n" |
"\twire\tsigned\t[(CWIDTH-1):0] ir_coef_r, ir_coef_i;\n" |
928,19 → 944,30
(inv)?"i":""); |
{ |
FILE *cmem; |
char memfile[128], *ptr; |
|
strncpy(memfile, fname, 125); |
if ((NULL != (ptr = strrchr(memfile, '/')))&&(ptr>memfile)) { |
ptr++; |
sprintf(ptr, "%scmem_%c%d.hex", (inv)?"i":"", (odd)?'o':'e', stage*2); |
} else { |
sprintf(memfile, "%s/%scmem_%c%d.hex", |
COREDIR, (inv)?"i":"", |
(odd)?'o':'e', stage*2); |
{ |
char *memfile, *ptr; |
|
memfile = new char[strlen(fname)+128]; |
strcpy(memfile, fname); |
if ((NULL != (ptr = strrchr(memfile, '/')))&&(ptr>memfile)) { |
ptr++; |
sprintf(ptr, "%scmem_%c%d.hex", (inv)?"i":"", (odd)?'o':'e', stage*2); |
} else { |
sprintf(memfile, "%s/%scmem_%c%d.hex", |
COREDIR, (inv)?"i":"", |
(odd)?'o':'e', stage*2); |
} |
// strcpy(&memfile[strlen(memfile)-2], ".hex"); |
cmem = fopen(memfile, "w"); |
if (NULL == cmem) { |
fprintf(stderr, "Could not open/write \'%s\' with FFT coefficients.\n", memfile); |
perror("Err from O/S:"); |
exit(-2); |
} |
|
delete[] memfile; |
} |
// strcpy(&memfile[strlen(memfile)-2], ".hex"); |
cmem = fopen(memfile, "w"); |
// fprintf(cmem, "// CBITS = %d, inv = %s\n", cbits, (inv)?"true":"false"); |
for(int i=0; i<stage/2; i++) { |
int k = 2*i+odd; |
1041,7 → 1068,7
"\t\tbfly(i_clk, i_rst, i_ce, ib_c,\n" |
"\t\t\tib_a, ib_b, ib_sync, ob_a, ob_b, ob_sync);\n" |
"endmodule;\n", |
lgdelay(nbits, xtra), (1<xtra)?(nbits+4):(nbits+xtra+3)); |
lgdelay(nbits, xtra), bflydelay(nbits, xtra)); |
} |
|
void usage(void) { |
1084,13 → 1111,18
bool bitreverse = true, inverse=false, interactive = false, |
verbose_flag = false; |
FILE *vmain; |
char fname[128], coredir[1024] = "fft-core"; |
std::string coredir = "fft-core", cmdline = ""; |
|
|
if (argc <= 1) |
usage(); |
|
cmdline = argv[0]; |
for(int argn=1; argn<argc; argn++) { |
cmdline += " "; |
cmdline += argv[argn]; |
} |
|
for(int argn=1; argn<argc; argn++) { |
if ('-' == argv[argn][0]) { |
for(int j=1; (argv[argn][j])&&(j<100); j++) { |
switch(argv[argn][j]) { |
1113,7 → 1145,7
printf("No extra number of coefficient bits given\n"); |
usage(); exit(-1); |
} |
strcpy(coredir, argv[++argn]); |
coredir = argv[++argn]; |
j += 200; |
break; |
case 'f': |
1242,27 → 1274,35
|
{ |
struct stat sbuf; |
if (lstat(coredir, &sbuf)==0) { |
if (lstat(coredir.c_str(), &sbuf)==0) { |
if (!S_ISDIR(sbuf.st_mode)) { |
fprintf(stderr, "\'%s\' already exists, and is not a directory!\n", coredir); |
fprintf(stderr, "\'%s\' already exists, and is not a directory!\n", coredir.c_str()); |
fprintf(stderr, "I will stop now, lest I overwrite something you care about.\n"); |
fprintf(stderr, "To try again, please remove this file.\n"); |
exit(-1); |
} |
} else |
mkdir(coredir, 0755); |
if (access(coredir, X_OK|W_OK) != 0) { |
fprintf(stderr, "I have no access to the directory \'%s\'.\n", coredir); |
mkdir(coredir.c_str(), 0755); |
if (access(coredir.c_str(), X_OK|W_OK) != 0) { |
fprintf(stderr, "I have no access to the directory \'%s\'.\n", coredir.c_str()); |
exit(-1); |
} |
} |
|
sprintf(fname, "%s/%sfftmain.v", coredir, (inverse)?"i":""); |
vmain = fopen(fname, "w"); |
if (NULL == vmain) { |
fprintf(stderr, "Could not open \'%s\' for writing\n", fname); |
perror("Err from O/S:"); |
exit(-1); |
{ |
std::string fname_string; |
|
fname_string = coredir; |
fname_string += "/"; |
if (inverse) fname_string += "i"; |
fname_string += "fftmain.v"; |
|
vmain = fopen(fname_string.c_str(), "w"); |
if (NULL == vmain) { |
fprintf(stderr, "Could not open \'%s\' for writing\n", fname_string.c_str()); |
perror("Err from O/S:"); |
exit(-1); |
} |
} |
|
fprintf(vmain, "/////////////////////////////////////////////////////////////////////////////\n"); |
1307,6 → 1347,11
fprintf(vmain, "//\t\t\tproduced by this FFT following a reset. Ever after,\n"); |
fprintf(vmain, "//\t\t\tthis will indicate the first sample of an FFT frame.\n"); |
fprintf(vmain, "//\n"); |
fprintf(vmain, "// Arguments:\tThis file was computer generated using the\n"); |
fprintf(vmain, "//\t\tfollowing command line:\n"); |
fprintf(vmain, "//\n"); |
fprintf(vmain, "//\t\t%% %s\n", cmdline.c_str()); |
fprintf(vmain, "//\n"); |
fprintf(vmain, "%s", creator); |
fprintf(vmain, "//\n"); |
fprintf(vmain, "%s", cpyleft); |
1363,11 → 1408,27
fprintf(vmain, "\t\t\t(~i_rst), i_right, w_o%d, w_os%d);\n", fftsize, fftsize); |
fprintf(vmain, "\n\n"); |
|
sprintf(fname, "%s/%sfftstage_e%d.v", coredir, (inverse)?"i":"", fftsize); |
build_stage(fname, fftsize/2, 0, nbits, inverse, xtracbits); // Even stage |
sprintf(fname, "%s/%sfftstage_o%d.v", coredir, (inverse)?"i":"", fftsize); |
build_stage(fname, fftsize/2, 1, nbits, inverse, xtracbits); // Odd stage |
{ |
std::string fname; |
char numstr[12]; |
|
fname = coredir + "/"; |
if (inverse) fname += "i"; |
fname += "fftstage_e"; |
sprintf(numstr, "%d", fftsize); |
fname += numstr; |
fname += ".v"; |
build_stage(fname.c_str(), fftsize/2, 0, nbits, inverse, xtracbits); // Even stage |
|
fname = coredir + "/"; |
if (inverse) fname += "i"; |
fname += "fftstage_o"; |
sprintf(numstr, "%d", fftsize); |
fname += numstr; |
fname += ".v"; |
build_stage(fname.c_str(), fftsize/2, 1, nbits, inverse, xtracbits); // Odd stage |
} |
|
nbits += 1; // New number of input bits |
tmp_size >>= 1; lgtmp--; |
dropbit = 0; |
1394,12 → 1455,28
fprintf(vmain, "\t\t\t\t\t\tw_s%d, w_o%d, w_o%d, w_os%d);\n", tmp_size<<1, tmp_size<<1, tmp_size, tmp_size); |
fprintf(vmain, "\n\n"); |
|
sprintf(fname, "%s/%sfftstage_e%d.v", coredir, (inverse)?"i":"", tmp_size); |
build_stage(fname, tmp_size/2, 0, nbits, inverse, xtracbits); // Even stage |
sprintf(fname, "%s/%sfftstage_o%d.v", coredir, (inverse)?"i":"", tmp_size); |
build_stage(fname, tmp_size/2, 1, nbits, inverse, xtracbits); // Odd stage |
{ |
std::string fname; |
char numstr[12]; |
|
fname = coredir + "/"; |
if (inverse) fname += "i"; |
fname += "fftstage_e"; |
sprintf(numstr, "%d", tmp_size); |
fname += numstr; |
fname += ".v"; |
build_stage(fname.c_str(), tmp_size/2, 0, nbits, inverse, xtracbits); // Even stage |
|
fname = coredir + "/"; |
if (inverse) fname += "i"; |
fname += "fftstage_o"; |
sprintf(numstr, "%d", tmp_size); |
fname += numstr; |
fname += ".v"; |
build_stage(fname.c_str(), tmp_size/2, 1, nbits, inverse, xtracbits); // Odd stage |
} |
|
|
dropbit ^= 1; |
nbits = obits; |
tmp_size >>= 1; lgtmp--; |
1483,21 → 1560,25
fprintf(vmain, "endmodule\n"); |
fclose(vmain); |
|
sprintf(fname, "%s/butterfly.v", coredir); |
build_butterfly(fname); |
{ |
std::string fname; |
|
sprintf(fname, "%s/shiftaddmpy.v", coredir); |
build_multiply(fname); |
fname = coredir + "/butterfly.v"; |
build_butterfly(fname.c_str(), xtracbits); |
|
sprintf(fname, "%s/qtrstage.v", coredir); |
build_quarters(fname); |
fname = coredir + "/shiftaddmpy.v"; |
build_multiply(fname.c_str()); |
|
sprintf(fname, "%s/dblstage.v", coredir); |
build_dblstage(fname); |
fname = coredir + "/qtrstage.v"; |
build_quarters(fname.c_str()); |
|
if (bitreverse) { |
sprintf(fname, "%s/dblreverse.v", coredir); |
build_dblreverse(fname); |
fname = coredir + "/dblstage.v"; |
build_dblstage(fname.c_str()); |
|
if (bitreverse) { |
fname = coredir + "/dblreverse.v"; |
build_dblreverse(fname.c_str()); |
} |
} |
} |
|
/trunk/sw/Makefile
14,13 → 14,13
|
.PHONY: fft |
fft: fftgen |
./fftgen -f 2048 -n 16 |
./fftgen -f 2048 -n 16 |
cd $(CORED)/; verilator -cc fftmain.v |
cd $(OBJDR); make -f Vfftmain.mk |
|
.PHONY: ifft |
ifft: fftgen |
./fftgen -f 2048 -1 -n 24 -m 24 |
./fftgen -f 2048 -1 -n 22 |
cd $(CORED)/; verilator -cc ifftmain.v |
cd $(OBJDR); make -f Vifftmain.mk |
|