	device pentagon1024

STACK=#6000;#be00;#5fe0;#be00
IMVEC=#aeff;#3bff
;IMVEC=#fe00;#be00
;IMER=#fdfd;#bfbf

pusher=#6000

scrdatahgt=192

	macro ddemacro
__=__+#100
	if (__&#700)==0
__=__+#20-#800
	if (__&#e0)==0
__=__+#700
	endif
	endif
	endm
	

	org #6100

begin
	xor a
	;ld a,1
	out (#fe),a
	ld hl,#4000
	ld de,#4001
	ld bc,#17ff
	ld (hl),l
	ldir
	ld hl,#5800
	ld de,#5801
	ld bc,#2ff
	ld (hl),0
	ldir


	;ld (#5d10),a
	ld (iy+1),#cc
	ld sp,STACK

	ld hl,scrdata
	ld de,#4000
	;ld bc,#1800
	;ldir
	call prsprscr
	ld bc,#7ffd
	ld a,#17
	out (c),a
	ld de,#c000
	;ld bc,#1800
	;ldir
	call prsprscr
	;ld bc,#7ffd
	;ld a,#10
	;out (c),a
	

	;if IMVEC==#3bff
       ;LD hl,#c9fb
       ;LD (#fff4),hl
	   ld a,#c3
	   ld (#fff4),a
       ld hl,ON_INT
       ld (#fff5),hl
	   ld a,#18
	   ld (#ffff),a
	;else
       ;ld hl,ON_INT;imer
       ;ld (IMVEC|255),hl
	;endif
	;ld a,IMVEC/256
	ld a,#3b
    ld i,a
    im 2
    ei 

	
	jp maingo

prsprscr
	ld b,192
prsprscr0
	push bc
	push de
	ld bc,32
	ldir
	pop de
	pop bc
	call dde
	djnz prsprscr0
	ret
	
dde
	inc d
	ld a,d
	and 7
	ret nz
	ld a,e
	add a,32
	ld e,a
	ret c
	ld a,d
	sub 8
	ld d,a
	ret



;=====================================
	ds #8000-$
	org #8000
maingo
	;jp mainTK90X
	
	ei
	halt
	
	ld hl,(timer)
	push hl
	ld h,0
	ld d,h
	ld bc,3400
	ldir
	pop de
	ld hl,(timer)
	or a
	sbc hl,de
	jr z,mainpent

	ld hl,(timer)
	push hl
	ld h,0
	ld d,h
	ld bc,3300
	ldir
	pop de
	ld hl,(timer)
	or a
	sbc hl,de
	;nz=48K, z=128K
	jr nz,main48k

	if 1==0 ;can't swap pages with I=#3B
	ld bc,#7ffd
	ld h,c,l,c
	ld de,#1011
	out (c),d
	ld (hl),c ;#fd
	out (c),e
	dec (hl)
	ld a,(hl)
	out (c),d
	cp (hl)
	;z=48K, nz=128K
	jr z,mainTK90X
	endif
main128
	 ;ld a,1
	 ;out (#fe),a
    ;   ld hl,imer128;ON_INT
    ;   ld (IMVEC|255),hl
	ld hl,274-202
	ld (mcwait),hl
	ld a,mclooppatch128
	ld (mclooppatch),a
	jr main48k
mainTK90X
	;NYI
	 ld a,3
	 out (#fe),a
	jr main48k
mainpent
	 ;ld a,2
	 ;out (#fe),a

main48k

;0: G/M/C - низ(нечет)
;1: R/C/M - низ(нечет)
;2: B/Y - верх(чёт)
;3: W - верх(чёт)

;frame1: W (оптимизируется), G/M/C
;frame2: B/Y (оптимизируется через push:push:... (4 rp)), R/C/M
;т.е. можно все записи атрибутов в верхнем бордере:
;768*10.5 + 768*5.5 = 12288 (55 строк растра)
	ld hl,pusher
	ld de,scrdata+6144+6144+(0*768)+767 ;G/M/C - низ(нечет)
	;ld de,scrdatazero+767
	ld bc,#5b00
	ld (push0),hl
	call mkldpusher ;низ(нечет) первого фрейма
	ld de,scrdata+6144+6144+(3*768)+767 ;W - верх(чёт)
	;ld de,scrdatazero+767
	ld bc,#db00
	;ld (push3),hl
	ld ix,#4747
	call mkpusher ;верх(чёт) первого фрейма
	
	ld de,scrdata+6144+6144+(1*768)+767 ;R/C/M - низ(нечет)
	;ld de,scrdatazero+767
	ld bc,#db00
	ld (push1),hl
	call mkldpusher ;низ(нечет) второго фрейма
	ld de,scrdata+6144+6144+(2*768)+767 ;B/Y - верх(чёт)
	;ld de,scrdatazero+767
	ld bc,#5b00
	;ld (push2),hl
	ld ix,#4641
	call mkpusher ;верх(чёт) второго фрейма
	
	;ld hl,wasmc
	;ld de,mc
	;ld bc,szmc
	;ldir
	
	;ld bc,#7ffd
	;ld a,#10
	;out (c),a
	;ei
	;halt
	;ld a,IMVEC/256
	;ld i,a
	;ld bc,1500
	;dec bc
	;ld a,b
	;or c
	;jr nz,$-3 ;пауза, чтобы не было видно атрибуты до показа мультиколора
	;im 2
	
	ld (scriptsp),sp
scrollloop

	;ld bc,#7ffd
	;ld a,#10
	;out (c),a
	ei
	halt
	;ld bc,#7ffd
	;ld a,#17
	;out (c),a
	ld ix,push0q
push0=$+1
	jp 0
push0q
	;ld ix,push3q
;push3=$+1
	;jp 0
;push3q
	ld sp,(scriptsp)
	ld a,#17
	call mc
	ld sp,(scriptsp)

	;ld bc,#7ffd
	;ld a,#10
	;out (c),a
	ei
	halt
	;ld bc,#7ffd
	;ld a,#17
	;out (c),a
	ld ix,push1q
push1=$+1
	jp 0
push1q
	;ld ix,push2q
;push2=$+1
	;jp 0
;push2q
	ld sp,(scriptsp)
	ld a,#1f
	call mc
scriptsp=$+1
	ld sp,0

	jp scrollloop

mc
wasmc
	disp mc
;mc
	;ld a,d
mcwait=$+1
	ld bc,446-202;446;pentagon
	;ld bc,274-202;128K
	ld d,b
	ld h,b
	ldir
	ld bc,#7ffd
	ld h,192
mcloop128
	nop
mcloop
	nop
	ld l,0
	ld l,11
	dec l
	jr nz,$-1
	xor 8
	out (c),a
	dec h
mclooppatch=$+1
	jr nz,mcloop
mclooppatch128=mcloop128-$
;delay before any other screen change
;(if any)
	;ld l,11
	;dec l
	;jr nz,$-1
;
	;ld a,3
	;out (#fe),a
	ret
	ent
szmc=$-wasmc

mkpusher
;ix=два разрешённых значения
	ld (hl),#31
	inc hl
	ld (hl),c
	inc hl
	ld (hl),b
	inc hl
	ld b,hx
	ld a,lx
	ld (hl),#01
	inc hl
	ld (hl),b
	inc hl
	ld (hl),b
	inc hl
	ld (hl),#11
	inc hl
	ld (hl),a;b
	inc hl
	ld (hl),b;a
	inc hl
	ld (hl),#21
	inc hl
	ld (hl),a
	inc hl
	ld (hl),a
	inc hl
	ld (hl),#e5 ;push hl
	inc hl
	ld (hl),#f1 ;pop af
	inc hl
	ld (hl),#21
	inc hl
	ld (hl),b;a
	inc hl
	ld (hl),a;b
	inc hl
	ld b,24;scrdatahgt
mkpusher0
	push de
	ld c,#20/2
mkpusher1
	ld a,(de)
	dec de
	xor lx
	sub 1
	sbc a,a
	and #20
	ld (hl),a
	ld a,(de)
	dec de
	xor lx
	sub 1
	sbc a,a
	and #10
	or (hl)
	;a=#00/#10/#20/#30
	add a,#c5
	ld (hl),a ;push bc/de/hl/af
	inc hl
	dec c
	jr nz,mkpusher1
	pop de
	call udeattr
	djnz mkpusher0
	ld (hl),#dd ;ix
	inc hl
	ld (hl),#e9 ;jp (hl)
	inc hl
	ret

mkldpusher
	ld (hl),#31
	inc hl
	ld (hl),c
	inc hl
	ld (hl),b
	inc hl
	ld b,24;scrdatahgt
mkldpusher0
	push de
	ld c,#20/2
mkldpusher1
	ld (hl),#11 ;ld de
	inc hl
	dec de
	ld a,(de)
	inc de
	ld (hl),a
	inc hl
	ld a,(de)
	ld (hl),a
	inc hl
	dec de
	dec de
	ld (hl),#d5 ;push de
	inc hl
	dec c
	jr nz,mkldpusher1
	pop de
	call udeattr
	djnz mkldpusher0
	;ld (hl),#dd ;ix
	;inc hl
	;ld (hl),#e9 ;jp (hl)
	;inc hl
	ret
	
udeattr
	ld a,e
	sub 32
	ld e,a
	ret nc
	dec d
	ret

	
	;if IMVEC!=#3bff
ON_INT
	push af,bc,hl
	ld hl,(timer)
	inc hl
	ld (timer),hl
	;push de
	;push ix,iy
	;ld a,(curpg)
	;push af
	;ld a,pgmuz
	;call OUTA
	;;call muz+5
	;pop af
	;call OUTA
	;pop iy,ix
	;pop de
	pop hl,bc,af
	ei
	ret
	;endif
timer
	dw 0

scrdata
	;incbin "BABYLONA.rm"
	;incbin "0912.rm"
	;incbin "crazykids.rm"
	incbin "p7.rm"
	;incbin "p2.rm"
scrdatazero
	ds 768
end

	;display "pushsize=",pushsize ;#41
	;display "mcsize=",mcsize
	display "Size ",/d,end-begin," bytes"
	
	savebin "code.c",begin,end-begin
	
	LABELSLIST "..\us\user.l"
