-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.c
721 lines (645 loc) · 17.1 KB
/
lib.c
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/* lib.c - Some basic library functions (printf, strlen, etc.)
* vim:ts=4 noexpandtab
*/
#include "lib.h"
#define VIDEO 0xB8000
#define NUM_COLS 80
#define NUM_ROWS 25
#define SIZE_OF_ROW 0xA0
unsigned char ATTRIB =0x0;
static int screen_x;
static int screen_y;
static char* video_mem = (char *)VIDEO;
/*
* void clear(void);
* Inputs: void
* Return Value: none
* Function: Clears video memory
*/
void
clear(void)
{
int32_t i;
for(i=0; i<NUM_ROWS*NUM_COLS; i++) {
*(uint8_t *)(video_mem + (i << 1)) = ' ';
*(uint8_t *)(video_mem + (i << 1) + 1) = ATTRIB;
}
clear_buf();
}
/* Standard printf().
* Only supports the following format strings:
* %% - print a literal '%' character
* %x - print a number in hexadecimal
* %u - print a number as an unsigned integer
* %d - print a number as a signed integer
* %c - print a character
* %s - print a string
* %#x - print a number in 32-bit aligned hexadecimal, i.e.
* print 8 hexadecimal digits, zero-padded on the left.
* For example, the hex number "E" would be printed as
* "0000000E".
* Note: This is slightly different than the libc specification
* for the "#" modifier (this implementation doesn't add a "0x" at
* the beginning), but I think it's more flexible this way.
* Also note: %x is the only conversion specifier that can use
* the "#" modifier to alter output.
* */
int32_t
printf(int8_t *format, ...)
{
/* Pointer to the format string */
int8_t* buf = format;
/* Stack pointer for the other parameters */
int32_t* esp = (void *)&format;
esp++;
while(*buf != '\0') {
switch(*buf) {
case '%':
{
int32_t alternate = 0;
buf++;
format_char_switch:
/* Conversion specifiers */
switch(*buf) {
/* Print a literal '%' character */
case '%':
putc('%');
break;
/* Use alternate formatting */
case '#':
alternate = 1;
buf++;
/* Yes, I know gotos are bad. This is the
* most elegant and general way to do this,
* IMHO. */
goto format_char_switch;
/* Print a number in hexadecimal form */
case 'x':
{
int8_t conv_buf[64];
if(alternate == 0) {
itoa(*((uint32_t *)esp), conv_buf, 16);
puts(conv_buf);
} else {
int32_t starting_index;
int32_t i;
itoa(*((uint32_t *)esp), &conv_buf[8], 16);
i = starting_index = strlen(&conv_buf[8]);
while(i < 8) {
conv_buf[i] = '0';
i++;
}
puts(&conv_buf[starting_index]);
}
esp++;
}
break;
/* Print a number in unsigned int form */
case 'u':
{
int8_t conv_buf[36];
itoa(*((uint32_t *)esp), conv_buf, 10);
puts(conv_buf);
esp++;
}
break;
/* Print a number in signed int form */
case 'd':
{
int8_t conv_buf[36];
int32_t value = *((int32_t *)esp);
if(value < 0) {
conv_buf[0] = '-';
itoa(-value, &conv_buf[1], 10);
} else {
itoa(value, conv_buf, 10);
}
puts(conv_buf);
esp++;
}
break;
/* Print a single character */
case 'c':
putc( (uint8_t) *((int32_t *)esp) );
esp++;
break;
/* Print a NULL-terminated string */
case 's':
puts( *((int8_t **)esp) );
esp++;
break;
default:
break;
}
}
break;
default:
putc(*buf);
break;
}
buf++;
}
return (buf - format);
}
/*
* int32_t puts(int8_t* s);
* Inputs: int_8* s = pointer to a string of characters
* Return Value: Number of bytes written
* Function: Output a string to the console
*/
int32_t
puts(int8_t* s)
{
register int32_t index = 0;
while(s[index] != '\0') {
putc(s[index]);
index++;
}
return index;
}
/*
* cursor
* Inputs: x,y - coordinates
* Return Value: none
* Function: sync harwdare and software cursor to x,y
*/
void cursor(int x,int y) {
unsigned short position = y*80 + x;
outb(0x0F,0x3D4);
outb((unsigned char)(position&0xFF),0x3D5);
outb(0x0E,0x3D4);
outb((unsigned char)((position>>8)&0xFF),0x3D5);
term[term_live].screen_x = x;
term[term_live].screen_y = y;
screen_x = 0;
screen_y = 0;
}
/*
* ter_write
* Inputs: c - cahracter to print to screen
* Return Value: none
* Function: Output a char to the console
*/
void ter_write(char c, uint32_t idx, uint32_t idx_end) {
uint32_t pos, attr;
int i;
ATTRIB = 0x0F;//((ATTRIB+1)%4) + 0x0A;
pos = (uint32_t)(video_mem + ((NUM_COLS*term[term_live].screen_y + term[term_live].screen_x) << 1));
attr = pos + 1;
if(c == '\t') {
// do nothing
} if(c == '\n' || c == '\r') {
term[term_live].screen_y++;
term[term_live].screen_x=0;
if(term[term_live].screen_y == NUM_ROWS) {
scroll_up();
term[term_live].screen_y--;
term[term_live].screen_x=0;
}
} else if(c == '\b') {
if(term[term_live].screen_x != 0) {
term[term_live].screen_x--;
} else {
term[term_live].screen_x = NUM_COLS-1;
term[term_live].screen_y--;
}
for(i=0;i<idx_end-idx;i++)
memcpy((uint8_t *)pos+2*(i-1),(uint8_t *)pos+2*(i),1);
memcpy((uint8_t *)pos+2*(idx_end-idx-1),(uint8_t *)pos+2*(idx_end-idx),1);
} else {
for(i=idx_end-idx;i>0;i--)
memcpy((uint8_t *)pos+2*(i),(uint8_t *)pos+2*(i-1),1);
*(uint8_t *)pos = c;
*(uint8_t *)attr = ATTRIB;
term[term_live].screen_x++;
if(term[term_live].screen_x == NUM_COLS) {
term[term_live].screen_y++;
term[term_live].screen_x = 0;
}
if(term[term_live].screen_y == NUM_ROWS) {
scroll_up();
term[term_live].screen_y--;
term[term_live].screen_x=0;
}
}
cursor(term[term_live].screen_x,term[term_live].screen_y);
}
void ter_write_pit(char c) {
ATTRIB = 0x02;//((ATTRIB+1)%4) + 0x0A;
void * vid_mem = (void*)((0xD1+term_pit)*FOURKILO);
if(c == '\t') {
// do nothing
}
if(c == '\n' || c == '\r') {
term[term_pit].screen_y++;
term[term_pit].screen_x=0;
if(term[term_pit].screen_y == NUM_ROWS) {
scroll_up_pit();
term[term_pit].screen_y--;
term[term_pit].screen_x=0;
}
}
else if(c == '\b') {
if(term[term_pit].screen_x != 0) {
term[term_pit].screen_x--;
*(uint8_t *)(vid_mem + ((NUM_COLS*term[term_pit].screen_y + term[term_pit].screen_x) << 1)) = '\0';
*(uint8_t *)(vid_mem + ((NUM_COLS*term[term_pit].screen_y + term[term_pit].screen_x) << 1) + 1) = ATTRIB;
} else {
screen_x = NUM_COLS-1;
screen_y--;
*(uint8_t *)(vid_mem + ((NUM_COLS*term[term_pit].screen_y + term[term_pit].screen_x) << 1)) = '\0';
*(uint8_t *)(vid_mem + ((NUM_COLS*term[term_pit].screen_y + term[term_pit].screen_x) << 1) + 1) = ATTRIB;
}
}
else {
*(uint8_t *)(vid_mem + ((NUM_COLS*term[term_pit].screen_y + term[term_pit].screen_x) << 1)) = c;
*(uint8_t *)(vid_mem + ((NUM_COLS*term[term_pit].screen_y + term[term_pit].screen_x) << 1) + 1) = ATTRIB;
term[term_pit].screen_x++;
if(term[term_pit].screen_x == NUM_COLS) {
term[term_pit].screen_y++;
term[term_pit].screen_x = 0;
}
if(term[term_pit].screen_y == NUM_ROWS) {
scroll_up_pit();
term[term_pit].screen_y--;
term[term_pit].screen_x=0;
}
}
if(term_live == term_pit) cursor(term[term_pit].screen_x,term[term_pit].screen_y);
}
/*
* scroll_up
* Inputs: none
* Return Value: none
* Function: emulate one line of scrolling in console
*/
void scroll_up(void) {
int i;
for(i=0;i < NUM_ROWS-1;i++) {
memcpy(video_mem+i*SIZE_OF_ROW, video_mem+(i+1)*SIZE_OF_ROW, SIZE_OF_ROW);
}
for(i=0;i < NUM_COLS;i++) {
*(uint8_t *)(video_mem + ((NUM_COLS*(NUM_ROWS-1) + i) << 1)) = 0x00;
}
}
void scroll_up_pit(void) {
int i;
void * vid_mem = (void*)((0xD1+term_pit)*FOURKILO);
for(i=0;i < NUM_ROWS-1;i++) {
memcpy(vid_mem+i*SIZE_OF_ROW, vid_mem+(i+1)*SIZE_OF_ROW, SIZE_OF_ROW);
}
for(i=0;i < NUM_COLS;i++) {
*(uint8_t *)(vid_mem + ((NUM_COLS*(NUM_ROWS-1) + i) << 1)) = 0x00;
}
}
/*
* void putc(uint8_t c);
* Inputs: uint_8* c = character to print
* Return Value: void
* Function: Output a character to the console
*/
void
putc(uint8_t c)
{
if(c == '\n' || c == '\r') {
term[term_live].screen_y++;
term[term_live].screen_x=0;
} else {
*(uint8_t *)(video_mem + ((NUM_COLS*term[term_live].screen_y + term[term_live].screen_x) << 1)) = c;
*(uint8_t *)(video_mem + ((NUM_COLS*term[term_live].screen_y + term[term_live].screen_x) << 1) + 1) = ATTRIB;
term[term_live].screen_x++;
if(term[term_live].screen_x == NUM_COLS) {
term[term_live].screen_y++;
term[term_live].screen_x = 0;
}
if(term[term_live].screen_y == NUM_ROWS) {
scroll_up();
term[term_live].screen_y--;
term[term_live].screen_x=0;
}
}
}
/*
* int8_t* itoa(uint32_t value, int8_t* buf, int32_t radix);
* Inputs: uint32_t value = number to convert
* int8_t* buf = allocated buffer to place string in
* int32_t radix = base system. hex, oct, dec, etc.
* Return Value: number of bytes written
* Function: Convert a number to its ASCII representation, with base "radix"
*/
int8_t*
itoa(uint32_t value, int8_t* buf, int32_t radix)
{
static int8_t lookup[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int8_t *newbuf = buf;
int32_t i;
uint32_t newval = value;
/* Special case for zero */
if(value == 0) {
buf[0]='0';
buf[1]='\0';
return buf;
}
/* Go through the number one place value at a time, and add the
* correct digit to "newbuf". We actually add characters to the
* ASCII string from lowest place value to highest, which is the
* opposite of how the number should be printed. We'll reverse the
* characters later. */
while(newval > 0) {
i = newval % radix;
*newbuf = lookup[i];
newbuf++;
newval /= radix;
}
/* Add a terminating NULL */
*newbuf = '\0';
/* Reverse the string and return */
return strrev(buf);
}
/*
* int8_t* strrev(int8_t* s);
* Inputs: int8_t* s = string to reverse
* Return Value: reversed string
* Function: reverses a string s
*/
int8_t*
strrev(int8_t* s)
{
register int8_t tmp;
register int32_t beg=0;
register int32_t end=strlen(s) - 1;
while(beg < end) {
tmp = s[end];
s[end] = s[beg];
s[beg] = tmp;
beg++;
end--;
}
return s;
}
/*
* uint32_t strlen(const int8_t* s);
* Inputs: const int8_t* s = string to take length of
* Return Value: length of string s
* Function: return length of string s
*/
uint32_t
strlen(const int8_t* s)
{
register uint32_t len = 0;
while(s[len] != '\0')
len++;
return len;
}
/*
* void* memset(void* s, int32_t c, uint32_t n);
* Inputs: void* s = pointer to memory
* int32_t c = value to set memory to
* uint32_t n = number of bytes to set
* Return Value: new string
* Function: set n consecutive bytes of pointer s to value c
*/
void*
memset(void* s, int32_t c, uint32_t n)
{
c &= 0xFF;
asm volatile(" \n\
.memset_top: \n\
testl %%ecx, %%ecx \n\
jz .memset_done \n\
testl $0x3, %%edi \n\
jz .memset_aligned \n\
movb %%al, (%%edi) \n\
addl $1, %%edi \n\
subl $1, %%ecx \n\
jmp .memset_top \n\
.memset_aligned: \n\
movw %%ds, %%dx \n\
movw %%dx, %%es \n\
movl %%ecx, %%edx \n\
shrl $2, %%ecx \n\
andl $0x3, %%edx \n\
cld \n\
rep stosl \n\
.memset_bottom: \n\
testl %%edx, %%edx \n\
jz .memset_done \n\
movb %%al, (%%edi) \n\
addl $1, %%edi \n\
subl $1, %%edx \n\
jmp .memset_bottom \n\
.memset_done: \n\
"
:
: "a"(c << 24 | c << 16 | c << 8 | c), "D"(s), "c"(n)
: "edx", "memory", "cc"
);
return s;
}
/*
* void* memset_word(void* s, int32_t c, uint32_t n);
* Inputs: void* s = pointer to memory
* int32_t c = value to set memory to
* uint32_t n = number of bytes to set
* Return Value: new string
* Function: set lower 16 bits of n consecutive memory locations of pointer s to value c
*/
/* Optimized memset_word */
void*
memset_word(void* s, int32_t c, uint32_t n)
{
asm volatile(" \n\
movw %%ds, %%dx \n\
movw %%dx, %%es \n\
cld \n\
rep stosw \n\
"
:
: "a"(c), "D"(s), "c"(n)
: "edx", "memory", "cc"
);
return s;
}
/*
* void* memset_dword(void* s, int32_t c, uint32_t n);
* Inputs: void* s = pointer to memory
* int32_t c = value to set memory to
* uint32_t n = number of bytes to set
* Return Value: new string
* Function: set n consecutive memory locations of pointer s to value c
*/
void*
memset_dword(void* s, int32_t c, uint32_t n)
{
asm volatile(" \n\
movw %%ds, %%dx \n\
movw %%dx, %%es \n\
cld \n\
rep stosl \n\
"
:
: "a"(c), "D"(s), "c"(n)
: "edx", "memory", "cc"
);
return s;
}
/*
* void* memcpy(void* dest, const void* src, uint32_t n);
* Inputs: void* dest = destination of copy
* const void* src = source of copy
* uint32_t n = number of byets to copy
* Return Value: pointer to dest
* Function: copy n bytes of src to dest
*/
void*
memcpy(void* dest, const void* src, uint32_t n)
{
asm volatile(" \n\
.memcpy_top: \n\
testl %%ecx, %%ecx \n\
jz .memcpy_done \n\
testl $0x3, %%edi \n\
jz .memcpy_aligned \n\
movb (%%esi), %%al \n\
movb %%al, (%%edi) \n\
addl $1, %%edi \n\
addl $1, %%esi \n\
subl $1, %%ecx \n\
jmp .memcpy_top \n\
.memcpy_aligned: \n\
movw %%ds, %%dx \n\
movw %%dx, %%es \n\
movl %%ecx, %%edx \n\
shrl $2, %%ecx \n\
andl $0x3, %%edx \n\
cld \n\
rep movsl \n\
.memcpy_bottom: \n\
testl %%edx, %%edx \n\
jz .memcpy_done \n\
movb (%%esi), %%al \n\
movb %%al, (%%edi) \n\
addl $1, %%edi \n\
addl $1, %%esi \n\
subl $1, %%edx \n\
jmp .memcpy_bottom \n\
.memcpy_done: \n\
"
:
: "S"(src), "D"(dest), "c"(n)
: "eax", "edx", "memory", "cc"
);
return dest;
}
/*
* void* memmove(void* dest, const void* src, uint32_t n);
* Inputs: void* dest = destination of move
* const void* src = source of move
* uint32_t n = number of byets to move
* Return Value: pointer to dest
* Function: move n bytes of src to dest
*/
/* Optimized memmove (used for overlapping memory areas) */
void*
memmove(void* dest, const void* src, uint32_t n)
{
asm volatile(" \n\
movw %%ds, %%dx \n\
movw %%dx, %%es \n\
cld \n\
cmp %%edi, %%esi \n\
jae .memmove_go \n\
leal -1(%%esi, %%ecx), %%esi \n\
leal -1(%%edi, %%ecx), %%edi \n\
std \n\
.memmove_go: \n\
rep movsb \n\
"
:
: "D"(dest), "S"(src), "c"(n)
: "edx", "memory", "cc"
);
return dest;
}
/*
* int32_t strncmp(const int8_t* s1, const int8_t* s2, uint32_t n)
* Inputs: const int8_t* s1 = first string to compare
* const int8_t* s2 = second string to compare
* uint32_t n = number of bytes to compare
* Return Value: A zero value indicates that the characters compared
* in both strings form the same string.
* A value greater than zero indicates that the first
* character that does not match has a greater value
* in str1 than in str2; And a value less than zero
* indicates the opposite.
* Function: compares string 1 and string 2 for equality
*/
int32_t
strncmp(const int8_t* s1, const int8_t* s2, uint32_t n)
{
int32_t i;
for(i=0; i<n; i++) {
if( (s1[i] != s2[i]) ||
(s1[i] == '\0') /* || s2[i] == '\0' */ ) {
/* The s2[i] == '\0' is unnecessary because of the short-circuit
* semantics of 'if' expressions in C. If the first expression
* (s1[i] != s2[i]) evaluates to false, that is, if s1[i] ==
* s2[i], then we only need to test either s1[i] or s2[i] for
* '\0', since we know they are equal. */
return s1[i] - s2[i];
}
}
return 0;
}
/*
* int8_t* strcpy(int8_t* dest, const int8_t* src)
* Inputs: int8_t* dest = destination string of copy
* const int8_t* src = source string of copy
* Return Value: pointer to dest
* Function: copy the source string into the destination string
*/
int8_t*
strcpy(int8_t* dest, const int8_t* src)
{
int32_t i=0;
while(src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return dest;
}
/*
* int8_t* strcpy(int8_t* dest, const int8_t* src, uint32_t n)
* Inputs: int8_t* dest = destination string of copy
* const int8_t* src = source string of copy
* uint32_t n = number of bytes to copy
* Return Value: pointer to dest
* Function: copy n bytes of the source string into the destination string
*/
int8_t*
strncpy(int8_t* dest, const int8_t* src, uint32_t n)
{
int32_t i=0;
while(src[i] != '\0' && i < n) {
dest[i] = src[i];
i++;
}
while(i < n) {
dest[i] = '\0';
i++;
}
return dest;
}
/*
* void test_interrupts(void)
* Inputs: void
* Return Value: void
* Function: increments video memory. To be used to test rtc
*/
void
test_interrupts(void)
{
char * test="1";
terminal_write(0,test,1);
}