#include <stdio.h>

unsigned char ah,al,bh,bl,ch,cl,dh,dl,buff[256],mess[2];
unsigned int ax,bx,cx,dx,si,di,bp,ds,es;
int i,j,d,fd;

main(argc,argv)
	int argc;
	char **argv;
{
	mess[1]=0;
	if(argc!=4){ printf("usage : X, Y, color"); exit(0); }
	service_00();
	service_01(0x0103);
	service_03();
	bankset(1);
	for(i=0;i<atoi(argv[1]);i++){
		for(j=0;j<atoi(argv[2]);j++){
			plot(i,j,atoi(argv[3]));
		}
	}
	while(1){
		d=csts();
		if(d==0x1b) break;
	}
}

plot(xx,yy,data)
	int xx,yy;
	unsigned char data;
{
	al=data&0xff;
	cx=xx;
	dx=yy;
	es=0xa000;
#asm
	push	es
	push	ds
	mov	cx,word cx_
	mov	dx,word dx_
	clc
	mov	ax,800
	mul	dx
	add	ax,cx
	adc	dx,0
	mov	bx,ax
	mov	es,word es_
	mov	al,byte al_
	mov	byte es:[bx],al
	pop	ds
	pop	es
#
}

bankset(ddd)
	unsigned int ddd;
{
	dx=ddd;
#asm
	push	ds
	mov	bx,0
	mov	dx,word dx_
	mov	ax,4f05h
	int 10h
	pop	ds
#
}

service_03(){
#asm
	push	ds
	mov	ah,79
	mov	al,3
	int 10h
	mov	word ax_,ax
	mov	word bx_,bx
	pop	ds
#
/*
	printf("\nBIOS#3:\tresult = %04x , current mode = %04x",ax,bx);
*/
}

service_01(ddd)
	unsigned int ddd;
{
	di=&buff;
	es=_showds();
	cx=ddd;
#asm
	push	es
	push	ds
	mov	di,word di_
	mov	es,word es_
	mov	cx,word cx_
	mov	ah,79
	mov	al,1
	int 10h
	mov	word ax_,ax
	pop	ds
	pop	es
#
/*
	printf("\nBIOS#1:\tmode = %04x , result = %04x , ",cx,ax);
	printf("mode = %02x%02x",buff[1],buff[0]);
	printf("\n\twindow[A] = %02x , ",buff[2]);
	printf("window[B] = %02x , ",buff[3]);
	printf("grain = %02x%02x",buff[5],buff[4]);
	printf("\n\twindow size = %02x%02x , ",buff[7],buff[6]);
	printf("segment[A] = %02x%02x , ",buff[9],buff[8]);
	printf("segment[B] = %02x%02x",buff[11],buff[10]);
*/
}

service_00(){
	di=&buff;
	es=_showds();
#asm
	push	es
	push	ds
	mov	di,word di_
	mov	es,word es_
	mov	ah,79
	mov	al,0
	int 10h
	mov	word ax_,ax
	pop	ds
	pop	es
#
/*
	printf("\nBIOS#0:\tresult = %04x , ID = ",ax);
	for(i=0;i<4;i++){ mess[0]=buff[i]; printf("%s",mess); }
	printf(" , version = %02x%02x",buff[5],buff[4]);
*/
}

