@---------------------------------------------------------------------------------
	.section ".init"
@---------------------------------------------------------------------------------
	.align	4
	.arm
	.global _io_dlmi
	.global _start
@---------------------------------------------------------------------------------
.equ FEATURE_MEDIA,	0x00000001	@ is a media adapter
.equ FEATURE_RAMRW,	0x00000002	@ has writable ram
.equ FEATURE_RAMRO,	0x00000004	@ ram can be set to read-only
.equ FEATURE_FLASHRW,	0x00000008	@ has writable firmware
.equ FEATURE_FLASHRO,	0x00000010	@ flash can be set to read-only
.equ FEATURE_SRAM,	0x00000020	@ has SRAM
.equ FEATURE_RUMBLE,	0x00000040	@ has rumble motor
.equ FEATURE_MOTION,	0x00000080	@ has motion sensor(s)
.equ FEATURE_BOOT,	0x00000100	@ can be booted
.equ FEATURE_SERIAL,	0x00000200	@ has serial port

_dlmi_start:

@-- 0x0000 - 0x0007 --------------------------------------------------------------
@ Driver file standard header -- 8 bytes
	.word	0x494D4C44		@ Magic number to identify this region
	.word	0x00010000		@ Version number

@-- 0x0008 - 0x001F --------------------------------------------------------------
@ Text identifier - can be anything up to 23 chars + terminating null
	.asciz "EZ-Flash V 3-in-1"

@-- 0x0020 - 0x003F --------------------------------------------------------------
@ Card Identifiers - upto 8 cards
	.align  5
	.ascii  "EZ-V"

@-- 0x0040 - 0x???? --------------------------------------------------------------
@ IO_INTERFACE data -- 
	.align  6

_io_dlmi:
	.word	0x0000027e			@ Features
	.word	0x08000000			@ RAM Base
	.word	0x01000000			@ RAM Size
	.word	0x08000000			@ Flash Base
	.word	0x02000000			@ Flash Size
	.word	0x0A000000			@ SRAM Base
	.word	0x00010000			@ SRAM Size

@-- 0x0050 - 0x005F --------------------------------------------------------------
@ DLMI Pointers --
	.align 4

	.word	_EZ_Return_Error	@ Unlock Media
	.word	_EZ_Unlock_RAMRW	@ Unlock RAM R/W
	.word	_EZ_Return_Error	@ Unlock RAM R/O

	.word	_EZ_Return_Error	@ Flash Write
	.word	_EZ_Return_Error	@ Flash Read
	.word	_EZ_Return_Error	@ SRAM Unlock

	.word	_EZ_Return_Error	@ Rumble
	.word	_EZ_Return_Error	@ Motion
	.word	_EZ_Return_Error	@ Unlock Boot

	.word	_EZ_Return_Error	@ Serial Write
	.word	_EZ_Return_Error	@ Serial Read


_EZ_Return_Error:
	mov		r0, #0
	sub		r0, r0, #1
	bx		lr

#define _EZ_UNLOCK_ADDR_A	0x9fe0000
#define _EZ_UNLOCK_ADDR_B	0x8000000
#define _EZ_UNLOCK_ADDR_C	0x8020000
#define _EZ_UNLOCK_ADDR_D	0x8040000
#define _EZ_LOCK_ADDR		0x9fc0000

#define _EZ_NOROpenClose_ADDR	0x9c40000
#define _EZ_NOROpen_DATA	0x1500
#define _EZ_NORClose_DATA	0xd200

#define _EZ_RAMOpenClose_ADDR	0x9c40000
#define _EZ_RAMOpen_DATA	0xa500
#define _EZ_RAMClose_DATA	0xa200

@ void _ez_unlock(u32 reg, u16 val);

_EZ_unlock:
	ldr		r3, =_EZ_UNLOCK_ADDR_A
	mov		r2, #0xd200
	strh		r2, [r3]

	ldr		r3, =_EZ_UNLOCK_ADDR_B
	mov		r2, #0x1500
	strh		r2, [r3]

	ldr		r3, =_EZ_UNLOCK_ADDR_C
	mov		r2, #0xd200
	strh		r2, [r3]

	ldr		r3, =_EZ_UNLOCK_ADDR_D
	mov		r2, #0x1500
	strh		r2, [r3]

	strh		r1, [r0]

	ldr		r3, =_EZ_LOCK_ADDR
	mov		r2, #0x1500
	strh		r2, [r3]

	bx		lr

_LR_TEMP:
	.word	0x00000000

@ Not sure about this part.
@_EZ_Serial:
@	mov		r3, #FEATURE_SERIAL	@ return value
@	adr		r2, _CurrentMode
@	str		r3, [r2]
@
@	mov		r1, #0xe200
@	ldr		r0, =_EZ_SERIAL_CODE
@	bl		_EZ_Unlock
@	adr		r3, _LR_TEMP
@	ldr		lr, [r3]
@	bx		lr

.macro OpenNOR
	mov		r1, #_EZ_NOROpen_DATA
	ldr		r0, =_EZ_NOROpenClose_ADDR
	bl		_EZ_Unlock
.endm

.macro CloseNOR
	mov		r1, #_EZ_NORClose_DATA
	ldr		r0, =_EZ_NOROpenClose_ADDR
	bl		_EZ_Unlock
.endm

.macro OpenRAM
	mov		r1, #_EZ_RAMOpen_DATA
	ldr		r0, =_EZ_RAMOpenClose_ADDR
	bl		_EZ_Unlock
.endm

.macro CloseRAM
	mov		r1, #_EZ_RAMClose_DATA
	ldr		r0, =_EZ_RAMOpenClose_ADDR
	bl		_EZ_Unlock
.endm

.macro RAMPage
	ldr		r0, =_EZ_RAMPage_ADDR
	bl		_EZ_Unlock
.endm

.macro ROMPage						
	ldr		r0, =_EZ_ROMPagee_ADDR		
	bl		_EZ_Unlock
.endm

.macro _Save_Mode
	adr		r2, _CurrentMode
	str		r0, [r2]
.endm

.macro _Get_Mode
	adr		r2, _CurrentMode
	ldr		r0, [r2]
.endm

.macro _Save_LR
	adr		r2, _LR_TEMP
	str		lr, [r2]
.endm

.macro _Get_LR
	adr		r2, _LR_TEMP
	ldr		lr, [r2]
.endm

_EZ_Unlock_Rumble:
	_Save_LR

	ldr		r0, =_EZ_Rumble_ADDR
	add		r1, #0xef
	bl		_EZ_Unlock

	_Get_Mode
	_Get_LR

	bx		lr

_EZ_Return_Mode:
	_Get_Mode
	bx		lr

_EZ_Unlock_RAMRW:
	_Save_LR

	mov		r1, #384

	ROMPage
	OpenNOR

	mov		r0, #0x22

	_Save_Mode
	_Get_LR

	bx		lr

@ Assume that fatlib has put the card in media mode
_CurrentMode:
	.word		0x00000021

@---------------------------------------------------------------------------------
	.align
	.pool
_dlmi_end:
_start:
	.end
@---------------------------------------------------------------------------------

