* TAB=10 (why? that's how my DevPac came way back when...)
*
* DCS.S - Assembly Lesson # 1
*   by Keith Gerdes
*    & Michael B. Vederman
*
* Developed and tested using HiSoft's DevPac ST/TT
*

* Equates for picture file type
*  Change "type_pic" variable in DATA segment to desired picture type
NEO	= 0
PIx	= 1
* NOTE: PIx refers to PI1, PI2 or PI3 file

	TEXT			; TEXT segment
START:
* Get new screen address (STs require a 256 ($100) byte boundary)
	move.l	#buffer,d0	; 32000+256 byte buffer
	clr.b	d0		; $100 byte boundary round off
	add.l	#$100,d0		; now round up
	move.l	d0,new_scrn	; new screen buffer

* Get current resolution
	move.w	#4,-(a7)
	trap	#14		; getRez
	addq.l	#2,a7
	move.w	d0,rez		; resolution (0,1,2)
	move.w	d0,the_rez	; resolution variable

* Get current screen address
	move.w	#2,-(a7)
	trap	#14		; physBase
	addq.l	#2,a7
	move.l	d0,old_scrn	; present screen address

	dc.w	$a00a		; hide mouse cursor

* Open picture file
	lea	name_neo,a0	; NEO
	cmp.b	#NEO,type_pic	; NEO?
	beq.s	f_open		; yep
	lea	name_pix,a0	; PIx
f_open:	move.l	a0,-(a7)		; filename
	move.w	#$3d,-(a7)
	trap	#1		; Fopen
	addq.l	#6,a7
	tst.l	d0
	bmi	exit1		; error
	move.w	d0,handle

* Read resolution & palette
	lea	the_pal,a0	; buffer
	moveq	#34,d0		; count (1.w+16.w)
	cmp.b	#PIx,type_pic	; PIx?
	beq.s	get_f_pal		; yep
	moveq	#36,d0		; count (2.w+16.w)
get_f_pal:bsr	f_read		; read
	ble	exit1		; error

* Get current color palette
* Note: registers D3-D7 & A3-A7 are saved by TRAP #13 & #14
	lea	save_pal+32,a6
	moveq	#15,d7
get_pal:	move.w	#-1,-(a7)		; color value (-1 gets color)
	move.w	d7,-(a7)		; color #
	move.w	#7,-(a7)
	trap	#14		; get/set color
	addq.l	#6,a7
	move.w	d0,-(a6)		; save it
	dbf	d7,get_pal

* Set palette (all black)
	lea	clr_pal,a0	; palette address (all zeroes)
	bsr	set_pal		; set palette
* Set screen to new resolution & address
	move.w	the_pal,d0	; resolution
	and.w	#3,d0		; see text
	move.l	new_scrn,a0	; change screen location
	bsr	setscreen		; set screen

	cmp.b	#PIx,type_pic	; PIx?
	beq.s	get_data		; yep

* Dummy read of NEO header...could have done an Fseek
	move.l	new_scrn,a0	; buffer
	moveq	#92,d0		; count (128-36)
	bsr	f_read		; read
	ble.s	exit1		; error

* Read screen data
get_data:	move.l	new_scrn,a0	; buffer
	move.l	#32000,d0		; count
	bsr	f_read		; read
	ble.s	exit1		; error

* Set new palette
*  Palette read at +2 for PIx & +4 for NEO
	lea	the_pal+2,a0	; palette address
	cmp.b	#PIx,type_pic	; PIx?
	beq.s	scrn_pal		; yep
	lea	the_pal+4,a0	; palette address
scrn_pal:	bsr	set_pal		; set palette

* Wait for keypress
	move.w	#7,-(a7)
	trap	#1		; Crawcin
	addq.l	#2,a7


* Set palette (all black)
	lea	clr_pal,a0	; palette address (all zeroes)
	bsr.s	set_pal		; set palette

* Restore screen resolution & address
	move.w	rez,d0		; original resolution
	move.l	old_scrn,a0	; original screen address
	bsr.s	setscreen		; set screen
* Restore palette
	lea	save_pal,a0	; original palette
	bsr.s	set_pal		; set palette

exit1:	dc.w	$a009		; show mouse cursor

* Close file handle
	tst.w	handle		; file opened?
	ble.s	exit2		; nope
	move.w	handle,-(a7)	; handle
	move.w	#$3e,-(a7)
	trap	#1		; Fclose
	addq.l	#4,a7

* Terminate
exit2:	clr.w	-(a7)
	trap	#1		; Pterm
	illegal			; won't get here?

* D0 = resolution
* A0 = new screen address
setscreen:cmp.w	#2,d0		; hi rez switch?
	beq.s	no_change		; yep
	cmp.w	#2,rez		; in hi rez?
	beq.s	no_change		; yep
	lea	the_rez,a1
	move.w	(a1),d1		; old rez
	move.w	d0,(a1)		; new rez
	cmp.w	d1,d0		; same?
	bne.s	new_rez		; nope - do rez change
no_change:moveq	#-1,d0		; -1 no change
new_rez:	move.w	d0,-(a7)		; rez (0,1,2)
	move.l	a0,-(a7)		; physical location
	move.l	a0,-(a7)		; logical location
	move.w	#5,-(a7)
	trap	#14		; setScreen
	lea	12(a7),a7
	rts

* A0 = palette address
set_pal:	move.l	a0,-(a7)		; push palette for below

	move.w	#37,-(a7)
	trap	#14		; vsync
	addq.l	#2,a7

	move.w	#6,-(a7)
	trap	#14		; set palette
	addq.l	#6,a7
	rts

* A0 = buffer address
* D0 = amount to read
f_read:	move.l	a0,-(a7)		; buffer
	move.l	d0,-(a7)		; count
	move.w	handle,-(a7)	; handle
	move.w	#$3f,-(a7)
	trap	#1		; Fread
	lea	12(a7),a7
	tst.l	d0		; test error condition for return
	rts

	DATA			; DATA segment
name_neo:	dc.b	'*.neo',0		; NEO filename
name_pix:	dc.b	'*.pi?',0		; PIx filename
type_pic:	dc.b	NEO		; picture type 0-NEO !0-PIx

	BSS			; BSS segment
rez:	ds.w	1		; original resolution value
the_rez:	ds.w	1		; resolution variable
handle:	ds.w	1		; file handle from Fopen
new_scrn:	ds.l	1		; address of allocated screen
old_scrn:	ds.l	1		; saved address of original screen
save_pal:	ds.w	16		; saved palette
clr_pal:	ds.w	16		; palette of 0s to blank screen
the_pal:	ds.w	36		; new palette read from pic file
buffer:	ds.b	32000+256		; buffer for screen
