~kris/9p

9hist

ref: 095a5fde44639f98e35cb2e149e13d07a4e4d02f 9hist/pc/cga.c -rw-r--r-- 1.7 KiB
095a5fde — David du Colombier Plan 9 from Bell Labs 1997-06-18 29 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"

enum {
	Width		= 160,
	Height		= 25,

	Attr		= 7,		/* white on black */
};

#define CGASCREENBASE	((uchar*)KADDR(0xB8000))

static int pos;
static int screeninitdone;
QLock screenlock;
void (*vgascreenputc)(char*);

static uchar
cgaregr(int index)
{
	outb(0x3D4, index);
	return inb(0x3D4+1) & 0xFF;
}

static void
cgaregw(int index, int data)
{
	outb(0x3D4, index);
	outb(0x3D4+1, data);
}

static void
movecursor(void)
{
	cgaregw(0x0E, (pos/2>>8) & 0xFF);
	cgaregw(0x0F, pos/2 & 0xFF);
	CGASCREENBASE[pos+1] = Attr;
}

static void
cgascreenputc(int c)
{
	int i;

	if(c == '\n'){
		pos = pos/Width;
		pos = (pos+1)*Width;
	}
	else if(c == '\t'){
		i = 8 - ((pos/2)&7);
		while(i-->0)
			cgascreenputc(' ');
	}
	else if(c == '\b'){
		if(pos >= 2)
			pos -= 2;
		cgascreenputc(' ');
		pos -= 2;
	}
	else{
		CGASCREENBASE[pos++] = c;
		CGASCREENBASE[pos++] = Attr;
	}
	if(pos >= Width*Height){
		memmove(CGASCREENBASE, &CGASCREENBASE[Width], Width*(Height-1));
		memset(&CGASCREENBASE[Width*(Height-1)], 0, Width);
		pos = Width*(Height-1);
	}
	movecursor();
}

void
screeninit(void)
{
	pos = cgaregr(0x0E)<<8;
	pos |= cgaregr(0x0F);
	pos *= 2;
	screeninitdone = 1;
}

void
screenputs(char* s, int n)
{
	int i;
	Rune r;
	char buf[4];

	if(!islo()){
		if(!canqlock(&screenlock))
			return;
	}
	else
		qlock(&screenlock);

	if(vgascreenputc == nil){
		while(n-- > 0)
			cgascreenputc(*s++);
		qunlock(&screenlock);
		return;
	}

	while(n > 0) {
		i = chartorune(&r, s);
		if(i == 0){
			s++;
			--n;
			continue;
		}
		memmove(buf, s, i);
		buf[i] = 0;
		n -= i;
		s += i;
		vgascreenputc(buf);
	}

	qunlock(&screenlock);
}