sys.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. void BDOS_PrintcConsole(char c)
  58. {
  59. char* p = (char*) SYSCALL_RETVAL_ADDR;
  60. p[1] = c;
  61. syscall(3);
  62. }
  63. // Prints string on BDOS console untill terminator
  64. // Does not add newline at end
  65. void BDOS_PrintConsole(char* str)
  66. {
  67. char chr = *str; // first character of str
  68. while (chr != 0) // continue until null value
  69. {
  70. BDOS_PrintcConsole(chr);
  71. str++; // go to next character address
  72. chr = *str; // get character from address
  73. }
  74. }
  75. void BDOS_PrintlnConsole(char* str)
  76. {
  77. BDOS_PrintConsole(str);
  78. BDOS_PrintcConsole('\n');
  79. }
  80. void BDOS_PrintDecConsole(word i)
  81. {
  82. char buffer[11];
  83. itoa(i, buffer);
  84. BDOS_PrintConsole(buffer);
  85. }
  86. // Returns command line args
  87. char* BDOS_GetArgs()
  88. {
  89. char* p = syscall(4);
  90. return (char*) p[0];
  91. }
  92. // Writes command line argument n into buf
  93. // Arg 0 is the command itself
  94. void BDOS_GetArgN(word n, char* buf)
  95. {
  96. char* args = BDOS_GetArgs();
  97. word i = 0;
  98. word bufi = 0;
  99. word currentArg = 0;
  100. char prevChar = 0;
  101. buf[0] = 0;
  102. while (args[i] != 0)
  103. {
  104. // new argument
  105. if (args[i] == ' ' && prevChar != ' ')
  106. {
  107. currentArg++;
  108. }
  109. if (args[i] != ' ')
  110. {
  111. if (currentArg == n)
  112. {
  113. buf[bufi] = args[i];
  114. bufi++;
  115. }
  116. }
  117. prevChar = args[i];
  118. i++;
  119. }
  120. buf[bufi] = 0; // terminate
  121. }
  122. // Returns BDOS current path
  123. char* BDOS_GetPath()
  124. {
  125. word* p = syscall(5);
  126. return (char*) p[0];
  127. }