1
0

sys.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Contains System Call functions
  3. */
  4. #define SYSCALL_RETVAL_ADDR 0x200000
  5. // Interrupt IDs for interrupt handler
  6. #define INTID_TIMER1 0x1
  7. #define INTID_TIMER2 0x2
  8. #define INTID_UART0 0x3
  9. #define INTID_GPU 0x4
  10. #define INTID_TIMER3 0x5
  11. #define INTID_PS2 0x6
  12. #define INTID_UART1 0x7
  13. #define INTID_UART2 0x8
  14. // executes system call to BDOS
  15. // ID is written to the same location as the output of the system call
  16. // at address SYSCALL_RETVAL_ADDR
  17. // This address is also returned
  18. word* syscall(word ID)
  19. {
  20. word* p = (word*) SYSCALL_RETVAL_ADDR;
  21. *p = ID;
  22. asm("push r1\n"
  23. "push r2\n"
  24. "push r3\n"
  25. "push r4\n"
  26. "push r5\n"
  27. "push r6\n"
  28. "push r7\n"
  29. "push r8\n"
  30. "push r9\n"
  31. "push r10\n"
  32. "push r11\n"
  33. "push r12\n"
  34. "push r13\n"
  35. "push r14\n"
  36. "push r15\n"
  37. "savpc r1\n"
  38. "push r1\n"
  39. "jump 4\n"
  40. "pop r15\n"
  41. "pop r14\n"
  42. "pop r13\n"
  43. "pop r12\n"
  44. "pop r11\n"
  45. "pop r10\n"
  46. "pop r9\n"
  47. "pop r8\n"
  48. "pop r7\n"
  49. "pop r6\n"
  50. "pop r5\n"
  51. "pop r4\n"
  52. "pop r3\n"
  53. "pop r2\n"
  54. "pop r1\n");
  55. return p;
  56. }
  57. void BDOS_PrintcConsole(char c)
  58. {
  59. char* p = (char*) SYSCALL_RETVAL_ADDR;
  60. p[1] = c;
  61. syscall(3);
  62. }
  63. // Prints string on BDOS console untill terminator
  64. // Does not add newline at end
  65. void BDOS_PrintConsole(char* str)
  66. {
  67. char chr = *str; // first character of str
  68. while (chr != 0) // continue until null value
  69. {
  70. BDOS_PrintcConsole(chr);
  71. str++; // go to next character address
  72. chr = *str; // get character from address
  73. }
  74. }
  75. // Returns command line args
  76. char* BDOS_GetArgs()
  77. {
  78. char* p = syscall(4);
  79. return (char*) p[0];
  80. }
  81. // Writes command line argument n into buf
  82. // Arg 0 is the command itself
  83. void BDOS_GetArgN(word n, char* buf)
  84. {
  85. char* args = BDOS_GetArgs();
  86. word i = 0;
  87. word bufi = 0;
  88. word currentArg = 0;
  89. char prevChar = 0;
  90. buf[0] = 0;
  91. while (args[i] != 0)
  92. {
  93. // new argument
  94. if (args[i] == ' ' && prevChar != ' ')
  95. {
  96. currentArg++;
  97. }
  98. if (args[i] != ' ')
  99. {
  100. if (currentArg == n)
  101. {
  102. buf[bufi] = args[i];
  103. bufi++;
  104. }
  105. }
  106. prevChar = args[i];
  107. i++;
  108. }
  109. buf[bufi] = 0; // terminate
  110. }
  111. // Returns BDOS current path
  112. char* BDOS_GetPath()
  113. {
  114. word* p = syscall(5);
  115. return (char*) p[0];
  116. }