1
0

sys.c 6.5 KB

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