[comp.lang.c] How do I point to the frame buffer?

psc@lznv.ATT.COM (Paul S. R. Chisholm) (07/08/88)

< "Would you buy a used operating system from these guys?" >

In article <1584@dataio.Data-IO.COM>, bright@Data-IO.COM (Walter Bright) writes:
> In article <1391@lznv.ATT.COM> psc@lznv.ATT.COM (Paul S. R. Chisholm) writes:
> >The frame buffer starts at B800:0000, which would be 0xB8000 if the
> >8088 had a linear address space.  It doesn't.  The right way to do this
> >in MS or Turbo C or Zortech C is
> >	#include <dos.h>
> >	far char * const Screen = MK_FP( 0xB800, 0 );
> >(The "const" is optional; it tells the compiler that Screen is a
> >constant, and that no one should be allowed to muck with it.)

> Replace "const" with "volatile". All hardware device locations (except
> ROM ones) should be "volatile", since they can be modified by an interrupt
> routine (like a TSR). One example is the ^C that appears on the screen
> when you type a control C.

I don't think that any compiler would generate different code with the
"volatile" keyword, but that's not a bad idea.

> Also, you've declared a <const pointer to><far char>. Try instead:
> 	volatile unsigned short far *Screen = ...
> which is <far pointer to><volatile unsigned short>. The unsigned short
> instead of char is because the display memory is actually organized as
> words instead of bytes. The high byte is the attribute, the low byte
> the character.

Using an short pointer is a good idea; even on an 8/16 bit 8088, it's
faster to move one word once than one byte twice.

But I *meant* to have a constant pointer!  I didn't want anyone mucking
around with Screen.

	volatile unsigned short far * const Screen;

> Note also that "const" and "volatile" are left-associative. "far" and
> "near" are right-associative. Confusing? You bet!

"TYPE const * foo" means that *foo (the thing foo points to) is constant.
"TYPE * const foo" means that foo (the pointer itself) is constant.

Do we have you all sufficiently confused now?  Good, try this trivia
question (answer after the .sig and ^L):  When might you use this?

	volatile TYPE const * const foo;

-Paul S. R. Chisholm, {ihnp4,cbosgd,allegra,rutgers}!mtune!lznv!psc
AT&T Mail !psrchisholm, Internet psc@lznv.att.com
I'm not speaking for my employer, I'm just speaking my mind.

volatile time_t const far * const Timer = TIMER_ADDR;

Our code isn't allowed to change either Timer or the data it points to;
but the data there may change all by itself.  A real time clock!