;
;  DOS.MAC - DOS Functions
;
;  DOSVER  FDOS  BDOS  BIOS  BIOSHL  PC@  PC!  ?terminal  conin
;  conout  lstout  ZENV
;

;	DOSVER  ( -- minor major )

	hdr	1,'DOSVER'
dosver:	ld	hl,(dosv)
dosver1:ld	e,h
	xor	a
	ld	h,a
 	ld	d,a
	jp	dpush

;	FDOS  ( DE u -- HL A )

	hdr	1,'FDOS'
fdos:	pop	hl
	ld	a,l
	pop	de
	call	bdoss
	push	hl
	jp	apush

;	BDOS  ( DE u -- A )

	hdr	1,'BDOS'
bdos:	call	docol
	dw	fdos,nip
	dw	exit

;	subroutine for BIOS call

	cseg

biosj:	cp	15		; console or list functions may use CP/M 2
	jp	z,biosj2	; style bios calls (faster than BDOS 50)
	cp	2
	jp	c,biosj1
	cp	6
	jp	c,biosj2
biosj1:	ld	a,(dosv)
	cp	30h
	jp	c,biosj2
	ld	de,biospb
	ld	c,50		; CP/M 3 BDOS 50
	jp	0005h

biosj2:	push	hl		; CP/M 2 bios call
	ld	hl,(biospb+1)
	ld	c,l
	ld	b,h
	ld	hl,(0000h+1)
	dec	de		; fn
	add	hl,de
	add	hl,de
	add	hl,de
	pop	de
	jp	(hl)

;	BIOS  ( BC u -- A )

	hdr	1,'BIOS'
bios:	pop	de
	ld	a,e
	ld	(biospb),a
	pop	hl
	ld	(biospb+1),hl
	push	bc
	call	biosj
	pop	bc
	jp	apush

;	BIOSHL  ( DE BC u -- HL )

	hdr	1,'BIOSHL'
biosh:	pop	de
	ld	a,e
	ld	(biospb),a
	pop	hl
	ld	(biospb+1),hl
	pop	hl
	ld	(biospb+3),hl
	push	bc
	call	biosj
	pop	bc
	jp	hpush

; Port fetch and store instructions use self-modifying code

;	PC@  ( p-addr -- x )

	hdr	1,'PC@'		; FIG P@
pcat:	pop	hl
	ld	a,l
	ld	(pcat1),a
	in	a,(0)		; modified
pcat1	equ	$-1
	jp	apush

;	PC!  ( x p-addr -- )

	hdr	1,'PC!'		; FIG P!
pcsto:	pop	hl
	pop	de
	ld	a,l
	ld	(pcsto1),a
	ld	a,e
	out	(0),a		; modified
pcsto1	equ	$-1
	jp	next

;	?terminal  ( -- flag )	0 2 bios

	hdr	x,'?TERMINAL'
qterm:	call	docol
	dw	zero,two
	dw	bios
	dw	exit

;	conin  ( -- char )	begin key? until 0 3 bios

	hdr	x,'CONIN'
conin:	call	docol		; console input
conin1:	dw	keyq
	dw	zbran,conin1
	dw	zero,three
	dw	bios
	dw	exit

;	conout  ( char -- )

	hdr	x,'CONOUT'
conout:	call	docol
	dw	clit
	db	4
	dw	bios,drop
	dw	exit

;	lstout  ( char -- )

	hdr	x,'LSTOUT'
lstout:	call	docol
	dw	clit
	db	5
	dw	bios,drop
	dw	exit

;	ZENV  ( -- addr )	return Z-System address (0=none)

	hdr	1,'ZENV'
zenv:	ld	hl,(z3eadr)
	jp	hpush

;	end

