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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [bench/] [cpp/] [div_tb.cpp] - Blame information for rev 148

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

Line No. Rev Author Line
1 77 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    div_tb.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     Bench testing for the divide unit found within the Zip CPU.
8
//
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
///////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015, Gisselquist Technology, LLC
16
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// License:     GPL, v3, as defined and found on www.gnu.org,
28
//              http://www.gnu.org/licenses/gpl.html
29
//
30
//
31
///////////////////////////////////////////////////////////////////////////////
32
//
33
//
34
#include <signal.h>
35
#include <time.h>
36
#include <unistd.h>
37
#include <assert.h>
38
 
39
#include <ctype.h>
40
 
41
#include "verilated.h"
42
#include "Vdiv.h"
43
 
44
#include "testb.h"
45
// #include "twoc.h"
46
 
47
class   DIV_TB : public TESTB<Vdiv> {
48
public:
49
        DIV_TB(void) {
50
        }
51
 
52
        ~DIV_TB(void) {}
53
 
54
        void    reset(void) {
55
                // m_flash.debug(false);
56
                TESTB<Vdiv>::reset();
57
        }
58
 
59
        bool    on_tick(void) {
60
                tick();
61
                return true;
62
        }
63
 
64
        void    bprint(char *str, int nbits, unsigned long v) {
65
                while(*str)
66
                        str++;
67
                for(int i=0; i<nbits; i++) {
68
                        if ((1l<<(nbits-1-i))&v)
69
                                *str++ = '1';
70
                        else
71
                                *str++ = '0';
72
                        if (((nbits-1-i)&3)==0)
73
                                *str++ = ' ';
74
                } *str = '\0';
75
        }
76
 
77
        void    tick(void) {
78 147 dgisselq
                bool    debug = false;
79
 
80
                if (debug) {
81
                        char    outstr[2048], *s;
82
                        sprintf(outstr, "Tick %4ld %s%s%s%s%s%s %2d",
83
                                m_tickcount,
84
                                (m_core->o_busy)?"B":" ",
85
                                (m_core->o_valid)?"V":" ",
86
                                (m_core->i_wr)?"W":" ",
87
                                (m_core->v__DOT__pre_sign)?"+":" ",
88
                                (m_core->v__DOT__r_sign)?"-":" ",
89
                                (m_core->v__DOT__r_z)?"Z":" ",
90
                                m_core->v__DOT__r_bit);
91
                        s = &outstr[strlen(outstr)];
92
                        sprintf(s, "%s\n%10s %40s",s, "Div","");
93
                                s = &s[strlen(s)];
94
                        bprint( s, 32, m_core->v__DOT__r_dividend);
95
                                s=&s[strlen(s)];
96
                        sprintf(s, "%s\n%10s ",s, "Div"); s = &s[strlen(s)];
97
                        bprint( s, 64, m_core->v__DOT__r_divisor);
98
                                s=&s[strlen(s)];
99
                        sprintf(s, "%s\n%10s %40s",s, "Q",""); s=&s[strlen(s)];
100
                        bprint( s, 32, m_core->o_quotient); s = &s[strlen(s)];
101
                        sprintf(s, "%s\n%10s %38s",s, "Diff","");
102
                                s=&s[strlen(s)];
103
                        bprint( s, 33, m_core->v__DOT__diff); s = &s[strlen(s)];
104
                        strcat(s, "\n");
105
                        puts(outstr);
106
                }
107 77 dgisselq
                TESTB<Vdiv>::tick();
108
        }
109
 
110
        void    divs(int n, int d) {
111 147 dgisselq
                bool    dbg = false;
112 77 dgisselq
                int     ans;
113
                ans = (d==0)?0:   (n / d);
114
                assert(m_core->o_busy == 0);
115
 
116
                m_core->i_rst = 0;
117
                m_core->i_wr = 1;
118
                m_core->i_signed = 1;
119
                m_core->i_numerator = n;
120
                m_core->i_denominator = d;
121
 
122
                tick();
123
 
124
                m_core->i_wr = 0;
125
                m_core->i_signed = 0;
126
                m_core->i_numerator = 0;
127
                m_core->i_denominator = 0;
128
 
129
                assert(m_core->o_busy);
130
                assert(m_core->o_valid == 0);
131
 
132
                // while((!m_core->o_valid)&&(!m_core->o_err))
133
                while(!m_core->o_valid)
134
                        tick();
135
 
136 147 dgisselq
                if (dbg) {
137
                        printf("%s%s: %d / %d =? %d\n",
138
                                (m_core->o_valid)?"V":" ",
139
                                (m_core->o_err)?"E":" ",
140
                                n, d, m_core->o_quotient);
141
                }
142 77 dgisselq
                if ((m_core->o_err)||(d==0)) {
143
                        if (d==0)
144
                                assert(m_core->o_err);
145
                        else    assert(!m_core->o_err);
146
                } else
147
                        assert(ans == (int)m_core->o_quotient);
148
        }
149
 
150
        void    divu(unsigned n, unsigned d) {
151 147 dgisselq
                bool    dbg = false;
152 77 dgisselq
                unsigned        ans;
153
                ans = (d==0)?0:   (n / d);
154
                assert(m_core->o_busy == 0);
155
 
156
                m_core->i_rst = 0;
157
                m_core->i_wr = 1;
158
                m_core->i_signed = 0;
159
                m_core->i_numerator = n;
160
                m_core->i_denominator = d;
161
 
162
                tick();
163
 
164
                m_core->i_wr = 0;
165
                m_core->i_signed = 0;
166
                m_core->i_numerator = 0;
167
                m_core->i_denominator = 0;
168
 
169
                assert(m_core->o_busy);
170
                assert(m_core->o_valid == 0);
171
 
172
                while(!m_core->o_valid)
173
                        tick();
174
 
175 147 dgisselq
                if (dbg) {
176
                        printf("%s%s: %u / %u =? %d (Expecting %u)\n",
177
                                (m_core->o_valid)?"V":" ",
178
                                (m_core->o_err)?"E":" ",
179
                                n, d, m_core->o_quotient, ans);
180
                }
181 77 dgisselq
                if ((m_core->o_err)||(d==0)) {
182
                        if (d==0)
183
                                assert(m_core->o_err);
184
                        else    assert(!m_core->o_err);
185
                } else
186
                        assert(ans == (unsigned)m_core->o_quotient);
187
        }
188
 
189
        void    divide(int n, int d) {
190
                divs(n,d);
191
        }
192
};
193
 
194
void    usage(void) {
195
        printf("USAGE: div_tb\n");
196
        printf("\n");
197
        printf("\t\n");
198
}
199
 
200
int     main(int argc, char **argv) {
201
        Verilated::commandArgs(argc, argv);
202
        DIV_TB  *tb = new DIV_TB();
203 147 dgisselq
        int     rcode = EXIT_SUCCESS;
204 77 dgisselq
 
205
        tb->reset();
206
        tb->divide(125,7);
207
        tb->tick();
208
        tb->divide(125,-7);
209
        tb->tick();
210
        tb->divu((1u<<31),7);
211
        tb->divu((7u<<29),(1u<<31));
212
        tb->tick();
213
        tb->divs(32768,0);
214
        tb->tick();
215
        tb->divu((1u<<31),0);
216
        tb->tick();
217
        tb->divs((1u<<30),0);
218
        tb->tick();
219
        for(int i=32767; i>=0; i--) {
220
                tb->divs((1u<<30),i);
221
                tb->tick();
222
        } for(int i=32767; i>=0; i--) {
223
                // tb->divu(-1, i);
224
                tb->divu((1u<<31), i);
225
                tb->tick();
226
        } for(int i=32767; i>=0; i--) {
227
                tb->divide(32768, i);
228
                tb->tick();
229
        }
230
        /*
231
         * While random data is a nice test idea, the following just never
232
         * really tested the divide unit thoroughly enough.
233
         *
234
        tb->divide(rand(),rand()/2);
235
        tb->tick();
236
        tb->divide(rand(),rand()/2);
237
        tb->tick();
238
        tb->divide(rand(),rand()/2);
239
        tb->tick();
240
        tb->divide(rand(),rand()/2);
241
        */
242
 
243 147 dgisselq
        printf("SUCCESS!\n");
244 77 dgisselq
        exit(rcode);
245
}
246
 

powered by: WebSVN 2.1.0

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