ExecBase:  equ 4
CIA_B:	   equ $bfd000
CIA_A:	   equ $bfe001
CustomBase: equ $dff000
Identification: equ $544c
WriteMode: equ 1
ReadMode:  equ 2
WriteList: equ 3
ReadList:  equ 4
;---
 incdir	 "include/"
 include "exec/memory.i"
 include "exec/ports.i"
 include "exec/io.i"
 include "exec/exec_lib.i"
 include "devices/trackdisk.i"
 include "hardware/cia.i"
 include "hardware/custom.i"
 include "libraries/dos.i"
 include "libraries/dos_lib.i"
;---
Beispiel:
; lea WriteTable,a0
; move.b #WriteList,d4
; bsr.s HandleTracks
 lea ReadTable,a0
 move.b	#ReadList,d4
 bsr.s HandleTracks
 rts
;---
WriteTable: dc.l Write1,Write2,0
ReadTable: dc.l Read1,Read2,0
Write1: dc.w $a111
 dc.b 90,15,1
 dc.b "c/d",0
 even

Write2: dc.w $a89a
 dc.b 40,5,1
 dc.b "c/cm",0
 even

Read1: dc.w $a111
 dc.l $40000
 dc.b 90,15,1
 even

Read2: dc.w $a89a
 dc.l $48000
 dc.b 40,5,1
 even
;---
HandleTracks: cmp.b #WriteList,d4
 beq HandleInfo
 cmp.b #ReadList,d4
 beq HandleReadInfo
 move.l a0,DataBuffer
 move.b d1,NumTracks
 move.b d0,StartTrack
 move.b d2,DriveNumber
 move.w d3,CustomSync
 move.b d4,SelectMode
;---
EntryII: move.l #MEMF_CLEAR!MEMF_PUBLIC,d1
 moveq #MP_SIZE,d0
 move.l ExecBase,a6
 jsr _LVOAllocMem(a6)
 move.l d0,DiskMsgPort
 beq NoPortMemory
 moveq #-1,d0
 jsr _LVOAllocSignal(a6)
 move.l DiskMsgPort,a1
 move.b d0,MP_SIGBIT(a1)
 beq NoPortSignal
 move.b #NT_MSGPORT,LN_TYPE(a1)
 suba.l a1,a1
 jsr _LVOFindTask(a6)
 move.l DiskMsgPort,a1
 move.l d0,MP_SIGTASK(a1)
 jsr _LVOAddPort(a6)
;---
 move.l #MEMF_CLEAR!MEMF_PUBLIC,d1
 moveq #IOSTD_SIZE,d0
 jsr _LVOAllocMem(a6)
 move.l d0,IOStdReq
 beq NoReqMemory
 move.l d0,a0
 move.l DiskMsgPort,MN_REPLYPORT(a0)
 move.w #IOSTD_SIZE,MN_LENGTH(a0)
 move.b #NT_MESSAGE,LN_TYPE(a0)
;---
 lea TrackDiskName,a0
 move.l IOStdReq,a1
 moveq #0,d0
 move.b DriveNumber,d0
 moveq #0,d1
 jsr _LVOOpenDevice(a6)
 tst.l d0
 bne.s CannotAccess
;---
 move.l IOStdReq,a1
 move.w #TD_MOTOR,IO_COMMAND(a1)
 moveq #1,d0
 move.l d0,IO_LENGTH(a1)
 jsr _LVODoIO(a6)
;---
 cmp.b #WriteMode,SelectMode
 bne.s DoNotWrite
 bsr WriteTheTracks
DoNotWrite: cmp.b #ReadMode,SelectMode
 bne.s DoNotRead
 bsr ReadTheTracks
;---
DoNotRead: move.l IOStdReq,a1
 move.w #TD_MOTOR,IO_COMMAND(a1)
 moveq #0,d0
 move.l d0,IO_LENGTH(a1)
 jsr _LVODoIO(a6)
;---
 move.l IOStdReq,a1
 jsr _LVOCloseDevice(a6)
;---
CannotAccess: move.l IOStdReq,a1
 moveq #IOSTD_SIZE,d0
 jsr _LVOFreeMem(a6)
NoReqMemory: move.l DiskMsgPort,a1
 jsr _LVORemPort(a6)
NoPortSignal: move.l DiskMsgPort,a1
 moveq #MP_SIZE,d0
 jsr _LVOFreeMem(a6)
;---
NoPortMemory: clr.l d0
 rts
;---
WriteTheTracks: move.l #MEMF_CLEAR!MEMF_PUBLIC!MEMF_CHIP,d1
 move.l #$4004,d0
 jsr _LVOAllocMem(a6)
 move.l d0,DiskBuffer
 beq NoBufferMem
;---
 move.l d0,a1
 move.w #$1000,d0
WriteNull: move.l #$44444444,(a1)+
 dbra d0,WriteNull
;---
WriteLoop: clr.l d0
 move.b StartTrack,d0
 mulu #$1600,d0
 move.l IOStdReq,a1
 move.l d0,IO_OFFSET(a1)
 move.w #TD_SEEK,IO_COMMAND(a1)
 jsr _LVODoIO(a6)
;---
 move.l DiskBuffer,a0
 lea $1140(a0),a0
 move.w CustomSync,(a0)+
 move.w #$aaaa,d0
 btst #0,-1(a0)
 beq.s GapOkay
 move.w #$2aaa,d0
GapOkay: move.w d0,(a0)+
 move.b StartTrack,(a0)+
 move.l DataBuffer,a1
;---
 clr.b d2
 move.w #$16ff,d3
CheckSumLoop: move.b (a1)+,d4
 add.b d3,d4
 eor.b d4,d2
 dbra d3,CheckSumLoop
 move.b d2,(a0)+
 move.w #Identification,(a0)+
;---
 move.l DiskBuffer,a0
 lea $1140(a0),a0
 move.l 4(a0),d2
 move.l d2,d3
 and.l #$55555555,d3
 lsr.l #1,d2
 and.l #$55555555,d2
 move.l d2,4(a0)
 move.l d3,8(a0)
;---
 lea 12(a0),a2
 move.l DataBuffer,a1
 move.w #$5bf,d2
CopyLoop: move.l (a1)+,d4
 move.l d4,d3
 and.l #$55555555,d3
 lsr.l #1,d4
 and.l #$55555555,d4
 move.l d4,(a2)+
 move.l d3,(a2)+
 dbra d2,CopyLoop
;---
 lea 4(a0),a2
 move.w #$1703,d3
ClockBitLoop: move.w (a2),d0
 move.w d0,d2
 eor.w #$5555,d0
 move.w d0,d1
 lsl.w #1,d0
 lsr.w #1,d1
 bset #15,d1
 and.w d1,d0
 or.w d2,d0
 btst #0,-1(a2)
 beq.s WasNotSet
 bclr #15,d0
WasNotSet: move.w d0,(a2)+
 dbra d3,ClockBitLoop
;---
 jsr _LVOForbid(a6)
 bsr.s SelectDrive
 bsr.s SelectHead
 bsr DiskWriteBuffer
 bsr.s  ResetTheDrive
 jsr _LVOPermit(a6)
;---
 add.l #$1700,DataBuffer
 addq.b #1,StartTrack
 subq.b #1,NumTracks
 bne WriteLoop
;---
 move.l DiskBuffer,a1
 move.l #$4004,d0
 jmp _LVOFreeMem(a6)
SelectDrive: lea CIA_B,a0
 clr.l d0
 move.b #$7f,ciaprb(a0)
 move.b DriveNumber,d0
 addq #3,d0
 bclr d0,ciaprb(a0)
NoBufferMem: rts
;---
SelectHead: lea CIA_B,a0
 bclr #2,ciaprb(a0)
 btst #0,StartTrack
 bne.s OkaySelect
 bset #2,ciaprb(a0)
OkaySelect: rts
;---
ResetTheDrive: lea CIA_B,a0
 or.b #$78,ciaprb(a0)
 rts
;---
WaitReadySignal: lea CIA_A,a1
WaitForIt: btst #5,ciapra(a1)
 bne.s WaitForIt
 rts
;---
DiskWriteBuffer: bsr.s WaitReadySignal
 lea CustomBase,a0
 move.w #$4000,dsklen(a0)
 move.l DiskBuffer,dskpt(a0)
 move.w #$e001,dsklen(a0)
 move.w #$e001,dsklen(a0)
 jsr _LVODisable(a6)
 move.w #$0002,intreq(a0)
WaitForDMA: move.w intreqr(a0),d2
 btst #1,d2
 beq.s WaitForDMA
 move.w d2,intreq(a0)
 move.w #$4000,dsklen(a0)
 jsr _LVOEnable(a6)
 rts
;---
ReadTheTracks: move.l #MEMF_CLEAR!MEMF_PUBLIC!MEMF_CHIP,d1
 move.l #$2f00,d0
 jsr _LVOAllocMem(a6)
 move.l d0,DiskBuffer
 beq NoBufferMem
;---
ReadLoop: clr.l d0
 move.b StartTrack,d0
 mulu #$1600,d0
 move.l IOStdReq,a1
 move.l d0,IO_OFFSET(a1)
 move.w #TD_SEEK,IO_COMMAND(a1)
 jsr _LVODoIO(a6)
;---
 jsr _LVOForbid(a6)
 bsr SelectDrive
 bsr SelectHead
 bsr DiskReadBuffer
 bsr  ResetTheDrive
 jsr _LVOPermit(a6)
;---
 move.l DiskBuffer,a2
 addq.l #2,a2
 move.l (a2)+,d2
 move.l (a2)+,d3
 and.l #$55555555,d2
 and.l #$55555555,d3
 lsl.l #1,d2
 or.l d3,d2
;---
 cmp.w #Identification,d2
 bne.s ReadLoop
 swap d2
 move.b d2,d7
 lsr.w #8,d2
 cmp.b StartTrack,d2
 bne.s ReadLoop
;---
 move.l DataBuffer,a1
 move.w #$5bf,d4
CopyLoop2: move.l (a2)+,d2
 move.l (a2)+,d3
 and.l #$55555555,d2
 and.l #$55555555,d3
 lsl.l #1,d2
 or.l d3,d2
 move.l d2,(a1)+
 dbra d4,CopyLoop2
;---
 move.l DataBuffer,a1
 clr.b d2
 move.w #$16ff,d3
CheckSumLoop2: move.b (a1)+,d4
 add.b d3,d4
 eor.b d4,d2
 dbra d3,CheckSumLoop2
 cmp.b d2,d7
 bne ReadLoop
;---
 add.l #$1700,DataBuffer
 addq.b #1,StartTrack
 subq.b #1,NumTracks
 bne ReadLoop
;---
 move.l DiskBuffer,a1
 move.l #$2f00,d0
 jmp _LVOFreeMem(a6)
DiskReadBuffer: bsr WaitReadySignal
 lea CustomBase,a0
 move.w CustomSync,dsksync(a0)
 move.w #$8400,adkcon(a0)
 move.w #$4000,dsklen(a0)
 move.l DiskBuffer,dskpt(a0)
 move.w #$9780,dsklen(a0)
 move.w #$9780,dsklen(a0)
 jsr _LVODisable(a6)
 move.w #$0002,intreq(a0)
WaitForRDMA: move.w intreqr(a0),d2
 btst #1,d2
 beq.s WaitForRDMA
 move.w d2,intreq(a0)
 move.w #$4000,dsklen(a0)
 jsr _LVOEnable(a6)
 move.w #$0400,adkcon(a0)
 rts
;---
PrintOut: move.l a6,-(sp)
 move.l DOSBase,a6
 move.l a0,-(sp)
 jsr _LVOOutput(a6)
 move.l (sp)+,a0
 move.l d0,d1
 move.l a0,d2
 move.l a0,a1
WaitEndString: tst.b (a1)+
 bne.s WaitEndString
 suba.l a0,a1
 move.l a1,d3
 subq.l #1,d3
 jsr _LVOWrite(a6)
 move.l (sp)+,a6
 rts
;---
HandleInfo: move.l a0,FilePoint
 lea DOSLibName,a1
 move.l ExecBase,a6
 jsr _LVOOldOpenLibrary(a6)
 move.l d0,DOSBase
 lea TitleText,a0
 bsr.s PrintOut
;---
FileLoop: move.l ExecBase,a6
 move.l FilePoint,a0
 move.l (a0)+,d0
 beq EverythingDone
 move.l a0,FilePoint
 move.l d0,a0
 move.w (a0)+,CustomSync
 move.b (a0)+,StartTrack
 move.b (a0)+,NumTracks
 move.b (a0)+,DriveNumber
 move.b #WriteMode,SelectMode
 move.l a0,-(sp)
;---
 clr.l d0
 move.b NumTracks,d0
 mulu #$1700,d0
 move.l #MEMF_CLEAR!MEMF_PUBLIC,d1
 jsr _LVOAllocMem(a6)
 move.l (sp)+,a0
 move.l d0,DataBuffer
 beq.s FileNotFound
 move.l a0,-(sp)
 bsr PrintOut
 lea NextLine,a0
 bsr PrintOut
 move.l (sp)+,d1
 move.l #MODE_OLDFILE,d2
 move.l DOSBase,a6
 jsr _LVOOpen(a6)
 move.l d0,d1
 beq.s FileNotFound
 move.l d1,-(sp)
 move.l DataBuffer,d2
 clr.l d3
 move.b NumTracks,d3
 mulu #$1700,d3
 jsr _LVORead(a6)
 move.l (sp)+,d1
 jsr _LVOClose(a6)
;---
 move.l #250,d1
 jsr _LVODelay(a6)
;---
 bsr EntryII
;---
 move.l DataBuffer,a1
 clr.l d0
 move.b NumTracks,d0
 mulu #$1700,d0
 move.l ExecBase,a6
 jsr _LVOFreeMem(a6)
FileNotFound: bra FileLoop
;---
EverythingDone: move.l DOSBase,a1
 move.l ExecBase,a6
 jsr _LVOCloseLibrary(a6)
 clr.l d0
 rts
;---
HandleReadInfo: move.l a0,FilePoint
;---
FileReadLoop: move.l FilePoint,a0
 move.l (a0)+,d0
 beq EverythingRead
 move.l a0,FilePoint
 move.l d0,a0
 move.w (a0)+,CustomSync
 move.l (a0)+,DataBuffer
 move.b (a0)+,StartTrack
 move.b (a0)+,NumTracks
 move.b (a0)+,DriveNumber
 move.b #ReadMode,SelectMode
;---
 bsr EntryII
 bra.s FileReadLoop
EverythingRead: rts
;---
CustomSync: dc.w 0
DiskBuffer: dc.l 0
DiskMsgPort: dc.l 0
IOStdReq: dc.l 0
DataBuffer: dc.l 0
ErrorFlag: dc.b 0
NumTracks: dc.b 0
StartTrack: dc.b 0
DriveNumber: dc.b 0
FilePoint: dc.l 0
DOSBase: dc.l 0
SelectMode: dc.b 0
TrackDiskName: TD_NAME
DOSLibName: DOSNAME
NextLine: dc.b $0a,0
TitleText: dc.b "Fremdformat 1.00 - (c) by Markt und Technik"
 dc.b 10,"geschrieben von Thomas Lopatic",10,10
 dc.b "Bearbeitete Files:",10,0
