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

Subversion Repositories aemb

[/] [aemb/] [trunk/] [sw/] [c/] [aeMB_testbench.c] - Blame information for rev 97

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

Line No. Rev Author Line
1 97 sybreon
/* $Id: aeMB_testbench.c,v 1.14 2007-12-28 21:44:04 sybreon Exp $
2
**
3
** AEMB Function Verification C Testbench
4
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap <shawn.tan@aeste.net>
5
**
6
** This file is part of AEMB.
7
**
8
** AEMB is free software: you can redistribute it and/or modify it
9
** under the terms of the GNU General Public License as published by
10
** the Free Software Foundation, either version 3 of the License, or
11
** (at your option) any later version.
12
**
13
** AEMB is distributed in the hope that it will be useful, but WITHOUT
14
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
** License for more details.
17
**
18
** You should have received a copy of the GNU General Public License
19
** along with AEMB.  If not, see <http://www.gnu.org/licenses/>.
20
*/
21 2 sybreon
 
22 97 sybreon
#include <malloc.h>
23
#include <errno.h>
24
#include <reent.h>
25
 
26 79 sybreon
#include "libaemb.h"
27
 
28 29 sybreon
/**
29
   INTERRUPT TEST
30
 
31
   This tests for the following:
32
   - Pointer addressing
33
   - Interrupt handling
34
 */
35 67 sybreon
 
36 79 sybreon
void __attribute__ ((interrupt_handler)) int_handler();
37 67 sybreon
volatile int service = 0xDEADDEAD;
38
 
39 46 sybreon
void int_service()
40
{
41
  int* pio = (int*)0xFFFFFFFC;
42 29 sybreon
  *pio = 0x52544E49; // "INTR"
43 67 sybreon
  service = 0;
44 6 sybreon
}
45 2 sybreon
 
46 46 sybreon
void int_handler()
47
{
48
  int_service();
49
}
50
 
51 60 sybreon
/**
52
   INTERRUPT TEST ROUTINE
53
*/
54
int int_test ()
55
{
56
  // Delay loop until hardware interrupt triggers
57 97 sybreon
  volatile int i;
58
  for (i=0; i < 999; i++) {
59 67 sybreon
    if (service == 0) return 0;
60
  };
61
 
62
  return -1;
63 60 sybreon
}
64 46 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 53 sybreon
 
245 29 sybreon
/**
246 53 sybreon
   FSL TEST ROUTINE
247
*/
248
 
249
int fsl_test ()
250
{
251
  // TEST FSL1 ONLY
252
  int FSL = 0xCAFEF00D;
253
 
254
  asm ("PUT %0, RFSL1" :: "r"(FSL));
255
  asm ("GET %0, RFSL1" : "=r"(FSL));
256
 
257 67 sybreon
  if (FSL != 0x01) return -1;
258 53 sybreon
 
259
  asm ("PUT %0, RFSL31" :: "r"(FSL));
260
  asm ("GET %0, RFSL31" : "=r"(FSL));
261
 
262 67 sybreon
  if (FSL != 0x1F) return -1;
263 53 sybreon
 
264
  return 0;
265
}
266
 
267 97 sybreon
// static int errnum;
268
/*
269
int *__errno ()
270
{
271
  return &_REENT->_errno;
272
  // return &errnum;
273
}
274
*/
275
 
276
int malloc_test()
277
{
278
  void *alloc;
279
 
280
  alloc = (void *)malloc(256); // allocate 32 bytes
281
 
282
  if (alloc == NULL)
283
    return -1;
284
  else
285
    return (int) alloc;
286
}
287
 
288 53 sybreon
/**
289 29 sybreon
   MAIN TEST PROGRAMME
290
 
291
   This is the main test procedure. It will output signals onto the
292
   MPI port that is checked by the testbench.
293
 */
294
 
295
int main ()
296
{
297
  // Message Passing Port
298
  int* mpi = (int*)0xFFFFFFFF;
299 42 sybreon
 
300 29 sybreon
  // Number of each test to run
301 46 sybreon
  int max = 10;
302 29 sybreon
 
303 97 sybreon
  // lock T0 if it's multi-threaded
304
  /*
305
  if ((aemb_isthreaded() == 0) && (aemb_isthread1() != 0)) {
306
    while (1) {
307
      asm volatile ("nop;");
308
    }
309
  }
310
  */
311
 
312 60 sybreon
  // Enable Global Interrupts
313 79 sybreon
  aemb_enable_interrupt();
314 60 sybreon
 
315
  // INT TEST
316 79 sybreon
  //if (int_test() == -1) { *mpi = 0x4641494C; }
317 60 sybreon
 
318 97 sybreon
  // TEST MALLOC
319
  if (malloc_test() == -1) { *mpi = 0x4641494C; }
320
 
321 53 sybreon
  // FSL TEST
322 79 sybreon
  //if (fsl_test() == -1) { *mpi = 0x4641494C; }
323 53 sybreon
 
324 29 sybreon
  // Fibonacci Test
325 31 sybreon
  if (fib_test(max) == -1) { *mpi = 0x4641494C; }
326 29 sybreon
 
327
  // Euclid Test
328 31 sybreon
  if (euclid_test(max) == -1) { *mpi = 0x4641494C; }
329 29 sybreon
 
330
  // Newton-Rhapson Test
331 31 sybreon
  if (newton_test(max) == -1) { *mpi = 0x4641494C; }
332 29 sybreon
 
333 42 sybreon
  // Disable Global Interrupts
334 79 sybreon
  aemb_disable_interrupt();
335
 
336 29 sybreon
  // ALL PASSED
337
  return 0;
338
}
339 97 sybreon
 
340
/*
341
  HISTORY
342
  $Log: not supported by cvs2svn $
343
  Revision 1.13  2007/12/11 00:44:31  sybreon
344
  Modified for AEMB2
345
 
346
  Revision 1.12  2007/11/18 19:41:45  sybreon
347
  Minor simulation fixes.
348
 
349
  Revision 1.11  2007/11/14 23:41:06  sybreon
350
  Fixed minor interrupt test typo.
351
 
352
  Revision 1.10  2007/11/14 22:12:02  sybreon
353
  Added interrupt test routine.
354
 
355
  Revision 1.9  2007/11/09 20:51:53  sybreon
356
  Added GET/PUT support through a FSL bus.
357
 
358
  Revision 1.8  2007/11/03 08:40:18  sybreon
359
  Minor code cleanup.
360
 
361
  Revision 1.7  2007/11/02 18:32:19  sybreon
362
  Enable MSR_IE with software.
363
 
364
  Revision 1.6  2007/04/30 15:57:10  sybreon
365
  Removed byte acrobatics.
366
 
367
  Revision 1.5  2007/04/27 15:17:59  sybreon
368
  Added code documentation.
369
  Added new tests that test floating point, modulo arithmetic and multiplication/division.
370
 
371
  Revision 1.4  2007/04/25 22:15:05  sybreon
372
  Added support for 8-bit and 16-bit data types.
373
 
374
  Revision 1.3  2007/04/04 14:09:04  sybreon
375
  Added initial interrupt/exception support.
376
 
377
  Revision 1.2  2007/04/04 06:07:45  sybreon
378
  Fixed C code bug which passes the test
379
 
380
  Revision 1.1  2007/03/09 17:41:57  sybreon
381
  initial import
382
*/

powered by: WebSVN 2.1.0

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