Skip to content

Commit

Permalink
init commit for os development
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanhui Zhao committed Oct 23, 2019
1 parent e93a8fb commit eacb639
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
OBJECTS = start.o scrn.o main.o
CC = gcc
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -I./include -c
LDFLAGS = -T link.ld -melf_i386
AS = nasm
ASFLAGS = -f elf

all: kernel

kernel: $(OBJECTS)
ld $(LDFLAGS) $(OBJECTS) -o kernel

kerndev.iso: kernel
cp kernel boot
grub-mkrescue -o kerndev.iso .

run: kerndev.iso
qemu-system-i386 -cdrom kerndev.iso

%.o: %.c
$(CC) $(CFLAGS) $< -o $@

%.o: %.asm
$(AS) $(ASFLAGS) $< -o $@

clean:
rm *.o kernel boot/kernel kerndev.iso
4 changes: 4 additions & 0 deletions boot/grub/grub.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
menuentry "kernel" {
multiboot /boot/kernel
}

18 changes: 18 additions & 0 deletions include/system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __SYSTEM_H
#define __SYSTEM_H

/* MAIN.C */
extern unsigned char *memecpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *emeset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
extern unsigned char inportb(unsigned short _port);
extern void outportb(unsigned short _port, unsigned char _data);

extern void cls();
extern void putch(unsigned char c);
extern void puts(unsigned char *str);
extern void settextcolor(unsigned char forecolor, unsigned char backcolor);
extern void init_video();

#endif
24 changes: 24 additions & 0 deletions link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}
61 changes: 61 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <system.h>

unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
{
const char *sp = (const char *)src;
char *dp = (char *)dest;
while (count > 0) {
*dp++ = *sp++;
count--;
}
return dest;

}

unsigned char *memset(unsigned char *dest, unsigned char val, int count)
{
char *temp = (char *)dest;
while (count > 0) {
*temp++ = val;
count--;
}
return dest;
}

unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{
unsigned short *temp = (unsigned short *)dest;
while (count > 0) {
*temp++ = val;
count--;
}
return dest;
}

int strlen(const char *str)
{
int retval;
for (retval = 0; *str != '\0'; str++)
retval++;
return retval;
}

unsigned char inportb(unsigned short _port)
{
unsigned char rv;
__asm__ __volatile__("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}

void outportb(unsigned short _port, unsigned char _data)
{
__asm__ __volatile__("outb %1, %0" : : "dN" (_port), "a" (_data));
}

void main()
{
init_video();
//__asm__ __volatile__("sti");
puts("Hello World!\n");
for(;;);
}
105 changes: 105 additions & 0 deletions scrn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <system.h>

unsigned short *textmemptr;
int attrib = 0x0F;
int csr_x = 0;
int csr_y = 0;

void scroll(void)
{
unsigned blank, temp;

blank = 0x20 | (attrib << 8);

if (csr_y >= 25)
{
temp = csr_y - 25 + 1;
memcpy(textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2);
memsetw(textmemptr + (25 - temp) * 80, blank, 80);
csr_y = 25 - 1;
}
}

void move_csr(void)
{
unsigned temp;
temp = csr_y * 80 + csr_x;

outportb(0x3D4, 14);
outportb(0x3D5, temp >> 8);
outportb(0x3D3, 15);
outportb(0x3D5, temp);
}

void cls()
{
unsigned blank;
int i;

blank = 0x20 | (attrib << 8);

for (i = 0; i < 25; i++)
memsetw(textmemptr + i * 80, blank, 80);

csr_x = 0;
csr_y = 0;
move_csr();
}

void putch(unsigned char c)
{
unsigned short *where;
unsigned att = attrib << 8;

if (c == 0x08)
{
if (csr_x != 0)
csr_x--;
}
else if (c == 0x09)
{
csr_x = (csr_x + 8) & ~(8 - 1);
}
else if (c == '\r')
{
csr_x = 0;
}
else if (c == '\n')
{
csr_x = 0;
csr_y++;
}
else if (c >= ' ')
{
where = textmemptr + (csr_y * 80 + csr_x);
*where = c | att;
csr_x++;
}

if (csr_x >= 80)
{
csr_x = 0;
csr_y++;
}

scroll();
move_csr();
}

void puts(unsigned char *text)
{
int i;
for (i = 0; i < strlen(text); i++)
putch(text[i]);
}

void settextcolor(unsigned char forecolor, unsigned char backcolor)
{
attrib = (backcolor << 4) | (forecolor & 0x0F);
}

void init_video(void)
{
textmemptr = (unsigned short *)0xB8000;
cls();
}
34 changes: 34 additions & 0 deletions start.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
global start
start:
mov esp, _sys_stack
jmp stublet

ALIGN 4
mboot:
MULTIBOOT_PAGE_ALIGN equ 1<<0
MULTIBOOT_MEMORY_INFO equ 1<<1
MULTIBOOT_AOUT_KLUDGE equ 1<<16
MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
EXTERN code, bss, end

dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_CHECKSUM

dd mboot
dd code
dd bss
dd end
dd start

stublet:
extern main
call main
;jmp $

SECTION .bss
resb 8192

_sys_stack:

0 comments on commit eacb639

Please sign in to comment.