	opt c- o+
mminx:		equ	0
mmaxx:		equ	319
mminy:		equ	0
mmaxy:		equ	199
cxmin:		equ	0
cxmax:		equ	319
cymin:		equ	0
cymax:		equ	199
vbl_vec:	equ	$70
key_vec:	equ	$118
hbl_vec:	equ	$120
bpwidth:	equ	320
bpheight:	equ	200
start:
	clr.l   -(SP)           ;Go into supervisor mode
        move.w  #$20,-(SP)
        trap    #1
        addq.l  #6,SP
        move.l  D0,savesup
	dc.w	$a00a

	lea	palsave,a0
	move.l	#$ff8240,a1
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+

	move.w	#4,-(sp)
	trap	#14
	addq.l	#2,sp
	move.w	d0,ressave

	move.w	#7,-(sp)	;set to low res
	move.l	#-1,-(sp)
	move.l	#-1,-(sp)
	move.w	#5,-(sp)
	trap	#14
	add.l	#12,sp
	bsr	getscrn
*	bsr	plotinit
	lea	greypalette,a0
	bsr	mydocolors
	bsr	putrout

	move.l	seen,a0
	move.w	#20*200-1,d7
	move.w	#$aaaa,d0
filltop:
	move.w	d0,(a0)+
	move.w	d0,(a0)+
	move.w	d0,(a0)+
	move.w	d0,(a0)+
	move.l	#0,(a0)+
	move.l	#0,(a0)+
	dbf	d7,filltop


mainloop:
	bsr	domag
	bsr	keyboard
	bra	mainloop





currentmag:	dc.w	2	;current magnification factor
currentmh:	dc.w	88/2-1	;the number of magnified lines to do
currentmw:	dc.w	320/2-1	;320/mag = number pixels horiz to mag
mousemove:	dc.w	-1
magtrack:	dc.w	-1

domag:
*	cmpi.w	#1,paletteshowing
*	bne.s	nomag
*	tst.w	mousemove
*	bmi.s	nomag
dodomag:
	move.w	mx,d0
	move.w	my,d1
	move.w	currentmw,d2
	asr.w	#1,d2		;halve it
	sub.w	d2,d0
	bpl.s	mlisok
	moveq	#0,d0
mlisok:				;left of mouse is ok
	move.w	d0,d3
	add.w	d2,d3
	add.w	d2,d3
	cmpi.w	#320,d3		;is it too far right
	blt.s	mrisok
	move.w	#319,d0
	sub.w	currentmw,d0
mrisok:
	move.w	currentmh,d2
	asr.w	#1,d2		;halve it
	sub.w	d2,d1
	bpl.s	muisok
	moveq	#0,d1
muisok:				;left of mouse is ok
	move.w	d1,d3
	add.w	d2,d3
	add.w	d2,d3
	cmpi.w	#480,d3		;is it too far right
	blt.s	mdisok
	move.w	#479,d1
	sub.w	currentmh,d1
mdisok:
	move.l	seen,a1		;was currentscreen
	mulu	#320,d1
	add.l	d1,a1
	and.w	#$fff0,d0
	add.w	d0,a1
	move.l	seen,a0		;was maghiden

	add.l	#320*240,a0	;halfwaydown	(only for test)

	move.l	a0,a2
	moveq	#87,d7
	moveq	#0,d0
clearmag:
	rept	20
	move.l	d0,(a2)+
	move.l	d0,(a2)+
	move.l	d0,(a2)+
	move.l	d0,(a2)+
	endr
	dbf	d7,clearmag
	
*****************************************************************
*	generic routine for 2 4 and 8				*
*	Expands from a1 to a0.					*
*****************************************************************
expand:
	move.w	currentmag,d4		;bf width for set = magnification
	move.w	currentmh,d6		;precalc'd height
	moveq	#0,d5			;offset destination
	move.w	#15,a2			;counter for 15 shifts
	move.w	currentmw,a3		;precalc'd width in pixels






	movem.l	d4-d6/a0-a3,-(sp)
	move.l	a0,a4
	move.l	a1,a5
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
magloop1:
	move.w	a3,d7		;number of bits to test=width of source
magloop2:
p0test:
	lsl.w	#1,d0
	bcc.s	p0clea			;bit was 0 so go clear in dest
	dc.w	%1110111011010000	;bfset	a0{d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
p0clea:
	lsl.w	#1,d1
	bcc.s	p1clea			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	2(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	2
p1clea:
	lsl.w	#1,d2
	bcc.s	p2clear			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	4(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	4
p2clear:
	lsl.w	#1,d3
	bcc.s	p3clear			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	6(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	6
p3clear:
	sub.w	#1,a2		;count shifts
	cmp.w	#-1,a2
	bne.s	offok
	move.w	#15,a2
	addq.l	#8,a1
*	lea	16(a1),a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
offok:
	add.w	d4,d5		;advance destination by magnification
	andi.w	#15,d5
	bne	offok2
	lea	16(a0),a0
offok2:
	dbf	d7,magloop2
	lea	320(a5),a5
	bsr	expanddown
	move.l	a0,a4
	move.l	a5,a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
	dbf	d6,magloop1

	movem.l	(sp)+,d4-d6/a0-a3
	addq.l	#8,a0
	addq.l	#8,a1
	move.l	a0,a4
	move.l	a1,a5
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3

magloop1b:
	move.w	a3,d7		;number of bits to test=width of source
magloop2b:
p0testb:
	lsl.w	#1,d0
	bcc.s	p0clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011010000	;bfset	a0{d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
p0clearb:
	lsl.w	#1,d1
	bcc.s	p1clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	2(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	2
p1clearb:
	lsl.w	#1,d2
	bcc.s	p2clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	4(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	4
p2clearb:
	lsl.w	#1,d3
	bcc.s	p3clearb			;bit was 0 so go clear in dest
	dc.w	%1110111011101000	;bfset	6(a0){d5:d4}
	dc.w	%0000100101100100	;set dest with proper width
	dc.w	6
p3clearb:
	sub.w	#1,a2		;count shifts
	cmp.w	#-1,a2
	bne.s	offokb
	move.w	#15,a2
	addq.l	#8,a1
*	lea	16(a1),a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
offokb:
	add.w	d4,d5		;advance destination by magnification
	andi.w	#15,d5
	bne	offok2b
	lea	16(a0),a0
offok2b:
	dbf	d7,magloop2b
	lea	320(a5),a5
	bsr	expanddown
	move.l	a0,a4
	move.l	a5,a1
	move.w	(a1)+,d0
	move.w	(a1)+,d1
	move.w	(a1)+,d2
	move.w	(a1)+,d3
	dbf	d6,magloop1b
*	move.l	magseen,d0
*	move.l	maghiden,magseen
*	move.l	d0,maghiden
	rts

expanddown:
	move.w	d4,d0		;get mag
	subq.w	#2,d0		;-1 for dbf and -1 for line already done
	lea	320(a4),a0	;we get data from a4 and stick it to a0
	move.l	a4,a1
edl1:
	rept	20
	move.l	(a1)+,(a0)+	;one whole line
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	move.l	(a1)+,(a0)+
	endr
	move.l	a4,a1
	subq.w	#1,d0
	bpl	edl1
	rts










flag:	dc.w	0
plotline:
	movem.l	d0-a6,-(sp)
	move.w	#0,flag		;flag for how many coords swapped
	move.w	x1,d0
	cmp.w	x2,d0
	blt.s	noexgx
	move.w	x2,x1
	move.w	d0,x2
	or.w	#1,flag
noexgx:
	move.w	y1,d0
	cmp.w	y2,d0
	blt.s	noexgy
	move.w	y2,y1
	move.w	d0,y2
	or.w	#2,flag
noexgy:
noshit:
	move.w	x1,d0
	sub.w	x2,d0
	bge.s	li10
	neg.w	d0
li10:
	move.w	y1,d1
	sub.w	y2,d1
	bge.s	li20
	neg.w	d1
li20:
	cmp.w	d0,d1
	bgt	ymajor

	move.w	d1,d4
	sub.w	d0,d4
	move.w	d4,d5
	add.w	d4,d4
	add.w	d1,d5
	move.w	d1,d3
	add.w	d1,d3

	move.w	x2,d6
	cmp.w	x1,d6
	bge.s	li30
	move.w	y2,d7
	move.w	x1,d2
	bra.s	li40
li30:
	move.w	x1,d6
	move.w	y1,d7
	move.w	x2,d2
li40:
	subq.w	#1,d2
xmajor1:
	cmpi.w	#0,flag
	beq.s	stayxm1
	cmpi.w	#3,flag
	bne.s	xmajor2
stayxm1:
	move.w	d6,d0
	move.w	d7,d1
	bsr	singleplot
	bra.s	xm1li80
xm1li50:
	addq.w	#1,d6
	tst.w	d5
	bge.s	xm1li60
	add.w	d3,d5
	bra.s	xm1li70
xm1li60:
	addq.w	#1,d7
	add.w	d4,d5
xm1li70:
	move.w	d6,d0
	move.w	d7,d1
	bsr	singleplot
xm1li80:
	cmp.w	d6,d2
	bge.s	xm1li50
	movem.l	(sp)+,d0-a6
	rts
xmajor2:
	cmpi.w	#1,flag
	bne.s	skk
	move.w	y2,d7
	bra	skkk
skk:
	cmpi.w	#2,flag
	bne.s	skkk
	move.w	y2,d7
skkk:
	move.w	d6,d0
	move.w	d7,d1
	bsr	singleplot
	bra.s	xm2li80
xm2li50:
	addq.w	#1,d6
	tst.w	d5
	bge.s	xm2li60
	add.w	d3,d5
	bra.s	xm2li70
xm2li60:
	subq.w	#1,d7
	add.w	d4,d5
xm2li70:
	move.w	d6,d0
	move.w	d7,d1
	bsr	singleplot
xm2li80:
	cmp.w	d6,d2
	bge.s	xm2li50
	movem.l	(sp)+,d0-a6
	rts

ymajor:
	move.w	d0,d4
	sub.w	d1,d4
	move.w	d4,d5
	add.w	d4,d4
	add.w	d0,d5
	move.w	d0,d3
	add.w	d0,d3
	move.w	y2,d6
	cmp.w	y1,d6
	bge.s	li30b
	move.w	x2,d7
	move.w	y1,d2
	bra.s	li40b
li30b:
	move.w	y1,d6
	move.w	x1,d7
	move.w	y2,d2
li40b:
	subq.w	#1,d2
ymajor1:
	cmpi.w	#0,flag
	beq.s	stayym1
	cmpi.w	#3,flag
	bne.s	ymajor2
stayym1:
	move.w	d7,d0
	move.w	d6,d1
	bsr	singleplot
	bra.s	ym1li80
ym1li50:
	addq.w	#1,d6
	tst.w	d5
	bge.s	ym1li60
	add.w	d3,d5
	bra.s	ym1li70
ym1li60:
	addq.w	#1,d7
	add.w	d4,d5
ym1li70:
	move.w	d7,d0
	move.w	d6,d1
	bsr	singleplot
ym1li80:
	cmp.w	d6,d2
	bge.s	ym1li50
	movem.l	(sp)+,d0-a6
	rts
ymajor2:
	cmpi.w	#1,flag
	bne.s	skkb
	move.w	x2,d7
	bra	skkkb
skkb:
	cmpi.w	#2,flag
	bne.s	skkkb
	move.w	x2,d7
skkkb:
	move.w	d7,d0
	move.w	d6,d1
	bsr	singleplot
	bra.s	ym2li80
ym2li50:
	addq.w	#1,d6
	tst.w	d5
	bge.s	ym2li60
	add.w	d3,d5
	bra.s	ym2li70
ym2li60:
	subq.w	#1,d7
	add.w	d4,d5
ym2li70:
	move.w	d7,d0
	move.w	d6,d1
	bsr	singleplot
ym2li80:
	cmp.w	d6,d2
	bge.s	ym2li50
	movem.l	(sp)+,d0-a6
	rts



****************************************
keyboard:
	move.w	#$ff,-(sp)
	move.w	#$6,-(sp)
	trap	#1
	addq.l	#4,sp
	tst.l	d0
	beq	keyback
	swap	d0

	cmp.b	#16,d0		*space
	beq	gem		*yes/no
keyback:
	rts

*****************************************
*	number in d0.l			*
*	d1.l is base			*
*	d2,x cursor			*
*	d3,y cursor			*
*****************************************
prtnum:
	movem.l	d0/d1,-(sp)
	lea	posstring,a0
	add.w	#32,d3
	move.b	d3,2(a0)	
	move.b	d3,10(a0)	
	add.w	#32,d2
	move.b	d2,3(a0)
	move.b	d2,11(a0)	
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	movem.l	(sp)+,d0/d1

	lea	linebuffer,a0
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
	clr.l	(a0)+
loop1:
	divu	d1,d0
	swap	d0
	addi	#'0',d0
	cmpi.w	#'9',d0
	ble.s	ok
	addq.w	#7,d0
ok:
	move.b	d0,-(a0)
	clr	d0
	swap	d0
	bne	loop1
loop2:
	move.l	a0,-(sp)
	move.w	#9,-(sp)
	trap	#1
	addq.l	#6,sp
	rts


linebuffer:
	ds.l	5
digits:
	dc.b	0
posstring:	dc.b	27,'Y',0,0,' ',' ',' ',' ',27,'Y',0,0,0
	even



***********************************
mydocolors:
	move.l	#$ff8240,a1
	moveq	#15,d0
mdcloop:
	move.w	(a0)+,(a1)+
	dbf	d0,mdcloop
	rts

************get screen address***************
getscrn:move.w  #2,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,seen
	rts

oldmouse:	dc.l	0
******************************************
*               RETURN TO GEM
gem:
*	move.b	#%11111110,$ff820a
	move.l	#$ff8240,a0
	lea	palsave,a1
	moveq	#15,d7
rspl:
	move.w	(a1)+,d0
	and.w	#$0777,d0
	move.w	d0,(a0)+
	dbf	d7,rspl

	move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	oldmouse,16(a0)

*	move.l	vblslot,a0
*	move.l	#0,(a0)

        move.w  ressave,-(sp)
        move.l  seen,-(sp)
        move.l  seen,-(sp)
        move.w  #5,-(sp)
        trap    #14
        add.l  #12,sp

	move.l  savesup,-(SP)    ;Back to user mode
        move.w  #$20,-(SP)
        trap    #1
        addq.l  #6,SP
*illegal
	clr.w	-(sp)
	trap	#1

buttondat:	dc.w	0
mx:		dc.w	0
my:	dc.w	0
*************puts my mouse interupt in***************** 
putrout:move.w  #34,-(sp)
        trap    #14
        addq.l  #2,sp
        move.l  d0,a0
	move.l	16(a0),oldmouse
        move.l  #myrout,16(a0)
        rts
lastb:		dc.w	-1
mousehasgoneup:	dc.w	0

**************my ubeut mouse interupt*******************
myrout:
	movem.l	d0-d2/a0-a1,-(sp)
	moveq	#0,d0	
	move.b	(a0),d0
	cmpi.b	#$fa,d0
	bne	myr1
	move.w	#1,buttondat
	bra	myrxy
myr1:
	cmpi.b	#$f9,d0
	bne	myr2
	move.w	#2,buttondat
	bra	myrxy
myr2:
	cmpi.b	#$fb,d0
	bne	myr3
	move.w	#3,buttondat
	bra	myrxy
myr3:
	move.w	#1,mousehasgoneup
	move.w	#0,buttondat
myrxy:
	moveq	#0,d0
	move.b	1(a0),d0
	ext.w	d0
	moveq	#0,d1
	move.b	2(a0),d1
	ext.w	d1
	add.w	d0,mx
	add.w	d1,my
*	sub.w	d1,my2
myr4:	move.w	mx,d0
	move.w	my,d1
	bsr	extra
	move.w	d0,mx
	move.w	d1,my
	movem.l	(sp)+,d0-d2/a0-a1
	rts

extra:  cmpi.w	#mminx,d0
        bge	extra1
        move.w	#mminx,d0
        bra	extra2
extra1: cmpi.w   #mmaxx,d0
        ble	extra2
        move.w  #mmaxx,d0
extra2:	cmpi.w  #mminy,d1
        bge	extra3
        move.w  #mminy,d1
        bra	extra4
extra3: cmpi.w   #mmaxy,d1
        ble	extra4
        move.w  #mmaxy,d1
extra4:
        rts



********************************************
* Atari ST hi-speed plotting subroutine    *
* For Low-resolution mode only		   *
********************************************
plotinit:
	move.l	seen,a0
	move.b	#200,d1
	lea	ytable1,a1
iloop:
	move.l	a0,(a1)+
	adda.w	#160,a0
	subq.b	#1,d1
	bne	iloop
	rts
**********************************************
singleplot:
	movem.l d0-d5/a0-a1,-(sp)
*	move.w	pixelx,d0
	cmp.w	#cxmax,d0
	bgt	pltxit
	cmpi.w	#cxmin,d0
	blt	pltxit
	move.w	d0,d3
	andi.w	#15,d3
*	move.w	pixely,d1
	cmp.w	#cymax,d1
	bgt	pltxit
	cmpi.w	#cymin,d1
	blt	pltxit
	lsl.w	#2,d1
	move.w	pixelcolor,d2

	lsr.w	#1,d0
	and.w	#$00f8,d0
	move.w	d0,a0
	lea	ytable1,a1
	adda.l	(a1,d1.w),a0

	move.w #$8000,d4
	lsr.w d3,d4
	move.l #$80008000,d5
	lsr.l d3,d5
	eor.l #$ffffffff,d5
******************************
*    Turn off old pixel!     *
******************************
myphere:
	and.l d5,(a0)	     ;turn off old pixel
	and.l d5,4(a0)	     ;(both longwords)

******************************
* Now turn on the new pixel! *
******************************
	btst #0,d2
	beq bit2
	or.w d4,(a0)
bit2:
	btst #1,d2
	beq bit4
	or.w d4,2(a0)
bit4:
	btst #2,d2
	beq bit8
	or.w d4,4(a0)
bit8:
	btst #3,d2
	beq pltxit
	or.w d4,6(a0)
pltxit:
	movem.l (sp)+,d0-d5/a0-a1
	rts
pltxit2:
	movem.l (sp)+,d0-d5/a0-a1
	moveq	#-1,d0
	rts
pltxit3:
	movem.l (sp)+,d0-d5/a0-a1
	moveq	#-2,d0
	rts



rvsave:	dc.l	0
resave:	dc.l	0
	even
savesup:	dc.l	0
ressave:	dc.w	0
filename:	dc.l	0
greypalette:
*	incbin	'ste.pal'
	dc.w	$0000,$0222,$0444,$0fff,$0fff,$0fff,$0fff
	dc.w	$0fff,$0fff,$0fff,$0fff,$0fff,$0fff,$0fff,$0fff,$0fff
	dc.w	$0fff,$0fff
save_usp:
	dc.l	0
wfnc:
	dc.w	0
save_stk:
	dc.l	0
pointer:	dc.l	0
pixelx:		dc.w	0
pixely:	dc.w	0
pixelcolor:	dc.w	15
flip:	dc.w	0
flip2:	dc.w	0
seen:	dc.l	0
hiden:	dc.l	0
clrbuffseen:	dc.l	0
clrbuffhiden:	dc.l	0
screen:	dc.l	0
	ds.w	16
*	section	bss
x1:	ds.w	1
y1:	ds.w	1
x2:	ds.w	1
y2:	ds.w	1

number:	ds.w	1
palsave:
	ds.w	16
vblslot:	ds.l	1
d7emulator:	ds.w	1
ytable1:	ds.w	200
