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

Subversion Repositories darkriscv

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

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 4 marcelos
// idle time, update timer and blink the led! :)
38
 
39
void _idle(void)
40
{
41
    if(io.irq&IRQ_TIMR)
42
    {
43
      if(!utimers--)
44
      {
45
        io.led++;
46
        utimers=999999;
47
      }
48
      io.irq = IRQ_TIMR;
49
    }
50
}
51
 
52 2 marcelos
// putchar and getchar uses the "low-level" io
53
 
54
int getchar(void)
55
{
56 4 marcelos
  while((io.uart.stat&2)==0) _idle(); // uart empty, wait...
57 2 marcelos
  return io.uart.fifo;
58
}
59
 
60
int putchar(int c)
61
{
62
  if(c=='\n')
63
  {
64 4 marcelos
    while(io.uart.stat&1) _idle(); // uart busy, wait...
65 2 marcelos
    io.uart.fifo = '\r';
66
  }
67
 
68 4 marcelos
  while(io.uart.stat&1) _idle(); // uart busy, wait...
69 2 marcelos
  return io.uart.fifo = c;
70
}
71
 
72
#endif
73
 
74 4 marcelos
// high-level functions use the getchar/putchar
75 2 marcelos
 
76
char *gets(char *p,int s)
77
{
78
  char *ret = p;
79
  int c;
80
 
81
  while(--s)
82
  {
83
    c=getchar();
84
 
85
    if(c=='\n'||c=='\r') break;
86
#ifdef __RISCV__     
87
    putchar(c);
88
#endif
89
    if(c=='\b') // backspace!
90
    {
91
        if(p!=ret)
92
        {
93
            *--p = 0;
94
            s++;
95
        }
96
    }
97
    else
98
        *p++ = c;
99
  }
100
#ifdef __RISCV__
101
  putchar('\n');
102
#endif
103
  *p=0;
104
 
105
  return p==ret ? NULL : ret;
106
}
107
 
108
void putstr(char *p)
109
{
110
    if(p) while(*p) putchar(*p++);
111
    else putstr("(NULL)");
112
}
113
 
114
int puts(char *p)
115
{
116 4 marcelos
    putstr(p);
117
    return putchar('\n');
118 2 marcelos
}
119
 
120
void putdx(unsigned i, int mode) // mode1 = dec, mode0 = hex
121
{
122
    char *hex="0123456789abcdef";
123
 
124
    int dbd[] = { 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1, 0 };
125
    int dbx[] = { 16777216, 65536, 256, 1, 0 };
126
 
127
    int *db = mode ? dbd : dbx;
128
 
129
    if(mode && i<0)
130
    {
131
        putchar('-');
132
        i=-1;
133
    }
134
 
135
    int j,k,l;
136
 
137
    for(j=0,k=24;(l=db[j]);j++,k-=8)
138
    {
139
        if(l==1 || i>=l)
140
        {
141
            if(mode)
142
            {
143
                putchar(hex[(i/l)%10]);
144
            }
145
            else
146
            {
147
                putchar(hex[(i>>(k+4))&15]);
148
                putchar(hex[(i>>k)&15]);
149
            }
150
        }
151
    }
152
}
153
 
154
void putx(unsigned i)
155
{
156
    putdx(i,0);
157
}
158
 
159
void putd(int i)
160
{
161
    putdx(i,1);
162
}
163
 
164
int printf(char *fmt,...)
165
{
166
    va_list ap;
167
 
168
    for(va_start(ap, fmt);*fmt;fmt++)
169
    {
170
        if(*fmt=='%')
171
        {
172
            fmt++;
173
                 if(*fmt=='s') putstr(va_arg(ap,char *));
174
            else if(*fmt=='x') putx(va_arg(ap,int));
175
            else if(*fmt=='d') putd(va_arg(ap,int));
176
            else putchar(*fmt);
177
        }
178
        else putchar(*fmt);
179
    }
180
 
181
    va_end(ap);
182
 
183
    return 0;
184
}
185
 
186
int strncmp(char *s1,char *s2,int len)
187
{
188
    while(--len && *s1 && *s2 && (*s1==*s2)) s1++, s2++;
189
 
190
    return (*s1-*s2);
191
}
192
 
193
int strcmp(char *s1, char *s2)
194
{
195
    return strncmp(s1,s2,-1);
196
}
197
 
198
int strlen(char *s1)
199
{
200
    int len;
201
 
202
    for(len=0;s1&&*s1++;len++);
203
 
204
    return len;
205
}
206
 
207
char *memcpy(char *dptr,char *sptr,int len)
208
{
209
    char *ret = dptr;
210
 
211
    while(len--) *dptr++ = *sptr++;
212
 
213
    return ret;
214
}
215
 
216
char *memset(char *dptr, int c, int len)
217
{
218
    char *ret = dptr;
219
 
220
    while(len--) *dptr++ = c;
221
 
222
    return ret;
223
}
224
 
225
char *strtok(char *str,char *dptr)
226
{
227
    static char *nxt = NULL;
228
 
229
    int dlen = strlen(dptr);
230
    char *tmp;
231
 
232
         if(str) tmp=str;
233
    else if(nxt) tmp=nxt;
234
    else return NULL;
235
 
236
    char *ret=tmp;
237
 
238
    while(*tmp)
239
    {
240
        if(strncmp(tmp,dptr,dlen)==0)
241
        {
242
            *tmp=NUL;
243
            nxt = tmp+1;
244
            return ret;
245
        }
246
        tmp++;
247
    }
248
    nxt = NULL;
249
    return ret;
250
}
251
 
252
int atoi(char *s1)
253
{
254
    int ret,sig;
255
 
256
    for(sig=ret=0;s1&&*s1;s1++)
257
    {
258
        if(*s1=='-')
259
            sig=1;
260
        else
261
            ret = *s1-'0'+(ret<<3)+(ret<<1); // val = val*10+int(*s1)
262
    }
263
 
264
    return sig ? -ret : ret;
265
}
266
 
267
int xtoi(char *s1)
268
{
269
    int ret;
270
 
271
    for(ret=0;s1&&*s1;s1++)
272
    {
273
        if(*s1<='9')
274
            ret = *s1-'0'+(ret<<4); // val = val*16+int(*s1)
275
        else
276
            ret = 10+(*s1&0x5f)-'A'+(ret<<4); // val = val*16+int(toupper(*s1))
277
    }
278
 
279
    return ret;
280
}
281
 
282
int mac(int acc,short x,short y)
283
{
284
#ifdef __RISCV__
285
    __asm__(".word 0x00c5857F"); // mac a0,a1,a2
286
    // "template"
287
    //acc += (x^y);
288
#else
289
    acc+=x*y;
290
#endif
291
    return acc;
292
}
293
 
294
unsigned __umulsi3(unsigned x,unsigned y)
295
{
296
    unsigned acc;
297
 
298
    if(x<y) { unsigned z = x; x = y; y = z; }
299
 
300
    for(acc=0;y;x<<=1,y>>=1) if (y & 1) acc += x;
301
 
302
    return acc;
303
}
304
 
305
int __mulsi3(int x, int y)
306
{
307
    unsigned acc,xs,ys;
308
 
309
    if(x<0) { xs=1; x=-x; } else xs=0;
310
    if(y<0) { ys=1; y=-y; } else ys=0;
311
 
312
    acc = __umulsi3(x,y);
313
 
314
    return xs^ys ? -acc : acc;
315
}
316
 
317
unsigned __udiv_umod_si3(unsigned x,unsigned y,int opt)
318
{
319
    unsigned acc,aux;
320
 
321
    if(!y) return 0;
322
 
323 4 marcelos
    for(aux=1;y<x&&!(y&(1<<31));aux<<=1,y<<=1);
324 2 marcelos
    for(acc=0;x&&aux;aux>>=1,y>>=1) if(y<=x) x-=y,acc+=aux;
325
 
326
    return opt ? acc : x;
327
}
328
 
329
int __udivsi3(int x, int y)
330
{
331
    return __udiv_umod_si3(x,y,1);
332
}
333
 
334
int __umodsi3(int x,int y)
335
{
336
    return __udiv_umod_si3(x,y,0);
337
}
338
 
339
int __div_mod_si3(int x,int y,int opt)
340
{
341
    unsigned acc,xs,ys;
342
 
343
    if(!y) return 0;
344
 
345
    if(x<0) { xs=1; x=-x; } else xs=0;
346
    if(y<0) { ys=1; y=-y; } else ys=0;
347
 
348
    acc = __udiv_umod_si3(x,y,opt);
349
 
350
    if(opt) return xs^ys ? -acc : acc;
351
    else    return xs    ? -acc : acc;
352
}
353
 
354
int __divsi3(int x, int y)
355
{
356
    return __div_mod_si3(x,y,1);
357
}
358
 
359
int __modsi3(int x,int y)
360
{
361
    return __div_mod_si3(x,y,0);
362
}
363
 
364
void usleep(int delay)
365
{
366
    {
367
        while(delay--)
368
        {
369
            for(io.irq=IRQ_TIMR;!io.irq;);
370
        }
371
    }
372
}

powered by: WebSVN 2.1.0

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