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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [tools/] [test.c] - Blame information for rev 21

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

powered by: WebSVN 2.1.0

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