1
0

BDOS.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Bart's Drive Operating System(BDOS)
  2. * A relatively simple OS that allows for some basic features including:
  3. * - Running a single user program with full hardware control
  4. * - Some system calls
  5. * - A basic shell
  6. * - Network loaders for sending data and controlling the FPGC
  7. * - HID fifo including USB keyboard driver
  8. * - BRFS Filesystem
  9. */
  10. /* List of reserved stuff:
  11. - Timer2 is used for USB keyboard polling, even when a user program is running
  12. - Socket 7 is used for netHID
  13. */
  14. /*
  15. * Defines (also might be used by included libraries below)
  16. */
  17. // As of writing, BCC assigns 4 memory addresses to ints, so we should use chars instead
  18. // However, this is confusing, so we typedef it to word, since that is what it basically is
  19. #define word char
  20. #define SYSCALL_RETVAL_ADDR 0x200000 // Address for system call communication with user program
  21. #define TEMP_ADDR 0x220000 // Address for (potentially) large temporary outputs/buffers
  22. #define RUN_ADDR 0x400000 // Address of loaded user program
  23. #define NETWORK_LOCAL_IP 213 // local IP address (last byte)
  24. #define MAX_PATH_LENGTH 127 // Max length of a file path
  25. #define BDOS_DEFAULT_BLOCKS 1024 // Default number of blocks for the BRFS filesystem
  26. #define BDOS_DEFAULT_BLOCK_SIZE 128 // Default number of words per block for the BRFS filesystem
  27. // Interrupt IDs for interrupt handler
  28. #define INTID_TIMER1 0x1
  29. #define INTID_TIMER2 0x2
  30. #define INTID_UART0 0x3
  31. #define INTID_GPU 0x4
  32. #define INTID_TIMER3 0x5
  33. #define INTID_PS2 0x6
  34. #define INTID_UART1 0x7
  35. #define INTID_UART2 0x8
  36. // System call IDs
  37. #define SYSCALL_FIFO_AVAILABLE 1
  38. #define SYSCALL_FIFO_READ 2
  39. #define SYSCALL_PRINT_C_CONSOLE 3
  40. #define SYSCALL_GET_ARGS 4 // Get arguments for executed program
  41. #define SYSCALL_GET_PATH 5 // Get OS path
  42. #define SYSCALL_GET_USB_KB_BUF 6 // Get the 8 bytes of the USB keyboard buffer
  43. /*
  44. * Global vars (also might be used by included libraries below)
  45. */
  46. // Flag that indicates whether a user program is running
  47. word bdos_userprogram_running = 0;
  48. /*
  49. * Included libraries
  50. */
  51. // Data includes
  52. #include "data/ASCII_BW.c"
  53. #include "data/PS2SCANCODES.c"
  54. #include "data/USBSCANCODES.c"
  55. // Code includes
  56. #include "lib/stdlib.c"
  57. #include "lib/math.c"
  58. #include "lib/gfx.c"
  59. #include "lib/hidfifo.c"
  60. #include "lib/ps2.c"
  61. #include "lib/brfs.c"
  62. #include "lib/shell.c"
  63. #include "lib/usbkeyboard.c"
  64. #include "lib/wiz5500.c"
  65. #include "lib/netloader.c"
  66. #include "lib/nethid.c"
  67. #include "lib/spiflash.c"
  68. /*
  69. * Functions
  70. */
  71. /**
  72. * Initialize the BRFS filesystem
  73. * Returns 1 if successful, 0 if not
  74. */
  75. word bdos_init_brfs()
  76. {
  77. // Initialize the SPI flash
  78. spiflash_init();
  79. // Try to read the filesystem from the SPI flash
  80. GFX_PrintConsole("Reading BRFS from SPI flash...\n");
  81. if (brfs_read_from_flash())
  82. {
  83. GFX_PrintConsole("BRFS read from flash\n");
  84. }
  85. else
  86. {
  87. GFX_PrintConsole("Could not read BRFS from flash!\n");
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. // Clears all VRAM
  93. // and copies the default ASCII pattern table and palette table
  94. // also resets the cursor
  95. void bdos_init_vram()
  96. {
  97. GFX_initVram();
  98. GFX_copyPaletteTable((word)DATA_PALETTE_DEFAULT);
  99. GFX_copyPatternTable((word)DATA_ASCII_DEFAULT);
  100. GFX_cursor = 0;
  101. }
  102. // Initialize the W5500
  103. void bdos_init_network()
  104. {
  105. word ip_addr[4] = {192, 168, 0, NETWORK_LOCAL_IP};
  106. word gateway_addr[4] = {192, 168, 0, 1};
  107. word mac_addr[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0x24, 0x64};
  108. word sub_mask[4] = {255, 255, 255, 0};
  109. wiz_Init(ip_addr, gateway_addr, mac_addr, sub_mask);
  110. }
  111. // Restores certain things when returning from a user program
  112. void bdos_restore()
  113. {
  114. // restore graphics (trying to keep text in window plane)
  115. GFX_copyPaletteTable((word)DATA_PALETTE_DEFAULT);
  116. GFX_copyPatternTable((word)DATA_ASCII_DEFAULT);
  117. GFX_clearBGtileTable();
  118. GFX_clearBGpaletteTable();
  119. GFX_clearWindowpaletteTable();
  120. GFX_clearSprites();
  121. // restore netloader
  122. NETLOADER_init(NETLOADER_SOCKET);
  123. }
  124. // Main BDOS code
  125. int main()
  126. {
  127. uprintln("BDOS_INIT");
  128. bdos_userprogram_running = 0; // Indicate that no user program is running
  129. bdos_init_vram();
  130. GFX_PrintConsole("VRAM initialized\n"); // Print to console now VRAM is initialized
  131. GFX_PrintConsole("Starting BDOS\n");
  132. GFX_PrintConsole("Initalizing network\n");
  133. bdos_init_network();
  134. GFX_PrintConsole("Initalizing netloader\n");
  135. NETLOADER_init(NETLOADER_SOCKET);
  136. GFX_PrintConsole("Initalizing netHID\n");
  137. NETHID_init(NETHID_SOCKET);
  138. GFX_PrintConsole("Initalizing USB keyboard\n");
  139. USBkeyboard_init();
  140. GFX_PrintConsole("Initalizing filesystem\n");
  141. if (!bdos_init_brfs())
  142. {
  143. GFX_PrintConsole("Error initializing FS\n");
  144. return 0;
  145. }
  146. GFX_PrintConsole("Initalizing shell\n");
  147. shell_init();
  148. // Main loop
  149. while (1)
  150. {
  151. shell_loop(); // Update shell state
  152. NETLOADER_loop(NETLOADER_SOCKET); // Update netloader state
  153. }
  154. return 1;
  155. }
  156. // System call handler
  157. // Returns at the same address it reads the command from
  158. void syscall()
  159. {
  160. word *p = (word *)SYSCALL_RETVAL_ADDR;
  161. word ID = p[0];
  162. switch (ID)
  163. {
  164. case SYSCALL_FIFO_AVAILABLE:
  165. p[0] = HID_FifoAvailable();
  166. break;
  167. case SYSCALL_FIFO_READ:
  168. p[0] = HID_FifoRead();
  169. break;
  170. case SYSCALL_PRINT_C_CONSOLE:
  171. GFX_PrintcConsole(p[1]);
  172. p[0] = 0;
  173. break;
  174. case SYSCALL_GET_ARGS:
  175. p[0] = 0; // TODO: implement
  176. break;
  177. case SYSCALL_GET_PATH:
  178. p[0] = 0; // TODO: implement
  179. break;
  180. case SYSCALL_GET_USB_KB_BUF:
  181. p[0] = USBkeyboard_buffer_parsed;
  182. break;
  183. default:
  184. p[0] = 0;
  185. break;
  186. }
  187. }
  188. // Interrupt handler
  189. void interrupt()
  190. {
  191. // Handle BDOS interrupts
  192. int i = getIntID();
  193. switch (i)
  194. {
  195. case INTID_TIMER1:
  196. timer1Value = 1; // Notify ending of timer1
  197. break;
  198. case INTID_TIMER2:
  199. USBkeyboard_HandleInterrupt(); // Handle USB keyboard interrupt
  200. break;
  201. case INTID_UART0:
  202. break;
  203. case INTID_GPU:
  204. if (NETHID_isInitialized == 1)
  205. {
  206. // Check using CS if we are not interrupting any critical access to the W5500
  207. word *spi3ChipSelect = (word *)0xC02732; // TODO: use a define for this address
  208. if (*spi3ChipSelect == 1)
  209. {
  210. NETHID_loop(NETHID_SOCKET); // Look for an input sent to netHID
  211. }
  212. }
  213. break;
  214. case INTID_TIMER3:
  215. break;
  216. case INTID_PS2:
  217. PS2_HandleInterrupt(); // Handle PS2 interrupt
  218. break;
  219. case INTID_UART1:
  220. break;
  221. case INTID_UART2:
  222. break;
  223. }
  224. // Handle user program interrupts
  225. if (bdos_userprogram_running)
  226. {
  227. // Call interrupt() of user program
  228. asm(
  229. "; backup registers\n"
  230. "push r1\n"
  231. "push r2\n"
  232. "push r3\n"
  233. "push r4\n"
  234. "push r5\n"
  235. "push r6\n"
  236. "push r7\n"
  237. "push r8\n"
  238. "push r9\n"
  239. "push r10\n"
  240. "push r11\n"
  241. "push r12\n"
  242. "push r13\n"
  243. "push r14\n"
  244. "push r15\n"
  245. "savpc r1\n"
  246. "push r1\n"
  247. "jump 0x400001\n"
  248. "; restore registers\n"
  249. "pop r15\n"
  250. "pop r14\n"
  251. "pop r13\n"
  252. "pop r12\n"
  253. "pop r11\n"
  254. "pop r10\n"
  255. "pop r9\n"
  256. "pop r8\n"
  257. "pop r7\n"
  258. "pop r6\n"
  259. "pop r5\n"
  260. "pop r4\n"
  261. "pop r3\n"
  262. "pop r2\n"
  263. "pop r1\n");
  264. return;
  265. }
  266. else
  267. {
  268. // Code to only run when not running a user program
  269. }
  270. }