Printing characters - random colors... [solved]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
bgraybr
Posts: 8
Joined: Thu Oct 08, 2009 9:02 pm

Printing characters - random colors... [solved]

Post 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 1396 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");
Last edited by bgraybr on Sun Oct 25, 2009 7:48 pm, edited 1 time in total.
smeezekitty
Member
Member
Posts: 50
Joined: Sat Mar 21, 2009 9:42 pm

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

Post 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)
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

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

Post 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;
Post Reply