Subversion Repositories Shiroi

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 nishi 1
/* $Id: text.c 13 2024-08-29 04:36:14Z nishi $ */
2
 
3
#include "text.h"
4
 
5
#include "char.h"
6
#include "video.h"
9 nishi 7
#include "math.h"
8 nishi 8
#include "io.h"
9
 
10
char caps;
11
 
12
unsigned short posx;
13
unsigned short posy;
14
unsigned short curx;
15
unsigned short cury;
12 nishi 16
unsigned char cursorcolor;
17
unsigned char cursorindex;
8 nishi 18
 
19
short text_kbd_data;
20
 
21
extern int scrwidth;
22
extern int scrheight;
23
 
24
extern unsigned char keylist[];
25
extern unsigned char keylist_caps[];
26
 
12 nishi 27
char agetch(void){
28
	char k;
29
rep:
30
	if((k = inp(text_kbd_data)) == 0) return 0;
31
	while(inp(text_kbd_data) != 0);
32
	unsigned char top = (k & 0xf0) >> 4;
33
	unsigned char bot = (k & 0x0f);
34
	top--;
35
	bot--;
36
	if(keylist[top * 13 + bot] == '!'){
37
		caps = caps == 0 ? 1 : 0;
38
		goto rep;
39
	}
40
	return caps ? keylist_caps[top * 13 + bot] : keylist[top * 13 + bot];
41
}
42
 
8 nishi 43
char getch(void){
44
	char k = 0;
45
rep:
46
	while((k = inp(text_kbd_data)) == 0);
47
	while(inp(text_kbd_data) != 0);
48
	unsigned char top = (k & 0xf0) >> 4;
49
	unsigned char bot = (k & 0x0f);
50
	top--;
51
	bot--;
52
	if(keylist[top * 13 + bot] == '!'){
53
		caps = caps == 0 ? 1 : 0;
54
		goto rep;
55
	}
56
	return caps ? keylist_caps[top * 13 + bot] : keylist[top * 13 + bot];
57
}
58
 
59
void clear(void){
60
	int i;
13 nishi 61
	int size = muli(scrwidth, scrheight);
8 nishi 62
	setvramaddr(0);
63
	for(i = 0; i < size; i++) vramchar(' ');
64
}
65
 
66
void scroll_y(void){
67
	int i;
13 nishi 68
	int size = muli(scrwidth, scrheight - 1);
8 nishi 69
	for(i = 0; i < size; i++){
70
		setreadvramaddr(i + 32);
71
		unsigned char ch = getvramchar();
72
		setvramaddr(i);
73
		vramchar(ch);
74
	}
75
	for(i = 0; i < scrwidth; i++){
76
		vramchar(' ');
77
	}
78
}
79
 
80
void print_ptr(void* ptr){
81
	unsigned short p = (unsigned short)ptr;
82
	int i;
83
	const char hex[] = "0123456789ABCDEF";
84
	putstr("0x");
85
	for(i = 0; i < 4; i++){
86
		putchar(hex[(p & 0xf000) >> 12]);
87
		p = p << 4;
88
	}
89
}
90
 
91
void cursor(void){
13 nishi 92
	setvramaddr(muli(posy, scrwidth) + posx);
12 nishi 93
	_vramchar(136 + 8 * cursorcolor);
94
	cursorindex++;
95
	if(cursorindex == 2){
96
		cursorcolor++;
97
		cursorindex = 0;
98
		if(cursorcolor == 15) cursorcolor = 0;
99
	}
8 nishi 100
	curx = posx;
101
	cury = posy;
102
	if(curx == scrwidth){
103
		curx = 0;
104
		cury++;
105
	}
106
}
107
 
108
void killcursor(void){
13 nishi 109
	setvramaddr(muli(cury, scrwidth) + curx);
8 nishi 110
	vramchar(' ');
111
}
112
 
113
void putchar(char c){
114
	if(c == '\r'){
115
		posx = 0;
116
	}else if(c == '\n'){
117
		posy++;
118
	}else{
13 nishi 119
		setvramaddr(muli(posy, scrwidth) + posx);
8 nishi 120
		vramchar(c);
121
		posx++;
122
		if(posx == scrwidth){
123
			posx = 0;
124
			posy++;
125
		}
126
	}
127
	if(posy == scrheight){
128
		scroll_y();
129
		posy = scrheight - 1;
130
		posx = 0;
131
	}
132
}
133
 
134
void putstr(char* str){
135
	int i;
136
	for(i = 0; str[i] != 0; i++) putchar(str[i]);
137
}
138
 
139
void text_init(void){
140
	posx = 0;
141
	posy = 0;
142
	curx = 0;
143
	cury = 0;
144
	caps = 0;
12 nishi 145
	cursorcolor = 0;
146
	cursorindex = 0;
8 nishi 147
}
148
 
149
void text_card(int t, int port){
150
	if(t == 0x22){
151
		text_kbd_data = port - 2;
152
	}
153
}