-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.cpp
More file actions
479 lines (403 loc) · 12.3 KB
/
screen.cpp
File metadata and controls
479 lines (403 loc) · 12.3 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
void printChar(char msg, short color)
{
//only do it if the inputted character is a valid ascii code
if (msg > 31)
{
/**videoStart = msg;
videoStart++;
*videoStart = color;
videoStart++;*/
//see if doing 16 bit transfer is faster, since each char takes 2 bytes anyway
short dat = msg;
dat += (color<<8);
*(short*)videoStart = dat;
videoStart+=2;
}
return;
}
void printCharAdr(char msg, short color, unsigned int curPos)
{
char* screen_address = (char*)(0xB8000 + (curPos*2));
*screen_address = msg;
screen_address++;
*screen_address = color;
return;
}
//prints a null terminated character array to screen
//returns number of spaces it printed out
int printCharArray(char* array, short color, unsigned int curPos)
{
char charToPrint = *(char*)array;
int i = 0;
while (charToPrint != 0)
{
printCharAdr(charToPrint, color, curPos + i);
i++;
charToPrint = array[i];
}
return i;
}
void printString(string text, short color, unsigned int curpos)
{
for (int i = 0; i < text.length(); i++)
{
printCharAdr(text.at(i),color, curpos+i);
}
return;
}
void printString(string text, short color)
{
for (int i = 0; i < text.length(); i++)
{
printChar(text.at(i),color);
}
return;
}
//prints an int to the screen
void printInt(unsigned int number, short color, bool hex)
{
//all the ducks aren't perfectly aligned, bug this should eliminate that rare base 10 bug
//char printableResult[10]; //designate 5 spaces for the maximum sized 16 bit integer
char *printableResult = (char*)malloc(sizeof(char) * 40); //make a bigger array than we need to circumvent the base 10 bugs. just remember to free when done with it
for (int i = 0; i < 40; i++)
{
printableResult[i] = 0;
}
//use a different encoding scheme for hex and decimal
if (hex)
{
//convert each digit to single chars
printableResult[0] = (number & 0xF0000000)>>28;
printableResult[1] = (number & 0x0F000000)>>24;
printableResult[2] = (number & 0x00F00000)>>20;
printableResult[3] = (number & 0x000F0000)>>16;
printableResult[4] = (number & 0x0000F000)>>12;
printableResult[5] = (number & 0x00000F00)>>8;
printableResult[6] = (number & 0x000000F0)>>4;
printableResult[7] = number & 0x0000000F;
//for each char, convert to hex represensation
for (int i = 0; i < 8; i++)
{
printableResult[i] = intToHexChar(printableResult[i]);
}
}
else
{
for (int i = 0; i < 10; i++)
{
char numPow = ((number/(pow_slow(10, i)))%10)+48; //num / 10 power i
printableResult[9-i] = numPow;
}
}
bool foundFirstDigit = false;
for (int i = 0; i < 10; i++)
{
if (printableResult[i] != 48)
{
foundFirstDigit = true;
}
if(foundFirstDigit)
{
//print the digit to the screen
printChar(printableResult[i], color);
}
}
//no memory leaks
free(printableResult);
//edge case for if the inputted number = 0
if (number == 0) printChar('0', color);
return;
/*printableResult[0] = (num % 10) + 48; least significant digit
printableResult[1] = ((num / 10) % 10) + 48;
printableResult[2] = ((num / 100) % 10) + 48;
printableResult[3] = ((num / 1000) % 10) + 48;
printableResult[4] = ((num / 10000) % 10) + 48;
printableResult[5] = ((num / 100000) % 10) + 48;
printableResult[6] = ((num / 1000000) % 10) + 48;
printableResult[7] = ((num / 10000000) % 10) + 48;
printableResult[8] = ((num / 100000000) % 10) + 48;
printableResult[9] = ((num / 1000000000) % 10) + 48; most significant digit
printCharAdr(printableResult[0], 0x0F, curPos + 9);
printCharAdr(printableResult[1], 0x0F, curPos + 8);
printCharAdr(printableResult[2], 0x0F, curPos + 7);
printCharAdr(printableResult[3], 0x0F, curPos + 6);
printCharAdr(printableResult[4], 0x0F, curPos + 5);
printCharAdr(printableResult[5], 0x0F, curPos + 4);
printCharAdr(printableResult[6], 0x0F, curPos + 3);
printCharAdr(printableResult[7], 0x0F, curPos + 2);
printCharAdr(printableResult[8], 0x0F, curPos + 1);
printCharAdr(printableResult[9], 0x0F, curPos);*/
}
void printFloat(float value, short color, bool hex)
{
if (hex)
{
//just print floats to the screen in hex format for now
int lol = *(unsigned int*)&value;
printInt(lol, color, true);
}
else
{
}
return;
}
void printTest()
{
printChar('T', 0x0F);
printChar('E', 0x0F);
printChar('S', 0x0F);
printChar('T', 0x0F);
return;
}
//clears the screen to black
void clearScreen(int vgaMode)
{
//if text mode 3, do the thing you have to do in order to clear the screen in text mode 3
if (vgaMode == 3)
{
//there are 2048 character positions on a textmode 3 screen
for (int i = 0; i < 2048; i++)
{
printCharAdr(0, 0x0, i);
}
}
return;
}
//sets videoStart to the correct address for the inputted cursor position
void setCurPos(int posX, int posY)
{
//set the address of video start to the desired position
videoStart = (char*)(0xB8000 + ((posY * 80) + posX)*2);
return;
}
void setVGAtextModeCursor(unsigned int posX, unsigned int posY, int vgaMode)
{
/*3D4 = address
//3D5 = data
note that ega compatible and monochrome modes use different ports for this*/
unsigned short cursorPosShort = posX + (posY*80);
//seperate them into high and low bytes
unsigned short low = cursorPosShort&(0xFF);
unsigned short high = cursorPosShort&(0x00FF);
high>>=8;
//send 0E to the address register
outb(0x0E, 0x03D4);
//send the cursor location high value
outb(high, 0x3D5);
//send 0F to the address register
outb(0x0F, 0x03D4);
//send the cursor location high value
outb(low, 0x3D5);
//set the cursor blink in front
setTextModeAttribute(posX, posY, 0x0F, 3);
//that should do it
return;
}
void setVGAtextModeCursor(int vgaMode)
{
/*3D4 = address
//3D5 = data
note that ega compatible and monochrome modes use different ports for this*/
//seperate them into high and low bytes
unsigned short low = (((unsigned int)videoStart)-0xB8000)/2;
unsigned short high = low;
low = low&(0xFF);
low = high&(0x00FF);
high>>=8;
//send 0E to the address register
outb(0x0E, 0x03D4);
//send the cursor location high value
outb(high, 0x3D5);
//send 0F to the address register
outb(0x0F, 0x03D4);
//send the cursor location high value
outb(low, 0x3D5);
//print8bitNumber(1800, low);
//print8bitNumber(1804, high);
/*char printableResult[3];
printableResult[0] = (low % 10) + 48; //least significant digit
printableResult[1] = ((low / 10) % 10) + 48; //middle digit
printableResult[2] = ((low / 100)) + 48; //most significant digit
printCharAdr(printableResult[0], 0x0F, 1800 + 2);
printCharAdr(printableResult[1], 0x0F, 1800 + 1);
printCharAdr(printableResult[2], 0x0F, 1800);
printableResult[0] = (high % 10) + 48; //least significant digit
printableResult[1] = ((high / 10) % 10) + 48; //middle digit
printableResult[2] = ((high / 100)) + 48; //most significant digit
printCharAdr(printableResult[0], 0x0F, 1794 + 2);
printCharAdr(printableResult[1], 0x0F, 1794 + 1);
printCharAdr(printableResult[2], 0x0F, 1794);*/
//set the cursor blinking to the text in front
setTextModeAttribute(0x0F, 3);
//that should do it
return;
}
//set pixel attribute byte from a given input position
void setTextModeAttribute(unsigned int posX, unsigned int posY, short atr, int vgaMode)
{
unsigned int curPos = (posY*80) + posX;
char* atr_address = (char*)(0xB8000 + (curPos*2) + 1);
*atr_address = atr;
return;
}
//set pixel attribute byte based on whatever value is in the videoStart pointer
void setTextModeAttribute(short atr, int vgaMode)
{
char* atr_address = videoStart + 1;
*atr_address = atr;
return;
}
//inserts a character on the screen at the specified position, shifting everything forwards 1 position
void insertChar(char msg, short color, int posX, int posY)
{
//if the rest of the line is blank, just print a character normally without moving anything around
if (restOfLineIsBlank(posX, posY))
{
printChar(msg, color);
}
//if the rest of the line isn't blank, move all the characters forward 1 and then print the char
else
{
char *rowAdr = (char*)(0xB8000 + ((posY * 80))*2);
for(int i = 159; i > (posX*2); i--)
{
*(rowAdr + i + 2) = *(char*)(rowAdr + i);
}
//everything has been shifting forward a little. Now, print the character to the now-empty spot
printChar(msg, color);
}
}
//inserts a character on the screen at the specified position, shifting everything forwards 1 position. Derives position automatically from videoStart
void insertChar(char msg, short color)
{
//i'm not pasting this thing twice
unsigned int X = (((unsigned int)videoStart-0xB8000)/2) % 80;
unsigned int Y = (((unsigned int)videoStart-0xB8000)/160);
insertChar(msg, color, X, Y);
//videoStart+=2;
return;
}
//returns true if everything to the left of the given position up until position 80 is blank
bool restOfLineIsBlank(int posX, int posY)
{
unsigned int curPos = (posY*80) + posX;
char* atr_address = (char*)(0xB8000 + (curPos*2));
bool isBlank = true;
int i = posX;
while(i < 80 && isBlank)
{
if (*atr_address > 30)
{
isBlank = false;
}
atr_address+=2;
i++;
}
return isBlank;
}
void consoleNewLine(unsigned int videoMode)
{
//convert video start pointer to individual x y values
unsigned int X = (((unsigned int)videoStart-0xB8000)/2) % 80;
unsigned int Y = (((unsigned int)videoStart-0xB8000)/160);
setCurPos(0, Y + 1);
setVGAtextModeCursor(3);
//if there are less than 2 "blank" lines under this one worth of screen space left, move the screen up by 1
if (Y > 21)
{
unsigned int origAdr = (unsigned int)videoStart; //save the cursor address so it can be restored later
videoStart = (char*)0xB80A0;
/*time for a for loop.
*/
//try to use memcpy instead
/*for (unsigned int i = (unsigned int)videoStart; i < 0xBFFFF; i++)
{
*(char*)(videoStart-160) = *(char*)videoStart;
videoStart = (char*)i;
}*/
memcpy(videoStart-160, videoStart, 0xBFFFF-(unsigned int)videoStart);
//wow that loop is always fairly complicated when I do it with raw assembly
//be sure to put the original screen address pointer back to where it was
setCurPos(0, Y);
setVGAtextModeCursor(videoMode);
}
return;
}
void print8BitHexInt(char hexNum)
{
char high = intToHexChar((hexNum & 0xF0)>>4);
char low = intToHexChar(hexNum & 0x0F);
//intToHexChar(high);
//intToHexChar(low);
printChar(high, 0x0F);
printChar(low, 0x0F);
return;
}
void printFileList(rarray<fileInfo> *list, filesystemInfo *sys)
{
for (int i = 0; i < list->getSize(); i++)
{
fileInfo *file = &list->at(i);
if (file->isValidFile)
{
printString(file->fileName, 0x0F);
if (!file->isDirectory)
{
printChar('.', 0x0F);
printString(file->fileExtension, 0x0F);
printString(" ",0x0F);
unsigned int yeah = file->size;
printInt(yeah, 0x0F, false);
printString(" bytes", 0x0F);
}
else
{
//printChar('d', 0x0F);
}
//print the sector number too
//printString(" Cluster number: 0x",0x0F);//computationally wasteful way of adding spaces, meh dont care. 486s are fast enough for this type of crap
printString(" Cluster: ",0x0F); //take up less space b/c of information spam
if (sys->partitionType == 0x0B || sys->partitionType == 0x0C)
{
//if filesystem is not fat16, pring the high cluster word
printInt((short)file->clusterNumberHigh & 0x0000FFFF, 0x0F, true);
//printChar(' ', 0x0F);
}
printInt((short)file->clusterNumberLow & 0x0000FFFF, 0x0F, true);
//print the date and time
printDateTime(&file->fileDateTime);
consoleNewLine();
}
}
}
void printDateTime(datetime *input)
{
printString(" ", 0x0F);
printInt(input->month & 0x0000007F, 0x0F);
printChar('/', 0x0F);
printInt(input->day & 0x0000001F, 0x0F);
printChar('/', 0x0F);
printInt(input->year+1980, 0x0F);
printChar(' ', 0x0F);
printInt(input->hours & 0x000000FF, 0x0F);
printChar(':', 0x0F);
printInt(input->minutes & 0x0000003F, 0x0F);
printChar(':', 0x0F);
printInt(input->seconds * 2, 0x0F); //for seconds, multiply by 2
}
//checks videoStart to see if a newline needs to be inserted and if so, inserts it. Try to avoid using for performance sensitive applications
void checkIfNewlineNeeded()
{
unsigned int X = (((unsigned int)videoStart-0xB8000)/2) % 80;
unsigned int Y = (((unsigned int)videoStart-0xB8000)/160);
/*if ((unsigned int)videoStart > 0xB8C80)
{
consoleNewLine();
}*/
if (Y > 22)
{
consoleNewLine();
setCurPos(1, Y-1); //reposition to xpos because reasons
setVGAtextModeCursor(3);
}
}