Emulating Linus Torvalds: Tsim koj tus kheej operating system los ntawm kos (II)

Zoo siab txais tos rau lwm tus ncej ntawm yuav ua li cas los tsim peb tus kheej kev ua haujlwm, hauv qhov no NextDivel.

Yog tias peb rov qab mus rau tus lej ntawm tus thawj ncej Qhov kawg ntawm txhua yam peb yuav tsum tau los nrog qee yam zoo li no:

Tom ntejDivel-1

Yog tias qhov no raug lawm peb tuaj yeem txuas ntxiv. Kuv yuav siv qhov system thiab qauv uas kuv muaj ntawm GitHub (http://github.com/AdrianArroyoCalle/next-divel) raws li nws yooj yim dua rau kuv thiab koj. Raws li koj tuaj yeem pom cov ntawv sau yog cov lus qhia yooj yim, nws tsis yog qhov txaus nyiam. Nws yuav zoo li qee yam tawm ntawm qhov tsis zoo. Tab sis raws li cov lus hais mus, kom saj cov xim, thiab hauv peb cov kev ua haujlwm yuav muaj xim. Thawj cov xim uas peb yuav tau tso yog mus yuav cov uas txhais tau daim npav VGA thiab lawv muaj 0:

  1. Dub
  2. Xiav
  3. ntsuab
  4. Cyan
  5. Liab
  6. magenta
  7. Brown
  8. Lub teeb txho
  9. Tsaus grey
  10. Lub teeb xiav
  11. Lub teeb ntsuab
  12. Cyan huv
  13. Lub teeb liab
  14. Lub teeb magenta
  15. Lub teeb xim av
  16. Dawb

Peb tab tom yuav txhais cov xim no nyob hauv ib lub header kom nws ntau qhov chaw thiab tej zaum yav tom ntej los ua ib feem ntawm qhov system API. Yog li peb tsim cov ntawv ND_Colors.hpp hauv NextDivel suav nrog.

#ifndef ND_COLOR_HPP
#define ND_COLOR_HPP
typedef enum ND_Color{
ND_COLOR_BLACK = 0,
ND_COLOR_BLUE = 1,
ND_COLOR_GREEN = 2,
ND_COLOR_CYAN = 3,
ND_COLOR_RED = 4,
ND_COLOR_MAGENTA = 5,
ND_COLOR_BROWN = 6,
ND_COLOR_LIGHT_GREY = 7,
ND_COLOR_DARK_GREY = 8,
ND_COLOR_LIGHT_BLUE = 9,
ND_COLOR_LIGHT_GREEN = 10,
ND_COLOR_LIGHT_CYAN = 11,
ND_COLOR_LIGHT_RED = 12,
ND_COLOR_LIGHT_MAGENTA = 13,
ND_COLOR_LIGHT_BROWN = 14,
ND_COLOR_WHITE = 15
} ND_Color;
#endif

Tib lub sijhawm peb yuav los txhais cov haujlwm tshiab los sau rau ntawm lub vijtsam kom zoo dua (tsis yog, peb tseem tsis tau siv printf tsis tau, Kuv paub tias koj xav tau). Peb yuav tsim cov ntawv thiab cov ntawv rau nws teeb rau lub vijtsam ntsig txog kev tshuaj ntsuam (ND_Screen.cpp thiab ND_Screen.hpp). Hauv lawv peb yuav tsim cov haujlwm rau: hloov xim ntawm cov tsiaj ntawv thiab tom qab, sau kab lus thiab cov tsiaj ntawv, ntxuav lub vijtsam thiab txav mus ncig lub vijtsam. Peb txuas ntxiv siv VGA cov ntxaij vab tshaus tab sis tam sim no peb yuav siv ob peb bytes uas yuav muab cov xim. ND_Screen.cpp yuav zoo li:

/**
* @file ND_Screen.cpp
* @author Adrián Arroyo Calle
* @brief Implements four easy functions for write strings directly
*/
#include <ND_Types.hpp>
#include <ND_Color.hpp>
#include <ND_Screen.hpp>
uint16_t *vidmem= (uint16_t *)0xB8000;
ND_Color backColour = ND_COLOR_BLACK;
ND_Color foreColour = ND_COLOR_WHITE;
uint8_t cursor_x = 0;
uint8_t cursor_y = 0;
/**
* @brief Gets the current color
* @param side The side to get the color
* */
ND_Color ND::Screen::GetColor(ND_SIDE side)
{
if(side==ND_SIDE_BACKGROUND){
return backColour;
}else{
return foreColour;
}
}
/**
* @brief Sets the color to a screen side
* @param side The side to set colour
* @param colour The new colour
* @see GetColor
* */
void ND::Screen::SetColor(ND_SIDE side, ND_Color colour)
{
if(side==ND_SIDE_BACKGROUND)
{
backColour=colour;
}else{
foreColour=colour;
}
}
/**
* @brief Puts the char on screen
* @param c The character to write
* */
void ND::Screen::PutChar(char c)
{
uint8_t attributeByte = (backColour << 4) | (foreColour & 0x0F);
uint16_t attribute = attributeByte << 8; uint16_t *location; if (c == 0x08 && cursor_x) { cursor_x--; }else if(c == '\r') { cursor_x=0; }else if(c == '\n') { cursor_x=0; cursor_y=1; } if(c >= ' ') /* Printable character */
{
location = vidmem + (cursor_y*80 + cursor_x);
*location = c | attribute;
cursor_x++;
}
if(cursor_x >= 80) /* New line, please*/
{
cursor_x = 0;
cursor_y++;
}
/* Scroll if needed*/
uint8_t attributeByte2 = (0 /*black*/ << 4) | (15 /*white*/ & 0x0F);
uint16_t blank = 0x20 /* space */ | (attributeByte2 << 8); if(cursor_y >= 25)
{
int i;
for (i = 0*80; i < 24*80; i++)
{
vidmem[i] = vidmem[i+80];
}
// The last line should now be blank. Do this by writing
// 80 spaces to it.
for (i = 24*80; i < 25*80; i++)
{
vidmem[i] = blank;
}
// The cursor should now be on the last line.
cursor_y = 24;
}
}
/**
* @brief Puts a complete string to screen
* @param str The string to write
* */
void ND::Screen::PutString(const char* str)
{
int i=0;
while(str[i])
{
ND::Screen::PutChar(str[i++]);
}
}
/**
* @brief Cleans the screen with a color
* @param colour The colour to fill the screen
* */
void ND::Screen::Clear(ND_Color colour)
{
uint8_t attributeByte = (colour /*background*/ << 4) | (15 /*white - foreground*/ & 0x0F);
uint16_t blank = 0x20 /* space */ | (attributeByte << 8);
int i;
for (i = 0; i < 80*25; i++)
{
vidmem[i] = blank;
}
cursor_x = 0;
cursor_y = 0;
}
/**
* @brief Sets the cursor via software
* @param x The position of X
* @param y The position of y
* */
void ND::Screen::SetCursor(uint8_t x, uint8_t y)
{
cursor_x=x;
cursor_y=y;
}

Cov kab ntawv yuav yog qhov yooj yim heev yog li kuv tsis suav nws nyob ntawm no, tab sis hais txog lub ntsiab txhais ntawm ND_SIDE hom

typedef enum ND_SIDE{
ND_SIDE_BACKGROUND,
ND_SIDE_FOREGROUND
} ND_SIDE;

Kuj tseem hais txog tias peb siv ND_Types.hpp header, qhov header no txhais qee hom pib rau uint8_t, uint16_t, thiab lwm yam raws li char thiab int. Qhov tseeb cov ntawv header no yog ib qho hauv C99 tus qauv thiab qhov tseeb kuv ND_Types.hpp yog daim ntawv luam / muab cov ntaub ntawv los ntawm Linux, yog li koj tuaj yeem hloov lawv thiab tsis muaj dab tsi yuav tshwm sim (tsuas muaj cov lus txhais, tsis muaj haujlwm).

Yuav kom kuaj yog tias txoj haujlwm no ua haujlwm peb yuav tau hloov pauv C nkag kis nkag ntawm lub sijhawm:

ND::Screen::Clear(ND_COLOR_WHITE);
ND::Screen::SetColor(ND_SIDE_BACKGROUND,ND_COLOR_WHITE);
ND::Screen::SetColor(ND_SIDE_FOREGROUND,ND_COLOR_GREEN);
ND::Screen::PutString("NextDivel\n");
ND::Screen::SetColor(ND_SIDE_FOREGROUND,ND_COLOR_BLACK);
ND::Screen::PutString("Licensed under GNU GPL v2");

Thiab yog tias peb ua raws li cov kauj ruam no peb yuav tau txais qhov tshwm sim no

Tom ntejDivel-3

Ua tsaug rau cov haujlwm no peb tau tsim, peb tuaj yeem pib ua GUI me me, xws li lub kernel ceeb uas peb yuav qhia txhua zaus muaj qhov ua tsis tiav. Tej yam zoo li no:

Tom ntejDivel-4

Thiab GUI me me no peb tau ua nws tsuas yog nrog cov haujlwm no:

void ND::Panic::Show(const char* error)
{
ND::Screen::Clear(ND_COLOR_RED);
ND::Screen::SetColor(ND_SIDE_BACKGROUND, ND_COLOR_WHITE);
ND::Screen::SetColor(ND_SIDE_FOREGROUND, ND_COLOR_RED);
ND::Screen::SetCursor(29,10); //(80-22)/2
ND::Screen::PutString("NextDivel Kernel Error\n");
ND::Screen::SetCursor(15,12);
ND::Screen::PutString(error);
}

Thiab mus rau ntawm no tus ncej. Kuv nco koj cov lus qhia los mus suav qhov system los ntawm 0:

git clone http://github.com/AdrianArroyoCalle/next-divel
cd next-divel
mkdir build && cd build
cmake ..
make
make DESTDIR=next install
chmod +x iso.sh
./iso.sh
qemu-system-i386 nextdivel.iso

Thiab kuv siv lub sijhawm no los ua tsaug rau txoj kev txais tos zoo uas thawj tus tau los.


Cov ntsiab lus ntawm tsab xov xwm ua raws li peb cov ntsiab cai ntawm kev tswj hwm kev ncaj nceesCov. Tshaj tawm ib qho yuam kev nyem no.

5 lus, tawm koj li

Tso koj saib

Koj email chaw nyob yuav tsis tsum luam tawm. Yuav tsum tau teb cov cim nrog *

*

*

  1. Lub luag haujlwm rau cov ntaub ntawv: Miguel Ángel Gatón
  2. Lub hom phiaj ntawm cov ntaub ntawv: Tswj SPAM, kev tswj xyuas tawm tswv yim.
  3. Sau raws cai: Koj kev tso cai
  4. Kev sib txuas lus ntawm cov ntaub ntawv: Cov ntaub ntawv yuav tsis raug xa mus rau lwm tus neeg thib peb tsuas yog los ntawm kev txiav txim siab raug cai.
  5. Cov ntaub ntawv khaws cia: Cov Ntaub Ntawv khaws tseg los ntawm Occentus Networks (EU)
  6. Txoj Cai: Txhua lub sijhawm koj tuaj yeem txwv, rov qab thiab tshem tawm koj cov ntaub ntawv.

  1.   F3niX said

    Cov phooj ywg zoo, txawm yog li kuv tua kuv lub kaus mom hlau taub qhov chaws hauv c ++.

    Txais tos.

  2.   pandacris ua said

    cov khoom no zoo kawg li. Lawv tau piqued kuv xav paub txog qhov qis-qib kev coj ua ntawm cov processors ib zaug ntxiv.
    tej zaum yog tias kuv muaj sijhawm kuv yuav pib ua si nrog tom ntej-divel.
    Kuv tsis tau xa ib tsab xov xwm rau lub sijhawm ntev. twb xav tau

  3.   Jon faus said

    Tag nrho cov cai, uas yog txoj kev.

  4.   Miguel said

    Kuv twb xav paub ntev npaum li cas los tsim kom tau ib qho operating system.

    Tos koj cov ntawv tom ntej. Cheers

  5.   Giuliano said

    Phooj ywg zoo!
    Kuv tsuas muaj teeb meem, lwm tus neeg tuaj yeem xa kuv cov ntaub ntawv hauv C ntawm qhov piv txwv no?
    Nws ib txwm xa kuv cov teeb meem hauv qhov xaus