| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- /* run command: load raw binary from FAT floppy to 0x21000 and execute.
- * load: read raw bytes from serial into 0x21000 until ESC; then run serial to execute.
- * run serial: execute last serial-loaded program. */
- #include "fat.h"
- #include "fbtext.h"
- #include "io.h"
- #include "prog_api.h"
- #include "serial.h"
- #include "timer.h"
- #include "vga.h"
- #define PROGRAM_LOAD_ADDR 0x21000
- #define PROGRAM_LOAD_MAX 0x10000
- #define NAME_BUF_SIZE 12
- static unsigned int serial_load_size;
- /* Trim N bytes from start and/or end of serial stream so stored data matches file. Set via "serial trim <first> <last>". */
- static unsigned int serial_trim_first = 0;
- static unsigned int serial_trim_last = 9;
- static void to_8_3(const char *in, unsigned int in_len, char *out)
- {
- unsigned int i, j, dot;
- for (i = 0; i < in_len && in[i] == ' '; i++)
- ;
- dot = i;
- while (dot < in_len && in[dot] != '.' && in[dot] != ' ')
- dot++;
- j = 0;
- while (i < dot && j < 8) {
- out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
- i++;
- }
- while (j < 8)
- out[j++] = ' ';
- if (i < in_len && in[i] == '.')
- i++;
- while (i < in_len && in[i] != ' ' && j < 11) {
- out[j++] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
- i++;
- }
- while (j < 11)
- out[j++] = ' ';
- out[11] = '\0';
- }
- static int is_serial_name(const char *filename)
- {
- static const char rest[] = "erial";
- unsigned int i;
- if (!filename || !filename[0])
- return 0;
- if (filename[0] == '-' && filename[1] == '\0')
- return 1;
- if (filename[0] != 's' && filename[0] != 'S')
- return 0;
- for (i = 1; i < 6 && filename[i]; i++) {
- char c = filename[i];
- if (c != rest[i-1] && c != (char)(rest[i-1] - 32))
- return 0;
- }
- return (i == 6 && (filename[6] == '\0' || filename[6] == ' ' || filename[6] == '.'));
- }
- static void run_at_load_addr(void)
- {
- io_drain_keyboard();
- {
- struct prog_api api = {
- .draw_string_xy = fbtext_draw_string_xy,
- .get_cols = fbtext_get_cols,
- .get_rows = fbtext_get_rows,
- .poll_key = io_poll_key,
- .poll_key_with_hw = io_poll_key_with_hw,
- .get_fb_width = vga_fb_width,
- .get_fb_height = vga_fb_height,
- .clear_rgb = vga_clear_rgb,
- .put_pixel_rgb = vga_put_pixel_rgb,
- .get_ticks_ms = get_ticks_ms,
- };
- *(struct prog_api *)PROGRAM_API_ADDR = api;
- }
- ((void (*)(void))PROGRAM_LOAD_ADDR)();
- }
- void program_run_serial_main(void)
- {
- if (serial_load_size == 0) {
- io_putstr("run serial: no file loaded (use load first)\n");
- return;
- }
- run_at_load_addr();
- }
- /* CRC-32 (same as POSIX cksum) for verifying serial-loaded data. Compare with: cksum <file> on Linux. */
- static unsigned int crc32_table[256];
- static void crc32_init_table(void)
- {
- unsigned int i, j;
- unsigned int crc;
- for (i = 0; i < 256; i++) {
- crc = i;
- for (j = 0; j < 8; j++)
- crc = (crc >> 1) ^ (0xEDB88320u & -(crc & 1));
- crc32_table[i] = crc;
- }
- }
- /* Same as POSIX cksum: data bytes, then length as 4 bytes LSB first, then ones' complement. */
- static unsigned int crc32_cksum(const unsigned char *data, unsigned int len)
- {
- unsigned int crc = 0;
- unsigned int i;
- for (i = 0; i < len; i++)
- crc = (crc >> 8) ^ crc32_table[(crc ^ data[i]) & 0xFF];
- crc = (crc >> 8) ^ crc32_table[(crc ^ (len & 0xFF)) & 0xFF];
- crc = (crc >> 8) ^ crc32_table[(crc ^ ((len >> 8) & 0xFF)) & 0xFF];
- crc = (crc >> 8) ^ crc32_table[(crc ^ ((len >> 16) & 0xFF)) & 0xFF];
- crc = (crc >> 8) ^ crc32_table[(crc ^ ((len >> 24) & 0xFF)) & 0xFF];
- return crc ^ 0xFFFFFFFFu;
- }
- void program_serial_checksum_main(void)
- {
- static int table_done;
- unsigned int crc;
- const unsigned char *buf = (const unsigned char *)PROGRAM_LOAD_ADDR;
- if (serial_load_size == 0) {
- io_putstr("No serial data (use load first). Compare with: cksum <file> on host.\n");
- return;
- }
- if (!table_done) {
- crc32_init_table();
- table_done = 1;
- }
- crc = crc32_cksum(buf, serial_load_size);
- io_putdec(crc);
- io_putstr(" ");
- io_putdec(serial_load_size);
- io_putstr("\n");
- }
- void program_serial_trim_main(unsigned int first, unsigned int last)
- {
- serial_trim_first = first;
- serial_trim_last = last;
- io_putstr("serial trim: skip first ");
- io_putdec(first);
- io_putstr(", drop last ");
- io_putdec(last);
- io_putstr(" bytes.\n");
- }
- void program_load_main(void)
- {
- int c;
- unsigned char *dst = (unsigned char *)PROGRAM_LOAD_ADDR;
- serial_init();
- serial_drain(); /* discard any stale bytes */
- /* Discard first N bytes (e.g. tty sends when port opened). */
- {
- unsigned int i;
- for (i = 0; i < serial_trim_first; i++) {
- if (serial_getchar(500) < 0)
- break;
- }
- }
- serial_load_size = 0;
- io_putstr("Reading from serial - wait for file to finish, then press ESC. On host: cat <file> > /dev/ttyS0\n");
- for (;;) {
- if (io_poll_key() == 0x1B)
- break;
- c = serial_getchar(50);
- if (c >= 0) {
- dst[serial_load_size] = (unsigned char)c;
- serial_load_size++;
- if (serial_load_size >= PROGRAM_LOAD_MAX)
- break;
- }
- }
- if (serial_load_size >= serial_trim_last)
- serial_load_size -= serial_trim_last;
- io_putstr("Received ");
- io_putdec(serial_load_size);
- io_putstr(" bytes. Run with: run serial\n");
- }
- /* Simple serial link test (no XMODEM): send a string, then listen and print RX for 15s. */
- void program_serial_test_main(void)
- {
- unsigned int t0 = get_ticks_ms();
- const unsigned int test_duration_ms = 15000;
- const unsigned int tx_interval_ms = 1000;
- unsigned int last_tx = 0;
- int c;
- serial_init();
- io_putstr("Serial test in 3s - on host run: cat /dev/ttyS0 (then you should see 'Serial OK' every 1s).\n");
- while ((get_ticks_ms() - t0) < 3000)
- ; /* 3s to start cat on the other side */
- t0 = get_ticks_ms();
- io_putstr("Sending and listening 15s. To test RX: echo hello > /dev/ttyS0\n");
- while ((get_ticks_ms() - t0) < test_duration_ms) {
- if ((get_ticks_ms() - last_tx) >= tx_interval_ms) {
- serial_putstr("Serial OK\n");
- last_tx = get_ticks_ms();
- }
- c = serial_getchar(200);
- if (c >= 0)
- io_putchar((char)c);
- }
- io_putstr("Serial test done.\n");
- }
- /* Loopback test: short COM1 TX to RX (DB9 pin 2 to pin 3). If we receive what we send,
- * the UART works on this machine; if not, cable/host may be the issue. */
- void program_serial_loopback_main(void)
- {
- unsigned int i;
- int got;
- const unsigned char test_byte = 0x55;
- serial_init();
- io_putstr("Loopback: short COM1 TX to RX (DB9: pin 2 to pin 3). Press a key to test.\n");
- while (!io_poll_key())
- ;
- io_drain_keyboard();
- io_putstr("Sending 0x55 and reading for 1s...\n");
- for (i = 0; i < 20; i++)
- serial_putchar((char)test_byte);
- got = serial_getchar(1000);
- if (got == (int)test_byte) {
- io_putstr("Loopback OK - UART TX and RX work on this machine. Cable or host RX may be bad.\n");
- } else {
- io_putstr("No loopback (got ");
- if (got >= 0)
- io_puthex((unsigned int)got);
- else
- io_putstr("timeout");
- io_putstr("). Check short between pins 2 and 3, or UART.\n");
- }
- }
- void program_run_main(const char *filename)
- {
- char name_8_3[NAME_BUF_SIZE];
- int n;
- if (!filename || !filename[0]) {
- io_putstr("run: usage run <filename>\n");
- return;
- }
- if (is_serial_name(filename)) {
- program_run_serial_main();
- return;
- }
- to_8_3(filename, 64, name_8_3);
- n = fat_init();
- if (n == -2) {
- io_putstr("run: disk read error (see above)\n");
- return;
- }
- if (n != 0) {
- io_putstr("run: no FAT filesystem found\n");
- return;
- }
- n = fat_read_file(name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
- if (n == -2) {
- io_putstr("run: disk read error (see above)\n");
- return;
- }
- if (n <= 0) {
- io_putstr("run: file not found\n");
- return;
- }
- run_at_load_addr();
- }
|