Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / AcessNative / acesskernel_src / helpers.c
1 /*
2  * Acess2 Native Kernel
3  * - Acess kernel emulation on another OS using SDL and UDP
4  *
5  * Kernel Main
6  */
7 #include <arch.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <sys/time.h>
12 #include <string.h>
13
14 #if 0
15 void LogF(const char *Fmt, ...)
16 {
17         va_list args;
18         va_start(args, Fmt);
19         vprintf(Fmt, args);
20         va_end(args);
21 }
22
23 void Log(const char *Fmt, ...)
24 {
25         va_list args;
26         printf("Log: ");
27         va_start(args, Fmt);
28         vprintf(Fmt, args);
29         va_end(args);
30         printf("\n");
31 }
32
33 void Warning(const char *Fmt, ...)
34 {
35         va_list args;
36         printf("Warning: ");
37         va_start(args, Fmt);
38         vprintf(Fmt, args);
39         va_end(args);
40         printf("\n");
41 }
42
43 void Panic(const char *Format, ...)
44 {
45         va_list args;
46         printf("Panic: ");
47         va_start(args, Format);
48         vprintf(Format, args);
49         va_end(args);
50         printf("\n");
51         exit(-1);
52 }
53
54 void Debug_SetKTerminal(const char *Path)
55 {
56         // Ignored, kernel debug goes to stdout instead of a virtual terminal
57 }
58 #endif
59
60 void KernelPanic_SetMode(void)
61 {
62         // NOP - No need
63 }
64 void KernelPanic_PutChar(char ch)
65 {
66         fprintf(stderr, "%c", ch);
67 }
68 void Debug_PutCharDebug(char ch)
69 {
70         printf("%c", ch);
71         if( ch == '\n' )
72                 fflush(stdout);
73 }
74 void Debug_PutStringDebug(const char *String)
75 {
76         printf("%s", String);
77         if( strchr(String, '\n') )
78                 fflush(stdout);
79 }
80
81 void *Heap_Allocate(const char *File, int Line, int ByteCount)
82 {
83         return malloc(ByteCount);
84 }
85
86 void *Heap_AllocateZero(const char *File, int Line, int ByteCount)
87 {
88         return calloc(ByteCount, 1);
89 }
90
91 void *Heap_Reallocate(const char *File, int Line, void *Ptr, int Bytes)
92 {
93         return realloc(Ptr, Bytes);
94 }
95
96 void Heap_Deallocate(void *Ptr)
97 {
98         free(Ptr);
99 }
100
101 char *Heap_StringDup(const char *File, int Line, const char *Str)
102 {
103         return strdup(Str);
104 }
105
106 void Heap_Dump(void)
107 {
108 }
109
110 tPAddr MM_GetPhysAddr(tVAddr VAddr)
111 {
112         return VAddr;   // HACK!
113 }
114
115 int MM_IsValidBuffer(tVAddr Base, int Size)
116 {
117         return 1;
118 }
119
120 Uint MM_GetFlags(tVAddr VAddr)
121 {
122         return 0;
123 }
124
125 int Modules_InitialiseBuiltin(const char *Name)
126 {
127         return 0;       // Ignored
128 }
129
130 Sint64 now(void)
131 {
132         struct timeval tv;
133         struct timezone tz;
134         gettimeofday(&tv, &tz);
135         return tv.tv_sec * 1000 + tv.tv_usec/1000;
136 }
137
138 void IPStack_SendDebugText(const char *str)
139 {
140         // nop
141 }
142
143 int strpos(const char *str, char ch)
144 {
145         const char *retptr = strchr(str, ch);
146         int rv = retptr ? retptr - str : -1;
147         return rv;
148 }
149
150 int CheckString(const char *string)
151 {
152         if( (intptr_t)string < 0x1000 )
153                 return 0;
154         return 1;
155 }
156
157 int CheckMem(const void *buf, size_t len)
158 {
159         if( (intptr_t)buf < 0x1000 )
160                 return 0;
161         return 1;
162 }
163
164 void itoa(char *buf, Uint64 num, int base, int minLength, char pad)
165 {
166         static const char cUCDIGITS[] = "0123456789ABCDEF";
167         char    tmpBuf[64+1];
168          int    pos=0, i;
169         Uint64  rem;
170
171         // Sanity check
172         if(!buf)        return;
173         
174         // Sanity Check
175         if(base > 16 || base < 2) {
176                 buf[0] = 0;
177                 return;
178         }
179         
180         // Convert 
181         while(num > base-1) {
182                 rem = num % base;
183                 num = num / base;       // Shift `num` and get remainder
184                 tmpBuf[pos] = cUCDIGITS[ rem ];
185                 pos++;
186         }
187         tmpBuf[pos++] = cUCDIGITS[ num ];               // Last digit of `num`
188         
189         // Put in reverse
190         i = 0;
191         minLength -= pos;
192         while(minLength-- > 0)
193                 buf[i++] = pad;
194         while(pos-- > 0)
195                 buf[i++] = tmpBuf[pos]; // Reverse the order of characters
196         buf[i] = 0;
197 }
198

UCC git Repository :: git.ucc.asn.au