SYS.C 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // quit the user program and return to BDOS in a somewhat controlled way
  58. void exit()
  59. {
  60. asm("ccache\n");
  61. asm("jump Return_BDOS\n");
  62. }
  63. word HID_FifoAvailable()
  64. {
  65. char* p = syscall(1);
  66. return p[0];
  67. }
  68. word HID_FifoRead()
  69. {
  70. char* p = syscall(2);
  71. return p[0];
  72. }
  73. void BDOS_PrintcConsole(char c)
  74. {
  75. char* p = (char*) SYSCALL_RETVAL_ADDR;
  76. p[1] = c;
  77. syscall(3);
  78. }
  79. // Prints string on BDOS console untill terminator
  80. // Does not add newline at end
  81. void BDOS_PrintConsole(char* str)
  82. {
  83. char chr = *str; // first character of str
  84. while (chr != 0) // continue until null value
  85. {
  86. BDOS_PrintcConsole(chr);
  87. str++; // go to next character address
  88. chr = *str; // get character from address
  89. }
  90. }
  91. void BDOS_PrintlnConsole(char* str)
  92. {
  93. BDOS_PrintConsole(str);
  94. BDOS_PrintcConsole('\n');
  95. }
  96. void BDOS_PrintDecConsole(word i)
  97. {
  98. char buffer[12];
  99. if (i < 0)
  100. {
  101. buffer[0] = '-';
  102. itoa(MATH_abs(i), &buffer[1]);
  103. }
  104. else
  105. {
  106. itoa(i, buffer);
  107. }
  108. BDOS_PrintConsole(buffer);
  109. }
  110. void BDOS_PrintlnDecConsole(word i)
  111. {
  112. BDOS_PrintDecConsole(i);
  113. BDOS_PrintcConsole('\n');
  114. }
  115. void BDOS_PrintHexConsole(word i)
  116. {
  117. char buffer[11];
  118. itoah(i, buffer);
  119. BDOS_PrintConsole(buffer);
  120. }
  121. // Returns command line args
  122. char* BDOS_GetArgs()
  123. {
  124. char* p = syscall(4);
  125. return (char*) p[0];
  126. }
  127. // Writes command line argument n into buf
  128. // Arg 0 is the command itself
  129. void BDOS_GetArgN(word n, char* buf)
  130. {
  131. char* args = BDOS_GetArgs();
  132. word i = 0;
  133. word bufi = 0;
  134. word currentArg = 0;
  135. char prevChar = 0;
  136. buf[0] = 0;
  137. while (args[i] != 0)
  138. {
  139. // new argument
  140. if (args[i] == ' ' && prevChar != ' ')
  141. {
  142. currentArg++;
  143. }
  144. if (args[i] != ' ')
  145. {
  146. if (currentArg == n)
  147. {
  148. buf[bufi] = args[i];
  149. bufi++;
  150. }
  151. }
  152. prevChar = args[i];
  153. i++;
  154. }
  155. buf[bufi] = 0; // terminate
  156. }
  157. // Returns BDOS current path
  158. char* BDOS_GetPath()
  159. {
  160. word* p = syscall(5);
  161. return (char*) p[0];
  162. }
  163. // Returns 1 if key is being held on USB keyboard
  164. word BDOS_USBkeyHeld(word c)
  165. {
  166. word* p = syscall(6);
  167. word* usbKeyBuffer = (char*) p[0];
  168. word i;
  169. for (i = 0; i < 8; i++)
  170. {
  171. if (usbKeyBuffer[i] == c)
  172. {
  173. return 1;
  174. }
  175. }
  176. return 0;
  177. }