
; SW-DEBUGGER - 80C535

IEN0		DATA	0A8H
IEN1		DATA	0B8H
IP0 		DATA	0A9H
IP1 		DATA	0B9H
IRCON		DATA	0C0H
P4		DATA	0E8H
P5		DATA	0F8H

PC_BYTE_1	DATA	 70H
PC_BYTE_2	DATA	 71H
MARKEN		DATA	 72H
MARKE		DATA	 73H
FLAGS_0		DATA	 74H
START		CODE   1C00H
INT_0		CODE	 03H
INT_1		CODE	 13H
INT_6		CODE	 6BH

START_ADR:
	CSEG	AT	RESET
	LJMP	START
	CSEG	AT	INT_0
	LJMP	INTERRUPT_0
	CSEG	AT	INT_1
	LJMP	INTERRUPT_0
	CSEG	AT	INT_6
	LJMP	INTERRUPT_6
	CSEG	AT	START

ORG	1C00H

PROGRAMM_START:
	MOV	R0,#15H
	MOV	DPTR,#X_USER_BREAKPOINT_1
LOOP_1:	MOVX	@DPTR,A
	INC	DPTR
	DJNZ	R0,LOOP_1
LOOP_2:	MOV	@R0,A
	DJNZ	R0,LOOP_2
	MOV	SP,#74H
	MOV	A,#7
	MOV	DPTR,#U_SP
	MOVX	@DPTR,A
	ORL	IP0,#20H
	ORL	IP1,#20H
	SETB	TCON.0
	ORL	IEN0,#81H
	SETB	IEN1.5
	CLR	P4.0
	SETB	P4.0
INTERRUPT_LOOP:
	SJMP	INTERRUPT_LOOP

INTERRUPT_0:
	MOV	DPTR,#X_PC_BEFEHL
	MOV	P5,#0FFH
	CLR	P4.3
	MOV	A,P5
	SETB	P4.3
	MOV	P5,A
	MOVX	@DPTR,A
	ORL	FLAGS_0,#01H
INT0_1:	MOV	A,MARKEN	
	JZ	INT0_BEFEHLE
	JMP	INT0_4

INT0_BEFEHLE:
	MOV	DPTR,#X_PC_BEFEHL
	MOVX	A,@DPTR
	ANL	A,#0FH
	RL	A
	MOV	DPTR,#JMP_TABELLE
	JMP	@A+DPTR
JMP_TABELLE:
	AJMP	B0_GO
	AJMP	B1_SHOW_SFR_BYTE
	AJMP	B2_PROGRAM_COUNTER_TO_PC
	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