debug=0

IMVEC=#8000;#be00
IMSTACK=#8140;#bf40
ON_INT=#8181;#bfbf
STACK=ON_INT

SHOWFPS=0

basetime=192
endtime=7450;(5*192)

timer=23728

        include "cube/cube_h.asm"


	device pentagon1024

	org #6200
begin
	xor a
	out (#fe),a
	;ld (#5d10),a
	;ld (iy+1),#cc
	;ld sp,STACK

	ld hl,module
	call pt3player.INIT

	;ld hl,wasimer
	;ld de,ON_INT
	;ld bc,szimer
	;ldir
	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
	ld a,IMVEC/256
	else
           ;ld hl,ON_INT
           ;ld (IMVEC+255),hl
	ld hl,IMVEC
	ld a,h
	ld d,ON_INT/256
	ld (hl),d
	inc l
	jr nz,$-2
	inc h
	ld (hl),d
	endif
	ld i,a

;init

	ld hl,#5800
	ld de,#5801
	ld (hl),7;#38;#3e+bright;7
	ld bc,767
	ldir ;зачерняем, чтобы не было кривых атрибутов

	ld hl,#4000
	ld de,#4001
        ld bc,#17ff
        ld (hl),l;#ff
        ldir
        
        ld hl,0x4460
        ld bc,0x20ff
        ld (hl),c
        inc l
        djnz $-2
        ld hl,0x5480
        ld bc,0x20ff
        ld (hl),c
        inc l
        djnz $-2
        ld hl,0x5860
        ld bc,0x2002
        ld (hl),c
        inc l
        djnz $-2
        ld hl,0x5a80
        ld bc,0x2002
        ld (hl),c
        inc l
        djnz $-2
        
	halt

        call mkchunk

        call cube_init
        
        call moveall
        
	im 2
	;ei 
	jp maingo
        
message
        db 3
        db "Welcome to the future...",13
        db 6;2+16
        db "        We have survived.......",13
        db 2
        db "     We became stronger........"
        db 11 ;accent
        db 12,30,13
        db 4
        db "We know what to do",13
        db "         We know how to do",13
        db "                  We are doing",12,95,13
        
        db 13
        db 13
        db 13
        db 13
        db 13
        db 13
        
messageloop
        db 6
        db "Code by Alone Coder/Conscience",13
        db 7
        db "Font by DamienG",13
        db 3
        db "  https://damieng.com/zx-origins";13
        db 6
        db "Music by Moran/CPU",13
        db 4
        db "Texture from a painting by",13
        db 7
        db "            Borja Fresco Costal",12,44,13

        db 13
        db 13
        db 13
        db 13
        db 13
        db 13
        
        db 7+16
        db "         Info Guide #13",13
        db 7
        db "    Russian scene diskzine",13
        db 3
        db "       issued since 1998",13
        db 6
        db "      Previously known as",13
        db 4
        db "ZX-Guide, Inferno, Inferno Guide";13
        db 1+16
        db "        and Sportloto :)",12,57,13

        db 13
        db 13
        db 13
        db 13
        db 13
        db 13

        db 7
        db "Thanks to all who support",13
        db 7+16
        db "     the ZX Spectrum scene!",13,13
        db 6
        db "Special thanks to",13
        db 6+16
        db "Your Game authors and sponsors",13
        db 7+16
        db "and NedoOS contributors!",12,55,13

        db 13
        db 13
        db 13
        db 13
        db 13
        db 13
        
        db 7
        db "Visit our websites",13
        db 3
        db "http://nedoos.ru/",13
        db "http://alonecoder.nedopc.com/",13
        db 4
        db "And our YouTube channel",13
        db 4+16
        db "named 'Alone Coder' -",13
        db 7+16
        db "the place to learn assembly :)",12,55,13

        db 13
        db 13
        db 13
        db 13
        db 13
        db 13

        db 7
        db "ZX Spectrum is the machine",13
        db "    where anyone can cook :)",13
        db 13
        db 4
        db "Unite and make masterpieces!",13
        db 13
        db 7+16
        db "      Don't code alone :)",12,55,13
        
        db 13
        db 13
        db 13
        db 13
        db 13
        db 13

        db 7
        db "If you don't see colours here,",13
        db 7+16
        db "     use Pentagon timings!",13,13
        
        db 4
        db "However this intro supports",13
        db 4+16
        db "       many ZX Spectrum models.",13
        db 6+16
        db "    (Try to run in 48K mode)",12,55,13
        
        db 13
        db 13
        db 13
        db 13
        db 13
        db 13
        
        db 0

type_play
        call pt3player.PLAY
        ld a,64
        add a,0
        ld ($-1),a
        ret nc
curmsgwait=$+1
        ld a,0
        or a
        jr z,type
        dec a
        ld (curmsgwait),a
;todo blink cursor
        and 4
        add a,255
        sbc a,a
        ld c,a
        ld a,(curmsgscr)
        ld e,a
        rla
        sbc a,a
        and 16
        add a,0x40+7
        ld d,a
        ld a,c
        ld (de),a
        inc d
        call prchrcolor
        ret
type
curmsgaddr=$+1
        ld hl,message
        ld a,(hl)
        or a
        jr nz,$+5
        ld hl,messageloop
        ld a,(hl)
        inc hl
        ld (curmsgaddr),hl
curmsgscr=$+1
        ld e,0;0x20
       cp 11
       jr z,type_accent
       cp 12
       jr z,type_pause
       cp 13
       jr z,type_cr
       jr c,type_color
       cp 32
       jr nc,prchr
        add a,64-16
type_color
        ld (curmsgcolor),a
        ret
type_pause
        ld a,(hl)
        inc hl
        ld (curmsgaddr),hl
        ld (curmsgwait),a
        ret
type_accent
        ld a,e
        and 0xe0
        ld e,a
        rla
        sbc a,a
        and 2
        add a,0x58
        ld d,a
        ld b,32
type_accent0
        ld a,(de)
        or 0x40
        ld (de),a
        inc e
        djnz type_accent0
        ret
type_cr
;clear to the end of line
        ld a,e
       ;push af
        rla
        sbc a,a
        and 16
        add a,0x40
        ld c,a
type_cr0
        ld d,c
        xor a
        dup 8
        ld (de),a
        inc d
        edup
        org $-1
        inc e
        ld a,e
        and 0x1f
        jr nz,type_cr0
       ;pop af
        ;and 0xe0
        ;add a,32
       ld a,e
       cp 0x60
       jr nz,$+4
       add a,0x40
        ld (curmsgscr),a
        ret
;type_color
;        ld (curmsgcolor),a
;        ret
prchr
       cp 32
       jr nz,prchr_nospace
       call prchr_nospace
       jr type
prchr_nospace
        add a,a
        ld l,a
        ld h,0
        add hl,hl
        add hl,hl
        ld bc,font
        add hl,bc
        ld a,e
        rla
        sbc a,a
        and 16
        add a,0x40
        ld d,a
       push de
        ld b,8
prchr0
        ld a,(hl)
        ld (de),a
        inc l
        inc d
        djnz prchr0
        call prchrcolor
       pop de
        ld a,e
        inc a
        ;xor e
        ;and 0x1f
        ;xor e
        ;ld e,a
       cp 0x60
       jr nz,$+4
       add a,0x40
        ld (curmsgscr),a
        ret
prchrcolor
        ld a,d ;0x48/58
        rrca   ;0x24/2c
        rrca   ;0x12/14
        rrca   ;0x09/0a
        add a,0x58-0x09
        ld d,a
curmsgcolor=$+1
        ld a,6
        ld (de),a
        ret
        
        align 8;256
font=$-256
        incbin "Trouble.ch8"
        
wastexture
        ;incbin "cube/clown.raw"
        ;incbin "cyberpunk.raw"
        
	display "free before fastmem=",tablestart-$;ON_INT-$
;=====================================
	ds ON_INT-$
	;org #8000
wasimer
	disp ON_INT
	ld (imstack),sp
        ex (sp),hl
        ld (imjp),hl
	ld sp,IMSTACK
	push af
	push bc
	push de
	push hl
	
        exx
        push bc
        push de
        push hl
        
	push ix
	ex af,af'
	push af
	push iy

	ld hl,(timer)
	inc hl
	ld (timer),hl
        ;xor a:xor a
        bit 0,l
intcall1=$+1
        call z,reter;multicolor
intcall2=$+1
	call reter;pt3player.PLAY
        
timersec=$+1
	ld a,50
	dec a
	jr nz,timernsec
       if SHOWFPS
	call showfps
       endif
	ld a,50
timernsec
	ld (timersec),a

	pop iy
	pop af
	ex af,af'
	pop ix

        pop hl
        pop de
        pop bc
        exx
        
	pop hl
	pop de
	pop bc
	pop af
imstack=$+1
	ld sp,0
        pop hl
	ei
imjp=$+1
	jp 0
;timer
	;dw 0
	ent
szimer=$-wasimer



	include "pt3play.asm"
module
	incbin "ALERT.pt3"

maingo

	ei
	halt
	
	ld hl,(timer)
	push hl
	ld h,0
	ld d,h
	ld bc,3400-20 ;сложный on_int
	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-20 ;сложный on_int
	ldir
	pop de
	ld hl,(timer)
	or a
	sbc hl,de
	;nz=48K, z=128K
	jr nz,main48k

	ld bc,#7ffd
	ld h,c
        ld 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=TK90X, nz=128K
	jr nz,main128
mainTK90X
	 ;ld a,3
	 ;out (#fe),a
	ld hl,689 +12;295-1 +8 +4 -78
	ld (waitsplus2),hl
	;ld hl,multicolorplus2
	;ld (mkmcdata),hl
	;jr main128kq
main128
	 ;ld a,1
	 ;out (#fe),a
	;ld hl,689
	;ld (waitsplus2),hl
;main128q
	ld hl,multicolorplus2
	;ld (mkmcdata),hl
	jr main48kq
main48k
	;ld bc,#7ffd
	;ld h,c
        ;ld 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=ATM (Harlequin 128K - fail!)
	ei
	halt
	ld hl,(timer)
	push hl
	ld h,#40
	ld d,h
        ld e,l
	ld bc,3300-20 ;сложный on_int
	ldir
	pop de
	ld hl,(timer)
	or a
	sbc hl,de
	;nz=48K, z=ATM
        ld hl,multicolor48
	jr nz,main48kq
	ld hl,830-138  ;ATM
	ld (pentwaits),hl
	;jr mainpenthl
mainpent
	 ;ld a,2
	 ;out (#fe),a
	;ld hl,837 ;PENTAGON
	;ld (pentwaits),hl
mainpenthl
	ld hl,multicolorpent
	;ld (mkmcdata),hl

main48kq
;mkmcdata=$+1
        ;ld hl,multicolor48

       push hl
        if 0
        ld hl,0x5800
        ld de,0x5801
        ld (hl),l
        ld bc,0x2ff
        ldir
        ld hl,0x4000
        ld de,0x4001
        ld bc,0x1800
        ld (hl),l
        ldir
        ld (hl),7
        ld bc,0x2ff
        ldir
        endif
       pop hl

        ld (intcall1),hl
        ld hl,type_play ;pt3player.PLAY
        ld (intcall2),hl

	ld hl,(timer)
	ld (oldtimer),hl
	;jp mainloop
mainloop
        ;halt
        
        call drawcube
        
        call c2p
        
        ld hl,framesdrawn
        inc (hl)
        
	ld hl,(timer)
oldtimer=$+1
	ld de,0
	ld (oldtimer),hl
	or a
	sbc hl,de
	jp z,mainloop
	ld b,l
	call moveall
	djnz $-3
        
        ld a,0x7f
        in a,(0xfe)
        rra
        jr nc,quit
        
	ld hl,(timer)
	ld de,endtime
	ld (oldtimer),hl
	or a
	sbc hl,de
	jp c,mainloop
quit
        halt
        ld a,0x7f
        in a,(0xfe)
        rra
        jr nc,quit ;until key unpress
        call pt3player.MUTE
        ld a,63
        ld i,a
        ld iy,23610
        im 1
        ld hl,0x5800
        ld de,0x5801
        ld bc,0x2ff
        ld (hl),l
        ldir
         ld de,(0x5cf4)
         ld bc,0x1b05
         ld hl,0x6000
         push hl
         call 0x3d13
         pop hl
         ld de,0x4000
         ld bc,0x1800
         ldir
         halt
         ld a,1
         out (0xfe),a
         ld b,3
         ldir
showpic0
        halt
        ld a,0x7f
        in a,(0xfe)
        rra
        jr c,showpic0 ;until keypress
        ret ;jp $

cury
        db 0 ;NU

reter
        ret

        include "mc4lines.asm"
        include "c2p.asm"
       
       if SHOWFPS
showfps
	ld a,(framesdrawn)
	push de
	ld de,#4000
	ld bc,10-256
	inc b
	sub c
	jr nc,$-2
	add a,c
	ld c,a
	ld a,b
	call showdig
	ld a,c
	call showdig
	pop de
	xor a
	ld (framesdrawn),a
	ret
showdig
	add a,"0"
	push de
	add a,a
	ld h,15
	ld l,a
	add hl,hl
	add hl,hl
	ld b,8
showdig0
	ld a,(hl)
	ld (de),a
	inc l
	inc d
	djnz showdig0
	pop de
	inc e
	ret
       endif
framesdrawn
	db 0

        include "cube/cube.asm"

activeend
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

        include "cube/cube_init.asm"
        include "c2p_init.asm"

        ds 0xc000-$
        incbin "cyberpunk.raw"

tablestart=_tables
	
end

	display "End=",end
	display "Free after end=",/d,#c000-end
	display "Size ",/d,end-begin," bytes"
	
	savebin "maincode.c",begin,end-begin
	
	LABELSLIST "..\us\user.l"
