-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.C
executable file
·167 lines (135 loc) · 4.93 KB
/
utils.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
/*
File: utils.C
Author: R. Bettati
Department of Computer Science
Texas A&M University
Date : 09/02/12
*/
/* Some of the code comes from Brandon Friesens OS Tutorial:
* bkerndev - Bran's Kernel Development Tutorial
* By: Brandon F. ([email protected])
* Desc: Interrupt Descriptor Table management
*
* Notes: No warranty expressed or implied. Use at own risk. */
/*--------------------------------------------------------------------------*/
/* DEFINES */
/*--------------------------------------------------------------------------*/
/* -- (none ) -- */
/*--------------------------------------------------------------------------*/
/* INCLUDES */
/*--------------------------------------------------------------------------*/
#include "utils.H"
/*--------------------------------------------------------------------------*/
/* DATA STRUCTURES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* CONSTANTS */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* ABORT (USED e.g. IN _ASSERT() */
/*--------------------------------------------------------------------------*/
void abort() {
for(;;);
}
/*--------------------------------------------------------------------------*/
/* MEMORY OPERATIONS */
/*--------------------------------------------------------------------------*/
void *memcpy(void *dest, const void *src, int count)
{
const char *sp = (const char *)src;
char *dp = (char *)dest;
for(; count != 0; count--) *dp++ = *sp++;
return dest;
}
void *memset(void *dest, char val, int count)
{
char *temp = (char *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{
unsigned short *temp = (unsigned short *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
/*--------------------------------------------------------------------------*/
/* STRING OPERATIONS */
/*--------------------------------------------------------------------------*/
int strlen(const char *_str) {
/* This loops through character array 'str', returning how
* many characters it needs to check before it finds a 0.
* In simple words, it returns the length in bytes of a string */
int len = 0;
while (*_str != 0) {
_str++;
len++;
}
return len;
}
void strcpy(char* _dst, char* _src) {
while (*_src != 0) {
*_dst = *_src;
_dst++;
_src++;
}
*_dst = 0; // put terminating 0 at end.
}
void int2str(int _num, char * _str) {
/* -- THIS IMPLEMENTATION IS ONE PRETTY BAD HACK. */
int i;
char temp[11];
temp[0] = '\0';
for(i = 1; i <= 10; i++) {
temp[i] = _num % 10 + '0';
_num /= 10;
}
for(i = 10; temp[i] == '0'; i--);
if( i == 0 )
i++;
while( i >= 0 )
*_str++ = temp[i--];
}
void uint2str(unsigned int _num, char * _str) {
/* -- THIS IS A BAD HACK AS WELL. */
int i;
char temp[11];
temp[0] = '\0';
for(i = 1; i <= 10; i++) {
temp[i] = _num % 10 + '0';
_num /= 10;
}
for(i = 10; temp[i] == '0'; i--);
if( i == 0 )
i++;
while( i >= 0 )
*_str++ = temp[i--];
}
/*--------------------------------------------------------------------------*/
/* POERT I/O OPERATIONS */
/*--------------------------------------------------------------------------*/
/* We will use this later on for reading from the I/O ports to get data
* from devices such as the keyboard. We are using what is called
* 'inline assembly' in these routines to actually do the work */
char inportb (unsigned short _port) {
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
unsigned short inportw (unsigned short _port) {
unsigned short rv;
__asm__ __volatile__ ("inw %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
/* We will use this to write to I/O ports to send bytes to devices. This
* will be used in the next tutorial for changing the textmode cursor
* position. Again, we use some inline assembly for the stuff that simply
* cannot be done in C */
void outportb (unsigned short _port, char _data) {
__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
void outportw (unsigned short _port, unsigned short _data) {
__asm__ __volatile__ ("outw %1, %0" : : "dN" (_port), "a" (_data));
}