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

Subversion Repositories dblclockfft

[/] [dblclockfft/] [trunk/] [bench/] [cpp/] [butterfly_tb.cpp] - Diff between revs 4 and 5

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 4 Rev 5
Line 2... Line 2...
#include <stdint.h>
#include <stdint.h>
 
 
#include "Vbutterfly.h"
#include "Vbutterfly.h"
#include "verilated.h"
#include "verilated.h"
 
 
void    tick(Vbutterfly *bfly) {
class   BFLY_TB {
        bfly->i_clk = 0;
public:
        bfly->eval();
        Vbutterfly      *m_bfly;
        bfly->i_clk = 1;
        unsigned long   m_left[64], m_right[64];
        bfly->eval();
        int             m_addr, m_lastaux;
 
 
 
        BFLY_TB(void) {
 
                m_bfly = new Vbutterfly;
 
                m_addr = 0;
}
}
 
 
void    reset(Vbutterfly *bfly) {
        void    tick(void) {
        bfly->i_ce  = 0;
                m_lastaux = m_bfly->o_aux;
        bfly->i_rst = 1;
                m_bfly->i_clk = 0;
        tick(bfly);
                m_bfly->eval();
        bfly->i_ce  = 0;
                m_bfly->i_clk = 1;
        bfly->i_rst = 0;
                m_bfly->eval();
        tick(bfly);
 
}
}
 
 
int     main(int argc, char **argv, char **envp) {
        void    reset(void) {
        Verilated::commandArgs(argc, argv);
                m_bfly->i_ce    = 0;
        Vbutterfly      *bfly = new Vbutterfly;
                m_bfly->i_aux   = 1;
        int16_t         ir0, ii0, lstr, lsti;
                m_bfly->i_coef  = 0l;
        int32_t         sumr, sumi, difr, difi;
                m_bfly->i_left  = 0;
        int32_t         smr, smi, dfr, dfi;
                m_bfly->i_right = 0;
        int             rnd = 0;
                tick();
 
                m_bfly->i_ce  = 1;
        reset(bfly);
                m_bfly->i_aux = 1;
 
 
        for(int k=0; k<270; k++) {
                for(int i=0; i<40; i++)
                int32_t or0, oi0, or1, oi1;
                        tick();
 
                m_bfly->i_aux = 0;
 
                tick();
 
        }
 
 
                bfly->i_ce = 1;
        void    test(const int n, const int k, const unsigned long cof,
                bfly->i_sync = ((k&0x0ff)==0);
                        const unsigned lft, const unsigned rht, const int aux) {
                // Let's pick some random values, ...
 
                ir0 = rand(); if (ir0&4) ir0 = -ir0;
 
                ii0 = rand(); if (ii0&2) ii0 = -ii0;
 
 
 
                bfly->i_data  = ((ir0&0x0ffff) << 16) | (ii0 & 0x0ffff);
 
                tick(bfly);
 
 
 
                printf("k=%3d: COEF=%08x, LFT=%08x, RHT=%08x, AUX=%d, OLFT =%09lx, ORHT=%09lx, AUX=%d\n",
 
                        k, bfly->i_coef, bfly->i_left, bfly->i_right, bfly->i_aux,
 
                        bfly->o_left, bfly->o_right, bfly->o_sync);
 
 
 
                or0 = (bfly->o_data  >> 17) & 0x01ffff;
 
                oi0 =  bfly->o_data         & 0x01ffff;
 
                if (or0 & 0x010000)     or0 |= (-1<<16);
 
                if (oi0 & 0x010000)     oi0 |= (-1<<16);
 
 
 
                if (k>3) {
                m_bfly->i_coef  = cof & (~(-1l << 40));
 
                m_bfly->i_left  = lft;
 
                m_bfly->i_right = rht;
 
                m_bfly->i_aux   = aux & 1;
 
 
 
                tick();
 
 
 
                if ((m_bfly->o_aux)&&(!m_lastaux))
 
                        printf("\n");
 
                printf("n,k=%d,%3d: COEF=%010lx, LFT=%08x, RHT=%08x, A=%d, OLFT =%09lx, ORHT=%09lx, AUX=%d\n",
 
                        n,k,
 
                        m_bfly->i_coef & (~(-1l<<40)),
 
                        m_bfly->i_left,
 
                        m_bfly->i_right,
 
                        m_bfly->i_aux,
 
                        m_bfly->o_left,
 
                        m_bfly->o_right,
 
                        m_bfly->o_aux);
                        /*
                        /*
                        printf("\tOR0 = %6x, OI0 = %6x, SUM = %6x + %6x, DIF = %6x + %6x\n",
                printf("\tFI=%010lx",
                                or0, oi0, sumr, sumi, difr, difi);
                        ((((long)m_bfly->v__DOT__r_aux_2)&1l)<<34)
 
                        |((((long)m_bfly->v__DOT__r_sum_r)&0x01ffffl)<<17)
 
                        |(((long)m_bfly->v__DOT__r_sum_i)&0x01ffffl));
 
                printf("\tFO=%010lx SUMR=%05x SUMI=%05x A=%d",
 
                        m_bfly->v__DOT__fifo_read,
 
                        m_bfly->v__DOT__r_sum_r,
 
                        m_bfly->v__DOT__r_sum_i,
 
                        m_bfly->v__DOT__r_aux_2);
 
                printf("\tML=%09lx, MR=%09lx, ",
 
                        m_left[ (m_addr-23)&(64-1)],
 
                        m_right[(m_addr-23)&(64-1)]);
                        */
                        */
                        if (0==(k&1)) {
                /*
                                if (or0 != sumr)        {fprintf(stderr, "FAIL 1\n"); exit(-1);}
                printf("\tBLFTR=%10lx BLFTI=%10lx",
                                if (oi0 != sumi)        {fprintf(stderr, "FAIL 2\n"); exit(-1);}
                        m_bfly->v__DOT__b_left_r & (~(-1l<<40)),
                        } else if (1==(k&1)) {
                        m_bfly->v__DOT__b_left_i & (~(-1l<<40)));
                                if (or0 != difr)        {fprintf(stderr, "FAIL 3\n"); exit(-1);}
                printf("\tMPYR=%10lx MPYI=%10lx",
                                if (oi0 != difi)        {fprintf(stderr, "FAIL 4\n"); exit(-1);}
                        m_bfly->v__DOT__mpy_r & (~(-1l<<40)),
 
                        m_bfly->v__DOT__mpy_i & (~(-1l<<40)));
 
                printf("\n");
 
                */
 
 
 
                if (m_left[(m_addr-23)&(64-1)] != m_bfly->o_left) {
 
                        fprintf(stderr, "WRONG O_LEFT!\n");
 
                        exit(-1);
                        }
                        }
 
 
 
                if (m_right[(m_addr-23)&(64-1)] != m_bfly->o_right) {
 
                        fprintf(stderr, "WRONG O_RIGHT!\n");
 
                        exit(-1);
                }
                }
 
 
                if (((4==(k&0x0ff))?1:0) != bfly->o_sync) { fprintf(stderr, "BAD O-SYNC\n"); exit(-1); }
                // Now, let's calculate an "expected" result ...
 
                long    rlft, ilft;
 
 
                if (1 == (k&1)) {
                // Extract left and right values ...
                        sumr = smr; sumi = smi; difr=dfr, difi= dfi;
                rlft = (m_bfly->i_left >> 16) & 0x0ffff;
 
                ilft = (m_bfly->i_left      ) & 0x0ffff;
 
                // Make certain they are properly sign extended ...
 
                if (rlft & 0x8000) rlft |= (-1<<16);
 
                if (ilft & 0x8000) ilft |= (-1<<16);
 
 
 
                // Now repeat for the right hand value ...
 
                long    rrht, irht;
 
                // Extract left and right values ...
 
                rrht = (m_bfly->i_right >> 16) & 0x0ffff;
 
                irht = (m_bfly->i_right      ) & 0x0ffff;
 
                // Make certain they are properly sign extended ...
 
                if (rrht & 0x8000) rrht |= (-1<<16);
 
                if (irht & 0x8000) irht |= (-1<<16);
 
 
 
 
 
                // and again for the coefficients
 
                long    rcof, icof;
 
                // Extract left and right values ...
 
                rcof = (m_bfly->i_coef >> 20) & 0x0fffff;
 
                icof = (m_bfly->i_coef      ) & 0x0fffff;
 
                // Make certain they are properly sign extended ...
 
                if (rcof & 0x80000) rcof |= (-1<<20);
 
                if (icof & 0x80000) icof |= (-1<<20);
 
 
 
 
 
                // Now, let's do the butterfly ourselves ...
 
                long sumi, sumr, difi, difr;
 
                sumr = rlft + rrht;
 
                sumi = ilft + irht;
 
                difr = rlft - rrht;
 
                difi = ilft - irht;
 
 
                        smr = lstr + ir0 + rnd;
        /*
                        smi = lsti + ii0 + rnd;
                printf("L=%5lx+%5lx,R=%5lx+%5lx,S=%5lx+%5lx,D=%5lx+%5lx, ",
 
                        rlft & 0x02ffffl,
 
                        ilft & 0x02ffffl,
 
                        rrht & 0x02ffffl,
 
                        irht & 0x02ffffl,
 
                        sumr & 0x02ffffl,
 
                        sumi & 0x02ffffl,
 
                        difr & 0x02ffffl,
 
                        difi & 0x02ffffl);
 
        */
 
                long p1, p2, p3, mpyr, mpyi;
 
                p1 = difr * rcof;
 
                p2 = difi * icof;
 
                p3 = (difr + difi) * (rcof + icof);
 
 
 
                mpyr = p1-p2;
 
                mpyi = p3-p1-p2;
 
 
 
        /*
 
                printf("RC=%lx, IC=%lx, ", rcof, icof);
 
                printf("P1=%lx,P2=%lx,P3=%lx, ", p1,p2,p3);
 
                printf("MPYr = %lx, ", mpyr);
 
                printf("MPYi = %lx, ", mpyi);
 
        */
 
 
                        dfr = lstr - ir0 + rnd;
                long    o_left_r, o_left_i, o_right_r, o_right_i;
                        dfi = lsti - ii0 + rnd;
                unsigned long   o_left, o_right;
 
 
 
                o_left_r = sumr & 0x01ffff; o_left_i = sumi & 0x01ffff;
 
                o_left = (o_left_r << 17) | (o_left_i);
 
 
 
                o_right_r = (mpyr>>18) & 0x01ffff;
 
                o_right_i = (mpyi>>18) & 0x01ffff;
 
                o_right = (o_right_r << 17) | (o_right_i);
 
        /*
 
                printf("oR_r = %lx, ", o_right_r);
 
                printf("oR_i = %lx\n", o_right_i);
 
        */
 
 
 
                m_left[ m_addr&(64-1)] = o_left;
 
                m_right[m_addr&(64-1)] = o_right;
 
 
 
                m_addr++;
                }
                }
 
};
 
 
 
int     main(int argc, char **argv, char **envp) {
 
        Verilated::commandArgs(argc, argv);
 
        BFLY_TB *bfly = new BFLY_TB;
 
        int16_t         ir0, ii0, lstr, lsti;
 
        int32_t         sumr, sumi, difr, difi;
 
        int32_t         smr, smi, dfr, dfi;
 
        int             rnd = 0;
 
 
 
        const int       TESTSZ = 256;
 
 
 
        bfly->reset();
 
 
 
        bfly->test(9,0,0x4000000000l,0x7fff0000,0x7fff0000, 1);
 
        bfly->test(9,1,0x4000000000l,0x7fff0000,0x80010000, 0);
 
        bfly->test(9,2,0x4000000000l,0x00007fff,0x00008001, 0);
 
        bfly->test(9,3,0x4000000000l,0x00007fff,0x00007fff, 0);
 
 
 
        bfly->test(8,0,0x4000000000l,0x80010000,0x80010000, 1);
 
        bfly->test(8,1,0x4000000000l,0x00008001,0x00008001, 0);
 
 
 
        bfly->test(9,0,0x4000000000l,0x40000000,0xc0000000, 1);
 
        bfly->test(9,1,0x4000000000l,0x40000000,0x40000000, 0);
 
        bfly->test(9,2,0x4000000000l,0x00004000,0x0000c000, 0);
 
        bfly->test(9,3,0x4000000000l,0x00004000,0x00004000, 0);
 
 
 
        bfly->test(9,0,0x4000000000l,0x20000000,0xe0000000, 1);
 
        bfly->test(9,1,0x4000000000l,0x20000000,0x20000000, 0);
 
        bfly->test(9,2,0x4000000000l,0x00002000,0x0000e000, 0);
 
        bfly->test(9,3,0x4000000000l,0x00002000,0x00002000, 0);
 
 
 
        bfly->test(9,0,0x4000000000l,0x00080000,0xfff80000, 1);
 
        bfly->test(9,1,0x4000000000l,0x00080000,0x00080000, 0);
 
        bfly->test(9,2,0x4000000000l,0x00000008,0x0000fff8, 0);
 
        bfly->test(9,3,0x4000000000l,0x00000008,0x00000008, 0);
 
 
 
        bfly->test(9,0,0x4000000000l,0x00010000,0xffff0000, 1);
 
        bfly->test(9,1,0x4000000000l,0x00010000,0x00010000, 0);
 
        bfly->test(9,2,0x4000000000l,0x00000001,0x0000ffff, 0);
 
        bfly->test(9,3,0x4000000000l,0x00000001,0x00000001, 0);
 
 
 
        for(int n=0; n<4; n++) for(int k=0; k<TESTSZ; k++) {
 
                long    iv, rv;
 
                unsigned long   lft, rht, cof;
 
                double  c, s, W;
 
                bool    inv = 1;
 
                int     aux;
 
 
 
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k) / TESTSZ * 64;
 
                c = cos(W); s = sin(W);
 
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
 
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
 
 
 
                rv = (rv << 16) | (iv & (~(-1<<16)));
 
                lft = rv;
 
 
 
                W = ((inv)?-1:1) * 2.0 * M_PI * (2*k+1) / TESTSZ * 64;
 
                c = cos(W); s = sin(W);
 
                rv = (long)((double)(1l<<(16-2-n))*c+0.5);
 
                iv = (long)((double)(1l<<(16-2-n))*s+0.5);
 
 
 
                rv = (rv << 16) | (iv & (~(-1<<16)));
 
                rht = rv;
 
 
 
 
 
                // Switch the sign of W
 
                W = ((inv)?1:-1) * 2.0 * M_PI * (2*k) / TESTSZ;
 
                c = cos(W); s = sin(W);
 
                rv = (long)((double)(1l<<(20-2))*c+0.5); // Keep 20-2 bits for
 
                iv = (long)((double)(1l<<(20-2))*s+0.5); // coefficients
 
 
 
                rv = (rv << 20) | (iv & (~(-1<<20)));
 
                cof = rv;
 
 
 
                aux = ((k&(TESTSZ-1))==0);
 
 
                lstr = ir0;
                bfly->test(n,k, cof, lft, rht, aux);
                lsti = ii0;
 
        }
        }
 
 
        delete  bfly;
        delete  bfly;
 
 
        printf("SUCCESS!\n");
        printf("SUCCESS!\n");

powered by: WebSVN 2.1.0

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