Download PC Graphics - Bitsavers.org

Transcript
(Listing continued from page 42.)
void print byte(unsigned char byte)
{
-
int i;
for(i = 7; i >=0; i--)
byte & (1 « i) ? printf("1 ") : printf("0 ");
/* See below for description of the "?: II ternary expression. * /
printf ("\n") ;
#endif defined (test)
/* You can put things after an
II #endif II
as notes to
yourself; the compiler ignores them. */
/* And now, the function definitions: */
void outbyte(unsigned char byte)
/*
Send a byte to the serial-to-parallel chip (LSI64)
*/
{
int i; .
RAISE_CLOCK_O;
/* This is just for the benefit of the initial call. */
for(i = 7; i >= 0; i--){ /* I start with 7 because that bit goes out first */
LOWER_CLOCK_O;
/* Now present the data at pin 1 of the DB-25 */
outport(BASE, byte & (1 « i) ? DATA_O : DATA_I); /* Outputs are
inverted */
/* The "ternary expression" returns the value to the left of the I I . II
of the question is true (i.e. non-zero) and the value to the right
of the ":" if the question is false (i. e. zero).
It's very nifty
and compact, since it not only performs an "if-then-else, II but it
also returns a value. */
/* clock the data in */
unsigned char inbyte()
/* read a value from the parallel-to-serial chip (LSI65)
*/
{
int i;
unsigned char result = 0;
RAISE_CLOCK_I; /* Clock should be high before loading data into the chip */
LOAD LS165;
/* Move the data at the inputs into the flip-flops */
SHIFT LS165; /* Enable shifting out of the data */
for(i-= 7; i >= 0; i--) { /* bit 7 comes out of the LS165 first */
/* Bit 7 is present at the .output imm:ediately when the chip is loaded,
so we have to pluck it off (into pin 12 of the DB-25) before we do
any clocking. * /
result 1= «inport (BASE+1) & BIT (5»
? 1 : 0) « i;
/* form result:
The ternary expression returns 1 if bit 5 of i/o
address BASE+l is a "1" and O.if it's a zero.
This value is
shifted into the appropriate bit position by the "« i".
result 1= is the same as saying: result = result I, so the bit is
ORed into the result.
This way, we poke each serial bit into the
correct place in the parallel . result . * /
LOWER_CLOCK_I;
RAISE_CLOCK_I; /* The next bit appears on the rising edge of
the clock. * /
return result;
/* COLORS.H: definitions for CGA screen characteristics and colors */
#define SCREEN_BASE Oxb800
/* base address of color graphics card (and EGA
in color graphics mode
*/
#define SCREEN HEIGHT 25
#define SCREEN-WIDTH 80
#define SCREEN CHARS (SCREEN_WIDTH * SCREEN HEIGHT * 2)
./* number of chars and attributes in a screen */
#define BIT (I)
(unsigned char) (1
«
It· /*. A bit mask macro.
The compiler
evaluates it, so it doesn't cost anything. */
/* Make a complete attribute by ORing a CHARacter type with a
BACKground type */
(Listing continues on page 46.)
44
MICRO CORNUCOPIA, #39, Jan-Feb 1988