소스 검색

Add a bunch of serial commands

Johannes Zellner 8 달 전
부모
커밋
67a7900761
9개의 변경된 파일과 621개의 추가작업 그리고 23개의 파일을 삭제
  1. 11 5
      Makefile
  2. 16 0
      io.c
  3. 1 0
      io.h
  4. 214 18
      run.c
  5. 78 0
      serial.c
  6. 14 0
      serial.h
  7. 158 0
      terminal.c
  8. 119 0
      xmodem.c
  9. 10 0
      xmodem.h

+ 11 - 5
Makefile

@@ -53,7 +53,13 @@ ls.o: ls.c fat.h io.h
 cat.o: cat.c fat.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-run.o: run.c fat.h io.h fbtext.h prog_api.h timer.h vga.h
+run.o: run.c fat.h io.h fbtext.h prog_api.h serial.h timer.h vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+serial.o: serial.c serial.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+xmodem.o: xmodem.c xmodem.h serial.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 rtc.o: rtc.c rtc.h
@@ -80,7 +86,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o xmodem.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -135,8 +141,8 @@ root.img: prog.bin space.bin
 	mcopy -i $@ space.bin ::SPACE.BIN
 
 run: image.bin root.img
-	qemu-system-i386 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
-	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
+	qemu-system-i386 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy -serial stdio 2>/dev/null || \
+	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy -serial stdio
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o serial.o xmodem.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 16 - 0
io.c

@@ -187,6 +187,22 @@ void io_puthex(unsigned int n)
 	io_putstr(buf);
 }
 
+void io_putdec(unsigned int n)
+{
+	char buf[12];
+	unsigned int i = sizeof(buf) - 1;
+	buf[i] = '\0';
+	if (n == 0) {
+		io_putchar('0');
+		return;
+	}
+	while (n && i > 0) {
+		buf[--i] = (char)('0' + (n % 10));
+		n /= 10;
+	}
+	io_putstr(buf + i);
+}
+
 unsigned int io_get_cursor(void)
 {
 	return cursor;

+ 1 - 0
io.h

@@ -21,6 +21,7 @@ int io_did_scroll(void);  /* returns 1 once after a scroll, then 0 until next sc
 void io_putchar(char c);
 void io_putstr(const char *s);
 void io_puthex(unsigned int n);  /* print n as 8 hex digits, no 0x prefix */
+void io_putdec(unsigned int n);  /* print n in decimal */
 unsigned int io_get_cursor(void);
 void io_set_cursor(unsigned int pos);
 void io_cursor_show_block(void);

+ 214 - 18
run.c

@@ -1,9 +1,12 @@
-/* run command: load raw binary from FAT floppy to 0x21000 and execute. */
+/* 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"
 
@@ -11,6 +14,11 @@
 #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;
@@ -37,6 +45,206 @@ static void to_8_3(const char *in, unsigned int in_len, char *out)
 	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];
@@ -46,6 +254,10 @@ void program_run_main(const char *filename)
 		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) {
@@ -65,21 +277,5 @@ void program_run_main(const char *filename)
 		io_putstr("run: file not found\n");
 		return;
 	}
-	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)();
+	run_at_load_addr();
 }

+ 78 - 0
serial.c

@@ -0,0 +1,78 @@
+/* Serial (UART) driver: COM1 at 0x3F8, 115200 8N1, polled. */
+
+#include "serial.h"
+#include "timer.h"
+
+#define COM1_BASE  0x3F8
+#define RBR        0    /* Receive Buffer (DLAB=0) */
+#define THR        0    /* Transmit Holding (DLAB=0) */
+#define DLL        0    /* Divisor Latch Low (DLAB=1) */
+#define IER        1    /* Interrupt Enable */
+#define DLH        1    /* Divisor Latch High (DLAB=1) */
+#define LCR        3    /* Line Control */
+#define LSR        5    /* Line Status */
+
+#define LCR_8N1    0x03
+#define LCR_DLAB   0x80
+#define LSR_DR     0x01   /* Data Ready */
+#define LSR_THRE   0x20   /* Transmit Holding Register Empty */
+
+static inline unsigned char inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+void serial_init(void)
+{
+	/* Divisor for 115200: 115200 / 115200 = 1. Use 1. */
+	outb(COM1_BASE + LCR, LCR_DLAB);
+	outb(COM1_BASE + DLL, 1);
+	outb(COM1_BASE + DLH, 0);
+	outb(COM1_BASE + LCR, LCR_8N1);
+	outb(COM1_BASE + IER, 0);  /* No interrupts */
+}
+
+void serial_drain(void)
+{
+	while (serial_getchar(0) >= 0)
+		;
+}
+
+void serial_putchar(char c)
+{
+	unsigned int t0 = get_ticks_ms();
+	while ((get_ticks_ms() - t0) < 100) {
+		if (inb(COM1_BASE + LSR) & LSR_THRE) {
+			outb(COM1_BASE + THR, (unsigned char)c);
+			return;
+		}
+	}
+}
+
+void serial_putstr(const char *s)
+{
+	while (*s)
+		serial_putchar(*s++);
+}
+
+int serial_getchar(unsigned int timeout_ms)
+{
+	unsigned int t0 = get_ticks_ms();
+	while ((get_ticks_ms() - t0) < timeout_ms) {
+		if (inb(COM1_BASE + LSR) & LSR_DR)
+			return (unsigned char)inb(COM1_BASE + RBR);
+	}
+	return -1;
+}
+
+int serial_has_byte(void)
+{
+	return (inb(COM1_BASE + LSR) & LSR_DR) ? 1 : 0;
+}

+ 14 - 0
serial.h

@@ -0,0 +1,14 @@
+/* Serial (UART) driver: COM1 polled I/O for XMODEM load. */
+
+#ifndef SERIAL_H
+#define SERIAL_H
+
+void serial_init(void);
+void serial_drain(void);  /* discard any bytes already in RX buffer */
+void serial_putchar(char c);
+void serial_putstr(const char *s);
+/* Get one byte; block up to timeout_ms. Returns -1 on timeout. */
+int serial_getchar(unsigned int timeout_ms);
+int serial_has_byte(void);
+
+#endif

+ 158 - 0
terminal.c

@@ -11,6 +11,11 @@ extern void program_reboot_main(void);
 extern void program_ls_main(void);
 extern void program_cat_main(const char *filename);
 extern void program_run_main(const char *filename);
+extern void program_load_main(void);
+extern void program_serial_test_main(void);
+extern void program_serial_loopback_main(void);
+extern void program_serial_checksum_main(void);
+extern void program_serial_trim_main(unsigned int first, unsigned int last);
 extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
@@ -263,6 +268,133 @@ static int is_cmd_help(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_load(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 'l' || line[i+1] != 'o' || line[i+2] != 'a' || line[i+3] != 'd')
+		return 0;
+	i += 4;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_serial_test(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 11 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
+	    line[i+4] != 'a' || line[i+5] != 'l')
+		return 0;
+	i += 6;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 't' || line[i+1] != 'e' || line[i+2] != 's' || line[i+3] != 't')
+		return 0;
+	i += 4;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_serial_loopback(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 14 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
+	    line[i+4] != 'a' || line[i+5] != 'l')
+		return 0;
+	i += 6;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 8 > line_len)
+		return 0;
+	if (line[i] != 'l' || line[i+1] != 'o' || line[i+2] != 'o' || line[i+3] != 'p' ||
+	    line[i+4] != 'b' || line[i+5] != 'a' || line[i+6] != 'c' || line[i+7] != 'k')
+		return 0;
+	i += 8;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_serial_checksum(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 14 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
+	    line[i+4] != 'a' || line[i+5] != 'l')
+		return 0;
+	i += 6;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 8 > line_len)
+		return 0;
+	if (line[i] != 'c' || line[i+1] != 'h' || line[i+2] != 'e' || line[i+3] != 'c' ||
+	    line[i+4] != 'k' || line[i+5] != 's' || line[i+6] != 'u' || line[i+7] != 'm')
+		return 0;
+	i += 8;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
+static int is_cmd_serial_trim(const char *line, unsigned int line_len,
+	unsigned int *first, unsigned int *last)
+{
+	unsigned int i = 0;
+	unsigned int a = 0, b = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 11 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
+	    line[i+4] != 'a' || line[i+5] != 'l')
+		return 0;
+	i += 6;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 4 > line_len)
+		return 0;
+	if (line[i] != 't' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'm')
+		return 0;
+	i += 4;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len || line[i] < '0' || line[i] > '9')
+		return 0;
+	while (i < line_len && line[i] >= '0' && line[i] <= '9')
+		a = a * 10 + (unsigned int)(line[i++] - '0');
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i >= line_len || line[i] < '0' || line[i] > '9')
+		return 0;
+	while (i < line_len && line[i] >= '0' && line[i] <= '9')
+		b = b * 10 + (unsigned int)(line[i++] - '0');
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i != line_len)
+		return 0;
+	*first = a;
+	*last = b;
+	return 1;
+}
+
 static int is_cmd_clear(const char *line, unsigned int line_len)
 {
 	unsigned int i = 0;
@@ -339,6 +471,7 @@ void terminal_run(void)
 				} else if (k == '\n') {
 					static char cat_fname[CAT_FNAME_MAX];
 					unsigned int drive_sel;
+					unsigned int trim_first, trim_last;
 					int skip_prompt = 0;
 					io_putchar('\n');
 					if (is_cmd_hello(line, line_len)) {
@@ -365,6 +498,26 @@ void terminal_run(void)
 						program_cat_main(cat_fname);
 						io_drain_keyboard();
 						draw_header_row();
+					} else if (is_cmd_load(line, line_len)) {
+						program_load_main();
+						io_drain_keyboard();
+						draw_header_row();
+					} else if (is_cmd_serial_test(line, line_len)) {
+						program_serial_test_main();
+						io_drain_keyboard();
+						draw_header_row();
+					} else if (is_cmd_serial_loopback(line, line_len)) {
+						program_serial_loopback_main();
+						io_drain_keyboard();
+						draw_header_row();
+					} else if (is_cmd_serial_checksum(line, line_len)) {
+						program_serial_checksum_main();
+						io_drain_keyboard();
+						draw_header_row();
+					} else if (is_cmd_serial_trim(line, line_len, &trim_first, &trim_last)) {
+						program_serial_trim_main(trim_first, trim_last);
+						io_drain_keyboard();
+						draw_header_row();
 					} else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
 						program_run_main(cat_fname);
 						io_drain_keyboard();
@@ -388,6 +541,11 @@ void terminal_run(void)
 						io_putstr("colors\n");
 						io_putstr("cat <file>\n");
 						io_putstr("run <file>\n");
+						io_putstr("load\n");
+						io_putstr("serial test\n");
+						io_putstr("serial loopback\n");
+						io_putstr("serial checksum\n");
+						io_putstr("serial trim <first> <last>\n");
 						io_putstr("space\n");
 						io_putstr("display\n");
 						io_putstr("drive <0|1>\n");

+ 119 - 0
xmodem.c

@@ -0,0 +1,119 @@
+/* XMODEM receiver: 128-byte packets, 1-byte checksum. */
+
+#include "serial.h"
+#include "timer.h"
+#include "xmodem.h"
+
+#define SOH  0x01
+#define EOT  0x04
+#define ACK  0x06
+#define NAK  0x15
+#define CAN  0x18
+
+#define PACKET_HEADER  2   /* block number, 255-block (SOH already consumed) */
+#define PACKET_PAYLOAD 128
+#define PACKET_CSUM    1
+
+#define NAK_INTERVAL_MS  1000
+#define RECV_TIMEOUT_MS  3000
+
+static int get_byte(unsigned int timeout_ms)
+{
+	return serial_getchar(timeout_ms);
+}
+
+static void send_byte(unsigned char b)
+{
+	serial_putchar((char)b);
+}
+
+int xmodem_receive(void *buf, unsigned int max_len)
+{
+	unsigned char *dst = (unsigned char *)buf;
+	unsigned int total = 0;
+	unsigned char block_num = 1;
+	unsigned int last_nak = 0;
+
+	/* Send NAK repeatedly until we get SOH (start of first packet) or EOT (empty file). */
+	for (;;) {
+		unsigned int now = get_ticks_ms();
+		if (now - last_nak >= NAK_INTERVAL_MS) {
+			send_byte(NAK);
+			last_nak = now;
+		}
+		{
+			int c = get_byte(100);
+			if (c < 0)
+				continue;
+			if (c == EOT) {
+				send_byte(ACK);
+				return 0;
+			}
+			if (c == SOH)
+				break;
+			if (c == CAN)
+				return -1;
+		}
+	}
+
+	for (;;) {
+		unsigned char header[PACKET_HEADER];
+		unsigned char payload[PACKET_PAYLOAD];
+		unsigned char csum;
+		unsigned int i;
+		unsigned char sum = 0;
+
+		for (i = 0; i < PACKET_HEADER; i++) {
+			int c = get_byte(RECV_TIMEOUT_MS);
+			if (c < 0)
+				return -1;
+			header[i] = (unsigned char)c;
+		}
+		for (i = 0; i < PACKET_PAYLOAD; i++) {
+			int c = get_byte(RECV_TIMEOUT_MS);
+			if (c < 0)
+				return -1;
+			payload[i] = (unsigned char)c;
+			sum += payload[i];
+		}
+		{
+			int c = get_byte(RECV_TIMEOUT_MS);
+			if (c < 0)
+				return -1;
+			csum = (unsigned char)c;
+		}
+
+		if (header[0] != block_num || header[1] != (unsigned char)(255 - block_num)) {
+			send_byte(NAK);
+			continue;
+		}
+		if (csum != sum) {
+			send_byte(NAK);
+			continue;
+		}
+
+		/* Copy payload; respect max_len. */
+		for (i = 0; i < PACKET_PAYLOAD && total < max_len; i++, total++)
+			dst[total] = payload[i];
+
+		send_byte(ACK);
+		block_num++;
+
+		/* Next: expect another SOH or EOT. */
+		{
+			int c = get_byte(RECV_TIMEOUT_MS);
+			if (c < 0)
+				return -1;
+			if (c == EOT) {
+				send_byte(ACK);
+				return (int)total;
+			}
+			if (c == CAN)
+				return -1;
+			if (c != SOH) {
+				/* Unexpected; treat as abort. */
+				return -1;
+			}
+		}
+	}
+}

+ 10 - 0
xmodem.h

@@ -0,0 +1,10 @@
+/* XMODEM receiver: receive into buffer, max_len bytes. */
+
+#ifndef XMODEM_H
+#define XMODEM_H
+
+/* Receive XMODEM (128-byte packets, checksum) into buf, max max_len bytes.
+ * Returns bytes received on success, -1 on error/abort/timeout. */
+int xmodem_receive(void *buf, unsigned int max_len);
+
+#endif