Browse Source

Separated functionality into separate files. Added initial (untested) support for ARM.

master
L. Bradley LaBoon 8 years ago
parent
commit
da4cc8b645
  1. 34
      Makefile
  2. 33
      arch/arm-none-eabi/boot.s
  3. 42
      arch/arm-none-eabi/linker.ld
  4. 0
      arch/i586-elf/boot.s
  5. 0
      arch/i586-elf/linker.ld
  6. 91
      driver/video/vga.c
  7. 8
      include/brados/string.h
  8. 64
      include/video/vga.h
  9. 160
      kernel/brados.c
  10. 12
      lib/string.c

34
Makefile

@ -0,0 +1,34 @@
# Build environment
PREFIX ?= /home/brad/brados/brados-gcc
ARCH ?= i586-elf
GNU ?= $(PREFIX)/$(ARCH)/bin/$(ARCH)
# Source files
SOURCES_ASM := arch/$(ARCH)/boot.s
SOURCES_C := driver/video/vga.c kernel/brados.c lib/string.c
# Object files
OBJS := $(patsubst %.s,%.o,$(SOURCES_ASM))
OBJS += $(patsubst %.c,%.o,$(SOURCES_C))
# Build flags
CFLAGS = -std=gnu99 -ffreestanding -Iinclude/ -O2 -Wall -Wextra
LDFLAGS = -ffreestanding -O2 -nostdlib -lgcc
# Build rules
all: brados.bin
.PHONY: all clean
brados.bin: $(OBJS) arch/$(ARCH)/linker.ld
$(GNU)-gcc -T arch/$(ARCH)/linker.ld -o $@ $(LDFLAGS) $(OBJS)
clean:
rm -f $(OBJS) brados.bin
# C
%.o: %.c Makefile
$(GNU)-gcc -c $< -o $@ $(CFLAGS)
# Assembly
%.o: %.s Makefile
$(GNU)-as $< -o $@

33
arch/arm-none-eabi/boot.s

@ -0,0 +1,33 @@
.section ".text.boot"
.globl Start
Start:
// Setup the stack
mov sp, #0x8000
// Clear out bss
ldr r4, =_bss_start
ldr r9, =_bss_end
mov r5, #0
mov r6, #0
mov r7, #0
mov r8, #0
b 2f
1:
// Store multiple at r4
stmia r4!, {r5-r8}
2:
// If we are still below bss_end, loop
cmp r4, r9
blo 1b
// Call brados_main
ldr r3, =brados_main
blx r3
halt:
// Halt
wfe
b halt

42
arch/arm-none-eabi/linker.ld

@ -0,0 +1,42 @@
ENTRY(Start)
SECTIONS
{
/* Starts at LOADER_ADDR */
. = 0x8000
_start = .;
_text_start = .;
.text:
{
KEEP(*(.text.boot))
*(.text)
}
. = ALIGN(4096);
_text_end = .;
_rodata_start = .;
.rodata:
{
*(.rodata)
}
. = ALIGN(4096)
_rodata_end = .;
_data_start = .;
.data:
{
*(.data)
}
. = ALIGN(4096);
_data_end = .;
_bss_start = .;
.bss:
{
bss = .;
*(.bss)
}
. = ALIGN(4096)
_bss_end = .;
_end = .;
}

0
boot/boot.s → arch/i586-elf/boot.s

0
linker.ld → arch/i586-elf/linker.ld

91
driver/video/vga.c

@ -0,0 +1,91 @@
#include <stddef.h>
#include <stdint.h>
#include <brados/string.h>
#include <video/vga.h>
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color)
{
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
// Initialize a VGA term
void term_init(struct vgastate *state)
{
state->term_row = state->term_col = 0;
state->term_color = make_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
state->term_buf = (uint16_t *) VGA_BUF;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
state->term_buf[index] = make_vgaEntry(' ', state->term_color);
}
}
}
// Write a character to a VGA term
void term_putChar(struct vgastate *state, char c)
{
if (c == '\n')
state->term_col = VGA_WIDTH - 1;
else
term_putEntryAt(state, c, state->term_color, state->term_col, state->term_row);
if (++state->term_col == VGA_WIDTH) {
state->term_col = 0;
if (++state->term_row == VGA_HEIGHT) {
state->term_row--;
term_scroll(state);
}
}
}
// Place a character in a VGA term's buffer
void term_putEntryAt(struct vgastate *state, char c, uint8_t color, size_t x, size_t y)
{
const size_t index = y * VGA_WIDTH + x;
state->term_buf[index] = make_vgaEntry(c, color);
}
// Scroll the term down 1 line
void term_scroll(struct vgastate *state)
{
// Move each row up
for (size_t y = 1; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
const size_t indexBelow = (y - 1) * VGA_WIDTH + x;
state->term_buf[indexBelow] = state->term_buf[index];
}
}
// Clear the last line
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
state->term_buf[index] = ' ';
}
}
// Set VGA term color
void term_setColor(struct vgastate *state, uint8_t color)
{
state->term_color = color;
}
// Write a string to a VGA term
void term_writeStr(struct vgastate *state, const char *data)
{
size_t dataLen = strlen(data);
for (size_t i = 0; i < dataLen; i++)
term_putChar(state, data[i]);
}

8
include/brados/string.h

@ -0,0 +1,8 @@
#ifndef __brados_string_h__
#define __brados_string_h__
#include <stddef.h>
size_t strlen(const char *str);
#endif

64
include/video/vga.h

@ -0,0 +1,64 @@
#ifndef __brados_video_vga_h__
#define __brados_video_vga_h__
#include <stddef.h>
#include <stdint.h>
#define VGA_BUF 0xB8000
#define VGA_WIDTH 80
#define VGA_HEIGHT 24
// VGA State
struct vgastate {
size_t term_row, term_col;
uint8_t term_color;
uint16_t *term_buf;
};
// Color constants
enum vga_color
{
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15
};
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg);
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color);
// Initialize a VGA term
void term_init(struct vgastate *state);
// Write a character to a VGA term
void term_putChar(struct vgastate *state, char c);
// Place a character in a VGA term's buffer
void term_putEntryAt(struct vgastate *state, char c, uint8_t color, size_t x, size_t y);
// Scroll the term down 1 line
void term_scroll(struct vgastate *state);
// Set VGA term color
void term_setColor(struct vgastate *state, uint8_t color);
// Write a string to a VGA term
void term_writeStr(struct vgastate *state, const char *data);
#endif

160
kernel/brados.c

@ -1,156 +1,44 @@
#include <stddef.h>
#include <stdint.h>
#include <brados/string.h>
#include <video/vga.h>
// Make sure we are using the right compiler
#if defined(__linux__)
#error "You are using the wrong compiler."
#endif
// Color constants
enum vga_color
{
COLOR_BLACK = 0,
COLOR_BLUE = 1,
COLOR_GREEN = 2,
COLOR_CYAN = 3,
COLOR_RED = 4,
COLOR_MAGENTA = 5,
COLOR_BROWN = 6,
COLOR_LIGHT_GREY = 7,
COLOR_DARK_GREY = 8,
COLOR_LIGHT_BLUE = 9,
COLOR_LIGHT_GREEN = 10,
COLOR_LIGHT_CYAN = 11,
COLOR_LIGHT_RED = 12,
COLOR_LIGHT_MAGENTA = 13,
COLOR_LIGHT_BROWN = 14,
COLOR_WHITE = 15
};
// Create a VGA color
uint8_t make_color(enum vga_color fg, enum vga_color bg)
{
return fg | bg << 4;
}
// Create a VGA character
uint16_t make_vgaEntry(char c, uint8_t color)
{
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
// Calculate the length of a string
size_t strlen(const char *str)
{
size_t len = 0;
while (str[len] != 0)
len++;
return len;
}
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 24;
size_t term_row, term_col;
uint8_t term_color;
uint16_t *term_buf;
// Initialize the terminal
void term_init()
{
term_row = term_col = 0;
term_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
term_buf = (uint16_t *) 0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
term_buf[index] = make_vgaEntry(' ', term_color);
}
}
}
// Set the terminal color
void term_setColor(uint8_t color)
{
term_color = color;
}
// Place a character in the buffer
void term_putEntryAt(char c, uint8_t color, size_t x, size_t y)
// Test various VGA functionality
void vgaTests(struct vgastate *term)
{
const size_t index = y * VGA_WIDTH + x;
term_buf[index] = make_vgaEntry(c, color);
}
// Scroll down 1 line
void term_scroll()
{
for (size_t y = 1; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
const size_t indexBelow = (y - 1) * VGA_WIDTH + x;
term_buf[indexBelow] = term_buf[index];
}
}
// Test line wrapping
term_writeStr(term, "Welcome to the desert of the real. I thought what I'd do was I'd pretend I was one of those deaf mutes.\n\n");
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
term_buf[index] = ' ';
}
}
// Write a character to the terminal
void term_putChar(char c)
{
if (c == '\n')
term_col = VGA_WIDTH - 1;
else
term_putEntryAt(c, term_color, term_col, term_row);
// Test colors
term_setColor(term, make_color(VGA_COLOR_BLACK, VGA_COLOR_WHITE));
term_writeStr(term, "This text should be inverse.\n");
term_setColor(term, make_color(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_GREEN));
term_writeStr(term, "This text should be colorful.\n\n");
if (++term_col == VGA_WIDTH) {
term_col = 0;
if (++term_row == VGA_HEIGHT) {
term_row--;
term_scroll();
}
// Test scrolling
term_setColor(term, make_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
for (size_t i = 0; i < 20; i++) {
term_writeStr(term, "Printing some lines.\n");
for (volatile size_t j = 0; j < 100000000; j++);
}
}
// Write a string to the terminal
void term_writeStr(const char *data)
{
size_t dataLen = strlen(data);
for (size_t i = 0; i < dataLen; i++)
term_putChar(data[i]);
}
// Main kernel function
void brados_main()
{
term_init();
// Test printing and newlines
term_writeStr("Welcome to BRaDOS!\n");
term_writeStr("written by L. Bradley LaBoon\n\n");
struct vgastate term;
term_init(&term);
// Test line wrapping
term_writeStr("The Laughing Man always says: 'I thought what I'd do was I'd pretend I was one of those deaf mutes.'\n\n");
// Welcome message
term_writeStr(&term, "Welcome to BRaDOS!\n");
term_writeStr(&term, "written by L. Bradley LaBoon\n\n");
// Test colors
term_setColor(make_color(COLOR_BLACK, COLOR_WHITE));
term_writeStr("This text should be inverse.\n");
term_setColor(make_color(COLOR_LIGHT_BLUE, COLOR_GREEN));
term_writeStr("This text should be colorful.\n\n");
// Test scrolling
term_setColor(make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
for (size_t i = 0; i < 20; i++) {
term_writeStr("Printing some lines.\n");
for (size_t j = 0; j < 100000000; j++);
}
vgaTests(&term);
term_writeStr("Done.");
term_writeStr(&term, "Done.");
}

12
lib/string.c

@ -0,0 +1,12 @@
#include <stddef.h>
#include <brados/string.h>
// Find the length of a string
size_t strlen(const char *str)
{
size_t len = 0;
while (str[len] != 0)
len++;
return len;
}
Loading…
Cancel
Save