143 lines
2.9 KiB
C
Executable File
143 lines
2.9 KiB
C
Executable File
#include <kernel/init.h>
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <sys/io.h>
|
|
#include "gdt.h"
|
|
|
|
int tss[16][2];
|
|
|
|
int test_libc(void) {
|
|
int failed = 0;
|
|
|
|
printf("==Testing stdio.h==\n");
|
|
|
|
printf("printf(\"%%d\", 10): %d (expected 10)\n", 10);
|
|
printf("printf(\"%%o\", 14): %o (expected 16)\n", 14);
|
|
printf("printf(\"%%u\", 10): %u (expected 10)\n", 10);
|
|
printf("printf(\"%%x\", 26): %x (expected 1a)\n", 26);
|
|
printf("printf(\"%%X\", 0xC9A): %x (expected C9A)\n", 0xC9A);
|
|
printf("printf(\"%%p\", &i): %p (expected 0x[hex])\n", &failed);
|
|
printf("printf(\"%%n\"): %n (expected 14)\n");
|
|
|
|
printf("==Testing math.h==\n");
|
|
if(cos(0) == 1.0) {
|
|
printf("[");
|
|
/* I want the status to be coloured. How do I do that? */
|
|
printf("OK");
|
|
printf("] cos()\n");
|
|
}
|
|
else {
|
|
printf("[");
|
|
printf("FAIL");
|
|
printf("] cos()\n");
|
|
failed++;
|
|
}
|
|
|
|
if(fabs(-2.3) == 2.3) {
|
|
printf("[");
|
|
printf("OK");
|
|
printf("] fabs()\n");
|
|
}
|
|
else {
|
|
printf("[");
|
|
printf("FAIL");
|
|
printf("] fabs()\n");
|
|
failed++;
|
|
}
|
|
|
|
printf("==Testing time.h==\n");
|
|
printf("time(0): Current UNIX time: %d\n", time(0));
|
|
|
|
printf("==Testing stdlib.h==\n");
|
|
|
|
if(abs(-5) == 5) {
|
|
printf("[");
|
|
printf("OK");
|
|
printf("] abs()\n");
|
|
}
|
|
else {
|
|
printf("[");
|
|
printf("FAIL");
|
|
printf("] abs(); expected 5, got %d\n", abs(-5));
|
|
failed++;
|
|
}
|
|
|
|
srand(time(0));
|
|
int rand_test = rand();
|
|
printf("rand() output with seed time(0): %d\n", rand_test);
|
|
|
|
printf("==Testing string.h==\n");
|
|
|
|
if(strcmp("Hi", "Hi") == 0 && strcmp("Hi", "Hello") != 0) {
|
|
printf("[");
|
|
printf("OK");
|
|
printf("] strcmp()\n");
|
|
}
|
|
else {
|
|
printf("[");
|
|
printf("FAIL");
|
|
printf("] strcmp()\n");
|
|
failed++;
|
|
}
|
|
|
|
if(strcspn("Hello!", "!e") == 1) {
|
|
printf("[");
|
|
printf("OK");
|
|
printf("] strcspn()\n");
|
|
}
|
|
else {
|
|
printf("[");
|
|
printf("FAIL");
|
|
printf("] strcspn(); expected 1, got %d\n", strcspn("Hello!", "!e"));
|
|
failed++;
|
|
}
|
|
|
|
printf("Tests failed: %d\n", failed);
|
|
|
|
return failed;
|
|
}
|
|
|
|
void NMI_disable(void) {
|
|
outb(0x70, inb(0x70) | 0x80);
|
|
}
|
|
|
|
void NMI_enable(void) {
|
|
outb(0x70, inb(0x70) & 0x7F);
|
|
}
|
|
|
|
void A20_enable(void) {
|
|
unsigned short int a = inb(0x92);
|
|
if((a & 2) != 0) {
|
|
return;
|
|
}
|
|
a |= 2;
|
|
outb(a, 0x92);
|
|
}
|
|
|
|
void setup_gdt(void) {
|
|
struct GDT gdt[4];
|
|
|
|
gdt[0].base = 0; gdt[0].limit = 0; gdt[0].type = 0;
|
|
gdt[1].base = 0; gdt[1].limit = 0xFFFFFFFF; gdt[1].type = 0x9A;
|
|
gdt[2].base = 0; gdt[2].limit = 0xFFFFFFFF; gdt[2].type = 0x92;
|
|
gdt[3].base = (unsigned int) tss; gdt[3].limit=sizeof(tss); gdt[3].type = 0x89;
|
|
|
|
unsigned short int GDT[4];
|
|
encode_gdt_entry(&GDT[0], gdt[0]);
|
|
encode_gdt_entry(&GDT[1], gdt[1]);
|
|
encode_gdt_entry(&GDT[2], gdt[2]);
|
|
encode_gdt_entry(&GDT[3], gdt[3]);
|
|
|
|
set_gdt(GDT, sizeof(GDT));
|
|
}
|
|
|
|
int init(void) {
|
|
NMI_disable();
|
|
A20_enable();
|
|
setup_gdt();
|
|
printf("Fenix Dev Pre-release v0.0.3\n");
|
|
return test_libc();
|
|
}
|