source: libcaca/trunk/kernel/kernel.c @ 573

Last change on this file since 573 was 573, checked in by Sam Hocevar, 15 years ago
  • Added assembly code for inb() and outb().
  • Print a small message at kernel boot time just to check that the kernel was properly loaded.
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
3 *  libcaca       ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
5 *                All Rights Reserved
6 *
7 *  This library is free software; you can redistribute it and/or
8 *  modify it under the terms of the Do What The Fuck You Want To
9 *  Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/** \file kernel.c
14 *  \version \$Id: kernel.c 573 2006-03-09 15:35:00Z sam $
15 *  \author Sam Hocevar <sam@zoy.org>
16 *  \brief Kernel features
17 *
18 *  This file contains replacement functions for the standard C library
19 *  that must be used when building libcucul and libcaca into a kernel.
20 */
21
22#include "config.h"
23
24#ifdef __KERNEL__
25
26/* Our memory mapping */
27static uint32_t *freemem = (uint32_t*) 0x00200000;
28
29/* Multiboot kernel entry point */
30void cmain(unsigned long int magic, unsigned long int addr)
31{
32    static char const text[] = "Booting libcaca kernel...";
33    char const *ptr = text;
34    char *video = (char*)0xB8000;
35
36    char *argv[] = { NULL };
37    int argc = 0;
38
39    /* Print a text message to say hello */
40    while(*ptr)
41        *video = *ptr++; video += 2;
42
43    /* Launch the main program */
44    main(argc, argv);
45}
46
47/* stdlib.h functions */
48void *malloc(size_t size)
49{
50    uint32_t *p = freemem;
51    size = (size + 0x7) / 4;
52    *p = size;
53    freemem += size + 1;
54    return p + 1;
55}
56
57void free(void *ptr)
58{
59    return;
60}
61
62void *realloc(void *ptr, size_t size)
63{
64    uint32_t oldsize = ((uint32_t *)ptr)[-1];
65    void *p;
66    if(oldsize >= size)
67        return ptr;
68
69    p = malloc(size);
70    memcpy(p, ptr, oldsize);
71    return p;
72}
73
74char *getenv(const char *name)
75{
76    return NULL;
77}
78
79int rand(void)
80{
81    static int seed = 0x68743284;
82    seed = (seed * 0x7f32ba17) ^ 0xf893a735;
83    return seed % RAND_MAX;
84}
85
86int abs(int j)
87{
88    if(j < 0)
89        return -j;
90    return j;
91}
92
93void exit(int status)
94{
95    /* FIXME: reboot? */
96    while(1);
97}
98
99/* string.h functions */
100void *memset(void *s, int c, size_t n)
101{
102    uint8_t *ptr = s;
103
104    while(n--)
105        *ptr++ = c;
106
107    return s;
108}
109
110void *memcpy(void *dest, const void *src, size_t n)
111{
112    uint8_t *destptr = dest;
113    uint8_t const *srcptr = src;
114
115    while(n--)
116        *destptr++ = *srcptr++;
117
118    return dest;
119}
120
121size_t strlen(const char *s)
122{
123    int len = 0;
124
125    while(*s++)
126        len++;
127
128    return len;
129}
130
131int strcasecmp(const char *s1, const char *s2)
132{
133    /* FIXME */
134    return -1;
135}
136
137/* stdarg.h functions */
138int vsnprintf(char *str, size_t size, const char *format, va_list ap)
139{
140    /* FIXME */
141    return 0;
142}
143
144/* stdio.h functions */
145FILE *fopen(const char *path, const char *mode)
146{
147    /* FIXME */
148    return NULL;
149}
150
151int feof(FILE *stream)
152{
153    /* FIXME */
154    return 0;
155}
156
157char *fgets(char *s, int size, FILE *stream)
158{
159    /* FIXME */
160    return NULL;
161}
162
163int fclose(FILE *fp)
164{
165    /* FIXME */
166    return 0;
167}
168
169int printf(const char *format, ...)
170{
171    /* FIXME */
172    return 0;
173}
174
175int sprintf(char *str, const char *format, ...)
176{
177    /* FIXME */
178    return 0;
179}
180
181int sscanf(const char *str, const char *format, ...)
182{
183    /* FIXME */
184    return 0;
185}
186
187/* unistd.h functions */
188void usleep(unsigned long usec)
189{
190    /* FIXME */
191    return;
192}
193
194/* time.h functions */
195int gettimeofday(struct timeval *tv, struct timezone *tz)
196{
197    static int usec = 0;
198    static int sec = 0;
199
200    /* FIXME */
201    usec += 10000;
202    if(usec > 1000000)
203    {
204        sec++;
205        usec -= 1000000;
206    }
207
208    tv->tv_sec = sec;
209    tv->tv_usec = usec;
210
211    return 0;
212}
213
214#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.