/* trproc.h - Routinen fr Transputerbentzung */

#include <conio.h>                              /* inp,outp */
#include <io.h>                                 /* open,read,close */
#include <fcntl.h>                              /* O_.. */

#define inreg           0x150                   /* C011 */
#define outreg          0x151
#define instat          0x152
#define outstat         0x153
#define reset           0x160                   /* PAL */
#define analyse         0x161
#define errflag         0x160

long trdummy;

void puttr(char *string,int length) {           /* length Bytes senden */
  int i;
  for (i=0; i<length; i++) {
    while ((inp(outstat) & 1)!=1);
    outp(outreg,*string++); } }

#define puttrchar(c); puttr(&c,1);
#define puttrshort(s); puttr((char *)(&s),2);
#define puttrint(i); trdummy=(long)i; puttr((char *)(&trdummy),4);
#define puttrlong(l); puttr((char *)(&l),4);
#define puttrfloat(f); puttr((char *)(&f),4);
#define puttrdouble(d); puttr((char *)(&d),8);

void gettr(char *string,int length) {           /* length Bytes empfangen */
  int i;
  for (i=0; i<length; i++) {
    while ((inp(instat) & 1)!=1);
    *string++=(char)inp(inreg); } }

#define gettrchar(c); gettr(&c,1);
#define gettrshort(s); gettr((char *)(&s),2);
#define gettrint(i); gettr((char *)(&trdummy),4); i=(int)trdummy;
#define gettrlong(l); gettr((char *)(&l),4);
#define gettrfloat(f); gettr((char *)(&f),4);
#define gettrdouble(d); gettr((char *)(&d),8);

int loadtr(char *name) {                        /* Occam-Programm laden */
  int file,i,length;
  char occamprog[512];
  file=open(name,O_RDONLY|O_BINARY);            /* File 'name' ”ffnen */
  if (file==-1)
    return(0);                                  /* nicht gefunden, Abbruch */
  outp(analyse,0);                              /* reset Transputer */
  outp(reset,0);
  outp(reset,1);
  for (i=0; i<500; i++);                        /* ungef„r 1ms warten */
  outp(reset,0);
  do {
    length=read(file,occamprog,512);            /* in PC holen und */
    puttr(occamprog,length);                    /* zum Transputer versenden */
    } while (length==512);
  close(file);
  return(1); }                                  /* erfolgreich geladen */

