/**************************************
 * TESTXMEM - Test Library xmemory.h02 *
 **************************************/
 
//use -h option on command line
#include <screen.h02>
#include <stddef.h02>
#include <stdlib.h02>
#include <intlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <xmemory.h02>

char aa;                    //Function Arguments

int icount, isize;          //Load/Save Index and Size

char i;                     //Index Variable
char chrctr;                //Get/Put Character
int  intger;                //Get/Put Integer
char r[255],w[255],s[255];  //Read/Write/Swap Arrays

char ibank, ipage, ibyte;   //Index Bank, Page, and Byte
char lbank, lpage, lbyte;   //Logical Bank, Page, and Byte
char mbank, mpage, mbyte;   //Memory Bank, Page, and Byte
char pbank, ppage, pbyte;   //Physical Bank, Page, and Byte
char sbank, spage, sbyte;   //Starting Bank, Page, and Byte
char ebank, epage, ebyte;   //Starting Bank, Page, and Byte

char varend;

savbgn: //Start Address of Save

void passed() { putln("PASSED");}

void paddrs(aa) {
  if (aa) newlin(); else puts(", ");
  printf(setdst(xmbank,xmaddr),"PA=$%l, ");
  printf(setdst(xgetad()),"LA=$%l  ");
}

void perror() {
  printf(); //Implied Arguments
  paddrs(#FALSE); 
  newlin();
  goto exit;
}

/* Test xgetad() and xsetad() */
void tstgsl(ibank,ipage,ibank) {
  while (ibank<=#XLBANK) {
    putc('.');
    while() {
      while() {
        xsetad(ibank,ipage,ibyte); lbank,lpage,lbyte = xgetad();
        if (lbank<>ibank or lpage<>ipage or ibyte<>lbyte) {
          printf(setdst(ibank,ipage,ibyte),"%nERROR: XSETAD($%x,$%w); ");
          printf(setdst(lbank,lpage,lbyte),"XGETAD($%x,$%w);%n");
          goto exit;
        } 
        ibyte=ibyte+$10; if (!ibyte) break;
      } ipage++; if (!ipage) break;
    } ibank++; if (!ibank) break;
  } 
}

/* Test xgetc() and xputc() */
void tstgpc(sbank,spage,sbyte) {
  while() {
    putc('.');
    ibank=sbank; ipage=spage; ibyte=sbyte;
    xsetad(ibank,ipage,ibyte);
    i=0; do { xputc(i); i++; } while (i);
    xsetad(ibank,ipage,ibyte);
    i=0; do {
      if (xgetc()<>i)
        perror(setdst(xmbank,xmaddr),"%nERROR IN BANK $%x ADDRESS $%w");
      i++;
    } while (i);
    sbank++;spage++;sbyte++;
    if (!sbank or sbank>#XLBANK) break;
  }
}

/* Test xgeti() and xputi() */
void tstgpi(sbank,spage,sbyte) {
  while() {
    putc('.');
    ibank=sbank; ipage=spage; ibyte=sbyte;
    xsetad(ibank,ipage,ibyte); //Write 256 Integers
    i=0; do {mpage=i;mbyte=i^$FF; xputi(.,mpage,mbyte); i++;} while (i);
    xsetad(ibank,ipage,ibyte); //Read 256 Integers
    i=0; do {
      mpage=i;mbyte=i^$FF;
      intger = xgeti();
      if (<intger<>mbyte or >intger<>mpage) 
        perror(setdst(xmbank,xmaddr),"%nERROR IN BANK $%x ADDRESS $%w");
      i++;
    } while (i);
    sbank++;spage++;sbyte++; if (!sbank or sbank>#XLBANK) break;
  }
}

/* Test xgetl() and xputl() */
void tstgpl(sbank,spage,sbyte) {
  while() {
    putc('.');
    ibank=sbank; ipage=spage; ibyte=sbyte;
    xsetad(ibank,ipage,ibyte);  //Write 256 Longs
    i=0; do { 
      mbyte=i; mpage=i^$FF; mbank = -i; 
      xputi(mbank,mpage,mbyte); 
      i++;
    } while (i);
    xsetad(ibank,ipage,ibyte); //Read 256 Longs
    i=0; do {
      mbyte=i; mpage=i^$FF; mbank = -i; 
      intger = xgeti();
      if (<intger<>mbyte or >intger<>mpage) 
        perror(setdst(xmbank,xmaddr),"%nERROR IN BANK $%x ADDRESS $%w");
      i++;
    } while (i);
    sbank++;spage++;sbyte++; if (!sbank or sbank>#XLBANK) break;
  }
}

/* Test xread() and xwrite() */
void tstxrw(sbank,spage,epage) {
  i=0; do {w[i]=i;s[i]=i^$FF;i++;} while (i); //Fill Write/Swap Arrays
  ibank=sbank;
  while (ibank<=#XLBANK) {
    putc('.');
    ipage=spage; xsetad(ibank,0,ipage); //Write 256,1-255 bytes of Array 
    do {xwrite(ipage,&w); ipage++; } while (ipage<>epage);
    ipage=spage; xsetad(ibank,0,ipage);
    do {
      xread(ipage,&r);
      i=0; do {if (r[i]<>w[i]) perror(i,"ERROR IN INDEX %d");i++;} while(i<>ipage);        
      ipage++;
    } while (ipage<>epage);
    ibank++; if (!ibank) break;
  }
}       

savend: //End Address of Save

/* Test xload() and xsave() */
void tstxls(sbank,spage,sbyte) {
  savrxy(&savbgn); isize = subtxy(&savend); //Load/Save Length
  xsetad(sbank,spage,sbyte); setsrc(&savbgn); xsave(isize);
  xsetad(sbank,spage,sbyte); setdst(&varend); xload(isize);
  srcptr = &savbgn; dstptr = &varend; icount = isize;
  while (<icount or >icount) {
    if (*srcptr <> *dstptr)
      perror("ERROR IN XLOAD() AND/OR XSAVE()\n");
    if (!<icount&63) putc('.'); 
    srcptr++; dstptr++; icount--;
  }
}

main:

  if (!#XLBANK) perror("Extended Memory Not Supported");

  puts("TESTING XGETAD() AND XSETAD()");
  tstgsl($00,$00,$00); 
  passed();

  puts("TESTING XPUTC() AND XGETC()..");
  tstgpc($00,$00,$00);
  passed();

  puts("TESTING XPUTI() AND XGETI()..");
  tstgpi($00,$00,$00);
  passed();

  puts("TESTING XPUTL() AND XGETL()..");
  tstgpi($00,$00,$00);
  passed();

  puts("TESTING XREAD() AND XWRITE().");
  tstxrw($00,$00,$7F);
  passed();

  puts("TESTING XLOAD() AND XSAVE()......");
  tstxls($00,$00,$00);
  passed();

  goto exit;  
