#include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" #include #include #include "screen.h" #include "vga.h" #define MINX 8 extern GSubfont defont0; GSubfont *defont; struct{ Point pos; int bwid; }out; int islittle = 1; /* little endian bit ordering in bytes */ extern Cursor arrow; extern uchar cswizzle[256]; /* * screen dimensions */ #define MAXX 640 #define MAXY 480 /* * 'soft' screen bitmap */ GBitmap gscreen; GBitmap vgascreen; typedef struct VGAmode VGAmode; struct VGAmode { uchar general[2]; uchar sequencer[5]; uchar crt[0x19]; uchar graphics[9]; uchar attribute[0x15]; }; /* * 640x480 display, 16 bit color. */ VGAmode mode12 = { /* general */ 0xe7, 0x00, /* sequence */ 0x03, 0x01, 0x0f, 0x00, 0x06, /* crt */ 0x65, 0x4f, 0x50, 0x88, 0x55, 0x9a, 0x09, 0x3e, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x8b, 0xdf, 0x28, 0x00, 0xe7, 0x04, 0xe3, 0xff, /* graphics */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x0f, 0xff, /* attribute */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x3f, 0x01, 0x10, 0x0f, 0x00, 0x00, }; static Rectangle mbb; static Rectangle NULLMBB = {10000, 10000, -10000, -10000}; void genout(int reg, int val) { if(reg == 0) outb(EMISCW, val); else if (reg == 1) outb(EFCW, val); } void srout(int reg, int val) { outb(SRX, reg); outb(SR, val); } void grout(int reg, int val) { outb(GRX, reg); outb(GR, val); } void arout(int reg, int val) { inb(0x3DA); if (reg <= 0xf) { outb(ARW, reg | 0x0); outb(ARW, val); inb(0x3DA); outb(ARW, reg | 0x20); } else { outb(ARW, reg | 0x20); outb(ARW, val); } } void crout(int reg, int val) { outb(CRX, reg); outb(CR, val); } void setmode(VGAmode *v) { int i; for(i = 0; i < sizeof(v->general); i++) genout(i, v->general[i]); for(i = 0; i < sizeof(v->sequencer); i++) srout(i, v->sequencer[i]); crout(Cvre, 0); /* allow writes to CRT registers 0-7 */ for(i = 0; i < sizeof(v->crt); i++) crout(i, v->crt[i]); for(i = 0; i < sizeof(v->graphics); i++) grout(i, v->graphics[i]); for(i = 0; i < sizeof(v->attribute); i++) arout(i, v->attribute[i]); } void getmode(VGAmode *v) { int i; v->general[0] = inb(0x3cc); v->general[1] = inb(0x3ca); for(i = 0; i < sizeof(v->sequencer); i++) { outb(SRX, i); v->sequencer[i] = inb(SR); } for(i = 0; i < sizeof(v->crt); i++) { outb(CRX, i); v->crt[i] = inb(CR); } for(i = 0; i < sizeof(v->graphics); i++) { outb(GRX, i); v->graphics[i] = inb(GR); } for(i = 0; i < sizeof(v->attribute); i++) { inb(0x3DA); outb(ARW, i | 0x20); v->attribute[i] = inb(ARR); } } void printmode(VGAmode *v) { int i; print("g %2.2x %2x\n", v->general[0], v->general[1]); print("s "); for(i = 0; i < sizeof(v->sequencer); i++) { print(" %2.2x", v->sequencer[i]); } print("\nc "); for(i = 0; i < sizeof(v->crt); i++) { print(" %2.2x", v->crt[i]); } print("\ng "); for(i = 0; i < sizeof(v->graphics); i++) { print(" %2.2x", v->graphics[i]); } print("\na "); for(i = 0; i < sizeof(v->attribute); i++) { print(" %2.2x", v->attribute[i]); } print("\n"); } void dumpmodes(void) { VGAmode *v; int i; print("general registers: %02x %02x %02x %02x\n", inb(0x3cc), inb(0x3ca), inb(0x3c2), inb(0x3da)); print("sequence registers: "); for(i = 0; i < sizeof(v->sequencer); i++) { outb(SRX, i); print(" %02x", inb(SR)); } print("\nCRT registers: "); for(i = 0; i < sizeof(v->crt); i++) { outb(CRX, i); print(" %02x", inb(CR)); } print("\nGraphics registers: "); for(i = 0; i < sizeof(v->graphics); i++) { outb(GRX, i); print(" %02x", inb(GR)); } print("\nAttribute registers: "); for(i = 0; i < sizeof(v->attribute); i++) { inb(0x3DA); outb(ARW, i | 0x20); print(" %02x", inb(ARR)); } } #endif uchar *pad1, *pad2; void setscreen(int maxx, int maxy, int ldepth) { int len; uchar *p; mbb = NULLMBB; /* * zero hard screen and setup a bitmap for the new size */ memset((void*)SCREENMEM, 0xff, 64*1024); if(ldepth == 3) vgascreen.ldepth = 3; else vgascreen.ldepth = 0; vgascreen.base = (void*)SCREENMEM; vgascreen.width = (maxx*(1<bits->base; for(i = defont->bits->width*Dy(defont->bits->r); i > 0; i--, l++) *l = (*l<<24) | ((*l>>8)&0x0000ff00) | ((*l<<8)&0x00ff0000) | (*l>>24); bitreverse((uchar*)defont->bits->base, defont->bits->width*BY2WD*Dy(defont->bits->r)); /* * set up 'soft' and hard screens */ if(conf.maxx == 0) conf.maxx = MAXX; if(conf.maxy == 0) conf.maxy = MAXY; setscreen(conf.maxx, conf.maxy, conf.ldepth); /* * set up color map */ outb(CMWX, 0); for(i = 0; i < 16; i++){ outb(CM, colourxpnd(i<<1)); outb(CM, colourxpnd(i>>2)); outb(CM, colourxpnd(i&~1)); } outb(CMWX, 5); outb(CM, 21); outb(CM, 21); outb(CM, 21); outb(CMWX, 0xa); outb(CM, 42); outb(CM, 42); outb(CM, 42); } /* * collect changes to the 'soft' screen */ void mbbrect(Rectangle r) { if (r.min.x < mbb.min.x) mbb.min.x = r.min.x; if (r.min.y < mbb.min.y) mbb.min.y = r.min.y; if (r.max.x > mbb.max.x) mbb.max.x = r.max.x; if (r.max.y > mbb.max.y) mbb.max.y = r.max.y; screenupdate(); } void mbbpt(Point p) { if (p.x < mbb.min.x) mbb.min.x = p.x; if (p.y < mbb.min.y) mbb.min.y = p.y; if (p.x >= mbb.max.x) mbb.max.x = p.x+1; if (p.y >= mbb.max.y) mbb.max.y = p.y+1; } void unlocktseng(void) { outb(0x3bf, 0x03); outb(0x3d8, 0xa0); } /* * copy litte endian soft screen to big endian hard screen */ void screenupdate(void) { uchar *sp, *hp; int y, len, incs, inch, off, page, y2pg, ey; Rectangle r; r = mbb; mbb = NULLMBB; if(Dy(r) < 0) return; if(r.min.x < 0) r.min.x = 0; if(r.min.y < 0) r.min.y = 0; if(r.max.x > gscreen.r.max.x) r.max.x = gscreen.r.max.x; if(r.max.y > gscreen.r.max.y) r.max.y = gscreen.r.max.y; incs = gscreen.width * BY2WD; inch = vgascreen.width * BY2WD; switch(gscreen.ldepth){ case 0: off = r.min.x>>3; hp = (uchar*)(vgascreen.base+(r.min.y*vgascreen.width)) + off; sp = (uchar*)(gscreen.base+(r.min.y*gscreen.width)) + off; len = (r.max.x + 7)/8 - r.min.x/8; if(len < 1) return; /* reverse the bits */ for (y = r.min.y; y < r.max.y; y++){ l0update(sp, hp, len); sp += incs; hp += inch; } break; case 1: r.min.x &= ~15; /* 16 bit allignment for l1update() */ off = r.min.x>>3; hp = (uchar*)(vgascreen.base+(r.min.y*vgascreen.width)) + off; sp = (uchar*)(gscreen.base+(r.min.y*gscreen.width)) + 2*off; len = (r.max.x + 15)/8 - r.min.x/8; if(len < 2) len = 2; /* reverse the bits and split into 2 bit planes */ for (y = r.min.y; y < r.max.y; y++){ l1update(sp, hp, len); sp += incs; hp += inch; } break; case 2: off = r.min.x>>3; hp = (uchar*)(vgascreen.base+(r.min.y*vgascreen.width)) + off; sp = (uchar*)(gscreen.base+(r.min.y*gscreen.width)) + 4*off; len = (r.max.x + 7)/8 - r.min.x/8; if(len < 1) len = 1; /* reverse the bits and split into 2 bit planes */ for (y = r.min.y; y < r.max.y; y++){ l2update(sp, hp, len); sp += incs; hp += inch; } break; case 3: y2pg = (64*1024/BY2WD)/gscreen.width; off = (r.min.y % y2pg) * gscreen.width * BY2WD + r.min.x; hp = (uchar*)(vgascreen.base) + off; sp = (uchar*)(gscreen.base) + off; len = r.max.x - r.min.x; if(len < 1) return; y = r.min.y; for(page = y/y2pg; y < r.max.y; page++){ unlocktseng(); outb(0x3cd, (page<<4)|page); ey = (page+1)*y2pg; if(ey > r.max.y) ey = r.max.y; for (; y < ey; y++){ memmove(sp, hp, len); sp += incs; hp += inch; } hp = (uchar*)(vgascreen.base) + r.min.x; } break; } } void screenputnl(void) { Rectangle r; out.pos.x = MINX; out.pos.y += defont0.height; if(out.pos.y > gscreen.r.max.y-defont0.height) out.pos.y = gscreen.r.min.y; r = Rect(0, out.pos.y, gscreen.r.max.x, out.pos.y+2*defont0.height); gbitblt(&gscreen, r.min, &gscreen, r, flipD[0]); mbbrect(r); screenupdate(); } Lock screenlock; void screenputs(char *s, int n) { Rune r; int i; Rectangle rs; char buf[4]; rs.min = Pt(0, out.pos.y); while(n > 0){ i = chartorune(&r, s); if(i == 0){ s++; --n; continue; } memmove(buf, s, i); buf[i] = 0; n -= i; s += i; if(r == '\n') screenputnl(); else if(r == '\t'){ out.pos.x += (8-((out.pos.x-MINX)/out.bwid&7))*out.bwid; if(out.pos.x >= gscreen.r.max.x) screenputnl(); }else if(r == '\b'){ if(out.pos.x >= out.bwid+MINX){ out.pos.x -= out.bwid; gsubfstring(&gscreen, out.pos, defont, " ", flipD[S]); } }else{ if(out.pos.x >= gscreen.r.max.x-out.bwid) screenputnl(); out.pos = gsubfstring(&gscreen, out.pos, defont, buf, flipD[S]); } } rs.max = Pt(gscreen.r.max.x, out.pos.y+defont0.height); mbbrect(rs); screenupdate(); } int screenbits(void) { return 1<