
		output	.tos

		section	text

start:
		pea	demo				;Run demo in supervisor
		move.w	#$26,-(sp)			;supexec()
		trap	#14				;
		addq.l	#6,sp				;

		clr.w	-(sp)				;pterm()
		trap	#1				;



demo:		lea	save_screenadr,a0		;Save old screen address
		move.b	$ffff8201.w,(a0)+		;
		move.b	$ffff8203.w,(a0)+		;
		move.b	$ffff820d.w,(a0)+		;

		movem.l	$ffff8240.w,d0-d7		;Save old palette
		movem.l	d0-d7,save_pal			;

		move.l	#screen+256,d0			;Align new screen address
		clr.b	d0				;

		lsr.w	#8,d0				;Set new screen address
		move.l	d0,$ffff8200.w			;

		move.b	$ffff8260.w,save_res		;Save old resolution
		clr.b	$ffff8260.w			;Set low resolution

    ;            bsr     routine+44           ; ACIA PATCH
     ;           bsr     routine+60           ; SPURIOUS Interrupt Patch
                lea     modul(PC),A0
                move.w  #%1011,D0            ; Timer A,B,D duerfen fuer SIDs, etc. verwendet werden
                bsr     routine+4            ; * INIT 1 *

		move.w	#$2700,sr			;Stop all interrupts
		move.l	$70.w,save_vbl			;Save old VBL
		move.l	$68.w,save_hbl			;Save old HBL
		move.l	$134.w,save_ta			;Save old Timer A
		move.l	$120.w,save_tb			;Save old Timer B
		move.l	$114.w,save_tc			;Save old Timer C
		move.l	$110.w,save_td			;Save old Timer D
		move.l	$118.w,save_acia		;Save old ACIA
		move.l	#vbl,$70.w			;Install our own VBL
		move.l	#dummy,$68.w			;Install our own HBL (dummy)
		move.l	#dummy,$134.w			;Install our own Timer A (dummy)
		move.l	#timer_b,$120.w			;Install our own Timer B
		move.l	#timer_c,$114.w			;Install our own Timer C (dummy)
		move.l	#dummy,$110.w			;Install our own Timer D (dummy)
		move.l	#dummy,$118.w			;Install our own ACIA (dummy)
		move.b	$fffffa09.w,save_intb		;Save MFP state for interrupt enable B
		move.b	$fffffa15.w,save_intb_mask	;Save MFP state for interrupt mask B
	;	clr.b	$fffffa07.w			;Interrupt enable A (Timer-A & B)
	;	clr.b	$fffffa13.w			;Interrupt mask A (Timer-A & B)
	;	clr.b	$fffffa09.w			;Interrupt enable B (Timer-C & D)
	;	clr.b	$fffffa15.w			;Interrupt mask B (Timer-C & D)

	;	bset	#5,$fffffa07
	;	bset	#5,$fffffa09
	;	bset	#5,$fffffa0D
	;	bset	#5,$fffffa11
	;		bset	#5,$fffffa15
		move.w	#$2300,sr			;Interrupts back on

		move.b	#$12,$fffffc02.w		;Kill mouse



		;---------------------------------------
.mainloop:	tst.w	vblcount			;Wait VBL
		beq.s	.mainloop			;
		clr.w	vblcount			;
				andi    #$F4FF,SR            ; (fr den Fall, dass Digis,SIDs, etc.
                bsr     routine+12         ; 200Hz-Ansprung (Register werden
;                                              alle gerettet)
		cmp.b	#$39,$fffffc02.w		;Space?
		bne.s	.mainloop			;
		;----------------------------------------


		move.w	#$2700,sr			;Stop all interrupts
		move.l	save_vbl,$70.w			;Restore old VBL
		move.l	save_hbl,$68.w			;Restore old HBL
		move.l	save_ta,$134.w			;Restore old Timer A
		move.l	save_tb,$120.w			;Restore old Timer B
		move.l	save_tc,$114.w			;Restore old Timer C
		move.l	save_td,$110.w			;Restore old Timer D
		move.l	save_acia,$118.w		;Restore old ACIA
		move.b	save_intb,$fffffa09.w		;Restore MFP state for interrupt enable B
		move.b	save_intb_mask,$fffffa15.w	;Restore MFP state for interrupt mask B
		clr.b	$fffffa1b.w			;Timer B control (Stop)
		move.w	#$2300,sr			;Interrupts back on

		move.b	save_res,$ffff8260.w		;Restore old resolution

		movem.l	save_pal,d0-d7			;Restore old palette
		movem.l	d0-d7,$ffff8240.w		;

		lea	save_screenadr,a0		;Restore old screen address
		move.b	(a0)+,$ffff8201.w		;
		move.b	(a0)+,$ffff8203.w		;
		move.b	(a0)+,$ffff820d.w		;

		move.b	#$8,$fffffc02.w			;Enable mouse



		rts


vbl:		addq.w	#1,vblcount
		clr.w	raster_ofs

		;Start up Timer B each VBL
		move.w	#$2700,sr			;Stop all interrupts
		clr.b	$fffffa1b.w			;Timer B control (stop)
		bset	#0,$fffffa07.w			;Interrupt enable A (Timer B)
		bset	#0,$fffffa13.w			;Interrupt mask A (Timer B)
		move.l	#timer_b,$120.w			;Install our own Timer B
		move.b	#1,$fffffa21.w			;Timer B data (number of scanlines to next interrupt)
		bclr	#3,$fffffa17.w			;Automatic end of interrupt
		move.b	#8,$fffffa1b.w			;Timer B control (event mode (HBL))
		move.w	#$2300,sr			;Interrupts back on

		rte

timer_b:	move.l	a0,-(sp)			;Save A0
		lea	rasters,a0			;Raster list
		add.w	raster_ofs,a0			;Raster list offset
		move.w	(a0),$ffff8240.w		;Set colour
		addq.w	#2,raster_ofs			;Next raster offset
		move.l	(sp)+,a0			;Restore A0
		rte

timer_c:
		;move #$2600,sr
		;movem.l	a0-a7,-(sp)
		;jsr		music+12
		;movem.l	(sp)+,a0-a7
		rte
dummy:		rte

		section	data

rasters:
		rept	4
		dc.w	$0001,$0002,$0003,$0004,$0005,$0006,$0007,$0017,$0027,$0037
		dc.w	$0047,$0057,$0067,$0077,$0076,$0075,$0074,$0073,$0072,$0071
		dc.w	$0070,$0170,$0270,$0370,$0470,$0570,$0670,$0770,$0760,$0750
		dc.w	$0740,$0730,$0720,$0710,$0700,$0701,$0702,$0703,$0704,$0705
		dc.w	$0706,$0707,$0606,$0505,$0404,$0303,$0202,$0101,$0000,$0000
		endr

routine:
	incbin mm25pl.bin
	even

modul:
	incbin wh.mod
	even

		section	bss

raster_ofs:	ds.w	1
vblcount:	ds.w	1
screen:		ds.b	32000+256
save_pal:	ds.w	16
save_screenadr:	ds.l	1
save_vbl:	ds.l	1
save_hbl:	ds.l	1
save_ta:	ds.l	1
save_tb:	ds.l	1
save_tc:	ds.l	1
save_td:	ds.l	1
save_acia:	ds.l	1
save_intb:	ds.b	1
save_intb_mask:	ds.b	1
save_res:	ds.b	1
		even

	end
