-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.cpp
521 lines (467 loc) · 11.8 KB
/
utilities.cpp
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//figure out how much memory the system has
void memoryTest(bool printToScreen, unsigned int startAddress, unsigned int kbToTest)
{
bool success = true;
char* currentAddress = (char*)startAddress;
//print32bitNumber(startAddress, 100);
while (success && ((unsigned int)currentAddress - (unsigned int)startAddress)/1000 < kbToTest)
{
//first, set the address to zero then check if it's zero when read
*currentAddress = 0x00;
char read = *(char*)currentAddress;
if (read != 0x00)
{
success = false;
}
//next, set the address to 255 then check to see if it changed to 255
*currentAddress = 0xFF;
read = *(char*)currentAddress;
if (read != 0xFF)
{
success = false;
}
//set the memory location back to zero before going on to the next one
*currentAddress = 0x00;
currentAddress++;
if (printToScreen)
{
printCharAdr('m', 0x0F, 95);
printCharAdr('e', 0x0F, 96);
printCharAdr('m', 0x0F, 97);
printCharAdr(':', 0x0F, 98);
print32bitNumber((unsigned int)currentAddress - (unsigned int)startAddress, 100);
printCharAdr('b', 0x0F, 111);
printCharAdr('y', 0x0F, 112);
printCharAdr('t', 0x0F, 113);
printCharAdr('e', 0x0F, 114);
printCharAdr('s', 0x0F, 115);
}
}
//return (unsigned int)currentAddress - (unsigned int)startAddress;
return;
}
//takes an 8 bit value and prints it to the screen
void print8bitNumber(unsigned int num, unsigned int curPos)
{
char printableResult[3];
printableResult[0] = (num % 10) + 48; //least significant digit
printableResult[1] = ((num / 10) % 10) + 48; //middle digit
printableResult[2] = ((num / 100)) + 48; //most significant digit
printCharAdr(printableResult[0], 0x0F, curPos + 2);
printCharAdr(printableResult[1], 0x0F, curPos + 1);
printCharAdr(printableResult[2], 0x0F, curPos);
return;
}
//takes a 16 bit value and prints it to the screen
void print16bitNumber(unsigned int num, unsigned int curPos)
{
//yes, there are more "better" ways to do this but at the time of this writing, I have no memory management system in place for dynamically declaring array sizes on the fly. These functions are to allow me to debug stuff until I get a better system working
char printableResult[5]; //designate 5 spaces for the maximum sized 16 bit integer
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; //most significant digit
printCharAdr(printableResult[0], 0x0F, curPos + 4);
printCharAdr(printableResult[1], 0x0F, curPos + 3);
printCharAdr(printableResult[2], 0x0F, curPos + 2);
printCharAdr(printableResult[3], 0x0F, curPos + 1);
printCharAdr(printableResult[4], 0x0F, curPos);
return;
}
//takes an 32 bit value and prints it to the screen
void print32bitNumber(unsigned int num, unsigned int curPos)
{
char printableResult[10]; //designate 5 spaces for the maximum sized 16 bit integer
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);
}
//slower than the other number printing functions but more versatile and it doesn't have leading zeros
void printAnyNumber(unsigned int num, short color, unsigned int curPos)
{
unsigned int numLength = 0;
unsigned int result = (num % 10) + 48;
int i = 1;
while (i < 10)
{
result = ((num / (10)) % 10) + 48;
printCharAdr(char(result), color, curPos + i);
i++;
}
numLength = i;
//now that we know the length, do the same thing except print the number and don't do any leading zeros
while (i > 0)
{
i--;
unsigned int thingToPrint = ((num / i) % 10) + 48;
char output = thingToPrint;
//printCharAdr(output, color, curPos + i);
}
return;
}
//adds 2 numbers together then returns the result
int testIfReturnTypesWork(int num1, int num2)
{
return num1 + num2;
}
void setCharEqual(char *target, unsigned int *poop)
{
print32bitNumber(poop[0], 81);
return;
}
//prints the memory address of all occurances of the word "fooo" in the given memory range
void findFooo(unsigned int memStart, unsigned int length, int curPos)
{
unsigned int foosFound = 0;
for (int i = memStart; i < memStart + length && foosFound < 10; i++)
{
if (*(char*)(i) == 'f' && *(char*)(i+1) == 'o' && *(char*)(i+2) == 'o' && *(char*)(i+3) == 'o')
{
print32bitNumber(i, 150+(foosFound*80));
foosFound++;
}
}
print32bitNumber(69, 70);
return;
}
//shifts the cursor to the left or right. Positive values shift right. Negative values shift left.
void shiftCursor(int howFar)
{
videoStart+=(howFar*2);
setVGAtextModeCursor(3);
return;
}
//performs the equivalent of backspace on textmode 7 or 3 or whatever the default one is
void backspaceScreen()
{
//shift cursor back 1
shiftCursor(-1);
//replace this spot with a blank space
printChar(' ', 0x0F);
//go back one to simulate the back spacing type effect
shiftCursor(-1);
//now shift everything to the right of the cursor position and on this line only to the left once
unsigned int X = (((unsigned int)videoStart-0xB8000)/2) % 80;
unsigned int Y = (((unsigned int)videoStart-0xB8000)/160);
char *rowAdr = (char*)(0xB8000 + ((Y * 80))*2) + 1;
for(int i = (X*2); i < 160; i++)
{
*(rowAdr + i) = *(char*)(rowAdr + i + 2);
}
return;
}
/*void printMemoryAllocation(bool contents)
{
unsigned int X = (((unsigned int)videoStart-0xB8000)/2) % 80;
unsigned int Y = (((unsigned int)videoStart-0xB8000)/160);
unsigned int usedMemory = 0;
for (int i = 0; i < memf.getSize(); i++)
{
//some cross compilers dont do nullptr
usedMemory += getSizeOfMemBlock((void*)memf.at(i), nullptr);
//usedMemory += getSizeOfMemBlock((void*)memf.at(i), 0);
}
print32bitNumber(usedMemory, X+(Y*80));
printCharArray(" bytes", 0x0F, X+(Y*80)+10);
Y++;
//first, completely erase anything that would be behind this
for (int i = 0; i < memf.getSize() + 6; i++)
{
//print16bitNumber(getSizeOfMemBlock((void*)memf.at(i), nullptr), X-6+((i+Y)*80));
printCharArray(" ", 0x0E, X-6+((i+Y)*80));
}
for (int i = 0; i < memf.getSize(); i++)
{
//print16bitNumber(getSizeOfMemBlock((void*)memf.at(i), nullptr), X-6+((i+Y)*80));
print16bitNumber(getSizeOfMemBlock((void*)memf.at(i), nullptr), X-6+((i+Y)*80));
print32bitNumber((unsigned int)memf.at(i), X+((i+Y)*80));
print32bitNumber((unsigned int)memb.at(i), X+11+((i+Y)*80));
printCharAdr('-',0x0F, X+10+((i+Y)*80));
//print contents if applicable
if (contents)
{
char v = ' ';
for (int c = 0; c < 10 && v != char(0); c++)
{
v = *(char*)(memf.at(i)+c);
printCharAdr(v, 0x0F, X+21+c+((i+Y)*80));
}
}
}
return;
}*/
//i'm, sick and tired of pasting that one snippet of code over and over to calculate x y values from videoStart. Here's a function to do that
void cursorAdrToInts(int *x, int *y)
{
unsigned int X = (((unsigned int)videoStart-0xB8000)/2) % 80;
unsigned int Y = (((unsigned int)videoStart-0xB8000)/160);
*x = X;
*y = Y;
return;
}
//fuck it. 0 = intel. 1 = amd. 2 = cyrix. 3 = ibm
string cpuModelToString(int manufacturer, int model, int family)
{
string vendorName;
string modelName;
if (family == 4)
{
//486 dx1
if (model >= 0 && model < 2)
{
modelName = "486DX";
}
else if (model == 2)
{
modelName = "486SX";
}
else if (model == 3 || model == 7)
{
modelName = "486DX2";
}
else if (model == 4)
{
modelName = "486SL";
}
else if (model == 5)
{
modelName = "486SX2";
}
else if (model == 8 || model == 9)
{
modelName = "486DX4";
}
else if (model == 0xE || model == 0xF)
{
modelName = "5x86 DX5";
}
}
else if (family == 5)
{
if (manufacturer == 0)
{
if (model < 4)
{
modelName = "Pentium";
}
else if (model >= 4)
{
modelName = "Pentium MMX";
}
}
else
{
if (model < 4)
{
modelName = "K5";
}
else if (model ==6 || model == 7)
{
modelName = "K6";
}
else if (model == 8)
{
modelName = "K6-II";
}
else if (model == 9)
{
modelName = "K6-III";
}
else if (model == 0xD)
{
modelName = "K6-III+";
}
}
}
else if (family == 6)
{
//if intel
if (manufacturer == 0)
{
if (model < 2)
{
modelName = "Pentium Pro";
}
else if (model < 7)
{
modelName = "Pentium II";
}
else if (model < 9 || model == 0xA)
{
modelName = "Pentium III";
}
else if (model == 5 || model == 6)
{
modelName = "Pentium 4";
}
else
{
modelName = "Pentium M";
}
}
//AMD
else
{
if (model < 6 && model != 3)
{
modelName = "Athlon";
}
else if (model == 7 || model == 3)
{
modelName = "Duron";
}
else
{
modelName = "Athlon XP";
}
}
}
//one more level just for fun
else if (family == 0xF)
{
if (manufacturer == 0)
{
modelName = "Pentium 4";
}
else
{
if (model == 5)
{
modelName = "Opteron";
}
else
{
modelName = "Athlon 64";
}
}
}
//the special cases
else
{
if (family == 0x15 && model == 3)
{
modelName = "Pentium Overdrive";
}
modelName = "something modern";
}
//now do the vendor stuff
switch (manufacturer)
{
case 0:
vendorName = "Intel ";
break;
case 1:
vendorName = "AMD ";
break;
case 2:
vendorName = "Cyrix ";
break;
case 3:
vendorName = "VIA ";
break;
case 4:
vendorName = "SIS ";
break;
case 5:
vendorName = "UMC ";
break;
case 6:
vendorName = "Vortex 86 ";
break;
default:
vendorName = "unknown vendor. ";
break;
}
string finalString = vendorName + modelName;
return finalString;
}
//fuck it. 0 = intel. 1 = amd. 2 = cyrix. 3 = via. 4 = sis. 5 = umc. 6 = vortex86
int getCpuVendor()
{
int result = cpu_vendor();
//"Genu" - it's probably intel
if (result == 0x756E6547)
{
return 0;
}
//both amd strings
else if (result == 0x68747541 || result == 0x69444D41)
{
return 1;
}
//cyrix
else if (result == 0x69727943)
{
return 2;
}
//VIA
else if (result == 0x20414956)
{
return 3;
}
//sis
else if (result == 0x20534953)
{
return 4;
}
//umc
else if (result == 0x20434D55)
{
return 5;
}
//vortex 86
else if (result == 0x73726F56)
{
return 6;
}
return 7;
}
void printMemoryManagement()
{
//fucking strings fuckity fuck fuck
printString("Total used memory is ", 0x0F);
printInt(totalUsedMemory(), 0x0E);
printString(" out of ", 0x0F);
printInt(totalMaxMemory(), 0x0E);
//messageString += totalUsedMemory()
}
void printMemInfo(bool verbose)
{
for (int i = 0; i < currentSize; i++)
{
printInt(mmt[i].m_start, 0x0F, false);
printChar('-',0x0F);
printInt(mmt[i].m_end, 0x0F, false);
printChar(' ',0x0F);
printChar(' ',0x0F);
printInt(mmt[i].m_end - mmt[i].m_start, 0x0F, false);
if (verbose)
{
for (int u = 0; u < 40; u++)
{
printChar(*(char*)(mmt[i].m_start + u), 0x0D);
}
}
consoleNewLine(3);
}
//printMemoryManagement();
printInt(currentSize, 0x0E, false);
}