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

Subversion Repositories aemb

[/] [aemb/] [branches/] [AEMB2_712/] [sw/] [c/] [aeMB_testbench.c] - Blame information for rev 31

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

Line No. Rev Author Line
1 2 sybreon
/*
2 31 sybreon
 * $Id: aeMB_testbench.c,v 1.6 2007-04-30 15:57:10 sybreon Exp $
3 2 sybreon
 *
4 6 sybreon
 * AEMB Function Verification C Testbench
5 29 sybreon
 * Copyright (C) 2004-2007 Shawn Tan Ser Ngiap <shawn.tan@aeste.net>
6 2 sybreon
 *
7 29 sybreon
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public License
9
 * as published by the Free Software Foundation; either version 2.1 of
10
 * the License, or (at your option) any later version.
11 2 sybreon
 *
12 29 sybreon
 * This library is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 * Lesser General Public License for more details.
16 2 sybreon
 *
17 29 sybreon
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
 * USA
21 2 sybreon
 *
22 29 sybreon
 * DESCRIPTION
23
 * It tests a whole gamut of operations and is tightly linked to the
24
 * ae68_testbench.v testbench module for verification.
25 2 sybreon
 *
26
 * HISTORY
27
 * $Log: not supported by cvs2svn $
28 31 sybreon
 * Revision 1.5  2007/04/27 15:17:59  sybreon
29
 * Added code documentation.
30
 * Added new tests that test floating point, modulo arithmetic and multiplication/division.
31
 *
32 29 sybreon
 * Revision 1.4  2007/04/25 22:15:05  sybreon
33
 * Added support for 8-bit and 16-bit data types.
34
 *
35 22 sybreon
 * Revision 1.3  2007/04/04 14:09:04  sybreon
36
 * Added initial interrupt/exception support.
37
 *
38 14 sybreon
 * Revision 1.2  2007/04/04 06:07:45  sybreon
39
 * Fixed C code bug which passes the test
40
 *
41 6 sybreon
 * Revision 1.1  2007/03/09 17:41:57  sybreon
42
 * initial import
43
 *
44 2 sybreon
 */
45
 
46 29 sybreon
/**
47
   INTERRUPT TEST
48
 
49
   This tests for the following:
50
   - Pointer addressing
51
   - Interrupt handling
52
 */
53 14 sybreon
void int_call_func (); // __attribute__((save_volatiles));
54 6 sybreon
void int_handler_func () __attribute__ ((interrupt_handler));
55 2 sybreon
 
56 6 sybreon
void int_handler_func () {
57
  int_call_func();
58
}
59 2 sybreon
 
60 6 sybreon
void int_call_func () {
61 29 sybreon
  int* pio = (int*)0xFFFFFFFF;
62
  *pio = 0x52544E49; // "INTR"
63 6 sybreon
}
64 2 sybreon
 
65 29 sybreon
/**
66
   FIBONACCI TEST
67
   http://en.literateprograms.org/Fibonacci_numbers_(C)
68 2 sybreon
 
69 29 sybreon
   This tests for the following:
70
   - Recursion & Iteration
71
   - 32/16/8-bit data handling
72
*/
73
 
74
unsigned int fib_slow(unsigned int n)
75 6 sybreon
{
76 29 sybreon
  return n < 2 ? n : fib_slow(n-1) + fib_slow(n-2);
77 6 sybreon
}
78 2 sybreon
 
79 29 sybreon
unsigned int fib_fast(unsigned int n)
80 6 sybreon
{
81
  unsigned int a[3];
82
  unsigned int *p=a;
83
  unsigned int i;
84
 
85
  for(i=0; i<=n; ++i) {
86
    if(i<2) *p=i;
87
    else {
88
      if(p==a) *p=*(a+1)+*(a+2);
89
      else if(p==a+1) *p=*a+*(a+2);
90
      else *p=*a+*(a+1);
91
    }
92
    if(++p>a+2) p=a;
93 2 sybreon
  }
94 6 sybreon
 
95
  return p==a?*(p+2):*(p-1);
96 2 sybreon
}
97
 
98 29 sybreon
int fib_test(int max) {
99 6 sybreon
  unsigned int n;
100 29 sybreon
  unsigned int fast, slow;
101
  // 32-bit LUT
102
  unsigned int fib_lut32[] = {
103
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
104
  };
105
  // 16-bit LUT
106
  unsigned short fib_lut16[] = {
107
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
108
  };
109
  // 8-bit LUT
110
  unsigned char fib_lut8[] = {
111
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
112
  };
113 6 sybreon
 
114 29 sybreon
  for (n=0;n<max;n++) {
115
    slow = fib_slow(n);
116
    fast = fib_fast(n);
117
    if ((slow != fast) ||
118
        (fast != fib_lut32[n]) ||
119
        (fast != fib_lut16[n]) ||
120
        (fast != fib_lut8[n])) {
121
      return -1;
122 6 sybreon
    }
123 22 sybreon
  }
124
  return 0;
125 6 sybreon
}
126 22 sybreon
 
127 29 sybreon
/**
128
   EUCLIDEAN TEST
129
   http://en.literateprograms.org/Euclidean_algorithm_(C)
130
 
131
   This tests for the following:
132
   - Modulo arithmetic
133
   - Goto
134
*/
135
 
136
int euclid_gcd(int a, int b) {
137
  if (b > a) goto b_larger;
138
  while (1) {
139
    a = a % b;
140
    if (a == 0) return b;
141
  b_larger:
142
    b = b % a;
143
    if (b == 0) return a;
144
  }
145
}
146
 
147
int euclid_test(int max)
148
{
149
  int n;
150
  int euclid;
151
  // Random Numbers
152
  int euclid_a[] = {
153
    1804289383, 1681692777, 1957747793, 719885386, 596516649,
154
    1025202362, 783368690, 2044897763, 1365180540, 304089172,
155
    35005211, 294702567, 336465782, 278722862
156
  };
157
  int euclid_b[] = {
158
    846930886, 1714636915, 424238335, 1649760492, 1189641421,
159
    1350490027, 1102520059, 1967513926, 1540383426, 1303455736,
160
    521595368, 1726956429, 861021530, 233665123
161
  };
162
 
163
  // GCD 
164
  int euclid_lut[] = {
165
    1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 1, 3, 2, 1
166
  };
167
 
168
  for (n=0;n<max;n++) {
169
    euclid = euclid_gcd(euclid_a[n],euclid_b[n]);
170
    if (euclid != euclid_lut[n]) {
171
      return -1;
172
    }
173
  }
174
  return 0;
175
}
176
 
177
/**
178
   NEWTON-RHAPSON
179
   http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)
180
 
181
   This tests for the following:
182
   - Multiplication & Division
183
   - Floating point arithmetic
184
   - Integer to Float conversion
185
*/
186
 
187
float newton_sqrt(float n)
188
{
189
  float x = 0.0;
190
  float xn = 0.0;
191
  int iters = 0;
192
  int i;
193
  for (i = 0; i <= (int)n; ++i)
194
    {
195
      float val = i*i-n;
196
      if (val == 0.0)
197
        return i;
198
      if (val > 0.0)
199
        {
200
          xn = (i+(i-1))/2.0;
201
          break;
202
        }
203
    }
204
  while (!(iters++ >= 100
205
           || x == xn))
206
    {
207
      x = xn;
208
      xn = x - (x * x - n) / (2 * x);
209
    }
210
  return xn;
211
}
212
 
213
int newton_test (int max) {
214
  int n;
215
  float newt;
216
  // 32-bit LUT
217
  float newt_lut[] = {
218
    0.000000000000000000000000,
219
    1.000000000000000000000000,
220
    1.414213538169860839843750,
221
    1.732050776481628417968750,
222
    2.000000000000000000000000,
223
    2.236068010330200195312500,
224
    2.449489831924438476562500,
225
    2.645751237869262695312500,
226
    2.828427076339721679687500,
227
    3.000000000000000000000000,
228
    3.162277698516845703125000,
229
    3.316624879837036132812500,
230
    3.464101552963256835937500,
231
    3.605551242828369140625000,
232
    3.741657495498657226562500
233
  };
234
 
235
  for (n=0;n<max;n++) {
236
    newt = newton_sqrt(n);
237
    if (newt != newt_lut[n]) {
238
      return -1;
239
    }
240
  }
241
  return 0;
242
}
243
 
244
/**
245
   MAIN TEST PROGRAMME
246
 
247
   This is the main test procedure. It will output signals onto the
248
   MPI port that is checked by the testbench.
249
 */
250
 
251
int main ()
252
{
253
  // Message Passing Port
254
  int* mpi = (int*)0xFFFFFFFF;
255
 
256
  // Number of each test to run
257 31 sybreon
  int max = 3;
258 29 sybreon
 
259
  // Fibonacci Test
260 31 sybreon
  if (fib_test(max) == -1) { *mpi = 0x4641494C; }
261 29 sybreon
 
262
  // Euclid Test
263 31 sybreon
  if (euclid_test(max) == -1) { *mpi = 0x4641494C; }
264 29 sybreon
 
265
  // Newton-Rhapson Test
266 31 sybreon
  if (newton_test(max) == -1) { *mpi = 0x4641494C; }
267 29 sybreon
 
268
  // ALL PASSED
269
  return 0;
270
}

powered by: WebSVN 2.1.0

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