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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sim/] [verilator/] [div_tb.cpp] - Blame information for rev 204

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

Line No. Rev Author Line
1 204 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 <stdint.h>
36
#include <time.h>
37
#include <unistd.h>
38
#include <assert.h>
39
 
40
#include <ctype.h>
41
 
42
#include "verilated.h"
43
#include "Vdiv.h"
44
 
45
#include "testb.h"
46
// #include "twoc.h"
47
 
48
class   DIV_TB : public TESTB<Vdiv> {
49
public:
50
        DIV_TB(void) {
51
        }
52
 
53
        ~DIV_TB(void) {}
54
 
55
        void    reset(void) {
56
                // m_flash.debug(false);
57
                TESTB<Vdiv>::reset();
58
        }
59
 
60
        bool    on_tick(void) {
61
                tick();
62
                return true;
63
        }
64
 
65
        void    bprint(char *str, int nbits, unsigned long v) {
66
                while(*str)
67
                        str++;
68
                for(int i=0; i<nbits; i++) {
69
                        if ((1l<<(nbits-1-i))&v)
70
                                *str++ = '1';
71
                        else
72
                                *str++ = '0';
73
                        if (((nbits-1-i)&3)==0)
74
                                *str++ = ' ';
75
                } *str = '\0';
76
        }
77
 
78
        void    dbgdump(void) {
79
                char    outstr[2048], *s;
80
                sprintf(outstr, "Tick %4ld %s%s%s%s%s%s%s %2d(%s= 0)",
81
                        m_tickcount,
82
                        (m_core->o_busy)?"B":" ",
83
                        (m_core->v__DOT__r_busy)?"R":" ",
84
                        (m_core->o_valid)?"V":" ",
85
                        (m_core->i_wr)?"W":" ",
86
                        (m_core->v__DOT__pre_sign)?"+":" ",
87
                        (m_core->v__DOT__r_sign)?"-":" ",
88
                        (m_core->v__DOT__r_z)?"Z":" ",
89
                        m_core->v__DOT__r_bit,
90
                        (m_core->v__DOT__last_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
 
108
        void    tick(void) {
109
                bool    debug = false;
110
 
111
                if (debug)
112
                        dbgdump();
113
                TESTB<Vdiv>::tick();
114
        }
115
 
116
        void    divtest(uint32_t n, uint32_t d, uint32_t ans, bool issigned) {
117
                const bool      dbg = false;
118
 
119
                // The test bench is supposed to assert that we are idle when
120
                // we come in here.
121
                assert(m_core->o_busy == 0);
122
 
123
                // Request a divide
124
                m_core->i_rst = 0;
125
                m_core->i_wr = 1;
126
                m_core->i_signed = (issigned)?1:0;
127
                m_core->i_numerator = n;
128
                m_core->i_denominator = d;
129
 
130
                // Tick once for the request to be registered
131
                tick();
132
 
133
                // Clear the input lines.
134
                m_core->i_wr = 0;
135
                m_core->i_signed = 0;
136
                m_core->i_numerator = 0;
137
                m_core->i_denominator = 0;
138
 
139
                // Make certain busy is immediately true upon the first clock
140
                // after we issue the divide, and that our result is not also
141
                // listed as a valid result.
142
                if (!m_core->o_busy) {
143
                        closetrace();
144
                        assert(m_core->o_busy);
145
                } if (m_core->o_valid != 0) {
146
                        closetrace();
147
                        assert(m_core->o_valid == 0);
148
                }
149
 
150
                // while((!m_core->o_valid)&&(!m_core->o_err))
151
                while(!m_core->o_valid) {
152
                        // If we aren't yet valid, we'd better at least
153
                        // be busy--the CPU requires this.
154
                        if (!m_core->o_busy) {
155
                                // We aren't valid, and we aren't busy.  This
156
                                // is a test failure.
157
                                dbgdump();
158
                                closetrace();
159
                                assert(m_core->o_busy);
160
                        }
161
 
162
                        // Let the algorithm work for another clock tick.
163
                        tick();
164
                } if (dbg) dbgdump();
165
 
166
                // Insist that the core not be busy any more, now that a valid
167
                // result has been produced.
168
                if (m_core->o_busy) {
169
                        closetrace();
170
                        assert(!m_core->o_busy);
171
                }
172
 
173
                if (dbg) {
174
                        printf("%s%s: %d / %d =? %d\n",
175
                                (m_core->o_valid)?"V":" ",
176
                                (m_core->o_err)?"E":" ",
177
                                n, d, m_core->o_quotient);
178
                }
179
 
180
 
181
                // Now that we're done, we need to check the result.
182
                //
183
                // First case to check: was there an error condition or, if not,
184
                // should there have been one?
185
                if (d == 0) {
186
                        // We attempted to divide by zero, the result should've
187
                        // been an error condition.  Let's check:
188
                        // Then insist on a division by zero error
189
                        if (!m_core->o_err) {
190
                                // Don't forget to close the trace before the
191
                                // assert, lest the file not get the final
192
                                // values into it.
193
                                closetrace();
194
                                assert(m_core->o_err);
195
                        }
196
                } else if (m_core->o_err) {
197
                        // Otherwise, there should not have been any divide
198
                        // errors.  The only errors allowed should be the
199
                        // divide by zero.  So, this is an error.  Let's
200
                        // stop and report it.
201
                        closetrace();
202
                        assert(!m_core->o_err);
203
                } else if (ans != (uint32_t)m_core->o_quotient) {
204
                        // The other problem we might encounter would be if the
205
                        // result doesn't match the one we are expecting.
206
                        //
207
                        // Stop on this bug as well.
208
                        //
209
                        closetrace();
210
                        assert(ans == (uint32_t)m_core->o_quotient);
211
                }
212
        }
213
 
214
        // Test a signed divide
215
        void    divs(int n, int d) {
216
                int     ans;
217
                // Calculate the answer we *should* get from the divide
218
                ans = (d==0)?0:   (n / d);
219
 
220
                divtest((uint32_t)n, (uint32_t)d, (uint32_t)ans, true);
221
        }
222
 
223
        // Test an unsigned divide
224
        void    divu(unsigned n, unsigned d) {
225
                unsigned        ans;
226
 
227
                // Pre-Calculate the answer we *should* get from the divide
228
                ans = (d==0)?0:   (n / d);
229
 
230
                divtest((uint32_t)n, (uint32_t)d, (uint32_t)ans, false);
231
        }
232
 
233
        // divide() is just another name for a signed divide--just switch to
234
        // that function call instead.
235
        void    divide(int n, int d) {
236
                divs(n,d);
237
        }
238
};
239
 
240
//
241
// Standard usage functions.
242
//
243
// Notice that the test bench provides no options.  Everything is
244
// self-contained.
245
void    usage(void) {
246
        printf("USAGE: div_tb\n");
247
        printf("\n");
248
        printf("\t\n");
249
}
250
 
251
//
252
int     main(int argc, char **argv) {
253
        // Setup
254
        Verilated::commandArgs(argc, argv);
255
        DIV_TB  *tb = new DIV_TB();
256
 
257
        // tb->opentrace("divtrace.vcd");
258
 
259
        tb->reset();
260
        // tb->opentrace("div_tb.vcd");
261
 
262
        // Now we're ready.  All we need to do to test the divide of two
263
        // numbers is to call the respective divide(), divs(), or divu()
264
        // functions.  The program will crash on an assert error if anything
265
        // goes wrong.
266
        tb->divu((unsigned)-1,10);
267
        tb->divide(125,7);
268
        // And give us an extra clock tick in-between each test for good
269
        // measure.
270
        tb->tick();
271
 
272
        // Some other gentle tests
273
        tb->divide(125,-7);
274
        tb->tick();
275
        tb->divu((1u<<31),7);
276
        // Now some boundary conditions
277
        tb->divu((7u<<29),(1u<<31));
278
        tb->tick();
279
        tb->divs(32768,0);
280
        tb->tick();
281
        tb->divu((1u<<31),0);
282
        tb->tick();
283
        tb->divs((1u<<30),0);
284
        tb->tick();
285
        //
286
        // Now we switch to a more thorough test set.  It's not complete, just
287
        // ... more thorough.
288
        for(int i=32767; i>=0; i--) {
289
                tb->divs((1u<<30),i);
290
                tb->tick();
291
        } for(int i=32767; i>=0; i--) {
292
                // tb->divu(-1, i);
293
                tb->divu((1u<<31), i);
294
                tb->tick();
295
        } for(int i=32767; i>=0; i--) {
296
                tb->divide(32768, i);
297
                tb->tick();
298
        }
299
 
300
        /*
301
         * While random data is a nice test idea, the following just never
302
         * really tested the divide unit thoroughly enough.
303
         *
304
        tb->divide(rand(),rand()/2);
305
        tb->tick();
306
        tb->divide(rand(),rand()/2);
307
        tb->tick();
308
        tb->divide(rand(),rand()/2);
309
        tb->tick();
310
        tb->divide(rand(),rand()/2);
311
        */
312
 
313
        // Any failures above will be captured with a failed assert.  If we
314
        // get here, it means things worked.  Close up shop ...
315
        //
316
        // This closes any potential trace file
317
        delete  tb;
318
 
319
        // And declare success
320
        printf("SUCCESS!\n");
321
        exit(EXIT_SUCCESS);
322
}
323
 

powered by: WebSVN 2.1.0

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