;
;
;
;    * * * * * * * * * * * * * * * * * * * * * * * * *
;    *                                               *
;    *      Z-80 REMOTE DEBUGGER                     *
;    *                                               *
;    *      ING. G. BECATTINI      		     *
;    *                                               *
;    *      DATA: 7/10/88			     *
;    *      REV.: B                                  *
;    *                                               *
;    *						     *
;    * * * * * * * * * * * * * * * * * * * * * * * * *
;
;
;
;
CR	EQU	13
LF	EQU	10

BOFAZ	EQU	3		;LOCATION WHERE TO FIND THE ADDRESS OF BOFA
EOFAZ	EQU	5		;"	"	"	"	"      EOFA

SIOAD	EQU	44H		;VALUES FOR G10 BOARD
SIOAC	EQU	46H
PIOAD	EQU
PIOAC	EQU	PIOAD+2

RAMST	EQU	A000H-200
STACK	EQU	RAMST+200H


	.ORG	0
	JP	XEBUG
	ORG	38H
	JP	BREAK
XEBUG:	LD	SP,STACK
	CALL	INISIO
	LD	HL,IBUF
	LD	(BUFPOIN),HL
DEBG1:
	CALL	CRLF
	LD	A,">"		;EMETTI >
	CALL	COMOUT
	CALL	GETMSG		;ACCETTA UN MESSAGGIO
	CALL	GCI		;RICERCA COMANDO
	AND	7FH
	CP	'M'
	JP	Z,MMRY		;COMANDO M (MEMORY)
	CP	'G'
	JP	Z,GO		;COMANDO G (GO)
	CP	'D'
	JP	Z,DOWNLD	;DOWNLOAD
	CP	'R'
	JP	Z,REG		;DISPLAY REGISTERS
	CP	'S'		
	JP	Z,SETX		;EDIT
	CP	'I'
	JP	Z,INTELD	;INTEL DUMP
	CP	'B'
	JP	Z,0		;RETURN TO BASIC - (BOOSTRAP)
	CP	'P'
	JP	Z,SENDPRO	;SEND BASIC PROGRAM IN INTEL FORMAT
	JR	DEBG1
;
;	* * DISPLAY REGISTERS
;
REG:	CALL	DISPREG
	JR	DEBG1
;
;
;	* * - MEMORY COMMAND
;
;	Mxxxx 		- 8 ROW DISPLAY
;	Mxxxx-yyyy 	- DISPLAY WITH LIMITS
;	M		- 8 ROW DISPLAY FROM CURRENT ADDRESS
;
MMRY:
	CALL	GC		;CHECK NEXT CHAR
	CP	CR
	JR	Z,DEFAU		;CR=USE DEFAULT ADDRESS
	CALL	RDNUM		;NO, READ ADDRESS
	CP	'-'
	JR	Z,MM
	CP	CR
	JR	Z,DIS8		;DISPLAY 8 ROWS
	JP	DEBG1
DEFAU:
DIS8:	JP	DEBG1
;
;	GC - GET NEXT CHAR WITHOUT INCREMENTING TXA
;
GC:	PUSH	HL
	LD	HL,(TXA)
	LD	A,(HL)
	POP	HL
	RET
;
;	GCI - GET NEXT CHAR - CONVERT IT TO UPPER CASE - INC TXA
;
GCI:	PUSH	HL
	LD	HL,(TXA)
	LD	A,(HL)
	INC	HL
	LD	(TXA),HL
	POP	HL
	RET
;
;	RDNUM - READS FROM TEXT AN HEX NUMBER TO HL
;		ONLY LAST FOUR DIGITS ARE CONSIDERED
;		RETURNS WITH CARRY SET ON FIRST NON ASCII CHAR
;		IF THIS IS = CR THEN ACC=FF
;
RDNUM:
	LD	HL,0	;INIZIA CON VALORE 0
	CALL	RDHEX	;LEGGI UNA CIFRA
	JR	NC,RDNXT
	CP	0DH	;E' UN CAR RET?
	RET	NZ
	LD	A,0FFH
	RET
RDNXT:	ADD	HL,HL	;SHIFTA HL DI 4 A SINISTRA
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	OR	L	;COMBINA CON LA NUOVA CIFRA
	LD	L,A
	CALL	RDHEX	;PRENDI UN'ALTRA CIFRA
	RET	C	;SE CARRY=1 LAVORO FINITO
	JR	RDNXT
;
;	INCTXA
;
INCTXA:	PUSH	HL
	LD	HL,(TXA)
	INC	HL
	LD	(TXA),HL
	POP	HL
	RET
;
MM:	
			;DISPLAY A PAGINA
	LD	B,H
	LD	C,L	;IND. INIZIALE IN BC
	CALL	RDNUM	;IND. FINALE IN HL
	CALL	CRLF
MM1:	LD	A,C	;NECESSARIA INTERLINEA?
	AND	1111B
	JP	NZ,MOREDB	;NO, STAMPA SOLO
	PUSH	BC
	PUSH	HL
	LD	HL,-16
	ADD	HL,BC
	LD	B,16
MASC:	LD	A,(HL)
	CALL	ASCI4
	INC	HL
	DJNZ	MASC
	POP	HL
	POP	BC
	CALL	CRLF
	CALL	DBYTE	;STAMPA INDIRIZZO
	PUSH 	BC
	LD	B,C
	CALL	DBYTE
	CALL	SEP	;E : SPAZIO
	POP 	BC
MOREDB:	LD 	A,(BC)
	PUSH 	BC

	LD	B,A
	CALL	DBYTE	;STAMPA IL DATO
;	CALL	DISPASC	;ANCHE IN ASCII
	CALL	SEP1	;SPAZIO
	POP 	BC
	INC	BC
	LD	A,B
	CP	H
	JP	NC,M2M	;CONTROLLO FINE LAVORO
	JR	MM1
M2M:	LD	A,C
	CP	L
	JP	C,MM1	;CONTROLLO FINE LAVORO
	JP	DEBG1
;---------
;
;
;	* * - INTEL DUMP COMMAND
;
;	Ixxxx-yyyy[/zzzz] xxxx=START ADDR.   yyyy=END ADDR 
;			  zzzz=TRANSLATE ADDRESS TO zzzz
;			  IF /zzzz NOT SPECIFIED, NO TRANSLATION IS DONE
;
INTELD:
	CALL	GC		;CHECK NEXT CHAR
	CP	CR
	JP	Z,DEBG1		;ERROR, RESTART
	CALL	RDNUM		;READ ADDRESS
	CP	'-'
	JR	Z,INTEL3	;TWO ADDRESSES ARE MANDATORY
	JP	DEBG1

SENDPRO:
	LD	HL,(BOFAZ)
	LD	C,(HL)
	INC	HL
	LD	B,(HL)		;GET THE START ADDRESS OF THE PROGRAM
	DEC	BC		;DECREMENT SIX TIMES TO INCLUDE:
	DEC	BC		;MEMTOP
	DEC	BC		;EOFA
	DEC	BC	
	DEC	BC		;BOFA
	DEC	BC
	LD	HL,0		;TRANSLATE PROGRAM LOAD ADDRESS TO 0000
	LD	(TRADD),HL	;TO ALLOW EPROM PROGRAMMING
	LD	HL,(EOFAZ)
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	PUSH	DE		;COPY DE TO HL - END ADDRESS
	POP	HL
	INC	HL		;ONE BYTE MORE
	JR	INTEL5






INTEL3:	LD	B,H
	LD	C,L		;START ADDRESS INTO BC
	LD	(TRADD),HL	;PRESET ADDRESS POINTER = START ADDRESS
	CALL	RDNUM		;END ADDRESS INTO HL\
	CP	'/'		;TRANSLATION REQUIRED?
	JR	NZ,INTEL5

	PUSH	HL
	PUSH	BC
	CALL	RDNUM		;GET TRANSLATE ADDRESS
	LD	(TRADD),HL
	POP	BC
	POP	HL

INTEL5:	XOR	A
	LD	(CHK),A
	LD	(BYTCNT),A	;BYTCNT (BYTES COUNTER) IS USED FOR
				;THE NEW LINE DECISION

	CALL	CRLF
INTEL1:	LD	A,(BYTCNT)	;LINE FEED REQUIRED?
	AND	00001111B
	JP	NZ,MOREINT	;NO, JUST PRINT
	PUSH	BC
	LD	A,(CHK)		;COMPLEMENT AND INCREMENT CHECKSUM
	CPL
	INC	A
	LD	B,A
	CALL	DBYTE		;PRINT CHECKSUM LAST
	XOR	A
	LD	(CHK),A		;AND CLEAR IT
	CALL	CRLF
	LD	A,":"
	CALL	COMOUT
	LD	B,16		;NUMBER OF BYTES TO SEND
	CALL	ACCUMCK
	CALL	DBYTE
	LD	BC,(TRADD)
	CALL	ACCUMCK
	CALL	DBYTE		;PRINT ADDRESS HI
	LD	BC,(TRADD)
	LD	B,C
	CALL	ACCUMCK
	CALL	DBYTE		;PRINT ADDRESS LOW
	LD	B,0
	CALL	DBYTE		;PRINT RECORD TYPE=00
	POP	BC
MOREINT:
	CALL	SENDATA		;SEND A BYTE OF DATA (TWO HEX DIGITS)

	LD	A,B
	CP	H
	JP	NC,INTEL2	;END OF THE JOB?
	JR	INTEL1
INTEL2:	LD	A,C
	CP	L
	JP	C,INTEL1	;END OF THE JOB (2)?

INTEL7:	LD	A,(BYTCNT)	;KEEP ON SENDING DATA TO COMPLETE A
				;16 BYTES RECORD
	AND	00001111B
	JR	Z,INTEL6
	CALL	SENDATA	
	JR	INTEL7

INTEL6:	LD	A,(CHK)
	CPL
	INC	A
	LD	B,A
	CALL	DBYTE		;PRINT LAST CHECKSUM LAST
	JP	DEBG1


ACCUMCK:			;ACCUMULATE CHECKSUM
	LD	A,(CHK)
	ADD	A,B
	LD	(CHK),A
	RET

SENDATA:
	LD 	A,(BC)		;SEND DATA
	PUSH 	BC
	LD	B,A
	CALL	ACCUMCK
	CALL	DBYTE		;PRINT DATA
	POP 	BC
	INC	BC

	PUSH	HL
	LD	HL,(TRADD)
	INC	HL
	LD	(TRADD),HL
	POP	HL

	LD	A,(BYTCNT)
	INC	A
	LD	(BYTCNT),A
	RET
;---------
DISPASC:
	PUSH	AF
	LD	A,"."
	CALL	COMOUT
	POP	AF
ASCI4:	CP	20
	JR	NC,ASCI1
	LD	A,' '
ASCI1:	BIT	7,A
	JR	Z,ASCI2
	LD	A,' '
ASCI2:	CP	'>'
	JR	NZ,ASCI3
	LD	A,'*'
ASCI3:	CALL	COMOUT
	RET
;
;	* * S COMMAND - SET MEMORY OR REGISTER
;
SETX:	CALL	RDNUM	;GET ADDRESS TO EDIT
SETX1:	PUSH	HL
	CALL	CRLF
	LD	B,H
	CALL	DBYTE	;EMETTI PRIMA META' INDIRIZZO
	LD	B,L
	CALL	DBYTE	;EMETTI SECONDA META'
	CALL	SEP	;EMETTI : E SPAZIO
	LD	A,(HL)	;LEGGI IL DATO NELLA LOCAZIONE RICH.
	LD	B,A	;E TRASMETTILO
	CALL	DBYTE
	CALL	DISPASC
	CALL	SEP2	;EMETTI UNO SPAZIO E INVITO A TRASMETTERE
	CALL	GETMSG	;ACCETTA COMANDO
	CALL	RDNUM	;LEGGI COMANDO DA BUFFER
	LD	B,L
	POP	HL
	CP	0FFH	;FF E' UN FLAG CHE DICE
			;CHE IN RDNUM NON SONO STATI
			;BATTUTI DATI MA SUBITO UN CR
	JR	Z,MEM2
	CP	"-"
	JR	Z,DECRE	;ALL'INDIETRO?
	CP	"."
	JR	Z,DEBGX	;FINE COMANDO MEMORY
	LD	A,B
	LD	(HL),A
MEM2:	INC	HL
	JR	SETX1
DECRE:	DEC	HL
	JR	SETX1
DEBGX:	JP	DEBG1
;
SEP:	LD	A,':'
	CALL	COMOUT
SEP1:	LD	A,' '
	CALL	COMOUT
	RET
SEP2:	CALL	SEP1 	;SPAZIO + INVITO A TRASM '>'
	LD	A,'>'
	CALL	COMOUT
	RET


;
;
;	* * COMANDO D - DOWNLOAD DATA 
DOWNLD:
	XOR	A
	LD	(ERR),A		;CLEAR CHECKSUM ERROR FLAG
	CALL	RDNUM		;LEGGI INDIRIZZO DAL BUFFER
	PUSH	HL		;SALVA INDIRIZZO
	XOR	A
	LD	(CHK),A		;CLEAR CHECKSUM
	LD	(BYTCNT),A	;CLEAR BYTES COUNTER
DWN1:	CALL	RDNUM		;READ DATA
	LD	A,(CHK)
	ADD	A,L
	LD	(CHK),A
	LD	A,L
	POP	HL
	LD	(HL),A		;STORE DATA TO ADDRESS
	INC	HL
	PUSH	HL
	LD	A,(BYTCNT)
	INC	A
	LD	(BYTCNT),A
	CP	16
	JR	NZ,DWN1
	CALL	RDNUM		;READ CHECKSUM
	LD	A,(CHK)
	CP	L
	JR	Z,DWN3
	LD	A,1
	LD	(ERR),A
	CALL	CRLF
	LD	HL,LERR
	CALL	STROUT
	CALL	CRLF
DWN3:	POP	HL
	JP	DEBG1
	
;
;	* * COMANDO G - SALTA ALL'INDIRIZZO XXXX
;		        ESEMPIO G123A
;
GO:	CALL	RDNUM
	CP	0FFH
	JR	NZ,GO1		;JUMP IF ARGUMENT IS PRESENT
	LD	SP,(RSP)
	INC	SP
	INC	SP
	LD	HL,(RPC)
	INC	HL
	PUSH	HL
	CALL	RESREG
	RET
	
GO1:	JP	(HL)

BREAK:	LD	(RSP),SP
	CALL	SAVREG
	LD	HL,(RSP)
	DEC	HL
	DEC	HL
	LD	(RSP),HL
	POP	HL		;GET BREAK ADDRESS
	PUSH	HL
	DEC	HL
	LD	(RPC),HL
	CALL	DISPREG
	JP	DEBG1	

DISPREG:
	CALL	CRLF
	LD	HL,RIG1		;HEADERS
	CALL	STROUT	
	CALL	CRLF

	LD	HL,(RAF)
	LD	B,H
	CALL	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PUSH	HL
	POP	AF
	LD	BC,(RBCX)
	LD	DE,(RDEX)
	LD	HL,(RHLX)
	EX	AF,AF'
	EXX
	LD	A,(RI)
	LD	I,A
	LD	HL,(RAF)
	PUSH	HL
	POP	AF
	LD	BC,(RBC)
	LD	DE,(RDE)
	LD	HL,(RHL)
	LD	IX,(RIX)
	LD	IY,(RIY)
	RET

;
;	* * DBYTE -	EMETTE SUL VIDEO IN HEX IL CONTENUTO
;		    	DI B
;
DBYTE:	PUSH	AF	
	LD	A,B
	RRA
	RRA
	RRA	
	RRA
	CALL	CVTAS	;EMISSIONE PRIMA CIFRA HEX
	LD	A,B
	CALL	CVTAS	;EMISSIONE SECONDA CIFRA
	POP	AF
	RET
;
;	STRING OUT - STRING ENDS WITH 80H
;
STROUT:
	LD	A,(HL)
	BIT	7,A
	RET	NZ
	CALL	COMOUT
	INC	HL
	JR	STROUT
;
;
;	* * RDHEX - LEGGE UN CARATTERE ASCII DAL TESTO, GLI
;		    TOGLIE IL BIT PIU' SIGNIFICATIVO E LO ESA-
;		    MINA PER VEDERE SE APPARTIENE AL SET DELLE
;	            CIFRE ESADECIMALI (0-9 A-F). SE SI, VIENE
;		    LASCIATO IL CORRISPONDENTE VALORE BINARIO
;		    IN ACC. ED IL CARRY=0. SE NO, OGNI ALTRO
;		    CARATTERE E' LASCIATO INTATTO ED IL CARRY
;		    E' POSTO AD 1.
;

RDHEX:	CALL	GCI	;LEGGI UN CARATTERE
	AND	7FH	;TOGLI MSB
	CP	'0'	;MENO DI 0 ASCII? (NON CERTO HEX)
	RET	C
	CP	'9'+1	;E' NEL RANGO 0-9?
	JR	C,RDDIG
	CP	'A'	;E' COMPRESO TRA 9 ED A (ASCII)?
	RET	C
	CP	'F'+1	;E' MAGGIORE DI F?
	CCF
	RET	C
	SUB	'A'-10	;TRADUCI A-F
	RET
RDDIG:	SUB	'0'	;TRADUCI '0'..'9'
	RET
;
;	* * CVTAS - CONVERTE UN NUMERO BINARIO CONTENUTO
;		    NEI 4 BIT MENO SIGNIFICATIVI DI ACC
;		    IN UN CARATTERE ASCII CHE NE RAPPRESENTA
;		    IL VALORE IN ASCII E QUINDI LO FA USCIRE
;		    SU VIDEO
;
CVTAS:
	AND	0FH	;TIRA VIA I 4 BIT PIU' SIGNIF.
	ADD	A,'0'	;CONVERTI IN ASCII
	CP	'9'+1	;GUARDA SE ERA 0..9
	JP	C,COMOUT
	ADD	A,'A'-'0'-10 ;CONVERTI A..F
	JP	COMOUT

GETMSG:	PUSH	HL
	LD	HL,IBUF
GETM1:	CALL	RXCHAR
	LD	(HL),A
	INC	HL
	CP	CR
	JR	NZ,GETM1
	LD	HL,IBUF
	LD	(TXA),HL
	POP	HL
	RET

RXCHAR:	CALL	SIORX
	AND	7FH	;TOGLIE MSB
	CP	61H
	JR	C,RDC1	;SE E' MINUSCOLO LO CONVERTE
	SUB	20H
RDC1:	PUSH	BC
	PUSH	DE
	PUSH	HL
	PUSH	AF
;	LD	C,2
;	LD	E,A
;	LD	C,1
;	CALL	SYSTEM
;	CALL	SIOTX
	POP	AF
	POP	HL
	POP	DE
	POP	BC
	RET
;

COMOUT:	PUSH	BC
	PUSH	AF
	PUSH	DE
	PUSH	HL
	CALL	SIOTX
	POP	HL
	POP	DE
	POP	AF
	POP 	BC
	RET

CRLF:	LD	A,LF
	CALL	COMOUT
	LD	A,CR
	JR	COMOUT
;
;
;	INITIALIZE SERIAL COMMUNICATION PORT
;
INISIO:

	LD	HL,TABSIOA
	LD	C,SIOAC
	LD	B,11
	OTIR
	RET
;
;	RX CHAR FROM SERIAL PORT TO ACC
;
SIORX:

	LD	A,5
	OUT	(SIOAC),A
	LD	A,6AH		;RTS
	OUT	(SIOAC),A
RX:	IN	A,(SIOAC)
	BIT	0,A
	JR	Z,RX
	LD	A,5
	OUT	(SIOAC),A
	LD	A,68H		;VIA RTS
	OUT	(SIOAC),A
	IN	A,(SIOAD)
	RET

SIOTX:	PUSH    AF
SIOT1:	IN	A,(SIOAC)
	BIT	2,A		;TEST TX EMPTY
	JR	Z,SIOT1
        POP     AF
	OUT	(SIOAD),A
	RET


;
VERS:	DB	'2.0'		;VERSION #
LERR:	DB	'LINE ERROR',80H
TRADDM:	DB	'TRANSLATE ADDRESS TO ',80H
RIG1:	DB	'A  F  BC   DE   HL   A'' F'' BC''  DE''  HL''  IX   IY   I  SP   PC',80H
;		 00 00 0000 0000 0000 00 00 0000 0000 0000 0000 0000 00 0000 0000
TABSIOA:
	DB	18H,10H		;CHANNEL RESET
				;RESET EXT/STATUS INT
	DB	4,44H		;CLOCK X 16 - NO SYNC - 1 STOP BIT
				;NO PARITY
	DB	3,0E1H		;AUTO ENABLE - RX ENABLE - 8 BIT CHAR
				;
	DB	5,6AH		;8 BIT CHAR - TX ENABLE - RTS
				;
	DB	10H		;RESET EXT/STATUS INT
	DB	1,0		;NO INTERRUPTS
LASTRO:				;LAST ROM LOC
	.ORG	RAMST
BYTCNT:	DS	1
CHK:	DS	1
ERR:	DS	1
TXAº	DÓ	2
OCHANN:	DS	1
RAF:	DS	2
RBC:	DS	2
RDE:	DS	2
RHL:	DS	2
RAFX:	DS	2
RBCX:	DS	2
RDEX:	DS	2
RHLX:	DS	2
RIX:	DS	2
RIY:	DS	2
RSP:	DS	2
RI:	DS	1
RPC:	DS	2
TRADD:	DS	2
BUFPOIN:
	DS	2
IBUF:
	DS	80
	END
