;[]=======================================================================[]
;
; main.z80 -- stub file for TextOut procedures
;
; Created by Anton Enin 26-June-2002
;
; Last update 27-June-2002
;
; Copyright (C) 2002 R-lab
;
; This program should be compiled using z80asm
; http://www.zxasm.narod.ru/
;
; z80asm.exe -f bin main.z80 -o a_print.exe
;
;[]=======================================================================[]
; Sprinter constans -- see Sprinter docs

VPAGE		equ	0x50			; video memory page
YPORT		equ	0x89			; y pos for write to the vram
PAGE0		equ	0x82			; memory page ports
PAGE1		equ	0xA2			;
PAGE2		equ	0xC2                    ;
PAGE3		equ	0xE2                    ;

		struc HeaderStruct		; Estex Exe Header
id:		resb	3			; exe file Id (must be 'EXE')
version:	resb	1			; exe file version
code_offeset:	resd	1			; offset to start code (header size)
loader_size:	resw	1			; first loader size
reserved1:	resw	3			; reserved
start_address:	resw	1			; program org
entry_point:	resw	1			; program entry point
stack_point:	resw	1			; program stack point
reserved2:	resb	490			; reserved
		endstruc

FONT_BASE	equ	0x8000

;[]=======================================================================[]

		section .text
		org	0x7E00

;[]=======================================================================[]
; EXE header

ExeHeader:	istruc HeaderStruct		; initialized structure
		at id,            db 'EXE'
                at version,       db 0
                at code_offeset,  dd CodeStart - ExeHeader
                at loader_size,   dw 0
		at start_address, dw 0x8000
		at entry_point,   dw EntryPoint
		at stack_point,   dw 0xBFFF
		iend
CodeStart:					; start address is 0x8000
;[]=======================================================================[]
MyFont:						; characters images
		incbin	"font.raw"

;[]=======================================================================[]
; palette from Flex Navigator
CustomPalette:
		db	0x00, 0x00, 0x00, 0x00	; Black		00
		db	0x00, 0x00, 0xFF, 0x00	; B.Red		01
		db	0x00, 0x80, 0x00, 0x00	; Green		02
		db	0x00, 0xFF, 0xFF, 0x00	; B.Yellow	03
		db	0x80, 0x00, 0x00, 0x00	; Blue		04
		db	0xFF, 0xFF, 0x00, 0x00	; Invert bg	05
		db	0x80, 0x00, 0x00, 0x00	; Invert fg	06
		db	0x80, 0x80, 0x80, 0x00	; BlackGray	07
		db	0xC0, 0xC0, 0xC0, 0x00	; HighGray	08
		db	0x80, 0x00, 0x00, 0x00	; Panel		09
		db	0xFF, 0xFF, 0x00, 0x00	; Files		0A
		db	0x00, 0xFF, 0xFF, 0x00	; Select Files	0B
		db	0x00, 0xFF, 0xFF, 0x00	; InvSel Files	0C
		db	0xC0, 0xC0, 0xC0, 0x00	; Reserved	0D
		db	0xFF, 0xFF, 0xFF, 0x00	; White		0E
		db	0xFF, 0xFF, 0xFF, 0x00	; White		0F

;[]=======================================================================[]
; entry point
		align	16
EntryPoint:
		call	SetVideoMode		; set video mode

		ld	hl, TextStr1		; text string
		ld	de, 0x0020		; x position
		ld	bc, 0x0000              ; y position
		ld	a, 0x02			; background color = 0x00
						; foreground color = 0x02
		call	TextOut

		ld	hl, TextStr2		; text string
		ld	de, 0x0020		; x position
		ld	bc, 0x0008		; y position
		ld	a, 0x1F			; background color = 0x01
						; foreground color = 0x0F
		call	TextOut

		ld	c, 0x30			; wait keypressed
		rst	0x10

		call	ResVideoMode		; restore video mode

		ld	bc, 0x0041
		rst	0x10			; return to OS

;[]=======================================================================[]
; set requist video mode 640x256x16
SetVideoMode:
		ld	c, 0x51			; save previos vmode
		rst	0x10
		ld	(vmode + 1), a
		ld	a, b
		ld	(vscrn + 1), a
		sub	a
		call	CrearVideoRam
		ld	bc, 0x0050		; set 640x256x16
		ld	a, 0x82
		rst	0x10
		ld	hl, CustomPalette
		ld	de, 0x1000
		ld	bc, 0xFFA4
		sub	a
		rst	0x08			; load palette
		ret

;[]=======================================================================[]
; restore previos video mode
ResVideoMode:
		sub	a
		call	CrearVideoRam
vscrn:		ld	b, 0x00
vmode:		ld	a, 0x00
		ld	c, 0x50
		rst	0x10			; set previos vmode
		ret

;[]=======================================================================[]
; Clear video memory (first screen)
; Input:
;	a - clear color (0-15)
CrearVideoRam:
		and	0x0F
		ld	e, a
		rlca
		rlca
		rlca
		rlca
		or	e
		ld	e, a
		in	a, (YPORT)		; store modify ports
		ld	c, a
		in	a, (PAGE1)
		ld	b, a
		push	bc
		ld	a, VPAGE
		out	(PAGE1), a
		ld	hl, 0x4000
		ld	bc, 320			; screen x size in bytes
		di
		ld	d, d
		ld	a, 0x00			; set accel lenght to 256 bytes
.loop:		ld	e, e			; fill vertical lines
		ld	(hl), e
		ld	b, b
		inc	hl
		dec	bc
		ld	a, b
		or	c
		jr	nz, .loop
		ei
		pop	bc
		ld	a, b
		out	(PAGE1), a
		ld	a, c
		out	(YPORT), a		; restore modify ports
		ret

;[]=======================================================================[]

		%include "print.z80"

;[]=======================================================================[]
; some text
TextStr1:	db	'Color print with acceleration.', 0
TextStr2:	db	'Written by Enin Anton. (c) Copyright 2002 R-lab', 0

;[]=======================================================================[]

CodeEnd:

