.PAGE
;******************************************************
;*						      *
;*	     SET BASE REGISTER			      *
;*						      *
;*  COPYRIGHT (c) 1982, 1983 SAGE Computer Technology *
;*  All Rights Reserved				      *
;*						      *
;******************************************************

;$X - Set standard base register

CM_BASE
	CLR.L	D0		;Assume no standard base
	MOVE.B	(A0),D0		;Any register specified
	BEQ.S	$1		;No - Reset base counter
	MOVEQ	#MAXBASE-1,D2	;Max data size
	BSR	GETNUM		;Get standard base register
	BMI	CMNDERRA	;Error detected in address/data field
$1
	
	MOVE.W	D0,LOCCNTR	;Save new standard
	ASL.W	#2,D0
	ADD.W	#BASEADDR,D0	;Save new base for display
	MOVE.W	D0,LASTBASE
	RTS
.PAGE
;****************************************
;*					*
;*	       MOVE DATA		*
;*					*
;****************************************

;M [$X+]XXXXXX,[#,$X+]XXXXXX,[$X+]XXXXXX - Move memory

CMM
	BSR	CNVTADDR	;Get starting address
	BNE	CMNDERRA	;Error detected
	MOVE.L	A1,D4		;Save starting address
	BSR	CNVTADDR	;Get source ending addr or tran size
	BMI	CMNDERRA	;Error detected
	EXG	A1,D4		;Swap starting and ending addr
	BNE.S	$1		;address form #XXXXXX
	SUB.L	A1,D4		;Compute transfer size
	BRA.S	$2
$1
	SUBQ.L	#1,D4		;Convert for DBF
$2
	BCS	CMNDERRA	;Error - Source start addr > Source end addr
	MOVEA.L A1,A3		;Set starting address
	BSR	CNVTADDR	;Get destination address
	BNE	CMNDERRA	;Error detected
	LEA	MSG26,A0	;Print Moving memory
	BSR	TERMTEXT
	MOVEQ	#1,D1		;Set forward direction
	CMPA.L	A1,A3		;Copy which direction
	BCC.S	$3		;Copy forward
	ADDA.L	D4,A1		;Destination starts at end
	ADDA.L	D4,A3		;Source starts at end
	MOVEQ	#-1,D1		;Copy backward
$3
	BSR.S	SETBE		;Set bus error
$4
	MOVE.B	(A3),(A1)	;Copy byte
	ADDA.L	D1,A1		;Move to next byte
	ADDA.L	D1,A3		;Move to next byte
	SUBQ.L	#1,D4		;Repeat for all bytes
	BGE	$4
	MOVE.L	A2,BE_TRAP	;Restore bus error trap
	RTS

;Set Bus Error Trap

;	BSR	SETBE
;			A4 : ?
;			A2 : Old value of bus error trap

SETBE
	MOVEA.L BE_TRAP,A2	;Save old bus error
	LEA	BUSERROR,A4	;Set new trap value
	MOVE.L	A4,BE_TRAP
	RTS
	
; Bus error detected

BUSERROR
	LEA	MSG3,A0		;Print error message
	BSR	TERMTEXT
	ADDQ.L	#2,A7		;Skip unneeded stack infow
	MOVE.L	(A7)+,D0	;Print bad memory address
	BSR	PRNTADDR
	MOVE.L	A2,BE_TRAP	;Restore Bus Error vector
	BRA	DEBUG		;Return to debug
.PAGE
;************************************************
;*						*
;*	     EXERCISE COMMANDS			*
;*						*
;************************************************

CME
	LEA	SCME,A1		;Find command in table
	BRA	SRCHCMND

;EWX - Exercise floppy write
CMEW
	MOVE.W	#1,-(A7)	;Set up write flag
	BRA.S	CMER1

;ERX - Exercise floppy read

CMER
	CLR.W	-(A7)		;Read flag
CMER1	MOVEQ	#1,D2		;Get drive number
	BSR	GETNUM
	BMI	CMNDERR		;Drive number in error
	MOVE.W	D0,-(A7)	;Save drive number on stack
	MOVE.L	#0FFFFH,D2	;Get disk block number
	BSR	GETNUM
	BMI	CMNDERR		;Illegal block number
	MOVE.W	D0,-(A7)	;Save block number on stack
	CLR.W	FI_DRV0+FI_BPS	;Force initialization
	CLR.W	FI_DRV1+FI_BPS
	MOVE.B	#1,FT_TEST	;Set up test mode
$10	MOVE.W	(A7),-(A7)	;Block number
	MOVE.L	#400H,-(A7)	;Address
	MOVE.L	#1000H,-(A7)	;Size
	MOVE.W	12.(A7),-(A7)	;Drive number
	TST.W	16.(A7)
	BEQ.S	$12		;Was read
	BSR	FDWRITE		;Do the write
	BRA.S	$13
	
$12	BSR	FDREAD		;Do the read
$13	BNE.S	$30		;Failed
	MOVEQ	#".",D0		;Print period for success
$20	BSR	TERMCHAR
	BSET	#1,FT_TEST	;Don't select any more
	BSR	KEYCHK
	BEQ	$10		;No character
	BSR	KEYBCHAR	;Read keyboard
	MOVE.B	#7,F8255	;Turn off selects
	MOVE.B	#9,F8255
	CLR.B	FT_TEST		;Turn off test mode
	ADDQ.L	#6,A7		;Get rid of data on stack
	RTS

$30	MOVEQ	#"X",D0		;Print X for failure
	BRA	$20


                                                                                                                                                                                                                                                                                   