Sapphire SoC DS Sapphire SoC UG Sapphire HP SoC DS Sapphire HP SoC UG RISC-V Embedded IDE UG Board Support Package
Loading...
Searching...
No Matches
syscall.h
Go to the documentation of this file.
1#include <sys/stat.h>
2#include <sys/types.h>
3#include <errno.h>
4#include <sys/time.h>
5#include <unistd.h>
6#include <stdio.h>
7#include <stdint.h>
8#include <string.h>
9
10
11/* Linker must provide these (define in linker.ld) */
12extern char _heap_start; /* start of heap area (set in linker script) */
13extern char _heap_end; /* optional end of heap area */
14
15static char *heap_ptr = &_heap_start;
16const char msg[] = "\033[31m\nERR: Heap Collision!\n\033[0m";
17
18/* _sbrk: provide heap for malloc */
19ssize_t _sbrk(int incr)
20{
21 char *prev_heap = heap_ptr;
22 if ((heap_ptr + incr) > &_heap_end) {
23 write(1,msg,sizeof(msg));
24 errno = ENOMEM;
25 // while(1); //Should we halt the program when hit this issue ?
26 return (ssize_t)-1;
27 }
28 heap_ptr += incr;
29 return (ssize_t)prev_heap;
30}
31
32/* _write: used by printf/vfprintf -> send to UART */
33ssize_t _write(int fd, const void *buf, size_t count)
34{
35 (void)fd; /* ignore fd; 1/2 -> stdout/stderr */
36 const char *p = buf;
37 for (size_t i = 0; i < count; ++i) {
38 char c = p[i];
39 if (c == '\n') {
40 bsp_putChar('\r');
41 //uart_write(&uart0, '\r');
42 }
43 bsp_putChar(c);
44 //uart_write(&uart0, c);
45 }
46 return (ssize_t)count;
47}
48
49ssize_t _read(int fd, char *buf, size_t count)
50{
51 (void)fd;
52 char *start = buf; // Remember where we started so we don't backspace too far
53 size_t i = 0;
54
55 for (; i < count; ) { // Remove ++i from here, we handle it manually
56
57 char c = bsp_getChar(); // Read raw char
58
59 // --- 1. Handle Enter (CR) ---
60 if (c == '\r') {
61 bsp_putChar('\r'); // Move to start of line
62 bsp_putChar('\n'); // Move down (Visual Refresh)
63
64 *buf = '\n'; // Standard C expects \n
65 buf++;
66 return (ssize_t)(i + 1); // Return immediately (Line Complete)
67 }
68
69 // --- 2. Handle Backspace (0x08 or 0x7F) ---
70 if (c == 0x08 || c == 0x7F) {
71 if (i > 0) {
72 // Logic: Step back in buffer
73 buf--;
74 i--;
75
76 // Visual: Erase character from screen
77 bsp_putChar('\b'); // Cursor Left
78 bsp_putChar(' '); // Overwrite with Space
79 bsp_putChar('\b'); // Cursor Left again
80 }
81 continue; // Skip the rest, get next char
82 }
83
84 // --- 3. Handle Normal Characters ---
85 // Optional: Block weird control characters
86 if (c >= ' ' && c <= '~') {
87 *buf = c;
88 bsp_putChar(c); // Echo
89 buf++;
90 i++;
91 }
92 }
93
94 return (ssize_t)i;
95}
96
97/* _close */
98int _close(int fd)
99{
100 (void)fd;
101 return -1;
102}
103
104/* _fstat: mark all fds as character special devices */
105int _fstat(int fd, struct stat *st)
106{
107 (void)fd;
108 st->st_mode = S_IFCHR;
109 return 0;
110}
111
112/* _isatty */
113int _isatty(int fd)
114{
115 (void)fd;
116 return 1; /* treat as TTY */
117}
118
119/* _lseek */
120off_t _lseek(int fd, off_t offset, int whence)
121{
122 (void)fd; (void)offset; (void)whence;
123 return (off_t)-1;
124}
125
126/* _exit - terminate program: loop forever */
127void _exit(int status)
128{
129 (void)status;
130 while (1) { __asm__ volatile ("wfi"); }
131}
132
133/* _kill - required by newlib sometimes */
134int _kill(int pid, int sig)
135{
136 (void)pid; (void)sig;
137 errno = EINVAL;
138 return -1;
139}
140
141/* _getpid */
142int _getpid(void)
143{
144 return 1;
145}
#define bsp_getChar()
Definition bsp.h:43
#define bsp_putChar(c)
Definition bsp.h:42
int _kill(int pid, int sig)
Definition syscall.h:134
int _isatty(int fd)
Definition syscall.h:113
const char msg[]
Definition syscall.h:16
int _close(int fd)
Definition syscall.h:98
int _fstat(int fd, struct stat *st)
Definition syscall.h:105
off_t _lseek(int fd, off_t offset, int whence)
Definition syscall.h:120
int _getpid(void)
Definition syscall.h:142
ssize_t _sbrk(int incr)
Definition syscall.h:19
char _heap_start
ssize_t _write(int fd, const void *buf, size_t count)
Definition syscall.h:33
ssize_t _read(int fd, char *buf, size_t count)
Definition syscall.h:49
void _exit(int status)
Definition syscall.h:127
char _heap_end