Subversion Repositories Shiroi

Rev

Rev 19 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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