# HG changeset patch
# User John Tsiombikas <nuclear@member.fsf.org>
# Date 1291233762 -7200
# Node ID 662ff2170531f7c2ffe6550ffddd17ae5f5b7f67

starting the kernel

diff -r 000000000000 -r 662ff2170531 Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,25 @@
+# collect all of our C and assembly source files
+csrc = $(wildcard src/*.c) $(wildcard src/klibc/*.c)
+asmsrc = $(wildcard src/*.S) $(wildcard src/klibc/*.S)
+
+# each source file will generate one object file
+obj = $(csrc:.c=.o) $(asmsrc:.S=.o)
+
+CC = gcc
+
+# -nostdinc instructs the compiler to ignore standard include directories
+# -m32 instructs the compiler to produce 32bit code (in case we have a 64bit compiler)
+CFLAGS = -m32 -Wall -g -nostdinc -fno-builtin -Isrc -Isrc/klibc
+ASFLAGS = -m32 -g -nostdinc -fno-builtin -Isrc -Isrc/klibc
+
+bin = kernel.elf
+
+# default target: make an ELF binary by linking the object files
+# we need to specify where to assume the text segment (code) is going
+# in memory, as well as the kernel entry point (kstart).
+$(bin): $(obj)
+	ld -melf_i386 -o $@ -Ttext 0x200000 -e _start $(obj)
+
+.PHONY: clean
+clean:
+	rm -f $(obj) $(bin)
diff -r 000000000000 -r 662ff2170531 src/klibc/inttypes.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/klibc/inttypes.h	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,12 @@
+#ifndef INTTYPES_H_
+#define INTTYPES_H_
+
+typedef char int8_t;
+typedef short int16_t;
+typedef int int32_t;
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+
+#endif	/* INTTYPES_H_ */
diff -r 000000000000 -r 662ff2170531 src/klibc/stdlib.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/klibc/stdlib.h	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,9 @@
+#ifndef STDLIB_H_
+#define STDLIB_H_
+
+#include <inttypes.h>
+
+typedef int32_t ssize_t;
+typedef uint32_t size_t;
+
+#endif	/* STDLIB_H_ */
diff -r 000000000000 -r 662ff2170531 src/klibc/string.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/klibc/string.c	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,11 @@
+#include <string.h>
+
+void memset(void *s, int c, size_t sz)
+{
+	int i;
+	char *ptr = s;
+
+	for(i=0; i<sz; i++) {
+		*ptr++ = c;
+	}
+}
diff -r 000000000000 -r 662ff2170531 src/klibc/string.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/klibc/string.h	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,8 @@
+#ifndef STRING_H_
+#define STRING_H_
+
+#include <stdlib.h>
+
+void memset(void *s, int c, size_t sz);
+
+#endif	/* STRING_H_ */
diff -r 000000000000 -r 662ff2170531 src/main.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main.c	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,10 @@
+/*#include <stdio.h>*/
+#include "vid.h"
+
+void kmain(void)
+{
+	clear_scr();
+	put_char('a', 0, 0, 4, 0);
+	put_char('b', 40, 12, 1, 7);
+	/*printf("Hello world from kernel space\n");*/
+}
diff -r 000000000000 -r 662ff2170531 src/mboot.S
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mboot.S	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,32 @@
+#define MAGIC		0x1badb002
+#define FLAGS		0
+#define STACK_SIZE	0x4000
+
+	.text
+	.globl _start
+_start:
+	/* jump over the multiboot header onto the startup code */
+	jmp kentry
+
+	.align 4
+	/* multiboot header */
+mboot_hdr:
+	.long MAGIC
+	.long FLAGS
+	.long -(MAGIC + FLAGS)	/* checksum */
+	.fill 5, 4	/* fill out the rest with zeroes */
+
+kentry:
+	/* setup a temporary kernel stack */
+	movl $(stack + STACK_SIZE), %esp
+	/* reset eflags register */
+	pushl $0
+	popf
+	/* call the kernel main function */
+	call kmain
+	/* we dropped out of main, halt the CPU */
+	cli
+	hlt
+
+	/* space for the temporary kernel stack */
+	.comm stack, STACK_SIZE
diff -r 000000000000 -r 662ff2170531 src/vid.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/vid.c	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,26 @@
+#include <string.h>
+#include "vid.h"
+
+#define WIDTH	80
+#define HEIGHT	25
+static uint16_t *vmem = (uint16_t*)0xb8000;
+
+void clear_scr(void)
+{
+	memset(vmem, 0, WIDTH * HEIGHT * 2);
+}
+
+void set_cursor(int x, int y)
+{
+	if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
+		/* disable cursor */
+		return;
+	}
+	/* set cursor position */
+}
+
+void put_char(char c, int x, int y, int fg, int bg)
+{
+	uint16_t attr = (fg & 0xf) | ((bg & 7) << 4);
+	vmem[y * 80 + x] = (uint16_t)c | (attr << 8);
+}
diff -r 000000000000 -r 662ff2170531 src/vid.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/vid.h	Wed Dec 01 22:02:42 2010 +0200
@@ -0,0 +1,8 @@
+#ifndef VID_H_
+#define VID_H_
+
+void clear_scr(void);
+void set_cursor(int x, int y);
+void put_char(char c, int x, int y, int fg, int bg);
+
+#endif	/* VID_H_ */