SYS.C 3.1 KB

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