فهرست منبع

Add specific cirrus logic GPU driver

Johannes Zellner 1 ماه پیش
والد
کامیت
f70cb39227
11فایلهای تغییر یافته به همراه480 افزوده شده و 16 حذف شده
  1. 5 2
      Makefile
  2. 253 0
      cirrus.c
  3. 23 0
      cirrus.h
  4. 33 0
      fbtext.c
  5. 6 0
      fbtext.h
  6. 5 0
      gpu.c
  7. 3 1
      gpu.h
  8. 30 11
      mouse.c
  9. 6 0
      vga.c
  10. 2 0
      vga.h
  11. 114 2
      wm.c

+ 5 - 2
Makefile

@@ -95,6 +95,9 @@ gpu.o: gpu.c gpu.h pci.h ati.h nvidia.h
 ati.o: ati.c ati.h pci.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+cirrus.o: cirrus.c cirrus.h pci.h vga.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 nvidia.o: nvidia.c nvidia.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -185,7 +188,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -334,4 +337,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o uhci.o usb.o lsusb.o blkdev.o ata.o fatdetect.o storage.o fatformat.o format.o vfs.o idle.o kexec.o kexec_tramp.o dbg.o save.o rm.o mkdir.o rmdir.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 253 - 0
cirrus.c

@@ -0,0 +1,253 @@
+/* Cirrus Logic GD5446 BitBLT acceleration.
+   Reference: QEMU hw/display/cirrus_vga.c (register offsets, ROP codes). */
+
+#include "cirrus.h"
+#include "pci.h"
+#include "vga.h"
+
+/* Sequencer (0x3C4/0x3C5). */
+#define SR_UNLOCK     0x06
+#define SR_UNLOCK_VAL 0x12
+#define SR_CURSOR_X   0x10
+#define SR_CURSOR_Y   0x11
+#define SR_CURSOR_ATTR 0x12
+#define SR_CURSOR_PAT 0x13
+
+#define CURSOR_SHOW     0x01
+#define CURSOR_HIDDENPEL 0x02
+
+/* Graphics controller BitBLT registers (0x3CE/0x3CF). */
+#define GR_WIDTH_LO    0x20
+#define GR_WIDTH_HI    0x21
+#define GR_HEIGHT_LO   0x22
+#define GR_HEIGHT_HI   0x23
+#define GR_DSTPITCH_LO 0x24
+#define GR_DSTPITCH_HI 0x25
+#define GR_SRCPITCH_LO 0x26
+#define GR_SRCPITCH_HI 0x27
+#define GR_DSTADDR_LO  0x28
+#define GR_DSTADDR_MID 0x29
+#define GR_DSTADDR_HI  0x2a
+#define GR_SRCADDR_LO  0x2c
+#define GR_SRCADDR_MID 0x2d
+#define GR_SRCADDR_HI  0x2e
+#define GR_MODE        0x30
+#define GR_STATUS      0x31
+#define GR_ROP         0x32
+#define GR_MODEEXT     0x33
+
+#define BLT_BUSY  0x01
+#define BLT_START 0x02
+
+#define BLTMODE_BACKWARDS      0x01
+#define BLTMODE_PIXELWIDTH_MASK 0x30
+#define BLTMODE_PIXELWIDTH16   0x10
+#define BLTMODE_PIXELWIDTH24   0x20
+#define BLTMODE_PIXELWIDTH32   0x30
+#define BLTMODE_PATTERNCOPY    0x40
+#define BLTMODE_COLOREXPAND    0x80
+
+#define BLTMODEEXT_SOLIDFILL   0x04
+
+#define ROP_SRC 0x0d
+
+static int cirrus_present;
+
+static void gr_write(unsigned char idx, unsigned char val)
+{
+	port_outb(0x3CE, idx);
+	port_outb(0x3CF, val);
+}
+
+static unsigned char gr_read(unsigned char idx)
+{
+	port_outb(0x3CE, idx);
+	return port_inb(0x3CF);
+}
+
+static void sr_write(unsigned char idx, unsigned char val)
+{
+	port_outb(0x3C4, idx);
+	port_outb(0x3C5, val);
+}
+
+static unsigned char sr_read(unsigned char idx)
+{
+	port_outb(0x3C4, idx);
+	return port_inb(0x3C5);
+}
+
+void cirrus_init(void)
+{
+	cirrus_present = 0;
+	sr_write(SR_UNLOCK, SR_UNLOCK_VAL);
+	if (sr_read(SR_UNLOCK) == SR_UNLOCK_VAL)
+		cirrus_present = 1;
+}
+
+int cirrus_available(void)
+{
+	return cirrus_present;
+}
+
+void cirrus_blit(unsigned int sx, unsigned int sy,
+		 unsigned int dx, unsigned int dy,
+		 unsigned int w, unsigned int h)
+{
+	unsigned int bpp = vga_fb_bpp();
+	unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
+	unsigned int pitch = vga_fb_pitch();
+	unsigned int src = sy * pitch + sx * pb;
+	unsigned int dst = dy * pitch + dx * pb;
+	unsigned int width_bytes = w * pb;
+	unsigned int mode = 0;
+
+	if (bpp == 16)
+		mode |= BLTMODE_PIXELWIDTH16;
+	else if (bpp == 24)
+		mode |= BLTMODE_PIXELWIDTH24;
+	else
+		mode |= BLTMODE_PIXELWIDTH32;
+
+	if (dst > src) {
+		/* Overlapping, moving down/right: walk from the bottom-right so the
+		   source isn't clobbered. The engine runs backward with a positive
+		   pitch written; the address points at the last byte of the region. */
+		mode |= BLTMODE_BACKWARDS;
+		src += (h - 1) * pitch + (width_bytes - 1);
+		dst += (h - 1) * pitch + (width_bytes - 1);
+	}
+
+	gr_write(GR_WIDTH_LO, (unsigned char)((width_bytes - 1) & 0xFF));
+	gr_write(GR_WIDTH_HI, (unsigned char)(((width_bytes - 1) >> 8) & 0xFF));
+	gr_write(GR_HEIGHT_LO, (unsigned char)((h - 1) & 0xFF));
+	gr_write(GR_HEIGHT_HI, (unsigned char)(((h - 1) >> 8) & 0xFF));
+	gr_write(GR_DSTPITCH_LO, (unsigned char)(pitch & 0xFF));
+	gr_write(GR_DSTPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
+	gr_write(GR_SRCPITCH_LO, (unsigned char)(pitch & 0xFF));
+	gr_write(GR_SRCPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
+	gr_write(GR_DSTADDR_LO, (unsigned char)(dst & 0xFF));
+	gr_write(GR_DSTADDR_MID, (unsigned char)((dst >> 8) & 0xFF));
+	gr_write(GR_DSTADDR_HI, (unsigned char)((dst >> 16) & 0xFF));
+	gr_write(GR_SRCADDR_LO, (unsigned char)(src & 0xFF));
+	gr_write(GR_SRCADDR_MID, (unsigned char)((src >> 8) & 0xFF));
+	gr_write(GR_SRCADDR_HI, (unsigned char)((src >> 16) & 0xFF));
+	gr_write(GR_MODE, (unsigned char)mode);
+	gr_write(GR_ROP, ROP_SRC);
+	gr_write(GR_MODEEXT, 0);
+
+	gr_write(GR_STATUS, 0);         /* ensure START is clear so the write triggers */
+	gr_write(GR_STATUS, BLT_START);
+
+	while (gr_read(GR_STATUS) & BLT_BUSY)
+		;
+}
+
+void cirrus_fill(unsigned int x, unsigned int y,
+		 unsigned int w, unsigned int h,
+		 unsigned int r, unsigned int g, unsigned int b)
+{
+	unsigned int bpp = vga_fb_bpp();
+	unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
+	unsigned int pitch = vga_fb_pitch();
+	unsigned int dst = y * pitch + x * pb;
+	unsigned int width_bytes = w * pb;
+	unsigned int mode = BLTMODE_PATTERNCOPY | BLTMODE_COLOREXPAND;
+
+	/* Set the foreground color (the solid fill source). */
+	if (bpp == 16) {
+		unsigned short rgb565 = (unsigned short)
+			(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
+		mode |= BLTMODE_PIXELWIDTH16;
+		gr_write(0x01, (unsigned char)(rgb565 & 0xFF));
+		gr_write(0x11, (unsigned char)((rgb565 >> 8) & 0xFF));
+	} else if (bpp == 24) {
+		mode |= BLTMODE_PIXELWIDTH24;
+		gr_write(0x01, (unsigned char)b);
+		gr_write(0x11, (unsigned char)g);
+		gr_write(0x13, (unsigned char)r);
+	} else {
+		mode |= BLTMODE_PIXELWIDTH32;
+		gr_write(0x01, (unsigned char)b);
+		gr_write(0x11, (unsigned char)g);
+		gr_write(0x13, (unsigned char)r);
+		gr_write(0x15, 0);
+	}
+
+	gr_write(GR_WIDTH_LO, (unsigned char)((width_bytes - 1) & 0xFF));
+	gr_write(GR_WIDTH_HI, (unsigned char)(((width_bytes - 1) >> 8) & 0xFF));
+	gr_write(GR_HEIGHT_LO, (unsigned char)((h - 1) & 0xFF));
+	gr_write(GR_HEIGHT_HI, (unsigned char)(((h - 1) >> 8) & 0xFF));
+	gr_write(GR_DSTPITCH_LO, (unsigned char)(pitch & 0xFF));
+	gr_write(GR_DSTPITCH_HI, (unsigned char)((pitch >> 8) & 0xFF));
+	gr_write(GR_DSTADDR_LO, (unsigned char)(dst & 0xFF));
+	gr_write(GR_DSTADDR_MID, (unsigned char)((dst >> 8) & 0xFF));
+	gr_write(GR_DSTADDR_HI, (unsigned char)((dst >> 16) & 0xFF));
+	gr_write(GR_MODE, (unsigned char)mode);
+	gr_write(GR_ROP, ROP_SRC);
+	gr_write(GR_MODEEXT, BLTMODEEXT_SOLIDFILL);
+
+	gr_write(GR_STATUS, 0);         /* ensure START is clear so the write triggers */
+	gr_write(GR_STATUS, BLT_START);
+
+	while (gr_read(GR_STATUS) & BLT_BUSY)
+		;
+}
+
+/* Pointer-arrow shape (16x16), same as the software cursor: tip at top-left,
+   widening triangle then a narrowing tail. Placed in the top-left of the
+   32x32 hardware-cursor box. MSB (bit 15) = column 0. */
+static const unsigned short cirrus_cursor_arrow[16] = {
+	0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
+	0xFC00, 0xE000, 0x8000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
+};
+
+void cirrus_cursor_setup(void)
+{
+	unsigned char bitmap[256];
+	unsigned int r, i;
+	unsigned int vram = vga_fb_vram_kb() * 1024;
+	unsigned char *dst;
+
+	/* 32x32 two-plane cursor: AND plane (offset 0..127), XOR plane (128..255),
+	   4 bytes per row. The arrow is drawn in the XOR plane (so pixels are
+	   color0 = white) with the AND plane all-zero (no invert). */
+	for (i = 0; i < 256; i++)
+		bitmap[i] = 0;
+	for (r = 0; r < 16; r++) {
+		unsigned short bits = cirrus_cursor_arrow[r];
+		bitmap[128 + r * 4 + 0] = (unsigned char)((bits >> 8) & 0xFF); /* x=0..7  */
+		bitmap[128 + r * 4 + 1] = (unsigned char)(bits & 0xFF);        /* x=8..15 */
+	}
+
+	/* Upload to the top of VRAM (SR[0x13] = 0 selects the first 256-byte block). */
+	sr_write(SR_CURSOR_PAT, 0);
+	dst = (unsigned char *)(vga_fb_lfb_addr() + vram - 16384);
+	for (i = 0; i < 256; i++)
+		dst[i] = bitmap[i];
+
+	/* Cursor colors via the hidden DAC palette: entry 0 = white, 15 = black. */
+	sr_write(SR_CURSOR_ATTR, CURSOR_HIDDENPEL);
+	port_outb(0x3C8, 0);
+	port_outb(0x3C9, 63); port_outb(0x3C9, 63); port_outb(0x3C9, 63);
+	port_outb(0x3C8, 15);
+	port_outb(0x3C9, 0); port_outb(0x3C9, 0); port_outb(0x3C9, 0);
+	sr_write(SR_CURSOR_ATTR, CURSOR_SHOW);
+}
+
+void cirrus_cursor_move(unsigned int x, unsigned int y)
+{
+	/* Position is 11 bits; low 3 bits select the index alias, high 8 are data. */
+	sr_write(SR_CURSOR_X | ((x & 0x7) << 5), (unsigned char)(x >> 3));
+	sr_write(SR_CURSOR_Y | ((y & 0x7) << 5), (unsigned char)(y >> 3));
+}
+
+void cirrus_cursor_show(int show)
+{
+	unsigned char v = sr_read(SR_CURSOR_ATTR);
+	if (show)
+		v |= CURSOR_SHOW;
+	else
+		v &= ~CURSOR_SHOW;
+	sr_write(SR_CURSOR_ATTR, v);
+}

+ 23 - 0
cirrus.h

@@ -0,0 +1,23 @@
+/* Cirrus Logic GD5446 hardware acceleration (BitBLT engine). */
+#ifndef CIRRUS_H
+#define CIRRUS_H
+
+void cirrus_init(void);
+int  cirrus_available(void);
+
+/* Copy a pixel rectangle from (sx,sy) to (dx,dy), both within VRAM. */
+void cirrus_blit(unsigned int sx, unsigned int sy,
+		 unsigned int dx, unsigned int dy,
+		 unsigned int w, unsigned int h);
+
+/* Fill a pixel rectangle in VRAM with a solid color. */
+void cirrus_fill(unsigned int x, unsigned int y,
+		 unsigned int w, unsigned int h,
+		 unsigned int r, unsigned int g, unsigned int b);
+
+/* Hardware cursor (32x32). */
+void cirrus_cursor_setup(void);
+void cirrus_cursor_move(unsigned int x, unsigned int y);
+void cirrus_cursor_show(int show);
+
+#endif

+ 33 - 0
fbtext.c

@@ -173,6 +173,39 @@ void fbtext_draw_cells(const unsigned short *cells, unsigned int cols, unsigned
 	vga_fb_mark_dirty_rect(y0, rows * ROW_HEIGHT);
 }
 
+void fbtext_draw_cells_clip(const unsigned short *cells, unsigned int cols, unsigned int rows,
+		unsigned int x0, unsigned int y0,
+		unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch)
+{
+	int col0 = ((int)cx - (int)x0) / 8;
+	int col1 = ((int)(cx + cw) - (int)x0 + 7) / 8;
+	int row0 = ((int)cy - (int)y0) / ROW_HEIGHT;
+	int row1 = ((int)(cy + ch) - (int)y0 + (ROW_HEIGHT - 1)) / ROW_HEIGHT;
+	unsigned int row, col;
+
+	if (col0 < 0) col0 = 0;
+	if (row0 < 0) row0 = 0;
+	if (col1 > (int)cols) col1 = (int)cols;
+	if (row1 > (int)rows) row1 = (int)rows;
+	if (col1 <= col0 || row1 <= row0)
+		return;
+
+	for (row = (unsigned int)row0; row < (unsigned int)row1; row++) {
+		for (col = (unsigned int)col0; col < (unsigned int)col1; col++) {
+			unsigned short cell = cells[row * cols + col];
+			unsigned char c = (unsigned char)(cell & 0xFF);
+			unsigned char attr = (unsigned char)(cell >> 8);
+			unsigned int rf, gf, bf, rb, gb, bb;
+			attr_to_rgb(attr, 1, &rf, &gf, &bf);
+			attr_to_rgb(attr, 0, &rb, &gb, &bb);
+			draw_glyph_at(x0 + col * 8, y0 + row * ROW_HEIGHT, c,
+				rf, gf, bf, rb, gb, bb, ROW_HEIGHT, 0);
+		}
+	}
+	vga_fb_mark_dirty_rect(y0 + (unsigned int)row0 * ROW_HEIGHT,
+		(unsigned int)(row1 - row0) * ROW_HEIGHT);
+}
+
 void fbtext_scroll_up_one_line(void)
 {
 	unsigned int y, i;

+ 6 - 0
fbtext.h

@@ -15,6 +15,12 @@ void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, uns
    origin (x0, y0). 8 px per column, 10 px per row. */
 void fbtext_draw_cells(const unsigned short *cells, unsigned int cols, unsigned int rows,
 		unsigned int x0, unsigned int y0);
+
+/* Same, but only cells whose (cell-aligned) bounds intersect the clip rect
+   (cx, cy, cw, ch). */
+void fbtext_draw_cells_clip(const unsigned short *cells, unsigned int cols, unsigned int rows,
+		unsigned int x0, unsigned int y0,
+		unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch);
 void fbtext_scroll_up_one_line(void);
 void fbtext_set_cursor(unsigned int col, unsigned int row);
 void fbtext_show_cursor(void);

+ 5 - 0
gpu.c

@@ -3,6 +3,7 @@
 #include "gpu.h"
 #include "pci.h"
 #include "ati.h"
+#include "cirrus.h"
 #include "nvidia.h"
 
 static int gpu_vendor_id;
@@ -33,6 +34,9 @@ void gpu_init(void)
 				nvidia_init();
 			} else if (vendor == PCI_VENDOR_INTEL) {
 				gpu_vendor_id = GPU_VENDOR_INTEL;
+			} else if (vendor == PCI_VENDOR_CIRRUS) {
+				gpu_vendor_id = GPU_VENDOR_CIRRUS;
+				cirrus_init();
 			} else {
 				gpu_vendor_id = GPU_VENDOR_OTHER;
 			}
@@ -58,6 +62,7 @@ const char *gpu_vendor_name(void)
 	case GPU_VENDOR_ATI:     return "ATI/AMD";
 	case GPU_VENDOR_NVIDIA:  return "NVIDIA";
 	case GPU_VENDOR_INTEL:   return "Intel";
+	case GPU_VENDOR_CIRRUS:  return "Cirrus Logic";
 	case GPU_VENDOR_OTHER:   return "Other";
 	default:                 return "Unknown";
 	}

+ 3 - 1
gpu.h

@@ -6,11 +6,13 @@
 #define GPU_VENDOR_ATI      1
 #define GPU_VENDOR_NVIDIA   2
 #define GPU_VENDOR_INTEL    3
-#define GPU_VENDOR_OTHER    4
+#define GPU_VENDOR_CIRRUS   4
+#define GPU_VENDOR_OTHER    5
 
 #define PCI_VENDOR_ATI      0x1002
 #define PCI_VENDOR_NVIDIA   0x10DE
 #define PCI_VENDOR_INTEL    0x8086
+#define PCI_VENDOR_CIRRUS   0x1013
 
 void gpu_init(void);
 int gpu_vendor(void);

+ 30 - 11
mouse.c

@@ -1,4 +1,5 @@
 #include "mouse.h"
+#include "cirrus.h"
 #include "timer.h"
 #include "vga.h"
 
@@ -44,6 +45,7 @@ static unsigned int mouse_id;
 static int mouse_cursor_flag;
 static unsigned int mouse_pkt_size;
 static unsigned int mouse_buttons;  /* bit0=left bit1=right bit2=middle */
+static int mouse_hw_cursor;         /* 1 = Cirrus hardware cursor in use */
 
 static unsigned int fbw, fbh;
 static unsigned int cur_x, cur_y;
@@ -170,8 +172,19 @@ static void mouse_cursor_setup(void)
 	fbh = vga_fb_height();
 	if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
 		return;
-	/* Start cursor at a fixed visible spot (clamped to the framebuffer).
-	   Using a constant offset avoids any overlap with the terminal header/prompt. */
+
+	/* Cirrus hardware cursor: the card overlays it; no save/restore. */
+	if (cirrus_available()) {
+		mouse_hw_cursor = 1;
+		cur_x = 100;
+		cur_y = 100;
+		cirrus_cursor_setup();
+		cirrus_cursor_move(cur_x, cur_y);
+		mouse_cursor_flag = 1;
+		return;
+	}
+
+	mouse_hw_cursor = 0;
 	cur_x = 100;
 	cur_y = 100;
 	{
@@ -270,13 +283,19 @@ void mouse_poll_update(void)
 			long ny = (long)cur_y - dy;
 			mouse_clamp_ptr(&nx, &ny);
 			if ((unsigned int)nx != cur_x || (unsigned int)ny != cur_y) {
-				vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
-					cur_saved);
-				cur_x = (unsigned int)nx;
-				cur_y = (unsigned int)ny;
-				vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
-					cur_saved);
-				mouse_draw_cursor(cur_x, cur_y);
+				if (mouse_hw_cursor) {
+					cur_x = (unsigned int)nx;
+					cur_y = (unsigned int)ny;
+					cirrus_cursor_move(cur_x, cur_y);
+				} else {
+					vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
+						cur_saved);
+					cur_x = (unsigned int)nx;
+					cur_y = (unsigned int)ny;
+					vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
+						cur_saved);
+					mouse_draw_cursor(cur_x, cur_y);
+				}
 			}
 		}
 	}
@@ -324,7 +343,7 @@ unsigned int mouse_buttons_state(void)
 
 void mouse_redraw_cursor(void)
 {
-	if (!mouse_cursor_flag)
+	if (!mouse_cursor_flag || mouse_hw_cursor)
 		return;
 	vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
 	mouse_draw_cursor(cur_x, cur_y);
@@ -332,7 +351,7 @@ void mouse_redraw_cursor(void)
 
 void mouse_cursor_erase(void)
 {
-	if (!mouse_cursor_flag)
+	if (!mouse_cursor_flag || mouse_hw_cursor)
 		return;
 	vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
 }

+ 6 - 0
vga.c

@@ -188,6 +188,12 @@ void *vga_fb_ptr(void)
 	return (void *)vga_fb_lfb;
 }
 
+unsigned long vga_fb_lfb_addr(void)
+{
+	vga_cache_boot_fb();
+	return (unsigned long)vga_fb_lfb;
+}
+
 int vga_has_vesa(void)
 {
 	vga_cache_boot_fb();

+ 2 - 0
vga.h

@@ -27,6 +27,8 @@ unsigned int vga_fb_bpp(void);
 unsigned int vga_fb_vram_kb(void);
 unsigned int vga_fb_pci_id(void);
 void *vga_fb_ptr(void);
+/* Physical address of the linear framebuffer (for off-screen VRAM access). */
+unsigned long vga_fb_lfb_addr(void);
 int vga_has_vesa(void);
 
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);

+ 114 - 2
wm.c

@@ -2,6 +2,7 @@
    Slot 0 runs wm_run(); each terminal window is a separate process. */
 
 #include "wm.h"
+#include "cirrus.h"
 #include "fbtext.h"
 #include "idle.h"
 #include "io.h"
@@ -219,6 +220,113 @@ static void wm_drag_accumulate(unsigned int oy, unsigned int ny, unsigned int ph
 	}
 }
 
+/* Byte copy that handles overlapping source/destination. */
+static void wm_memmove(unsigned char *dst, const unsigned char *src, unsigned int n)
+{
+	if (dst == src || n == 0)
+		return;
+	if (dst < src) {
+		unsigned int i;
+		for (i = 0; i < n; i++)
+			dst[i] = src[i];
+	} else {
+		while (n--)
+			dst[n] = src[n];
+	}
+}
+
+/* Copy a rectangle within the shadow buffer (to mirror a BitBLT'd drag). */
+static void wm_shadow_copy(unsigned int ox, unsigned int oy,
+			   unsigned int nx, unsigned int ny,
+			   unsigned int w, unsigned int h)
+{
+	unsigned char *fb = (unsigned char *)vga_fb_ptr();
+	unsigned int pitch = vga_fb_pitch();
+	unsigned int bpp = vga_fb_bpp();
+	unsigned int pb = (bpp == 16) ? 2 : (bpp == 24) ? 3 : 4;
+	unsigned int row_bytes = w * pb;
+	unsigned int r;
+
+	if (ny > oy || (ny == oy && nx > ox)) {
+		for (r = h; r-- > 0; )
+			wm_memmove(fb + (ny + r) * pitch + nx * pb,
+				   fb + (oy + r) * pitch + ox * pb, row_bytes);
+	} else {
+		for (r = 0; r < h; r++)
+			wm_memmove(fb + (ny + r) * pitch + nx * pb,
+				   fb + (oy + r) * pitch + ox * pb, row_bytes);
+	}
+}
+
+/* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,ch). */
+static void wm_fill_rect_clip(unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh,
+			      unsigned int cx, unsigned int cy, unsigned int cw, unsigned int ch,
+			      unsigned int r, unsigned int g, unsigned int b)
+{
+	unsigned int x0 = (rx > cx) ? rx : cx;
+	unsigned int y0 = (ry > cy) ? ry : cy;
+	unsigned int x1 = (rx + rw < cx + cw) ? rx + rw : cx + cw;
+	unsigned int y1 = (ry + rh < cy + ch) ? ry + rh : cy + ch;
+	if (x1 > x0 && y1 > y0)
+		vga_fill_rect(x0, y0, x1 - x0, y1 - y0, r, g, b);
+}
+
+/* Redraw a rectangular region of the desktop (background + underlying windows,
+   excluding the window being dragged). Restores the exposed sliver. */
+static void wm_redraw_sliver(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
+{
+	unsigned int i;
+	vga_fill_rect(x, y, w, h, 32, 32, 48);
+	for (i = 0; i < MAX_WINDOWS; i++) {
+		struct window *w2 = &windows[i];
+		if (!w2->used || (int)i == drag_window)
+			continue;
+		if (!(x < w2->x + w2->pw && x + w > w2->x &&
+		      y < w2->y + w2->ph && y + h > w2->y))
+			continue;
+
+		/* Border. */
+		wm_fill_rect_clip(w2->x, w2->y, w2->pw, w2->ph,
+			x, y, w, h, 200, 200, 200);
+		/* Title bar. */
+		if (w2->focused)
+			wm_fill_rect_clip(w2->x + 1, w2->y + 1, w2->pw - 2, WM_TITLE_H,
+				x, y, w, h, 70, 90, 150);
+		else
+			wm_fill_rect_clip(w2->x + 1, w2->y + 1, w2->pw - 2, WM_TITLE_H,
+				x, y, w, h, 100, 100, 100);
+		/* Content background + cells. */
+		wm_fill_rect_clip(w2->x + 1, w2->y + 1 + WM_TITLE_H,
+			w2->pw - 2, SCREEN_ROWS * 10, x, y, w, h, 0, 0, 0);
+		fbtext_draw_cells_clip(w2->term->screen.cells,
+			w2->term->screen.cols, w2->term->screen.rows,
+			w2->x + 1, w2->y + 1 + WM_TITLE_H, x, y, w, h);
+	}
+}
+
+/* Cirrus fast drag: move the window with the BitBLT, restore the exposed
+   sliver, and mirror both in the RAM shadow buffer. No full recomposite. */
+static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
+			 unsigned int nx, unsigned int ny)
+{
+	int dx = (int)nx - (int)ox;
+	int dy = (int)ny - (int)oy;
+
+	cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
+	wm_shadow_copy(ox, oy, nx, ny, w->pw, w->ph);
+
+	if (dy > 0)
+		wm_redraw_sliver(ox, oy, w->pw, (unsigned int)dy);
+	else if (dy < 0)
+		wm_redraw_sliver(ox, ny + w->ph, w->pw, (unsigned int)(-dy));
+	if (dx > 0)
+		wm_redraw_sliver(ox, oy, (unsigned int)dx, w->ph);
+	else if (dx < 0)
+		wm_redraw_sliver(nx + w->pw, oy, (unsigned int)(-dx), w->ph);
+
+	vga_fb_flush();
+}
+
 static void wm_handle_mouse(void)
 {
 	unsigned int buttons = mouse_buttons_state();
@@ -258,8 +366,12 @@ static void wm_handle_mouse(void)
 			unsigned int ox = w->x, oy = w->y;
 			wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
 			if (ox != w->x || oy != w->y) {
-				wm_drag_accumulate(oy, w->y, w->ph);
-				drag_render_needed = 1;
+				if (cirrus_available())
+					wm_drag_fast(w, ox, oy, w->x, w->y);
+				else {
+					wm_drag_accumulate(oy, w->y, w->ph);
+					drag_render_needed = 1;
+				}
 			}
 		} else {
 			drag_active = 0;