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

Subversion Repositories mlite

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

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
--   After being compiled with the Microsoft MIPS compiler, the program
18
--   convert will pull out the MIPS opcodes, and switch the executable
19
--   to Big Endian, and convert absolute jumps into relative jumps,
20
--   and save the opcodes in "code.txt".
21
--------------------------------------------------------------------*/
22
#ifdef SIMULATE
23
#undef putchar
24
// The MIPS CPU VHDL supports a virtual UART.  All character writes
25
// to address 0xffff will be stored in the file "output.txt".
26
#define putchar(C) *(volatile unsigned char*)0xffff=(unsigned char)(C)
27
#endif
28
 
29
//The main entry point must be the first function
30
//The program convert will change the first opcode to setting 
31
//the stack pointer.
32
int main()
33
{
34
   int main2();
35 6 rhoads
#ifdef MIPS
36
   __asm(".set noreorder");
37
   //The convertion tool will add two opcodes here
38
   __asm("ori $4,$0,0x1");
39
   __asm("mtc0 $4,$12");  //STATUS=1; enable interrupts
40
   __asm(".set reorder");
41
#endif
42
 
43 2 rhoads
   main2();
44 6 rhoads
 
45
#ifdef MIPS
46
   __asm(".set noreorder");
47
   __asm("nop");
48
 
49
   /*address 0x20 used for storing ISR register values*/
50
   __asm("nop");
51
   __asm("nop");
52
   __asm("nop");
53
   __asm("nop");
54
   __asm("nop");
55
 
56
   /*address 0x30 interrupt service routine*/
57
   __asm("sw $4,0x20($0)");
58
   __asm("sw $5,0x24($0)");
59
 
60
   __asm("ori $5,$0,0xffff");
61
   __asm("ori $4,$0,46");
62
//   __asm("sb $4,0($5)");   /*echo out '.'*/
63
   __asm("nop");
64
 
65
   /*normally clear the interrupt source here*/
66
   /*re-enable interrupts*/
67
   __asm("ori $4,$0,0x1");
68
   __asm("mtc0 $4,$12");   //STATUS=1; enable interrupts
69
   __asm("mfc0 $4, $14");  //C0_EPC=14
70
   __asm("nop");
71
   __asm("lw $5,0x24($0)");
72
   __asm("j $4");
73
   __asm("lw $4,0x20($0)");
74
   __asm(".set reorder");
75
#endif
76 2 rhoads
}
77
 
78
char *strcpy2(char *s, const char *t)
79
{
80
   char *tmp=s;
81
   while((int)(*s++=*t++));
82
   return(tmp);
83
}
84
 
85
static void itoa2(long n, char *s, int base, long *digits)
86
{
87
   long i,j,sign;
88
   unsigned long n2;
89
   char number[20];
90
   for(i=0;i<15;++i) number[i]=' ';
91
   number[15]=0;
92
   if(n>=0||base!=10) sign=1;
93
   else sign=-1;
94
   n2=n*sign;
95
   for(j=14;j>=0;--j) {
96
      i=n2%base;
97
      n2/=base;
98
      number[j]=i<10?'0'+i:'a'+i-10;
99
      if(n2==0&&15-j>=*digits) break;
100
   }
101
   if(sign==-1) {
102
      number[--j]='-';
103
   }
104
   if(*digits==0||*digits<15-j) {
105
      strcpy2(s,&number[j]);
106
      *digits=15-j;
107
   } else {
108
      strcpy2(s,&number[15-*digits]);
109
   }
110
}
111
 
112
void print(long num,long base,long digits)
113
{
114
   volatile unsigned char *uart_base = (unsigned char *)0xffff;
115
   char *ptr,buffer[128];
116
   itoa2(num,buffer,base,&digits);
117
   ptr=buffer;
118
   while(*ptr) {
119
      putchar(*ptr++);          /* Put the character out */
120
      if(ptr[-1]=='\n') *--ptr='\r';
121
   }
122
}
123
 
124
void print_hex(unsigned long num)
125
{
126
   long i;
127
   unsigned long j;
128
   for(i=28;i>=0;i-=4) {
129
      j=((num>>i)&0xf);
130
      if(j<10) putchar('0'+j);
131
      else putchar('a'-10+j);
132
   }
133
}
134
 
135
int prime()
136
{
137
   int i,j,k;
138
   //show all prime numbers less than 1000
139
   for(i=3;i<1000;i+=2) {
140
      for(j=3;j<i;j+=2) {
141
         if(i%j==0) {
142
            j=0;
143
            break;
144
         }
145
      }
146
      if(j) {
147
         print(i,10,0);
148
         putchar(' ');
149
      }
150
   }
151
   putchar('\n');
152
   return 0;
153
}
154
 
155
int main2()
156
{
157
   long i,j,k;
158
   unsigned long m;
159
   char char_buf[16];
160
   short short_buf[16];
161
   long long_buf[16];
162 6 rhoads
 
163
#if 1 
164 2 rhoads
   //test shift
165
   j=0x12345678;
166
   for(i=0;i<32;++i) {
167
      print_hex(j>>i);
168
      putchar(' ');
169
   }
170
   putchar('\n');
171
   j=0x92345678;
172
   for(i=0;i<32;++i) {
173
      print_hex(j>>i);
174
      putchar(' ');
175
   }
176
   putchar('\n');
177
   j=0x12345678;
178
   for(i=0;i<32;++i) {
179
      print_hex(j<<i);
180
      putchar(' ');
181
   }
182
   putchar('\n');
183
   putchar('\n');
184 6 rhoads
#endif
185
 
186
#if 1 
187 2 rhoads
   //test multiply and divide
188
   j=7;
189
   for(i=0;i<=10;++i) {
190
      print(j*i,10,0);
191
      putchar(' ');
192
   }
193
   putchar('\n');
194
   j=0x321;
195
   for(i=0;i<=5;++i) {
196
      print_hex(j*(i+0x12345));
197
      putchar(' ');
198
   }
199
   putchar('\n');
200
   j=0x54321;
201
   for(i=0;i<=5;++i) {
202
      print_hex(j*(i+0x123));
203
      putchar(' ');
204
   }
205
   putchar('\n');
206
   j=0x12345;
207
   for(i=1;i<10;++i) {
208
      print_hex(j/i);
209
      putchar(' ');
210
   }
211
   putchar('\n');
212
   for(i=1;i<10;++i) {
213
      print_hex(j%i);
214
      putchar(' ');
215
   }
216
   putchar('\n');
217
   putchar('\n');
218 6 rhoads
#endif
219 2 rhoads
 
220 6 rhoads
#if 1
221 2 rhoads
   //test addition and subtraction
222
   j=0x1234;
223
   for(i=0;i<10;++i) {
224
      print_hex(j+i);
225
      putchar(' ');
226
   }
227
   putchar('\n');
228
   for(i=0;i<10;++i) {
229
      print_hex(j-i);
230
      putchar(' ');
231
   }
232
   putchar('\n');
233
   putchar('\n');
234 6 rhoads
#endif
235
 
236
#if 1 
237 2 rhoads
   //test bit operations
238
   i=0x1234;
239
   j=0x4321;
240
   print_hex(i&j);
241
   putchar(' ');
242
   print_hex(i|j);
243
   putchar(' ');
244
   print_hex(i^j);
245
   putchar(' ');
246
   print_hex(~i);
247
   putchar(' ');
248
   print_hex(i+0x12);
249
   putchar(' ');
250
   print_hex(i-0x12);
251
   putchar('\n');
252
   putchar('\n');
253 6 rhoads
#endif
254
 
255
#if 1 
256 2 rhoads
   //test memory access
257
   for(i=0;i<10;++i) {
258
      char_buf[i]=i;
259
      short_buf[i]=i;
260
      long_buf[i]=i;
261
   }
262
   for(i=0;i<10;++i) {
263
      j=char_buf[i];
264
      print(j,10,0);
265
      putchar(' ');
266
      j=short_buf[i];
267
      print(j,10,0);
268
      putchar(' ');
269
      j=long_buf[i];
270
      print(j,10,0);
271
      putchar('\n');
272
   }
273
   putchar('\n');
274 6 rhoads
#endif
275 2 rhoads
 
276
   prime();
277
 
278
   putchar('d'); putchar('o'); putchar('n'); putchar('e'); putchar('\n');
279 6 rhoads
 
280
   for(;;) ;
281 2 rhoads
}
282
 

powered by: WebSVN 2.1.0

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