;ADFS floppy image file system
;-----------------------------
;1772 emulation of ADF / APD / JFD floppy images, part of the
;Archimedes Software Preservation Project (JASPP)
;
;Author: Jonathan Abbott
; eMail: jon@jaspp.org.uk

;#set     compile_RO  = 311		;IOC 26bit OS
;#set     compile_RO  = 400		;IOMD 26bit OS
;#set     compile_RO  = 450		;IOMD 32bit OS
;#set     compile_RO  = 500		;ARMv5/6 32bit OS
;#set     compile_RO  = 550		;ARMv7 32bit OS

#set    module_version = 264

#speed 1024

#strset a_v = STR$(module_version)
#strset version_string = LEFT$(a_v, 1) + "." + RIGHT$(a_v,2) + " (" + MID$(TIME$, 5, 11) + ") " + compile_title
#if High_Vectors == 0 AND compile_RO >= 500
#strset version_string = version_string + " low vector"
#endif
#if High_Vectors == 1
#strset version_string = version_string + " high vector"
#endif
#if compile_DEBUG == 1
#strset version_string = version_string + " (Debug)"
#endif

#set VSync_at_Key_Scan		= 0	;1=add VSync(s) if required when keys are
					;  scanned via OS_Byte 121, 122 or 129
#set Recording_immediate	= 1	;1=save recordings after each DiscOp

					;************* DEBUGGING **************
#set Save_MiscOpValues		= 0	;1=saves MiscOp values to recording file
#set Display_DiscOpValues	= 0	;1=display DiscOp values as they happen
#set CTRL_PauseDebug		= 0	;1=CTRL pauses debugging info
#set lock_machine_on_abort	= 0	;1=will lock the machine when an
					;  unhandled abort occurs
#set Trap_ModeExtensions	= 0	;1=abort for ModeExtensions
#set No_device_9_IRQ		= 0	;1=dont raise sound IRQs
#set JIT_dont_reinit_BASIC	= 0	;1=dont RMREINIT BASIC on a JIT abort
#set instruction_trace_ptr	= &880000	;address-4 used to store the instruction trace

#if compile_DEBUG == 1 AND compile_RO >= 400
  #set JIT_debug		= 1	;show JIT debug info
  #set JIT_detailed_debug	= 1	;update every instr. and show inst.
  #set debug_overlay		= 1	;1 will overlay debug text
  #set JIT_instruction_trace	= 1	;stores an instruction *JITTRACE to view
  #set JIT_prevent_OS_EnterOS	= 0	;1=report, 2=prevent code from changing CPU mode
  #set JIT_prevent_Exit_Handlers = 0	;prevent Exit handlers
  #set JIT_prevent_Error_Handler = 0	;prevent Error handlers
  #set JIT_allow_Breakpoints	= 0	;allow Breakpoints to work
  #set JIT_dump_crash		= 0	;saves 8000-1B80000 when JIT breakpoint
					;is reached
  #set JIT_report_zeros		= 0	;report an instr. address containing 0
  #set JIT_leave_voices		= 0	;doesn't kill installed voices on abort
#else
  #set JIT_debug		= 0
  #set JIT_detailed_debug	= 0
  #set debug_overlay		= 0
  #set JIT_instruction_trace	= 0
  #set JIT_prevent_OS_EnterOS	= 0
  #set JIT_prevent_Exit_Handlers = 0
  #set JIT_prevent_Error_Handler = 0
  #set JIT_allow_Breakpoints	= 0
  #set JIT_dump_crash		= 0
  #set JIT_report_zeros		= 0
  #set JIT_leave_voices		= 0
#endif
					;**************************************
#if compile_RO > 400
  #set swi_flag_preservation	= 1	;1=preserve flags across SWI calls
  #set MMUControl2_support	= 1	;1=use RO5.23 ArmOp's
#else
  #set swi_flag_preservation	= 0
  #set MMUControl2_support	= 0
#endif

#if compile_RO > 311
  #set JIT_self_modifying_support = 1	;1=enabled
  #set JIT_flush_TLB		= 1	;1=flushes the TLB after changing page
					;  permissions
  #set JIT_codelet_recycling	= 1	;1=recycle codelets
  #set JIT_codelet_reuse	= 1	;1=reuse codelets for identical instr.
  #set Managed_ChannelHandler	= 1	;if 1, ADFFS sits between SoundDMA and
					;SoundChannels to fix differences in
					;Buffer length and period and provides
					;emulation of MEMC sound DMA
  #set JIT_predictive_branching	= 0	;if 1, B / BL are walked by the JIT
  #set JIT_WIMP_support		= 1	;1=support WIMP tasks
  #set JIT_pipelining		= 1	;1=JIT implements pipelining
  #set call_vectors_in_USER	= 0	;1=vectors run as USER, not SVC or IRQ
  #set Private_CLib		= 1	;1=load local instance of CLib
  #set Private_BASIC		= 1	;1=load local instance of BASIC
#else
  #set JIT_debug		= 2	;ignore code
  #set JIT_self_modifying_support = 2	;ignore code
  #set JIT_flush_TLB		= 2	;ignore code
  #set swi_flag_preservation	= 2	;ignore code
  #set JIT_codelet_recycling	= 2	;ignore code
  #set JIT_codelet_reuse	= 2	;ignore code
  #set Managed_ChannelHandler	= 0
  #set JIT_predictive_branching	= 2	;ignore code
  #set JIT_WIMP_support		= 2	;ignore code
  #set JIT_pipelining		= 2	;ignore code
  #set MMUControl2_support	= 2	;ignore code
  #set JIT_instruction_trace	= 2	;ignore code
  #set Private_CLib		= 2	;ignore code
  #set Private_BASIC		= 2	;ignore code
  #set JIT_leave_voices		= 2	;ignore code
  #set call_vectors_in_USER	= 2	;ignore code
  #set compile_IOMD		= 2	;ignore code
#endif

#if JIT_instruction_trace == 1
  #set JIT_codelet_reuse	= 0	;force off when instruction tracing
#endif

#if compile_RO > 311 AND compile_RO < 500
  #set No_Frame_Pacing		= 1	;turn frame pacing off for IOC/IOMD
  #set compile_IOMD		= 1
  #set ARMv3_support		= 1	;1=use speedups for ARM600/610/700/710
#else
  #set No_Frame_Pacing		= 1	;turn frame pacing off for IOC
  					;(breaks audio in GODS + a few others)
					;When on, some games will flicker
					; Chuck Rock for example
  #set ARMv3_support		= 2	;ignore code
#endif

#if compile_RO == 500
  #set No_Frame_Pacing		= 0	;1=turn frame pacing off
  #set compile_GPU		= 1
  #set compile_IOMD		= 0
  #set power_save_processor	= 0	;1=wait for an IRQ after blitting
  #set Cached_DA2		= 1	;1=enable caching/buffering on DA2/GPU
  #set ZeroPain			= 1	;1=silently allow Page Zero access
  #if compile_DEBUG == 1 AND compile_RO == 500
    #set ZeroPain		= 0
  #endif
  #if ARMv7_support == 1 AND compile_RO == 500
    #set Unaligned_Support	= 1
  #endif
  #if ARMv7_support == 0 AND compile_RO == 500
    #set Unaligned_Support	= 0	;ARMv6 and lower support unaligned
  #endif
  #set PLL_vsync		= 1	;1=PLL based VSync, 0=TickerV based VSync
#else
  #set compile_GPU		= 0
  #set power_save_processor	= 0
  #set Cached_DA2		= 2	;ignore code
  #set ZeroPain			= 2	;ignore code
  #set ARMv7_support		= 2	;ignore code
  #set Unaligned_Support	= 2	;ignore code
  #set PLL_vsync		= 2	;ignore code
#endif

#if compile_RO == 400
  #set JIT_protect_page_zero	= 1	;protect page zero
#else
  #set JIT_protect_page_zero	= 0	;no need to protect page zero
#endif

#if compile_RO == 311
  #set update_VIDC_softcopy	= 0
  #set update_Cursor_softcopy	= 0
#else
  #set update_VIDC_softcopy	= 1	;update OS softcopies of VIDC registers
  #set update_Cursor_softcopy	= 1	;update OS softcopies of VIDC registers
#endif

#base 0
#type &FFA

#area external_constants .readonly
  #include "<Tmp$Dir>.riscos.constants"
  #include "<Tmp$Dir>.zlib.constants"
#end

#include "<Tmp$Dir>.adffs.header"
#include "<Tmp$Dir>.adffs.SWItable"
#include "<Tmp$Dir>.adffs.SWI"

#area global_constants .readonly
  #include "<Tmp$Dir>.adffs.constants"
  #include "<Tmp$Dir>.adffs.errtable"
  #include "<Tmp$Dir>.adffs.file_types.JFD_struct"
#end

ALIGN   32, 4
#include "<Tmp$Dir>.adffs.global"
#include "<Tmp$Dir>.adffs.service"
#include "<Tmp$Dir>.adffs.vectors.vectors"
#include "<Tmp$Dir>.adffs.vectors.disc_swap"
ALIGN   32, 4
#include "<Tmp$Dir>.adffs.vectors.eventv"
#include "<Tmp$Dir>.adffs.vectors.swi"
#include "<Tmp$Dir>.adffs.vectors.swi_OS_ReadMonotonicTime"
#include "<Tmp$Dir>.adffs.vectors.swi_Filecore_Create"
#include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_19"
#include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_113"
#if compile_RO > 311
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_ChangeDynamicArea"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_ReadDynamicArea"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_DynamicArea"
#endif
#include "<Tmp$Dir>.adffs.vectors.swi_Sound_Stereo"
#if Managed_ChannelHandler == 1
  #include "<Tmp$Dir>.adffs.vectors.swi_Sound_Configure"
  #include "<Tmp$Dir>.adffs.vectors.swi_Sound_SampleRate"
#endif
#if compile_RO == 400
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_ClaimDeviceVector"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_ReleaseDeviceVector"
#endif
ALIGN   32, 4
#include "<Tmp$Dir>.adffs.vars"
ALIGN   32, 4
#include "<Tmp$Dir>.adffs.file_types.APD"
#include "<Tmp$Dir>.adffs.file_types.JFD"
#include "<Tmp$Dir>.adffs.file_types.ADF"
ALIGN   32, 4
#include "<Tmp$Dir>.adffs.adfs.miscop"
ALIGN   32, 4
#include "<Tmp$Dir>.adffs.adfs.discop"
#include "<Tmp$Dir>.adffs.commands.format"
#include "<Tmp$Dir>.adffs.commands.openmount"
#include "<Tmp$Dir>.adffs.commands.eject"
#include "<Tmp$Dir>.adffs.commands.flush"
#include "<Tmp$Dir>.adffs.commands.save"
#include "<Tmp$Dir>.adffs.commands.recording"
#include "<Tmp$Dir>.adffs.commands.bootfloppy"
#include "<Tmp$Dir>.adffs.commands.protectmodules"
#include "<Tmp$Dir>.adffs.commands.protectcmos"
#include "<Tmp$Dir>.adffs.commands.hourglass"
#if compile_RO > 311
  #include "<Tmp$Dir>.adffs.commands.remapvideomemory"
#endif
#if compile_RO == 400
  #include "<Tmp$Dir>.adffs.commands.screencaching"
#endif
#include "<Tmp$Dir>.adffs.commands.forcevsync"
#include "<Tmp$Dir>.adffs.commands.writeprotect"
#include "<Tmp$Dir>.adffs.commands.emulatefdc"
#include "<Tmp$Dir>.adffs.commands.memalloc"
#include "<Tmp$Dir>.adffs.commands.reservememory"
#include "<Tmp$Dir>.adffs.commands.donothing"
#include "<Tmp$Dir>.adffs.cpuid"
#include "<Tmp$Dir>.adffs.module"

#include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte"
#include "<Tmp$Dir>.adffs.vectors.swi_OS_Module"

#if compile_RO == 311
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_DynamicAreaRO31x"
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_ChangeDynamicAreaRO31x"
#endif

#if compile_RO > 311
 OUTPUT "Start: Abort handlers"
 ALIGN   32, 4
 OUTPUT "           - core"
 #include "<Tmp$Dir>.adffs.jitarm3.core"
 OUTPUT "           - cache_ops"
 #include "<Tmp$Dir>.adffs.jitarm3.cache_ops"
 OUTPUT "                     - abort_handler"
 #include "<Tmp$Dir>.adffs.jitarm3.abort_handler"
 OUTPUT "                     - data_abort"
 #include "<Tmp$Dir>.adffs.aborts.data_abort"
 OUTPUT "                     - vidc1_abort"
 #include "<Tmp$Dir>.adffs.aborts.vidc1_abort"
 #if compile_GPU == 0 AND compile_RO > 311
   OUTPUT "                     - mouse_ptr_abort"
   #include "<Tmp$Dir>.adffs.aborts.mouse_ptr_abort"
 #endif
 OUTPUT "                     - page_zero_abort"
 #if High_Vectors == 1
   #include "<Tmp$Dir>.adffs.aborts.page_zero_abort_HV"
 #endif
 #if High_Vectors == 0
   #include "<Tmp$Dir>.adffs.aborts.page_zero_abort"
 #endif
 OUTPUT "                     - memc_abort"
 #include "<Tmp$Dir>.adffs.aborts.memc_abort"
 OUTPUT "                     - ioc_abort"
 #include "<Tmp$Dir>.adffs.aborts.ioc_abort"
 OUTPUT "                     - vidc20_abort"
 #include "<Tmp$Dir>.adffs.aborts.vidc20_abort"
 OUTPUT "End: Abort handlers"
#endif

#if compile_IOMD == 1
  .hwd_framestore DCD 0
#endif

  #include "<Tmp$Dir>.adffs.blitter.macros"
#if compile_GPU == 1
  OUTPUT "Start: Blitter"
  ALIGN   32, 4
  #include "<Tmp$Dir>.adffs.vectors.graphicsV"
  #include "<Tmp$Dir>.adffs.blitter.blitter"
  OUTPUT "End: Blitter"
#endif

  #include "<Tmp$Dir>.adffs.jitarm3.macros"
#if compile_RO > 311
  OUTPUT "Start: JIT"
  OUTPUT "           - jitmemorya"
  #include "<Tmp$Dir>.adffs.commands.jitmemorya"
  OUTPUT "           - goarm3jit"
  #include "<Tmp$Dir>.adffs.commands.goarm3jit"
  OUTPUT "           - alu"
  #include "<Tmp$Dir>.adffs.jitarm3.alu"
  OUTPUT "           - bl"
  #include "<Tmp$Dir>.adffs.jitarm3.bl"
  OUTPUT "           - ldr"
  #include "<Tmp$Dir>.adffs.jitarm3.ldr"
  OUTPUT "           - ldm"
  #include "<Tmp$Dir>.adffs.jitarm3.ldm"
  OUTPUT "           - mov"
  #include "<Tmp$Dir>.adffs.jitarm3.mov"
  OUTPUT "           - str"
  #include "<Tmp$Dir>.adffs.jitarm3.str"
  OUTPUT "           - stm"
  #include "<Tmp$Dir>.adffs.jitarm3.stm"
  OUTPUT "           - swi"
  #include "<Tmp$Dir>.adffs.jitarm3.swi"
  #if Private_CLib == 0
    OUTPUT "           - swi_clib"
    #include "<Tmp$Dir>.adffs.jitarm3.swi_clib"
  #endif
  OUTPUT "           - b"
  #include "<Tmp$Dir>.adffs.jitarm3.b"
  OUTPUT "           - ldf_stf"
  #include "<Tmp$Dir>.adffs.jitarm3.ldf_stf"
  OUTPUT "           - mul"
  #include "<Tmp$Dir>.adffs.jitarm3.mul"
  OUTPUT "           - hypervised_swi"
  #include "<Tmp$Dir>.adffs.jitarm3.hypervised_swi"
  OUTPUT "           - IRQ_handler"
  #include "<Tmp$Dir>.adffs.jitarm3.IRQ_handler"
  OUTPUT "           - branch_thru_zero"
  #include "<Tmp$Dir>.adffs.jitarm3.branch_thru_zero"
  OUTPUT "           - codelets"
  #include "<Tmp$Dir>.adffs.jitarm3.codelets"
  OUTPUT "           - copro"
  #include "<Tmp$Dir>.adffs.jitarm3.copro"
  OUTPUT "           - swi"
  #if ARMv7_support == 1
    OUTPUT "           - swp"
    #include "<Tmp$Dir>.adffs.jitarm3.swp"
  #endif
  OUTPUT "           - environment handlers"
  #include "<Tmp$Dir>.adffs.jitarm3.environment_handlers"
  OUTPUT "End: JIT"
#endif

#if No_Frame_Pacing == 0
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_13_14"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_176"
#endif
#include "<Tmp$Dir>.adffs.vectors.swi_OS_Mouse"
#if VSync_at_Key_Scan == 1
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_121_122_129"
#endif

  #include "<Tmp$Dir>.adffs.sound.macros"
#if compile_RO > 311
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_FSControl"
  #include "<Tmp$Dir>.adffs.sound.VoiceGenerators"
  #include "<Tmp$Dir>.adffs.commands.emulateRISCOS"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_ReadVduVariables"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_UpdateMEMC"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_129"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_SynchroniseCodeAreas"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_CallASWI"
  #include "<Tmp$Dir>.adffs.vectors.swi_OS_File"
#endif

#if Managed_ChannelHandler == 1
  #include "<Tmp$Dir>.adffs.sound.ChannelHandler"
#endif

#if compile_RO > 311
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_Word"
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_Word_21"
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_SpriteOp"
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_SpriteOp_3"
 #include "<Tmp$Dir>.adffs.vectors.swi_OS_SpriteOp_36"
 #include "<Tmp$Dir>.adffs.vectors.swi_Wimp_SlotSize"
#endif

#include "<Tmp$Dir>.adffs.commands.pause"
#include "<Tmp$Dir>.adffs.commands.voices"
#include "<Tmp$Dir>.adffs.vectors.swi_OS_Byte_253"
#include "<Tmp$Dir>.adffs.vectors.swi_OS_Claim"

#if compile_RO > 311
  #include "<Tmp$Dir>.adffs.jitarm3.modules"
  #include "<Tmp$Dir>.adffs.jitarm3.machine_state"
#endif

  #include "<Tmp$Dir>.debug.macros"
#if compile_RO > 311 OR Display_DiscOpValues == 1
  #include "<Tmp$Dir>.debug.debug"
#endif
#if JIT_debug == 1
  #include "<Tmp$Dir>.debug.jitbreakpoint"
#endif
#if JIT_debug == 1 AND JIT_instruction_trace == 1
  #include "<Tmp$Dir>.debug.jittrace"
#endif
.ADFFS_end

#area cmdtable .readonly
 .ADFFS_Commands
 #include "<Tmp$Dir>.adffs.cmdtable"
 #include "<Tmp$Dir>.adffs.cmdhelp"
 ALIGN
 #include "<Tmp$Dir>.adffs.memalloc"
#end

; PADDING TO WORKAROUND BUG IN extASM                                          ;
;                                                                              ;
;                                                                              ;
;                                                                              ;
;                                                                              ;
