Subversion Repositories Shiroi

Rev

Rev 20 | Rev 29 | 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 28 2024-08-31 12:49:20Z 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(' ');
69
}
70
 
71
void print_ptr(void* ptr){
72
	unsigned short p = (unsigned short)ptr;
73
	int i;
74
	const char hex[] = "0123456789ABCDEF";
75
	putstr("0x");
76
	for(i = 0; i < 4; i++){
77
		putchar(hex[(p & 0xf000) >> 12]);
78
		p = p << 4;
79
	}
80
}
81
 
28 nishi 82
unsigned short charbuf;
83
 
18 nishi 84
void cursor(void){
85
	setvramaddr(muli(posy, scrwidth) + posx);
86
	_vramchar(136 + 8 * cursorcolor);
87
	cursorindex++;
88
	if(cursorindex == 2){
89
		cursorcolor++;
90
		cursorindex = 0;
91
		if(cursorcolor == 15) cursorcolor = 0;
92
	}
93
	curx = posx;
94
	cury = posy;
95
	if(curx == scrwidth){
96
		curx = 0;
97
		cury++;
98
	}
99
}
100
 
101
void killcursor(void){
102
	setvramaddr(muli(cury, scrwidth) + curx);
28 nishi 103
	if(charbuf == 0xffff){
104
		vramchar(' ');
105
	}else{
106
		vramchar(charbuf & 0xff);
107
	}
18 nishi 108
}
109
 
110
void putchar(char c){
111
	if(c == '\r'){
112
		posx = 0;
113
	}else if(c == '\n'){
114
		posy++;
28 nishi 115
	}else if(c == 0x08){
116
		if(posx > 0){
117
			posx--;
118
		}else{
119
			posy--;
120
			posx = scrwidth - 1;
121
		}
122
		setreadvramaddr(muli(posy, scrwidth) + posx);
123
		charbuf = getvramchar();
18 nishi 124
	}else{
125
		setvramaddr(muli(posy, scrwidth) + posx);
126
		vramchar(c);
127
		posx++;
128
		if(posx == scrwidth){
129
			posx = 0;
130
			posy++;
131
		}
132
	}
133
	if(posy == scrheight){
134
		scroll_y();
135
		posy = scrheight - 1;
136
		posx = 0;
137
	}
138
}
139
 
140
void putstr(char* str){
141
	int i;
142
	for(i = 0; str[i] != 0; i++) putchar(str[i]);
143
}
144
 
145
void text_init(void){
146
	posx = 0;
147
	posy = 0;
148
	curx = 0;
149
	cury = 0;
150
	caps = 0;
151
	cursorcolor = 0;
152
	cursorindex = 0;
28 nishi 153
	charbuf = 0xffff;
18 nishi 154
}
155
 
156
void text_card(int t, int port){
157
	if(t == 0x22){
158
		text_kbd_data = port - 2;
159
	}
160
}