;***********************************************************************
;*                                                                     *
;* MONITOR 8051  Configuration                                         *
;* Version 2.9                                                         *
;*                                                                     *
;* COPYRIGHT 1987-1995  KEIL ELEKTRONIK GmbH and KEIL SOFTWARE, INC.   *
;*                                                                     *
;* written by:     RK  11.06.87                                        *
;* updated by:     RK  23.10.89                                        *
;* updated by:     HS  02.09.91                                        *
;* updated by:     HS  11.04.95                                        *
;*                                                                     *
;***********************************************************************

		NAME	CONFIG

PUBLIC	INCHAR		; CHARACTER INPUT-ROUTINE 
PUBLIC	OUTCHAR		; CHARACTER OUTPUT-ROUTINE 
PUBLIC	INSTAT		; INPUT STATUS OF SERIAL INTERFACE
PUBLIC	OUTSTAT		; OUTPUT STATUS OF SERIAL INTERFACE
PUBLIC  CLR_TI		; CLEAR SERIAL TRANSMIT INTERRUPT FLAG
PUBLIC  SET_TI		; SET SERIAL TRANSMIT INTERRUPT FLAG
PUBLIC  CLR_RI		; CLEAR SERIAL RECEIVE INTERRUPT FLAG
PUBLIC  CLR_SER_IE	; CLEAR SERIAL INTERRUPT ENABLE FLAG
PUBLIC  SET_SER_IE	; SET SERIAL INTERRUPT ENABLE FLAG
PUBLIC	SER_INT_ADR	; ADDRESS OF SERIAL INTERRUPT VECTOR
PUBLIC	SER_INT_ADR_OFF	; ADDRESS OF SERIAL INTERRUPT VECTOR + INT_ADR_OFF
PUBLIC	BEFORE_GO	; DO SOME THINGS BEFORE STARTING USER PROGRAM
PUBLIC	AFTER_GO	; DO SOME THINGS AFTER TERMINATION OF USER PROGRAM

EXTRN CODE (MON51)	; START OF MONITOR-51
EXTRN CODE (SER_ISR)	; SERIAL INTERRUPT SERVICE ROUTINE

INT_ADR_OFF	EQU	8000H	; INTERRUPT VECTOR OFFSET IF MONITOR
				; IS INSTALLED AT ADDRESS 0000H
INITSEG		SEGMENT CODE
		RSEG	INITSEG

		LJMP	InitSerial
		LJMP	INT_ADR_OFF + 03H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 0BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 13H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 1BH
		NOP
		NOP
		NOP
		NOP
		NOP
$IF ((SERIAL = 3) OR (SERIAL = 5))
		LJMP	INT_ADR_OFF + 23H
$ELSE
		LJMP	SER_ISR
$ENDIF
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 2BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 33H
		NOP
		NOP
		NOP
		NOP
		NOP
$IF (SERIAL = 5)
		LJMP	SER_ISR
$ELSE
		LJMP	INT_ADR_OFF + 3BH
$ENDIF
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 43H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 4BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 53H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 5BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 63H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 6BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 73H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 7BH
		NOP
		NOP
		NOP
		NOP
		NOP
$IF (SERIAL = 3)
		LJMP	SER_ISR
$ELSE
		LJMP	INT_ADR_OFF + 83H
$ENDIF
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 8BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 93H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 9BH
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 0A3H
		NOP
		NOP
		NOP
		NOP
		NOP
		LJMP	INT_ADR_OFF + 0ABH


;********************************************************************
;* This Macro checks whether a PROM or a RAM is at Address 0.       *
;* If a PROM is detected -> Jump to Program in PROM (Address 0)     *
;* If a RAM  is detected -> Jump to Monitor                         *
;********************************************************************
PROMCHECK	MACRO
$IF (PROMCHECK)
		USING	0

		MOV	DPTR,#0			; IF EPROM THEN JMP 0
		CLR	A
		MOVC	A,@A+DPTR
		MOV	R1,A
		CPL	A
		MOVX	@DPTR,A
		CLR	A
		MOVC	A,@A+DPTR
		CPL	A
		CJNE	A,AR1,JMP0
		MOVX	@DPTR,A
$ENDIF
		ENDM

INSTALLCODE	SEGMENT CODE
		RSEG INSTALLCODE

$IF (PROMCHECK)
JMP0:		LJMP	0
$ENDIF


$IF (SERIAL = 0)
;********************************************************************
;*  Using TIMER 1 to Generate Baud Rates                            *
;*  Oscillator frequency = 11.059 MHz                               *
;*  Set Baudrate to 9600 Baud                                       *
;********************************************************************

InitSerial:	PROMCHECK               ; Check if PROM in System
                MOV	TMOD,#00100000B		;C/T = 0, Mode = 2
		MOV	TH1,#0FDH
		SETB	TR1
		MOV	SCON,#01011010B ; Init Serial Interface
		JMP	Mon51

$ENDIF

$IF (SERIAL = 1)
;********************************************************************
;*  Using internal Baud Rate Generator (only for 80515 and 80C517)  *
;*  Oscillator frequency = 12.000 MHz                               *
;*  Set Baudrate to 9600 Baud                                       *
;********************************************************************
BD      BIT     0DFH

InitSerial:	PROMCHECK               ; Check if PROM in System
                SETB	BD
		ORL	PCON,#10000000B
		MOV	SCON,#01011010B ; Init Serial Interface
		JMP	Mon51
$ENDIF

$IF (SERIAL = 2)
;********************************************************************
;*  Using TIMER 2 to Generate Baud Rates (only for 8052)            *
;*  Oscillator frequency = 12.000 MHz                               *
;*  Set Baudrate to 9600 Baud                                       *
;********************************************************************
RCAP2L  DATA    0CAH
RCAP2H  DATA    0CBH
T2CON   DATA    0C8H

InitSerial:	PROMCHECK               ; Check if PROM in System
                MOV	T2CON,#34H
		MOV	RCAP2H,#0FFH
		MOV	RCAP2L,#0D9H
		MOV	SCON,#01011010B ; Init Serial Interface
		JMP	Mon51
$ENDIF


$IF (SERIAL = 3)
;********************************************************************
;*  Using Serial Interface 1 Baudrate Generator (only for 80C517)   *
;*  Oscillator frequency = 12.000 MHz                               *
;*  Set Baudrate to 9600 Baud                                       *
;********************************************************************
S1REL		DATA	09DH
S1CON		DATA	09BH
S1BUF		DATA	09CH
IEN2 		DATA	09AH

InitSerial:	PROMCHECK                ; Check if PROM in System
		MOV	S1REL,#0D9H	 ; Reload Value
		MOV	S1CON,#10111010B ; Init Serial Interface
		JMP	Mon51
$ENDIF

$IF (SERIAL = 4)
;********************************************************************
;*  Using TIMER 2 to Generate Baud Rates (only for 80C320/520/530)  *
;*  with dual data pointer support                                  *
;*  Oscillator frequency = 12.000 MHz                               *
;*  Set Baudrate to 9600 Baud                                       *
;********************************************************************
RCAP2L  DATA    0CAH
RCAP2H  DATA    0CBH
T2CON   DATA    0C8H
DPS     DATA    086H

InitSerial:	PROMCHECK               ; Check if PROM in System
		MOV	DPS,#00H
                MOV	T2CON,#34H
		MOV	RCAP2H,#0FFH
		MOV	RCAP2L,#0D9H    ; 9600 baud @ 12 MHz
;		MOV	RCAP2L,#0BFH    ; 9600 baud @ 20 MHz
;		MOV	RCAP2L,#0DCH    ; 9600 baud @ 11.0592 MHz
;		MOV	RCAP2L,#0EEH    ; 19200 baud @ 11.0592 MHz
;		MOV	RCAP2L,#0F7H    ; 38400 baud @ 11.0592 MHz
;		MOV	RCAP2L,#0FAH    ; 57600 baud @ 11.0592 MHz
		MOV	SCON,#01011010B ; Init Serial Interface
		JMP	Mon51
$ENDIF

$IF (SERIAL = 5)
;********************************************************************
;*  Using second serial interface of 80C320/520/530 with TIMER 1    *
;*  and dual data pointer support                                   *
;*  Oscillator frequency = 12.000 MHz                               *
;*  Set Baudrate to 9600 Baud                                       *
;********************************************************************
DPS     DATA    086H
CKCON   DATA    08EH
SCON1   DATA    0C0H
SBUF1   DATA    0C1H
WDCON   DATA    0D8H

InitSerial:	PROMCHECK               ; Check if PROM in System
		MOV	DPS,#00H
                MOV	TMOD,#00100000B	;C/T = 0, Mode = 2

		MOV	TH1,#0F6H       ; 9600 baud @ 12 MHz
		ORL	CKCON,#010H     ; Timer 1 uses a divide by 4 clock
;		ORL	WDCON,#10000000B; 19200 baud @ 12 MHz

;		MOV	TH1,#0FDH	; 9600 baud @ 11.0592 MHz

;		MOV	TH1,#0FFH	; 57600 baud @ 11.0592 MHz
;		ORL	WDCON,#10000000B

		SETB	TR1
		MOV	SCON1,#01011010B ; Init Serial Interface
		JMP	Mon51
$ENDIF



$IF (SERIAL = 3)
;********************************************************************
;*  Interface via additional 80517 UART                             *
;********************************************************************
SER_INT_ADR	EQU	83H		 ; ADDRESS OF SERIAL INTERRUPT VECTOR
SER_INT_ADR_OFF EQU (INT_ADR_OFF + SER_INT_ADR)

INSTAT:		PUSH	B		 ; INPUT STATUS OF SERIAL INTERFACE
		MOV	B,S1CON
		MOV	C,B.0
		POP	B
		RET

OUTSTAT:	PUSH	B		 ; OUTPUT STATUS OF SERIAL INTERFACE
		MOV	B,S1CON
		MOV	C,B.1
		POP	B
		RET

INCHAR:		MOV	A,S1BUF		 ; CHARACTER INPUT-ROUTINE 
		RET

OUTCHAR:	MOV	S1BUF,A		 ; CHARACTER OUTPUT-ROUTINE 
		RET

CLR_TI:		ANL	S1CON,#11111101B ; CLEAR SERIAL TRANSMIT INTERRUPT FLAG
		RET

SET_TI:		ORL	S1CON,#00000010B ; SET SERIAL TRANSMIT INTERRUPT FLAG
		RET

CLR_RI:		ANL	S1CON,#11111110B ; CLEAR SERIAL RECEIVE INTERRUPT FLAG
		RET

CLR_SER_IE:	ANL	IEN2,#11111110B  ; CLEAR SERIAL INTERRUPT ENABLE FLAG
		RET

SET_SER_IE:	ORL	IEN2,#00000001B  ; SET SERIAL INTERRUPT ENABLE FLAG
		RET

BEFORE_GO:                               ; this code is executed before a
		RET                      ; a go or proc step is executed

AFTER_GO:	                         ; this code is executed after a go
		RET                      ; command (when a breakpoint was set)


$ELSEIF (SERIAL = 5)
;********************************************************************
;*  Interface via additional 80C320 UART                            *
;********************************************************************
SER_INT_ADR	EQU	03BH		 ; ADDRESS OF SERIAL INTERRUPT VECTOR
SER_INT_ADR_OFF EQU (INT_ADR_OFF + SER_INT_ADR)
TI1     BIT     0C1H
RI1     BIT     0C0H
ES1     BIT     0AEH

INSTAT:		MOV	C,RI1		; INPUT STATUS OF SERIAL INTERFACE
		RET

OUTSTAT:	MOV	C,TI1		; OUTPUT STATUS OF SERIAL INTERFACE
		RET

INCHAR:		MOV	A,SBUF1		; CHARACTER INPUT-ROUTINE
		RET

OUTCHAR:	MOV	SBUF1,A		; CHARACTER OUTPUT-ROUTINE
		RET

CLR_TI:		CLR	TI1		; CLEAR SERIAL TRANSMIT INTERRUPT FLAG
		RET

SET_TI:		SETB	TI1		; SET SERIAL TRANSMIT INTERRUPT FLAG
		RET

CLR_RI:		CLR	RI1		; CLEAR SERIAL RECEIVE INTERRUPT FLAG
		RET

CLR_SER_IE:	CLR	ES1		; CLEAR SERIAL INTERRUPT ENABLE FLAG
		RET

SET_SER_IE:	SETB	ES1		; SET SERIAL INTERRUPT ENABLE FLAG
		RET

BEFORE_GO:                               ; this code is executed before a
		RET                      ; a go or proc step is executed

AFTER_GO:	                         ; this code is executed after a go
		RET                      ; command (when a breakpoint was set)

$ELSE

;********************************************************************
;*  Interface via standard 8051 UART                                *
;********************************************************************
SER_INT_ADR	EQU	23H		; ADDRESS OF SERIAL INTERRUPT VECTOR
SER_INT_ADR_OFF EQU (INT_ADR_OFF + SER_INT_ADR)

INSTAT:		MOV	C,RI		; INPUT STATUS OF SERIAL INTERFACE
		RET

OUTSTAT:	MOV	C,TI		; OUTPUT STATUS OF SERIAL INTERFACE
		RET

INCHAR:		MOV	A,SBUF		; CHARACTER INPUT-ROUTINE
		RET

OUTCHAR:	MOV	SBUF,A		; CHARACTER OUTPUT-ROUTINE
		RET

CLR_TI:		CLR	TI		; CLEAR SERIAL TRANSMIT INTERRUPT FLAG
		RET

SET_TI:		SETB	TI		; SET SERIAL TRANSMIT INTERRUPT FLAG
		RET

CLR_RI:		CLR	RI		; CLEAR SERIAL RECEIVE INTERRUPT FLAG
		RET

CLR_SER_IE:	CLR	ES		; CLEAR SERIAL INTERRUPT ENABLE FLAG
		RET

SET_SER_IE:	SETB	ES		; SET SERIAL INTERRUPT ENABLE FLAG
		RET

BEFORE_GO:                               ; this code is executed before a
		RET                      ; a go or proc step is executed

AFTER_GO:	                         ; this code is executed after a go
		RET                      ; command (when a breakpoint was set)


$ENDIF


		END
