// experi3b.c

#include <conio.h>
#include <stdio.h>
#include <bios.h>

// include header with constants
#include "const3a.h"

// external prototypes
extern void set_up_ppi(int mode);
extern void get_port(void);
extern int is_closure(int closurenumber);

void main(void)
{
  int x,y;

  get_port(); // get the port number and establish register locations

  // make A an output and B an input
  set_up_ppi(AOUT_CUPPERIN_BIN_CLOWERIN);

  clrscrn();  // clear the screen

  while(1) // stay in loop forever
  { 
    // "keyboard hit" is a function that checks
    // to see if a key has been pressed.
    if(kbhit())
      break;// A key was pressed -- break out of the while(1) loop

    poscurs(0,0); // put the cursor at the top left

    for(x=1; x<4; x++) // first 3 switch numbers
      printf("%4d",x); // printed 4 characters wide with leading blanks
    puts(""); // go to next line

    for(x=1; x<4; x++)
    {
      if(is_closure(x))     // is there a closure -- if so, print ON, else print OFF
        printf("%4s","ON"); // "%4s" means print string 4 characters wide with leading blanks
      else printf("%4s","OFF");
    }
    puts(""); // go to next line

    // go through matrix, starting at 4, as long as less than 68, step 8 (4, 12, 20, etc.)
    for(x=4; x<68; x+=8)
    {
      for(y=0; y<8; y++) // go through 8 places on a matrix row -- x + y will be the closure #
        printf("%4d",x+y); // print the numbers
      puts(""); // go to next line

      for(y=0; y<8; y++)
      {
        if(is_closure(x+y)) // closure number = x + y
          printf("%4s","ON"); // is there a closure -- if so, print ON, else print OFF
        else printf("%4s","OFF");
      }
      puts(""); // go to next line
    }
    // sleep(1); // optional wait 1 second before doing it again so results can be seen
  }
} // end experi3b.c

