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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [tools/] [test.c] - Blame information for rev 33

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

Line No. Rev Author Line
1 2 rhoads
/*-------------------------------------------------------------------
2
-- TITLE: MIPS CPU test code
3
-- AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
-- DATE CREATED: 4/21/01
5
-- FILENAME: test.c
6
-- PROJECT: MIPS CPU core
7
-- COPYRIGHT: Software placed into the public domain by the author.
8
--    Software 'as is' without warranty.  Author liable for nothing.
9
-- DESCRIPTION:
10
--   The executable image of this file is used as input to the VHDL.
11
--
12
--   This file must not contain any global or static data since
13
--   there isn't a loader to relocate the .data segment and since
14
--   having static data causes the opcodes to begin at a different
15
--   location in the resulting executable file.
16
--
17 33 rhoads
--   Save the opcodes in "code.txt".
18 7 rhoads
--
19
--   The interrupt vector is set to address 0x30.
20 2 rhoads
--------------------------------------------------------------------*/
21 21 rhoads
#ifdef MIPS 
22 2 rhoads
#undef putchar
23
// The MIPS CPU VHDL supports a virtual UART.  All character writes
24
// to address 0xffff will be stored in the file "output.txt".
25
#define putchar(C) *(volatile unsigned char*)0xffff=(unsigned char)(C)
26 33 rhoads
void isr_enable(int);
27
#else
28
#define isr_enable(A)
29 2 rhoads
#endif
30
 
31 33 rhoads
char text[]="Testing the MIPS-lite core.\n";
32
char buf[20];
33
int xyz=0xbadbeef;
34
int abc;
35
 
36 2 rhoads
char *strcpy2(char *s, const char *t)
37
{
38
   char *tmp=s;
39 21 rhoads
   while((int)(*s++=*t++)) ;
40 2 rhoads
   return(tmp);
41
}
42
 
43
static void itoa2(long n, char *s, int base, long *digits)
44
{
45
   long i,j,sign;
46
   unsigned long n2;
47
   char number[20];
48 21 rhoads
   for(i=0;i<15;++i) {
49
      number[i]=' ';
50
   }
51 2 rhoads
   number[15]=0;
52 21 rhoads
   if(n>=0||base!=10) {
53
      sign=1;
54
   } else {
55
      sign=-1;
56
   }
57 2 rhoads
   n2=n*sign;
58
   for(j=14;j>=0;--j) {
59
      i=n2%base;
60
      n2/=base;
61
      number[j]=i<10?'0'+i:'a'+i-10;
62
      if(n2==0&&15-j>=*digits) break;
63
   }
64
   if(sign==-1) {
65
      number[--j]='-';
66
   }
67
   if(*digits==0||*digits<15-j) {
68
      strcpy2(s,&number[j]);
69
      *digits=15-j;
70
   } else {
71
      strcpy2(s,&number[15-*digits]);
72
   }
73
}
74
 
75
void print(long num,long base,long digits)
76
{
77
   char *ptr,buffer[128];
78
   itoa2(num,buffer,base,&digits);
79
   ptr=buffer;
80
   while(*ptr) {
81
      putchar(*ptr++);          /* Put the character out */
82
      if(ptr[-1]=='\n') *--ptr='\r';
83
   }
84
}
85
 
86
void print_hex(unsigned long num)
87
{
88
   long i;
89
   unsigned long j;
90
   for(i=28;i>=0;i-=4) {
91
      j=((num>>i)&0xf);
92
      if(j<10) putchar('0'+j);
93
      else putchar('a'-10+j);
94
   }
95
}
96
 
97 21 rhoads
void print_string(char *p)
98
{
99
   int i;
100 33 rhoads
   for(i=0;p[i];++i) {
101 21 rhoads
      putchar(p[i]);
102
   }
103
}
104
 
105 2 rhoads
int prime()
106
{
107 33 rhoads
   int i,j;
108 2 rhoads
   //show all prime numbers less than 1000
109
   for(i=3;i<1000;i+=2) {
110
      for(j=3;j<i;j+=2) {
111
         if(i%j==0) {
112
            j=0;
113
            break;
114
         }
115
      }
116
      if(j) {
117
         print(i,10,0);
118
         putchar(' ');
119
      }
120
   }
121
   putchar('\n');
122
   return 0;
123
}
124
 
125
int main2()
126
{
127 33 rhoads
   long i,j;
128 2 rhoads
   char char_buf[16];
129
   short short_buf[16];
130
   long long_buf[16];
131 21 rhoads
 
132 33 rhoads
   //Uncomment to test interrupts
133
//   isr_enable(1);
134
 
135 6 rhoads
#if 1 
136 2 rhoads
   //test shift
137
   j=0x12345678;
138
   for(i=0;i<32;++i) {
139
      print_hex(j>>i);
140
      putchar(' ');
141
   }
142
   putchar('\n');
143
   j=0x92345678;
144
   for(i=0;i<32;++i) {
145
      print_hex(j>>i);
146
      putchar(' ');
147
   }
148
   putchar('\n');
149
   j=0x12345678;
150
   for(i=0;i<32;++i) {
151
      print_hex(j<<i);
152
      putchar(' ');
153
   }
154
   putchar('\n');
155
   putchar('\n');
156 6 rhoads
#endif
157
 
158
#if 1 
159 2 rhoads
   //test multiply and divide
160
   j=7;
161
   for(i=0;i<=10;++i) {
162
      print(j*i,10,0);
163
      putchar(' ');
164
   }
165
   putchar('\n');
166
   j=0x321;
167
   for(i=0;i<=5;++i) {
168
      print_hex(j*(i+0x12345));
169
      putchar(' ');
170
   }
171
   putchar('\n');
172
   j=0x54321;
173
   for(i=0;i<=5;++i) {
174
      print_hex(j*(i+0x123));
175
      putchar(' ');
176
   }
177
   putchar('\n');
178
   j=0x12345;
179
   for(i=1;i<10;++i) {
180
      print_hex(j/i);
181
      putchar(' ');
182
   }
183
   putchar('\n');
184
   for(i=1;i<10;++i) {
185
      print_hex(j%i);
186
      putchar(' ');
187
   }
188
   putchar('\n');
189
   putchar('\n');
190 6 rhoads
#endif
191 2 rhoads
 
192 6 rhoads
#if 1
193 2 rhoads
   //test addition and subtraction
194
   j=0x1234;
195
   for(i=0;i<10;++i) {
196
      print_hex(j+i);
197
      putchar(' ');
198
   }
199
   putchar('\n');
200
   for(i=0;i<10;++i) {
201
      print_hex(j-i);
202
      putchar(' ');
203
   }
204
   putchar('\n');
205
   putchar('\n');
206 6 rhoads
#endif
207
 
208
#if 1 
209 2 rhoads
   //test bit operations
210
   i=0x1234;
211
   j=0x4321;
212
   print_hex(i&j);
213
   putchar(' ');
214
   print_hex(i|j);
215
   putchar(' ');
216
   print_hex(i^j);
217
   putchar(' ');
218
   print_hex(~i);
219
   putchar(' ');
220
   print_hex(i+0x12);
221
   putchar(' ');
222
   print_hex(i-0x12);
223
   putchar('\n');
224
   putchar('\n');
225 6 rhoads
#endif
226
 
227
#if 1 
228 2 rhoads
   //test memory access
229
   for(i=0;i<10;++i) {
230
      char_buf[i]=i;
231
      short_buf[i]=i;
232
      long_buf[i]=i;
233
   }
234
   for(i=0;i<10;++i) {
235
      j=char_buf[i];
236
      print(j,10,0);
237
      putchar(' ');
238
      j=short_buf[i];
239
      print(j,10,0);
240
      putchar(' ');
241
      j=long_buf[i];
242
      print(j,10,0);
243
      putchar('\n');
244
   }
245
   putchar('\n');
246 6 rhoads
#endif
247 2 rhoads
 
248
   prime();
249
 
250
   putchar('d'); putchar('o'); putchar('n'); putchar('e'); putchar('\n');
251 6 rhoads
 
252
   for(;;) ;
253 2 rhoads
}
254
 

powered by: WebSVN 2.1.0

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