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

Subversion Repositories darkriscv

[/] [darkriscv/] [trunk/] [src/] [stdio.c] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marcelos
/*
2
 * Copyright (c) 2018, Marcelo Samsoniuk
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * * Redistributions of source code must retain the above copyright notice, this
9
 *   list of conditions and the following disclaimer.
10
 *
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 *   this list of conditions and the following disclaimer in the documentation
13
 *   and/or other materials provided with the distribution.
14
 *
15
 * * Neither the name of the copyright holder nor the names of its
16
 *   contributors may be used to endorse or promote products derived from
17
 *   this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
 
31
#include <io.h>
32
#include <stdio.h>
33
#include <stdarg.h>
34
 
35
#ifdef __RISCV__
36
 
37
// putchar and getchar uses the "low-level" io
38
 
39
int getchar(void)
40
{
41
  while((io.uart.stat&2)==0); // uart empty, wait...
42
 
43
  return io.uart.fifo;
44
}
45
 
46
int putchar(int c)
47
{
48
  if(c=='\n')
49
  {
50
    while(io.uart.stat&1); // uart busy, wait...
51
    io.uart.fifo = '\r';
52
  }
53
 
54
  while(io.uart.stat&1); // uart busy, wait...
55
  return io.uart.fifo = c;
56
}
57
 
58
#endif
59
 
60
// high-level functions uses the getchar/putchar
61
 
62
char *gets(char *p,int s)
63
{
64
  char *ret = p;
65
  int c;
66
 
67
  while(--s)
68
  {
69
    c=getchar();
70
 
71
    if(c=='\n'||c=='\r') break;
72
#ifdef __RISCV__     
73
    putchar(c);
74
#endif
75
    if(c=='\b') // backspace!
76
    {
77
        if(p!=ret)
78
        {
79
            *--p = 0;
80
            s++;
81
        }
82
    }
83
    else
84
        *p++ = c;
85
  }
86
#ifdef __RISCV__
87
  putchar('\n');
88
#endif
89
  *p=0;
90
 
91
  return p==ret ? NULL : ret;
92
}
93
 
94
void putstr(char *p)
95
{
96
    if(p) while(*p) putchar(*p++);
97
    else putstr("(NULL)");
98
}
99
 
100
int puts(char *p)
101
{
102
  putstr(p);
103
  return putchar('\n');
104
}
105
 
106
void putdx(unsigned i, int mode) // mode1 = dec, mode0 = hex
107
{
108
    char *hex="0123456789abcdef";
109
 
110
    int dbd[] = { 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1, 0 };
111
    int dbx[] = { 16777216, 65536, 256, 1, 0 };
112
 
113
    int *db = mode ? dbd : dbx;
114
 
115
    if(mode && i<0)
116
    {
117
        putchar('-');
118
        i=-1;
119
    }
120
 
121
    int j,k,l;
122
 
123
    for(j=0,k=24;(l=db[j]);j++,k-=8)
124
    {
125
        if(l==1 || i>=l)
126
        {
127
            if(mode)
128
            {
129
                putchar(hex[(i/l)%10]);
130
            }
131
            else
132
            {
133
                putchar(hex[(i>>(k+4))&15]);
134
                putchar(hex[(i>>k)&15]);
135
            }
136
        }
137
    }
138
}
139
 
140
void putx(unsigned i)
141
{
142
    putdx(i,0);
143
}
144
 
145
void putd(int i)
146
{
147
    putdx(i,1);
148
}
149
 
150
int printf(char *fmt,...)
151
{
152
    va_list ap;
153
 
154
    for(va_start(ap, fmt);*fmt;fmt++)
155
    {
156
        if(*fmt=='%')
157
        {
158
            fmt++;
159
                 if(*fmt=='s') putstr(va_arg(ap,char *));
160
            else if(*fmt=='x') putx(va_arg(ap,int));
161
            else if(*fmt=='d') putd(va_arg(ap,int));
162
            else putchar(*fmt);
163
        }
164
        else putchar(*fmt);
165
    }
166
 
167
    va_end(ap);
168
 
169
    return 0;
170
}
171
 
172
int strncmp(char *s1,char *s2,int len)
173
{
174
    while(--len && *s1 && *s2 && (*s1==*s2)) s1++, s2++;
175
 
176
    return (*s1-*s2);
177
}
178
 
179
int strcmp(char *s1, char *s2)
180
{
181
    return strncmp(s1,s2,-1);
182
}
183
 
184
int strlen(char *s1)
185
{
186
    int len;
187
 
188
    for(len=0;s1&&*s1++;len++);
189
 
190
    return len;
191
}
192
 
193
char *memcpy(char *dptr,char *sptr,int len)
194
{
195
    char *ret = dptr;
196
 
197
    while(len--) *dptr++ = *sptr++;
198
 
199
    return ret;
200
}
201
 
202
char *memset(char *dptr, int c, int len)
203
{
204
    char *ret = dptr;
205
 
206
    while(len--) *dptr++ = c;
207
 
208
    return ret;
209
}
210
 
211
char *strtok(char *str,char *dptr)
212
{
213
    static char *nxt = NULL;
214
 
215
    int dlen = strlen(dptr);
216
    char *tmp;
217
 
218
         if(str) tmp=str;
219
    else if(nxt) tmp=nxt;
220
    else return NULL;
221
 
222
    char *ret=tmp;
223
 
224
    while(*tmp)
225
    {
226
        if(strncmp(tmp,dptr,dlen)==0)
227
        {
228
            *tmp=NUL;
229
            nxt = tmp+1;
230
            return ret;
231
        }
232
        tmp++;
233
    }
234
    nxt = NULL;
235
    return ret;
236
}
237
 
238
int atoi(char *s1)
239
{
240
    int ret,sig;
241
 
242
    for(sig=ret=0;s1&&*s1;s1++)
243
    {
244
        if(*s1=='-')
245
            sig=1;
246
        else
247
            ret = *s1-'0'+(ret<<3)+(ret<<1); // val = val*10+int(*s1)
248
    }
249
 
250
    return sig ? -ret : ret;
251
}
252
 
253
int xtoi(char *s1)
254
{
255
    int ret;
256
 
257
    for(ret=0;s1&&*s1;s1++)
258
    {
259
        if(*s1<='9')
260
            ret = *s1-'0'+(ret<<4); // val = val*16+int(*s1)
261
        else
262
            ret = 10+(*s1&0x5f)-'A'+(ret<<4); // val = val*16+int(toupper(*s1))
263
    }
264
 
265
    return ret;
266
}
267
 
268
int mac(int acc,short x,short y)
269
{
270
#ifdef __RISCV__
271
    __asm__(".word 0x00c5857F"); // mac a0,a1,a2
272
    // "template"
273
    //acc += (x^y);
274
#else
275
    acc+=x*y;
276
#endif
277
    return acc;
278
}
279
 
280
unsigned __umulsi3(unsigned x,unsigned y)
281
{
282
    unsigned acc;
283
 
284
    if(x<y) { unsigned z = x; x = y; y = z; }
285
 
286
    for(acc=0;y;x<<=1,y>>=1) if (y & 1) acc += x;
287
 
288
    return acc;
289
}
290
 
291
int __mulsi3(int x, int y)
292
{
293
    unsigned acc,xs,ys;
294
 
295
    if(x<0) { xs=1; x=-x; } else xs=0;
296
    if(y<0) { ys=1; y=-y; } else ys=0;
297
 
298
    acc = __umulsi3(x,y);
299
 
300
    return xs^ys ? -acc : acc;
301
}
302
 
303
unsigned __udiv_umod_si3(unsigned x,unsigned y,int opt)
304
{
305
    unsigned acc,aux;
306
 
307
    if(!y) return 0;
308
 
309
    for(aux=1,acc=y;acc<x;aux<<=1,acc<<=1,y=acc);
310
    for(acc=0;x&&aux;aux>>=1,y>>=1) if(y<=x) x-=y,acc+=aux;
311
 
312
    return opt ? acc : x;
313
}
314
 
315
int __udivsi3(int x, int y)
316
{
317
    return __udiv_umod_si3(x,y,1);
318
}
319
 
320
int __umodsi3(int x,int y)
321
{
322
    return __udiv_umod_si3(x,y,0);
323
}
324
 
325
int __div_mod_si3(int x,int y,int opt)
326
{
327
    unsigned acc,xs,ys;
328
 
329
    if(!y) return 0;
330
 
331
    if(x<0) { xs=1; x=-x; } else xs=0;
332
    if(y<0) { ys=1; y=-y; } else ys=0;
333
 
334
    acc = __udiv_umod_si3(x,y,opt);
335
 
336
    if(opt) return xs^ys ? -acc : acc;
337
    else    return xs    ? -acc : acc;
338
}
339
 
340
int __divsi3(int x, int y)
341
{
342
    return __div_mod_si3(x,y,1);
343
}
344
 
345
int __modsi3(int x,int y)
346
{
347
    return __div_mod_si3(x,y,0);
348
}
349
 
350
void usleep(int delay)
351
{
352
    if(threads>1)
353
    {
354
        while(delay--)
355
        {
356
            int t0 = utimers;
357
 
358
            while(t0==utimers);
359
        }
360
    }
361
    else
362
    {
363
        while(delay--)
364
        {
365
            for(io.irq=IRQ_TIMR;!io.irq;);
366
        }
367
    }
368
}

powered by: WebSVN 2.1.0

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