[rec.ham-radio] NOAA-METEOR receiver project part 3 of 3

magore@watdcsu.waterloo.edu (Mike Gore, Institute Computer Research - ICR) (03/03/89)

                2400HZ Crystal phase lock Weather Satellite decoder
                             Updated by Mike Gore March 1989

Part 3 of 3
===========
: This is a shar archive.	Extract with sh, not csh.
: The rest of this file will extract:
: makefile sat.h sup.h disp.c dofloyd.c dohist.c get.c sup.c
echo Extracting makefile
sed 's/^X//' > makefile << 'e-o-f'
X# Mark Williams version 3.0.9 C for the ATARI ST
X#
XCFLAGS= -O -c
XLDFLAGS=
X
XSUP= dofloyd.o dohist.o sup.o
X
Xall:	get.ttp disp.ttp 
X
Xget.ttp:    get.o setrte.o sup.o
X	cc $(LDFLAGS) get.o sup.o setrte.o -o get.ttp
X
Xdisp.ttp:	$(SUP) disp.o
X	cc $(LDFLAGS) disp.o $(SUP) -o disp.ttp -lm
X
Xdisp.o:	disp.c
X	cc $(CFLAGS) disp.c 
X
Xdofloyd.o:	dofloyd.c
X	cc $(CFLAGS) dofloyd.c
X
Xdohist.o:	dohist.c
X	cc $(CFLAGS) dohist.c
X
Xsup.o:	sup.c
X	cc $(CFLAGS) sup.c
X
Xget.o:	get.c
X	cc $(CFLAGS) get.c
X
Xsetrte.o:	setrte.s
X	cc $(CFLAGS) setrte.s
e-o-f
echo Extracting sat.h
sed 's/^X//' > sat.h << 'e-o-f'
X/*
X * Mike Gore
X * March 1989
X *		watmath!watdcsu!magore
X *		watmath!mgvax!mike
X*/
X
X#define HSIZE 1200
X#define GREY 256
X#define BITS 8
e-o-f
echo Extracting sup.h
sed 's/^X//' > sup.h << 'e-o-f'
X/*
X * Mike Gore
X * March 1989
X *		watmath!watdcsu!magore
X *		watmath!mgvax!mike
X*/
X
X#define setres(a)   ( *((char *) 0xff8260L) = (int) (a)) 
X#define getres()	( *((char *) 0xff8260L) & 0xff)
X#define extsync()   ( *((char *) 0xff820aL) = (1) )
X#define intsync()   ( *((char *) 0xff820aL) = (0) )
X#define giport(a)   ( *((char *) 0xff8800L) = (a) )
X#define giwrite(a)  ( *((char *) 0xff8802L) = (a) )
X#define giread()    ( *((unsigned char *) 0xff8800L) )
X#define strobeon()  ( giport(14), giwrite(giread() | 0x20) )
X#define strobeoff() ( giport(14), giwrite(giread() & ~0x20) )
X#define stat()      ( *((char *) 0xfffa01L) )
X#define setcol(a,b) ( *((int *) (0xff8240L+((int)(a)<<1)) ) = ((int)(b))&0x1ff )
X#define getcol(a) 	( *((int *) (0xff8240L+((int)(a)<<1)) )  & 0x1ff )
e-o-f
echo Extracting disp.c
sed 's/^X//' > disp.c << 'e-o-f'
X/*
X * Mike Gore
X * March 1989
X *		watmath!watdcsu!magore
X *		watmath!mgvax!mike
X*/
X
X#include <osbind.h>
X#include <linea.h>
X#include <math.h>
X#include <stdio.h>
X
X#include "sat.h"
X#include "sup.h"
X
X
Xint XMAX = 300;
Xint YMAX = 200;
X
X#define USAGE "line offset [-hcys1234]
X
Xlong	 histb[GREY];
Xunsigned char mapb[GREY];
X
Xunsigned char inbuf[HSIZE+1], outbuf[HSIZE+1], tmpbuf[HSIZE+1];
X
Xchar line[256];
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{
X	int fp;
X	int Lskip = 0;
X	int Loff = 0;
X	int Lswap = 0;
X	long Vpos = 0L;
X	int nres, ores;
X	int hflag = 0;
X	int cflag = 0;
X	int yflag = 0;
X	int sflag = 1;
X	int val,vmin,vmax;
X
X	int i,s,x,y;
X	char *ptr = "";
X
X
X	long lseek();
X
X
X	if(argc < 4 || argc > 5) {
X		fprintf(stderr,"Usage: %s\n",argv[0],USAGE);
X		exit(1);
X	}
X	if(argc == 5) {
X		ptr= argv[4];
X		if(*ptr != '-') {
X			fprintf(stderr,"Usage: %s\n",argv[0],USAGE);
X			exit(1);
X		}
X
X		for(i=1;ptr[i];++i) {
X			switch(ptr[i]) {
X				case '1':
X				case '2':
X				case '3':
X				case '4':
X					sflag = ptr[i] - '0';
X					break;
X				case 'h':
X					hflag = 1;
X					break;
X				case 's':
X					Lswap = 1;
X					break;
X				case 'c':
X					cflag = 1;
X					break;
X				case 'y':
X					yflag = 1;
X					break;
X				default:
X					fprintf(stderr,
X						"%s: unknown mode:%s\n",
X							argv[0],ptr);
X					exit(1);
X			}
X		}
X	
X	}
X	if((fp=open(argv[1],0)) < 0 ) {
X		fprintf(stderr,"%s:Can't open:%s\n",argv[0],argv[1]);
X		exit(1);
X	}
X
X	Lskip = atoi(argv[2]);
X	Loff = atoi(argv[3]);
X
X	Vpos = (long)HSIZE*Lskip + (long)Loff;
X
X	map_init(mapb);
X
X	ores = Getrez();
X	nres = (ores == 2) ? 2 : 0;
X	scrinit(nres);
X
X	switch(nres) {
X		case 0:
X			XMAX = 320;
X			YMAX = 200;
X			break;
X		case 1:
X			XMAX = 500;
X			YMAX = 200;
X			break;
X		case 2:
X			floyd_init();
X			XMAX = 500;
X			YMAX = 400;
X			break;
X	}
X
X	for(y=0; y<YMAX; y += sflag) {
X
X		if(lseek(fp, Vpos, 0)<0) 
X			break;
X		if(read(fp, inbuf, XMAX)<XMAX) 
X			break;
X		Vpos += HSIZE;
X
X		if(yflag) {
X			if(lseek(fp, Vpos, 0)<0) 
X				break;
X			if(read(fp, tmpbuf, XMAX)<XMAX) 
X				break;
X			Vpos += HSIZE;
X			for(i=0;i<XMAX;++i)
X				inbuf[i] = (inbuf[i]+tmpbuf[i])>>1;
X		}
X
X		if(cflag) {
X			vmin = 255;
X			vmax = 0;
X			for(i=0;i<XMAX;++i) {
X				val = inbuf[i];
X				if(val > vmax)
X					vmax += ((val-vmax)/2);
X				if(val < vmin)
X					vmin -= ((vmin-val)/2);
X			}
X			val = vmax - vmin + 1;
X			for(i=0;i<GREY;++i) {
X				if(i < vmin)
X					mapb[i] = 0;
X				else if(i > vmax)
X					mapb[i] = 255;
X				else	
X					mapb[i] = (i-vmin)*256/val;
X			}
X		}
X
X		if(hflag) {
X			hist_init(histb);
X			hist_add(histb, inbuf, XMAX/sflag);
X			hist_to_map(histb, mapb);
X		}
X
X		if(sflag == 1) {
X			map(mapb, inbuf, outbuf, XMAX);
X		}
X		else {
X			map(mapb, inbuf, tmpbuf, XMAX/sflag);
X			scale(tmpbuf,outbuf,XMAX,sflag);
X		}
X
X		if(nres) {
X			floyd(outbuf,tmpbuf,XMAX);
X			for(s=0;s<sflag;++s) {
X				if(Lswap == 1)
X					wline1(tmpbuf, YMAX-(y+s)-1, XMAX, 1);
X				else
X					wline1(tmpbuf, (y+s), XMAX, 0);
X			}
X		}
X		else {
X			for(s=0;s<sflag;++s) {
X				if(Lswap == 1)
X					wline4(outbuf, YMAX-(y+s)-1, XMAX, 1);
X				else
X					wline4(outbuf, (y+s), XMAX, 0);
X			}
X		}
X	}
X	gets(line);
X	screxit(ores);
X	close(fp);
X}
X
Xscale(iptr,optr,len,mag)
Xregister unsigned char *iptr, *optr;
Xint len,mag;
X{
X	register int i,s;
X	
X	for(i=0;i<len; i+= mag) {
X		for(s=0;s<mag;++s) 
X			*optr++ = *iptr;	 	
X		iptr++;
X	}
X}
e-o-f
echo Extracting dofloyd.c
sed 's/^X//' > dofloyd.c << 'e-o-f'
X#include "sat.h"
X
X/* 
X * The Original code for this routine was writen by Alan Paeth
X *					watmath!watcgl!awpaeth
X */
X
Xstatic int	errtab[HSIZE+1];
X
Xfloyd_init()
X{
X	int i;
X
X	for(i=0;i<HSIZE;++i) 
X		errtab[i] = 0;
X}
X
X
X/*
X * fast conversion loop
X */
X	
Xfloyd(invec,outvec, count)
Xregister unsigned char *invec, *outvec;
Xregister int count;
X{
X
X	register int 	err, err4, err16;
X	register int	*errv;
X	register int 	maxval, maxval2, diag, down;
X
X	errv = errtab;
X
X	err4 = err16 = diag = down = 0;
X	maxval = (1L<<BITS)-1;
X	maxval2 = maxval>>1;
X
X	do  {
X		err = (*invec++)+(errv[1])+err4+err4-err16;
X		if (err >= maxval2) {
X			*outvec++ = 1;
X			err -= maxval;
X		}
X		else *outvec++ = 0;
X		err4 = err>>2;
X		err16 = err>>4;
X		*errv++ = down + err4 - err16;
X		down = diag + err4 + err16;
X		diag = err16;
X	} while (--count > 0);
X	*errv++ = down;
X}
e-o-f
echo Extracting dohist.c
sed 's/^X//' > dohist.c << 'e-o-f'
X/*
X * Mike Gore
X * March 1989
X *		watmath!watdcsu!magore
X *		watmath!mgvax!mike
X*/
X
X#include "sat.h"
X
X
Xhist_init(hist_buf)
Xlong *hist_buf;
X{
X	register int x;
X
X	for(x=0;x<GREY;++x) 
X		hist_buf[x] = 0L;
X}
X
Xhist_add(hist_buf, inbuf, len)
Xregister long *hist_buf;
Xregister unsigned char *inbuf;
Xregister int len;
X{
X	register int x;
X	
X	for(x=0;x<len;++x) 
X		++hist_buf[inbuf[x]];
X}
X
Xhist_to_map(hist_buf, map)
Xregister long *hist_buf;
Xregister unsigned char *map;
X{
X	register int x;
X	register long Hsum,Hcnt;
X
X	for(Hcnt=0L,x=0;x<GREY;++x)
X		Hcnt += hist_buf[x];
X
X	for(Hsum=0L,x=0;x<GREY;++x) {
X		Hsum += hist_buf[x];
X		map[x] = (Hsum << 8) / (Hcnt+1);
X	}
X}
X
Xmap_init(map)
Xregister unsigned char *map;
X{
X	register int x;
X
X	for(x=0;x<GREY;++x) 
X		map[x] = x;
X}
X
X
Xmap(map,inbuf,outbuf,len)
Xregister unsigned char *map, *inbuf, *outbuf;
Xregister int len;
X{
X	register int x;
X
X	for(x=0;x<len;++x) 
X		outbuf[x] = map[inbuf[x]&0xff];
X}
e-o-f
echo Extracting get.c
sed 's/^X//' > get.c << 'e-o-f'
X/*
X * Mike Gore
X * March 1989
X *		watmath!watdcsu!magore
X *		watmath!mgvax!mike
X*/
X
X#include <osbind.h>
X#include <linea.h>
X#include <stdio.h>
X
X#include "sup.h"
X#include "sat.h"
X
Xint XMAX = 300;
Xint YMAX = 200;
X
X#define EXT
X
X#define VMEM (16L*(long)HSIZE)
X
Xlong save_ssp;
Xunsigned char *mptr;
Xunsigned int Vsum = 0;
Xunsigned int Vsize = 0L;
Xunsigned int Vhead = 0L;
Xunsigned int Vtail = 0L;
Xint Verror = 0;
Xint Vcnt = 0;
X
Xint pmin = 0;
Xint pmax = 0;
X
Xunsigned char scan[HSIZE+1];
Xunsigned char Vbuff[HSIZE+1];
X
X#ifdef EXT
Xvoid extstrobe()
X{
X	setrte();
X	if(Vsize < VMEM) {
X		giport(15);
X		mptr[Vhead++] = giread();
X		if(Vhead >= VMEM)
X			Vhead = 0;
X			Vsize++;
X	}
X	else {
X		Verror = 1;
X	}
X	*((char *) 0xfffa11L) &= ~0x01;
X}
X
X#else
X
Xvoid timetick()
X{
X	setrte();
X	if(++Vcnt >= 2) {
X		if(Vsize < VMEM) {
X			mptr[Vhead++] = Vsum>>1;
X			if(Vhead >= VMEM)
X				Vhead = 0;
X				Vsize++;
X		}
X		else {
X			Verror = 1;
X		}
X		Vcnt = 0;
X		Vsum = 0;
X	}
X	giport(15);
X	Vsum += giread();
X	strobeoff();
X	strobeon();
X	*((char *) 0xfffa0fL) &= ~0x20;
X}
X
X#endif
X
X
Xmain(argc,argv)
Xint argc; char *argv[];
X{
X	register unsigned char *ptr1;
X	register int i;
X	int	x,y;
X
X	int	nres, ores;
X	int 	val = 0;
X	int 	line = 0;
X	long	*vecp = (long *) 0x100L;
X	long	oldvec = 0L;
X	unsigned int oldmask = 0;
X	int 	fp;
X
X
X	if(argc < 2) {
X		printf("Usage: vmain OUTfile\n");
X		exit(1);
X	}
X
X	if((fp=creat(argv[1],1)) < 0) {
X		printf("Can't open: %s\n",argv[1]);
X		exit(1);
X	}
X	fflush(stdout);
X
X	init();
X	ores = Getrez();
X	nres = (ores == 2) ? 2 : 0;
X	scrinit(nres);
X
X	switch(nres) {
X		case 0:
X			XMAX = 320;
X			YMAX = 200;
X			break;
X		case 1:
X			XMAX = 640;
X			YMAX = 200;
X			break;
X		case 2:
X			XMAX = 640;
X			YMAX = 400;
X			break;
X	}
X
X	save_ssp = Super(0L);
X#ifdef EXT
X	Jdisint(0);
X	oldvec = vecp[0];
X	vecp[0] = (long) extstrobe;
X	oldmask = *((char *) 0xfffa03L);
X	*((char *) 0xfffa03L) |= 0x01;
X	Jenabint(0);
X#else
X	Xbtimer(0,3,32,timetick);
X#endif
X	Super(save_ssp);
X	
X	while(Verror == 0) {
X		if(Cconis() != 0) {
X			Verror = 1;
X			printf("\n\n");
X			break;
X		}
X		if(Vsize < HSIZE )
X			continue;
X		ptr1 = (unsigned char *) &mptr[Vtail];
X		for(i=0;i<HSIZE; i++ ) {
X			val = *ptr1++;
X			Vbuff[i] = val;
X			switch(nres) {
X				case 0:
X					if((i&3)==0) {
X						x = i >> 2;
X						y = 128 - (val>>1);
X						clr4(x,scan[x],15);
X						set4(x,y,15);
X						scan[x] = y;
X					}
X					break;
X				case 1:
X					if((i&1)==0) {
X						x = i >> 1;
X						y = 128 - (val>>1);
X						clr2(x,scan[x],3);
X						set2(x,y,3);
X						scan[x] = y;
X					}
X					break;
X				case 2:
X					if((i&1)==0) {
X						x = i >> 1;
X						y = 256 - val;
X						clr1(x,scan[x],1);
X						set1(x,y,1);
X						scan[x] = y;
X					}
X					break;
X			}
X		}
X		Vsize -= HSIZE;
X		Vtail += HSIZE;
X		if(Vtail >= VMEM)	
X			Vtail = 0;
X		if(write(fp,Vbuff,(int)HSIZE) != (int)HSIZE ) {
X			printf("Write error: %s\n", argv[1]);
X			Verror = 1;
X			break;
X		}
X		++line;
X		printf("line:%5d, Vsize:%5d\r",line, Vsize);
X		fflush(stdout);
X	}
X
X	save_ssp = Super(0L);
X#ifdef EXT
X	Jdisint(0);
X	vecp[0] = oldvec;
X	oldmask = *((char *) 0xfffa03L) = oldmask;
X#else
X	Xbtimer(0,0,0,0L);
X#endif
X	Super(save_ssp);
X
X	close(fp);
X	screxit(ores);
X	portout();
X}
X
X
Xinit()
X{
X	register int i;
X	
X	Vsize = 0;
X	Vhead = 0;
X	Vtail = 0;
X	Verror = 0;
X 
X	portin();
X	mptr = (unsigned char *) Malloc((long)(VMEM)+1L);
X	if(mptr == 0L) {
X		printf("Malloc Failed\n");
X		exit(1);
X	}
X	for(i=0;i<VMEM;++i)
X		mptr[i] = 0;
X}
e-o-f
echo Extracting sup.c
sed 's/^X//' > sup.c << 'e-o-f'
X#include <osbind.h>
X#include <linea.h>
X#include <stdio.h>
X#include "sup.h"
X
Xunsigned int disp[] = {
X      0x000,0x001,0x011,0x111,
X      0x112,0x222,0x223,0x333,
X      0x334,0x444,0x445,0x555,
X      0x556,0x666,0x667,0x777
X};
X
Xunsigned int Smap[] = {
X      0x000,0x001,0x011,0x111,
X      0x112,0x222,0x223,0x333,
X      0x334,0x444,0x445,0x555,
X      0x556,0x666,0x667,0x777
X};
X
Xlong Vbase;
Xlong save_ssp;
X
Xvclr(ptr,size)
Xregister unsigned char *ptr;
Xregister long size;
X{
X	while(size--)
X		*ptr++ = 0;
X}
X
Xvcopy(dest, src, size)
Xregister unsigned char *dest, *src;
Xregister long size;
X{
X	while(size--)
X		*dest++ = *src++;
X}
X
Xscrinit(mode)
Xint mode;
X{
X	Cursconf(0,30);
X	Getpallete(Smap);
X	Setscreen(-1L, -1L, mode);
X	Setpallete(disp);
X	linea0();
X	Vbase = Physbase();
X	vclr(Vbase, 0x8000L);
X}
X
Xscrexit(mode)
Xint mode;
X{
X	Setscreen(-1L, -1L, mode);
X	Setpallete(Smap);
X	Cursconf(1,30);
X}
X
Xportin()
X{
X	save_ssp = Super(0L);
X	strobeon();
X	giport(7);
X	giwrite( (giread() & 0x7f) );
X	*((char *) 0xfffa0fL) &= ~0x20;
X	Super(save_ssp);
X
X}
X
Xportout()
X{
X	save_ssp = Super(0L);
X	giport(7);
X	giwrite( (giread() | 0x80) );
X	strobeon();
X	Super(save_ssp);	
X}
X
XGetpallete(pal)
Xunsigned int *pal;
X{
X	int i;
X
X	save_ssp = Super(0L);
X	for(i=0;i<16;++i)
X		*pal++ = getcol(i);
X	Super(save_ssp);
X}
X
Xset1(x,y,color)
Xregister unsigned int x,y,color;
X{
X	register unsigned int mask = ((unsigned) 0x8000) >> (x & 0xf);
X	register unsigned int *ptr = Vbase + (80L * y) + ((x & 0xfff0)>>3);
X	*ptr |= ((color & 1) ? mask : 0);
X}
X
Xclr1(x,y,color)
Xregister unsigned int x,y,color;
X{
X	register unsigned int mask = ((unsigned) 0x8000) >> (x & 0xf);
X	register unsigned int *ptr = Vbase + (80L * y) + ((x & 0xfff0)>>3);
X	*ptr &= ~((color & 1) ? mask : 0);
X}
X
Xset4(x,y,color)
Xregister unsigned int x,y,color;
X{
X	register unsigned int mask = ((unsigned) 0x8000) >> (x & 0xf);
X	register unsigned int *ptr = Vbase + (160L * y) + ((x & 0xfff0)>>1);
X	*ptr++ |= ((color & 1) ? mask : 0);
X	*ptr++ |= ((color & 2) ? mask : 0);
X	*ptr++ |= ((color & 4) ? mask : 0);
X	*ptr   |= ((color & 8) ? mask : 0);
X}
X
Xclr4(x,y,color)
Xregister unsigned int x,y,color;
X{
X	register unsigned int mask = ((unsigned) 0x8000) >> (x & 0xf);
X	register unsigned int *ptr = Vbase + (160L * y) + ((x & 0xfff0)>>1);
X	*ptr++ &= ~((color & 1) ? mask : 0);
X	*ptr++ &= ~((color & 2) ? mask : 0);
X	*ptr++ &= ~((color & 4) ? mask : 0);
X	*ptr   &= ~((color & 8) ? mask : 0);
X}
X
Xwline1(buf, y, len, swap)
Xregister unsigned char *buf; 
Xint y, len, swap;
X{
X	register unsigned int tmp;
X	register unsigned int mask;
X	register unsigned int *ptr = Vbase + (80L * y);
X
X	if(!swap) {
X		while(len>0) {
X			for(tmp=0,mask=0x8000;mask>0 && len>0;mask >>= 1)
X				tmp |= (*buf++ ? mask : 0);
X			*ptr++ = tmp;
X			len -= 16;
X		}
X	}
X	else {
X		buf += (len-1);
X		while(len>0) {
X			for(tmp=0,mask=0x8000;mask>0 && len>0;mask >>= 1)
X				tmp |= (*buf-- ? mask : 0);
X			*ptr++ = tmp;
X			len -= 16;
X		}
X	}
X}
X
Xwline4(buf, y, len, swap)
Xregister unsigned char *buf; 
Xint y, len, swap;
X{
X	register unsigned int mask;
X	register unsigned int *ptr = Vbase + (160L * y);
X	register unsigned int val;
X
X	if(!swap) {
X		while(len>0) {
X			for(mask=0x8000;mask>0 && len>0;mask >>= 1) {
X				val = *buf++ >> 4;
X				ptr[0] |= ((val & 1) ? mask : 0);
X				ptr[1] |= ((val & 2) ? mask : 0);
X				ptr[2] |= ((val & 4) ? mask : 0);
X				ptr[3] |= ((val & 8) ? mask : 0);
X			}
X			ptr += 4;
X			len -= 16;
X		}
X	}
X	else {
X		buf += (len-1);
X		while(len>0) {
X			for(mask=0x8000;mask>0 && len>0;mask >>= 1) {
X				val = *buf-- >> 4;
X				ptr[0] |= ((val & 1) ? mask : 0);
X				ptr[1] |= ((val & 2) ? mask : 0);
X				ptr[2] |= ((val & 4) ? mask : 0);
X				ptr[3] |= ((val & 8) ? mask : 0);
X			}
X			ptr += 4;
X			len -= 16;
X		}
X	}
X}
X
X
X
X/* set2 & clr2 are untested */
Xset2(x,y,color)
Xregister unsigned int x,y,color;
X{
X	register unsigned int mask = ((unsigned) 0x8000) >> (x & 0xf);
X	register unsigned int *ptr = Vbase + (160L * y) + ((x & 0xfff0)>>2);
X	*ptr++ |= ((color & 1) ? mask : 0);
X	*ptr  |= ((color & 2) ? mask : 0);
X}
X
Xclr2(x,y,color)
Xregister unsigned int x,y,color;
X{
X	register unsigned int mask = ((unsigned) 0x8000) >> (x & 0xf);
X	register unsigned int *ptr = Vbase + (160L * y) + ((x & 0xfff0)>>2);
X	*ptr++ &= ~((color & 1) ? mask : 0);
X	*ptr   &= ~((color & 2) ? mask : 0);
X}
X
e-o-f
exit 0