	title	RSX Map Tool
	name	('RSXSHOW')
	maclib	base80

; This little program is used to display the current dynamic
; memory map of a CP/M Plus (3.x) system. It shows the amount
; of Transient Program Area (TPA) memory that is available.
; Next, each each installed RSX is shown by displaying its
; load address, name and size in pages.

; A sample output might look like

; CP/M Version 3.1                TPA is 47610 bytes
; ==================================================
;  BIOS begins at FC00
; *BIOS entry  at CA7D
;  BDOS begins at F600 and is  6 pages
; *RSX  entry  at E400
; *BDOS entry  at BAFA
; RSX  1) is  at  F300 and is  3 pages named LOADER  
; RSX  2) is  at  E900 and is 10 pages named CPM2    
; RSX  3) is  at  E400 and is  5 pages named PUT     
; ==================================================

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		Tel.: 040/4223247

;		Version 1.0, October 1990

	entry	$memry

	extrn	getver,string,scbfun,scbpb,decout,hexout

_SN	equ	 6		; Serial number of OS
_NXRSX	equ	 9		; Jump to next RSX
_RNAM	equ	16		; Pointer to name of RSX

_JPlen	equ	3

RSXadr	equ	0
RSXnam	equ	RSXadr+2
RSXpag	equ	RSXnam+2
RSXREC	equ	RSXpag+1

_SCBadr	equ	03ah

_OSENT	equ	098h
_RSXENT	equ	0feh

	ld	sp,LocStk	; Load stack
	call	iniheap		; Init heap pointer
	call	getver		; Get OS version
	ld	de,$ILLOS
	jp	c,InvOS
	call	stOSver		; Store OS version
	call	getAdr		; Fetch base addresses
;
; Process OS data
;
	ld	de,$VERS1
	call	string		; Give base info
	ld	hl,(TPAtop)	; Get BDOS entry
	ld	bc,-TPA-_SN
	add	hl,bc		; Fix for serial number
	ld	de,$NUM
	push	de
	ld	b,eot
	call	decout		; Convert free space
	pop	de
	call	string
	ld	de,$VERS3
	call	string
;
; Process BIOS data
;
	ld	de,$BIOSBEG
	ld	hl,(BIOSBEG)
	call	hexout		; Get BIOS start
	ld	hl,(OS+1)	; Get BIOS warm start
	ld	bc,-_JPlen
	add	hl,bc		; Fix for cold start
	ld	de,$BIOSENT
	call	hexout
	ld	de,$VERS5
	call	string
;
; Process BDOS data
;
	ld	de,$BDOSBEG
	ld	hl,(BDOSBEG)
	call	serout		; Get BDOS start
	ld	de,$BDOSRSX
	ld	hl,(BDOSRSX)
	call	serout		; Get RSX start
	ld	hl,(tpatop)	; Get BDOS entry
	ld	de,$BDOSENT
	call	serout
	ld	a,(BDOSBEG+1)	; Get BDOS page
	ld	b,a
	ld	a,(BIOSBEG+1)	; Get BIOS page
	sub	b		; Get pages
	ld	hl,$BDOScnt
	call	unpdec
	ld	de,$VERS6
	call	string
	ld	de,(BDOSRSX)
	ld	hl,(BDOSBEG)
	or	a
	sbc	hl,de		; Test RSX installed
	jr	z,noRSX
;
; Process RSX data
;
	xor	a
	ld	(RSXcnt),a	; Clear RSX count
	ld	(RSXnum),a	; And RSX number
	ld	de,(BDOSRSX)	; Init start of RSX
	call	RSXsample	; Sample RSX
	call	RSXdisp		; Display RSXes
noRSX:
	ld	de,$VERS4
InvOS:
	call	string		; Close text
	jp	OS		; Exit
;
; Convert BDOS vector to hex
;
serout:
	ld	bc,-_SN
	add	hl,bc		; Fix for serial number
	call	hexout		; Get start
	ret
;
; Unpack page size in Accu as ASCII to ^HL
;
unpdec:
	ld	(hl),' '	; Clear first
	inc	hl
	cp	10		; Test size
	jr	c,unp0..9
	dec	hl
	ld	(hl),'0'-1
unploop:
	inc	(hl)		; Advance
	sub	10
	jr	nc,unploop
	add	a,10
	inc	hl
unp0..9:
	add	a,'0'
	ld	(hl),a		; Save digit
	ret
;
; Initialize heap
;
iniheap:
	ld	hl,($memry)	; Get start of heap
	ld	(heap),hl	; Save
	ret
;
; Get heap space for new RSX record into reg IY
;
malloc:
	push	bc
	push	de
	push	hl
	ld	de,(heap)	; Get current heap
	ld	hl,RSXREC
	add	hl,de		; Build new top
	ld	(heap),hl	; Save
	ld	bc,(TPAtop)	; Load top of memory
	or	a
	sbc	hl,bc		; Test space
	push	de		; Get address into reg IX
	pop	iy
	pop	hl
	pop	de
	pop	bc
	ret	c		; Ok
	ld	de,$NOMEM
	call	string		; Tell memory error
	jp	OS		; And exit
;
; Sample RSXes starting at ^DE into memory
;
RSXsample:
	ld	hl,(BDOSBEG)
	or	a
	sbc	hl,de		; Test more RSX installed
	ret	z
	call	malloc		; Get space
	ld	(iy+RSXadr+0),e	; Save our address
	ld	(iy+RSXadr+1),d
	ld	hl,_RNAM-_SN
	add	hl,de
	ld	(iy+RSXnam+0),l	; Get address of name
	ld	(iy+RSXnam+1),h
	push	de
	pop	ix
	ld	a,(ix+_NXRSX+1-_SN+1)
				; Get page of next RSX
	sub	d
	ld	(iy+RSXpag),a	; Save
	ld	hl,RSXcnt
	inc	(hl)		; Advance RSX count
	ld	l,(ix+_NXRSX+1-_SN+0)
				; Get address of next RSX
	ld	d,(ix+_NXRSX+1-_SN+1)
	jr	RSXsample
;
; Display RSXes in chain ^IY
;
RSXdisp:
	ld	hl,RSXnum
	inc	(hl)		; Advance RSX number
	ld	a,(hl)		; Get it
	ld	hl,$RSXnum
	call	unpdec		; And convert it
	ld	l,(iy+RSXadr+0)	; Get RSX entry
	ld	h,(iy+RSXadr+1)
	ld	de,$RSXadr
	call	serout		; Get address
	ld	a,(iy+RSXpag)	; Get page count
	ld	hl,$RSXcnt
	call	unpdec		; And convert it
	ld	l,(iy+RSXnam+0)	; Get address of name
	ld	h,(iy+RSXnam+1)
	ld	de,$RSXnam
	ld	bc,NAMlen
	ldir			; Unpack name
	ld	de,$RSX
	call	string		; Tell it
	ld	de,-RSXREC
	add	iy,de		; Get previous record
	ld	hl,RSXcnt
	dec	(hl)		; Count RSX down
	jr	nz,RSXdisp
	ret
;
; Store OS version in reg L as ASCII
;
stOSver:
	ld	ix,$VERS2
	ld	a,l		; Get hi part of OS
	rrca
	rrca
	rrca
	rrca
	and	LoMask
	add	a,'0'
	ld	(ix+0),a	; Store into header
	ld	a,l
	and	LoMask		; Then get lo part
	add	a,'0'
	ld	(ix+2),a
	ret
;
; Get base address of BDOS and BIOS
;
; It fetches addresses from SCB
;
getAdr:
	ld	a,_SCBadr
	ld	(scbpb),a
	call	scbfun		; Get address of SCB
	ld	l,_RSXENT	; Point to BDOS entry point
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	(BDOSRSX),de
	ld	l,_OSENT	; Point to real BDOS start point
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	(BDOSBEG),de
	ld	l,0
	inc	h		; Get BIOS address
	ld	(BIOSBEG),hl
	ret

	dseg

$ILLOS:
	db	'This will only work under CP/M Plus'
	db	cr,lf,eot
$VERS1:
	db	'CP/M Version '
$VERS2:
	db	'x.x                TPA is ',eot
$VERS3:
	db	' bytes'
	db	cr,lf
$VERS4:
	db	'=================================================='
	db	cr,lf,eot
$VERS5:
	db	' BIOS begins at '
$BIOSBEG:
	db	'xxxx',cr,lf
	db	'*BIOS entry  at '
$BIOSENT:
	db	'xxxx',cr,lf,eot
$VERS6:
	db	' BDOS begins at '
$BDOSBEG:
	db	'xxxx and is '
$BDOScnt:
	db	'dd pages',cr,lf
	db	'*RSX  entry  at '
$BDOSRSX:
	db	'xxxx',cr,lf
	db	'*BDOS entry  at '
$BDOSENT:
	db	'xxxx',cr,lf,eot
$RSX:
	db	'RSX '
$RSXnum:
	db	'dd) is  at  '
$RSXadr:
	db	'xxxx and is '
$RSXcnt:
	db	'dd pages named '
$RSXnam:
	db	'12345678'
NAMlen	equ	$-$RSXnam
	db	cr,lf,eot
$NUM:
	db	'65535',eot
$NOMEM:
	db	'malloc says:: out of memory!'
	db	cr,lf,eot
$memry:
	ds	2
heap:
	ds	2
BDOSRSX:
	ds	2
BDOSBEG:
	ds	2
BIOSBEG:
	ds	2
RSXcnt:
	ds	1
RSXnum:
	ds	1
;
	ds	2*32
LocStk	equ	$

	end

