1+ #include "common.h"
2+ #include "drivers/screen.h"
3+
4+ char tbuf [64 ];
5+ char tbuf_long [64 ];
6+ char bchars [] = {'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'A' ,'B' ,'C' ,'D' ,'E' ,'F' };
7+
8+ // Reads a single char (8 bytes) from the specified port
9+ unsigned char inb (unsigned short Port )
10+ {
11+ unsigned char ret ;
12+ asm volatile ("inb %1, %0" : "=a" (ret ) : "dN" (Port ));
13+
14+ return ret ;
15+ }
16+
17+ // Reads a single short (16 bytes) from the specific port
18+ unsigned short inw (unsigned short Port )
19+ {
20+ unsigned short ret ;
21+ asm volatile ("inw %1, %0" : "=a" (ret ) : "dN" (Port ));
22+
23+ return ret ;
24+ }
25+
26+ // Writes a single char (8 bytes) to the specified port
27+ void outb (unsigned short Port , unsigned char Value )
28+ {
29+ asm volatile ("outb %1, %0" : : "dN" (Port ), "a" (Value ));
30+ }
31+
32+ // Writes a single short (16 bytes) to the specified port
33+ void outw (unsigned short Port , unsigned short Value )
34+ {
35+ asm volatile ("outw %1, %0" : : "dN" (Port ), "a" (Value ));
36+ }
37+
38+ // Writes a single int (32 bytes) to the specified port
39+ void outl (unsigned short Port , unsigned int Value )
40+ {
41+ asm volatile ("outl %1, %0" : : "dN" (Port ), "a" (Value ));
42+ }
43+
44+ // A simple memset implementation
45+ void * memset (void * s , int c , long n )
46+ {
47+ unsigned char * p = s ;
48+
49+ while (n -- )
50+ * p ++ = (unsigned char )c ;
51+
52+ return s ;
53+ }
54+
55+ // A simple memcpy implementation
56+ void memcpy (void * dest , void * src , int len )
57+ {
58+ int i ;
59+ char * csrc = (char * )src ;
60+ char * cdest = (char * )dest ;
61+
62+ for (i = 0 ; i < len ; i ++ )
63+ {
64+ cdest [i ] = csrc [i ];
65+ }
66+ }
67+
68+ // Returns the length of the given string
69+ int strlen (char * string )
70+ {
71+ int len = 0 ;
72+
73+ while (* string != '\0' )
74+ {
75+ len ++ ;
76+ string ++ ;
77+ }
78+
79+ return len ;
80+ }
81+
82+ // A simple strcpy implementation
83+ char * strcpy (char * destination , const char * source )
84+ {
85+ // return if no memory is allocated to the destination
86+ if (destination == 0x0 )
87+ return 0x0 ;
88+
89+ // take a pointer pointing to the beginning of destination string
90+ char * ptr = destination ;
91+
92+ // copy the C-string pointed by source into the array pointed by destination
93+ while (* source != '\0' )
94+ {
95+ * destination = * source ;
96+ destination ++ ;
97+ source ++ ;
98+ }
99+
100+ // include the terminating null character
101+ * destination = '\0' ;
102+
103+ // destination is returned by standard strcpy()
104+ return ptr ;
105+ }
106+
107+ // A simple strcmp implementation
108+ int strcmp (char * s1 , char * s2 )
109+ {
110+ int i = 0 ;
111+ int len = strlen (s2 );
112+
113+ while (* s1 && (* s1 == * s2 ) && i < len )
114+ {
115+ s1 ++ ;
116+ s2 ++ ;
117+ i ++ ;
118+ }
119+
120+ return * (unsigned char * )s1 - * (unsigned char * )s2 ;
121+ }
122+
123+ // Returns a substring from a given string
124+ int substring (char * source , int from , int n , char * target )
125+ {
126+ int length ,i ;
127+ //get string length
128+ for (length = 0 ;source [length ]!= '\0' ;length ++ );
129+
130+ if (from > length ){
131+ printf ("Starting index is invalid.\n" );
132+ return 1 ;
133+ }
134+
135+ if ((from + n )> length ){
136+ //get substring till end
137+ n = (length - from );
138+ }
139+
140+ //get substring in target
141+ for (i = 0 ;i < n ;i ++ ){
142+ target [i ]= source [from + i ];
143+ }
144+ target [i ]= '\0' ; //assign null at last
145+
146+ return 0 ;
147+ }
148+
149+ // Returns the position of the specific character in the given string
150+ int find (char * string , char junk )
151+ {
152+ int pos = 0 ;
153+
154+ while (* string != junk )
155+ {
156+ pos ++ ;
157+ string ++ ;
158+ }
159+
160+ return pos ;
161+ }
162+
163+ // Checks if a string starts with a given prefix
164+ int startswith (char * string , char * prefix )
165+ {
166+ while (* prefix )
167+ {
168+ if (* prefix ++ != * string ++ )
169+ return 0 ;
170+ }
171+
172+ return 1 ;
173+ }
174+
175+ // Converts a string to upper case
176+ void toupper (char * s )
177+ {
178+ for (; * s ; s ++ )
179+ if (('a' <= * s ) && (* s <= 'z' ))
180+ * s = 'A' + (* s - 'a' );
181+ }
182+
183+ // Converts a string to lower case
184+ void tolower (char * s )
185+ {
186+ for (; * s ; s ++ )
187+ if (('A' <= * s ) && (* s <= 'Z' ))
188+ * s = 'a' + (* s - 'A' );
189+ }
190+
191+ // Converts an integer value to a string value for a specific base (base 10 => decimal, base 16 => hex)
192+ void itoa (int i , unsigned base , char * buf )
193+ {
194+ if (base > 16 ) return ;
195+
196+ if (i < 0 )
197+ {
198+ * buf ++ = '-' ;
199+ i *= -1 ;
200+ }
201+
202+ itoa_helper (i , base , buf );
203+ }
204+
205+ // Helper function for the itoa function.
206+ static void itoa_helper (unsigned short i , unsigned base , char * buf )
207+ {
208+ int pos = 0 ;
209+ int opos = 0 ;
210+ int top = 0 ;
211+
212+ if (i == 0 || base > 16 )
213+ {
214+ buf [0 ] = '0' ;
215+ buf [1 ] = '\0' ;
216+ return ;
217+ }
218+
219+ while (i != 0 )
220+ {
221+ tbuf [pos ] = bchars [i % base ];
222+ pos ++ ;
223+ i /= base ;
224+ }
225+
226+ top = pos -- ;
227+
228+ for (opos = 0 ; opos < top ; pos -- ,opos ++ )
229+ {
230+ buf [opos ] = tbuf [pos ];
231+ }
232+
233+ buf [opos ] = 0 ;
234+ }
235+
236+ // Converts a long value to a string value for a specific base (base 10 => decimal, base 16 => hex)
237+ void ltoa (unsigned long i , unsigned base , char * buf )
238+ {
239+ if (base > 16 ) return ;
240+
241+ if (i < 0 )
242+ {
243+ * buf ++ = '-' ;
244+ i *= -1 ;
245+ }
246+
247+ ltoa_helper (i , base , buf );
248+ }
249+
250+ // Helper function for the ltoa function.
251+ static void ltoa_helper (unsigned long i , unsigned base , char * buf )
252+ {
253+ int pos = 0 ;
254+ int opos = 0 ;
255+ int top = 0 ;
256+
257+ if (i == 0 || base > 16 )
258+ {
259+ buf [0 ] = '0' ;
260+ buf [1 ] = '\0' ;
261+ return ;
262+ }
263+
264+ while (i != 0 )
265+ {
266+ tbuf [pos ] = bchars [i % base ];
267+ pos ++ ;
268+ i /= base ;
269+ }
270+
271+ top = pos -- ;
272+
273+ for (opos = 0 ; opos < top ; pos -- ,opos ++ )
274+ {
275+ buf [opos ] = tbuf [pos ];
276+ }
277+
278+ buf [opos ] = 0 ;
279+ }
280+
281+ // Converts an ASCII string to its integer value
282+ int atoi (char * str )
283+ {
284+ int res = 0 ;
285+ int i ;
286+
287+ for (i = 0 ; str [i ] != '\0' ; ++ i )
288+ {
289+ res = res * 10 + str [i ] - '0' ;
290+ }
291+
292+ return res ;
293+ }
0 commit comments