run.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* run command: load raw binary from FAT floppy to 0x21000 and execute.
  2. * load: read raw bytes from serial into 0x21000 until ESC; then run serial to execute.
  3. * run serial: execute last serial-loaded program. */
  4. #include "fat.h"
  5. #include "fbtext.h"
  6. #include "io.h"
  7. #include "prog_api.h"
  8. #include "serial.h"
  9. #include "timer.h"
  10. #include "vga.h"
  11. #define PROGRAM_LOAD_ADDR 0x21000
  12. #define PROGRAM_LOAD_MAX 0x10000
  13. #define NAME_BUF_SIZE 12
  14. static unsigned int serial_load_size;
  15. /* Trim N bytes from start and/or end of serial stream so stored data matches file. Set via "serial trim <first> <last>". */
  16. static unsigned int serial_trim_first = 0;
  17. static unsigned int serial_trim_last = 9;
  18. static void to_8_3(const char *in, unsigned int in_len, char *out)
  19. {
  20. unsigned int i, j, dot;
  21. for (i = 0; i < in_len && in[i] == ' '; i++)
  22. ;
  23. dot = i;
  24. while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
  25. dot++;
  26. j = 0;
  27. while (i < dot && j < 8) {
  28. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  29. i++;
  30. }
  31. while (j < 8)
  32. out[j++] = ' ';
  33. if (i < in_len && in[i] == '.')
  34. i++;
  35. while (i < in_len && in[i] != ' ' && j < 11) {
  36. out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  37. i++;
  38. }
  39. while (j < 11)
  40. out[j++] = ' ';
  41. out[11] = '\0';
  42. }
  43. static int is_serial_name(const char *filename)
  44. {
  45. static const char rest[] = "erial";
  46. unsigned int i;
  47. if (!filename || !filename[0])
  48. return 0;
  49. if (filename[0] == '-' && filename[1] == '\0')
  50. return 1;
  51. if (filename[0] != 's' && filename[0] != 'S')
  52. return 0;
  53. for (i = 1; i < 6 && filename[i]; i++) {
  54. char c = filename[i];
  55. if (c != rest[i-1] && c != (char)(rest[i-1] - 32))
  56. return 0;
  57. }
  58. return (i == 6 && (filename[6] == '\0' || filename[6] == ' ' || filename[6] == '.'));
  59. }
  60. static void run_at_load_addr(void)
  61. {
  62. io_drain_keyboard();
  63. {
  64. struct prog_api api = {
  65. .draw_string_xy = fbtext_draw_string_xy,
  66. .get_cols = fbtext_get_cols,
  67. .get_rows = fbtext_get_rows,
  68. .poll_key = io_poll_key,
  69. .poll_key_with_hw = io_poll_key_with_hw,
  70. .get_fb_width = vga_fb_width,
  71. .get_fb_height = vga_fb_height,
  72. .clear_rgb = vga_clear_rgb,
  73. .put_pixel_rgb = vga_put_pixel_rgb,
  74. .get_ticks_ms = get_ticks_ms,
  75. };
  76. *(struct prog_api *)PROGRAM_API_ADDR = api;
  77. }
  78. ((void (*)(void))PROGRAM_LOAD_ADDR)();
  79. }
  80. void program_run_serial_main(void)
  81. {
  82. if (serial_load_size == 0) {
  83. io_putstr("run serial: no file loaded (use load first)\n");
  84. return;
  85. }
  86. run_at_load_addr();
  87. }
  88. /* CRC-32 (same as POSIX cksum) for verifying serial-loaded data. Compare with: cksum <file> on Linux. */
  89. static unsigned int crc32_table[256];
  90. static void crc32_init_table(void)
  91. {
  92. unsigned int i, j;
  93. unsigned int crc;
  94. for (i = 0; i < 256; i++) {
  95. crc = i;
  96. for (j = 0; j < 8; j++)
  97. crc = (crc >> 1) ^ (0xEDB88320u & -(crc & 1));
  98. crc32_table[i] = crc;
  99. }
  100. }
  101. /* Same as POSIX cksum: data bytes, then length as 4 bytes LSB first, then ones' complement. */
  102. static unsigned int crc32_cksum(const unsigned char *data, unsigned int len)
  103. {
  104. unsigned int crc = 0;
  105. unsigned int i;
  106. for (i = 0; i < len; i++)
  107. crc = (crc >> 8) ^ crc32_table[(crc ^ data[i]) & 0xFF];
  108. crc = (crc >> 8) ^ crc32_table[(crc ^ (len & 0xFF)) & 0xFF];
  109. crc = (crc >> 8) ^ crc32_table[(crc ^ ((len >> 8) & 0xFF)) & 0xFF];
  110. crc = (crc >> 8) ^ crc32_table[(crc ^ ((len >> 16) & 0xFF)) & 0xFF];
  111. crc = (crc >> 8) ^ crc32_table[(crc ^ ((len >> 24) & 0xFF)) & 0xFF];
  112. return crc ^ 0xFFFFFFFFu;
  113. }
  114. void program_serial_checksum_main(void)
  115. {
  116. static int table_done;
  117. unsigned int crc;
  118. const unsigned char *buf = (const unsigned char *)PROGRAM_LOAD_ADDR;
  119. if (serial_load_size == 0) {
  120. io_putstr("No serial data (use load first). Compare with: cksum <file> on host.\n");
  121. return;
  122. }
  123. if (!table_done) {
  124. crc32_init_table();
  125. table_done = 1;
  126. }
  127. crc = crc32_cksum(buf, serial_load_size);
  128. io_putdec(crc);
  129. io_putstr(" ");
  130. io_putdec(serial_load_size);
  131. io_putstr("\n");
  132. }
  133. void program_serial_trim_main(unsigned int first, unsigned int last)
  134. {
  135. serial_trim_first = first;
  136. serial_trim_last = last;
  137. io_putstr("serial trim: skip first ");
  138. io_putdec(first);
  139. io_putstr(", drop last ");
  140. io_putdec(last);
  141. io_putstr(" bytes.\n");
  142. }
  143. void program_load_main(void)
  144. {
  145. int c;
  146. unsigned char *dst = (unsigned char *)PROGRAM_LOAD_ADDR;
  147. serial_init();
  148. serial_drain(); /* discard any stale bytes */
  149. /* Discard first N bytes (e.g. tty sends when port opened). */
  150. {
  151. unsigned int i;
  152. for (i = 0; i < serial_trim_first; i++) {
  153. if (serial_getchar(500) < 0)
  154. break;
  155. }
  156. }
  157. serial_load_size = 0;
  158. io_putstr("Reading from serial - wait for file to finish, then press ESC. On host: cat <file> > /dev/ttyS0\n");
  159. for (;;) {
  160. if (io_poll_key() == 0x1B)
  161. break;
  162. c = serial_getchar(50);
  163. if (c >= 0) {
  164. dst[serial_load_size] = (unsigned char)c;
  165. serial_load_size++;
  166. if (serial_load_size >= PROGRAM_LOAD_MAX)
  167. break;
  168. }
  169. }
  170. if (serial_load_size >= serial_trim_last)
  171. serial_load_size -= serial_trim_last;
  172. io_putstr("Received ");
  173. io_putdec(serial_load_size);
  174. io_putstr(" bytes. Run with: run serial\n");
  175. }
  176. /* Simple serial link test (no XMODEM): send a string, then listen and print RX for 15s. */
  177. void program_serial_test_main(void)
  178. {
  179. unsigned int t0 = get_ticks_ms();
  180. const unsigned int test_duration_ms = 15000;
  181. const unsigned int tx_interval_ms = 1000;
  182. unsigned int last_tx = 0;
  183. int c;
  184. serial_init();
  185. io_putstr("Serial test in 3s - on host run: cat /dev/ttyS0 (then you should see 'Serial OK' every 1s).\n");
  186. while ((get_ticks_ms() - t0) < 3000)
  187. ; /* 3s to start cat on the other side */
  188. t0 = get_ticks_ms();
  189. io_putstr("Sending and listening 15s. To test RX: echo hello > /dev/ttyS0\n");
  190. while ((get_ticks_ms() - t0) < test_duration_ms) {
  191. if ((get_ticks_ms() - last_tx) >= tx_interval_ms) {
  192. serial_putstr("Serial OK\n");
  193. last_tx = get_ticks_ms();
  194. }
  195. c = serial_getchar(200);
  196. if (c >= 0)
  197. io_putchar((char)c);
  198. }
  199. io_putstr("Serial test done.\n");
  200. }
  201. /* Loopback test: short COM1 TX to RX (DB9 pin 2 to pin 3). If we receive what we send,
  202. * the UART works on this machine; if not, cable/host may be the issue. */
  203. void program_serial_loopback_main(void)
  204. {
  205. unsigned int i;
  206. int got;
  207. const unsigned char test_byte = 0x55;
  208. serial_init();
  209. io_putstr("Loopback: short COM1 TX to RX (DB9: pin 2 to pin 3). Press a key to test.\n");
  210. while (!io_poll_key())
  211. ;
  212. io_drain_keyboard();
  213. io_putstr("Sending 0x55 and reading for 1s...\n");
  214. for (i = 0; i < 20; i++)
  215. serial_putchar((char)test_byte);
  216. got = serial_getchar(1000);
  217. if (got == (int)test_byte) {
  218. io_putstr("Loopback OK - UART TX and RX work on this machine. Cable or host RX may be bad.\n");
  219. } else {
  220. io_putstr("No loopback (got ");
  221. if (got >= 0)
  222. io_puthex((unsigned int)got);
  223. else
  224. io_putstr("timeout");
  225. io_putstr("). Check short between pins 2 and 3, or UART.\n");
  226. }
  227. }
  228. void program_run_main(const char *filename)
  229. {
  230. char name_8_3[NAME_BUF_SIZE];
  231. int n;
  232. if (!filename || !filename[0]) {
  233. io_putstr("run: usage run <filename>\n");
  234. return;
  235. }
  236. if (is_serial_name(filename)) {
  237. program_run_serial_main();
  238. return;
  239. }
  240. to_8_3(filename, 64, name_8_3);
  241. n = fat_init();
  242. if (n == -2) {
  243. io_putstr("run: disk read error (see above)\n");
  244. return;
  245. }
  246. if (n != 0) {
  247. io_putstr("run: no FAT filesystem found\n");
  248. return;
  249. }
  250. n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
  251. if (n == -2) {
  252. io_putstr("run: disk read error (see above)\n");
  253. return;
  254. }
  255. if (n <= 0) {
  256. io_putstr("run: file not found\n");
  257. return;
  258. }
  259. run_at_load_addr();
  260. }