bootstrap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // CONSTANT stdin 0
  18. // CONSTANT stdout 1
  19. // CONSTANT stderr 2
  20. // CONSTANT EOF 0xFFFFFFFF
  21. // CONSTANT NULL 0
  22. // CONSTANT EXIT_FAILURE 1
  23. // CONSTANT EXIT_SUCCESS 0
  24. // CONSTANT TRUE 1
  25. // CONSTANT FALSE 0
  26. int fgetc(FILE* f)
  27. {
  28. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  29. "!0 R0 LOAD32 R0 MEMORY"
  30. "{R0} PUSH_ALWAYS"
  31. "'0' SP R1 NO_SHIFT MOVE_ALWAYS"
  32. "!1 R2 LOADI8_ALWAYS"
  33. "!3 R7 LOADI8_ALWAYS"
  34. "SYSCALL_ALWAYS"
  35. "!0 CMPI8 R0 IMM_ALWAYS"
  36. "{R0} POP_ALWAYS"
  37. "!0 R0 MVNI8_EQUAL");
  38. }
  39. void fputc(char s, FILE* f)
  40. {
  41. asm("!8 R0 SUB R12 ARITH_ALWAYS"
  42. "!0 R0 LOAD32 R0 MEMORY"
  43. "!4 R1 SUB R12 ARITH_ALWAYS"
  44. "!1 R2 LOADI8_ALWAYS"
  45. "!4 R7 LOADI8_ALWAYS"
  46. "SYSCALL_ALWAYS");
  47. }
  48. void fputs(char* s, FILE* f)
  49. {
  50. while(0 != s[0])
  51. {
  52. fputc(s[0], f);
  53. s = s + 1;
  54. }
  55. }
  56. FILE* open(char* name, int flag, int mode)
  57. {
  58. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  59. "!0 R0 LOAD32 R0 MEMORY"
  60. "!8 R1 SUB R12 ARITH_ALWAYS"
  61. "!0 R1 LOAD32 R1 MEMORY"
  62. "!12 R2 SUB R12 ARITH_ALWAYS"
  63. "!0 R2 LOAD32 R2 MEMORY"
  64. "!5 R7 LOADI8_ALWAYS"
  65. "SYSCALL_ALWAYS");
  66. }
  67. FILE* fopen(char* filename, char* mode)
  68. {
  69. FILE* f;
  70. if('w' == mode[0])
  71. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  72. f = open(filename, 577 , 384);
  73. }
  74. else
  75. { /* Everything else is a read */
  76. f = open(filename, 0, 0);
  77. }
  78. /* Negative numbers are error codes */
  79. if(0 > f)
  80. {
  81. return 0;
  82. }
  83. return f;
  84. }
  85. int close(int fd)
  86. {
  87. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  88. "!6 R7 LOADI8_ALWAYS"
  89. "SYSCALL_ALWAYS");
  90. }
  91. int fclose(FILE* stream)
  92. {
  93. int error = close(stream);
  94. return error;
  95. }
  96. int brk(void *addr)
  97. {
  98. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  99. "!0 R0 LOAD32 R0 MEMORY"
  100. "!45 R7 LOADI8_ALWAYS"
  101. "SYSCALL_ALWAYS");
  102. }
  103. long _malloc_ptr;
  104. long _brk_ptr;
  105. void* malloc(int size)
  106. {
  107. if(NULL == _brk_ptr)
  108. {
  109. _brk_ptr = brk(0);
  110. _malloc_ptr = _brk_ptr;
  111. }
  112. if(_brk_ptr < _malloc_ptr + size)
  113. {
  114. _brk_ptr = brk(_malloc_ptr + size);
  115. if(-1 == _brk_ptr) return 0;
  116. }
  117. long old_malloc = _malloc_ptr;
  118. _malloc_ptr = _malloc_ptr + size;
  119. return old_malloc;
  120. }
  121. int strlen(char* str )
  122. {
  123. int i = 0;
  124. while(0 != str[i]) i = i + 1;
  125. return i;
  126. }
  127. void* memset(void* ptr, int value, int num)
  128. {
  129. char* s;
  130. for(s = ptr; 0 < num; num = num - 1)
  131. {
  132. s[0] = value;
  133. s = s + 1;
  134. }
  135. }
  136. void* calloc(int count, int size)
  137. {
  138. void* ret = malloc(count * size);
  139. if(NULL == ret) return NULL;
  140. memset(ret, 0, (count * size));
  141. return ret;
  142. }
  143. void free(void* l)
  144. {
  145. return;
  146. }
  147. void exit(int value)
  148. {
  149. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  150. "!0 R0 LOAD32 R0 MEMORY"
  151. "!1 R7 LOADI8_ALWAYS"
  152. "SYSCALL_ALWAYS");
  153. }