Skip to content

Commit

Permalink
Implement "Hello, world!" demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Mar 27, 2024
1 parent 052eed9 commit 9d3dab5
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
*.elf
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
BUILDDIR = build

all: githooks $(BUILDDIR)/kernel.elf

githooks: | hooks
@git config core.hooksPath hooks/

$(BUILDDIR):
@mkdir $(BUILDDIR)

clean:
@$(RM) -rf $(BUILDDIR)

CC = $(CROSS_PREFIX)gcc

SRCS = $(wildcard src/*.S src/*.c)
OBJS = $(patsubst src/%.S,$(BUILDDIR)/%.o,$(SRCS))
OBJS := $(patsubst src/%.c,$(BUILDDIR)/%.o,$(OBJS))

CFLAGS = -I include/ -mcmodel=medany
LDFLAGS = -ffreestanding -nostdlib

$(BUILDDIR)/%.o: src/%.S | $(BUILDDIR)
@printf " CCAS\t$(@F)\n"
@$(CC) $< -c -o $@ $(CFLAGS)

$(BUILDDIR)/%.o: src/%.c | $(BUILDDIR)
@printf " CC\t$(@F)\n"
@$(CC) $< -c -o $@ $(CFLAGS)

$(BUILDDIR)/kernel.elf: $(OBJS) kernel.ld | $(BUILDDIR)
@printf " CCLD\t$(@F)\n"
@$(CC) $(OBJS) -o $@ -T kernel.ld $(LDFLAGS)

run: $(BUILDDIR)/kernel.elf
@qemu-system-riscv64 -M virt -nographic -kernel $<
3 changes: 3 additions & 0 deletions include/attribs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include <stdnoreturn.h>
3 changes: 3 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#define KSTACK_BASE 0x88000000
17 changes: 17 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

typedef uintptr_t usize;
typedef intptr_t ssize;
33 changes: 33 additions & 0 deletions kernel.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ENTRY(_start)

SECTIONS {
. = 0x80200000;

.init : {
*(.init .init.*)
}

.text : {
*(.text .text.*)
}

.rodata : {
*(.rodata .rodata.*)
}

.data : {
*(.data .data.*)
}

.sdata : {
*(.sdata .sdata.*)
}

.sbss : {
*(.sbss .sbss.*)
}

.bss : {
*(.bss .bss.*)
}
}
8 changes: 8 additions & 0 deletions src/entry.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <config.h>

.section .init

.global _start
_start:
li sp, KSTACK_BASE
tail kmain
13 changes: 13 additions & 0 deletions src/kmain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <attribs.h>
#include <types.h>

noreturn void kmain(void) {
volatile u8 *uart = (volatile u8 *) 0x10000000;
const char *s = "Hello, world!\n";

for (int i = 0; s[i] != '\0'; i++)
*uart = s[i];

while (true)
;
}

0 comments on commit 9d3dab5

Please sign in to comment.