file.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. int fgetc(FILE* f)
  22. {
  23. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  24. "!0 R0 LOAD32 R0 MEMORY"
  25. "{R0} PUSH_ALWAYS"
  26. "'0' SP R1 NO_SHIFT MOVE_ALWAYS"
  27. "!1 R2 LOADI8_ALWAYS"
  28. "!3 R7 LOADI8_ALWAYS"
  29. "SYSCALL_ALWAYS"
  30. "!0 CMPI8 R0 IMM_ALWAYS"
  31. "{R0} POP_ALWAYS"
  32. "!0 R0 MVNI8_EQUAL");
  33. }
  34. void fputc(char s, FILE* f)
  35. {
  36. asm("!8 R0 SUB R12 ARITH_ALWAYS"
  37. "!0 R0 LOAD32 R0 MEMORY"
  38. "!4 R1 SUB R12 ARITH_ALWAYS"
  39. "!1 R2 LOADI8_ALWAYS"
  40. "!4 R7 LOADI8_ALWAYS"
  41. "SYSCALL_ALWAYS");
  42. }
  43. /* Important values needed for open */
  44. // CONSTANT O_RDONLY 0
  45. // CONSTANT O_WRONLY 1
  46. // CONSTANT O_RDWR 2
  47. // CONSTANT O_CREAT 64
  48. // CONSTANT O_TRUNC 512
  49. /* 00700 in octal is 448*/
  50. // CONSTANT S_IRWXU 448
  51. /* 00100 in octal is 64 */
  52. // CONSTANT S_IXUSR 64
  53. /* 00200 in octal is 128 */
  54. // CONSTANT S_IWUSR 128
  55. /* 00400 in octal is 256 */
  56. // CONSTANT S_IRUSR 256
  57. FILE* open(char* name, int flag, int mode)
  58. {
  59. asm("!4 R0 SUB R12 ARITH_ALWAYS"
  60. "!0 R0 LOAD32 R0 MEMORY"
  61. "!8 R1 SUB R12 ARITH_ALWAYS"
  62. "!0 R1 LOAD32 R1 MEMORY"
  63. "!12 R2 SUB R12 ARITH_ALWAYS"
  64. "!0 R2 LOAD32 R2 MEMORY"
  65. "!5 R7 LOADI8_ALWAYS"
  66. "SYSCALL_ALWAYS");
  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("!4 R0 SUB R12 ARITH_ALWAYS"
  89. "!6 R7 LOADI8_ALWAYS"
  90. "SYSCALL_ALWAYS");
  91. }
  92. int fclose(FILE* stream)
  93. {
  94. int error = close(stream);
  95. return error;
  96. }
  97. int fflush(FILE *stream){
  98. /* We don't buffer, nothing to flush */
  99. return 0;
  100. }
  101. // CONSTANT SEEK_SET 0
  102. // CONSTANT SEEK_CUR 1
  103. // CONSTANT SEEK_END 2
  104. int fseek(FILE* f, long offset, int whence)
  105. {
  106. asm("!19 R7 LOADI8_ALWAYS"
  107. "!12 R2 SUB R12 ARITH_ALWAYS"
  108. "!0 R2 LOAD32 R2 MEMORY"
  109. "!8 R1 SUB R12 ARITH_ALWAYS"
  110. "!0 R1 LOAD32 R1 MEMORY"
  111. "!4 R0 SUB R12 ARITH_ALWAYS"
  112. "!0 R0 LOAD32 R0 MEMORY"
  113. "SYSCALL_ALWAYS");
  114. }
  115. void rewind(FILE* f)
  116. {
  117. fseek(f, 0, SEEK_SET);
  118. }