; Relocation V2.0
bsr opendos
lea filename,a0

LoadFile: ; Settings IN :  A0 := Zeiger auf NAMEN
          ;          OUT:  D0 = Zeiger auf Allociertes Mem
          ;                D1 = Länge der eingelesenen Daten

move.l	a0,FileToLoad		;FileName 
move.l	Dosbase,a6		;
move.l	FileToLoad,d1		;FileName
move.l	#-2,d2			;Access_Mode read
jsr	-$54(a6)		;Lock      ;Lock suchen
tst.b d0
beq	LadeFehler		;
move.l dosbase,a6
move.l	d0,FileLock		;
move.l	d0,d1			;FileLock in d1
move.l	#InfoBlock,d2		;InfoBlock in d2
jsr	-$66(a6)		;Examine; FileInfoBlock laden
tst.l d0
beq exerror
lea	InfoBlock,a5		;
move.l	$7c(a5),Lenght		;Laenge d. Files 
move.l	Lenght,d0		;	
move.l #$10001,d1               ; Alloc public Mem and clear it
move.l	4,a6
jsr	-$c6(a6)		;allocmem; Memory allocieren
tst.l d0
beq	SpeicherFehler		;
move.l d0,Buffer;Speicher f.d. File
move.l	DosBase,a6		;
move.l	FileToLoad,d1		;File in d1
move.l	#1005,d2		;Mode OldFile
jsr	-$1e(a6)		;open; File oeffnen
move.l	d0,FileHd		;FileHandler 
move.l	d0,d1			;
move.l	Buffer,d2		;Adresse
move.l	Lenght,d3		;Laenge
jsr	-$2a(a6)		;read; File lesen
cmp.l	Lenght,d0		;Test ob alles gelesen
bne	LadeFehler		;
move.l	FileHd,d1		;
jsr	-36(a6)		;close; File schließen
move.l	Buffer,d0		;Adresse in d0 
move.l	Lenght,d1		;laenge in d1 


move.l d0,origstart
move.l d1,origlenght

move.l origstart,a0
cmp.l #$3f3,(a0)+
bne error
cmp.l #$000,(a0)+
bne error
move.l (a0)+,hunkan
add.l #8,a0
move.l hunkan,d0
lsl.l #2,d0
add.l d0,a0
move.l a0,origstart
move.l 4,a6
move.l #$10001,d1
jsr -198(a6)
tst.l d0
beq memerror
move.l d0,a2	; A2 : Tabelle für die Startadressen der code/data/bss h.
move.l d0,startstab
move.l origstart,a0
clr.l d3
HunkCMP1:
add.l #4,a0

cmp.l #$3e9,-4(a0)
beq codehunk1
cmp.l #$3ea,-4(a0)
beq codehunk1
cmp.l #$3eb,-4(a0)
beq bsshunk1
cmp.l #$3ec,-4(a0)
beq reloc32hunk1
cmp.l #$3f0,-4(a0)
beq symbolhunk1
cmp.l #$3f1,-4(a0)
beq debughunk1
cmp.l #$3f2,-4(a0)
beq endhunk1

move.l #$01,d0
rts


CodeHunk1:
move.l (a0)+,d0
lsl.l #2,d0
cmp.w #$4000,-8(a0)
bne CodeOk
add.l #4,d0
move.l d0,d2
bsr allocchipmem
move.l d0,a3
move.l d2,(a3)+
lsr.l #2,d2
sub.l #1,d2
CodeCopyLoop:
move.l (a0)+,(a3)+
tst.w d2
dbne d2,codecopyloop
bra hunkcmp1

CodeOk:
move.l a0,(a2)+
adda.l d0,a0
bra hunkcmp1

BssHunk1:
; Test if Chip Memory
move.l #$10002,d1
cmp.w #$4000,-4(a0)
bne bssChip
move.l #$10001,d1	; Bss Public
BssChip:
move.l (a0)+,d0
tst.l d0
beq hunkcmp1		; NO AllocMem !
lsl.l #2,d0
add.l #4,d0		; reserviert für Länge des BssHunks
move.l d0,d2
bsr allocmem
move.l d0,a3
move.l d2,(a3)+		; Länge eintragen
move.l a3,(a2)+
bra hunkcmp1

Reloc32Hunk1:
;SymbolHunk1:
;DebugHunk1:
move.l (a0)+,d0
tst.l d0
beq hunkcmp1
add.l #4,a0	; HunkNr.
lsl.l #2,d0
add.l d0,a0
bra reloc32hunk1

SymbolHunk1:
DebugHunk1:
rts
move.l (a0)+,d0
tst.l d0
beq hunkcmp1
add.l #4,a0	; HunkNr.
lsl.l #2,d0
add.l d0,a0
bra debughunk1

AllocChipMem:
move.l #$10002,d1
AllocMem:
movem.l d1-d7/a0-a6,-(sp)
move.l 4,a6
move.l #$10002,d1
jsr -198(a6)
tst.l d0
beq memerror1
movem.l (sp)+,d1-d7/a0-a6
rts

EndHunk1:
add.l #1,d3
cmp.l hunkan,d3
blo hunkcmp1

move.l #$ff,d0
rts

; *** Pass 2 (Relocation of all Code and Data Hunks)

clr.l d3
move.l origstart,a0
move.l startstab,a2

move.l origstart,a0
clr.l d3
HunkCMP2:
add.l #4,a0

cmp.l #$3e9,-4(a0)
beq codehunk2
cmp.l #$3ea,-4(a0)
beq codehunk2
cmp.l #$3eb,-4(a0)
beq bsshunk2
cmp.l #$3ec,-4(a0)
beq reloc32hunk2
cmp.l #$3f0,-4(a0)
beq symbolhunk2
cmp.l #$3f1,-4(a0)
beq debughunk2
cmp.l #$3f2,-4(a0)
beq endhunk2

move.l #$01,d0
rts

;Reloc32Hunk1
SymbolHunk2:
DebugHunk2:
move.l (a0)+,d0
tst.l d0
beq hunkcmp2
add.l #4,a0	; HunkNr.
lsl.l #2,d0
add.l d0,a0
bra debughunk2

CodeHunk2:
move.l (a0)+,d0
move.l a0,workhunkstart
lsl.l #2,d0
add.l d0,a0
bra hunkcmp2

BssHunk2:
add.l #4,a0
bra hunkcmp2

Reloc32Hunk2:
move.l (a0)+,d2		; RelocsAnzahl
tst.l d2
beq hunkcmp2
move.l (a0)+,d0		; HunkNr.
lsl.l #2,d0
move.l (a2,d0),d4	; BasisAdresse des Hunks (HunkNr.)
move.l workhunkstart,a3

sub.l #1,d2
reloc32loop2:
move.l (a0)+,d0
add.l d4,(a3,d0)
;tst.w d2
dbra d2,reloc32loop2
;sub.l #4,a0
bra reloc32hunk2

EndHunk2:
add.l #1,d3
cmp.l hunkan,d3
blo hunkcmp2

move.l (a2),d0		; Start des ersten hunks in D0
; FreeMems 

;
rts


OpenDos:
  move.l 4,a6
  lea dosname,a1
  moveq #0,d0
  jsr -408(a6)   ; OpenLib
  move.l d0,dosbase
  ;beq error                ; error!
rts
dosname:
  dc.b "dos.library",0,0
even
dosbase:
  blk.l 1

CloseDos:
move.l 4,a6
move.l dosbase,a1
jsr -414(a6)        ; CloseLib
rts


Ladefehler:
		clr.l	d0			;0 = LadeFehler
		rts
SpeicherFehler:
		
		Move.b	#$ff,d0			;$ff =Kein Speicher
		rts
ExError:
                move.l #$fe,d0
                rts
MemError1:	move.l (sp)+,d0
MemError:       move.l #$fd,d0
		rts

align 4
InfoBlock:	blk.l	67,0	
FileLock:	dc.l 0
Lenght:		dc.l 0
Buffer:		dc.l 0
FileHd:		dc.l 0
FileToLoad:	dc.l 0




; Pass 1 (Find startadresses of code,data and bss hunks)

FileName:	dc.b "df1:climate",0
OrigStart:	dc.l 0
OrigLenght:	dc.l 0
HunkAn:		dc.l 0
StartsTab:	dc.l 0
WorkHunkStart:	dc.l 0
Error:
move.l #$ffffffff,d0
rts
