OpenCores
URL https://opencores.org/ocsvn/dblclockfft/dblclockfft/trunk

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [bench/] [cpp/] [mpy_tb.cpp] - Blame information for rev 30

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 dgisselq
////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    mpy_tb.cpp
4
//
5
// Project:     A Doubletime Pipelined FFT
6
//
7
// Purpose:     A test-bench for the shift and add shiftaddmpy.v subfile of
8
//              the double clocked FFT.  This file may be run autonomously. 
9
//              If so, the last line output will either read "SUCCESS" on
10
//              success, or some other failure message otherwise.
11
//
12
//              This file depends upon verilator to both compile, run, and
13
//              therefore test shiftaddmpy.v
14
//
15
// Creator:     Dan Gisselquist, Ph.D.
16 30 dgisselq
//              Gisselquist Technology, LLC
17 6 dgisselq
//
18
///////////////////////////////////////////////////////////////////////////
19
//
20
// Copyright (C) 2015, Gisselquist Technology, LLC
21
//
22
// This program is free software (firmware): you can redistribute it and/or
23
// modify it under the terms of  the GNU General Public License as published
24
// by the Free Software Foundation, either version 3 of the License, or (at
25
// your option) any later version.
26
//
27
// This program is distributed in the hope that it will be useful, but WITHOUT
28
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
29
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
// for more details.
31
//
32
// You should have received a copy of the GNU General Public License along
33
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
34
// target there if the PDF file isn't present.)  If not, see
35
// <http://www.gnu.org/licenses/> for a copy.
36
//
37
// License:     GPL, v3, as defined and found on www.gnu.org,
38
//              http://www.gnu.org/licenses/gpl.html
39
//
40
//
41
///////////////////////////////////////////////////////////////////////////
42 29 dgisselq
#include "fftsize.h"
43
#ifdef  USE_OLD_MULTIPLY
44 3 dgisselq
#include "Vshiftaddmpy.h"
45 29 dgisselq
typedef Vshiftaddmpy    Vmpy;
46
#define AW      TST_SHIFTADDMPY_AW
47
#define BW      TST_SHIFTADDMPY_BW
48
#define DELAY   (TST_SHIFTADDMPY_AW+2)
49
#else
50
#include "Vlongbimpy.h"
51
typedef Vlongbimpy      Vmpy;
52
#define AW      TST_LONGBIMPY_AW
53
#define BW      TST_LONGBIMPY_BW
54
#define DELAY   ((AW/2)+(AW&1)+2)
55
#endif
56
 
57 3 dgisselq
#include "verilated.h"
58 29 dgisselq
#include "twoc.h"
59 3 dgisselq
 
60
class   MPYTB {
61
public:
62 29 dgisselq
        Vmpy    *mpy;
63 3 dgisselq
        long    vals[32];
64
        int     m_addr;
65
 
66
        MPYTB(void) {
67 29 dgisselq
                mpy = new Vmpy;
68 3 dgisselq
 
69
                for(int i=0; i<32; i++)
70
                        vals[i] = 0;
71
                m_addr = 0;
72
        }
73
        ~MPYTB(void) {
74
                delete mpy;
75
        }
76
 
77
        void    tick(void) {
78
                mpy->i_clk = 0;
79
                mpy->eval();
80
                mpy->i_clk = 1;
81
                mpy->eval();
82
        }
83
 
84
        void    reset(void) {
85
                mpy->i_clk = 0;
86
                mpy->i_ce = 1;
87
                mpy->i_a = 0;
88
                mpy->i_b = 0;
89
 
90
                for(int k=0; k<20; k++)
91
                        tick();
92
        }
93
 
94
        bool    test(const int ia, const int ib) {
95
                bool    success;
96 29 dgisselq
                long    a, b, out;
97 3 dgisselq
 
98 29 dgisselq
                a = sbits(ia, AW);
99
                b = sbits(ib, BW);
100 3 dgisselq
                mpy->i_ce = 1;
101 29 dgisselq
                mpy->i_a = ubits(a, AW);
102
                mpy->i_b = ubits(b, BW);
103 3 dgisselq
 
104 29 dgisselq
                vals[m_addr&31] = a * b;
105 3 dgisselq
 
106
                tick();
107 29 dgisselq
                if (rand()&1) {
108
                        mpy->i_ce = 0;
109
                        tick();
110
                }
111 3 dgisselq
 
112 29 dgisselq
                printf("k=%3d: A =%04x, B =%05x -> O = %9lx (ANS=%10lx)\n",
113
                        m_addr, (int)ubits(a,AW), (int)ubits(b,BW),
114
                        (long)mpy->o_r, ubits(vals[m_addr&31], AW+BW+4));
115 3 dgisselq
 
116 29 dgisselq
                out = sbits(mpy->o_r, AW+BW);
117 3 dgisselq
 
118
                m_addr++;
119
 
120 29 dgisselq
                success = (m_addr < (DELAY+2))||(out == vals[(m_addr-DELAY)&31]);
121 3 dgisselq
                if (!success) {
122 29 dgisselq
                        printf("WRONG ANSWER: %8lx (exp) != %8lx (sut)\n", vals[(m_addr-DELAY)&0x01f], out);
123 3 dgisselq
                        exit(-1);
124
                }
125
 
126
                return success;
127
        }
128
};
129
 
130
int     main(int argc, char **argv, char **envp) {
131
        Verilated::commandArgs(argc, argv);
132
        MPYTB           *tb = new MPYTB;
133
 
134
        tb->reset();
135
 
136
        for(int k=0; k<15; k++) {
137
                int     a, b;
138
 
139
                a = (1<<k);
140
                b = 1;
141
                tb->test(a, b);
142
        }
143
 
144
        for(int k=0; k<15; k++) {
145
                int     a, b, out;
146
 
147
                a = (1<<15);
148
                b = (1<<k);
149
                tb->test(a, b);
150
        }
151
 
152 29 dgisselq
        for(int k=0; k<2048; k++) {
153 3 dgisselq
                int     a, b, out;
154
 
155
                tb->test(rand(), rand());
156
        }
157
 
158
        delete  tb;
159
 
160 4 dgisselq
        printf("SUCCESS!\n");
161 3 dgisselq
        exit(0);
162
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.