move C library to separate module
This commit is contained in:
parent
3f162e2cfa
commit
7f5ad11115
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.o
|
||||
kernel
|
24
Makefile
24
Makefile
|
@ -1,26 +1,24 @@
|
|||
CFLAGS= -m32 -Wfatal-errors -nostdlib -Wno-builtin-declaration-mismatch
|
||||
ASFLAGS= --32
|
||||
LDFLAGS= -melf_i386 -nostdlib -e loader -T link.ld
|
||||
OBJS=kernel.bin
|
||||
OBJS=loader.o kernel.o c.o
|
||||
|
||||
all: $(OBJS)
|
||||
all: kernel
|
||||
|
||||
kernel.bin: loader.o kernel.o
|
||||
@echo " LD kernel.bin"
|
||||
@$(LD) $(LDFLAGS) $^ -o $@
|
||||
run: kernel
|
||||
qemu-system-i386 -enable-kvm -m 4g -kernel $^
|
||||
|
||||
kernel: $(OBJS)
|
||||
$(LD) $(LDFLAGS) $^ -o $@
|
||||
|
||||
install: $(OBJS)
|
||||
@echo " CP kernel.bin"
|
||||
@cp ./kernel.bin /boot/mykernel.bin
|
||||
cp ./kernel /boot/mykernel
|
||||
|
||||
%.o: %.c
|
||||
@echo " CC $@"
|
||||
@$(CC) $(CFLAGS) -c $^
|
||||
$(CC) $(CFLAGS) -c $^
|
||||
|
||||
%.o: %.s
|
||||
@echo " AS $@"
|
||||
@$(AS) $(ASFLAGS) -c $^ -o $@
|
||||
$(AS) $(ASFLAGS) -c $^ -o $@
|
||||
|
||||
clean:
|
||||
@echo " RM *.o kernel.bin"
|
||||
@$(RM) *.o kernel.bin
|
||||
$(RM) $(OBJS) kernel
|
||||
|
|
40
c.c
Normal file
40
c.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include"c.h"
|
||||
|
||||
FILE*stdout=&(FILE){.fd=0,.offset=(unsigned short*)0xb8000,.seek=0};
|
||||
|
||||
void fputc(char c,FILE*f)
|
||||
{
|
||||
if(c=='\n')
|
||||
f->seek=(f->seek+WIDTH)/WIDTH*WIDTH;
|
||||
else
|
||||
f->offset[f->seek++]=0x1f00|c;
|
||||
}
|
||||
|
||||
// puts to FILE
|
||||
void fputs(FILE*f,char*s)
|
||||
{
|
||||
for(unsigned i=0;s[i];++i)
|
||||
//*f->offset++=0x1f00|s[i];
|
||||
fputc(s[i],f);
|
||||
fputc('\n',f);
|
||||
}
|
||||
|
||||
// print integer
|
||||
void printif(FILE*f,int d)
|
||||
{
|
||||
char b[32];
|
||||
int i=0;
|
||||
|
||||
// Get each digit as character
|
||||
while(d>0)
|
||||
{
|
||||
int m=d%10;
|
||||
d/=10;
|
||||
b[i++]=m+'0';
|
||||
}
|
||||
b[i]=0;
|
||||
|
||||
// Print reversed string
|
||||
for(i;i>=0;--i)
|
||||
fputc(b[i],f);
|
||||
}
|
26
c.h
Normal file
26
c.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/****
|
||||
* Tiny standard C library
|
||||
* subset implementation
|
||||
****/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define SPACE " "
|
||||
#define WIDTH 80
|
||||
|
||||
// FILE type
|
||||
typedef struct FILE
|
||||
{
|
||||
unsigned fd;
|
||||
unsigned short*offset;
|
||||
unsigned int seek;
|
||||
} FILE;
|
||||
|
||||
extern FILE*stdout;
|
||||
|
||||
void fputc(char c,FILE*f);
|
||||
void fputs(FILE*f,char*s);
|
||||
void printif(FILE*f,int d);
|
||||
|
||||
#define puts(x) fputs(stdout,x)
|
||||
#define printi(x) printif(stdout,x)
|
56
kernel.c
56
kernel.c
|
@ -1,58 +1,4 @@
|
|||
#define SPACE " "
|
||||
#define WIDTH 80
|
||||
|
||||
// FILE type
|
||||
typedef struct FILE
|
||||
{
|
||||
unsigned fd;
|
||||
unsigned short*offset;
|
||||
unsigned int seek;
|
||||
} FILE;
|
||||
|
||||
FILE*stdout=&(FILE){.fd=0,.offset=(unsigned short*)0xb8000,.seek=0};
|
||||
|
||||
void fputc(char c,FILE*f)
|
||||
{
|
||||
if(c=='\n')
|
||||
f->seek=(f->seek+WIDTH)/WIDTH*WIDTH;
|
||||
else
|
||||
f->offset[f->seek++]=0x1f00|c;
|
||||
}
|
||||
|
||||
// puts to FILE
|
||||
void fputs(FILE*f,char*s)
|
||||
{
|
||||
for(unsigned i=0;s[i];++i)
|
||||
//*f->offset++=0x1f00|s[i];
|
||||
fputc(s[i],f);
|
||||
fputc('\n',f);
|
||||
}
|
||||
|
||||
#define puts(x) fputs(stdout,x)
|
||||
|
||||
// print integer
|
||||
void printif(FILE*f,int d)
|
||||
{
|
||||
char b[32];
|
||||
int i=0;
|
||||
|
||||
// Get each digit as character
|
||||
while(d>0)
|
||||
{
|
||||
int m=d%10;
|
||||
d/=10;
|
||||
b[i++]=m+'0';
|
||||
}
|
||||
b[i]=0;
|
||||
|
||||
// Print reversed string
|
||||
for(i;i>=0;--i)
|
||||
fputc(b[i],f);
|
||||
}
|
||||
|
||||
#define printi(x) printif(stdout,x)
|
||||
|
||||
|
||||
#include"c.h"
|
||||
|
||||
// Entry point
|
||||
void kernelmain(void*multiboot_structure,unsigned magicnumber)
|
||||
|
|
Loading…
Reference in New Issue
Block a user