; Standard RM Header, plus initialisation and finalisation.  For the whole
; thing to linkable as a binary image, and still come out as a working
; module this file MUST generate the first code of all.

                GET             hdr.Common
                GET             Asm:s.date

                AREA            |$Aardvark|, PIC, CODE

                IMPORT          Register
                IMPORT          Unregister
                IMPORT          Commands
                IMPORT          StopSharedSound
                IMPORT          ReleaseAudioBuffer

; As mentioned above, this next bit's GOT to be the first bit of code in the
; final linked output, for obvious reasons.

                &               0
                &               Initialise
                &               Finalise
                &               0               ;was Service
                &               Title
                &               Help
                &               Commands
                &               0               ; SWIChunk
                &               0               ; SWIHandler
                &               0               ; SWI table
                &               0               ; SWIDecode
                &               0               ; message file
                &               is32Bit         ; 32bit flag

Title           =               "CDFSSoftCDFaker", 0

Help            =               "CDFSSoftCDFaker", 9
                =               "0" + (ThisVersion / 100) :MOD: 10, "."
                =               "0" + (ThisVersion / 10) :MOD: 10
                =               "0" + ThisVersion :MOD: 10
                =               " ($MakeDate)"
                [               ThisVersion < 100
                =               " pre-release, not for sale"
                ]
                [               Debug
                =               " - debugging version"
                ]
                =               0
                ALIGN
is32Bit         =               1

Initialise      SAVE            "r0-r7"

                DBSET           DebugOn :OR: UseTracker
                DBF             "\fStarting\n\n"

Init1           LDR             r2, [ws]
                LDR             r3, = :INDEX: StoreSize
                TEQ             r2, #0
                BNE             Init2

; First time initialisation, so try to get enough RAM

                MOV             r0, #6
                SWI             XOS_Module
                RETURN_IF_ERR

                STR             r2, [ws]
Init2           MOV             ws, r2

                MOV             r0, #0
Init3           STR             r0, [r2], #word
                SUBS            r3, r3, #word
                BGT             Init3

                ADR             r2, DriveBlock
                MOV             r0, #0
                MOV             r4, #0
                MOV             r3, #-1

Init4
                STR             r3, [r2, #Drive_ID]
                STR             r3, [r2, #Drive_Card]
                STR             r3, [r2, #Drive_LUN]
		STR		r3, [r2, #Drive_ImageType]
                ADD             r2, r2, #Drive_Size :AND: &00FF
                ADD             r2, r2, #Drive_Size :AND: &FF00
                ADD             r0, r0, #1
                TEQ             r0, #MaxDrive
                BNE             Init4

                MOV             r0, #1
                STR             r0, Drives 

                BL              Register
                RETURN_ERR


Finalise        SAVE            "r0-r4, r10"
                LDR             ws, [ws]
                BL              Unregister
                ADR             r10, DriveBlock
                MOV             r4, #MaxDrive

Finalise1	BL              StopSharedSound         
                BL              ReleaseAudioBuffer

                LDR             r2, [r10, #Drive_Emulation]
                TEQ             r2, #0
                MOVNE           r0, #7
                SWINE           XOS_Module

		LDR		r2, [r10, #Drive_BINFiles]
                TEQ             r2, #0
                MOVNE           r0, #7
                SWINE           XOS_Module

                ADD             r10, r10, #Drive_Size :AND: &00FF
                ADD             r10, r10, #Drive_Size :AND: &FF00
                SUBS            r4, r4, #1
                BNE             Finalise1 

                RETURN

                END
