run.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "timer.h"
  7. #include "vga.h"
  8. #define PROGRAM_LOAD_ADDR 0x21000
  9. #define PROGRAM_LOAD_MAX 0x10000
  10. #define NAME_BUF_SIZE 12
  11. static void to_8_3(const char *in, unsigned int in_len, char *out)
  12. {
  13. unsigned int i, j, dot;
  14. for (i = 0; i < in_len && in[i] == ' '; i++)
  15. ;
  16. dot = i;
  17. while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
  18. dot++;
  19. j = 0;
  20. while (i < dot && j < 8) {
  21. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  22. i++;
  23. }
  24. while (j < 8)
  25. out[j++] = ' ';
  26. if (i < in_len && in[i] == '.')
  27. i++;
  28. while (i < in_len && in[i] != ' ' && j < 11) {
  29. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  30. i++;
  31. }
  32. while (j < 11)
  33. out[j++] = ' ';
  34. out[11] = '\0';
  35. }
  36. void program_run_main(const char *filename)
  37. {
  38. char name_8_3[NAME_BUF_SIZE];
  39. int n;
  40. if (!filename || !filename[0]) {
  41. io_putstr("run: usage run <filename>\n");
  42. return;
  43. }
  44. to_8_3(filename, 64, name_8_3);
  45. n = fat_init();
  46. if (n == -2) {
  47. io_putstr("run: disk read error (see above)\n");
  48. return;
  49. }
  50. if (n != 0) {
  51. io_putstr("run: no FAT filesystem found\n");
  52. return;
  53. }
  54. n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
  55. if (n == -2) {
  56. io_putstr("run: disk read error (see above)\n");
  57. return;
  58. }
  59. if (n <= 0) {
  60. io_putstr("run: file not found\n");
  61. return;
  62. }
  63. io_drain_keyboard();
  64. {
  65. struct prog_api api = {
  66. .draw_string_xy = fbtext_draw_string_xy,
  67. .get_cols = fbtext_get_cols,
  68. .get_rows = fbtext_get_rows,
  69. .poll_key = io_poll_key,
  70. .poll_key_with_hw = io_poll_key_with_hw,
  71. .get_fb_width = vga_fb_width,
  72. .get_fb_height = vga_fb_height,
  73. .clear_rgb = vga_clear_rgb,
  74. .put_pixel_rgb = vga_put_pixel_rgb,
  75. .get_ticks_ms = get_ticks_ms,
  76. };
  77. *(struct prog_api *)PROGRAM_API_ADDR = api;
  78. }
  79. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  80. }