	
  *************************************************************************
  *************************************************************************
  **                                                                     **
  **         Debugger handler v0.3 06/11/93 by Shagratt/LSD.             **
  **         Symbol converter v0.2 31/10/93 by Shagratt/LSD.             **
  **                                                                     **
  **         SIMBUG © Stefan Walter                                      **
  **                                                                     **
  *************************************************************************
  *************************************************************************

CommsSize	= 100*1000		; Memory Allocation For Symbol List
	
	*--------------------------------------------*

InitComms:	bsr	AllocMem
	beq	.Error

	bsr	Comms_ConfigMem
	bsr	GetSymbols

	bsr	GetTopChip
	lea	Comms_DebugCode,a6
	move.l	a1,0(a6)		;chipmem display
	move.l	Comms_DebugBuff,4(a6)	;chipmem backup
	bsr	CommsGetVBR
	move.l	a4,264(a6)		;current VBR
	
	Lea	OldVecs,a0
	Lea	VecList,a1
.setup	movem.w	(a1)+,d0/d1
	add.l	a6,d1
	move.l	(a4,d0.w),(a0,d0.w)
	move.l	d1,(a4,d0.w)
	cmp.w	#-1,(a1)
	bne	.setup

.Error	moveq.l	#0,d0
	rts	

	*--------------------------------------------*

AllocMem:	move.l	#CommsSize,d0
	move.l	#$10001,d1
	move.l	4,a6
	jsr	-198(a6)		;AllocMem
	move.l	d0,Comms_SymbBuff
	
	move.l	#$5000,d0
	move.l	#$10001,d1
	move.l	4,a6
	jsr	-198(a6)		;AllocMem
	move.l	d0,Comms_DebugBuff
	
	move.l	Comms_SymbBuff,d0
	or.l	Comms_DebugBuff,d0
	rts
	
	*--------------------------------------------*

Comms_ConfigMem:

	move.l	4,a6
	move.l	322(a6),a0		; memlist
	lea	Comms_DebugCode+$7b72,a1
	
	move.w	#5,d3
			
.loop	move.l	20(a0),d1		; lower
	and.l	#$fffff000,d1
	move.l	d1,(a1)+
	move.l	24(a0),(a1)+	; upper

	sub.w	#1,d3
	beq	.max

	move.l	0(a0),a0
	tst.l	(a0)
	bne	.loop
	
.max	lea	Comms_DebugCode+$7b72,a0
.chiploop	tst.l	(a0)
	bne	.notchip
	
	;put chipmem first is memlist
	
	lea	Comms_DebugCode+$7b72,a1
	move.l	(a0),d0
	move.l	(a1),(a0)
	move.l	d0,(a1)
	lea	4(a0),a0
	lea	4(a1),a1
	move.l	(a0),d0
	move.l	(a1),(a0)
	move.l	d0,(a1)		
	rts	

.notchip	lea	8(a0),a0
	add.w	#1,d3
	cmp.w	#5,d3
	bne	.chiploop

	rts
	
	*--------------------------------------------*

KillComms:	bsr	CommsGetVBR
	Lea	OldVecs,a0
	Lea	VecList,a1
.setup	movem.w	(a1)+,d0/d1
	move.l	(a0,d0.w),(a4,d0.w)
	cmp.w	#-1,(a1)
	bne	.setup

	tst.l	Comms_SymbBuff
	beq	.nomem1
	move.l	Comms_SymbBuff,a1
	move.l	#CommsSize,d0
	move.l	4,a6
	jsr	-210(a6)		;FreeMem
	move.l	#0,Comms_SymbBuff
	
.nomem1	tst.l	Comms_DebugBuff
	beq	.nomem2
	move.l	Comms_DebugBuff,a1
	move.l	#$5000,d0
	move.l	4,a6
	jsr	-210(a6)		;FreeMem
	move.l	#0,Comms_DebugBuff
.nomem2	rts

	*--------------------------------------------*

GetTopChip:	lea	$200000-$8000,a1
.loop	move.l	4,a6
	jsr	-534(a6)		;TypeOfMem
	tst.l	d0
	bne	.ok
	sub.w	#$8000,a1
	bra	.loop
.ok	rts
	
	*--------------------------------------------*

Comms:	btst	#2,$dff016
	bne	.ret
	
	jsr	Comms_DebugCode+8
	
.wait	move.w	$dff006,$dff180
	btst	#2,$dff016
	beq	.wait

.ret	rts	
	
	*--------------------------------------------*

CommsGetVBR	move.l	a6,-(a7)
	Move.l	4.w,A6
	Moveq	#15,D7
	And	296(A6),D7
	Beq	.68000
	Lea	.Find(Pc),A5
	Jsr	-30(A6)
.68000	Move.l	D7,a4
	move.l	(a7)+,a6
	Rts

.Find	dc.l	$4e7a7801		;Movec.l	VBR,D7
	Rte

	*--------------------------------------------*

GetSymbols:	Lea	Comms_DName,A1
	Moveq	#0,D0
	move.l	4,a6
	jsr	-552(a6)		;OpenLibrary
	Move.l 	D0,Comms_Dos
	Beq	.err

	bsr	OpenSymb
	tst.l	Comms_Handle
	beq	.nofile
		
	move.l	Comms_SymbBuff,a0
	move.l	#End,16(a0)
	move.l	#Start,20(a0)

	lea	32(a0),a0		;symbol list start
	move.l	a0,Comms_SymbPtr
	move.l	Comms_SymbBuff,Comms_DebugCode+236

	bsr	FindSymb
	bsr	CloseSymb

.nofile	Move.l	Comms_Dos,a1
	move.l	4,a6
	jsr	-414(a6)		;CloseLibrary
	
.err	rts

	*--------------------------------------------*

OpenSymb:	move.l	Comms_Dos,a6
	move.l	#Comms_DBList,d1
	move.l	#1005,d2
	jsr	-30(a6)		;Open
	move.l	d0,Comms_Handle
	
	IFNE	COMBIG
	tst.l	d0
	beq	.err
	
	move.l	Comms_Handle,d1
	move.l	#0,d2
	move.l	#1,d3		;OFFSET_END
	jsr	-66(a6)		;Seek
	
	move.l	Comms_Handle,d1
	move.l	#0,d2
	move.l	#-1,d3		;OFFSET_BEGGINING
	jsr	-66(a6)		;Seek
	move.l	d0,Comms_Len
	
	move.l	Comms_Len,d0
	add.l	#4,d0
	move.l	#$10001,d1
	move.l	4,a6
	jsr	-198(a6)		;AllocMem
	move.l	d0,Comms_TempBuff
	tst.l	d0
	beq	.err

	move.l	Comms_TempBuff,Comms_MemPtr
	
	move.l	Comms_Dos,a6	
	move.l	Comms_Handle,d1
	move.l	Comms_TempBuff,d2
	move.l	Comms_TempBuff,d3
	jsr	-42(a6)		;Read
	move.l	Comms_TempBuff,a0
	lea	(a0,d0.l),a0
	move.l	#-1,(a0)
	
	move.l	Comms_Handle,d1
	move.l	#0,d2
	move.l	#-1,d3		;OFFSET_BEGGINING
	jsr	-66(a6)		;Seek
	
	ENDC
	
.err	rts

	*--------------------------------------------*

FindSymb:	bsr	ReadLong
	cmp.l	#$3f3,d0
	bne	.err

	bsr	ReadLong
	cmp.l	#0,d0
	bne	.err

	bsr	ReadLong		;no of hunks
	move.l	d0,d1
	bsr	ReadLong		;first hunk
	bsr	ReadLong		;last hunk
.skip	bsr	ReadLong		;bss for each hunk
	sub.l	#1,d1
	bne	.skip

	Lea	Comms_SymHunks,a0
	move.l	#Start,(a0)+
	lea	Start-4,a1
.hunkloop	move.l	(a1),d0
	beq	.done
	asl.l	#2,d0
	move.l	d0,a1
	add.l	#4,d0
	move.l	d0,(a0)+
	bra	.hunkloop
	
.done	bsr	SymbProcHunk
.err	rts


	*--------------------------------------------*

ReadLong:	movem.l	d1-d7/a0-a6,-(a7)

	IFNE	COMBIG
	tst.l	Comms_MemPtr
	beq	.norm
	
	move.l	Comms_MemPtr,a0
	move.l	(a0)+,d0
	move.l	a0,Comms_MemPtr
	move.w	d0,$dff180
	movem.l	(a7)+,d1-d7/a0-a6
	rts
	ENDC
	
.norm	move.l	Comms_DebugBuff,a0
	move.l	#-1,(a0)
	move.l	Comms_Dos,a6		
	move.l	Comms_Handle,d1
	move.l	Comms_DebugBuff,d2
	move.l	#4,d3
	jsr	-42(a6)		;Read
	move.l	Comms_DebugBuff,a0
	move.l	(a0),d0
	move.w	d0,$dff180
	movem.l	(a7)+,d1-d7/a0-a6
	rts
	
	*--------------------------------------------*

SymbProcHunk:
	move.l	#0,Comms_HunkNo

.loop	bsr	ReadLong
	cmp.l	#-1,d0
	beq	.err
	
	sub.l	#$3e7,d0
	asl.l	#2,d0
	lea	SymbHunkTab,a0
	move.l	(a0,d0.l),a0
	cmp.l	#0,a0
	beq	.err
	jsr	(a0)
	bra	.loop
	
.err	rts

	*--------------------------------------------*

SeekNext:	movem.l	d1-d7/a0-a6,-(a7)

	IFNE	COMBIG
	tst.l	Comms_MemPtr
	beq	.norm
	asl.l	#2,d0
	add.l	d0,Comms_MemPtr
	movem.l	(a7)+,d1-d7/a0-a6
	rts
	ENDC
	
.norm	move.l	Comms_Handle,d1
	move.l	d0,d2
	asl.l	#2,d2
	move.l	#0,d3
	move.l	Comms_Dos,a6
	jsr	-66(a6)		;Seek
	movem.l	(a7)+,d1-d7/a0-a6
	rts

	*--------------------------------------------*

CloseSymb:	move.l	Comms_Dos,a6

	move.l	Comms_Handle,d1
	jsr	-36(a6)		;Close
	
	move.l	#Comms_DBList,d1
	jsr	-72(a6)		;DeleteFile
	
	IFNE	COMBIG
	tst.l	Comms_TempBuff
	beq	.err
	move.l	Comms_TempBuff,a1
	move.l	Comms_Len,d0
	add.l	#4,d0
	move.l	4,a6
	jsr	-210(a6)		;AllocMem
	move.l	#0,Comms_TempBuff
	ENDC
.err	rts	
	
	*--------------------------------------------*

HunkSymb:	move.l	Comms_SymbPtr,a0
	
.next	move.l	Comms_DebugBuff,a1
	lea	8(a1),a1
	bsr	ReadLong
	beq	.err
	move.l	d0,d1

.loop	bsr	ReadLong
	move.l	d0,(a1)+
	sub.l	#1,d1
	bne	.loop
	move.l	#0,(a1)+
		
	bsr	ReadLong
	move.l	d0,d1
	move.l	Comms_HunkNo,d2
	asl.l	#2,d2
	lea	Comms_SymHunks,a3
	add.l	(a3,d2.l),d1
	move.l	d1,(a0)+		;Address
	move.l	a0,a3
	move.w	#0,(a0)+		;skip length

	move.l	Comms_DebugBuff,a1
	lea	8(a1),a1
	move.w	#-1,d0	
.len1	add.w	#1,d0
	cmp.b	#0,(a1,d0.w)
	bne	.len1
	
	move.b	d0,(a0)+		;Len Byte
.len2	move.b	(a1)+,d1
	cmp.b	#"A",d1
	blt	.ok
	cmp.b	#"Z",d1
	bgt	.ok
	or.b	#32,d1
.ok	move.b	d1,(a0)+		;Symb Text
	dbf	d0,.len2
	
	move.l	Comms_SymbBuff,a5
	add.l	#1,4(a5)		;no of symbols
	
	lea	-1(a0),a0
	move.l	a0,d0
	btst	#0,d0
	beq	.even
	move.b	#0,(a0)+
.even	move.l	a0,d0
	sub.l	a3,d0
	move.w	d0,(a3)

	bra	.next

.err	add.l	#1,Comms_HunkNo
	move.l	a0,Comms_SymbPtr

	rts
	
	*-------------------------------*

HunkBSS:	bra	ReadLong
	
	*-------------------------------*

HunkData:	bsr	ReadLong
	bra	SeekNext
	
	*-------------------------------*
	
HunkReloc	bsr	ReadLong
	cmp.l	#0,d0
	beq	.end
	move.l	d0,d7
	bsr	ReadLong
	move.l	d7,d0
	bsr	SeekNext
	bra	HunkReloc
	
.end	rts

	*-------------------------------*

HunkEnd:	rts

	*-------------------------------*

SymbHunkTab:	dc.l	0	;$3e7
		dc.l	0	;$3e8
		dc.l	HunkData	;$3e9
		dc.l	HunkData	;$3ea
		dc.l	HunkBSS	;$3eb
		dc.l	HunkReloc	;$3ec
		dc.l	HunkReloc	;$3ed
		dc.l	HunkReloc	;$3ee
		dc.l	HunkSymb	;$3ef
		dc.l	HunkSymb	;$3f0
		dc.l	HunkData	;$3f1
		dc.l	HunkEnd	;$3f2
		dc.l	0	;$3f3
		dc.l	0	;$3f4
		dc.l	0	;$3f5
		dc.l	HunkEnd	;$3f6
		dc.l	HunkReloc	;$3f7	
		dc.l	HunkReloc	;$3f8	
		dc.l	HunkReloc	;$3f9	
		dc.l	HunkBSS	;$3fa	
		dc.l	HunkData	;$3fb	
		dc.l	0	;$3fc
		dc.l	HunkReloc	;$3fd	
		dc.l	0	;$3fe
		dc.l	0	;$3ff



	*--------------------------------------------*

OldVecs:	ds.l	256

VecList:	dc.w	$08,24	;Bus Error
	dc.w	$0C,28	;Address Error
	dc.w	$10,32	;Illegal Instruction
	dc.w	$14,36	;Zero Divide
	dc.w	$18,40	;CHK/CHK2 Instruction
	dc.w	$1C,44	;cpTRAPcc, TRAPcc, TRAPV Instructions
	dc.w	$24,52	;Trace Exception
	dc.w	$28,56	;Line-A Emulator (1010)
	dc.w	$2C,60	;Line-F Emulator (1111) & Coprocessor Instructions
	dc.l	-1

Comms_Dos		dc.l	0
Comms_Handle:	dc.l	0
Comms_HunkNo:	dc.l	0
Comms_SymbPtr:	dc.l	0
Comms_SymbBuff:	dc.l	0
Comms_DebugBuff:	dc.l	0
Comms_Len:		dc.l	0
Comms_TempBuff:	dc.l	0
Comms_MemPtr:	dc.l	0

Comms_DName		dc.b	"dos.library",0
Comms_DBList:	dc.b	"ram:temp.exe",0
		even
			
Comms_DebugCode:	incbin	"SimBug/sim.20"
Comms_SymHunks:	ds.l	32

	*--------------------------------------------*

End:
