
* Programma ShowKoala
* Vanuit CLI: ShowKoala <FileName>

	SECTION ShowKoala,CODE
Start	
*	move.l	#aFileName,A0	Voor het debuggen
*	move.l	#aFileLen,D0	Deze regel ook
	move.l	A0,Args		Bewaren Argumenen
	move.l	d0,ArgsLen
	move.b	#0,-1(a0,d0)
	bsr	OpenAll		Alle Libs openen
	
	cmp.l	#128,ArgsLen	Controleren op de lengte van de argumenten
	ble	.ok1
	lea	BadArg,A0
	bsr	Print
	bra	ClosAll
.ok1	cmp.l	#1,ArgsLen
	bne	.ok2
.noarg	lea	InfoText,A0
	bsr	Print
	bra	ClosAll
	
.ok2	move.l	Args,A0		Maken filenaam uit argumenten
	lea	FileName,A1
.skipsp	tst.b	(a0)
	beq.s	.noarg
	cmpi.b	#' ',(a0)+
	beq.s	.skipsp
	subq.w	#1,a0
	moveq	#0,d6		default: geen quotes
	cmp.b	#'"',(a0)
	bne.s	.loop
	moveq	#1,d6		quote mode
	addq.w	#1,a0
.loop	move.b	(A0)+,(A1)
	beq.s	.fndone
	cmp.b	#'"',(a1)
	beq.s	.fndone
	cmp.b	#' ',(a1)	spatie?
	bne.s	.fnloop
	tst.b	d6		tussen quotes?
	beq.s	.fndone
.fnloop	addq.w	#1,a1
	bra.s	.loop
.fndone	move.b	#0,(a1)		anders einde arg

	lea	FileName,A0
	bsr	Print
	lea	DubblePoint,A0
	bsr	Print
	
	lea	FileName,A0	Bestaat de file
	move.l	A0,D1
	move.l	#ACCESS_READ,D2
	CALLDOS Lock
	move.l	D0,MyLock
	bne	.ok3
	lea	NoKoala,A0
	bsr	Print
	bra	ClosAll
	
.ok3	move.l	MyLock,D1	Vraag file informatie op
	lea	MyFIB,A0
	move.l	A0,D2
	CALLDOS Examine
	tst.w	D0
	bne	.ok4
	lea	Iets,A0
	bsr	Print
	bra	ClosAll
	
.ok4	lea	MyFIB,A0	Is de file een koalafile
	move.l	fib_Size(A0),D0
	cmp.l	#10001,D0
	beq	.ok5
	cmp.l	#10003,D0
	beq	.ok5
	cmp.l	#10112,D0
	beq	.ok5
	lea	NoKoala,A0
	bsr	Print
	bra	ClosAll

.ok5	move.l	MyLock,D1	Lock is niet meer nodig, verwijderen
	CALLDOS UnLock
	move.l	#0,MyLock
	
	lea	FileName,A0	File Openen
	move.l	A0,D1
	move.l	#MODE_OLDFILE,D2
	CALLDOS Open
	move.l	D0,MyFile
	bne	.ok6
	lea	Iets,A0
	bsr	Print
	bra	ClosAll
	
.ok6	lea	MyFIB,A0	Eventueel eerste twee bytes skippen
	move.l	fib_Size(A0),D0
	cmp.l	#10001,D0
	beq	.ok7
	move.l	MyFile,D1
	lea	KoalaBitMap,A0
	move.l	A0,D2
	move.l	#2,D3
	CALLDOS Read
	cmp.l	#2,D0
	beq	.ok7
	lea	Iets,A0
	bsr	Print
	bra	ClosAll
	
.ok7	move.l	MyFile,D1	Inlezen koala plaatje
	lea	KoalaBitMap,A0
	move.l	A0,D2
	move.l	#10001,D3
	CALLDOS Read
	cmp.l	#10001,D0
	beq	.ok8
	lea	Iets,A0
	bsr	Print
	bra	ClosAll
	
.ok8	bsr	OpenScr	 	Scherm Openen e.d.
	lea	MyVars,A6	Initialisatie voor omrekenen
	lea	KoalaCol12,A0
	move.l	A0,PC12(A6)
	lea	KoalaCol3,A0
	move.l	A0,PC3(A6)
	move.b	#0,LineCnt(A6)
	lea	Kleur,A5
	move.b	KoalaBGCol,0(A5)
	move.l	MyScreen,A0
	lea	sc_BitMap(A0),A0
	move.l	bm_Planes+0(A0),A1
	move.l	bm_Planes+4(A0),A2
	move.l	bm_Planes+8(A0),A3
	move.l	bm_Planes+12(A0),A4
	lea	KoalaBitMap,A0
	move.l	A0,PBitMap(A6)
	
	moveq.w	#0,D0
.loop2	move.l	PC12(A6),A0
	move.b	(A0),D2
	and.b	#%11110000,D2
	lsr.b	#4,D2
	move.b	D2,1(A5)		Kleur 2
	move.b	(A0),D2
	and.b	#%1111,D2
	move.b	D2,2(A5)		Kleur 3
	add.l	#1,PC12(A6)
	move.l	PC3(A6),A0
	move.b	(A0),3(A5)		Kleur 4
	add.l	#1,PC3(A6)
	moveq.w	#0,D1
.loop3	move.l	PBitMap(A6),A0
	move.b	(A0),D2			D2=Byte van KoalaBitMap
	add.l	#1,PBitMap(A6)
	moveq.w	#0,D3
	move.b	D2,D3
	and.b	#192,D3
	lsr.b	#6,D3
	move.b	(A5,D3.w),D4		D4=eerste pixel
	move.b	D2,D3
	and.b	#48,D3
	lsr.b	#4,D3
	move.b	(A5,D3.w),D5		D5=tweede pixel
	move.b	D2,D3
	and.b	#12,D3
	lsr.b	#2,D3
	move.b	(A5,D3.w),D6		D6=derde pixel
	move.b	D2,D3
	and.b	#3,D3
	move.b	(A5,D3.w),D7		D7=vierde pixel
	
* Byte van eerste bitplane berekenen
	moveq.w	#0,D2
	ROXR	#1,D4
	ROXL	#1,D2
	ROXR	#1,D5
	ROXL	#1,D2
	ROXR	#1,D6
	ROXL	#1,D2
	ROXR	#1,D7
	ROXL	#1,D2
	lea	Blow,A0
	move.b	(A0,D2),D2
	move.b	D2,(A1)
	lea	40(A1),A1
* Byte van tweede bitplane berekenen
	moveq.w	#0,D2
	ROXR	#1,D4
	ROXL	#1,D2
	ROXR	#1,D5
	ROXL	#1,D2
	ROXR	#1,D6
	ROXL	#1,D2
	ROXR	#1,D7
	ROXL	#1,D2
	move.b	(A0,D2),D2
	move.b	D2,(A2)
	lea	40(A2),A2
* Byte van derde bitplane berekenen
	moveq.w	#0,D2
	ROXR	#1,D4
	ROXL	#1,D2
	ROXR	#1,D5
	ROXL	#1,D2
	ROXR	#1,D6
	ROXL	#1,D2
	ROXR	#1,D7
	ROXL	#1,D2
	move.b	(A0,D2),D2
	move.b	D2,(A3)
	lea	40(A3),A3
* Byte van vierde bitplane berekenen
	moveq.w	#0,D2
	ROXR	#1,D4
	ROXL	#1,D2
	ROXR	#1,D5
	ROXL	#1,D2
	ROXR	#1,D6
	ROXL	#1,D2
	ROXR	#1,D7
	ROXL	#1,D2
	move.b	(A0,D2),D2
	move.b	D2,(A4)
	lea	40(A4),A4
	
	addq.w	#1,D1			Einde loop3
	cmp.w	#8,D1
	bne	.loop3

	add.b	#1,LineCnt(A6)
	cmp.b	#40,LineCnt(A6)
	bne	.lineok
	move.b	#0,LineCnt(A6)
	lea	-39(A1),A1
	lea	-39(A2),A2
	lea	-39(A3),A3
	lea	-39(A4),A4
	bra	.noline
.lineok	lea	-319(A1),A1
	lea	-319(A2),A2
	lea	-319(A3),A3
	lea	-319(A4),A4
.noline	addq.w	#1,D0
	cmp.w	#1000,D0
	bne	.loop2			Einde loop2
	
	move.l	MyScreen,A0
	CALLINT ScreenToFront
	move.l	MyWindow,A0
	CALLINT ActivateWindow
	move.l	MyWindow,A0
	move.l	wd_UserPort(A0),D7
.loop4	move.l	D7,A0			D7=UserPort
	CALLEXEC WaitPort
	move.l	D7,A0
	CALLEXEC GetMsg
	move.l	D0,A1
	move.l	im_Class(A1),D6
	CALLEXEC ReplyMsg
	tst.w	TimeOut
	beq	.notime
	cmp.l	#INTUITICKS,D6
	bne	.notime
	subi.w	#1,TimeOut
	beq	Einde
.notime	cmp.l	#INACTIVEWINDOW,D6
	beq	Einde
	cmp.l	#MOUSEBUTTONS,D6
	bne	.loop4
	
Einde	lea	OkText,A0
	bsr	Print

	bra	ClosAll
	
OpenAll	lea	DosName,A1
	moveq.l	#0,D0
	CALLEXEC OpenLibrary
	move.l	D0,_DOSBase
	beq	ClosAll
	lea	IntName,A1
	moveq.l	#0,D0
	CALLEXEC OpenLibrary
	move.l	D0,_IntuitionBase
	beq	ClosAll
	lea	GfxName,A1
	moveq.l	#0,D0
	CALLEXEC OpenLibrary
	move.l	D0,_GfxBase
	beq	ClosAll
	rts

OpenScr	lea	MyNewScreen,A0
	CALLINT OpenScreen
	move.l	D0,MyScreen
	bne	.ok
	lea	NoMem,A0
	bsr	Print
	bra	ClosAll
.ok	move.l	MyScreen,A0
	LEA	sc_ViewPort(A0),A0
	LEA	C64Pallet,A1
	moveq.w	#16,D0
	CALLGRAF LoadRGB4
	lea	MyNewWindow,A0
	move.l	MyScreen,nw_Screen(A0)
	CALLINT	OpenWindow
	move.l	D0,MyWindow
	bne	.ok2
	lea	NoMem,A0
	bsr	Print
	bra	ClosAll
.ok2	rts

ClosAll	move.l	MyFile,D1
	beq	Clos2
	CALLDOS	Close
Clos2	move.l	MyLock,D1
	beq	Clos3
	CALLDOS UnLock
Clos3	tst.l	MyWindow
	beq	Clos4
	movea.l	MyWindow,A0
	CALLINT	CloseWindow
Clos4	tst.l	MyScreen
	beq	Clos5
	movea.l	MyScreen,A0
	CALLINT CloseScreen
Clos5	tst.l	_DOSBase
	beq	Clos6
	movea.l	_DOSBase,A1
	CALLEXEC CloseLibrary
Clos6	tst.l	_GfxBase
	beq	Clos7
	movea.l	_GfxBase,A1
	CALLEXEC CloseLibrary
Clos7	tst.l	_IntuitionBase
	beq	Clos8
	movea.l	_IntuitionBase,A1
	CALLEXEC CloseLibrary
Clos8	moveq.l	#0,D0
	rts

* Print String, A0=Pointer naar nul terminated string
Print
	move.l	a0,-(sp)
	CALLDOS Output
	move.l	(sp)+,a0
	move.l	D0,D1
	beq	Prnst
	moveq.l	#-1,D3
	move.l	A0,A1

Prntlp	addq.l	#1,D3
	tst.b	(A1)
	lea	1(A1),A1
	bne	Prntlp
	
	move.l	A0,D2
	CALLDOS Write
Prnst	rts

	SECTION Struct,DATA
DosName		dc.b "dos.library",0
IntName		dc.b "intuition.library",0
GfxName		dc.b "graphics.library",0
WinName 	dc.b "ShowKoala V1.0 Joran Jessurun (c) 1992",0
BadArg 		dc.b "Bad Args.",10,0
NoKoala 	dc.b "This isn't a koala picture.",10,0
Iets 		dc.b "What happend",10,0
OkText 		dc.b "Ok.",10,0
DubblePoint 	dc.b ": ",0
NoMem 		dc.b "Out of memory.",10,0
InfoText 	dc.b "ShowKoala V1.0 by Joran Jessurun",10
		dc.b "Usage: ShowKoala <FileName>",10
		dc.b "       LMB To quit",10,0
		even
	
*aFileName dc.b 'dh0:koala a',10	;Voor het debuggen
* 	even
*aFileLen equ	*-aFileName

_DOSBase dc.l 0
_GfxBase dc.l 0
_IntuitionBase dc.l 0

MyScreen dc.l 0
MyWindow dc.l 0
MyLock dc.l 0
MyFile dc.l 0
TimeOut dc.w 500

MyNewScreen
	dc.w 0,0,320,200,4
	dc.b 0,1
	dc.w 0,CUSTOMSCREEN+SCREENBEHIND
	dc.l 0,0,0,0
MyNewWindow
	dc.w 0,0,320,200
	dc.b 0,1
	dc.l MOUSEBUTTONS+INTUITICKS+INACTIVEWINDOW,BORDERLESS+BACKDROP,0,0,WinName,0,0
	dc.w 0,0,0,0,CUSTOMSCREEN
C64Pallet
	dc.w $000,$DDD,$C44,$5CC,$A4A,$4C5,$44C,$FF6
	dc.w $A52,$850,$E88,$666,$888,$9F9,$77F,$BBB

Blow	dc.b %00000000
	dc.b %00000011
	dc.b %00001100
	dc.b %00001111
	dc.b %00110000
	dc.b %00110011
	dc.b %00111100
	dc.b %00111111
	dc.b %11000000
	dc.b %11000011
	dc.b %11001100
	dc.b %11001111
	dc.b %11110000
	dc.b %11110011
	dc.b %11111100
	dc.b %11111111
	
	SECTION FileInfo,BSS
MyFIB	ds.b fib_SIZEOF
	
	SECTION Other,BSS

PC12	EQU	0
PC3	EQU	4
PBitMap	EQU	8
LineCnt	EQU	12
MyVars	ds.l 4

Args	ds.l 1
ArgsLen	ds.l 1
FileName ds.b 130
Kleur ds.b 4

KoalaBitMap
	ds.b 8000
KoalaCol12
	ds.b 1000
KoalaCol3
	ds.b 1000
KoalaBGCol
	ds.b 2
