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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [zlib/] [syscalls.c] - Blame information for rev 54

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 54 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    syscalls.c
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     
8
//
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
////////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
16
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// License:     GPL, v3, as defined and found on www.gnu.org,
28
//              http://www.gnu.org/licenses/gpl.html
29
//
30
//
31
////////////////////////////////////////////////////////////////////////////////
32
//
33
//
34
#include <sys/errno.h>
35
#include <stdint.h>
36
#include <sys/unistd.h>
37
#include <sys/types.h>
38
#include <sys/stat.h>
39
#include <sys/times.h>
40
#include <reent.h>
41
#include <stdio.h>
42
#include "artyboard.h"
43
#include "bootloader.h"
44
#include "zipcpu.h"
45
 
46
void
47
_outbyte(char v) {
48
#ifdef  _ZIP_HAS_WBUART
49
        if (v == '\n') {
50
                // Depend upon the WBUART, not the PIC
51
                while(_uart->u_fifo & 0x010000)
52
                        ;
53
                _uarttx = (unsigned)'\r';
54
        }
55
 
56
        // Depend upon the WBUART, not the PIC
57
        while(_uart->u_fifo & 0x010000)
58
                ;
59
        uint8_t c = v;
60
        _uarttx = (unsigned)c;
61
#else
62
#ifdef  _ZIP_HAS_UARTTX
63
        // Depend upon the WBUART, not the PIC
64
        while(UARTTX & 0x100)
65
                ;
66
        uint8_t c = v;
67
        UARTTX = (unsigned)c;
68
#endif
69
#endif
70
}
71
 
72
int
73
_inbyte(void) {
74
#ifdef  _ZIP_HAS_WBUARTRX
75
        const   int     echo = 1, cr_into_nl = 1;
76
        static  int     last_was_cr = 0;
77
        int     rv;
78
 
79
        // Character translations:
80
        // 1. All characters should be echoed
81
        // 2. \r's should quietly be turned into \n's
82
        // 3. \r\n's should quietly be turned into \n's
83
        // 4. \n's should be passed as is
84
        // Insist on at least one character
85
        rv = _uartrx;
86
        if (rv & 0x0100)
87
                rv = -1;
88
        else if ((cr_into_nl)&&(rv == '\r')) {
89
                rv = '\n';
90
                last_was_cr = 1;
91
        } else if ((cr_into_nl)&&(rv == '\n')) {
92
                if (last_was_cr) {
93
                        rv = -1;
94
                        last_was_cr = 0;
95
                }
96
        } else
97
                last_was_cr = 0;
98
 
99
        if ((rv != -1)&&(echo))
100
                _outbyte(rv);
101
        return rv;
102
#else
103
        return -1;
104
#endif
105
}
106
 
107
int
108
_close_r(struct _reent *reent, int file) {
109
        reent->_errno = EBADF;
110
 
111
        return -1;      /* Always fails */
112
}
113
 
114
char    *__env[1] = { 0 };
115
char    **environ = __env;
116
 
117
int
118
_execve_r(struct _reent *reent, const char *name, char * const *argv, char * const *env)
119
{
120
        reent->_errno = ENOSYS;
121
        return -1;
122
}
123
 
124
int
125
_fork_r(struct _reent *reent)
126
{
127
        reent->_errno = ENOSYS;
128
        return -1;
129
}
130
 
131
int
132
_fstat_r(struct _reent *reent, int file, struct stat *st)
133
{
134
        if ((STDOUT_FILENO == file)||(STDERR_FILENO == file)
135
                ||(STDIN_FILENO == file)) {
136
                st->st_mode = S_IFCHR;
137
                return 0;
138
#ifdef  _ZIP_HAS_SDCARD_NOTYET
139
        } else if (SDCARD_FILENO == file) {
140
                st->st_mode = S_IFBLK;
141
#endif
142
        } else {
143
                reent->_errno = EBADF;
144
                return -1;
145
        }
146
}
147
 
148
int
149
_getpid_r(struct _reent *reent)
150
{
151
        return 1;
152
}
153
 
154
int
155
_gettimeofday_r(struct _reent *reent, struct timeval *ptimeval, void *ptimezone)
156
{
157
#ifdef  _ZIP_HAS_RTC
158
        if (ptimeval) {
159
                uint32_t        now, date;
160
                unsigned        s, m, h, tod;
161
 
162
                now = _rtcdev->r_clock;
163
 
164
#ifdef  _ZIP_HAS_RTDATE
165
                unsigned        d, y, c, yy, days_since_epoch;
166
                int             ly;
167
 
168
                date= *_rtdate;
169
 
170
                d = ( date     &0x0f)+((date>> 4)&0x0f)*10;
171
                m = ((date>> 8)&0x0f)+((date>>12)&0x0f)*10;
172
                y = ((date>>16)&0x0f)+((date>>20)&0x0f)*10;
173
                c = ((date>>24)&0x0f)+((date>>28)&0x0f)*10;
174
 
175
                ly = 0;
176
                if ((y&3)==0) {
177
                        if (y!=0)
178
                                ly = 1;
179
                        else if ((y&3)==0)
180
                                ly = 1;
181
                }
182
 
183
                days_since_epoch = d;
184
                if (m>1) {
185
                        days_since_epoch += 31;
186
                        if (m>2) {
187
                                days_since_epoch += 28;
188
                                if (ly) days_since_epoch++;
189
                                if (m>3)  { days_since_epoch += 31;
190
                                if (m>4)  { days_since_epoch += 30;
191
                                if (m>5)  { days_since_epoch += 31;
192
                                if (m>6)  { days_since_epoch += 30;
193
                                if (m>7)  { days_since_epoch += 31;
194
                                if (m>8)  { days_since_epoch += 31;
195
                                if (m>9)  { days_since_epoch += 30;
196
                                if (m>10) { days_since_epoch += 31;
197
                                if (m>11)   days_since_epoch += 30;
198
                }}}}}}}}}}
199
 
200
                for(yy=1970; yy<(c*100+y); yy++) {
201
                        if ((yy&3)==0)
202
                                days_since_epoch += 366;
203
                        else
204
                                days_since_epoch += 365;
205
                }
206
 
207
                ptimeval->tv_sec  = days_since_epoch * 86400l;
208
#else
209
                ptimeval->tv_sec  = 0;
210
#endif
211
 
212
                s = ( now     &0x0f)+((now>> 4)&0x0f)*10;
213
                m = ((now>> 8)&0x0f)+((now>>12)&0x0f)*10;
214
                h = ((now>>16)&0x0f)+((now>>20)&0x0f)*10;
215
                tod = (h * 60 + m) * 60;
216
                ptimeval->tv_sec += tod;
217
 
218
                ptimeval->tv_usec = 0;
219
        }
220
        return 0;
221
#else
222
        reent->_errno = ENOSYS;
223
        return -1;
224
#endif
225
}
226
 
227
int
228
_isatty_r(struct _reent *reent, int file)
229
{
230
        if ((STDIN_FILENO == file)
231
                        ||(STDOUT_FILENO == file)
232
                        ||(STDERR_FILENO==file))
233
                return 1;
234
        return 0;
235
}
236
 
237
int
238
_kill_r(struct _reent *reent, int pid, int sig)
239
{
240
        reent->_errno = ENOSYS;
241
        return -1;
242
}
243
 
244
int
245
_link_r(struct _reent *reent, const char *existing, const char *new)
246
{
247
        reent->_errno = ENOSYS;
248
        return -1;
249
}
250
 
251
_off_t
252
_lseek_r(struct _reent *reent, int file, _off_t ptr, int dir)
253
{
254
#ifdef  _ZIP_HAS_SDCARD_NOTYET
255
        if (SDCARD_FILENO == file) {
256
                switch(dir) {
257
                case SEEK_SET:  rootfs_offset = ptr;
258
                case SEEK_CUR:  rootfs_offset += ptr;
259
                case SEEK_END:  rootfs_offset = rootfs->nsectors * rootfs->sectorsize - ptr;
260
                default:
261
                        reent->_errno = EINVAL; return -1;
262
                } return rootfs_offset;
263
        }
264
#endif
265
        reent->_errno = ENOSYS;
266
        return -1;
267
}
268
 
269
int
270
_open_r(struct _reent *reent, const char *file, int flags, int mode)
271
{
272
#ifdef  _ZIP_HAS_SDCARD_NOTYET
273
        if (strcmp(file, "/dev/sdcard")==0) {
274
                return SDCARD_FILENO;
275
        } else {
276
                reent->_errno = EACCES;
277
                return -1;
278
        }
279
#endif
280
        reent->_errno = ENOSYS;
281
        return -1;
282
}
283
 
284
int
285
_read_r(struct _reent *reent, int file, void *ptr, size_t len)
286
{
287
#ifdef  _ZIP_HAS_WBUARTRX
288
        if (STDIN_FILENO == file)
289
        {
290
                int     nr = 0, rv;
291
                char    *chp = ptr;
292
 
293
                while((rv=_inbyte()) &0x0100)
294
                        ;
295
                *chp++ = (char)rv;
296
                nr++;
297
 
298
                // Now read out anything left in the FIFO
299
                while((nr < len)&&(((rv=_inbyte()) & 0x0100)==0)) {
300
                        *chp++ = (char)rv;
301
                        nr++;
302
                }
303
 
304
                // if (rv & 0x01000) _uartrx = 0x01000;
305
                return nr;
306
        }
307
#endif
308
#ifdef  _ZIP_HAS_SDCARD_NOTYET
309
        if (SDCARD_FILENO == file)
310
        {
311
        }
312
#endif
313
        errno = ENOSYS;
314
        return -1;
315
}
316
 
317
int
318
_readlink_r(struct _reent *reent, const char *path, char *buf, size_t bufsize)
319
{
320
        reent->_errno = ENOSYS;
321
        return -1;
322
}
323
 
324
int
325
_stat_r(struct _reent *reent, const char *path, struct stat *buf) {
326
        reent->_errno = EIO;
327
        return -1;
328
}
329
 
330
int
331
_unlink_r(struct _reent *reent, const char *path)
332
{
333
        reent->_errno = EIO;
334
        return -1;
335
}
336
 
337
int
338
_times(struct tms *buf) {
339
        errno = EACCES;
340
        return -1;
341
}
342
 
343
int
344
_write_r(struct _reent * reent, int fd, const void *buf, size_t nbytes) {
345
        if ((STDOUT_FILENO == fd)||(STDERR_FILENO == fd)) {
346
                const   char *cbuf = buf;
347
                for(int i=0; i<nbytes; i++)
348
                        _outbyte(cbuf[i]);
349
                return nbytes;
350
        }
351
#ifdef  _ZIP_HAS_SDCARD_NOTYET
352
        if (SDCARD_FILENO == file)
353
        {
354
        }
355
#endif
356
 
357
        reent->_errno = EBADF;
358
        return -1;
359
}
360
 
361
int
362
_wait(int *status) {
363
        errno = ECHILD;
364
        return -1;
365
}
366
 
367
int     *heap = _top_of_heap;
368
 
369
void *
370
_sbrk_r(struct _reent *reent, int sz) {
371
        int     *prev = heap;
372
 
373
        heap += sz;
374
        return  prev;
375
}
376
 

powered by: WebSVN 2.1.0

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