1
0

SYS.C 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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[11];
  99. itoa(i, buffer);
  100. BDOS_PrintConsole(buffer);
  101. }
  102. void BDOS_PrintHexConsole(word i)
  103. {
  104. char buffer[11];
  105. itoah(i, buffer);
  106. BDOS_PrintConsole(buffer);
  107. }
  108. // Returns command line args
  109. char* BDOS_GetArgs()
  110. {
  111. char* p = syscall(4);
  112. return (char*) p[0];
  113. }
  114. // Writes command line argument n into buf
  115. // Arg 0 is the command itself
  116. void BDOS_GetArgN(word n, char* buf)
  117. {
  118. char* args = BDOS_GetArgs();
  119. word i = 0;
  120. word bufi = 0;
  121. word currentArg = 0;
  122. char prevChar = 0;
  123. buf[0] = 0;
  124. while (args[i] != 0)
  125. {
  126. // new argument
  127. if (args[i] == ' ' && prevChar != ' ')
  128. {
  129. currentArg++;
  130. }
  131. if (args[i] != ' ')
  132. {
  133. if (currentArg == n)
  134. {
  135. buf[bufi] = args[i];
  136. bufi++;
  137. }
  138. }
  139. prevChar = args[i];
  140. i++;
  141. }
  142. buf[bufi] = 0; // terminate
  143. }
  144. // Returns BDOS current path
  145. char* BDOS_GetPath()
  146. {
  147. word* p = syscall(5);
  148. return (char*) p[0];
  149. }
  150. // Returns 1 if key is being held on USB keyboard
  151. word BDOS_USBkeyHeld(word c)
  152. {
  153. word* p = syscall(6);
  154. word* usbKeyBuffer = (char*) p[0];
  155. word i;
  156. for (i = 0; i < 8; i++)
  157. {
  158. if (usbKeyBuffer[i] == c)
  159. {
  160. return 1;
  161. }
  162. }
  163. return 0;
  164. }