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

Subversion Repositories mlite

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

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
   main2();
36
}
37
 
38
char *strcpy2(char *s, const char *t)
39
{
40
   char *tmp=s;
41
   while((int)(*s++=*t++));
42
   return(tmp);
43
}
44
 
45
static void itoa2(long n, char *s, int base, long *digits)
46
{
47
   long i,j,sign;
48
   unsigned long n2;
49
   char number[20];
50
   for(i=0;i<15;++i) number[i]=' ';
51
   number[15]=0;
52
   if(n>=0||base!=10) sign=1;
53
   else sign=-1;
54
   n2=n*sign;
55
   for(j=14;j>=0;--j) {
56
      i=n2%base;
57
      n2/=base;
58
      number[j]=i<10?'0'+i:'a'+i-10;
59
      if(n2==0&&15-j>=*digits) break;
60
   }
61
   if(sign==-1) {
62
      number[--j]='-';
63
   }
64
   if(*digits==0||*digits<15-j) {
65
      strcpy2(s,&number[j]);
66
      *digits=15-j;
67
   } else {
68
      strcpy2(s,&number[15-*digits]);
69
   }
70
}
71
 
72
void print(long num,long base,long digits)
73
{
74
   volatile unsigned char *uart_base = (unsigned char *)0xffff;
75
   char *ptr,buffer[128];
76
   itoa2(num,buffer,base,&digits);
77
   ptr=buffer;
78
   while(*ptr) {
79
      putchar(*ptr++);          /* Put the character out */
80
      if(ptr[-1]=='\n') *--ptr='\r';
81
   }
82
}
83
 
84
void print_hex(unsigned long num)
85
{
86
   long i;
87
   unsigned long j;
88
   for(i=28;i>=0;i-=4) {
89
      j=((num>>i)&0xf);
90
      if(j<10) putchar('0'+j);
91
      else putchar('a'-10+j);
92
   }
93
}
94
 
95
int prime()
96
{
97
   int i,j,k;
98
   //show all prime numbers less than 1000
99
   for(i=3;i<1000;i+=2) {
100
      for(j=3;j<i;j+=2) {
101
         if(i%j==0) {
102
            j=0;
103
            break;
104
         }
105
      }
106
      if(j) {
107
         print(i,10,0);
108
         putchar(' ');
109
      }
110
   }
111
   putchar('\n');
112
   return 0;
113
}
114
 
115
int main2()
116
{
117
   long i,j,k;
118
   unsigned long m;
119
   char char_buf[16];
120
   short short_buf[16];
121
   long long_buf[16];
122
 
123
   //test shift
124
   j=0x12345678;
125
   for(i=0;i<32;++i) {
126
      print_hex(j>>i);
127
      putchar(' ');
128
   }
129
   putchar('\n');
130
   j=0x92345678;
131
   for(i=0;i<32;++i) {
132
      print_hex(j>>i);
133
      putchar(' ');
134
   }
135
   putchar('\n');
136
   j=0x12345678;
137
   for(i=0;i<32;++i) {
138
      print_hex(j<<i);
139
      putchar(' ');
140
   }
141
   putchar('\n');
142
   putchar('\n');
143
 
144
   //test multiply and divide
145
   j=7;
146
   for(i=0;i<=10;++i) {
147
      print(j*i,10,0);
148
      putchar(' ');
149
   }
150
   putchar('\n');
151
   j=0x321;
152
   for(i=0;i<=5;++i) {
153
      print_hex(j*(i+0x12345));
154
      putchar(' ');
155
   }
156
   putchar('\n');
157
   j=0x54321;
158
   for(i=0;i<=5;++i) {
159
      print_hex(j*(i+0x123));
160
      putchar(' ');
161
   }
162
   putchar('\n');
163
   j=0x12345;
164
   for(i=1;i<10;++i) {
165
      print_hex(j/i);
166
      putchar(' ');
167
   }
168
   putchar('\n');
169
   for(i=1;i<10;++i) {
170
      print_hex(j%i);
171
      putchar(' ');
172
   }
173
   putchar('\n');
174
   putchar('\n');
175
 
176
   //test addition and subtraction
177
   j=0x1234;
178
   for(i=0;i<10;++i) {
179
      print_hex(j+i);
180
      putchar(' ');
181
   }
182
   putchar('\n');
183
   for(i=0;i<10;++i) {
184
      print_hex(j-i);
185
      putchar(' ');
186
   }
187
   putchar('\n');
188
   putchar('\n');
189
 
190
   //test bit operations
191
   i=0x1234;
192
   j=0x4321;
193
   print_hex(i&j);
194
   putchar(' ');
195
   print_hex(i|j);
196
   putchar(' ');
197
   print_hex(i^j);
198
   putchar(' ');
199
   print_hex(~i);
200
   putchar(' ');
201
   print_hex(i+0x12);
202
   putchar(' ');
203
   print_hex(i-0x12);
204
   putchar('\n');
205
   putchar('\n');
206
 
207
   //test memory access
208
   for(i=0;i<10;++i) {
209
      char_buf[i]=i;
210
      short_buf[i]=i;
211
      long_buf[i]=i;
212
   }
213
   for(i=0;i<10;++i) {
214
      j=char_buf[i];
215
      print(j,10,0);
216
      putchar(' ');
217
      j=short_buf[i];
218
      print(j,10,0);
219
      putchar(' ');
220
      j=long_buf[i];
221
      print(j,10,0);
222
      putchar('\n');
223
   }
224
   putchar('\n');
225
 
226
   prime();
227
 
228
   putchar('d'); putchar('o'); putchar('n'); putchar('e'); putchar('\n');
229
 
230
}
231
 

powered by: WebSVN 2.1.0

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