title: C++ writes to VGA text-mode memory are not visible on the screen
body: <p>I'm trying to write a kernel (an OS kernel) in C++ and I've encountered a really strange problem (I'm sure it'll be quite obvious what I'm doing wrong for anyone else, I just for the life of me can't find what's wrong)</p>
  
  <p>I'm using a C++ class to represent a VGA console (The BIOS should load this at address 0xB8000) that can be accessed early in the OS bootsequence for debugging output.</p>
  
  <p>(for more, read: <a href="http://wiki.osdev.org/Bare_Bones" rel="nofollow noreferrer">http://wiki.osdev.org/Bare_Bones</a>)</p>
  
  <p>Here's my kernel's main function:</p>
  
  <pre><code>#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");
      }
  }
  </code></pre>
  
  <p>(Right now the output is commented out, but it works as expected, using the right characters and colours in the right places)</p>
  
  <p>Here's the VGATerminal constructor:</p>
  
  <pre><code>VGATerminal::VGATerminal ()
  {
      this-&gt;row = 0;
      this-&gt;col = 0;
      this-&gt;currentColour = generateVGAColour(WHITE, BLACK);
      this-&gt;vgaBuffer = (uint16*) VGATerminal::VGA_MEMORY;
  
      for (uint16 r = 0; r &lt; VGATerminal::HEIGHT; r++)    // Loop over rows
      {
          for (uint16 c = 0; c &lt; 8; c++)  // Loop over columns
          {
              this-&gt;vgaBuffer[(r * WIDTH) + c] = generateColouredChar(' ', this-&gt;currentColour);
              //this-&gt;print(' ');
          }
      }
  
      //this-&gt;row = 0;
      //this-&gt;col = 0;
  }
  </code></pre>
  
  <p>This code sends my OS into an infinite loop whenever the columns iterate past 7 (7 is fine, 8 sends it into an endless loop).
  (By 7 is fine, I mean it produces the expected behaviour of clearing the first 7 columns of the screen (although I can't really verify this, since before I clear the screen, it's not filled with text. The text that is on screen gets erased correctly though)</p>
  
  <p>The loop starts somewhere at the beginning of the kernel (When i generate an ISO for my OS, it actually loops back to before GRUB opens, GRUB opens, I select my OS and it goes back to the beginning).</p>
  
  <p>When I replace the <code>this-&gt;vgaBuffer[...] = ...</code> part with <code>this-&gt;print(' ')</code> everything works fine. (If I do this, I also need to reset <code>this-&gt;row</code> and <code>this-&gt;col</code>, that's why those two lines are commented out at the end)</p>
  
  <p>Here's the <code>VGATerminal::print(const char c)</code> function:</p>
  
  <pre><code>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-&gt;vgaBuffer[(this-&gt;row * WIDTH) + this-&gt;col] = generateColouredChar(' ', this-&gt;currentColour);
              this-&gt;col++;
  
              while ((this-&gt;col % 4) != 0)
              {
                  this-&gt;vgaBuffer[(this-&gt;row * WIDTH) + this-&gt;col] = generateColouredChar(' ', this-&gt;currentColour);
                  this-&gt;col++;
  
                  if (this-&gt;col == WIDTH)
                  {
                      this-&gt;row++;
                      this-&gt;col = 0;
                  }
              }
  
              break;
  
          case '\n':
              this-&gt;row++;
              this-&gt;col = 0;
              break;
  
          case '\r':
              this-&gt;col = 0;
              break;
  
          default:
              this-&gt;vgaBuffer[(this-&gt;row * WIDTH) + this-&gt;col] = generateColouredChar(c, this-&gt;currentColour);
              this-&gt;col++;
  
              if (this-&gt;col == WIDTH)
              {
                  this-&gt;row++;
                  this-&gt;col = 0;
              }
  
              break;
      }
  }
  </code></pre>
  
  <p>Support for carriage returns and newlines might not be complete, I'd like to get rid of this bug first before testing those out, altough newlines seem to be working correctly.</p>
  
  <p><code>uint16 generateColouredChar (const char c, uint8 colour)</code> is actually a  function (as opposed to a method):</p>
  
  <pre><code>uint16 VGATerminal::generateColouredChar (const char c, uint8 colour)
  {
      return (uint16) colour &lt;&lt; 8 | (uint16) c;
  }
  </code></pre>
  
  <p>uint8, uint16, ... are all aliases for their respective counterparts (uint8_t, uint16_t, ...) , created in another header as follows:</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  
  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;
  </code></pre>
  
  <p>(I did this because it doesn't reduce readability IMO but it saves me having to type that annoying '_')</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here's the complete VGATerminal class for future reference:</p>
  
  <pre><code>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);
  };
  </code></pre>