file-native.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 0x1100
  18. // CONSTANT stdout 0x1101
  19. // CONSTANT stderr 0
  20. // CONSTANT EOF 0xFFFFFFFF
  21. int match(char* a, char* b);
  22. int fgetc(FILE* f)
  23. {
  24. asm("LOAD R1 R14 0"
  25. "FGETC");
  26. }
  27. void fputc(char s, FILE* f)
  28. {
  29. asm("LOAD R0 R14 0"
  30. "LOAD R1 R14 4"
  31. "FPUTC");
  32. }
  33. FILE* open_read(int filename)
  34. {
  35. asm("LOAD R0 R14 0"
  36. "FOPEN_READ");
  37. }
  38. FILE* open_write(int filename)
  39. {
  40. asm("LOAD R0 R14 0"
  41. "FOPEN_WRITE");
  42. }
  43. int fclose(FILE* stream)
  44. {
  45. asm("LOAD R0 R14 0"
  46. "FCLOSE");
  47. }
  48. FILE* fopen(char* filename, char* mode)
  49. {
  50. FILE* f;
  51. int fd = 0;
  52. if(match(filename, "STDIN") || match(filename, "tape_01"))
  53. {
  54. fd = 0x1100;
  55. }
  56. else if(match(filename, "STDOUT") || match(filename, "tape_02"))
  57. {
  58. fd = 0x1101;
  59. }
  60. if('w' == mode[0])
  61. { /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
  62. f = open_write(fd);
  63. }
  64. else
  65. { /* Everything else is a read */
  66. f = open_read(fd);
  67. }
  68. /* Negative numbers are error codes */
  69. if(0 > f)
  70. {
  71. return 0;
  72. }
  73. return f;
  74. }