aboutsummaryrefslogtreecommitdiffstats
path: root/opencores/aemb/sw/c/aeMB_testbench.c
blob: c3402e0ef5db218abd19c030c00014185a667221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* $Id: aeMB_testbench.c,v 1.14 2007/12/28 21:44:04 sybreon Exp $
** 
** AEMB Function Verification C Testbench
** Copyright (C) 2004-2007 Shawn Tan Ser Ngiap <shawn.tan@aeste.net>
**
** This file is part of AEMB.
**
** AEMB is free software: you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** AEMB is distributed in the hope that it will be useful, but WITHOUT
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
** License for more details.
**
** You should have received a copy of the GNU General Public License
** along with AEMB.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <malloc.h>
#include <errno.h>
#include <reent.h>

#include "libaemb.h"

/**
   INTERRUPT TEST

   This tests for the following:
   - Pointer addressing
   - Interrupt handling
 */

void __attribute__ ((interrupt_handler)) int_handler();
volatile int service = 0xDEADDEAD;

void int_service() 
{
  int* pio = (int*)0xFFFFFFFC;
  *pio = 0x52544E49; // "INTR"
  service = 0;
}

void int_handler()
{
  int_service();
}

/**
   INTERRUPT TEST ROUTINE
*/
int int_test ()
{
  // Delay loop until hardware interrupt triggers
  volatile int i;
  for (i=0; i < 999; i++) {
    if (service == 0) return 0;
  };

  return -1;
}

/**
   FIBONACCI TEST
   http://en.literateprograms.org/Fibonacci_numbers_(C)

   This tests for the following:
   - Recursion & Iteration
   - 32/16/8-bit data handling
*/

unsigned int fib_slow(unsigned int n)
{
  return n < 2 ? n : fib_slow(n-1) + fib_slow(n-2);
}

unsigned int fib_fast(unsigned int n)
{
  unsigned int a[3];
  unsigned int *p=a;
  unsigned int i;
  
  for(i=0; i<=n; ++i) {
    if(i<2) *p=i;
    else {
      if(p==a) *p=*(a+1)+*(a+2);
      else if(p==a+1) *p=*a+*(a+2);
      else *p=*a+*(a+1);
    }
    if(++p>a+2) p=a;
  }
  
  return p==a?*(p+2):*(p-1);
}

int fib_test(int max) {
  unsigned int n;
  unsigned int fast, slow;  
  // 32-bit LUT
  unsigned int fib_lut32[] = {
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
  };  
  // 16-bit LUT
  unsigned short fib_lut16[] = {
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
  };    
  // 8-bit LUT
  unsigned char fib_lut8[] = {
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233
  };  
  
  for (n=0;n<max;n++) {
    slow = fib_slow(n);    
    fast = fib_fast(n);
    if ((slow != fast) || 
	(fast != fib_lut32[n]) || 
	(fast != fib_lut16[n]) || 
	(fast != fib_lut8[n])) {
      return -1;      
    }
  }      
  return 0;  
}

/**
   EUCLIDEAN TEST
   http://en.literateprograms.org/Euclidean_algorithm_(C)
   
   This tests for the following:
   - Modulo arithmetic
   - Goto
*/

int euclid_gcd(int a, int b) {
  if (b > a) goto b_larger;
  while (1) {
    a = a % b;
    if (a == 0) return b;
  b_larger:
    b = b % a;
    if (b == 0) return a;
  }
}

int euclid_test(int max) 
{
  int n;
  int euclid;
  // Random Numbers
  int euclid_a[] = {
    1804289383, 1681692777, 1957747793, 719885386, 596516649,
    1025202362, 783368690, 2044897763, 1365180540, 304089172,
    35005211, 294702567, 336465782, 278722862
  };  
  int euclid_b[] = {
    846930886, 1714636915, 424238335, 1649760492, 1189641421,
    1350490027, 1102520059, 1967513926, 1540383426, 1303455736,
    521595368, 1726956429, 861021530, 233665123
  };
  
  // GCD 
  int euclid_lut[] = {
    1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 1, 3, 2, 1
  };
    
  for (n=0;n<max;n++) {
    euclid = euclid_gcd(euclid_a[n],euclid_b[n]);
    if (euclid != euclid_lut[n]) {
      return -1;
    }
  }
  return 0;
}

/**
   NEWTON-RHAPSON
   http://en.literateprograms.org/Newton-Raphson's_method_for_root_finding_(C)

   This tests for the following:
   - Multiplication & Division
   - Floating point arithmetic
   - Integer to Float conversion
*/

float newton_sqrt(float n)
{
  float x = 0.0;
  float xn = 0.0;  
  int iters = 0;  
  int i;
  for (i = 0; i <= (int)n; ++i)
    {
      float val = i*i-n;
      if (val == 0.0)
	return i;
      if (val > 0.0)
	{
	  xn = (i+(i-1))/2.0;
	  break;
	}
    }  
  while (!(iters++ >= 100
	   || x == xn))
    {
      x = xn;
      xn = x - (x * x - n) / (2 * x);
    }
  return xn;
}

int newton_test (int max) {
  int n;
  float newt;
  // 32-bit LUT
  float newt_lut[] = {
    0.000000000000000000000000,
    1.000000000000000000000000,
    1.414213538169860839843750,
    1.732050776481628417968750,
    2.000000000000000000000000,
    2.236068010330200195312500,
    2.449489831924438476562500,
    2.645751237869262695312500,
    2.828427076339721679687500,
    3.000000000000000000000000,
    3.162277698516845703125000,
    3.316624879837036132812500,
    3.464101552963256835937500,
    3.605551242828369140625000,
    3.741657495498657226562500
  };

  for (n=0;n<max;n++) {
    newt = newton_sqrt(n);
    if (newt != newt_lut[n]) {
      return -1;
    }
  } 
  return 0;
}


/**
   FSL TEST ROUTINE
*/

int fsl_test ()
{
  // TEST FSL1 ONLY
  int FSL = 0xCAFEF00D;

  asm ("PUT %0, RFSL1" :: "r"(FSL));
  asm ("GET %0, RFSL1" : "=r"(FSL));
  
  if (FSL != 0x01) return -1;
  
  asm ("PUT %0, RFSL31" :: "r"(FSL));
  asm ("GET %0, RFSL31" : "=r"(FSL));
  
  if (FSL != 0x1F) return -1;
  
  return 0;  
}

// static int errnum;
/*
int *__errno ()
{
  return &_REENT->_errno;
  // return &errnum;
}
*/

int malloc_test()
{
  void *alloc;

  alloc = (void *)malloc(256); // allocate 32 bytes

  if (alloc == NULL)
    return -1;
  else
    return (int) alloc;
}

/**
   MAIN TEST PROGRAMME

   This is the main test procedure. It will output signals onto the
   MPI port that is checked by the testbench.
 */

int main () 
{
  // Message Passing Port
  int* mpi = (int*)0xFFFFFFFF;
  
  // Number of each test to run
  int max = 10;

  // lock T0 if it's multi-threaded
  /*
  if ((aemb_isthreaded() == 0) && (aemb_isthread1() != 0)) {
    while (1) {
      asm volatile ("nop;");
    }
  }
  */

  // Enable Global Interrupts
  aemb_enable_interrupt();

  // INT TEST
  //if (int_test() == -1) { *mpi = 0x4641494C; }

  // TEST MALLOC
  if (malloc_test() == -1) { *mpi = 0x4641494C; }

  // FSL TEST
  //if (fsl_test() == -1) { *mpi = 0x4641494C; }

  // Fibonacci Test
  if (fib_test(max) == -1) { *mpi = 0x4641494C; }

  // Euclid Test
  if (euclid_test(max) == -1) { *mpi = 0x4641494C; }

  // Newton-Rhapson Test
  if (newton_test(max) == -1) { *mpi = 0x4641494C; }
  
  // Disable Global Interrupts
  aemb_disable_interrupt();

  // ALL PASSED
  return 0;
}

/*
  HISTORY
  $Log: aeMB_testbench.c,v $
  Revision 1.14  2007/12/28 21:44:04  sybreon
  Added malloc() test

  Revision 1.13  2007/12/11 00:44:31  sybreon
  Modified for AEMB2
  
  Revision 1.12  2007/11/18 19:41:45  sybreon
  Minor simulation fixes.
  
  Revision 1.11  2007/11/14 23:41:06  sybreon
  Fixed minor interrupt test typo.
  
  Revision 1.10  2007/11/14 22:12:02  sybreon
  Added interrupt test routine.
  
  Revision 1.9  2007/11/09 20:51:53  sybreon
  Added GET/PUT support through a FSL bus.
  
  Revision 1.8  2007/11/03 08:40:18  sybreon
  Minor code cleanup.
  
  Revision 1.7  2007/11/02 18:32:19  sybreon
  Enable MSR_IE with software.
  
  Revision 1.6  2007/04/30 15:57:10  sybreon
  Removed byte acrobatics.
  
  Revision 1.5  2007/04/27 15:17:59  sybreon
  Added code documentation.
  Added new tests that test floating point, modulo arithmetic and multiplication/division.
  
  Revision 1.4  2007/04/25 22:15:05  sybreon
  Added support for 8-bit and 16-bit data types.
  
  Revision 1.3  2007/04/04 14:09:04  sybreon
  Added initial interrupt/exception support.
  
  Revision 1.2  2007/04/04 06:07:45  sybreon
  Fixed C code bug which passes the test
  
  Revision 1.1  2007/03/09 17:41:57  sybreon
  initial import  
*/