Page 1 of 1

Printing characters - random colors... [solved]

Posted: Sun Oct 25, 2009 6:05 pm
by bgraybr
I wrote some code thats supposed to write characters to the screen. Like the title of the thread says, only every other character/even characters show up, and every character has a different foreground and background color. everything else works, the cursor is in the right place, the screen scrolls, but I can't get the colors right and have absolutely no idea why only half of the characters appear.

Look at the screenshot attached and you'll see what I mean...
screenie.jpg
screenie.jpg (6.6 KiB) Viewed 1397 times

Code: Select all

	unsigned char *videoram = (unsigned char *) 0xb8000;
	videoram[1] = 0x17; //fore, background color
	unsigned char bgColor = 1;
	unsigned char fgColor = 7;
	int x = 0;
	int y = 0;

	void put(unsigned char c)
	{
		unsigned short attribByte = (bgColor << 4) | (fgColor & 0x0F);
		volatile unsigned short attrib = attribByte << 8;
		volatile unsigned char *location;

		//handle any printable character
		if (c >= ' ')
		{
			location = videoram + (y*80 + x);
			*location = c | attrib;
			x++;
		}

		if (x >= 80)
		{
			x = 0;
			y++;
		}

		cursor();
	}

	void write(char *c)
	{
		int i = 0;
		while (c[i])
		{
			put(c[i++]);
		}
	}

	clear();
	write("Hello World");

Re: Printing characters - random colors and only half show up

Posted: Sun Oct 25, 2009 6:31 pm
by smeezekitty

Code: Select all

 unsigned short *videoram = (unsigned char *) 0xb8000;
   videoram[1] = 0x17; //fore, background color
   unsigned char bgColor = 1;
   unsigned char fgColor = 7;
   int x = 0;
   int y = 0;

   void put(unsigned char c)
   {
      unsigned short attribByte = (bgColor << 4) | (fgColor & 0x0F);
      volatile unsigned short attrib = attribByte << 8;
      volatile unsigned short *location;

      //handle any printable character
      if (c >= ' ')
      {
         location = videoram + (y*80 + x);
         *location = c | attrib;
         x++;
      }

      if (x >= 80)
      {
         x = 0;
         y++;
      }

      cursor();
   }

   void write(char *c)
   {
      int i = 0;
      while (c[i])
      {
         put(c[i++]);
      }
   }

   clear();
   write("Hello World");
location and video ram variables need to be of type short to apply the attributes that way
(fixed in the above code)

Re: Printing characters - random colors... [solved]

Posted: Mon Oct 26, 2009 1:01 am
by zity
Smeezekitty got the answer, but a little incorrect code. You should change the following

Code: Select all

unsigned char *videoram = (unsigned char *) 0xb8000;
to this

Code: Select all

unsigned short *videoram = (unsigned short *) 0xb8000;