#
# Amstrad CPC exporter
# Bakes a .dsk image for CPC emulators
#

#
# assumes variable L contains file length
#
# 16486 for normal
# 32192 for overscans
#

# at 00-21

XBYTE "MV - CPCEMU Disk-File"
XBYTE $0D,$0A
XBYTE "Disk-Info"
XBYTE $0D,$00

# name of creator at 22-2f

XBYTE "Multipaint"
XBYTE $0D,$0A
XBYTE $00,$00

# no of tracks at 30 = 42

XBYTE $2A

# no of sides at 31

XBYTE $01

# size of track (little endian) at 32-33

XBYTE $00
XBYTE $13

# unused

XBYTE $FE
XBYTE $7F

# unused at 34-ff

XREPEAT $00,202

# contents
# 42 sectors

FOR I=0 TO 41

	# track info
	
	XBYTE "Track-Info"
	XBYTE $0D,$0A
	
	XBYTE $00,$00,$00,$00

	# track no

	XBYTE I

	# side

	XBYTE 0

	XBYTE $00,$00

	# sector size

	XBYTE $02

	# no of sectors

	XBYTE $09

	# GAP3 length

	XBYTE $4E

	# filler byte

	XBYTE $E5

	# sector information list

	XBYTE I,$00,$C1,$02,$00,$00,$00,$02
	XBYTE I,$00,$C6,$02,$00,$00,$00,$02
	XBYTE I,$00,$C2,$02,$00,$00,$00,$02
	XBYTE I,$00,$C7,$02,$00,$00,$00,$02
	XBYTE I,$00,$C3,$02,$00,$00,$00,$02
	XBYTE I,$00,$C8,$02,$00,$00,$00,$02
	XBYTE I,$00,$C4,$02,$00,$00,$00,$02
	XBYTE I,$00,$C9,$02,$00,$00,$00,$02
	XBYTE I,$00,$C5,$02,$00,$00,$00,$02

	# filler until 256 bytes

	XREPEAT $00,160

	# disk data

	IF I=0
		
		XBYTE $00
		XBYTE "CPCFILL BIN"
		XBYTE $00,$00,$00,$80,$02,$03,$04,$05
		XBYTE $06,$07,$08,$09,$0A,$0B,$0C,$0D
		XBYTE $0E,$0F,$10,$11
		XBYTE $00
		XBYTE "CPCFILL BIN"
		XBYTE $01,$00,$00

		IF L=16486
			XBYTE $02,$12
			XREPEAT $00,15
		ENDIF

		IF L=32192
			XBYTE $7D,$12,$13,$14,$15,$16,$17,$18
			XBYTE $19,$1A,$1B,$1C,$1D,$1E,$1F,$20
			XBYTE $21			
		ENDIF

		# 64 bytes so far

		XREPEAT $E5,448

		# file contents in sector order

		RESTORE 384
		XMOVE 512

		XREPEAT $E5,512

		RESTORE 896
		XMOVE 512

		XREPEAT $E5,512

		RESTORE 1408
		XMOVE 512

		XREPEAT $E5,512

		RESTORE 1920
		XMOVE 512

		#128 chunk

		XBYTE $00
		XBYTE "CPCFILL BIN"	
		XBYTE $00,$00,$00,$00,$00,$00
		
		XBYTE $02
		XBYTE $00,$00
		
		# start ad

		IF L=16486
			XBYTE $00
			XBYTE $60
		ENDIF
		IF L=32192
			XBYTE $00
			XBYTE $01
		ENDIF

		XBYTE $00
		
		# length

		XWORDR L

		# stard ad again

		IF L=16486
			XBYTE $00
			XBYTE $60
		ENDIF
		IF L=32192
			XBYTE $00
			XBYTE $01
		ENDIF
		
		XBYTE $00,$00,$00,$00,$00
		XREPEAT $00,31

		# length again

		XWORDR L

		XBYTE $00

		IF L=16486
			XBYTE $04,$05
		ENDIF
		IF L=32192
			XBYTE $74,$05
		ENDIF

		XREPEAT $00,59

		#file code starts here

		#3x128

		RESTORE 0
		XMOVE 128
		RESTORE 128
		XMOVE 128
		RESTORE 256
		XMOVE 128

	ENDIF

	IF I=1
		RESTORE 2432
		XMOVE 512

		RESTORE 4992
		XMOVE 512

		RESTORE 2944
		XMOVE 512

		RESTORE 5504
		XMOVE 512

		RESTORE 3456
		XMOVE 512

		RESTORE 6016
		XMOVE 512

		RESTORE 3968
		XMOVE 512

		RESTORE 6528
		XMOVE 512

		RESTORE 4480
		XMOVE 512

	ENDIF
	
	IF I=2	

		RESTORE 7040
		XMOVE 512

		RESTORE 9600
		XMOVE 512

		RESTORE 7552
		XMOVE 512

		RESTORE 10112
		XMOVE 512

		RESTORE 8064
		XMOVE 512

		RESTORE 10624
		XMOVE 512

		RESTORE 8576
		XMOVE 512

		RESTORE 11136
		XMOVE 512

		RESTORE 9088
		XMOVE 512

	ENDIF

	IF I=3

		RESTORE 11648
		XMOVE 512

		RESTORE 14208
		XMOVE 512

		RESTORE 12160
		XMOVE 512

		RESTORE 14720
		XMOVE 512

		RESTORE 12672
		XMOVE 512

		RESTORE 15232
		XMOVE 512

		RESTORE 13184
		XMOVE 512

		RESTORE 15744
		XMOVE 512

		RESTORE 13696
		XMOVE 512

	ENDIF

	IF L=16486

		IF I=4	
			RESTORE 16256
			XMOVE 128

			RESTORE 16384
			XMOVE 128

			XREPEAT $E5,128
			XREPEAT $E5,128

			FOR S=0 TO 7
				XREPEAT $E5,512
			NEXT S
		ENDIF

		IF I>4
			FOR S=0 TO 8
				XREPEAT $E5,512
			NEXT S
		ENDIF

	ENDIF

	IF L=32192

		IF I=4

			RESTORE 16256
			XMOVE 512

			RESTORE 18816
			XMOVE 512

			RESTORE 16768
			XMOVE 512

			RESTORE 19328
			XMOVE 512

			RESTORE 17280
			XMOVE 512

			RESTORE 19840
			XMOVE 512

			RESTORE 17792
			XMOVE 512

			RESTORE 20352
			XMOVE 512

			RESTORE 18304
			XMOVE 512
		ENDIF

		IF I=5

			RESTORE 20864
			XMOVE 512

			RESTORE 23424
			XMOVE 512

			RESTORE 21376
			XMOVE 512

			RESTORE 23936
			XMOVE 512

			RESTORE 21888
			XMOVE 512

			RESTORE 24448
			XMOVE 512

			RESTORE 22400
			XMOVE 512

			RESTORE 24960
			XMOVE 512

			RESTORE 22912
			XMOVE 512
		ENDIF

		IF I=6
			RESTORE 25472
			XMOVE 512

			RESTORE 28032
			XMOVE 512

			RESTORE 25984
			XMOVE 512

			RESTORE 28544
			XMOVE 512

			RESTORE 26496
			XMOVE 512

			RESTORE 29056
			XMOVE 512

			RESTORE 27008
			XMOVE 512

			RESTORE 29568
			XMOVE 512

			RESTORE 27520
			XMOVE 512

		ENDIF

		IF I=7

			RESTORE 30080
			XMOVE 512

			XREPEAT $E5,512

			RESTORE 30592
			XMOVE 512

			XREPEAT $E5,512

			RESTORE 31104
			XMOVE 512

			XREPEAT $E5,512

			RESTORE 31616
			XMOVE 512

			XREPEAT $E5,512	

			RESTORE 32128
			XMOVE 128		
							
			XREPEAT $E5,384

		ENDIF

		IF I>7
			FOR S=0 TO 8
				XREPEAT $E5,512
			NEXT S
		ENDIF		
	ENDIF

NEXT I

SAVE














