This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystdio.hpp
236 lines (211 loc) · 7 KB
/
mystdio.hpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef mystdio
#define mystdio
# include "mylowlevelfunc.hpp"
#ifndef COLORCONSTS
#define COLORCONSTS
#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define PURPLE 5
#define LIGHTBLUE 6
#define WHITE 7
#define LIGHTGREY 8
#define DARKRED 9
#define DARKGREEN 10
#define DARKYELLOW 11
#define DARKBLUE 12
#define DARKPURPLE 13
#define LIGHTDARKBLUE 14
#define DARKGREY 15
#endif
/* structure for screen related info */
struct BOOTINFO {
// all variables are of 8 bits. Order cares.
char cyls, leds, vmode, reserve;
short scrnx, scrny;
char *vram;
};
char mousebg[16][16]; // buffer for the original background at the mouse's location
// color related
void init_palette(void);
void set_palette(int start, int end, unsigned char *rgb);
// GUI related
void boxfill8(char *vram, int xsize, int ysize, unsigned char c, int x0, int y0, int x1, int y1);
void init_screen(char *vram, int xsize, int ysize);
void putfont8(char *vram, int xsize, int x, int y, char c, unsigned char *font);
void printStr(char *vram, int xsize, int x, int y, unsigned char color, char *c, int len, unsigned char *font);
void init_mouse_cursor(char *vram,int xsize, int x,int y);
void putblock8x8(char *vram, int vxsize, int pxsize, int pysize, int px0, int py0, char *buf, int bxsize);
void getblock8x8(char *vram, int vxsize, int pxsize, int pysize, int px0, int py0, char *buf, int bxsize);
/* Source code */
void init_palette(void){
// customized color map
static unsigned char table_rgb[16 * 3] = {
0x00, 0x00, 0x00, // 0: black
0xff, 0x00, 0x00, // 1: red
0x00, 0xff, 0x00, // 2. green
0xff, 0xff, 0x00, // 3. yellow
0x00, 0x00, 0xff, // 4. blue
0xff, 0x00, 0xff, // 5. purple
0x00, 0xff, 0xff, // 6. light blue
0xff, 0xff, 0xff, // 7, white
0xc6, 0xc6, 0xc6, // 8. light grey
0x84, 0x00, 0x00, // 9. dark red
0x00, 0x84, 0x00, // 10, dark green
0x84, 0x84, 0x00, // 11. dark yellow
0x00, 0x00, 0x84, // 12. dark blue
0x84, 0x00, 0x84, // 13. dark purple
0x00, 0x84, 0x84, // 14. light dark blue
0x84, 0x84, 0x84 // 15. dark grey
};
set_palette(0, 15, table_rgb);
return;
}
void set_palette(int start, int end, unsigned char *rgb){
/*
* following video DA converter rule to set palette
*/
int i, eflags;
eflags = io_load_eflags(); // store current flags
io_cli(); // temporarily prevent interruptions
io_out8(0x03c8, start);
for(i=start; i<= end; i++){
io_out8(0x03c9, rgb[0]/4);
io_out8(0x03c9, rgb[1]/4);
io_out8(0x03c9, rgb[2]/4);
rgb += 3;
}
io_store_eflags(eflags); //restore flags
// io_sti();
return;
}
void boxfill8(char *vram, int xsize, int ysize, unsigned char c, int x0, int y0, int x1, int y1){
/*
* This function will draw a box (rectangle) on the screen which has width xsize
* c is for color
* (x0, y0) and (x1, y1) are two diagonals of the box. These are absolute coordinates
*/
int x, y;
x0 = x0 < 0 ? xsize + x0 : x0;
y0 = y0 < 0 ? ysize + y0 : y0;
x1 = x1 < 0 ? xsize + x1 : x1;
y1 = y1 < 0 ? ysize + y1 : y1;
if(y0 > y1){
y = y0; y0 = y1; y1 = y;
}
if(x0 > x1){
x = x0; x0 = x1; x1 = x;
}
for (y = y0; y <= y1; y++){
for(x = x0; x <= x1; x++){
vram[y * xsize + x] = c;
}
}
}
void init_screen(char *vram, int xsize, int ysize){
boxfill8(vram, xsize, ysize, LIGHTDARKBLUE, 0, 0, -1, -19);
boxfill8(vram, xsize, ysize, LIGHTGREY, 0, -18, -1, -18);
boxfill8(vram, xsize, ysize, WHITE, 0, -17, -1, -17);
boxfill8(vram, xsize, ysize, LIGHTGREY, 0, -16, -1, -1);
boxfill8(vram, xsize, ysize, WHITE, 3, -14, 59, -14);
boxfill8(vram, xsize, ysize, WHITE, 2, -14, 2, -4);
boxfill8(vram, xsize, ysize, DARKGREY, 3, -4, 59, -4);
boxfill8(vram, xsize, ysize, DARKGREY, 59, -13, 59, -5);
boxfill8(vram, xsize, ysize, BLACK, 2, -3, 59, -3);
boxfill8(vram, xsize, ysize, BLACK, 60, -14, 60, -3);
boxfill8(vram, xsize, ysize, DARKGREY, -47, -14, -4, -14);
boxfill8(vram, xsize, ysize, DARKGREY, -47, -13, -47, -4);
boxfill8(vram, xsize, ysize, WHITE, -47, -3, -4, -3);
boxfill8(vram, xsize, ysize, WHITE, -3, -14, -3, -3);
}
void putfont8(char *vram, int xsize, int x, int y, char color, unsigned char *font){
int i;
char *p, d /* data */;
for (i = 0; i < 16; i++) {
p = vram + (y + i) * xsize + x;
d = font[i];
// set the color for the corresponding 8 pixels.
if ((d & 0x80) != 0) { p[0] = color; }
if ((d & 0x40) != 0) { p[1] = color; }
if ((d & 0x20) != 0) { p[2] = color; }
if ((d & 0x10) != 0) { p[3] = color; }
if ((d & 0x08) != 0) { p[4] = color; }
if ((d & 0x04) != 0) { p[5] = color; }
if ((d & 0x02) != 0) { p[6] = color; }
if ((d & 0x01) != 0) { p[7] = color; }
}
return;
}
// no auto wrap
// x, and y are the index of charactor location on the screen.
// x = 1 => pixel location x * 8
// y = 2 => pixel location y * 16
void printStr(char *vram, int xsize, int x, int y, unsigned char color, char *c, int len, unsigned char *font){
int i, x_, y_; // x_ and y_ are for the actual text pixel location
for(i = 0; i < len; i++){
x_ = (i*8 + x * 8) % xsize;
y_ = y*16 + (i*8 + x * 8) / xsize * 16;
putfont8(vram, xsize, x_, y_, color, font + c[i]*16);
}
}
/* mouse */
// void init_mouse_cursor(char *vram,int xsize, int x,int y,char bc){
void init_mouse_cursor(char *vram,int xsize, int x,int y){
// bc += 0;
//16*16 Mouse
//鼠标指针点阵
static char cursor[16][17] = {
"*...............",
"**..............",
"*O*.............",
"*OO*............",
"*OOO*...........",
"*OOOO*..........",
"*OOOOO*.........",
"*OOOOOO*........",
"*OOOOOOO*.......",
"*OOOO*****......",
"*OO***..........",
"*O*.............",
"**..............",
"*...............",
"................",
"................"
};
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
int off = (i+y)*xsize+x+j;
mousebg[i][j] = vram[off];
if (cursor[i][j] == '*') {
vram[off] = BLACK;
}
if (cursor[i][j] == 'O') {
vram[off] = WHITE;
}
if (cursor[i][j] == '.') {
// vram[off] = bc;
}
}
}
}
/* put buf contents back to vram*/
void putblock8x8(char *vram, int vxsize, int pxsize, int pysize, int px0, int py0, char *buf, int bxsize){
int x, y;
for(y = 0; y < pysize; y++){
for(x = 0; x < pxsize; x++){
vram[(py0+y) * vxsize + (px0 + x)] = buf[y * bxsize + x];
}
}
}
/* get buf contents from vram*/
void getblock8x8(char *vram, int vxsize, int pxsize, int pysize, int px0, int py0, char *buf, int bxsize){
int x, y;
for(y = 0; y < pysize; y++){
for(x = 0; x < pxsize; x++){
buf[y * bxsize + x] = vram[(py0+y) * vxsize + (px0 + x)];
}
}
}
#endif