/****************************************************************
 *	Module name  :	monitor.c				*
 *	Purpose      :	Dumps memory with page switching	*
 *	Created      :	18-08-89				* 
 *	Last edit    : 	23-08-89		Version 1.0	*
 * 	Copyright    :	G R Buckeridge				*
 ****************************************************************/

#include stdio.h

#define OUT_A_to	0xD3
#define LD_BC_with	0x01
#define LDIR		0xED,0xB0
#define RET		0xC9

#define HI		/256
#define LO		%256

main(argc,argv)
int argc;
char *argv[];

	{
	FAST char argv_buffer[MAXARGS * 2 +132];
	int page,block,i,j,st;
	unsigned addr,bufaddr;

	static char buffer[257];
	static char buf16[17];

	static char get256[] = {
	
		DI,
		
		LD_A_with, 4,
		OUT_A_to, 0xf0,
		
		LD_HL_with, 0, 0,
		LD_DE_with, 0, 0,
		LD_BC_with, 0xff, 0,
		LDIR,
		
		LD_A_with, 4,
		OUT_A_to, 0xf0,
	
		LD_A_with, 5,
		OUT_A_to, 0xf1,

		LD_A_with, 6,
		OUT_A_to, 0xf2,
		
		EI,
		
		RET

		};

	bufaddr=cast(unsigned)buffer;

	get256[9]=bufaddr LO;
	get256[10]=bufaddr HI;
	
	cpm_cmd_line (&argc,&argv,argv_buffer); /* for reading command line */

	copyright();
	
	if (argc > 4 || argc < 2)
		help();
		
	if (argc > 3)
		page=atoi(argv[3]);
	else
		page=0;				/* why not */
		
	if (argc > 2)
		block=atoi(argv[2]);
	else
		block=4;			/* normal bank 1 - TPA */
		
	sscanf(argv[1]," %4x",&addr);	
	
	if (page < 0 || page > 2)
		error("Page");
		
	if (block < 0 || block > 31)
		error("Block");	

	addr=addr-(addr % 16);			/* makes start address tidy */

	get256[2]=block;			/* sets up block & page in m/c */
	get256[4]=0xf0 | page;

	for(;;)
		{
	get256[6]=addr LO;			/* sets start address */
	get256[7]=addr HI;

	inline(					/* calls machine code routine */
	CALL, get256
	);

		for(st=0;st<256;st+=16)			/* start of display routine */
			{
			if ((addr+st)>=0xF430 && (addr+st) <=0xF44f)
				warning();
			printf("%04x - ",addr+st);
			for(i=0;i<16;i++)
				printf("%02x ",buffer[st+i]);
			printf("   ");
			for(j=0;j<16;j++)
				{
				if(isprint(buffer[st+j]))
					putchar(buffer[st+j]);
				else	putchar('.');
				}
			if (keyhit() && rawin(0)=='\003')
				{
				printf("\n");
				exit(0);
				}
			printf("\n");
			}	
		wait("\nPress ctrl-c to stop or any other key to continue\n\n");	
		addr+=256;
		}
	}

void wait(mess)
char *mess;

	{/*
	if (keyhit())			 emptys key buffer 
		rawin();*/
	printf("%s",mess);		/* prints message  */
	if (rawin()=='\003')		/* stops on ctrl-c */
		exit(0);
	}

void error(mess)
char *mess;

	{
	printf("\n%s out of range",mess);
	exit(0);
	}

void warning()

	{
	printf("\nWARNING part of this program resides between F430 and F450\n");
	printf("you are therefore prohibited from monitoring this section.\n\n");
	exit(0);
	}

void help()

	{
	printf("\nThis program allows you to monitor a memory location within any block of memory.\n");
	printf("Commands should be entered as follows:-\n");
	printf("\n     MONITOR 52F0 8 1\n");
	printf("\nwhich means, 'Switch block 8 into page 1 and look at memory starting at 52F0.'\n");
	printf("If Page and Block arguments are dropped, TPA bank remains switched in.\n");
	exit(0);
	}
	
void copyright()
	{
	printf("\nPCW MEMORY MONITOR v1.0  -  Copyright G.R. Buckeridge. 1989\n\n");
	}
	
	
#include ?cpm.lib
#include ?stdio.lib

ntf("\nPCW MEMORY MONITOR