// experi4a.c

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

// include header with constants
#include "const4a.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,r,c;

  char *names[] = {"                |--------------------------------|",
                   "Header 3 Pin  2 | 1| 2| 3|25|26|27|28|29|30|31|32|",
                   "Header 3 Pin  4 | 4| 5| 6|33|34|35|36|37|38|39|40|",
                   "Header 3 Pin  3 | 7| 8| 9|41|42|43|44|45|46|47|48|",
                   "Header 3 Pin  6 |10|11|12|49|50|51|52|53|54|55|56|",
                   "Header 3 Pin  5 |13|14|15|57|58|59|60|61|62|63|64|",
                   "Header 3 Pin  8 |16|17|18|65|66|67|68|69|70|71|72|",
                   "Header 3 Pin  7 |19|20|21|73|74|75|76|77|78|79|80|",
                   "Header 3 Pin 10 |22|23|24|81|82|83|84|85|86|87|88|",
                   "                |--------------------------------|",
                   "   Header Number H1 H1 H1 H3 H3 H3 H3 H3 H3 H3 H3",
                   "      Header Pin  5  4  3  9 11 13 15 17 19 21 23",
                   NULL};

  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 and automatically start printing
              // at 0,0 which is the top-left of the screen
              // (might not be available in all compilers)

  for(x=0; names[x]!=NULL; x++)
    printf("%s\n",names[x]);

  // (the following two lines might not be available in all compilers)
  r = cursrow() + 1; // get the screen row position plus 1
  c = curscol();     // get the screen column position

  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

    // (the following might not be available in all compilers)
    poscurs(r,c); // put the cursor one line below where the phrases ended

    // Go through 88 switches with a row step of 11.  This will give
    // the first of each row, or 1, 12, 23, etc.  What the user
    // sees is reasonable for human viewing rather than reflecting
    // the matrix layout.  The y loop then runs through each row,
    // starting at 0 and going through 10.
    // The first row is x+y = 1, 2, 3 .... 11.
    // The second row is x+y = 12, 13, 14 ...... 22, and so on.
    for(x=1; x<89; x+=11)
    {
      for(y=0; y<11; y++) // each row is 11 columns wide
      {
        printf("%3d ",x+y); // print 3 characters wide with leading blanks
        if(is_closure(x+y)) // is there a closure? yes = ON, else OFF
          printf("%-3s","ON"); // "%-3s" means print string 4 characters
        else printf("%-3s","OFF"); //  wide with following blanks
      }
      puts(""); // go to next line

    } // end for(x=1; x<89; x+=11)

  } // end while(1)

} // end experi4a.c

