Created
March 21, 2026 21:45
-
-
Save s5bug/05a55a9f6939c89bf194d95211c5cc6e to your computer and use it in GitHub Desktop.
C header for SPIM syscalls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef SPIM_H | |
| #define SPIM_H | |
| #include <stddef.h> | |
| __attribute__((always_inline)) static inline void spim_print_int(int value) { | |
| register unsigned long v0 __asm__("$v0") = 1; | |
| register int a0 __asm__("$a0") = value; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "r" (a0) | |
| : "memory" | |
| ); | |
| } | |
| #ifdef __mips_hard_float | |
| __attribute__((always_inline)) static inline void spim_print_float(float value) { | |
| register unsigned long v0 __asm__("$v0") = 2; | |
| register float f12 __asm__("$f12") = value; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "f" (f12) | |
| : "memory" | |
| ); | |
| } | |
| __attribute__((always_inline)) static inline void spim_print_double(double value) { | |
| register unsigned long v0 __asm__("$v0") = 3; | |
| register double f12 __asm__("$f12") = value; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "f" (f12) | |
| : "memory" | |
| ); | |
| } | |
| #else | |
| __attribute__((unused, noinline, error("spim_print_float() requires -mhard-float"))) | |
| static void spim_print_float(float) { | |
| __builtin_unreachable(); | |
| } | |
| __attribute__((unused, noinline, error("spim_print_double() requires -mhard-float"))) | |
| static void spim_print_double(double) { | |
| __builtin_unreachable(); | |
| } | |
| #endif | |
| __attribute__((always_inline)) static inline void spim_print_string(const char* value) { | |
| register unsigned long v0 __asm__("$v0") = 4; | |
| register const char* a0 __asm__("$a0") = value; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "r" (a0) | |
| : "memory" | |
| ); | |
| } | |
| __attribute__((always_inline)) static inline int spim_read_int(void) { | |
| register unsigned long v0 __asm__("$v0") = 5; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : | |
| : "memory" | |
| ); | |
| return (int) v0; | |
| } | |
| #ifdef __mips_hard_float | |
| __attribute__((always_inline)) static inline float spim_read_float(void) { | |
| register unsigned long v0 __asm__("$v0") = 6; | |
| register float f0 __asm__("$f0"); | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0), "=f" (f0) | |
| : | |
| : "memory" | |
| ); | |
| return f0; | |
| } | |
| __attribute__((always_inline)) static inline double spim_read_double(void) { | |
| register unsigned long v0 __asm__("$v0") = 7; | |
| register double f0 __asm__("$f0"); | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0), "=f" (f0) | |
| : | |
| : "memory" | |
| ); | |
| return f0; | |
| } | |
| #else | |
| __attribute__((unused, noinline, error("spim_read_float() requires -mhard-float"))) | |
| static float spim_read_float(void) { | |
| __builtin_unreachable(); | |
| } | |
| __attribute__((unused, noinline, error("spim_read_double() requires -mhard-float"))) | |
| static double spim_read_double(void) { | |
| __builtin_unreachable(); | |
| } | |
| #endif | |
| __attribute__((always_inline)) static inline void spim_read_string(char* buffer, size_t length) { | |
| register unsigned long v0 __asm__("$v0") = 8; | |
| register char* a0 __asm__("$a0") = buffer; | |
| register size_t a1 __asm__("$a1") = length; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "r" (a0), "r" (a1) | |
| : "memory" | |
| ); | |
| } | |
| __attribute__((always_inline)) static inline char* spim_sbrk(size_t size) { | |
| register unsigned long v0 __asm__("$v0") = 9; | |
| register size_t a0 __asm__("$a0") = size; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "r" (a0) | |
| : "memory" | |
| ); | |
| return (char*) v0; | |
| } | |
| __attribute__((always_inline, noreturn)) static inline void spim_exit(void) { | |
| register unsigned long v0 __asm__("$v0") = 10; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : | |
| : "r" (v0) | |
| : | |
| ); | |
| __builtin_unreachable(); | |
| } | |
| __attribute__((always_inline)) static inline void spim_print_char(char value) { | |
| register unsigned long v0 __asm__("$v0") = 11; | |
| register char a0 __asm__("$a0") = value; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0) | |
| : "r" (a0) | |
| : "memory" | |
| ); | |
| } | |
| __attribute__((always_inline)) static inline char spim_read_char(void) { | |
| register unsigned long v0 __asm__("$v0") = 12; | |
| register char a0 __asm__("$a0"); | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : "+r" (v0), "=r" (a0) | |
| : | |
| : "memory" | |
| ); | |
| return a0; | |
| } | |
| __attribute__((always_inline, noreturn)) static inline void spim_exit2(int code) { | |
| register unsigned long v0 __asm__("$v0") = 17; | |
| register int a0 __asm__("$a0") = code; | |
| __asm__ __volatile__ ( | |
| "syscall" | |
| : | |
| : "r" (v0), "r" (a0) | |
| : | |
| ); | |
| __builtin_unreachable(); | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment