1 /* Adapteva epiphany-core implementation of stdio support functions ()
3 Copyright (c) 2011, 2012 Adapteva, Inc.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Adapteva nor the names of its contributors may be
14 used to endorse or promote products derived from this software without
15 specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
31 /* simple include interface to EPIPHANY trap instruction. */
34 * The simulator uses a trap instruction to escape to the simulator to do file i/o
37 /* trapcode r0 r1 r2 */
38 #define TRAP_write 0 /* channel addr len */
39 #define TRAP_read 1 /* channel addr len */
40 #define TRAP_open 2 /* filepath mode */
41 #define TRAP_exit 3 /* status */
42 #define TRAP_pass 4 /* - */
43 #define TRAP_fail 5 /* - */
44 #define TRAP_close 6 /* channel */
45 #define TRAP_other 7 /* subcode r1 r2 */
48 #include "epiphany-syscalls.h"
51 /* prototypical inline asm */
52 int __attribute__ ((section ("libgloss_epiphany"))) asm_write (int CHAN
, void* ADDR
, int LEN
)
54 register int chan
asm("r0") = CHAN
;
55 register void* addr
asm("r1") = ADDR
;
56 register int len
asm("r2") = LEN
;
57 register int result
asm("r0");
58 register int error
asm("r3");
59 asm ("trap 0" : "=r" (result
), "=r" (error
) :
60 "r" (chan
), "r" (addr
), "r" (len
));
66 int __attribute__ ((section ("libgloss_epiphany"))) asm_read(int CHAN
, void *ADDR
, int LEN
)
68 register int chan
asm("r0") = CHAN
;
69 register void* addr
asm("r1") = ADDR
;
70 register int len
asm("r2") = LEN
;
71 register int result
asm("r0");
72 register int error
asm("r3");
73 asm ("trap 1" : "=r" (result
), "=r" (error
) :
74 "r" (chan
), "r" (addr
), "r" (len
));
81 int __attribute__ ((section ("libgloss_epiphany")))
82 asm_open(const char* FILE, int FLAGS
, int MODE
)
84 register const char* file
asm("r0") = FILE;
85 register int flags
asm("r1") = FLAGS
;
86 register int result
asm("r0");
87 register int error
asm("r3");
88 asm ("trap 2" : "=r" (result
), "=r" (error
) : "r" (file
), "r" (flags
));
94 void __attribute__ ((section ("libgloss_epiphany"))) asm_exit(int STATUS
)
96 register int status
asm("r0") = STATUS
;
97 asm("trap 3" :: "r" (status
));
100 int __attribute__ ((section ("libgloss_epiphany"))) asm_close(int CHAN
)
102 register int chan
asm("r0") = CHAN
;
103 register int result
asm("r0");
104 register int error
asm("r3");
105 asm ("trap 6" : "=r" (result
), "=r" (error
) : "r" (chan
));
111 int __attribute__ ((section ("libgloss_epiphany"))) asm_syscall(void *P1
,void *P2
, void *P3
, int SUBFUN
)
113 register void* p1
asm("r0") = (void*)P1
;
114 register void* p2
asm("r1") = (void*)P2
;
115 register void* p3
asm("r2") = (void*)P3
;
116 register int result
asm("r0");
117 register int subfun
asm("r3") = SUBFUN
;
118 register int error
asm("r3");
119 asm ("trap 7" : "=r" (result
), "=r" (error
) :
120 "r" (p1
), "r" (p2
), "r" (p3
), "r" (subfun
));
127 * Signal functions implementation
131 #include "epiphany-config.h"
135 #define SW_EXCEPTION_IVT_N 1
136 #define PAGE_MISS_IVT_N 2
137 #define TIMER0_IVT_N 3
138 #define TIMER1_IVT_N 4
139 #define MESSAGE_IVT_N 5
143 #define USR_SOFT_IVT_N 9
146 typedef void (*sighandler_t
)(int);
147 extern sighandler_t ISR_VECTOR
[];
148 extern void DEFAULT_ISR_CALLBACK();
150 sighandler_t
__attribute__ ((section ("libgloss_epiphany"))) signal(int signum
, sighandler_t handler
) {
153 case SIG_DFL
/* see signal.h */:
154 //the default is ignore
156 case SIG_IGN
/* see signal.h */ :
157 DEFAULT_ISR_CALLBACK();
163 ISR_VECTOR
[HW_RESET
] = handler
;
165 case SIG_SW_EXCEPTION
:
166 ISR_VECTOR
[SW_EXCEPTION_IVT_N
] = handler
;
169 ISR_VECTOR
[PAGE_MISS_IVT_N
] = handler
;
172 ISR_VECTOR
[TIMER0_IVT_N
] = handler
;
175 ISR_VECTOR
[TIMER1_IVT_N
] = handler
;
178 ISR_VECTOR
[MESSAGE_IVT_N
] = handler
;
181 ISR_VECTOR
[DMA0_IVT_N
] = handler
;
184 ISR_VECTOR
[DMA1_IVT_N
] = handler
;
187 ISR_VECTOR
[WAND_IVT_N
] = handler
;
191 ISR_VECTOR
[USR_SOFT_IVT_N
] = handler
;
201 //int e_raise(int signum) __attribute__ ((optimize("O0")));
203 int __attribute__ ((section ("libgloss_epiphany"))) e_raise(int signum
) {
205 //register int imask asm("r4") = 0 ;
206 volatile register int ilatst
/*asm("r5") */= signum
;
210 case SIG_DFL
/* see signal.h */:
211 //the default is ignore
213 case SIG_IGN
/* see signal.h */ :
220 //imask = 1 << HW_RESET;
221 ilatst
= 1 << HW_RESET
;
224 case SIG_SW_EXCEPTION
:
225 //imask = 1 << SW_EXCEPTION_IVT_N;
226 ilatst
= 1 << SW_EXCEPTION_IVT_N
;
229 //imask = 1 << PAGE_MISS_IVT_N;
230 ilatst
= 1 << PAGE_MISS_IVT_N
;
233 //imask = 1 << TIMER0_IVT_N;
234 ilatst
= 1 << TIMER0_IVT_N
;
237 //imask = 1 << TIMER1_IVT_N;
238 ilatst
= 1 << TIMER1_IVT_N
;
241 //imask = 1 << MESSAGE_IVT_N;
242 ilatst
= 1 << MESSAGE_IVT_N
;
245 //imask = 1 << DMA0_IVT_N;
246 ilatst
= 1 << DMA0_IVT_N
;
249 //imask = 1 << DMA1_IVT_N;
250 ilatst
= 1 << DMA1_IVT_N
;
253 __asm__
__volatile__ ("wand");
254 //ilatst = 1 << WAND_IVT_N;
259 ilatst
= 1 << USR_SOFT_IVT_N
;
266 //asm("movts imask, r4;");
267 //asm("movts ilatst, r5;");
268 __asm__
__volatile__ ("movts ilatst, %0" : "=r" (ilatst
) : "r" (ilatst
));