; -----------------------------------------------------------
; geoSnap: GEOS snapshotter (original code by Paul Bosacki, 1990	)
; revised 2013, ShadowM (remove MasterAdapter patches, add autoboot)
; -----------------------------------------------------------
.if	Pass1
	.include geosSym
	.include geosMac
.endif
; -----------------------------------------------------------
Main:	lda	curDrive
	ora	#$30
	sta	bootDrv	;for message
	sta	autoDrv	;for autoboot
	LoadW	r0,prmptDlg
	jsr	DoDlgBox
	lda	r0L
	cmp	#YES
	beq	10$
	jmp	EnterDeskTop
10$	lda	#2	;50% stipple
	jsr	SetPattern
	LoadB	r2L,0
	LoadB	r2H,199
	LoadW	r3,0
	LoadW	r4,319
	jsr	Rectangle	;clear screen
	lda	#0	;clear
	jsr	SetPattern
	LoadB	r2L,72
	LoadB	r2H,127
	LoadW	r3,76
	LoadW	r4,246
	jsr	Rectangle
	LoadB	r2L,72
	LoadB	r2H,127
	LoadW	r3,76
	LoadW	r4,246
	lda	#$ff
	jsr	FrameRectangle
	LoadW	r0,writing
	LoadB	r1H,86
	LoadW	r11,90
	jsr	PutString
	LoadW	r0,sure1
	LoadB	r1H,101
	LoadW	r11,90
	jsr	PutString
	LoadW	r0,sure2
	LoadB	r1H,113
	LoadW	r11,90
	jsr	PutString
; -----------------------------------------------------------
doSnap:	LoadB	dispBufferOn,%10000000
	lda	MoveData+2
	sub	#]($bf40-$2f40)
	sta	fixJMP+2	;fix relocated address
	lda	MoveData+1
	sub	#0
	sta	fixJMP+1
	LoadB	CPU_DATA,$30
	PushB	sysRAMFlg
	LoadB	sysFlgCopy,0
	sta	sysRAMFlag
	LoadB	r0H,$bf	;from $bf40
	LoadB	r1H,$2f	;to $2f40
	LoadB	r0L,$40
	sta	r1L
	sta	r2H
	LoadB	r2L,$c0	;$40c0 bytes (to top of address space)
	jsr	MoveData
	LoadB	r0H,$90	;from $9000
	LoadB	r1H,$70	;to $7000
	LoadB	r2H,$10	;$1000 bytes
	LoadB	r0L,0
	sta	r1L
	sta	r2L
	jsr	MoveData
	PopB	sysRAMFlg
	sta	sysFlgCopy
	jsr	GetDirHead
	MoveB	curDirHead,track	;save T&S of first directory sector
	MoveB	curDirHead+1,sector
	ldy	curDrive
	lda	driveType-8,y
	sta	typeDrive+1
	LoadW	r0,ColdStart	;copy cold-start code to beginning of snapshot
	LoadW	r1,$2f40-codeLong
	LoadW	r2,codeLong
	jsr	MoveData
	LoadW	$2f3e-codeLong,$2f40-codeLong  ;prepend start address for BASIC
	LoadW	r0,geoString
	jsr	DeleteFile	;delete autoboot if it already exists
	LoadW	r0,nameString
	jsr	DeleteFile	;delete snapshot if it already exists
	LoadW	r9,autoHead
	LoadB	r10L,0
	jsr	SaveFile	;write autoboot to disk
	LoadW	r9,bootHead
	LoadB	r10L,0
	jsr	SaveFile	;write snapshot to disk
	jmp	EnterDeskTop	;done!
; -----------------------------------------------------------
; prompt dialog
; -----------------------------------------------------------
prmptDlg:	.byte	SET_DB_POS | 1
	.byte	28,171
	.word	64,255
	.byte	DBTXTSTR,64,14
	.word	title
	.byte	DBTXTSTR,12,29
	.word	text1a
	.byte	DBTXTSTR,12,41
	.word	text1b
	.byte	DBTXTSTR,12,56
	.word	text2a
	.byte	DBTXTSTR,12,68
	.word	text2b
	.byte	DBTXTSTR,12,83
	.word	text3a
	.byte	DBTXTSTR,12,95
	.word	text3b
	.byte	DBTXTSTR,12,110
	.word	text4
	.byte	DBTXTSTR,12,130
	.word	text5
	.byte	YES,9,120
	.byte	NO,16,120
	.byte	0	;end of dialog box definition
; -----------------------------------------------------------
title:	.byte	BOLDON,"- geoSnap -",PLAINTEXT,0
text1a:	.byte	"This program will create a bootable",0
text1b:	.byte	"snapshot of your GEOS 2.0 system.",0
text2a:	.byte	BOLDON,"It will overwrite any files",PLAINTEXT,0
text2b:	.byte	BOLDON,"named GEOS or TURBOBOOT.",PLAINTEXT,0
text3a:	.byte	"When the snapshot has been written,",0
text3b:	.byte	"you can boot it with the command",0
text4:	.byte	"LOAD",$22,"GEOS",$22,","
bootDrv:	.byte	$38,",1",0
text5:	.byte	BOLDON,"PROCEED?",PLAINTEXT,0
; -----------------------------------------------------------
writing:	.byte	"writing snapshot...",0
sure1:	.byte	"Make sure you put deskTop and",0
sure2:	.byte	"CONFIGURE on your boot disk.",0
; -----------------------------------------------------------
; header block for snapshot
; -----------------------------------------------------------
nameString:	.byte	"TURBOBOOT",NULL
bootHead:	.word	nameString
	.byte	3,21,63|$80
	.byte	255,255,255,128,000,001
	.byte	191,224,001,191,255,001
	.byte	144,255,125,136,018,085
	.byte	136,060,017,136,100,017
	.byte	136,188,057,132,168,001
	.byte	132,188,001,132,046,113
	.byte	136,053,185,136,015,077
	.byte	136,000,037,136,240,005
	.byte	136,254,013,143,199,217
	.byte	143,192,241,128,000,001
	.byte	255,255,255
	.byte	PRG|$80,BASIC,SEQUENTIAL
	.word	$2f3e-codeLong,$7fff,$2f40-4
	.byte	"GeoTurbo    V2.0",0,0,0,0
	.byte	"Paul J. Bosacki    ",0
	.block	43
	.byte	"GEOS snapshot (bootable)",0
; -----------------------------------------------------------
; header block for stack autoboot
; -----------------------------------------------------------
geoString:	.byte	"GEOS",NULL
autoHead:	.word	geoString
	.byte	3,21,63|$80
	.byte	$ff,$ff,$ff
	.byte	$80,$00,$01
	.byte	$80,$7e,$01
	.byte	$81,$e3,$81
	.byte	$83,$a7,$c1
	.byte	$87,$3e,$e1
	.byte	$87,$00,$e1
	.byte	$8f,$01,$f1
	.byte	$8f,$01,$f1
	.byte	$8f,$9b,$f1
	.byte	$8f,$bd,$f1
	.byte	$8f,$bf,$f1
	.byte	$8f,$8f,$f1
	.byte	$87,$07,$e1
	.byte	$87,$87,$e1
	.byte	$83,$cf,$c1
	.byte	$81,$ef,$81
	.byte	$80,$6e,$01
	.byte	$80,$00,$01
	.byte	$80,$00,$01
	.byte	$ff,$ff,$ff
	.byte	PRG|$80,BASIC,SEQUENTIAL
	.word	autoLoad,autoEnd,$0122
	.byte	"TurboBoot   V2.0",0,0,0,0
	.byte	"ShadowM            ",0
	.block	43
	.byte	"loader for bootable GEOS snapshot",0
; -----------------------------------------------------------
; cold start routine to boot GEOS from snapshot
; -----------------------------------------------------------
ColdStart:	sei
	cld
	ldx	#$ff
	txs
	LoadB	CPU_DATA,$30
	ldy	#0
	tya
5$	sta	$8400,y
	sta	$8500,y
	sta	$8600,y
	sta	$8700,y
	sta	$8800,y
	dey
	bne	5$
	LoadB	r0H,$2f
	LoadB	r1H,$bf
	LoadB	r0L,$40
	sta	r1L
	sta	r2H
	LoadB	r2L,$c0
fixJMP:	jsr	$ffff	
	LoadB	r0H,$70
	LoadB	r1H,$90
	LoadB	r2H,$10
	LoadB	r0L,0
	sta	r1L
	sta	r2L
	jsr	MoveData
GreyScreen:	LoadB	CPU_DATA,$35
	LoadW	r0,$a000
	ldx	#$7d
10$	ldy	#63
20$	lda	#$55
	sta	(r0L),y
	dey
	lda	#$aa
	sta	(r0L),y
	dey
	bpl	20$
	lda	#64
	add	r0L
	sta	r0L
	bcc	30$
	inc	r0H
30$	dex
	bne	10$
	lda	$dc0f
	and	#$7f
	sta	$dc0f
	LoadB	$dc0b,$81
	LoadB	$dc0a,0
	sta	$dc09
	sta	$dc08
	LoadB	CPU_DATA,$30
	ldx	#7
	lda	#$bb
40$	sta	$8fe8,x
	dex
	bpl	40$
	LoadB	$8ff0,$bf
	jsr	FirstInit
	jsr	InitMouse
	LoadB	interleave,8
	LoadB	day,11
	LoadB	month,06
	LoadB	year,13
	LoadB	numDrives,1
	ldy	curDevice
typeDrive:	lda	#3
	sta	curType
	sta	driveType-8,y
	sty	curDrive
	tya
	jsr	SetDevice
	jsr	NewDisk
	LoadW	r0,$2f40-bootWhere
	LoadW	r1,$5000
	LoadW	r2,bootLong
	jsr	MoveData
	jmp	o_FindExecs
BootTop:	sei
	cld
	ldx	#$ff
	txs
	jsr	$c436
	jsr	$c40d
FindAutoExecs:	MoveB	o_sector,r1H
	MoveB	o_track,r1L
	lda	o_offset
	add	#32
	sta	o_offset	;update offset for the next time
	bne	10$
60$	MoveB	o_nSector,r1H
	MoveB	o_nTrack,r1L
	bne	10$
20$	lda	#$c3	;restore EnterDeskTop jump entry
	sta	EnterDeskTop+2
	lda	#$26
	sta	EnterDeskTop+1
	jmp	EnterDeskTop
10$	MoveB	r1H,o_sector
	MoveB	r1L,o_track
	LoadW	r4,diskBlkBuf
	jsr	GetBlock
	txa 
	bne	20$
	MoveB	diskBlkBuf+1,o_nSector
	MoveB	diskBlkBuf,o_nTrack
50$	ldy	o_offset
	lda	diskBlkBuf+2,y	;is file scratched?
	beq	30$
	lda	diskBlkBuf+$18,y
	cmp	#AUTO_EXEC
	beq	40$
30$	lda	o_offset
	add	#32
	sta	o_offset
	bne	50$
	beq	60$
40$	ldx	#0
70$	lda	diskBlkBuf+2,y
	sta	dirEntryBuf,x
	iny
	inx
	cpx	#$1e
	bne	70$
	LoadW	r9,dirEntryBuf
	LoadB	r0L,0	;bug fix: r0L must .eq 0
	LoadW	EnterDeskTop+1,o_BootTop
	lda	#$c3
	pha
	lda	#$25
	pha
	lda	#$c4
	pha
	lda	#$e5
	pha
	jmp	LdApplic
; -----------------------------------------------------------
track:	.block	1
sector:	.block	1
nTrack:	.block	1
nSector:	.block	1
offset:	.block	1	
HardStart:	jmp	$2f40-codeLong
	nop
endCode:	nop
codeLong	=	endCode-ColdStart
bootLong	=	HardStart-BootTop
bootWhere	=	endCode-BootTop
o_BootTop	=	$5000
o_sector	=	sector-BootTop+$5000
o_track	=	track-BootTop+$5000
o_nSector	=	nSector-BootTop+$5000
o_nTrack	=	nTrack-BootTop+$5000
o_offset	=	offset-BootTop+$5000
o_FindExecs	=	FindAutoExecs-BootTop+$5000
; -----------------------------------------------------------
; autoboot code to load GEOS snapshot
; BASIC stub ($0110) in case loaded relocatable (i.e. without ",1")
; -----------------------------------------------------------
autoLoad:	.word	$0110	;load address
autoBoot:	.byte	$11,$08	;forward line pointer
	.byte	$0a,$00	;line number
			;LOAD "GEOS",8,1
	.byte	$93,$22,$47,$45,$4f,$53,$22,$2c
autoDrv:	.byte	$38,$2c,$31	;(or 9,1)
	.byte	$00,$00,$00	;end-of-program marker
; -----------------------------------------------------------
; stack autoboot ($0122)
; -----------------------------------------------------------
	lda	#14	;default GEOS colors
	sta	$d020
	ldy	#6
	sty	$d021
	lda	#0
	jsr	$ff90	;SETMSG
	lda	#9
	ldx	#[((snapFile-autoBoot)+$0110)
	ldy	#]((snapFile-autoBoot)+$0110)
	jsr	$ffbd	;SETNAM
	lda	#50
	ldy	#1
	ldx	$ba
	jsr	$ffba	;SETLFS
	lda	#0
	ldx	#$ff
	ldy	#$ff
	jsr	$ffd5	;LOAD
	bcc	10$
	jmp	($0302)
10$	jmp	$2f3c	;cold-start GEOS snapshot
snapFile:	.byte	"TURBOBOOT"
	.byte	$02,$02,$02	;starting at $015d
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02,$02,$02,$02,$02,$02
	.byte	$02,$02,$02	;ending at $0202
	jmp	$0122
autoEnd:	.byte	0
