PAGE 74,97
	TITLE	Daffodil Debugging Monitor Main
; DaffMon.asm
; Last modified July 26, 1983  4:58 PM by Diebert

; This is the main module for the Daffodil Debugging Monitor
; It contians the initializeing code calls as well as all of
; the Data area defs.
	PAGE
	INCLUDE	DaffEQUs.d
	INCLUDE	DaffExtD.d
CodeSegment	SEGMENT	PUBLIC 'CodeSegment'
	ASSUME		CS:CodeSegment
	ASSUME		DS:SEG DataSegment


; Externals from DaffSubs.asm

	EXTRN	ConvertBinary:	NEAR
	EXTRN	GetCmd:	NEAR
	EXTRN	GetNewWord:	NEAR
	EXTRN	LookupRegs:	NEAR
	EXTRN	PrintAddress:	NEAR
	EXTRN	PrintChar:	NEAR
	EXTRN	PrintCRLF:	NEAR
	EXTRN	PrintStr:	NEAR
	EXTRN	ReadWord:	NEAR
	EXTRN	WriteWord:	NEAR

; This area is used for constants to be printed etc.

	PUBLIC	Herald
Herald	LABEL	WORD
	DB	"Welcome to DDM ", CR, LF
HeraldLen	EQU	17

	PUBLIC	Prompt
Prompt	LABEL	WORD
	DB	CR, LF, ">"
PromptLen	EQU	3


;This is the text for LookupRegs

	PUBLIC	RegNameTable
RegNameTable	LABEL	WORD
	DW	"AX"
	DW	"BX"
	DW	"CX"
	DW	"DX"
	DW	"SP"
	DW	"BP"
	DW	"SI"
	DW	"DI"
	DW	"CS"
	DW	"DS"
	DW	"SS"
	DW	"ES"
	DW	"FR"
	DW	"XS"
	DW	"YS"
	DW	"IP"
	DW	"PC"


	PAGE
; Start up DDM

	PUBLIC	DDM
DDM	LABEL	FAR

	CLI		;Reset Interrupts just in case.

	CALL	InitWorld	; Set up world

	MOV	BP, Herald	;Set up for Print of the Welcome Herald
	MOV	CL, HeraldLen
	CALL	PrintStr


Poll:
	MOV	BP, Prompt	;Print prompt
	MOV	CL, PromptLen
	CALL	PrintStr

	CALL	GetCmd		;Build up command string


;Parse command string for address
	CMP	DH, 0		;Check to see if there is anything to parse
	JNE	Poll5		;Something to parse
	JMP	Poll100	;This is to fix the fucking small conditional jump
Poll5:
	CMP	DH, 2		;Check to see if there are only 2 bytes
	JNE	Poll10		;No, there are more or less


;Suspect a register here
	MOV	AX, InStr	;Put first two bytes of string into AX
	CALL	LookupRegs	;See if it is in RegTable sets carry if found
	JNC	Poll10		;Item not in table.


;At this point we have a register.  Set up Reg display mode
	MOV	AL, TargetMode	;set register mode
	AND	AL, 0F0h	;Block off any previous mode stuff
	OR	AL, ModeReg	;Set Register mode
	MOV	TargetMode, AL
	MOV	TargetOffset, SI	;Save register offset in memory
	JMP	Poll100	;Done!


;Not a register Check to see if I/O
Poll10:
	CMP	InStr, "I"	;Check to see if this is an I/O operation
	JNE	Poll30


;At this point we have an I/O op
Poll20:
	MOV	AL, TargetMode	;set register mode
	AND	AL, 0F0h	;Block off any previous mode stuff
	OR	AL, ModeIO	;Set I/O mode
	MOV	TargetMode, AL
	MOV	AX, InStr[1]	;Get string into AX the 2 bytes after the "I"
	CALL	LookupRegs	;See if the string is in the RegTable
	JNC	Poll25		;Not a Reg string


;At this point the IO offset is in a register.  Move register contents to
; the TargetOffset. Remember offset is in BX
	MOV	BP, RegSaveArea
	MOV	AX, [SI+BP]	;Fetch the value
	MOV	TargetOffset, AX
	JMP	Poll100	;Done!


;Now we have a hex value still in the I/O space.  If not print ? and prompt.
Poll25:
	MOV	BP, InStr[1]	;Point to the start of the string
	MOV	CL, StrLen	;Get the string length
	CALL	ConvertBinary	;Convert input string to binary.
	JNC	PrintQuestion	;Take branch if string could not be converted
	MOV	TargetOffset, AX	;Put in the offset
	JMP	Poll100	;Done!


;At this point the value is a memory pair or garbage
Poll30:
	MOV	AL, TargetMode	;Set up for memory mode
	AND	AL, 0Fh	;Mask off the mode info
	OR	AL, ModeMemory
	MOV	TargetMode, AL	;Save it
	MOV	BP, InStr	;Set up to scan string for ":"
	SUB	CX, CX		;Get length
	MOV	CL, StrLen
	SUB	SI, SI		;Clear count
Poll32:
	MOV	AL, [BP+SI]	;Check to see if byte is ":"
	CMP	AL, ":"
	JE	Poll35		;Yes it is a ":"
	INC	SI		;Increment string pointer
	LOOP	Poll32		;loop until end or char found


;At this point we don't have a ":" so value is a hex string offset
	MOV	BP, InStr	;Point to the start of the string
	MOV	CL, StrLen	;Get the string length
	CALL	ConvertBinary	;Convert input string to binary.
	JNC	PrintQuestion	;Take branch if string could not be converted
	MOV	TargetOffset, AX	;Put in the offset
	JMP	Poll100	;Done!


;At this point we have found the ":" and it's offset is in CI
Poll35:
	MOV	AX, SI		;Save location of ":"
	MOV	Temp, AL
	CMP	AL, 0		;See if ":" was the first char
	JLE	PrintQuestion	;if ":" was first it is an error print ? and prompt
	CMP	AL, 3		;Test count of bytes equal to 3
	JNE	Poll37		;Probably not a register designation


;At this point it should be a register
	MOV	AX, InStr	;Get 2 byte string
	CALL	LookupRegs	;See if its valid
	JNC	PrintQuestion	;Not a register so print ? and prompt
	MOV	BP, RegSaveArea
	MOV	AX, [BP+SI]	;Fetch the register save value
	MOV	TargetSeg, AX	;and save it in Seg
	JMP	Poll40


;At this point the value of the string should be a hex constant
Poll37:
	MOV	BP, InStr	;Set ptr to start of string
	MOV	CL, Temp	;Load the count of the byte before the ":"
	DEC	CL
	CALL	ConvertBinary	;Convert the string to binary
	JNC	PrintQuestion	;String could not be converted to bin.
	MOV	TargetSeg, AX	;Save segment value.


;Now go and get the offset from the string
Poll40:
	MOV	BP, InStr	;Set up ptr
	MOV	AL, StrLen	;Get string length
	MOV	BL, Temp	;Get location of ":"
	SUB	AL, BL		;AL now holds the length of the remaining string
	SUB	BH, BH
	ADD	BP, BX		;BP now points to the location of the ":"
	INC	BP		;BP is at ":" + 1
	MOV	Temp, AL	;Save length past ":"

	CMP	AL, 2		;Test to see if next entry is 2 bytes maybe reg
	JNE	Poll45		;Nop

	MOV	AX, [BP]	;Get the 2 bytes
	CALL	LookupRegs	;See if it is a valid register
	JNC	Poll45		;No, check to see if it is a hex value
	MOV	BP, RegSaveArea	;Set up to get register contents
	MOV	AX, [BP+SI]	;Fetch the word
	MOV	TargetOffset, AX	;Put it into TargetOffset
	JMP	Poll100	;Done!


;Here we are with a suspected hex value
Poll45:
; BP still contains the address of the byte after the ":"
	MOV	CL, Temp	;Get length to end of string
	CALL	ConvertBinary	;Convert the string to bin.
	JNC	PrintQuestion	;Not convertable
	MOV	TargetOffset, AX	;Save it off.


;Do command dispatch on DL

Poll100:

;A CR, LF or "↑" at this point does nothing even if the command line contained
; valid stuff.
	CMP	DL, CR
	JE	Poll
	CMP	DL, "↑"
	JE	Poll
	CMP	DL, LF
	JE	Poll

	CMP	DL, "/"	;Check for /
	JE	MemRead

	CMP	DL, "\"	;Check for \
	JE	MemWrite

	CMP	DL, CntlB	;Check for ↑B
	JE	SetBkpt

	CMP	DL, CntlK	;Check for ↑K
	JE	ClearBkpt

	CMP	DL, CntlZ	;Check for ↑Z
	JE	ClearAllBkpt

	CMP	DL, CntlP	;Check for ↑P
	JE	Proceed

	CMP	DL, CntlS	;Check for ↑S
	JE	SingleStep

	CMP	DL, CntlL	;Check for ↑L
	JE	LoadHex

;Having gotten here the command is not recognized and the "?" is printed and
;a new prompt is issued.  The command line is ignored.
PrintQuestion:
	MOV	AL, "?"
	CALL	PrintChar
	JMP	Poll
	PAGE
;Start memory display stuff
MemRead:
	MOV	AL, TargetMode	;Set up for op.
	AND	AL, 0Fh		;Clear read write mode
	OR	AL, ModeReadWrite	;Set read before prompt
	MOV	TargetMode, AL
	JMP	MemOp

MemWrite:
	MOV	AL, TargetMode	;Set up for op.
	AND	AL, 0Fh		;Clear read write mode
	OR	AL, ModeReadOnly	;Set no read before prompt
	MOV	TargetMode, AL


MemOp:
	CALL	PrintAddress		;Print the current target address

	TEST	TargetMode, ModeReadOnly	;Should we read target?
	JNE	MemOp10		;No

	CALL	ReadWord	;Reads into CX

MemOp10:
	CALL	GetNewWord	;Poll input for new word, CR, LF, ↑, \ or /
	JNC	MemOp20	;No new contents

	Call	WriteWord	;Word to write in CX

MemOp20:
	CMP	DL, CR		;Is command a CR
	JE	Poll		;Yes; go back to normal mode and prompt

	CMP	DL, LF		;Is command a LF
	JNE	MemOp30	;No

;Command is a LF
	MOV	AX, TargetOffset 	;Increment TargetOffset
	INC	AX
	INC	AX
	MOV	TargetOffset, AX
	CALL	PrintCRLF	;Print a CR LF string
	JMP	MemOp		;Done

MemOp30:
	CMP	DL, "↑"	;Is command an ↑
	JNE	MemOp40	;no

;Command is a ↑
	MOV	AX, TargetOffset 	; decrement TargetOffset
	DEC	AX
	DEC	AX
	MOV	TargetOffset, AX
	CALL	PrintCRLF	;Print a CR LF string
	JMP	MemOp		;Done

MemOp40:
	CMP	DL, "/"	;Check for "/"
	JNE	MemOp50	;No

;Command is a /
	MOV	AL, " "	;Space on same line.
	CALL	PrintChar
	JMP	MemOp		;Do it all again

MemOp50:
	CMP	DL, "\"	;Check for "\"
	JNE	MemOp60	;No

;Command is a \
	MOV	AL, " "	;Space on same line
	CALL	PrintChar
	JMP	MemOp		;Do it all again

MemOp60:
	JMP	PrintQuestion	;Unknown command; print ? and prompt
	PAGE




		

CodeSegment	ENDS
	END