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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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