bootstrap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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("LOAD R0 R14 0"
  29. "LOADI R1 -1"
  30. "COPY R2 R15"
  31. "PUSHR R1 R15"
  32. "MOVE R1 R2"
  33. "LOADI R2 1"
  34. "SYS_READ"
  35. "POPR R1 R15"
  36. "JUMP.Z R0 @FUNCTION_FGETC_DONE"
  37. "SR0I R1 24"
  38. ":FUNCTION_FGETC_DONE"
  39. "MOVE R0 R1");
  40. }
  41. void fputc(char s, FILE* f)
  42. {
  43. asm("LOAD R0 R14 0"
  44. "SL0I R0 24"
  45. "COPY R1 R15"
  46. "PUSHR R0 R15"
  47. "LOAD R0 R14 4"
  48. "LOADI R2 1"
  49. "SYS_WRITE"
  50. "POPR R1 R15");
  51. }
  52. void fputs(char* s, FILE* f)
  53. {
  54. while(0 != s[0])
  55. {
  56. fputc(s[0], f);
  57. s = s + 1;
  58. }
  59. }
  60. FILE* open(char* name, int flag, int mode)
  61. {
  62. asm("LOAD R0 R14 0"
  63. "LOAD R1 R14 4"
  64. "LOAD R2 R14 8"
  65. "SYS_OPEN"
  66. "FALSE R2");
  67. }
  68. FILE* fopen(char* filename, char* mode)
  69. {
  70. FILE* f;
  71. if('w' == mode[0])
  72. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  73. f = open(filename, 577 , 384);
  74. }
  75. else
  76. { /* Everything else is a read */
  77. f = open(filename, 0, 0);
  78. }
  79. /* Negative numbers are error codes */
  80. if(0 > f)
  81. {
  82. return 0;
  83. }
  84. return f;
  85. }
  86. int close(int fd)
  87. {
  88. asm("LOAD R0 R14 0"
  89. "SYS_CLOSE");
  90. }
  91. int fclose(FILE* stream)
  92. {
  93. int error = close(stream);
  94. return error;
  95. }
  96. int brk(void *addr)
  97. {
  98. asm("LOAD R0 R14 0"
  99. "ADDU R0 R12 R0"
  100. "SWAP R0 R12");
  101. }
  102. long _malloc_ptr;
  103. long _brk_ptr;
  104. void* malloc(int size)
  105. {
  106. if(NULL == _brk_ptr)
  107. {
  108. _brk_ptr = brk(0);
  109. _malloc_ptr = _brk_ptr;
  110. }
  111. if(_brk_ptr < _malloc_ptr + size)
  112. {
  113. _brk_ptr = brk(_malloc_ptr + size);
  114. if(-1 == _brk_ptr) return 0;
  115. }
  116. long old_malloc = _malloc_ptr;
  117. _malloc_ptr = _malloc_ptr + size;
  118. return old_malloc;
  119. }
  120. int strlen(char* str )
  121. {
  122. int i = 0;
  123. while(0 != str[i]) i = i + 1;
  124. return i;
  125. }
  126. void* memset(void* ptr, int value, int num)
  127. {
  128. char* s;
  129. for(s = ptr; 0 < num; num = num - 1)
  130. {
  131. s[0] = value;
  132. s = s + 1;
  133. }
  134. }
  135. void* calloc(int count, int size)
  136. {
  137. void* ret = malloc(count * size);
  138. if(NULL == ret) return NULL;
  139. memset(ret, 0, (count * size));
  140. return ret;
  141. }
  142. void free(void* l)
  143. {
  144. return;
  145. }
  146. void exit(int value)
  147. {
  148. asm("LOAD R0 R14 0"
  149. "SYS_EXIT");
  150. }