run.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* run command: load raw binary from FAT floppy to 0x21000 and execute. */
  2. #include "fat.h"
  3. #include "fbtext.h"
  4. #include "io.h"
  5. #include "prog_api.h"
  6. #define PROGRAM_LOAD_ADDR 0x21000
  7. #define PROGRAM_LOAD_MAX 0x10000
  8. #define NAME_BUF_SIZE 12
  9. static void to_8_3(const char *in, unsigned int in_len, char *out)
  10. {
  11. unsigned int i, j, dot;
  12. for (i = 0; i < in_len && in[i] == ' '; i++)
  13. ;
  14. dot = i;
  15. while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
  16. dot++;
  17. j = 0;
  18. while (i < dot && j < 8) {
  19. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  20. i++;
  21. }
  22. while (j < 8)
  23. out[j++] = ' ';
  24. if (i < in_len && in[i] == '.')
  25. i++;
  26. while (i < in_len && in[i] != ' ' && j < 11) {
  27. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  28. i++;
  29. }
  30. while (j < 11)
  31. out[j++] = ' ';
  32. out[11] = '\0';
  33. }
  34. void program_run_main(const char *filename)
  35. {
  36. char name_8_3[NAME_BUF_SIZE];
  37. int n;
  38. if (!filename || !filename[0]) {
  39. io_putstr("run: usage run <filename>\n");
  40. return;
  41. }
  42. to_8_3(filename, 64, name_8_3);
  43. n = fat_init();
  44. if (n == -2) {
  45. io_putstr("run: disk read error\n");
  46. return;
  47. }
  48. if (n != 0) {
  49. io_putstr("run: no FAT filesystem found\n");
  50. return;
  51. }
  52. n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
  53. if (n == -2) {
  54. io_putstr("run: disk read error\n");
  55. return;
  56. }
  57. if (n <= 0) {
  58. io_putstr("run: file not found\n");
  59. return;
  60. }
  61. {
  62. struct prog_api api = {
  63. .draw_string_xy = fbtext_draw_string_xy,
  64. .get_cols = fbtext_get_cols,
  65. .get_rows = fbtext_get_rows,
  66. .poll_key = io_poll_key,
  67. };
  68. *(struct prog_api *)PROGRAM_API_ADDR = api;
  69. }
  70. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  71. }