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

Subversion Repositories mblite

[/] [mblite/] [trunk/] [sw/] [testbench/] [testbench.c] - Blame information for rev 5

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

Line No. Rev Author Line
1 2 takar
/* THIS IS THE OFFICIAL MB-LITE TESTBENCH */
2
 
3
/* This file should be modified with care. It is designed
4
   against the design in the directory mblite/design/core. */
5
 
6
/* Code coverage was measure by using modelsim coverage tools.
7
   All statements have been executed at least once. The
8
   correct console output is listed in the file
9
   result.txt. */
10
 
11
#include "stdio.h"
12
#include <stdio.h>
13
#include <malloc.h>
14
#include <math.h>
15
 
16
#include "dhry.h"
17
 
18
int interrupt = 1;
19
 
20
void test_passed()
21
{
22
    xil_printf("OK!\n\n");
23
}
24
 
25
void test_failed()
26
{
27
    xil_printf("FAILED!\n\n");
28
}
29
 
30
void __attribute__ ((interrupt_handler)) interruptHandler()
31
{
32
    xil_printf("Handling interrupt routine\n");
33
    interrupt = 0;
34
}
35
 
36
int fib1(int n)
37
{
38
    /* WARNING! Using n > 10 requires huge stack and might cause problems! */
39
 
40
    switch (n) {
41
        case 0:
42
            return 0;
43
        case 1:
44
            return 1;
45
        default:
46
            return fib1(n-1) + fib1(n-2);
47
    }
48
}
49
 
50
int fib2(int n)
51
{
52
    int a[3];
53
    int *p=a;
54
    int i;
55
 
56
    for(i=0; i<=n; ++i)
57
    {
58
        if(i<2)
59
            *p=i;
60
        else
61
        {
62
            if(p==a)
63
                *p=*(a+1)+*(a+2);
64
            else if(p==a+1)
65
                *p=*a+*(a+2);
66
            else
67
                *p=*a+*(a+1);
68
        }
69
        if(++p>a+2)
70
            p=a;
71
    }
72
 
73
    return p==a?*(p+2):*(p-1);
74
}
75
 
76
int gcd(int a, int b)
77
{
78
    if (b > a)
79
        goto b_larger;
80
    while (1)
81
    {
82
        a = a % b;
83
        if (a == 0) return b;
84
    b_larger:
85
        b = b % a;
86
        if (b == 0) return a;
87
    }
88
}
89
 
90
int memoryTest(int size)
91
{
92
    volatile void *alloc;
93
    int magic;
94
 
95
    alloc = malloc(size * sizeof(int)); /* allocate 32 byte */
96
    if (alloc == NULL)
97
        return 0;
98
 
99
    *(int *)alloc = 577; /* write to memory */
100
    magic = *(int *)alloc; /* read from memory */
101
 
102
    return magic;
103
}
104
 
105
int uncommonInstructions()
106
{
107
    int mtb = 1234567;
108
    int a = 1;
109
    int b = 4;
110
 
111
    /* jump to PC + mtb if a == 1; */
112
    __asm__ volatile ("beq %0, %1;"::"r"(a),"r"(mtb));
113
 
114
    mtb = 241;
115
 
116
    __asm__ volatile ("bsra %0, %1, %2":"=r"(mtb):"r"(mtb), "r"(b));
117
    if(mtb != 15)
118
    {
119
        return 1;
120
    }
121
    __asm__ volatile ("andn %0, %1, %2":"=r"(mtb):"r"(mtb), "r"(a));
122
    if(mtb != 14)
123
    {
124
        return 2;
125
    }
126
    mtb = 241;
127
    __asm__ volatile ("andni %0, %1, 192":"=r"(mtb):"r"(mtb));
128
    if(mtb != 49)
129
    {
130
        return 3;
131
    }
132
    mtb = 241;
133
    __asm__ volatile ("muli %0, %1, 241":"=r"(mtb):"r"(mtb));
134
    if(mtb != 58081)
135
    {
136
        return 4;
137
    }
138
    __asm__ volatile ("src %0, %1;":"=r"(mtb):"r"(mtb));
139
    if(mtb != 29040)
140
    {
141
        return 5;
142
    }
143
    return 0;
144
}
145
 
146
int main()
147
{
148
 
149
    int a, b;
150
    float f;
151
 
152
    xil_printf("Welcome to the MB-Lite Testbench\n\n");
153
    xil_printf("1. Testing Interrupt...\n");
154
    while(interrupt){}
155
    test_passed();
156
 
157
    xil_printf("2. Testing Integer Arithmetic\n");
158
    a = fib1(8);
159
    b = fib2(8);
160
    if(a == b && gcd(1365180540, 1540383426) == 6)
161
    {
162
         test_passed();
163
    }
164
    else
165
    {
166
         test_failed();
167
         return 0;
168
    }
169
 
170
    xil_printf("3. Testing memory allocation\n");
171
    if(memoryTest(1) == 577)
172
    {
173
         test_passed();
174
    }
175
    else
176
    {
177
         test_failed();
178
         return 0;
179
    }
180
 
181
    xil_printf("4. Testing Floating Point Arithmetic\n");
182
    f = sqrt(2.0);
183
    if(f < 1.41421354 && f > 1.41421353)
184
    {
185
         test_passed();
186
    }
187
    else
188
    {
189
         test_failed();
190
         return 0;
191
    }
192
 
193
    xil_printf("5. Testing uncommon instructions\n");
194
    a = uncommonInstructions();
195
    if(a == 0)
196
    {
197
         test_passed();
198
    }
199
    else
200
    {
201
        xil_printf("%d\n", a);
202
        test_failed();
203
        return 0;
204
    }
205
 
206
    xil_printf("6. Executing dhrystone benchmark\n");
207
    a = dhry();
208
    if(a == 0)
209
    {
210
         test_passed();
211
    }
212
    else
213
    {
214
         test_failed();
215
         return 0;
216
    }
217
 
218
    xil_printf("The testbench is now finished.\n");
219
    return 0;
220
}

powered by: WebSVN 2.1.0

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