////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Filename: mpy_tb.cpp
|
// Filename: mpy_tb.cpp
|
//
|
//
|
// Project: A General Purpose Pipelined FFT Implementation
|
// Project: A General Purpose Pipelined FFT Implementation
|
//
|
//
|
// Purpose: A test-bench for the shift and add shiftaddmpy.v subfile of
|
// Purpose: A test-bench for the shift and add shiftaddmpy.v subfile of
|
// the double clocked FFT. This file may be run autonomously.
|
// the double clocked FFT. This file may be run autonomously.
|
// If so, the last line output will either read "SUCCESS" on
|
// If so, the last line output will either read "SUCCESS" on success, or
|
// success, or some other failure message otherwise.
|
// some other failure message otherwise.
|
//
|
//
|
// This file depends upon verilator to both compile, run, and
|
// This file depends upon verilator to both compile, run, and therefore
|
// therefore test shiftaddmpy.v
|
// test shiftaddmpy.v
|
//
|
//
|
// Creator: Dan Gisselquist, Ph.D.
|
// Creator: Dan Gisselquist, Ph.D.
|
// Gisselquist Technology, LLC
|
// Gisselquist Technology, LLC
|
//
|
//
|
///////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Copyright (C) 2015-2018, Gisselquist Technology, LLC
|
// Copyright (C) 2015-2018, Gisselquist Technology, LLC
|
//
|
//
|
// This program is free software (firmware): you can redistribute it and/or
|
// 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
|
// 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
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
// your option) any later version.
|
// your option) any later version.
|
//
|
//
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// 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
|
// 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
|
// target there if the PDF file isn't present.) If not, see
|
// <http://www.gnu.org/licenses/> for a copy.
|
// <http://www.gnu.org/licenses/> for a copy.
|
//
|
//
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
// http://www.gnu.org/licenses/gpl.html
|
// http://www.gnu.org/licenses/gpl.html
|
//
|
//
|
//
|
//
|
///////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
#include "verilated.h"
|
#include "verilated.h"
|
#include "verilated_vcd_c.h"
|
#include "verilated_vcd_c.h"
|
|
|
#include "fftsize.h"
|
#include "fftsize.h"
|
|
|
#ifdef USE_OLD_MULTIPLY
|
#ifdef USE_OLD_MULTIPLY
|
#include "Vshiftaddmpy.h"
|
#include "Vshiftaddmpy.h"
|
typedef Vshiftaddmpy Vmpy;
|
typedef Vshiftaddmpy Vmpy;
|
#define AW TST_SHIFTADDMPY_AW
|
#define AW TST_SHIFTADDMPY_AW
|
#define BW TST_SHIFTADDMPY_BW
|
#define BW TST_SHIFTADDMPY_BW
|
#define DELAY (TST_SHIFTADDMPY_AW+2)
|
#define DELAY (TST_SHIFTADDMPY_AW+2)
|
#else
|
#else
|
#include "Vlongbimpy.h"
|
#include "Vlongbimpy.h"
|
typedef Vlongbimpy Vmpy;
|
typedef Vlongbimpy Vmpy;
|
#define AW TST_LONGBIMPY_AW
|
#define AW TST_LONGBIMPY_AW
|
#define BW TST_LONGBIMPY_BW
|
#define BW TST_LONGBIMPY_BW
|
#define DELAY ((AW/2)+(AW&1)+2)
|
#define DELAY ((AW/2)+(AW&1)+2)
|
#endif
|
#endif
|
|
|
#include "twoc.h"
|
#include "twoc.h"
|
|
|
class MPYTB {
|
class MPYTB {
|
public:
|
public:
|
Vmpy *m_mpy;
|
Vmpy *m_mpy;
|
VerilatedVcdC *m_trace;
|
VerilatedVcdC *m_trace;
|
long vals[32];
|
long vals[32];
|
int m_addr;
|
int m_addr;
|
uint64_t m_tickcount;
|
uint64_t m_tickcount;
|
|
|
MPYTB(void) {
|
MPYTB(void) {
|
Verilated::traceEverOn(true);
|
Verilated::traceEverOn(true);
|
m_mpy = new Vmpy;
|
m_mpy = new Vmpy;
|
m_tickcount = 0;
|
m_tickcount = 0;
|
|
|
for(int i=0; i<32; i++)
|
for(int i=0; i<32; i++)
|
vals[i] = 0;
|
vals[i] = 0;
|
m_addr = 0;
|
m_addr = 0;
|
}
|
}
|
~MPYTB(void) {
|
~MPYTB(void) {
|
closetrace();
|
closetrace();
|
delete m_mpy;
|
delete m_mpy;
|
}
|
}
|
|
|
void opentrace(const char *vcdname) {
|
void opentrace(const char *vcdname) {
|
if (!m_trace) {
|
if (!m_trace) {
|
m_trace = new VerilatedVcdC;
|
m_trace = new VerilatedVcdC;
|
m_mpy->trace(m_trace, 99);
|
m_mpy->trace(m_trace, 99);
|
m_trace->open(vcdname);
|
m_trace->open(vcdname);
|
}
|
}
|
}
|
}
|
|
|
void closetrace(void) {
|
void closetrace(void) {
|
if (m_trace) {
|
if (m_trace) {
|
m_trace->close();
|
m_trace->close();
|
delete m_trace;
|
delete m_trace;
|
m_trace = NULL;
|
m_trace = NULL;
|
}
|
}
|
}
|
}
|
|
|
void tick(void) {
|
void tick(void) {
|
m_tickcount++;
|
m_tickcount++;
|
|
|
m_mpy->i_clk = 0;
|
m_mpy->i_clk = 0;
|
m_mpy->eval();
|
m_mpy->eval();
|
if (m_trace) m_trace->dump((uint64_t)(10ul*m_tickcount-2));
|
if (m_trace) m_trace->dump((vluint64_t)(10ul*m_tickcount-2));
|
m_mpy->i_clk = 1;
|
m_mpy->i_clk = 1;
|
m_mpy->eval();
|
m_mpy->eval();
|
if (m_trace) m_trace->dump((uint64_t)(10ul*m_tickcount));
|
if (m_trace) m_trace->dump((vluint64_t)(10ul*m_tickcount));
|
m_mpy->i_clk = 0;
|
m_mpy->i_clk = 0;
|
m_mpy->eval();
|
m_mpy->eval();
|
if (m_trace) {
|
if (m_trace) {
|
m_trace->dump((uint64_t)(10ul*m_tickcount+5));
|
m_trace->dump((vluint64_t)(10ul*m_tickcount+5));
|
m_trace->flush();
|
m_trace->flush();
|
}
|
}
|
}
|
}
|
|
|
void cetick(void) {
|
void cetick(void) {
|
int ce = m_mpy->i_ce, nkce;
|
int ce = m_mpy->i_ce, nkce;
|
|
|
tick();
|
tick();
|
nkce = (rand()&1);
|
nkce = (rand()&1);
|
#ifdef FFT_CKPCE
|
#ifdef FFT_CKPCE
|
nkce += FFT_CKPCE;
|
nkce += FFT_CKPCE;
|
#endif
|
#endif
|
if ((ce)&&(nkce>0)) {
|
if ((ce)&&(nkce>0)) {
|
m_mpy->i_ce = 0;
|
m_mpy->i_ce = 0;
|
for(int kce=1; kce<nkce; kce++)
|
for(int kce=1; kce<nkce; kce++)
|
tick();
|
tick();
|
}
|
}
|
|
|
m_mpy->i_ce = ce;
|
m_mpy->i_ce = ce;
|
}
|
}
|
|
|
void reset(void) {
|
void reset(void) {
|
m_mpy->i_clk = 0;
|
m_mpy->i_clk = 0;
|
m_mpy->i_ce = 1;
|
m_mpy->i_ce = 1;
|
m_mpy->i_a_unsorted = 0;
|
m_mpy->i_a_unsorted = 0;
|
m_mpy->i_b_unsorted = 0;
|
m_mpy->i_b_unsorted = 0;
|
|
|
for(int k=0; k<20; k++)
|
for(int k=0; k<20; k++)
|
cetick();
|
cetick();
|
}
|
}
|
|
|
bool test(const int ia, const int ib) {
|
bool test(const int ia, const int ib) {
|
bool success;
|
bool success;
|
long a, b, out;
|
long a, b, out;
|
|
|
a = sbits(ia, AW);
|
a = sbits(ia, AW);
|
b = sbits(ib, BW);
|
b = sbits(ib, BW);
|
m_mpy->i_ce = 1;
|
m_mpy->i_ce = 1;
|
m_mpy->i_a_unsorted = ubits(a, AW);
|
m_mpy->i_a_unsorted = ubits(a, AW);
|
m_mpy->i_b_unsorted = ubits(b, BW);
|
m_mpy->i_b_unsorted = ubits(b, BW);
|
|
|
vals[m_addr&31] = a * b;
|
vals[m_addr&31] = a * b;
|
|
|
cetick();
|
cetick();
|
|
|
printf("k=%3d: A =%0*x, B =%0*x -> O = %*lx (ANS=%*lx)\n",
|
printf("k=%3d: A =%0*x, B =%0*x -> O = %*lx (ANS=%*lx)\n",
|
m_addr, (AW+3)/4, (int)ubits(a,AW),
|
m_addr, (AW+3)/4, (int)ubits(a,AW),
|
(BW+3)/4, (int)ubits(b,BW),
|
(BW+3)/4, (int)ubits(b,BW),
|
(AW+BW+3)/4, (long)m_mpy->o_r,
|
(AW+BW+3)/4, (long)m_mpy->o_r,
|
(AW+BW+7)/4, ubits(vals[m_addr&31], AW+BW+4));
|
(AW+BW+7)/4, ubits(vals[m_addr&31], AW+BW+4));
|
|
|
out = sbits(m_mpy->o_r, AW+BW);
|
out = sbits(m_mpy->o_r, AW+BW);
|
|
|
m_addr++;
|
m_addr++;
|
|
|
success = (m_addr < (DELAY+2))||(out == vals[(m_addr-DELAY)&31]);
|
success = (m_addr < (DELAY+2))||(out == vals[(m_addr-DELAY)&31]);
|
if (!success) {
|
if (!success) {
|
printf("WRONG ANSWER: %8lx (exp) != %8lx (sut)\n", vals[(m_addr-DELAY)&0x01f], out);
|
printf("WRONG ANSWER: %8lx (exp) != %8lx (sut)\n", vals[(m_addr-DELAY)&0x01f], out);
|
exit(-1);
|
exit(-1);
|
}
|
}
|
|
|
return success;
|
return success;
|
}
|
}
|
};
|
};
|
|
|
int main(int argc, char **argv, char **envp) {
|
int main(int argc, char **argv, char **envp) {
|
Verilated::commandArgs(argc, argv);
|
Verilated::commandArgs(argc, argv);
|
MPYTB *tb = new MPYTB;
|
MPYTB *tb = new MPYTB;
|
|
|
tb->opentrace("mpy.vcd");
|
// tb->opentrace("mpy.vcd");
|
tb->reset();
|
tb->reset();
|
|
|
for(int k=0; k<15; k++) {
|
for(int k=0; k<15; k++) {
|
int a, b;
|
int a, b;
|
|
|
a = (1<<k);
|
a = (1<<k);
|
b = 1;
|
b = 1;
|
tb->test(a, b);
|
tb->test(a, b);
|
}
|
}
|
|
|
for(int k=0; k<15; k++) {
|
for(int k=0; k<15; k++) {
|
int a, b, out;
|
int a, b, out;
|
|
|
a = (1<<15);
|
a = (1<<15);
|
b = (1<<k);
|
b = (1<<k);
|
tb->test(a, b);
|
tb->test(a, b);
|
}
|
}
|
|
|
if (AW+BW <= 20) {
|
if (AW+BW <= 20) {
|
// Exhaustive test
|
// Exhaustive test
|
for(int a=0; a< (1<<AW); a++)
|
for(int a=0; a< (1<<AW); a++)
|
for(int b=0; b< (1<<BW); b++)
|
for(int b=0; b< (1<<BW); b++)
|
tb->test(a, b);
|
tb->test(a, b);
|
printf("Exhaust complete\n");
|
printf("Exhaust complete\n");
|
} else {
|
} else {
|
// Pseudorandom test
|
// Pseudorandom test
|
for(int k=0; k<2048; k++)
|
for(int k=0; k<2048; k++)
|
tb->test(rand(), rand());
|
tb->test(rand(), rand());
|
}
|
}
|
|
|
delete tb;
|
delete tb;
|
|
|
printf("SUCCESS!\n");
|
printf("SUCCESS!\n");
|
exit(0);
|
exit(0);
|
}
|
}
|
|
|