sys.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * Contains system functions for user programs
  3. * Contains code for system calls and interrupt handling
  4. */
  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. #define MAX_PATH_LENGTH 127
  15. #define SYSCALL_RETVAL_ADDR 0x200000
  16. // System call IDs
  17. #define SYS_HID_CHECKFIFO 1
  18. #define SYS_HID_READFIFO 2
  19. #define SYS_BDOS_PRINTC 3
  20. #define SYS_BDOS_PRINT 4
  21. #define SYS_FS_OPEN 5
  22. #define SYS_FS_CLOSE 6
  23. #define SYS_FS_READ 7
  24. #define SYS_FS_WRITE 8
  25. #define SYS_FS_SETCURSOR 9
  26. #define SYS_FS_GETCURSOR 10
  27. #define SYS_FS_DELETE 11
  28. #define SYS_FS_MKDIR 12
  29. #define SYS_FS_MKFILE 13
  30. #define SYS_FS_STAT 14
  31. #define SYS_FS_READDIR 15
  32. #define SYS_FS_GETCWD 16
  33. // Syscalls 17-19 are reserved for future use
  34. #define SYS_SHELL_ARGC 20
  35. #define SYS_SHELL_ARGV 21
  36. #define SYS_USB_KB_BUF 99
  37. /**
  38. * Returns the interrupt ID
  39. */
  40. word get_int_id()
  41. {
  42. word retval = 0;
  43. asm(
  44. "readintid r2 ;reads interrupt id to r2\n"
  45. "write -4 r14 r2 ;write to stack to return\n"
  46. );
  47. return retval;
  48. }
  49. /**
  50. * Executes system call to BDOS
  51. * Argument specifies the system call ID
  52. * Returns the address of the return value
  53. */
  54. word* syscall(word ID)
  55. {
  56. word* p = (word*) SYSCALL_RETVAL_ADDR;
  57. *p = ID;
  58. asm("push r1\n"
  59. "push r2\n"
  60. "push r3\n"
  61. "push r4\n"
  62. "push r5\n"
  63. "push r6\n"
  64. "push r7\n"
  65. "push r8\n"
  66. "push r9\n"
  67. "push r10\n"
  68. "push r11\n"
  69. "push r12\n"
  70. "push r13\n"
  71. "push r14\n"
  72. "push r15\n"
  73. "savpc r1\n"
  74. "push r1\n"
  75. "jump 4\n"
  76. "pop r15\n"
  77. "pop r14\n"
  78. "pop r13\n"
  79. "pop r12\n"
  80. "pop r11\n"
  81. "pop r10\n"
  82. "pop r9\n"
  83. "pop r8\n"
  84. "pop r7\n"
  85. "pop r6\n"
  86. "pop r5\n"
  87. "pop r4\n"
  88. "pop r3\n"
  89. "pop r2\n"
  90. "pop r1\n");
  91. return p;
  92. }
  93. /**
  94. * Exits the user program and returns to BDOS in a somewhat controlled way
  95. */
  96. void exit()
  97. {
  98. asm("ccache\n");
  99. asm("jump Return_BDOS\n");
  100. }
  101. /**
  102. * Returns 1 if the HID buffer is not empty
  103. */
  104. word hid_checkfifo()
  105. {
  106. char* p = syscall(SYS_HID_CHECKFIFO);
  107. return p[0];
  108. }
  109. /**
  110. * Reads a character from the HID buffer
  111. */
  112. word hid_fiforead()
  113. {
  114. char* p = syscall(SYS_HID_READFIFO);
  115. return p[0];
  116. }
  117. /**
  118. * Prints a character on the BDOS console
  119. */
  120. void bdos_printc(char c)
  121. {
  122. char* p = (char*) SYSCALL_RETVAL_ADDR;
  123. p[1] = c;
  124. syscall(SYS_BDOS_PRINTC);
  125. }
  126. /**
  127. * Prints a string on the BDOS console
  128. */
  129. void bdos_print(char* c)
  130. {
  131. char* p = (char*) SYSCALL_RETVAL_ADDR;
  132. p[1] = (char)c;
  133. syscall(SYS_BDOS_PRINT);
  134. }
  135. /**
  136. * Prints a string with newline on the BDOS console
  137. */
  138. void bdos_println(char* str)
  139. {
  140. bdos_print(str);
  141. bdos_printc('\n');
  142. }
  143. /**
  144. * Prints a decimal on the BDOS console
  145. */
  146. void bdos_printdec(word i)
  147. {
  148. char buffer[12];
  149. if (i < 0)
  150. {
  151. buffer[0] = '-';
  152. itoa(MATH_abs(i), &buffer[1]);
  153. }
  154. else
  155. {
  156. itoa(i, buffer);
  157. }
  158. bdos_print(buffer);
  159. }
  160. /**
  161. * Prints a decimal with newline on the BDOS console
  162. */
  163. void bdos_printdecln(word i)
  164. {
  165. bdos_printdec(i);
  166. bdos_printc('\n');
  167. }
  168. /**
  169. * Prints a hexadecimal on the BDOS console
  170. */
  171. void bdos_printhex(word i)
  172. {
  173. char buffer[11];
  174. itoah(i, buffer);
  175. bdos_print(buffer);
  176. }
  177. /**
  178. * Opens a file in the filesystem
  179. */
  180. word fs_open(char* filename)
  181. {
  182. char* p = (char*) SYSCALL_RETVAL_ADDR;
  183. p[1] = (char) filename;
  184. syscall(SYS_FS_OPEN);
  185. return p[0];
  186. }
  187. /**
  188. * Closes a file in the filesystem
  189. */
  190. word fs_close(word fp)
  191. {
  192. char* p = (char*) SYSCALL_RETVAL_ADDR;
  193. p[1] = fp;
  194. syscall(SYS_FS_CLOSE);
  195. return p[0];
  196. }
  197. /**
  198. * Reads from a file in the filesystem
  199. */
  200. word fs_read(word fp, char* buffer, word len)
  201. {
  202. char* p = (char*) SYSCALL_RETVAL_ADDR;
  203. p[1] = fp;
  204. p[2] = (char) buffer;
  205. p[3] = len;
  206. syscall(SYS_FS_READ);
  207. return p[0];
  208. }
  209. /**
  210. * Writes to a file in the filesystem
  211. */
  212. word fs_write(word fp, char* buffer, word len)
  213. {
  214. char* p = (char*) SYSCALL_RETVAL_ADDR;
  215. p[1] = fp;
  216. p[2] = (char) buffer;
  217. p[3] = len;
  218. syscall(SYS_FS_WRITE);
  219. return p[0];
  220. }
  221. /**
  222. * Sets the cursor position in the filesystem
  223. */
  224. word fs_setcursor(word fp, word pos)
  225. {
  226. char* p = (char*) SYSCALL_RETVAL_ADDR;
  227. p[1] = fp;
  228. p[2] = pos;
  229. syscall(SYS_FS_SETCURSOR);
  230. return p[0];
  231. }
  232. /**
  233. * Gets the cursor position in the filesystem
  234. */
  235. word fs_getcursor(word fp)
  236. {
  237. char* p = (char*) SYSCALL_RETVAL_ADDR;
  238. p[1] = fp;
  239. syscall(SYS_FS_GETCURSOR);
  240. return p[0];
  241. }
  242. /**
  243. * Deletes a file in the filesystem
  244. */
  245. word fs_delete(char* filename)
  246. {
  247. char* p = (char*) SYSCALL_RETVAL_ADDR;
  248. p[1] = (char) filename;
  249. syscall(SYS_FS_DELETE);
  250. return p[0];
  251. }
  252. /**
  253. * Creates a directory in the filesystem
  254. */
  255. word fs_mkdir(char* dirname)
  256. {
  257. char* p = (char*) SYSCALL_RETVAL_ADDR;
  258. p[1] = (char) dirname;
  259. syscall(SYS_FS_MKDIR);
  260. return p[0];
  261. }
  262. /**
  263. * Creates a file in the filesystem
  264. */
  265. word fs_mkfile(char* filename)
  266. {
  267. char* p = (char*) SYSCALL_RETVAL_ADDR;
  268. p[1] = (char) filename;
  269. syscall(SYS_FS_MKFILE);
  270. return p[0];
  271. }
  272. /**
  273. * Gets the status of a file in the filesystem
  274. */
  275. word fs_stat(char* filename)
  276. {
  277. char* p = (char*) SYSCALL_RETVAL_ADDR;
  278. p[1] = (char) filename;
  279. syscall(SYS_FS_STAT);
  280. return p[0];
  281. }
  282. /**
  283. * Lists the contents of a directory in the filesystem
  284. */
  285. word fs_readdir(char* dirname)
  286. {
  287. char* p = (char*) SYSCALL_RETVAL_ADDR;
  288. p[1] = (char) dirname;
  289. syscall(SYS_FS_READDIR);
  290. return p[0];
  291. }
  292. /**
  293. * Gets the current working directory in the filesystem
  294. */
  295. char* fs_getcwd()
  296. {
  297. char* p = (char*) SYSCALL_RETVAL_ADDR;
  298. syscall(SYS_FS_GETCWD);
  299. return (char*) p[0];
  300. }
  301. /**
  302. * Returns the number of command line arguments
  303. */
  304. word shell_argc()
  305. {
  306. char* p = (char*) SYSCALL_RETVAL_ADDR;
  307. syscall(SYS_SHELL_ARGC);
  308. return p[0];
  309. }
  310. /**
  311. * Returns the command line arguments
  312. */
  313. char* shell_argv()
  314. {
  315. char* p = (char*) SYSCALL_RETVAL_ADDR;
  316. syscall(SYS_SHELL_ARGV);
  317. return p[0];
  318. }
  319. /**
  320. * Returns 1 if key is being held on USB keyboard
  321. */
  322. word bdos_usbkey_held(word c)
  323. {
  324. word* p = syscall(SYS_USB_KB_BUF);
  325. word* usbKeyBuffer = (char*) p[0];
  326. word i;
  327. for (i = 0; i < 8; i++)
  328. {
  329. if (usbKeyBuffer[i] == c)
  330. {
  331. return 1;
  332. }
  333. }
  334. return 0;
  335. }