
SYCLES  EQU 0
;START   EQU 1
ST1     EQU 2
DPAL1   EQU 3
ST2     EQU 4
DPAL2   EQU 5

	MACRO NEXT_LIN_H
		EX AF,AF'
		INC A
		OUT (RGADR),A
		EX AF,AF'
	ENDM

	MACRO NEXT_LIN_HP
		EX AF,AF'
		INC A
		OUT (RGADR),A
		JR NZ,.MD_NO_INC_H
		inc l
.MD_NO_INC_H:
		EX AF,AF'
	ENDM


;*****************************************
;
;	Инициализация страниц для
;	спектрумовского режима и
;	восстановление спектрумовского
;	экрана
;
;	Возврат по IX
;
;	!!! NO USE STACK !!!
;
;*****************************************
; инициализация страниц zx spectrum
INIT_PAGES:
	AND A
	LD E,A
	EX AF,AF'
	LD A,E

;	BIT 7,A
;	JR Z,SCORPION_256_MODE
;	BIT 6,A
;	JR Z,PENTAGON_128_MODE
;
;PENTAGON_48_MODE:
;	AND 03CH
;        OUT (SLOT0),A
;	INC A
;        OUT (SLOT1),A
;	INC A
;        OUT (SLOT2),A
;	INC A
;	LD D,A
;	LD BC,1FFDH
;	XOR A
;	OUT (C),A
;	LD B,7FH
;	OUT (C),A
;	LD A,D
;	OUT (SLOT3),A
;	JR INIT_VIDEO_REG
;
;
; PENTAGON_128_MODE:
; 	LD D,8
; 	AND #38
; 	JR ALL_MODE

SCORPION_256_MODE:
	LD D,16
	AND #30								; до 4-х спектрумов

ALL_MODE:
	OUT (SLOT0),A
	XOR 5
	OUT (SLOT1),A
	XOR 7
	OUT (SLOT2),A
	OR #0F
	LD E,A

.loop:  DEC D

	LD A,D
	RLCA
	AND #10
	LD BC,#1FFD
	OUT (C),A

	LD A,D
	AND 7
	OR #40
	LD B,#7F
	OUT (C),A

	LD A,E
	OUT (SLOT3),A
	DEC E

	DEC D
	INC D
	JR NZ,.loop

INIT_VIDEO_REG:
	XOR A
	OUT (RGADR),A						; регистр видео адреса
;	OUT (RGSCR),A						; регистр экрана
	OUT (RGMOD),A						; регистр моды
        
        ;!TEST
         ; Restore old Spectrum Screen
	 ;; LD HL,#4000
	 ;; LD DE,#4000
	 ;; LD BC,#1B00
	 ;; LDIR

	 ; EX AF,AF'
	 ; BIT 6,A
	 ; JR NZ,NO_SCREEN_ALT

	 ; EX AF,AF'

	 ; LD A,7								; седьмая экранная страница
	 ; LD BC,#7FFD
	 ; OUT (C),A

         ; Restore old Spectrum Screen 2
	 ;; LD HL,#C000
	 ;; LD DE,#C000
	 ;; LD BC,#1B00
	 ;; LDIR								

	 ; XOR A
	 ; LD BC,#7FFD
	 ; OUT (C),A
        ;

	EX AF,AF'
NO_SCREEN_ALT:
	AND A
	JP (IX)

;****************************************
;       Загрузка режима экрана.
;****************************************

;	CALL SINC_DEF
;	CALL SET_PAL_ZX
;	RET

;****************************************

;!TODO тут функция 
CL0     EQU 200
CL1     EQU 240

SET_PAL_IBM:
	LD DE,#8000
	JR SET_PAL_TXT
SET_PAL_ZX:
	LD DE,0
SET_PAL_TXT:
	PUSH IX
	IN A,(RGADR)
	PUSH AF
	IN A,(SLOT3)
	EX AF,AF'
	LD A,#50
	OUT (SLOT3),A

	LD IX,#C3F0                                                     ; !HARDCODE адрес палитры?
.generate_loop:
	CALL GENERATE_PAL1
	LD A,E
	OUT (RGADR),A
	LD (IX),L	; R
	LD (IX+1),B	; G
	LD (IX+2),C	; B
	LD (IX+3),H	; i ?
	INC E
	JR NZ,.generate_loop
	;
	LD BC,4
	ADD IX,BC
	INC D
	LD A,D
	AND 3
	JR NZ,.generate_loop

	EX AF,AF'
	OUT (SLOT3),A
	POP AF
	OUT (RGADR),A
	POP IX
	RET

;	генерация спектрумовской палитры.
; Вход:	 E - атрибут, D - номер палитры
; ВЫХОД: L - red, B - green, C - blue, H - intensity
GENERATE_PAL1:	XOR	A
		LD	C,A
		LD	B,A
		LD	L,A
		BIT	7,D
		JR	NZ,GENERATE_IBM
		BIT	1,D
		JR	NZ,.FLH
.NOF:		BIT	0,D
		JR	Z,.PAPER
.INK:		LD	A,CL0
		BIT	6,E
		JR	Z,.NOI1
		LD	A,CL1
.NOI1:		BIT	0,E		; BLUE
		JR	Z,.NO1
		LD	C,A
.NO1:		BIT	1,E		; RED
		JR	Z,.PP_NO2
		LD	L,A
.PP_NO2:	BIT	2,E		; GREEN
		JR	Z,.PP_NO3
		LD	B,A
.PP_NO3:	JR	.PP_NO6
;
.FLH:		BIT	7,E
		JR	Z,.NOF
		BIT	0,D
		JR	Z,.INK
.PAPER:		LD	A,CL0
		BIT	6,E
		JR	Z,.NOI2
		LD	A,CL1
.NOI2:		BIT	3,E		; BLUE
		JR	Z,.PP_NO4
		LD	C,A
.PP_NO4:	BIT	4,E		; RED
		JR	Z,.PP_NO5
		LD	L,A
.PP_NO5:	BIT	5,E		; GREEN
		JR	Z,.PP_NO6
		LD	B,A
.PP_NO6:	LD	A,C
		AND	A
		RRA
		ADD	A,L
		RRA
		ADD	A,B
		RRA
		LD	H,A		; B/W mode
		RET
;****************************************

GENERATE_IBM:
	BIT 7,E
	JR Z,.NO_FLH
	BIT 1,D
	JR Z,.PAPER
.NO_FLH:
	BIT 0,D
	JR Z,.PAPER
.INK:
.NO_INTENS:
	LD A,#A8
.INTENS:
	BIT 0,E		; BLUE
	JR Z,.PPI_NO4
	LD C,A
.PPI_NO4:
	BIT 2,E		; RED
	JR Z,.PPI_NO5
	LD L,A
.PPI_NO5:
	BIT 1,E		; GREEN
	JR Z,.PPI_NO6
	LD B,A
.PPI_NO6:
	LD A,E
	AND #0F
	CP 6
	JR NZ,.no_correct
	LD B,#54
.no_correct:
	BIT 3,E
	JR Z,GENERATE_PAL1.PP_NO6

	LD A,#54
	ADD A,C
	LD C,A
	LD A,#54
	ADD A,B
	LD B,A
	LD A,#54
	ADD A,L
	LD L,A
	JR GENERATE_PAL1.PP_NO6
.PAPER:
	LD A,#A8
	BIT 4,E		; BLUE
	JR Z,.PPI_NO4X
	LD C,A
.PPI_NO4X:
	BIT 6,E		; RED
	JR Z,.PPI_NO5X
	LD L,A
.PPI_NO5X:
	BIT 5,E		; GREEN
	JR Z,.PPI_NO6X
	LD B,A
.PPI_NO6X:
	LD A,E
	AND #70
	CP #60
	JR NZ,GENERATE_PAL1.PP_NO6
	LD B,#54
	JR GENERATE_PAL1.PP_NO6
;****************************************

;****************************************
;****************************************

; D - номер графической палитры
SET_PAL_GRAF:
	PUSH IX
	IN A,(RGADR)
	PUSH AF

	IN A,(SLOT3)
	EX AF,AF'
	LD A,#50
	OUT (SLOT3),A


	LD IX,#C3E0
	LD A,D
	AND 3
	ADD A,A
	ADD A,A
	LD E,A
	LD D,0
	ADD IX,DE

	LD E,0

	XOR A
	LD B,A
	LD C,A
	LD L,A
	LD H,A

SET_PAL_GR1:
	LD A,E
	OUT (RGADR),A

	LD (IX),L
	LD (IX+1),B
	LD (IX+2),C
	LD (IX+3),H

	CALL GENERATE_PAL3

	INC E
	LD A,E
	CP 40
	JR NZ,SET_PAL_GR1

	XOR A
	LD B,A
	LD C,A
	LD L,A
	LD H,A

SET_PAL_GR2:
	LD A,E
	OUT (RGADR),A

	LD (IX),L
	LD (IX+1),B
	LD (IX+2),C
	LD (IX+3),H

	CALL GENERATE_PAL2

	INC E
	JR NZ,SET_PAL_GR2

	EX AF,AF'
	OUT (SLOT3),A
	POP AF
	OUT (RGADR),A
	POP IX
	RET
;
;;*********************************
;

GENERATE_PAL3:		; gray-scale
	LD A,B
	ADD A,6
	LD B,A
	LD C,A
	LD L,A

	LD A,C
	AND A
	RRA
	ADD A,L
	RRA
	ADD A,B
	RRA
	LD H,A

	RET

GENERATE_PAL2:		; C - BLUE; L - RED; B - GREEN; H - INTENSITY
	LD A,C
	ADD A,50
	LD C,A
	JR NC,GEN_PAL2_L1
	LD C,0
	LD A,L
	ADD A,50
	LD L,A
	JR NC,GEN_PAL2_L1
	LD L,0
	LD A,B
	ADD A,50
	LD B,A
	JR NC,GEN_PAL2_L1
	LD B,0

GEN_PAL2_L1:
	LD A,C
	AND A
	RR A
	ADD A,L
	RR A
	ADD A,B
	RR A
	LD H,A

	RET
;
;;*****************************************

FN_SYNC:
	BIT 7,a
	JR Z,.old_mode

	ld h,a
	and #60								; check reserved bits
	jr nz,.error

	bit 2,h								; Set V-Sinc?
	jr nz,.set_v_sinc

	bit 4,h								; Set Waits?
	jr z,.error							; Error - no parameters

.set_waits:
	ld a,h
	and 8								; check waits. Z - no waits, NZ - waits
	ld h,#FF
	jr z,1F
	ld h,#FB
1:	
	ld bc,(Port_All_Mode)
	in a,(C)
	and h
	out (C),a
	RET	

.set_v_sinc:
	ld a,h
	and 3
	jr z,.SetDefLines
	dec a
	jr z,.SetCmosLines
	dec a
	jr z,.Set320Lines
;	jr Set312Lines

.set312lines:
	ld a,Port_VSYNC.SET_312L
	out (Port_VSYNC),a 

	LD C,SLOT3
	IN B,(C)
		LD A,SYS_PAGE
		OUT (C),A
		ld a,(SYS_PAGE.VSyncAndWaits)
		and 2
		ld (SYS_PAGE.VSyncAndWaits),a
	OUT (C),B		

.end_set_v_sinc:	
	bit 4,h
	ret z
	jr .set_waits


.Set320Lines:
	ld a,Port_VSYNC.SET_320L
	out (Port_VSYNC),a 

	LD C,SLOT3
	IN B,(C)
		LD A,SYS_PAGE
		OUT (C),A	
		ld a,(SYS_PAGE.VSyncAndWaits)
		or 1
		ld (SYS_PAGE.VSyncAndWaits),a
	OUT (C),B

	jr .end_set_v_sinc

.SetCmosLines:
	LD D,CMOS_CELL.ScreenSET
	CALL CMOS_RD

	AND high CMOS_CELL.ScreenSET.Mask.Sinc						; в регистре A значение ScreenSET
	jr z,.SetDefLines

	bit 6,a
	jr z,.set312lines

	jr .Set320Lines

.old_mode:	
	AND A
	JR Z,.INT_DEF
	DEC A
	JR Z,.INT_SCORP
	DEC A
	JR Z,.INT_PENT
	DEC A
	JR Z,.INT_ORIG
	DEC A
	JR Z,.INT_CMOS_SINC
	; [x] кастомный экран по таблице пользователя
	 DEC A
	 JR Z,.PROG_SCR
	;
.error:	SCF
	RET
	
.SetDefLines:
	LD C,SLOT3
	IN B,(C)
	 LD A,SYS_PAGE
	 OUT (C),A	
	 ld a,(SYS_PAGE.VSyncAndWaits)
	OUT (C),B
	and 1
	jr z,.set312lines
	jr .Set320Lines

.INT_DEF:
	LD C,SLOT3
	IN B,(C)
	 LD A,SYS_PAGE
	 OUT (C),A
	 LD DE,(SYS_PAGE.CONFIG_ALL)
	OUT (C),B

	CALL Test_CONFIG_ALL
	jr nz,.INT_CMOS_SINC
	jp (IX)

.INT_CMOS_SINC:
	LD C,SLOT3
	IN B,(C)
	 LD A,SYS_PAGE
	 OUT (C),A
	 LD DE,(SYS_PAGE.CONFIG_ALL)
	OUT (C),B

	LD D,CMOS_CELL.ScreenSET
	CALL CMOS_RD

	AND high CMOS_CELL.ScreenSET.Mask.Int				; в регистре A значение ScreenSET
	JR NZ,.skiptest

	CALL	Test_CONFIG_ALL
	JP	(IX)							; default int (non CMOS) 

.skiptest:
	cp #10
	jr z,.INT_SCORP						; scorpion int	

	cp #20
	jr z,.INT_PENT						; pentagon int		

;	jr ORIG_SINC						; original int

.INT_ORIG:
	LD IX,SCREEN_TABLES.ORIGINAL
	JR .PROG_SCR
.INT_SCORP:
	LD IX,SCREEN_TABLES.SCORPION
	JR .PROG_SCR
.INT_PENT:
	LD IX,SCREEN_TABLES.PENTAGON
;	JR PROG_SCR
.PROG_SCR:
	IN A,(SLOT3)
	EX AF,AF'
	LD A,SYS_PAGE
	OUT (SLOT3),A
	EX AF,AF'
	LD (SYS_PAGE.COPY_SLOT3),A
	IN A,(PORT_Y)
	LD (SYS_PAGE.COPY_RGADR),A

	LD (SYS_PAGE.CONFIG_ALL),IX
	XOR	A

;----[START]-----------------------[? 1]
.loop_1:
	OUT (PORT_Y),A
	EX AF,AF'

	LD A,#50
	OUT (SLOT3),A

	LD HL,#C300

;----[START]------------------[v 2]-----
.loop_2:
	LD C,(IX)
;----[START]-------------[v 3]----------
.loop_3:
; взять адрес данных для записи в служебную область экрана
	LD E,(IX+1)							; take adress of line X
	LD D,(IX+2)
;----[START]--------[v 4]---------------
.loop_4:
	LD A,(DE)							; take counter in table 1, line X, column Y
	INC DE
	AND A
	JR Z,.loop_4_exit					        ; exit if zero-counter
	LD B,A
	LD A,(DE)
	INC DE
;----[START]---[v 5]--------------------
.loop_5:
	LD (HL),A
	INC L
	LD (HL),0
	INC L
	LD (HL),0

	EX AF,AF'
	INC A
	OUT (PORT_Y),A
	EX AF,AF'

	LD (HL),0
	DEC L
	LD (HL),0
	DEC L
	LD (HL),A

	EX AF,AF'
	INC A
	OUT (PORT_Y),A
	EX AF,AF'

	DJNZ .loop_5
;--------------[^ 5]--------------------
	JR .loop_4
;-------------------[^ 4]---------------
.loop_4_exit:
	INC HL								; next line
	INC HL
	INC HL
	INC HL
	IN A,(PORT_Y)
	AND #80
	OUT (PORT_Y),A
	DEC C
	JR NZ,.loop_3
;------------------------[^ 3]----------
	INC IX								; next counter
	INC IX
	INC IX
	LD A,(IX)
	AND A
	JR NZ,.loop_2
;-----------------------------[^ 2]-----
	LD A,SYS_PAGE
	OUT (SLOT3),A
	LD IX,(SYS_PAGE.CONFIG_ALL)

	EX AF,AF'
	ADD A,#80
	JR NC,.loop_1
;----------------------------------[^ 1]

        ; Exit
	LD A,(SYS_PAGE.COPY_RGADR)
	OUT (PORT_Y),A
	LD A,(SYS_PAGE.COPY_SLOT3)
	OUT (SLOT3),A
.exit:	OR A
	RET

;DE - содержимое CONFIG_ALL
Test_CONFIG_ALL:
	LD HL,SCREEN_TABLES.SCORPION
	AND A
	SBC HL,DE
	ld IX,FN_SYNC.INT_SCORP
	RET Z

	LD HL,SCREEN_TABLES.ORIGINAL
	AND A
	SBC HL,DE
	ld IX,FN_SYNC.INT_ORIG
	RET Z

	LD HL,SCREEN_TABLES.PENTAGON
	AND A
	SBC HL,DE
	ld IX,FN_SYNC.INT_PENT
	RET Z
	and a
	RET


;--- Screen data table 1: counter1, data1 .. counterX, dataX
SCREEN_TABLES:
;
;        | число  | значения |
;        | строк  |    в     |
;        |   /2   |  строках |
.SCR:   DB   41   ,   #F8
        DB   3    ,   #FC
        DB   4    ,   #FC
        DB   7    ,   #FC
        DB   9    ,   #F8
        DB   0

;.SCR:   DB	41,#F8,  3,#FC,  4,#FC,  7,#FC,  9,#F8,  0   
.INT:	 DB	40,#FC,	 2,#FD,	 6,#FC,	 7,#FC,	 9,#FC,	 0	;!TEST PENTAGON INT 2 квадратика                   
.INT_SC: DB	41,#F8,	 1,#FD,	 6,#FC,	 7,#FC,	 9,#F8,	 0
.BLN:	 DB	41,#FC,	 3,#FC,	 4,#FC,	 7,#FC,	 9,#FC,	 0
.SNC:	 DB	41,#FC,	 3,#FC,	 4,#FC,	 7,#FC,	 9,#FC,	 0
.RES:	 DB	41,#F8,	 3,#FE,	 4,#FE,	 7,#FE,	 9,#F8,	 0
;---------------------------------------

;--- Screen data tables 2: counter1, data address1 .. counterX, data addressX
;
;                | кол-во | значения |
;                | строк  |     в    |
;                |        |  строке  |
.PENTAGON:      DB   33   :  DW .SCR
                DB   1    :  DW .INT      ;	DW .BLN
                DB   3    :  DW .SNC
                DB   1    :  DW .BLN
                DB   1    :  DW .SCR
                DB   1    :  DW .RES
                DB   0    ; end
.SCORPION:      DB   31   :  DW .SCR
                DB   1    :  DW .INT_SC
                DB   1    :  DW .SCR
                DB   1    :  DW .SNC
                DB   3    :  DW .SNC
                DB   1    :  DW .BLN
                DB   2    :  DW .RES
                DB   0    ; end
.ORIGINAL:      DB   33   :  DW .SCR
                DB   1    :  DW .SNC
                DB   1    :  DW .INT
                DB   2    :  DW .SNC
                DB   1    :  DW .BLN
                DB   1    :  DW .SCR
                DB   1    :  DW .RES
                DB   0    ; end
;---------------------------------------
FLEX_END:
