#
# Amstrad CPC overscan mode 0 (lowres bitmap) exporter
# A Multipaint internal script
#
# The line order and padding byte amounts are weird so this is rather horrible
#
# Based on CPC formats in Pixel Polizei by MarQ
#

XREM "CPC machine code header"

XBYTE $3E,$00,$CD,$0E,$BC,$CD,$19,$BD,$21,$60,$01,$CD,$3B,$01,$CD,$19
XBYTE $BD,$21,$70,$01,$11,$BE,$BC,$AF,$42,$ED,$79,$43,$ED,$A3,$3C,$FE
XBYTE $0E,$20,$F5,$CD,$19,$BD,$3A,$5F,$01,$47,$4F,$CD,$38,$BC,$CD,$19
XBYTE $BD,$21,$4F,$01,$CD,$3B,$01,$CD,$18,$BB,$C9,$06,$10,$3E,$00,$4E
XBYTE $23,$E5,$C5,$F5,$41,$CD,$32,$BC,$F1,$C1,$E1,$3C,$10,$F1,$C9

XREM "Palette order"

A=256
FOR I=0 TO 15
	R = PEEK A
	A = A+1
	G = PEEK A
	A = A+1
	B = PEEK A
	A = A+1

	H = SCALERGB G,3
	L = SCALERGB R,3
	M = SCALERGB B,3

	O = M
	O = O+L*3
	O = O+H*9

	XBYTE O
NEXT I

XREM "Border"

A = PEEK 0
XBYTE A

XREM "Code and padding"

XBYTE $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
XBYTE $3F,$30,$32,$89,$26,$00,$22,$23,$00,$07,$00,$00,$0D,$00,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
XBYTE $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF

XREM "Bitmap"

U = 256
R = -8
Q = -1

S = 8
T = -119
V = 15

FOR Y=0 TO 271

	IF Y=120
		T=1
	ENDIF
	IF Y=140
		T=-135
	ENDIF

	IF Q<V
		R=R+S
	ENDIF
	IF Q=V
		R=R+T
		Q=-1
		IF T=1
			V=17
		ENDIF
	ENDIF

    Q = Q+1

	FOR X=0 TO 95
		L = X*4
		Z = GETPOINT L,R

		A = 0
		B = 0
		C = 0
		D = 0
		E = 0
		F = 0
		G = 0
		H = 0

		M = Z&1
		IF M<>0
			H=1
		ENDIF

		M = Z&2
		IF M<>0
			D=1
		ENDIF

		M = Z&4
		IF M<>0
			F=1
		ENDIF

		M = Z&8
		IF M<>0
			B=1
		ENDIF

		L = L+2

		Z = GETPOINT L,R

		M = Z&1
		IF M<>0
			G=1
		ENDIF

		M = Z&2
		IF M<>0
			C=1
		ENDIF

		M = Z&4
		IF M<>0
			E=1
		ENDIF

		M = Z&8
		IF M<>0
			A=1
		ENDIF		

		O = A
		O = O+B*2
		O = O+C*4
		O = O+D*8
		O = O+E*16
		O = O+F*32
		O = O+G*64
		O = O+H*128

		XBYTE O

		U = U+1

		IF U = 1792
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 3840
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 5888
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 7936
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 9984
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 12032
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 14080
			XREPEAT $CF,512
			U = U+512
		ENDIF
		IF U = 17856
			XREPEAT $BE,320
			U = U+320
		ENDIF
		IF U = 19904
			XREPEAT $BE,320
			U = U+320
		ENDIF
		IF U = 21952
			XREPEAT $BE,320
			U = U+320
		ENDIF
		IF U = 24000
			XREPEAT $BE,320
			U = U+320
		ENDIF
		IF U = 26048
			XREPEAT $BE,320
			U = U+320
		ENDIF
		IF U = 28096
			XREPEAT $BE,320
			U = U+320
		ENDIF
		IF U = 30144
			XREPEAT $BE,320
			U = U+320
		ENDIF

	NEXT X

NEXT Y

# Write to file

L = 32192

SAVE
