]> rtime.felk.cvut.cz Git - fpga/plasma.git/blob - kernel/rtos_test.c
Local copy of Plasma MIPS project.
[fpga/plasma.git] / kernel / rtos_test.c
1 /*--------------------------------------------------------------------
2  * TITLE: Test Plasma Real Time Operating System
3  * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4  * DATE CREATED: 1/1/06
5  * FILENAME: rtos_test.c
6  * PROJECT: Plasma 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  *    Test Plasma Real Time Operating System
11  *--------------------------------------------------------------------*/
12 #include "plasma.h"
13 #include "rtos.h"
14 #include "tcpip.h"
15
16 /* Including mmu.h will cause all OS calls to use SYSCALL */
17 //#include "mmu.h"
18
19 //#define DLL_SETUP
20 //#define DLL_CALL
21 //#include "dll.h"
22
23 #define SEMAPHORE_COUNT 50
24 #define TIMER_COUNT     10
25
26 extern void TestMathFull(void);
27
28 typedef struct {
29    OS_Thread_t *MyThread[TIMER_COUNT];
30    OS_Semaphore_t *MySemaphore[SEMAPHORE_COUNT];
31    OS_Mutex_t *MyMutex;
32    OS_Timer_t *MyTimer[TIMER_COUNT];
33    OS_MQueue_t *MyQueue[TIMER_COUNT];
34    int TimerDone;
35 } TestInfo_t;
36
37 int Global;
38
39 //******************************************************************
40 static void TestCLib(void)
41 {
42    char s1[80], s2[80], *ptr;
43    int rc, v1, v2, v3;
44
45    printf("TestCLib\n");
46    strcpy(s1, "Hello ");
47    memset(s2, 0, sizeof(s2));
48    strncpy(s2, "World wide", 5);
49    strcat(s1, s2);
50    strncat(s1, "!\nthing", 14);
51    printf("%s", s1);
52    rc = strcmp(s1, "Hello World!\n");
53    assert(rc == 0);
54    rc = strcmp(s1, "Hello WOrld!\n");
55    assert(rc > 0);
56    rc = strcmp(s1, "Hello world!\n");
57    assert(rc < 0);
58    rc = strncmp(s1, "Hellx", 4);
59    assert(rc == 0);
60    ptr = strstr(s1, "orl");
61    assert(ptr[0] = 'o');
62    rc = strlen(s1);
63    assert(rc == 13);
64    memcpy(s2, s1, rc+1);
65    rc = memcmp(s1, s2, 8);
66    assert(rc == 0);
67    s2[5] = 'z';
68    rc = memcmp(s1, s2, 8);
69    assert(rc != 0);
70    memset(s2, 0, 5);
71    memset(s2, 'a', 3);
72    rc = abs(-5);
73    itoa(1234, s1, 10);
74    itoa(0, s1, 10);
75    itoa(-1234, s1, 10);
76    itoa(0xabcd, s1, 16);
77    itoa(0x12ab, s1, 16);
78    sprintf(s1, "test c%c d%d 0x%x s%s End\n", 'C', 1234, 0xabcd, "String");
79    printf("%s", s1);
80    sprintf(s1, "test c%c d%6d 0x%6x s%8s End\n", 'C', 1234, 0xabcd, "String");
81    printf("%s", s1);
82    sscanf("1234 -1234 0xabcd text h", "%d %d %x %s", &v1, &v2, &v3, s1);
83    assert(v1 == 1234 && v2 == -1234 && v3 == 0xabcd);
84    assert(strcmp(s1, "text") == 0);
85    //UartScanf("%d %d", &v1, &v2);
86    //printf("v1 = %d v2 = %d\n", v1, v2);
87    printf("Done.\n");
88 }
89
90 //******************************************************************
91 static void TestHeap(void)
92 {
93    uint8 *ptrs[256], size[256], *ptr;
94    int i, j, k, value;
95
96    printf("TestHeap\n");
97    memset(ptrs, 0, sizeof(ptrs));
98    for(i = 0; i < 1000; ++i)
99    {
100       j = rand() & 255;
101       if(ptrs[j])
102       {
103          ptr = ptrs[j];
104          value = size[j];
105          for(k = 0; k < value; ++k)
106          {
107             if(ptr[k] != value)
108                printf("Error\n");
109          }
110          OS_HeapFree(ptrs[j]);
111       }
112       size[j] = (uint8)(rand() & 255);
113       ptrs[j] = OS_HeapMalloc(NULL, size[j]);
114       if(ptrs[j] == NULL)
115          printf("malloc NULL\n");
116       else
117          memset(ptrs[j], size[j], size[j]);
118    }
119    for(i = 0; i < 256; ++i)
120    {
121       if(ptrs[i])
122          OS_HeapFree(ptrs[i]);
123    }
124    printf("Done.\n");
125 }
126
127 //******************************************************************
128 static void MyThreadMain(void *arg)
129 {
130    OS_Thread_t *thread;
131    int priority;
132
133    thread = OS_ThreadSelf();
134    priority = OS_ThreadPriorityGet(thread);
135    OS_ThreadSleep(10);
136    printf("Arg=%d thread=0x%x info=0x%x priority=%d\n", 
137       (uint32)arg, thread, OS_ThreadInfoGet(thread, 0), priority);
138    OS_ThreadExit();
139 }
140
141 static void TestThread(void)
142 {
143    OS_Thread_t *thread;
144    int i, priority;
145
146    printf("TestThread\n");
147    for(i = 0; i < 32; ++i)
148    {
149       priority = 50 + i;
150       thread = OS_ThreadCreate("MyThread", MyThreadMain, (uint32*)i, priority, 0);
151       OS_ThreadInfoSet(thread, 0, (void*)(0xabcd + i));
152       //printf("Created thread 0x%x\n", thread);
153    }
154
155    thread = OS_ThreadSelf();
156    priority = OS_ThreadPriorityGet(thread);
157    printf("Priority = %d\n", priority);
158    OS_ThreadPrioritySet(thread, 200);
159    printf("Priority = %d\n", OS_ThreadPriorityGet(thread));
160    OS_ThreadPrioritySet(thread, priority);
161
162    printf("Thread time = %d\n", OS_ThreadTime());
163    OS_ThreadSleep(100);
164    printf("Thread time = %d\n", OS_ThreadTime());
165 }
166
167 //******************************************************************
168 static void TestSemThread(void *arg)
169 {
170    int i;
171    TestInfo_t *info = (TestInfo_t*)arg;
172
173    for(i = 0; i < SEMAPHORE_COUNT/2; ++i)
174    {
175       printf("s");
176       OS_SemaphorePend(info->MySemaphore[i], OS_WAIT_FOREVER);
177       OS_SemaphorePost(info->MySemaphore[i + SEMAPHORE_COUNT/2]);
178    }
179    OS_ThreadExit();
180 }
181
182 static void TestSemaphore(void)
183 {
184    int i, rc;
185    TestInfo_t info;
186    printf("TestSemaphore\n");
187    for(i = 0; i < SEMAPHORE_COUNT; ++i)
188    {
189       info.MySemaphore[i] = OS_SemaphoreCreate("MySem", 0);
190       //printf("sem[%d]=0x%x\n", i, MySemaphore[i]);
191    }
192
193    OS_ThreadCreate("TestSem", TestSemThread, &info, 50, 0);
194
195    for(i = 0; i < SEMAPHORE_COUNT/2; ++i)
196    {
197       printf("S");
198       OS_SemaphorePost(info.MySemaphore[i]);
199       rc = OS_SemaphorePend(info.MySemaphore[i + SEMAPHORE_COUNT/2], 500);
200       assert(rc == 0);
201    }
202
203    printf(":");
204    rc = OS_SemaphorePend(info.MySemaphore[0], 10);
205    assert(rc != 0);
206    printf(":");
207    OS_SemaphorePend(info.MySemaphore[0], 100);
208    printf(":");
209
210    for(i = 0; i < SEMAPHORE_COUNT; ++i)
211       OS_SemaphoreDelete(info.MySemaphore[i]);
212
213    printf("\nDone.\n");
214 }
215
216 //******************************************************************
217 static void TestMutexThread(void *arg)
218 {
219    TestInfo_t *info = (TestInfo_t*)arg;
220
221    printf("Waiting for mutex\n");
222    OS_MutexPend(info->MyMutex);
223    printf("Have Mutex1\n");
224    OS_MutexPend(info->MyMutex);
225    printf("Have Mutex2\n");
226    OS_MutexPend(info->MyMutex);
227    printf("Have Mutex3\n");
228
229    OS_ThreadSleep(100);
230
231    OS_MutexPost(info->MyMutex);
232    OS_MutexPost(info->MyMutex);
233    OS_MutexPost(info->MyMutex);
234
235    OS_ThreadExit();
236 }
237
238 static void TestMutex(void)
239 {
240    TestInfo_t info;
241    printf("TestMutex\n");
242    info.MyMutex = OS_MutexCreate("MyMutex");
243    OS_MutexPend(info.MyMutex);
244    OS_MutexPend(info.MyMutex);
245    OS_MutexPend(info.MyMutex);
246
247    OS_ThreadSleep(100);
248
249    OS_ThreadCreate("TestMutex", TestMutexThread, &info, 50, 0);
250
251    OS_ThreadSleep(50);
252    OS_MutexPost(info.MyMutex);
253    OS_MutexPost(info.MyMutex);
254    OS_MutexPost(info.MyMutex);
255
256    printf("Try get mutex\n");
257    OS_MutexPend(info.MyMutex);
258    printf("Gotit\n");
259
260    OS_MutexDelete(info.MyMutex);
261    printf("Done.\n");
262 }
263
264 //******************************************************************
265 static void TestMQueue(void)
266 {
267    OS_MQueue_t *mqueue;
268    char data[16];
269    int i, rc;
270
271    printf("TestMQueue\n");
272    mqueue = OS_MQueueCreate("MyMQueue", 10, 16);
273    strcpy(data, "Test0");
274    for(i = 0; i < 16; ++i)
275    {
276       data[4] = (char)('0' + i);
277       OS_MQueueSend(mqueue, data);
278    }
279    for(i = 0; i < 16; ++i)
280    {
281       memset(data, 0, sizeof(data));
282       rc = OS_MQueueGet(mqueue, data, 20);
283       if(rc == 0)
284          printf("message=(%s)\n", data);
285       else
286          printf("timeout\n");
287    }
288
289    OS_MQueueDelete(mqueue);
290    printf("Done.\n");
291 }
292
293 //******************************************************************
294 static void TestTimerThread(void *arg)
295 {
296    int index;
297    uint32 data[4];
298    OS_Timer_t *timer;
299    TestInfo_t *info = (TestInfo_t*)arg;
300
301    //printf("TestTimerThread\n");
302
303    OS_ThreadSleep(1);
304    index = (int)OS_ThreadInfoGet(OS_ThreadSelf(), 0);
305    //printf("index=%d\n", index);
306    OS_MQueueGet(info->MyQueue[index], data, 1000);
307    timer = (OS_Timer_t*)data[1];
308    printf("%d ", data[2]);
309    OS_MQueueGet(info->MyQueue[index], data, 1000);
310    printf("%d ", data[2]);
311    ++info->TimerDone;
312    OS_ThreadExit();
313 }
314
315 static void TestTimer(void)
316 {
317    int i;
318    TestInfo_t info;
319
320    printf("TestTimer\n");
321    info.TimerDone = 0;
322    for(i = 0; i < TIMER_COUNT; ++i)
323    {
324       info.MyQueue[i] = OS_MQueueCreate("MyQueue", 10, 16);
325       info.MyTimer[i] = OS_TimerCreate("MyTimer", info.MyQueue[i], i);
326       info.MyThread[i] = OS_ThreadCreate("TimerTest", TestTimerThread, &info, 50, 0);
327       OS_ThreadInfoSet(info.MyThread[i], 0, (void*)i);
328       OS_TimerStart(info.MyTimer[i], 10+i*2, 220+i);
329    }
330
331    while(info.TimerDone < TIMER_COUNT)
332       OS_ThreadSleep(10);
333
334    for(i = 0; i < TIMER_COUNT; ++i)
335    {
336       OS_MQueueDelete(info.MyQueue[i]);
337       OS_TimerDelete(info.MyTimer[i]);
338    }
339
340    printf("Done.\n");
341 }
342
343 //******************************************************************
344 #if 1
345 void TestMath(void)
346 {
347    int i;
348    float a, b, sum, diff, mult, div;
349    uint32 compare;
350
351    //Check add subtract multiply and divide
352    for(i = -4; i < 4; ++i)
353    {
354       a = (float)(i * 10 + (float)63.2);
355       b = (float)(-i * 5 + (float)3.5);
356       sum = a + b;
357       diff = a - b;
358       mult = a * b;
359       div = a / b;
360       printf("a=%dE-3 b=%dE-3 sum=%dE-3 diff=%dE-3 mult=%dE-3 div=%dE-3\n",
361          (int)(a*(float)1000), (int)(b*(float)1000), 
362          (int)(sum*(float)1000), (int)(diff*(float)1000),
363          (int)(mult*(float)1000), (int)(div*(float)1000));
364    }
365
366    //Comparisons
367    b = (float)2.0;
368    compare = 0;
369    for(i = 1; i < 4; ++i)
370    {
371       a = (float)i;
372       compare = (compare << 1) | (a == b);
373       compare = (compare << 1) | (a != b);
374       compare = (compare << 1) | (a <  b);
375       compare = (compare << 1) | (a <= b);
376       compare = (compare << 1) | (a >  b);
377       compare = (compare << 1) | (a >= b);
378    }
379    printf("Compare = %8x %s\n", compare, 
380       compare==0x1c953 ? "OK" : "ERROR");
381
382    //Cosine
383    for(a = (float)0.0; a <= (float)(3.1415); a += (float)(3.1415/16.0))
384    {
385       b = FP_Cos(a);
386       printf("cos(%4dE-3) = %4dE-3\n", 
387          (int)(a*(float)1000.0), (int)(b*(float)1000.0));
388    }
389 }
390 #endif
391
392 //******************************************************************
393 #ifndef WIN32
394 static void MySyscall(void *arg)
395 {
396    uint32 *stack = arg;
397    stack[STACK_EPC] += 4;  //skip over SYSCALL
398    printf("Inside MySyscall %d\n", stack[28/4]);
399 }
400
401 void TestSyscall(void)
402 {
403    OS_InterruptRegister((uint32)(1<<31), MySyscall);
404    OS_Syscall(57);
405    OS_ThreadSleep(1);
406    printf("Done\n");
407 }
408 #endif
409
410 #ifdef __MMU_ENUM_H__
411 void TestProcess(void)
412 {
413    OS_Process_t *process;
414    process = (OS_Process_t*)OS_HeapMalloc(NULL, sizeof(OS_Process_t));
415    process->name = "test";
416    process->funcPtr = MainThread;
417    process->arg = NULL;
418    process->priority = 200;
419    process->stackSize = 1024*32;
420    process->heapSize = 1024*128;
421    process->processId = 1;
422    process->semaphoreDone = OS_SemaphoreCreate("processDone", 0);
423    printf("Creating process\n");
424    OS_MMUProcessCreate(process);
425    OS_SemaphorePend(process->semaphoreDone, OS_WAIT_FOREVER);
426    printf("Process done\n");
427    OS_MMUProcessDelete(process);
428 }
429 #endif
430
431
432 //******************************************************************
433 void MMUTest(void);
434 void HtmlThread(void *arg);
435 void ConsoleInit(void);
436 void exit(int);
437 uint8 macAddress[] =  {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4};
438
439
440 void MainThread(void *Arg)
441 {
442    int ch, i, display=1;
443    (void)Arg;
444 #ifdef __MMU_ENUM_H__
445    OS_MMUInit();
446 #endif
447
448 #ifdef INCLUDE_ETH
449    EthernetInit(macAddress);
450    IPInit(EthernetTransmit, macAddress, "plasma");
451    HtmlInit(1);
452 #endif
453
454 #ifdef INCLUDE_HTML
455    IPInit(NULL, macAddress, "plasma");
456    HtmlInit(1);
457 #endif
458
459 #ifdef INCLUDE_CONSOLE
460    ConsoleInit();
461 #endif
462
463    for(;;)
464    {
465       if(display)
466       {
467          printf("\n");
468          printf("1 CLib\n");
469          printf("2 Heap\n");
470          printf("3 Thread\n");
471          printf("4 Semaphore\n");
472          printf("5 Mutex\n");
473          printf("6 MQueue\n");
474          printf("7 Timer\n");
475          printf("8 Math\n");
476          printf("9 Syscall\n");
477 #ifdef __MMU_ENUM_H__
478          printf("p MMU Process\n");
479 #endif
480       }
481       printf("> ");
482       display = 1;
483       ch = UartRead();
484       printf("%c\n", ch);
485       switch(ch)
486       {
487 #ifdef WIN32
488       case '0': exit(0);
489 #endif
490       case '1': TestCLib(); break;
491       case '2': TestHeap(); break;
492       case '3': TestThread(); break;
493       case '4': TestSemaphore(); break;
494       case '5': TestMutex(); break;
495       case '6': TestMQueue(); break;
496       case '7': TestTimer(); break;
497       case '8': TestMath(); break;
498 #ifndef WIN32
499       case '9': TestSyscall(); break;
500 #endif
501 #ifdef __MMU_ENUM_H__
502       case 'p': TestProcess(); break;
503 #endif
504 #ifdef WIN32
505       case 'm': TestMathFull(); break;
506 #endif
507       case 'g': printf("Global=%d\n", ++Global); break;
508       default: 
509          printf("E");
510          display = 0;
511          for(i = 0; i < 30; ++i)
512          {
513             while(kbhit())
514                ch = UartRead();
515             OS_ThreadSleep(1);
516          }
517          break;
518       }
519    }
520 }
521
522