| Zeile 1: | Zeile 1: | ||
Die Programmierung eines [[Kernel|Kernels]] in [[C++]] unterscheidet sich in einigen Punkten von der mit [[C]]. Einen ausführlicheren Überblick zu den relevanten Unterschieden zu C gibt der Artikel [[C++]]. Dieses Tutorial arbeitet genau wie [[C-Kernel mit GRUB]] mit dem Bootloader [[GRUB]]. | Die Programmierung eines [[Kernel|Kernels]] in [[C++]] unterscheidet sich in einigen Punkten von der mit [[C]]. Einen ausführlicheren Überblick zu den relevanten Unterschieden zu C gibt der Artikel [[C++]]. Dieses Tutorial arbeitet genau wie [[C-Kernel mit GRUB]] mit dem Bootloader [[GRUB]]. | ||
| + | |||
== Kernel == | == Kernel == | ||
| Zeile 9: | Zeile 10: | ||
// Einbinden unseres Header | // Einbinden unseres Header | ||
#include "Video.h" | #include "Video.h" | ||
| + | #include "Multiboot.h" | ||
| + | |||
| + | void main(const Multiboot& multiboot_structur, | ||
| + | uint32_t multiboot_magic) | ||
| + | { | ||
| + | if (multiboot_magic != MULTIBOOT_MAGIC) | ||
| + | { | ||
| + | // Fehler! | ||
| + | screen << background(color::red) << color::white << "Fehler: Nicht von einem multibootfaehigem Bootloader geladen!"; | ||
| + | return; | ||
| + | } | ||
| − | |||
// Textausgabe | // Textausgabe | ||
| − | screen << background(color:: | + | screen << background(color::white) << color::blue << "Willkommen im C++-TestKernel!"; |
} | } | ||
| + | </cpp> | ||
| + | |||
| + | === types.h === | ||
| + | |||
| + | Ein paar Typendefinitionen: | ||
| + | |||
| + | <cpp> | ||
| + | typedef unsigned char uint8_t; | ||
| + | typedef unsigned short uint16_t; | ||
| + | typedef unsigned int uint32_t; | ||
</cpp> | </cpp> | ||
| Zeile 23: | Zeile 44: | ||
#ifndef VIDEO_H | #ifndef VIDEO_H | ||
#define VIDEO_H | #define VIDEO_H | ||
| + | |||
| + | #include "types.h" | ||
namespace color | namespace color | ||
| Zeile 81: | Zeile 104: | ||
private: | private: | ||
// Zeiger auf den Videospeicher | // Zeiger auf den Videospeicher | ||
| − | + | uint16_t* m_videomem; | |
// Y-Position der Textausgabe, je volle Zeile +80 | // Y-Position der Textausgabe, je volle Zeile +80 | ||
| Zeile 90: | Zeile 113: | ||
// FB/BG-Farbe | // FB/BG-Farbe | ||
| − | + | uint16_t m_color; | |
}; | }; | ||
| Zeile 109: | Zeile 132: | ||
Video::Video() | Video::Video() | ||
| − | : m_videomem(( | + | : m_videomem((uint16_t*) 0xb8000), m_off(0), m_pos(0), m_color(0x0700) |
{ | { | ||
//Bildschirm leeren | //Bildschirm leeren | ||
| Zeile 137: | Zeile 160: | ||
Video& Video::operator << (color::type color){ | Video& Video::operator << (color::type color){ | ||
| − | m_color = (static_cast< | + | m_color = (static_cast<uint16_t>(color) << 8) | (m_color & 0xF000); |
return *this; | return *this; | ||
} | } | ||
Video& Video::operator << (const background& color){ | Video& Video::operator << (const background& color){ | ||
| − | m_color = (static_cast< | + | m_color = (static_cast<uint16_t>(color.m_color) << 12) | (m_color & 0x0F00); |
return *this; | return *this; | ||
} | } | ||
| Zeile 159: | Zeile 182: | ||
// Setzen des Zeichens und der Farbe in den Videospeicher | // Setzen des Zeichens und der Farbe in den Videospeicher | ||
| − | m_videomem[m_off + m_pos] = ( | + | m_videomem[m_off + m_pos] = (uint16_t)c | m_color; |
m_pos++; | m_pos++; | ||
} | } | ||
| + | </cpp> | ||
| + | |||
| + | == Multiboot.h === | ||
| + | |||
| + | Eine Header um die von [[GRUB]] an das Betriebssystem übergebene Multibootstruktur zu verwenden (siehe auch [[Multiboot]]): | ||
| + | |||
| + | <cpp> | ||
| + | #ifndef MULTIBOOT_H | ||
| + | #define MULTIBOOT_H | ||
| + | |||
| + | #include <types.h> | ||
| + | |||
| + | #define MULTIBOOT_MAGIC 0x2BADB002 | ||
| + | |||
| + | struct Multiboot | ||
| + | { | ||
| + | uint32_t flags; | ||
| + | uint32_t mem_lower; | ||
| + | uint32_t mem_upper; | ||
| + | uint32_t bootdevce; | ||
| + | uint32_t cmdline; | ||
| + | uint32_t module_count; | ||
| + | uint32_t module_address; | ||
| + | /* etc... */ | ||
| + | }; | ||
| + | |||
| + | #endif | ||
</cpp> | </cpp> | ||