Subversion Repositories Shiroi

Rev

Rev 28 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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