
#include <stdio.h> // contains prototype for printf() and puts()
#include <conio.h> // contains prototypes for inp() and kbhit()

int is_switch(int input); // prototype for is_switch

void main(void)
{
  int x,y;

  while(1) // since 1 is always 1, this while() loop will run forever
  {

    if(kbhit()) // "keyboard hit" -- a compiler function that sees if a key has been
      break;    // pressed.  We will break out of the while() loop if a key is pressed.

    for(x=0; x<5; x++) // purposley use numbers less than 1 and greater than 3
    {
      y = is_switch(x);
      printf("x = %d y = %2d | ",x,y); // don't print a new line yet
    }
    puts(""); // puts("") will put a new line on the screen

  } // end while(1)
}

#define port 0x300 // change if there is a conflict
#define switch_port port + 0x18

/* =============================================================================
   Return -1 if input is less than 1 or greater than 3.

   Return a 1 if switch number designated by input is on, else return 0,
   using the following procedure:

   1. Get the data from switch_port with inp(switch_port).

   2. Shift the results of the above to the right input + 3 bits.

   3. AND the results of the above with 1.

   4. XOR the results of the above with 1 to reverse the polarity, and
      return the resulting 1 or 0.
   ============================================================================= */
int is_switch(int input)
{
  if(input < 1 || input > 3) // if the input is out of range,
    return -1;               // return -1 showing an error

  return ((inp(switch_port) >> (input + 3)) & 1) ^ 1;
}

