C ++写入VGA文本模式内存在屏幕上不可见

时间:2017-09-21 16:36:04

标签: c++ console operating-system kernel vga

我试图用C ++编写一个内核(一个操作系统内核),但我遇到了一个非常奇怪的问题(我确信它会非常明显地说明我是什么&#39我为别人做错了,我只是因为我的生活无法找到错误的事情。

我使用C ++类代表VGA控制台(BIOS应该在地址0xB8000加载),可以在操作系统启动顺序的早期访问,以便调试输出。

(更多内容,请阅读:http://wiki.osdev.org/Bare_Bones

这是我内核的主要功能:

#include "Kernel.hpp"

extern "C"
{
    void kernelMain ()
    {
        VGATerminal kernelTerm = VGATerminal ();

        //kernelTerm.print("[");
        //kernelTerm.setTextColour(VGATerminal::LIGHT_GREEN);
        //kernelTerm.print("OK");
        //kernelTerm.setTextColour(VGATerminal::WHITE);
        //kernelTerm.print("]\t\tKernel gained control of the CPU.\n");
        //kernelTerm.print("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123465789\n");
    }
}

(现在输出已被注释掉,但它按预期工作,在正确的位置使用正确的字符和颜色)

这里是VGATerminal构造函数:

VGATerminal::VGATerminal ()
{
    this->row = 0;
    this->col = 0;
    this->currentColour = generateVGAColour(WHITE, BLACK);
    this->vgaBuffer = (uint16*) VGATerminal::VGA_MEMORY;

    for (uint16 r = 0; r < VGATerminal::HEIGHT; r++)    // Loop over rows
    {
        for (uint16 c = 0; c < 8; c++)  // Loop over columns
        {
            this->vgaBuffer[(r * WIDTH) + c] = generateColouredChar(' ', this->currentColour);
            //this->print(' ');
        }
    }

    //this->row = 0;
    //this->col = 0;
}

只要列迭代超过7(7很好,8将其发送到无限循环),此代码就会将我的操作系统发送到无限循环。 (7很好,我的意思是它产生清除屏幕前7列的预期行为(虽然我无法真正验证这一点,因为在我清除屏幕之前,它没有填充文本。虽然屏幕上的文字被正确删除了

循环在内核开头的某处开始(当我为我的操作系统生成ISO时,它实际上循环回到GRUB打开之前,GRUB打开,我选择我的操作系统,它会回到开头)。

当我用this->vgaBuffer[...] = ...替换this->print(' ')部分时,一切正常。 (如果我这样做,我还需要重置this->rowthis->col,这就是为什么这两行在最后被注释掉的原因)

这里是VGATerminal::print(const char c)功能:

void VGATerminal::print (const char c)
{
    switch (c)
    {
        case '\t':
            // First insert 1 space, this will force the terminal to insert atleast this
            // (Otherwise, you might get something like this:)
            // CATS\tDOGS Becomes CATSDOGS instead of CATS    DOGS
            // This happens because after writing CATS, the terminal is at a multiple of 4
            // and decides it doesn't need to insert anything anymore
            this->vgaBuffer[(this->row * WIDTH) + this->col] = generateColouredChar(' ', this->currentColour);
            this->col++;

            while ((this->col % 4) != 0)
            {
                this->vgaBuffer[(this->row * WIDTH) + this->col] = generateColouredChar(' ', this->currentColour);
                this->col++;

                if (this->col == WIDTH)
                {
                    this->row++;
                    this->col = 0;
                }
            }

            break;

        case '\n':
            this->row++;
            this->col = 0;
            break;

        case '\r':
            this->col = 0;
            break;

        default:
            this->vgaBuffer[(this->row * WIDTH) + this->col] = generateColouredChar(c, this->currentColour);
            this->col++;

            if (this->col == WIDTH)
            {
                this->row++;
                this->col = 0;
            }

            break;
    }
}

对回车和换行的支持可能不完整,我想在测试这些错误之前先解决这个错误,尽管新行似乎正常工作。

uint16 generateColouredChar (const char c, uint8 colour)实际上是一个函数(与方法相对):

uint16 VGATerminal::generateColouredChar (const char c, uint8 colour)
{
    return (uint16) colour << 8 | (uint16) c;
}

uint8,uint16,...是它们各自对应物(uint8_t,uint16_t,...)的所有别名,在另一个标题中创建如下:

#include <stdint.h>

using uint8 = uint8_t;
using uint16 = uint16_t;
using uint32 = uint32_t;
using uint64 = uint64_t;

using uchar = uint16;

using int8 = int8_t;
using int16 = int16_t;
using int32 = int32_t;
using int64 = int64_t;

(我这样做是因为它没有降低IMO的可读性,但它节省了我不得不输入那令人讨厌的事情&#39; _&#39;)

修改

这里是完整的VGATerminal课程以供将来参考:

class VGATerminal
{
    private:
        constexpr static uint16 WIDTH = 80;
        constexpr static uint16 HEIGHT = 25;
        constexpr static int VGA_MEMORY = 0xB8000;

        uint16  row;
        uint16  col;
        uint8   currentColour;
        volatile uint16*    vgaBuffer;

    public:
        /// Colour constants
        constexpr static uint8 BLACK = 0;
        constexpr static uint8 BLUE = 1;
        constexpr static uint8 GREEN = 2;
        constexpr static uint8 CYAN = 3;
        constexpr static uint8 RED = 4;
        constexpr static uint8 MAGENTA = 5;
        constexpr static uint8 BROWN = 6;
        constexpr static uint8 LIGHT_GREY = 7;
        constexpr static uint8 DARK_GREY = 8;
        constexpr static uint8 LIGHT_BLUE = 9;
        constexpr static uint8 LIGHT_GREEN = 10;
        constexpr static uint8 LIGHT_CYAN = 11;
        constexpr static uint8 LIGHT_RED = 12;
        constexpr static uint8 LIGHT_MAGENTA = 13;
        constexpr static uint8 LIGHT_BROWN = 14;
        constexpr static uint8 WHITE = 15;

        VGATerminal ();

        void clear ();

        void setTextColour (uint8 colour);

        void setBackgroundColour (uint8 colour);

        void print (const char c);

        void print (const char* str);
};

1 个答案:

答案 0 :(得分:2)

没有明显的理由(原帖不是)为什么7/8应该有所不同。

您如何定义VGATerminal::vgaBuffer?您是否将其标记为volatile以避免编译器优化&#34;无用&#34;记忆写?

编译器并不了解0xB8000地址的写入值在C ++语言定义之外有外部影响,使用volatile给出编译器提示,它不是常规的< del> memory C ++变量写(编译器可以自由地实现C ++变量,即使没有实际的内存写入,只要代码可以像C ++语言定义那样工作),但是应该完成实际的内存读/写操作,因为它可能会导致外部影响(在写入的情况下),或者值可能已在外部修改(如不同的线程代码,或OS,或具有内存访问权限的外部设备) )。

作为汇编程序员,BTW让我觉得看到这样一个适当的嵌套for循环只是为了清除屏幕:

VGATerminal::VGATerminal ()
{
    this->row = 0;
    this->col = 0;
    this->currentColour = generateVGAColour(WHITE, BLACK);
    this->vgaBuffer = (uint16*) VGATerminal::VGA_MEMORY;

    const uint16 whiteSpace = generateColouredChar(' ', this->currentColour);
    for (unsigned i = 0; i < VGATerminal::HEIGHT*VGATerminal::WIDTH; ++i)
        this->vgaBuffer[i] = whiteSpace;
}

虽然我认为优化器可能已经将嵌套循环修改为单循环一旦你对列使用VGATerminal::WIDTH,但是asm程序员只是编写最小代码并避免乘法和多个计数器的习惯在使用连续内存块的情况下,行/列的逻辑分离对于当前任务并不重要。