#
# ZX Next NEX exporter 320x256
# A Multipaint internal script
#
# The image is shown as loading screen, the code just waits a keypress
#
# https://wiki.specnext.dev/NEX_file_format
#

# 512-byte header

XBYTE "Next"
XBYTE "V1.1"

# RAM req'd

XBYTE 0

# No of banks

XBYTE 6

# Loading-screen blocks = Layer2 w/ palt

XBYTE 128

# Border color

XBYTE 0

# SP = $0060

XBYTE $00,$60

# PC = $8000

XBYTE $00,$80

# no of extra files

XWORD 0

# byte flag of included 16K banks
# in real order
# The 16K contents at end are included in order
    # 5,2,0,1,3,4,6,7,8,9...

XBYTE 0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0
XBYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
XBYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
XBYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
XBYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
XBYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
XBYTE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

# loading bar?

XBYTE 0

# loading bar color

XBYTE 0

# loading delay per bank

XBYTE 0

# start delay frames

XBYTE 0

# preserve Next regs

XBYTE 0

# Req'd core v.

XBYTE 0,0,0

# Timex

XBYTE 0

# Entry bank to map into slot 3 (0xc000-)

XBYTE 0

# File handle address

XWORD 0

# padding up to 511

FOR I=0 TO 369
	XBYTE 0
NEXT I

# BANKS

# Code
# at $8000 = 16K bank no.2

#border, figure 8-bit RGB from 24-bit

A=PEEK 0
I=256+A*3
R=PEEK I
I=I+1
G=PEEK I
I=I+1
B=PEEK I

R=R/36
G=G/36
B=B/36

L=R*32
S=G*4
L=L|S

S=B&6
S=S/2
L=L|S

XBYTE  $f3, $ed, $91, $07, $02, $ed, $91, $1c, $01, $ed, $91, $43,
XBYTE  $11, $ed, $91, $14
XBYTE  L
XBYTE  $ed, $91, $42, $ff, $ed, $91, $4a,
XBYTE  L
XBYTE  $ed, $91, $16, $00, $ed, $91, $17, $00, $ed, $91, $18,
XBYTE  $00, $ed, $91, $18, $9f, $ed, $91, $18, $00, $ed, $91, $18,
XBYTE  $ff, $3e, $03, $01, $3b, $12, $ed, $79, $ed, $91, $70, $10,
XBYTE  $ed, $91, $40, $00, $06, $ff, $21, $99, $80, $7e, $ed, $92,
XBYTE  $44, $23, $7e, $ed, $92, $44, $23, $10, $f4, $7e, $ed, $92,
XBYTE  $44, $23, $7e, $ed, $92, $44, $16, $00, $3e, $fe, $db, $fe,
XBYTE  $cb, $47, $20, $02, $cb, $c2, $3e, $7f, $db, $fe, $cb, $47,
XBYTE  $20, $02, $cb, $ca, $7a, $d6, $03, $20, $e5, $ed, $91, $02,
XBYTE  $01, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
XBYTE  $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00,
XBYTE  $00, $00, $00, $00, $00, $00, $00, $00, $00









XREM "9-bit layer2 palette"

# RRRGGGBB .......B

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

	R=R/36
	G=G/36
	B=B/36

	L=R*32
	S=G*4
	L=L|S

	S=B&6
	S=S/2
	L=L|S

	H=B&1

	XBYTE L
	XBYTE H

NEXT I

# Length of above code + palt length

L=153

L=L+512
P=16384
P=P-L
P=P-1

# Padding this bank to 16384 

FOR Y=0 TO P
	XBYTE 0
NEXT Y

# Image data in subsequent banks (3 x 16384)

# size

H=PEEK 5
V=PEEK 7
H=H*8
V=V*8
H=H-1
V=V-1

FOR X=0 TO H
	FOR Y=0 TO V
		P=GETPOINT X,Y
		XBYTE P
	NEXT Y
NEXT X

# Write to file (bytes)

SAVE

