-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.C
More file actions
158 lines (127 loc) · 4.38 KB
/
utils.C
File metadata and controls
158 lines (127 loc) · 4.38 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
/*
File: utils.C
Author: R. Bettati
Department of Computer Science
Texas A&M University
Date : 09/02/12
*/
/*--------------------------------------------------------------------------*/
/* DEFINES */
/*--------------------------------------------------------------------------*/
/* -- (none ) -- */
/*--------------------------------------------------------------------------*/
/* INCLUDES */
/*--------------------------------------------------------------------------*/
#include "utils.H"
/*--------------------------------------------------------------------------*/
/* DATA STRUCTURES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* CONSTANTS */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* ABORT (USED e.g. IN _ASSERT() */
/*--------------------------------------------------------------------------*/
void abort() {
for(;;);
}
/*--------------------------------------------------------------------------*/
/* MEMORY OPERATIONS */
/*--------------------------------------------------------------------------*/
char * memcpy(char * _dest, const char * _src, const int _count) {
for (int i = 0; i < _count; i++) {
*_dest = *_src;
_dest++;
_src++;
}
return _dest-1;
}
char *memset(char * _dest, const char _val, const int _count) {
for (int i = 0; i < _count; i++) {
*_dest = _val;
_dest++;
}
return _dest-1;
}
unsigned char *memsetw( unsigned char * _dest,
const unsigned short _val,
const int _count) {
for (int i = 0; i < _count; i++) {
*_dest = ((unsigned char*)(&_val))[0];
*(_dest +1) = ((unsigned char*)(&_val))[1];
_dest = _dest + 2;
}
return _dest-2;
}
/*--------------------------------------------------------------------------*/
/* 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 c, 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 c, 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;
}
/* 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));
}