.PAGE
;******************************************************
;*						      *
;*		Disassembler Commands		      *
;*						      *
;*  COPYRIGHT (c) 1982, 1983 SAGE Computer Technology *
;*  All Rights Reserved				      *
;*						      *
;******************************************************

CMA
	LEA	SCMA,A1		;Find command in table
	BRA	SRCHCMND

;AR XXXXXXXX,XXXXXXXX - Compute sum and difference

CMAR
	BSR	GETDATA		;Get first operand
	BNE	CMNDERRA	;Illegal operand
	MOVE.L	D0,D5		;Save first operand
	BSR	GETDATA		;Get second operand
	BNE	CMNDERRA	;Illegal operand
	MOVE.L	D0,D4		;Save second operand
	ADD.L	D5,D0		;Display sum
	BSR	TERMCRLF
	MOVE.L	D0,D1
	MOVE.B	#"+",D0
	BSR.S	$10
	MOVE.L	D5,D1		;Display difference
	SUB.L	D4,D1
	MOVE.B	#"-",D0
	BSR.S	$10
	MOVE.L	D5,D1		;Display product
	MULU	D4,D1
	MOVE.B	#"*",D0
	BSR.S	$10
	TST.W	D4		;Divide by zero?
	BEQ.S	$1		;Yes skip operation
	MOVE.L	D5,D1
	DIVU	D4,D1
	MOVE.B	#"/",D0
	BSR.S	$5		;Print quotient
	BSR.S	$15
	MOVE.B	#"r",D0
	BSR	TERMCHAR
	MOVE.B	#"e",D0
	BSR	TERMCHAR
	MOVE.B	#"m",D0
	SWAP	D1
$5
	BSR	TERMCHAR	;Print remainder
	BSR	TERMCOLN
	MOVE.W	D1,D0
	BRA	TERMHEXW	;Print answer and return
$10
	BSR	TERMCHAR
	BSR	TERMCOLN
	MOVE.L	D1,D0
	BSR	TERMHEXL
$15
	BSR	TERMSPAC
	BRA	TERMSPAC
$1
	RTS

;AD [$X+]XXXXXX [$X,#]XXXXXX
; - Disassemble memory starting at arg 1 until arg 2

CMAD:
	MOVEQ	#10H,D6		;Assume print 16 lines
	BSR	SKIPSPAC	;Move to first nonspace
	MOVE.L	LASTMEM,D4	;Get old memory
	MOVEA.W LASTBASE,A4	;Get old base register
	MOVE.B	(A0),D0		;Any characters there
	BEQ.S	$02		;No input
	BSR	CNVTADDR	;Decode first argument
	BNE	CMNDERRA	;Error in command
				;Error - # not legal for 1st arg
	MOVE.L	A1,D4		;Save address register
	MOVEA.L A2,A4		;Save base register offset
	MOVE.W	A2,LASTBASE	;Set current base register
$02
	BSR	SKIPSPAC	;Move to next command group
	MOVEA.W #20,A1		;Set number of bytes to print
	MOVE.B	(A0),D0		;End of line reached
	BEQ.S	$04
	MOVEQ	#-1,D6		;Assume print address range
	BSR	CNVTADDR	;Get ending address
	BMI	CMNDERRA	;Illegal number
	BEQ.S	$05		;Number not #
$04
	MOVE.L	A1,D6		;Set repeat count
	BMI	CMNDERRA	;Positive numbers only
$05
	ADDQ.L	#1,A1		;Compute end of transfer
	MOVE.L	A1,D5
	MOVEA.L D4,A6		;Instruction address
	
;Print instruction address

$100
	BSR	TERMCRLF	;Start at beginning of link
	MOVE.L	A6,D0		;Print absolute address
	BSR	TERMHEXL
	CMPA.W	#BASEADDR,A4	;Need to print relative?
	BEQ.S	$06
	BSR	TERMSPAC
	MOVE.L	A6,D0		;Print relative address
	SUB.L	(A4),D0
	BSR	TERMHEXL
$06	BSR	TERMCOLN	;Delineate address
	
	MOVEM.L D5/D6,-(A7)	;Save ending location
	BSR.S	DISASM		;Dissamble location
	MOVEM.L (A7)+,D5/D6	;Restore ending location

;Check for completion 
	
	MOVE.L	A6,LASTMEM	;Save next memory address
	TST.L	D6		;Count or Max address
	BMI.S	$110		;Use address range
	SUBQ.L	#1,D6
	BEQ.S	$6		;Print done
	MOVE.L	A6,D5
$110
	CMP.L	A6,D5		;Reached ending location
	BCS	$6		;Done
	BSR	TESTTERM	;Any characters waiting
	BEQ	$100		;No - continue with next line
$5
	BSR	KEYBCH		;Get awaiting char
	CMPI.B	#CNTRLS,D0	;Break character
	BEQ	$5		;Yes - await next char
	CMPI.B	#CNTRLC,D0	;Escape character?
	BNE	$100		;No - print next line
$6
	RTS
.PAGE
DISASM
	MOVE.W	A6,D0		;Address on word boundy
	LSR.B	#1,D0
	BCS	CMNDERRA	;No - report error
	MOVEQ	#7,D6		;Set column address
	BSR	GETNEXTW	;Get instuction
	MOVE.W	D0,D7		;Save it
	ROL.W	#4,D0		;Jump to instuction type subroutine
	BSR	COMPGOTO	
$2000	.WORD	IMED-$2000	;Bit/MOVEP/Immediate
	.WORD	MOVB-$2000	;Move byte
	.WORD	MOVL-$2000	;Move long
	.WORD	MOVW-$2000	;Move word
	.WORD	MISC-$2000	;Miscellaneous
	.WORD	QUIC-$2000	;ADDQ/SUBQ/Scc/DBcc
	.WORD	BRAS-$2000	;Bcc
	.WORD	MOVQ-$2000	;MOVEQ
	.WORD	ODSI-$2000	;OR/DIV/SBCD
	.WORD	SUBS-$2000	;SUB/SUBX
	.WORD	IERROR-$2000	;(Unassigned)
	.WORD	CMPEOR-$2000	;CMP/EOR
	.WORD	AMAEI-$2000	;AND/MUL/ABCD/EXG
	.WORD	ADDS-$2000	;ADD/ADDX
	.WORD	SHFROT-$2000	;Shift/Rotate
	.WORD	IERROR-$2000	;(Unassigned)

;0000 - BIT/MOVEP/Immediate

IMED
	BTST	#8,D7		;Imediate = 0
	BNE.S	$110		;Not imediate
	LEA	INSTRI,A1	;Print instruction
	MOVE.W	D7,D1		;Get instuction
	LSR.W	#8,D1
	LSR.W	#1,D1
	BSR	PRTINST		;Prints instruction
	SUBQ.B	#4,D1		;ORI or ANDI
	BLE.S	$115		;Process it
	SUBI.B	#16.,D1		;EORI
	BEQ.S	$115		;Yes - Process it
	ADDQ.B	#4,D1		;Bit function?
	BNE.S	$101		;No
	CLR.W	D5		;Set to byte size operation
	BSR.S	$108		;Print Bit function type
	BRA.S	$102		;Skip size print
$101
	MOVE.W	D7,D5		;Print size
	BSR	PRTOSIZE	;Prints instruction size & skips to op field
$102
	BSR	NXTFIELD	;Skip to operand field
	BSR	PRTCONST	;Print immediate operand 
$105
	MOVE.B	D7,D3		;Print effective address
	BSR	TERMCOMA	;Print delimiter
	BSR	SPRTEADD
	RTS			;Continue on to next instruction

;Print Bit Function type

$108
	CLR.L	D5		;Set operand size to byte
	LEA	BITFUNC,A1	;Print bit function type
	MOVE.W	D7,D1		;Set bit function type
	ROR.W	#6,D1
	ANDI.W	#3,D1
	BRA	PRTINST		;Print type

$110
	MOVE.W	D7,D0		;Bit or MOVEP instruction?
	ANDI.B	#38H,D0
	CMPI.B	#08H,D0
	BEQ.S	$120		;MOVEP instruction
	MOVEQ	#"B",D0		;Print instruction
	BSR	PRTRINST
	BSR	$108		;Print bit function type
	BSR	NXTFIELD	;Skip to operand field
	MOVE.W	D7,D3		;Set up count register for print
	ROR.W	#6,D3
	BSR	DRD		;Print shift count register
	BRA	$105
$115
	MOVE.W	D7,D0		;Imediate move 
	ANDI.B	#3FH,D0
	CMPI.B	#3CH,D0
	BNE	$101		;No continue normal processing
	CLR.L	D2		;Set direction to to
	MOVE.L	FCCR,D3		;Assume CCR register
	BTST	#6,D7		;CCR or SR register
	BEQ.S	$116		;Guessed right
	MOVE.L	FSR,D3		;Must be SR register
$116
	BRA	M411		;Process special registers
$120
	MOVE.L	FMOVE,D0	;Print MOVEP instruction
	BSR	PRTRINST
	MOVEQ	#"P",D0
	BSR	PRTRINST
	MOVE.W	D7,D5		;Print size
	ROL.W	#1,D5		;Convert to standard size
	BSET	#6,D5
	BSR	PRTSIZE
	BSR	NXTFIELD	;Move to operand field
	BTST	#7,D7		;To or from memory?
	BEQ.S	$121		;From memory
	BSR.S	$130		;Print data register
	BSR	TERMCOMA
	BSR.S	$140		;Print memory address
	RTS			;Continue to next instruction
$121
	BSR.S	$140		;Print memory address
	BSR	TERMCOMA
	BSR.S	$130		;Print data register
	RTS			;Continue to next instruction

;Print data register for MOVEP

$130
	MOVE.W	D7,D3		;Print register
	ROR.W	#6,D3		;Position register for print
	BRA	DRD		;Prints D regster & returns

;Prints address with displacement for MOVEP

$140
	MOVE.W	D7,D3		;Print effective address
	BSET	#5,D3		;Alter effective add to d(Ax)
	BRA	SPRTEADD	;Print effective address and return
.PAGE
;0001 - Move byte

MOVB
	CLR.L	D5		;Set size to byte
	BRA.S	MOVL1

;0011 - Move word

MOVW
	MOVEQ	#40H,D5		;Set size to word
	BRA.S	MOVL1

;0010 -	 Move long

MOVL
	MOVEQ	#-1,D5		;Set size to long word
MOVL1
	MOVE.L	FMOVE,D0	;Print MOVE instruction
	BSR	PRTRINST
	MOVE.W	D7,D0		;MOVE or MOVEA instruction
	ROR.W	#6,D0		;Look at destination mode
	ANDI.B	#7,D0
	SUBQ.B	#1,D0		;Mode 1 = MOVEA
	BNE.S	$1		;MOVE instruction
	MOVEQ	#"A",D0		;Print A of MOVEA
	BSR	PRTRINST
$1
	BSR	PRTSIZE
	BSR	NXTFIELD	;Move to operand field
	MOVE.W	D7,D3		
	BSR	SPRTEADD	;Print source address field
	BSR	TERMCOMA	;Print operand delimeter (comma)
	MOVE.W	D7,D3		
	ROR.W	#6,D3		;Set destination field
	BSR	DPRTEADD	;Print destination address field
	RTS

;0100 - Miscellaneous

MISC
	BTST	#8,D7		;CHK or LEA commands?
	BNE	M900		;Yes
	MOVE.W	D7,D0		;Jump to misc subgroups
	ROL.W	#7.,D0
	ANDI.W	#7,D0
	MOVE.W	D0,D1		;Save instuction number
	BSR	COMPGOTO	;Branch
$2000	.WORD	M100-$2000	;NEGX / MOVE from SR
	.WORD	M200-$2000	;CLR
	.WORD	M300-$2000	;NEG / MOVE to CCR
	.WORD	M400-$2000	;NOT / MOVE to SR
	.WORD	M500-$2000	;NBCD PEA SWAP MOVEM to EA EXTW EXTL
	.WORD	M600-$2000	;TST TAS ILLEGAL
	.WORD	M700-$2000	;MOVEM to EA
	.WORD	M800-$2000	;Special subgroup of misc command
	
;NEGX / MOVE from SR

M100
	MOVEQ	#1,D2		;Set from flag
	BRA.S	M401		;Complete setup
	
;NEG / MOVE to CCR

M300
	MOVE.L	FCCR,D3		;Set operand
	CLR.L	D2		;Set to flag
	BRA.S	M402
	
;NOT / MOVE to SR

M400
	CLR.L	D2		;Set to flag
M401
	MOVE.L	FSR,D3		;Set operand
M402
	MOVE.L	FMOVE,D4	;Set special command to move
M403
	MOVE.W	D7,D0		;Special move command
	ANDI.B	#0C0H,D0
	CMPI.B	#0C0H,D0
	BEQ.S	M410		;Yes - process it
M200
	LEA	INSTMIS1,A1	;Print instuction
	BSR	PRTINST
	MOVE.W	D7,D5		;Print size modifier
M210
	BSR	PRTOSIZE
	BSR	NXTFIELD	;Skip to operand field
	BSR	QPRTEADD	;Print data field
	RTS
	
;Special move instructions

M410
	MOVE.L	D4,D0		;Print instuction
	BSR	PRTRINST
M411
	MOVEQ	#40H,D5		;Set operation size to word
	BSR	NXTFIELD	;Move to operand field
	MOVE.L	D3,D4		;Save other print flag
	TST.B	D2		;To or From operation
	BNE.S	$420		;From
	BSR	QPRTEADD	;Print source operand
	TST.L	D4		;Any thing else to print
	BEQ.S	$415		;No leave routine
	BSR	TERMCOMA
$412
	MOVE.L	D4,D0		;Print other operand
	BSR	PRTRINST	;Print destination operand
$415
	RTS
$420
	BSR	$412		;Print source special operand
	BSR	TERMCOMA
	BSR	QPRTEADD	;Print destination operand
	RTS
	
;NBCD / PEA / SWAP / MOVEM to EA / EXTW / EXTL

M500
	MOVE.W	D7,D1		;Compute EA mode
	ROR.W	#3,D1
	ANDI.W	#7,D1
	BTST	#7,D7		;NBCD or PEA or SWAP?
	BNE.S	M510		;No
	MOVE.L	FNBCD,D0	;Assume NBCD
	BTST	#6,D7		;NBCD
	BEQ.S	M505		;Yes
	MOVE.L	FPEA,D0		;Assume PEA
	TST.B	D1		;Mode = 0?
	BNE.S	M505		;Yes - PEA
	MOVE.L	FSWAP,D0
M505
	BSR	PRTRINST	;Print instuction
	BSR	NXTFIELD	;Move to next field
	BSR	QPRTEADD	;Print operand
	RTS
M510
	TST.B	D1		;Mode = 0?
	BEQ.S	M520		;Yes - process EXTW / EXTL / TST

;MOVEM

M700
	MOVE.L	FMOVE,D0	;Print MOVEM instuction
	BSR	PRTRINST
	MOVEQ	#"M",D0		;Assume MOVEML
	MOVE.W	D7,D5		;Compute size of transfer
	ROL.W	#1,D5
	BSET	#6,D5
	BSR	PRTRINST	;Complete instuction
	BSR	PRTSIZE
	BSR	NXTFIELD	;Move to operand field
	MOVEQ	#40H,D5		;Force word size
	BSR	GETNEXTW	;Get register definition
	MOVE.W	D0,CMNDLINE	;Save register definition
	BTST	#10.,D7		;Registers To / From EA
	BNE.S	$720		;From
	BSR	$718		;Print register mask
	BSR	TERMCOMA
$715
	BSR	QPRTEADD
	RTS
$718
	MOVEQ	#"#",D0		;Print registers moved
	BSR	TERMCHAR
	MOVE.W	CMNDLINE,D0
	BSR	TERMHEXW
	RTS
$720
	BSR	$715		;Print effective address
	BSR	TERMCOMA
	BSR	$718
	RTS
M520
	MOVE.L	FEXTW,D0	;Assume EXTW
	BTST	#6,D7		;Which is it
	BEQ	M505		;Guessed write (hard to believe huh)
	MOVE.L	FEXTL,D0	;Then it must be EXTL
	BRA	M505
	
;TST / TAS / ILLEGAL

M600
	CMPI.B	#0FCH,D7	;ILLEGAL instruction
	BNE.S	$610		;No
	MOVE.L	FILLE,D0	;Print instruction
	BSR	PRTRINST
	MOVE.L	FGAL,D0
	BSR	PRTRINST
	RTS
$610
	MOVE.W	D7,D5		;TAS or TST inst?
	ANDI.W	#0C0H,D5
	CMPI.W	#0C0H,D5
	BEQ.S	$620		;TAS
	MOVE.W	D5,D0		;Convert to standard size designator
	ROR.W	#1,D0
	OR.W	D0,D5
	BRA	M200		;Process instruction
$620
	MOVE.L	FTAS,D4		;Print TAS instruction
	CLR.L	D2
	CLR.L	D3
	BRA	M403		;Process

;Special misc instruction

M800
	BTST	#7,D7		;JSR or JMP
	BNE	$870		;Yes
	MOVE.W	D7,D1		;Print instruction
	ROR.W	#3.,D1
	ANDI.B	#7,D1
	MOVE.B	D1,D2		;Save work
	CMPI.B	#6,D1		;RESET, NOP, STOP, RTE, RTS, TRAPV, RTR
	BEQ.S	$850		;Yes
	CMPI.B	#3,D1		;TRAP, LINK, UNLK
	BLE.S	$805		;Yes
	MOVE.L	FUSP,D3		;Setup for MOVE USP if needed
	ANDI.W	#1,D2		;Set direction
	ANDI.W	#0FH,D7		;Convert for move calc
	ORI.W	#0C8H,D7
	BRA	M402		;Print move inst
$805
	LEA	INSTMIS2,A1	;Print instruction
	BSR	PRTINST
	BSR	NXTFIELD	;Move to operand field
	LSR.B	#1,D2		;TRAP instruction
	BNE.S	$810		;No
	MOVEQ	#"#",D0		;Print const desg
	BSR	TERMCHAR
	MOVE.W	D7,D0		;Print trap vector
	BSR	TERMDIGT
	RTS
$810
	MOVE.W	D7,D3		;Print address register
	ROL.W	#3,D3
	BSR	ARD
	BTST	#3,D7		;Link instruction?
	BNE.S	$815		;No
	BSR	TERMCOMA
	MOVEQ	#40H,D5		;Size is word
	BSR	PRTCONST
$815
	RTS
$850
	MOVE.W	D7,D1		;Print instruction
	ANDI.W	#7,D1
	MOVE.W	D1,D2		;Save work
	LEA	INSTMIS3,A1	;Print instruction
	BSR	PRTINST
	CMPI.B	#2,D2		;STOP inst?
	BNE.S	$853		;No
	BSR	NXTFIELD
	MOVEQ	#40H,D5		;Print word
	BSR	PRTCONST
$853
	MOVEQ	#"T",D0		;Assume RESET
	TST.B	D2
	BEQ.S	$851		;RESET instruction
	MOVEQ	#"V",D0		;Assume TRAPV
	CMPI.B	#6,D2		;TRAPV instruction
	BNE.S	$852		;No
$851
	BSR	TERMCHAR	;Print extention opcode
$852
	RTS

;JSR / JMP 

$870
	CLR.L	D0		;Set use first two table entries
	BRA.S	M905

;CHK / LEA

M900
	MOVEQ	#2,D0		;Use second two table entries
M905
	MOVE.W	D7,D1		;Print instruction
	ROR.W	#6,D1
	AND.W	#1,D1
	ADD.W	D0,D1
M906
	MOVEQ	#40H,D5		;Set operation size to word
	LEA	INSTMIS4,A1	;Print instruction
	BSR	PRTINST
	BSR	NXTFIELD	;Move to operand field
	BSR	QPRTEADD
	BTST	#8,D7		;JMP or JSR?
	BEQ.S	$910		;Yes
	BSR	TERMCOMA
	MOVE.W	D7,D3		;Print rest of argument
	ROL.W	#7,D3		;Position register number
	MOVEQ	#20H,D0		;Value to compute EA for CHK
	BTST	#6,D7		;CHK or LEA
	BEQ.S	$907		;CHK
	MOVEQ	#28H,D0		;Value to compute EA for LEA
$907
	EOR.W	D0,D3		;Calculate EA
	BSR	SPRTEADD
$910
	RTS
	
;0101 - ADDQ/SUBQ/Scc/DBcc

QUIC
	MOVE.W	D7,D1		;(ADDQ / SUBQ) or (Scc / DBcc)
	ANDI.B	#0C0H,D1
	CMPI.B	#0C0H,D1
	BEQ.S	$100		;Scc / DBcc
	MOVE.L	FADDQ,D0	;Assume ADDQ
	BTST	#8,D7		;ADDQ or SUBQ
	BEQ.S	$10		;ADDQ
	MOVE.L	FSUBQ,D0	;Set SUBQ
$10
	BSR	PRTRINST	;Print instruction
	MOVE.W	D7,D5		;Print size
	BSR	PRTOSIZE
	BSR	NXTFIELD	;Move to operand field
	MOVEQ	#"#",D0		;Print constant designator
	BSR	TERMCHAR
	MOVE.W	D7,D0		;Print constant
	ROL.W	#7,D0		;Convert 0 to 8
	ANDI.B	#7,D0
	TST.B	D0		;Value a 0
	BNE.S	$20		;No
	MOVEQ	#8,D0
$20
	BSR	TERMDIGT	;Print constant
	BSR	TERMCOMA
	BSR	QPRTEADD	;Print effective address
	RTS
$100
	MOVEQ	#"S",D0		;Assume Scc
	MOVE.W	D7,D4		;Scc or DBcc
	ANDI.B	#38H,D4
	CMPI.B	#08H,D4
	BNE.S	$110		;Scc
	BCLR	#3,D7		;Force for data access
	MOVE.W	#"DB",D0	;DBcc
$110
	BSR.S	PRTCOND		;Print condition code
	BSR	QPRTEADD	;Print effective address
	CMPI.B	#08H,D4		;DBcc
	BNE.S	$120		;No - skip address print
	BSR	TERMCOMA
	MOVEQ	#40H,D5		;Print address size of word
	BSR	PRTADDI		;Print address
	MOVEA.W D0,A2		;Print physical address
	BSR	PRELADDR
$120
	RTS

;Print condition code

PRTCOND
	LEA	CONDCODE,A1	;Print condition code
PRTCONDA
	BSR	PRTRINST	;Print instruction
	MOVE.W	D7,D1		;Calc condition code
	ROR.W	#7,D1
	ANDI.L	#1EH,D1
	ADDA.L	D1,A1		;Compute address of table
	CLR.L	D0
	MOVE.W	(A1),D0
	BSR	PRTRINST
	BSR	NXTFIELD	;Move to operand field
	RTS

;0110 - Bcc/BSR

BRAS
	MOVEQ	#40H,D5		;Set size to word
	MOVE.W	D7,D1		;Bcc or BSR
	ANDI.W	#0F00H,D1	;Test codition code
	CMPI.W	#100H,D1
	BEQ.S	$10		;BSR
	MOVEQ	#"B",D0		;Print branch instruction
	LEA	BRACOND,A1	;Branch condition codes
	BSR	PRTCONDA
	BRA.S	$20
$10
	MOVE.L	FBSR,D0		;Print BSR instruction
	BSR	PRTRINST
	BSR	NXTFIELD	;Move to next field
$20
	MOVE.W	D7,D0		;Print jump address
	TST.B	D0		;8 or 16 bit displacement
	BEQ.S	$30		;Print 16 bit displacement
	BSR	TERMHEXB	;Print 8 bit displacement
	ROL.W	#8,D0		;Convert to 16 bit offset
	ASR.W	#8,D0
	ADDQ.L	#2,A6
	BSR.S	$40		;Print physical address
	SUBQ.L	#2,A6
	RTS
$30
	BSR	PRTADDI		;Print 16 bit displacement
$40	
	MOVEA.W D0,A2		;Print physical address
	BSR	PRELADDR
	RTS
	
;0111 - MOVEQ

MOVQ
	BTST	#8,D7		;Real MOVEQ instruction
	BNE	IERROR		;No illegal instruction
	MOVE.L	FMOVE,D0	;Print instruction
	BSR	PRTRINST
	MOVEQ	#"Q",D0
	BSR	PRTRINST
	BSR	NXTFIELD	;Move to operand field
	MOVEQ	#"#",D0		;Print constant desginator
	BSR	TERMCHAR
	MOVE.B	D7,D0		;Print constant
	BSR	TERMHEXB
	BSR	TERMCOMA
	MOVE.W	D7,D3		;Print data register
	ROR.W	#6,D3
	BSR	DRD
	RTS

  