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

Subversion Repositories zipcpu

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

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
                char    outstr[2048], *s;
79
                sprintf(outstr, "Tick %4ld %s%s%s%s%s%s %2d", m_tickcount,
80
                        (m_core->o_busy)?"B":" ",
81
                        (m_core->o_valid)?"V":" ",
82
                        (m_core->i_wr)?"W":" ",
83
                        (m_core->v__DOT__pre_sign)?"+":" ",
84
                        (m_core->v__DOT__r_sign)?"-":" ",
85
                        (m_core->v__DOT__r_z)?"Z":" ",
86
                        m_core->v__DOT__r_bit); s = &outstr[strlen(outstr)];
87
                sprintf(s, "%s\n%10s %40s",s, "Div",""); s = &s[strlen(s)];
88
                bprint( s, 32, m_core->v__DOT__r_dividend);  s=&s[strlen(s)];
89
                sprintf(s, "%s\n%10s ",s, "Div"); s = &s[strlen(s)];
90
                bprint( s, 64, m_core->v__DOT__r_divisor); s=&s[strlen(s)];
91
                sprintf(s, "%s\n%10s %40s",s, "Q",""); s=&s[strlen(s)];
92
                bprint( s, 32, m_core->o_quotient); s = &s[strlen(s)];
93
                sprintf(s, "%s\n%10s %38s",s, "Diff",""); s=&s[strlen(s)];
94
                bprint( s, 33, m_core->v__DOT__diff); s = &s[strlen(s)];
95
                strcat(s, "\n");
96
                puts(outstr);
97
                TESTB<Vdiv>::tick();
98
        }
99
 
100
        void    divs(int n, int d) {
101
                int     ans;
102
                ans = (d==0)?0:   (n / d);
103
                assert(m_core->o_busy == 0);
104
 
105
                m_core->i_rst = 0;
106
                m_core->i_wr = 1;
107
                m_core->i_signed = 1;
108
                m_core->i_numerator = n;
109
                m_core->i_denominator = d;
110
 
111
                tick();
112
 
113
                m_core->i_wr = 0;
114
                m_core->i_signed = 0;
115
                m_core->i_numerator = 0;
116
                m_core->i_denominator = 0;
117
 
118
                assert(m_core->o_busy);
119
                assert(m_core->o_valid == 0);
120
 
121
                // while((!m_core->o_valid)&&(!m_core->o_err))
122
                while(!m_core->o_valid)
123
                        tick();
124
 
125
                printf("%s%s: %d / %d =? %d\n",
126
                        (m_core->o_valid)?"V":" ",
127
                        (m_core->o_err)?"E":" ",
128
                        n, d, m_core->o_quotient);
129
                if ((m_core->o_err)||(d==0)) {
130
                        if (d==0)
131
                                assert(m_core->o_err);
132
                        else    assert(!m_core->o_err);
133
                } else
134
                        assert(ans == (int)m_core->o_quotient);
135
        }
136
 
137
        void    divu(unsigned n, unsigned d) {
138
                unsigned        ans;
139
                ans = (d==0)?0:   (n / d);
140
                assert(m_core->o_busy == 0);
141
 
142
                m_core->i_rst = 0;
143
                m_core->i_wr = 1;
144
                m_core->i_signed = 0;
145
                m_core->i_numerator = n;
146
                m_core->i_denominator = d;
147
 
148
                tick();
149
 
150
                m_core->i_wr = 0;
151
                m_core->i_signed = 0;
152
                m_core->i_numerator = 0;
153
                m_core->i_denominator = 0;
154
 
155
                assert(m_core->o_busy);
156
                assert(m_core->o_valid == 0);
157
 
158
                while(!m_core->o_valid)
159
                        tick();
160
 
161
                printf("%s%s: %u / %u =? %d (Expecting %u)\n",
162
                        (m_core->o_valid)?"V":" ",
163
                        (m_core->o_err)?"E":" ",
164
                        n, d, m_core->o_quotient, ans);
165
                if ((m_core->o_err)||(d==0)) {
166
                        if (d==0)
167
                                assert(m_core->o_err);
168
                        else    assert(!m_core->o_err);
169
                } else
170
                        assert(ans == (unsigned)m_core->o_quotient);
171
        }
172
 
173
        void    divide(int n, int d) {
174
                divs(n,d);
175
        }
176
};
177
 
178
void    usage(void) {
179
        printf("USAGE: div_tb\n");
180
        printf("\n");
181
        printf("\t\n");
182
}
183
 
184
int     main(int argc, char **argv) {
185
        Verilated::commandArgs(argc, argv);
186
        DIV_TB  *tb = new DIV_TB();
187
        int     rcode = 0;
188
 
189
        tb->reset();
190
        tb->divide(125,7);
191
        tb->tick();
192
        tb->divide(125,-7);
193
        tb->tick();
194
        tb->divu((1u<<31),7);
195
        tb->divu((7u<<29),(1u<<31));
196
        tb->tick();
197
        tb->divs(32768,0);
198
        tb->tick();
199
        tb->divu((1u<<31),0);
200
        tb->tick();
201
        tb->divs((1u<<30),0);
202
        tb->tick();
203
        for(int i=32767; i>=0; i--) {
204
                tb->divs((1u<<30),i);
205
                tb->tick();
206
        } for(int i=32767; i>=0; i--) {
207
                // tb->divu(-1, i);
208
                tb->divu((1u<<31), i);
209
                tb->tick();
210
        } for(int i=32767; i>=0; i--) {
211
                tb->divide(32768, i);
212
                tb->tick();
213
        }
214
        /*
215
         * While random data is a nice test idea, the following just never
216
         * really tested the divide unit thoroughly enough.
217
         *
218
        tb->divide(rand(),rand()/2);
219
        tb->tick();
220
        tb->divide(rand(),rand()/2);
221
        tb->tick();
222
        tb->divide(rand(),rand()/2);
223
        tb->tick();
224
        tb->divide(rand(),rand()/2);
225
        */
226
 
227
        exit(rcode);
228
}
229
 

powered by: WebSVN 2.1.0

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