;; (c) Copyright Kevin Thacker 2015-2016
;; This code is part of the Arnold emulator distribution.
;; This code is free to distribute without modification
;; this copyright header must be retained.

;; HSYNC len<=2 doesn't generate hsync to monitor

org &4000

km_wait_char equ &bb06
txt_output equ &bb5a
scr_set_mode equ &bc0e

start:
ld a,2
call scr_set_mode
ld hl,message
call display_msg
call km_wait_char

ld a,2
call scr_set_mode
ld hl,message_monitor
call display_msg
call km_wait_char
cp 'y'
ld c,1
jr z,amstrad_monitor
cp 'x'
jr z,amstrad_monitor
cp 'Y'
jr z,amstrad_monitor
cp 'X'
jr z,amstrad_monitor
ld a,&06
ld (hsync3+1),a
ld (hsync4+1),a
ld (hsync5+1),a
amstrad_monitor:

ld a,1
call scr_set_mode

ld hl,&c000
ld e,l
ld d,h
inc de
ld (hl),%1010000
ld bc,&3fff
ldir

;; purple, but goes to grey for 1 pixel before graphics!

;; type 1 start (pen 3)
ld hl,&c000 ;; 0
dec hl
dec hl
dec hl
dec hl
ld a,h
or &c0
ld h,a
ld b,%11111111
ld c,%11111111
call draw_col


;; type 1 mid
ld hl,&c000+((8-1)*2)
ld b,%00110000
ld c,%11110000
call draw_col

;; type 1 end (pen 2)
ld hl,&c000+((15-1)*2)
ld b,%00000011
ld c,%00001111
call draw_col

ld b,26
ld c,26
call &bc38

xor a
ld b,13
ld c,13
call &bc32


ld a,1
ld b,6		;; red
ld c,6
call &bc32


ld a,2
ld b,16		;; pink
ld c,16
call &bc32


ld a,3
ld b,4
ld c,4
call &bc32

ld b,6*3
l2:
halt
djnz l2

di
ld bc,&bc02
out (c),c
ld bc,&bd00
out (c),c
ld bc,&bc00
out (c),c
ld bc,&bd00+31
out (c),c
ld bc,&bc04
out (c),c
ld bc,&bd00+77
out (c),c
ld bc,&bc07
out (c),c
ld bc,&bd00
out (c),c
ld bc,&bc06
out (c),c
ld bc,&bd00+&7f
out (c),c
loop: 
ld b,&f5
loop2:
in a,(c)
rra
jr nc,loop2
ld bc,&bc01
out (c),c
ld bc,&bd00
out (c),c
ld bc,&7f00
out (c),c
ld bc,&7f43
out (c),c
ld bc,&bc03
out (c),c
ld bc,&bd00+&8
out (c),c
ld b,64-8
loop3:
defs 64-1-3
dec b
jp nz,loop3
ld bc,&bc01
out (c),c
ld bc,&bd00+&7f
out (c),c
ld b,8
wl2b:
defs 64-1-3
dec b
jr nz,wl2b

ld bc,&7f00
out (c),c
ld bc,&7f40
out (c),c
;;ld bc,&bc03
;;out (c),c
;;ld bc,&bd00
;;out (c),c
;;call wait_lines
;;ld bc,&bc03
;;out (c),c
;;ld bc,&bd01
;;out (c),c
;;call wait_lines
;;ld bc,&bc03
;;out (c),c
;;ld bc,&bd02
;;out (c),c
;;call wait_lines
ld bc,&bc03
out (c),c
hsync3:
ld bc,&bd03
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
hsync4:
ld bc,&bd04
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
hsync5:
ld bc,&bd05
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd06
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd07
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd08
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd09
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd0a
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd0b
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd0c
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd0d
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd0e
out (c),c
call wait_lines
ld bc,&bc03
out (c),c
ld bc,&bd0f
out (c),c
call wait_lines
ld bc,&7f00
out (c),c
ld bc,&7f43
out (c),c
ld b,8
wl2:
defs 64-1-3
dec b
jr nz,wl2
ld bc,&bc01
out (c),c
ld bc,&bd00
out (c),c
ld bc,&bc03
out (c),c
ld bc,&bd00+&8
out (c),c
jp loop

draw_col:
ld d,0
dc:
push hl
ld (hl),b
inc hl
ld a,h
or &c0
ld h,a
ld (hl),c
pop hl
ld a,l
add a,64
ld l,a
ld a,h
adc a,0
ld h,a
dec d
jr nz,dc
ret

wait_lines:
ld b,15
wl1:
defs 64-1-3
dec b
jp nz,wl1
defs 64-5-3
ret


display_msg:
ld a,(hl)
or a
ret z
inc hl
call txt_output
jr display_msg

message_monitor:
defb "Amstrad monitor (Y or N). (Y for GT64,GT65,MM14,CTM640,CTM644,CM14)",0

message:
defb "This is a visual test.",13,10,13,10
defb "This test sets HSYNC length values (3-f) and shows the blanking which is",13,10
defb "generated by the Gate-Array.",13,10,13,10
defb "The screen has graphics to cover the screen. The background is grey",13,10
defb "where the test is active",13,10
defb "On CPC and Plus (turn up brightness to see):",13,10,13,10
defb "a) The screen is blanked - set to black - for the entire HSYNC length.",13,10
defb "b) two microsecond delay from the start of the HSYNC from CRTC",13,10
defb "to start of the monitor HSYNC (which is absolute black)",13,10
defb "c) monitor HSYNC lasts max of 4 microsecond (where HSYNCLEN>=6)",13,10
defb "d) monitor HSYNC is cut if the HSYNC from CRTC is too short (gfx will be seen)",13,10
defb "e) if the HSYNC is cut the screen distorts",13,10
defb "e) where the border is (it's white), this is also blanked",13,10
defb "f) there is a red bar. HSYNC is at HCC=0, this bar is at R0-2. (Type 1 shows red bar, and 1 pixel of graphics before HSYNC begins",13,10,13,10
defb "This test may not run on a television. Real CPC monitor is recommended!",13,10
defb "If you answer 'N' to Amstrad monitor, HSYNC less than 6 will not be shown",13,10,13,10
defb "Press a key to start",0

end start