*******************************************************************************
* External
MOD_MOD		equ *

MOD_init		jmp MIC.init
MOD_vbl			jmp MIC.vbl
MOD_pick_color	jmp MIC.pick_color
MOD_put_pixel	jmp MIC.put_pixel
MOD_enable_zoom		jmp MIC.enable_zoom
MOD_disable_zoom	jmp MIC.disable_zoom
MOD_zoom_update		jmp MIC.zoom_update
; updates a single zoomed line. Y=line number
MOD_zoom_update_line	jmp MIC.zoom_update_line
; updates upper part
MOD_zoom_update_top		jmp MIC.zoom_update_top

MOD_pmY			dta 20 ; in scanlines, Y position of the first line
MOD_pixel_w		dta 1 ; can be 1,2 or 4
MOD_pixel_h		dta 1 ; - anything

MOD_max_x		dta 160
MOD_max_y		dta 200

MOD_max_cursor_w	dta 4 ; for performance
MOD_max_cursor_h	dta 8

MOD_zoom_w			dta 80 ; in mode pixels
MOD_zoom_h			dta MIC.zoomlines ; in mode pixels, MAX: 47
MOD_zoom_pmY		dta 220-[2*MIC.zoomlines] ; in scanlines, Y position of a first line
MOD_zoom_pixel_w	dta 2 ; in 15 pixels
MOD_zoom_pixel_h	dta 2 ; in scanlines
MOD_zoom_top_h		dta 200-4-[2*MIC.zoomlines] ; lines from main panel visible

MOD_screen	equ *
	dta d'C0:.. C1:.. C2:.. C3:..            ::MIC'
	dta d'keys:QW ASDF 0123 JLIK PCZ UY           '

MOD_curcolor_X	dta 0  ; in 15 pixels, from 0
MOD_curcolor_W	dta 40 ; in 15 pixels

; returns the mode color as a byte (used in UNDO)
MOD_get_color	jmp MIC.get_color
; sets the mode colors as a byte (used in UNDO)
MOD_set_color	jmp MIC.set_color

MOD_MOD_LEN	equ *-MOD_MOD

*******************************************************************************
* Internal
MIC			.LOCAL

zoomlines	equ 47

pline		equ $8000
scrE		equ $8060

mic_dl		equ $ac00

loline		equ $b000
hiline		equ $b100
div4		equ $b200
ands		equ $b300
lsrs		equ $b400
zoomhi		equ $b500
zoomlo		equ $b600

dlzoom_fill	equ $b800 ; zoomlines*15

*******************************************************************************

palette		dta $00,$14,$28,$3e
cur_color 	dta 0
mic_hscrol	dta 12

set_color	
	sta cur_color
	rts
get_color
	lda cur_color
	rts

vbl
	mwa #mic_dl $d402
	mwa #dli_palette $200
	rts

init
	jsr create_DL
	jsr create_tabs
	jsr update_status
	ldxy MIC_KEYTABLE
	jsr KB.extend_keytable
	ldxy MIC_MENUTABLE
	jsr MENU.extend_menutable

	jsr prp_dlzoom_fill
	rts

dli_palette
	pha
	sta $d40a
	mva palette+0 $d01a
	mva palette+1 $d016
	mva palette+2 $d017
	mva palette+3 $d018
	mva mic_hscrol :HSCROL

	mwa DLI.down_vec $200	; switch DLI
	pla
	rti

create_DL
	mwa #scrE zerHlp
	mwa #core_dl _mcd+1
	ldy #200
_mcd0
	lda #$4e
	jsr _mcd
	lda zerHlp
	jsr _mcd
	lda zerHlp+1
	jsr _mcd
	lda zerHlp
	clc
	adc #40
	sta zerHlp
	bcc *+4
	inc zerHlp+1
	dey
	bne _mcd0

	; return to DL_return
	lda #$01
	jsr _mcd
	lda <DL_return
	jsr _mcd
	lda >DL_return
;	jsr _mcd
_mcd	sta core_dl
	inw _mcd+1
	rts

prp_dlzoom_fill .LOCAL
	mwa #dlzoom_fill wr+1
	mwa #DL_zoom+2 zer0
; first, fill lo byte
	ldx #[zoomlines]
lp	lda #{sta DL_zoom+2}
	jsr wr
	lda zer0
	jsr wr
	lda zer0+1
	jsr wr
	addtozer zer0 3
	dex
	bne lp
; then, fill hi byte
	mwa #DL_zoom+3 zer0
	mwa #0 zerHlp
	ldx #[zoomlines]
hloop ;)
	lda #{lda zoom_lineadr,X}
	jsr wr
	lda zerHlp
	jsr wr
	lda >zoom_lineadr
	jsr wr
	lda #{sta DL_zoom}
	jsr wr
	lda zer0
	jsr wr
	lda zer0+1
	jsr wr
	addtozer zer0 3
	inc zerHlp
	dex
	bne hloop
; and exit
	lda #{rts}
wr	sta dlzoom_fill
	inw wr+1
	rts
	.ENDL

* ****************************************************************************
* Palette management
colorpos dta 0,40,80,120

chg_color
	clc
	adc cur_color
	and #3
	sta cur_color

upd_color
	ldy cur_color
	lda colorpos,y
	sta MOD_curcolor_x
	rts
chg_col_val
	ldx cur_color
	clc
	adc palette,x
	sta palette,x
	jmp update_status
set_kolor
	sta cur_color
	jmp upd_color

* ****************************************************************************
pick_color
	ldy cursor_y
	lda loline,y
	sta zerHlp
	lda hiline,y
	sta zerHlp+1
	ldx cursor_x
	ldy div4,x
	lda (zerHlp),y
	and ands,x
	ldy lsrs,x
	beq mpc_cont
mpc	lsr @
	dey
	bne mpc
mpc_cont
	sta cur_color
	jmp upd_color
;	rts
* ****************************************************************************
mpp_cntY	dta 0
mpp_cntX	dta 0
put_pixel
	jsr UNDO.put_pixel_prelude

	mva cursor_h mpp_cntY

	ldy cursor_y
	sty UNDO.pos_y
	lda loline,y
	sta zerHlp
	lda hiline,y
	sta zerHlp+1
mpp_loopY
	mva #0 mpp_cntX
mpp_loopx
	lda cursor_x
	clc
	adc mpp_cntX
	tax
	stx UNDO.pos_X
	
; save old color
	ldy div4,x
	lda (zerHlp),y
	ldy lsrs,x
	beq pick_end
pick
	lsr @
	dey
	bne pick
pick_end
	and #3
	sta UNDO.old_c
; put pixel
	lda cur_color
	ldy lsrs,x
	beq mpp_cont
mpp	asl @
	dey
	bne mpp
mpp_cont
	sta zerHlp+2
	ldy div4,x
	lda ands,x
	eor #$ff
	and (zerHlp),y
	ora zerHlp+2
	sta (zerHlp),y
	
	lda cur_color
	sta UNDO.new_c
	jsr UNDO.put_pixel_save

	inc mpp_cntX
	lda mpp_cntX
	cmp cursor_W
	bne mpp_loopX

	lda zerHlp
	clc
	adc #40
	sta zerHlp
	bcc *+4
	inc zerHlp+1

	inc UNDO.pos_Y
	dec mpp_cntY
	bne mpp_loopY

	jsr UNDO.put_pixel_epilogue
	rts


* ****************************************************************************
mct_ands	dta $c0,$30,$0c,$03
mct_lsrs	dta 6,4,2,0
create_tabs
	mwa #scrE zer0
	ldy #0
_mct0	tya
	lsr @
	lsr @
	sta div4,y
	tya
	and #3
	tax
	lda mct_ands,x
	sta ands,y
	lda mct_lsrs,x
	sta lsrs,y
	lda zer0
	sta loline,y
	clc
	adc #40
	sta zer0
	lda zer0+1
	sta hiline,y
	adc #0
	sta zer0+1

	tya
	and #$c0
	sta zerHlp
:2	lsr @
	ora zerHlp
	sta zerHlp
	tya
	and #$30
:2	lsr @
	pha
	ora zerHlp
	sta zerHlp
	pla
:2	lsr @
	ora zerHlp
	sta zoomhi,y

	tya
	and #$0c
:2	asl @
	sta zerHlp
:2	asl @
	ora zerHlp
	sta zerHlp
	tya
	and #$03
	ora zerHlp
	sta zerHlp
	tya
	and #$03
:2	asl @
	ora zerHlp
	sta zoomlo,y

	iny
	bne _mct0
	rts
* ****************************************************************************
update_status
	lda palette
	ldxy MOD_SCREEN+3
	jsr write_hex
	lda palette+1
	ldxy MOD_SCREEN+9
	jsr write_hex
	lda palette+2
	ldxy MOD_SCREEN+15
	jsr write_hex
	lda palette+3
	ldxy MOD_SCREEN+21
	jmp write_hex

* ****************************************************************************
* zoom handling
* ****************************************************************************
zoom_lastY	dta 0

zoom_update .LOCAL
	mwa #DL_zoom+2 zer0
	lda ZOOM.zoom_X
	lsr @
	pha
	lda #12
	bcc *+4
	lda #10
	sta HSCROL
	sta mic_hscrol
	pla
	ldx ZOOM.zoom_Y
	jmp dlzoom_fill ;)
	.ENDL

enable_zoom
	rts

disable_zoom
	jsr fix_lastY
	; fix last line
	mwa #[core_dl+600] zer0
	ldy #0
	lda #$01
	sta (zer0),y+
	lda <DL_return
	sta (zer0),y+
	lda >DL_return
	sta (zer0),y
	; restore main DL
	mwa #core_dl DL_jsr
	rts

; Y - lineno
zoom_update_line	.LOCAL
	lda zoom_lineadr,y
	sta dst1+2
	sta dst2+2
	lda loline,y
	sta src+1
	lda hiline,y
	sta src+2
	ldx #39
	ldy #79
loop
src		lda scrE,x
	stx zerHlp
	tax
	lda zoomlo,x
dst1	sta $c000+1,y-
	lda zoomhi,x
dst2	sta $c000+1,y-
	ldx zerHlp
	dex
	bpl loop
	rts
	.ENDL

zoom_update_top
	;fix previous last line
	jsr fix_lasty

	;set new first line
	ldy ZOOM.zoom_top_y
	jsr calc_dl_pos
	lda zer0
	sta DL_jsr
	lda zer0+1
	sta DL_jsr+1

	;set new last line
	lda ZOOM.zoom_top_y
	clc
	adc MOD_zoom_top_h
	sta zoom_lastY
	tay
	jsr calc_dl_pos
	ldy #0
	lda #$01
	sta (zer0),y+
	lda <DL_zoom
	sta (zer0),y+
	lda >DL_zoom
	sta (zer0),y
	rts

; Y - lineno, sets zer0
calc_dl_pos
	mwa #core_dl zer0
	sty cdp_add+1
	ldx #3
cdp	lda zer0
	clc
cdp_add	adc #0
	sta zer0
	bcc *+4
	inc zer0+1
	dex
	bne cdp
	rts

fix_lasty
	ldy zoom_lastY
	jsr calc_dl_pos
	ldy #0
	lda #$4e
	sta (zer0),y+
	ldx zoom_lastY
	lda loline,x
	sta (zer0),y+
	lda hiline,x
	sta (zer0),y+
	rts

* ****************************************************************************
MIC_KEYTABLE
	dta KEYCODE_Q,7,a(chg_color),$ff
	dta KEYCODE_W,7,a(chg_color),$01

	dta KEYCODE_A,15,a(chg_col_val),$fe
	dta KEYCODE_S,15,a(chg_col_val),$02
	dta KEYCODE_D,15,a(chg_col_val),$f0
	dta KEYCODE_F,15,a(chg_col_val),$10
	dta KEYCODE_1,7,a(set_kolor),1
	dta KEYCODE_2,7,a(set_kolor),2
	dta KEYCODE_3,7,a(set_kolor),3
	dta KEYCODE_0,7,a(set_kolor),0

	dta $ff

* ****************************************************************************
* I/O routines
MIC_ext	dta c'MIC'

load_mic
	ldxy MIC_ext
	jsr IO.open_read
	bpl transfer_mic
	rts
save_mic
	ldxy MIC_ext
	jsr IO.open_write
	bpl transfer_mic
	rts
transfer_mic	.LOCAL
	mwa #scrE IO.adr
	mwa #8000 IO.len
	jsr IO.do
	bmi ex

	mwa #palette IO.adr
	mwa #4 io.len
	jsr IO.do
	bmi ex

	jsr IO.close
ex
	rts
	.ENDL ; transfer_mic

clear .LOCAL
	jsr IO.confirm
	beq ex

	mwa #scrE zer1
	mwa #8000 zer2
	lda #0
	jsr memset
ex	rts
	.ENDL


* ****************************************************************************
MIC_MENUTABLE
	dta '1',a(load_mic),a(load_mic_msg)
	dta '2',a(save_mic),a(save_mic_msg)
	dta 'C',a(clear),a(clear_msg)
	dta $ff

load_mic_msg
	dta c'1 - Load *.MIC',eol
save_mic_msg
	dta c'2 - Save *.MIC',eol
clear_msg
	dta c'C - Clear picture',eol

* ****************************************************************************
MIC_return	equ *

		ift INCLUDE_PICTURE
	org scrE
	ins 'mic/fight.mic'
;	ins 'mic/tqatrees.mic'
;	ins 'mic/rpl-a.mic'
		eif

	org pline
:10	dta 0
:10	dta $55
:10 dta $aa
:10 dta $ff

	org MIC_DL
	dta $80
:8	dta $4e,a(pline)
	dta 0,0
DL_jsr	equ *+1
	dta $01,a(core_dl)
DL_return
	dta $81,a(DL.down)

DL_zoom
	dta $20
:zoomlines	dta $5d,0,0
	dta $01,a(DL_return)

core_dl	equ *

	org MIC_return

MIC.MOD_LEN	equ *-MIC
	.ENDL ; MIC

