' 2005 (c) by Nohau
' Philips LPC3000 NAND Flash controller programming option setup
' Programmer: Mike Quirk
'

Dim PageSizeArray$(1) 			'NAND Flash Page Size Mode
Dim DeviceSizeArray$(8)			'Currently know device sizes available in descriptive format
Dim Pgm_SizeArray$(8)			'Secondary array of sizes for passing to Seehau command structure
Dim ControllerArray$ (1)		'NAND Flash Controller type Selections: SLC or MFC
Dim EccLocArray$(15)			'ECC locations for SLC mode: 6 bytes required

' Setup for the possiblity of 4 images to load at one time.
Dim Program0$ (12)				'Image file 0 settings
Dim Program1$ (12)				'Image file 1 settings
Dim Program2$ (12)				'Image file 2 settings
Dim Program3$ (12)				'Image file 3 settings
'Structure Programx$ {
'	Enabled
'	SecondBootLoader
'	TargetBlock
'	Controller
'	EccGeneration
'	EccLocM10
'	EccLocM11
'	EccLocM12
'	EccLocM20
'	EccLocM21
'	EccLocM22
'	BinaryFile
'}
'
'
Dim WorkingProgram$ (12)			'Used to simplify the code during loop operation
Dim WorkingDeviceSize As String		'
Dim WorkingPageSize As String		'
Dim WorkingDeviceLock As String		'

'Temp Flags
Dim CurPgmNum As Integer			
Dim EnableEcc As Integer

Sub Main
	Initialize_Arrays				'Pre-Initialize the global arrays used for selections in the UserDialog screens

	If(Wnd_IsEmulRunning) Then
		MsgBox "Emulator is Running and will be stopped!",vbOkOnly,"WARNING: Wnd_ISEmulRunning"
		Run_Break
		While (Wnd_IsEmulRunning)
			While (Wnd_IsEmulRunning)
			Wend
		Wend
	End If

	GetSavedSettings				'Check and recover previous settings for each of the 4 image files and Global parameters

Restart:

	Begin Dialog UserDialog 630,343,"NAND Flash Programming",.DialogFuncMain ' %GRID:10,7,1,1
		GroupBox 10,133,610,42,"Image 0",.GroupBox4
		GroupBox 10,259,610,42,"Image 3",.GroupBox3
		GroupBox 10,7,610,126,"Global Settings",.GlobalSettings
		DropListBox 30,42,170,70,PageSizeArray(),.DevicePageSize
		DropListBox 220,42,210,77,DeviceSizeArray(),.DeviceSize
		CheckBox 460,42,150,14,"Device is Lockable",.Lockable
		Text 40,28,120,14,"Device Page Size",.Text3
		Text 230,28,120,14,"Flash Device Size",.Text4
		GroupBox 10,217,610,42,"Image 2",.GroupBox1
		GroupBox 10,175,610,42,"Image 1",.GroupBox2
		CheckBox 20,154,140,14,"Program Enable",.EnablePgm0
		CheckBox 20,238,140,14,"Program Enable",.EnablePgm2
		CheckBox 20,280,140,14,"Program Enable",.EnablePgm3
		CheckBox 20,196,140,14,"Program Enable",.EnablePgm1
		TextBox 300,147,310,21,.ImageZero
		TextBox 300,189,310,21,.ImageOne
		TextBox 300,231,310,21,.ImageTwo
		TextBox 300,273,310,21,.ImageThree
		PushButton 180,147,110,21,"Select File 0 >>",.PBF0
		PushButton 180,189,110,21,"Select File 1 >>",.PBF1
		PushButton 180,231,110,21,"Select File 2 >>",.PBF2
		PushButton 180,273,110,21,"Select File 3 >>",.PBF3
		PushButton 50,315,130,21,"&Program Image(s)",.ProgramGo
		PushButton 250,315,140,21,"&Abort Programming",.Abort
		PushButton 440,315,140,21,"&Erase NAND Flash",.EraseNF
	End Dialog
	Dim dlg As UserDialog
'
' Special Operation Notes:
' if Secondary Boot Loader
'	Default target block to 0
'	Use MLC Controller type
'	No Ecc operation at all
	ReturnValue = Dialog (dlg)
	'Return value 5 = ProgramGo
	'Return value 6 = Abort
	'Return Value 7 = EraseNF
	'
	'Set the Global Variables
	'
   	WorkingDeviceSize = Pgm_SizeArray$(dlg.DeviceSize)
   	If dlg.DeviceSize = 1 Then
   		WorkingPageSize = "NF_LARGE"
   	Else
   		WorkingPageSize = "NF_SMALL"
   	End If
	If dlg.Lockable = 1 Then
		WorkingDeviceLock = "Yes"
	Else
		WorkingDeviceLock = "No"
	End If
	Select Case ReturnValue
		Case 5
			RunProgram
			Exit Sub
		Case 6
			Exit Sub
		Case 7
			EraseNANDFlash
			Exit Sub
	End Select
End Sub

Function DialogFuncMain%(DlgItem$, Action%, SuppValue%)
	Select Case Action%
    Case 1 ' Dialog box initialization

		'First the Enable Program boxes
   		If Program0$(0) = "Yes" Then
   			DlgValue "EnablePgm0",1
   		End If
   		If Program1$(0) = "Yes" Then
   			DlgValue "EnablePgm1",1
   		End If
   		If Program2$(0) = "Yes" Then
   			DlgValue "EnablePgm2",1
   		End If
   		If Program3$(0) = "Yes" Then
   			DlgValue "EnablePgm3",1
   		End If
   		DlgText "ImageZero",Program0$(11)
   		DlgText "ImageOne",Program1$(11)
   		DlgText "ImageTwo",Program2$(11)
   		DlgText "ImageThree",Program3$(11)
   		If WorkingPageSize = "NF_LARGE" Then
			DlgValue "DevicePageSize",1
		Else
			DlgValue "DevicePageSize",0
		End If
		If WorkingDeviceLock = "Yes" Then
			DlgValue "Lockable",1
		Else
			DlgValue "Lockable",0
		End If
		For sizeoptions = 0 To 8 Step 1
			If WorkingDeviceSize = Pgm_SizeArray$(sizeoptions) Then
				DlgValue "DeviceSize",sizeoptions
				sizeoptions = 9
			End If
		Next sizeoptions
        Beep
    Case 2 ' Value changing or button pressed
        Select Case DlgItem$
        	Case "PBF0"
        		DialogFuncMain% = True 'do not exit the dialog
        		LoadFile$ = GetFilePath$("","bin","","Open Binary Image File - Image 0 -")
        		DlgText "ImageZero", LoadFile$
				Program0$(11) = LoadFile$
        	Case "PBF1"
				DialogFuncMain% = True 'do not exit the dialog
        		LoadFile$ = GetFilePath$("","bin","","Open Binary Image File - Image 1 -")
        		DlgText "ImageOne", LoadFile$
				Program1$(11) = LoadFile$
        	Case "PBF2"
        		DialogFuncMain% = True 'do not exit the dialog
        		LoadFile$ = GetFilePath$("","bin","","Open Binary Image File - Image 2 -")
        		DlgText "ImageTwo", LoadFile$
				Program2$(11) = LoadFile$
        	Case "PBF3"
				DialogFuncMain% = True 'do not exit the dialog
        		LoadFile$ = GetFilePath$("","bin","","Open Binary Image File - Image 3 -")
        		DlgText "ImageThree", LoadFile$
				Program3$(11) = LoadFile$
        	Case "EnablePgm0"
        		If SuppValue = 1 Then
        			DialogFuncMain% = True 'do not exit the dialog
        			GetOptions(0)
        			Program0$ (0) = "Yes"
        		Else
        			Program0$ (0) = "No"
        		End If
        	Case "EnablePgm1"
        		If SuppValue = 1 Then
	        		DialogFuncMain% = True 'do not exit the dialog
    	    		GetOptions(1)
        			Program1$ (0) = "Yes"
        		Else
        			Program1$ (0) = "No"
    	    	End If
        	Case "EnablePgm2"
        		If SuppValue = 1 Then
	        		DialogFuncMain% = True 'do not exit the dialog
    	    		GetOptions(2)
        			Program1$ (0) = "Yes"
        		Else
        			Program1$ (0) = "No"
    	    	End If
        	Case "EnablePgm3"
        		If SuppValue = 1 Then
    	    		DialogFuncMain% = True 'do not exit the dialog
	        		GetOptions(3)
        			Program1$ (0) = "Yes"
        		Else
        			Program1$ (0) = "No"
	        	End If
	        Case "DevicePageSize"
	        	If SuppValue = 1 Then
	        		WorkingPageSize = "NF_LARGE"
	        	Else
	        		WorkingPageSize = "NF_SMALL"
	        	End If
	        Case "DeviceSize"
	        	ptr = Val(Str(SuppValue))
	        	WorkingDeviceSize = Pgm_SizeArray$(ptr)
	        Case "Lockable"
				If SuppValue = 1 Then
					WorkingDeviceLock = "Yes"
				Else
					WorkingDeviceLock = "No"
				End If
	        Case "Abort"
	        Case "EraseNF"
	        Case "ProgramGo"
		End Select
	End Select
End Function

Function GetOptions(ImagePgmNum%)
Dim Header As String
Select Case ImagePgmNum%
	Case 0
		CurPgmNum = ImagePgmNum
		Header = "Options for Image 0"
	Case 1
		CurPgmNum = ImagePgmNum
		Header = "Options for Image 1"
	Case 2
		CurPgmNum = ImagePgmNum
		Header = "Options for Image 2"
	Case 3
		CurPgmNum = ImagePgmNum
		Header = "Options for Image 3"
End Select
FixOptions:
	Begin Dialog UserDialog 430,287,Header,.DialogFuncSub ' %GRID:10,7,1,1
		CheckBox 20,14,190,14,"Secondary Boot Loader",.SBL
		Text 20,42,110,14,"Controller Type:",.CtrlrTypeText
		DropListBox 130,35,150,70,ControllerArray(),.ControllerType
		OKButton 180,259,90,21
		Text 20,70,130,14,"Starting Block #",.SBNText1
		TextBox 130,63,90,21,.BlockNumber
		GroupBox 10,98,410,147," ECC Generation Options",.EccOptions
		CheckBox 30,119,90,14,"Enable",.EnableEccGen
		Text 30,147,50,14,"M1[0]",.M10T
		Text 30,189,50,14,"M2[0]",.M20T
		Text 160,189,50,14,"M2[1]",.M21T
		Text 290,189,50,14,"M2[2]",.M22T
		Text 160,147,50,14,"M1[1]",.M11T
		Text 290,147,50,14,"M1[2]",.M12T
		DropListBox 80,140,70,98,EccLocArray(),.M10
		DropListBox 210,140,70,98,EccLocArray(),.M11
		DropListBox 340,140,70,98,EccLocArray(),.M12
		DropListBox 80,182,70,77,EccLocArray(),.M20
		DropListBox 210,182,70,70,EccLocArray(),.M21
		DropListBox 340,182,70,70,EccLocArray(),.M22
		TextBox 390,7,40,21,.PgmNumber
	End Dialog
	Dim dlg As UserDialog
	Dialog dlg
	Err.Clear
	On Error GoTo stayoptions
	'ECC locations can not overlap at all or fall into BAD block location.
	'
	If EnableEcc = 1 Then
		Check_EccLocations(dlg.M10,dlg.M11,dlg.M12,dlg.M20,dlg.M21,dlg.M22)
	End If
	Select Case CurPgmNum
		Case 0
			Program0$(2) = dlg.BlockNumber
			Program0$(5) = Trim(Hex$(Str(dlg.M10)))
			Program0$(6) = Trim(Hex$(Str(dlg.M11)))
			Program0$(7) = Trim(Hex$(Str(dlg.M12)))
			Program0$(8) = Trim(Hex$(Str(dlg.M20)))
			Program0$(9) = Trim(Hex$(Str(dlg.M21)))
			Program0$(10) = Trim(Hex$(Str(dlg.M22)))
		Case 1
			Program1$(2) = dlg.BlockNumber
			Program1$(5) = Trim(Hex$(Str(dlg.M10)))
			Program1$(6) = Trim(Hex$(Str(dlg.M11)))
			Program1$(7) = Trim(Hex$(Str(dlg.M12)))
			Program1$(8) = Trim(Hex$(Str(dlg.M20)))
			Program1$(9) = Trim(Hex$(Str(dlg.M21)))
			Program1$(10) = Trim(Hex$(Str(dlg.M22)))
		Case 2
			Program2$(2) = dlg.BlockNumber
			Program2$(5) = Trim(Hex$(Str(dlg.M10)))
			Program2$(6) = Trim(Hex$(Str(dlg.M11)))
			Program2$(7) = Trim(Hex$(Str(dlg.M12)))
			Program2$(8) = Trim(Hex$(Str(dlg.M20)))
			Program2$(9) = Trim(Hex$(Str(dlg.M21)))
			Program2$(10) = Trim(Hex$(Str(dlg.M22)))
		Case 3
			Program3$(2) = dlg.BlockNumber
			Program3$(5) = Trim(Hex$(Str(dlg.M10)))
			Program3$(6) = Trim(Hex$(Str(dlg.M11)))
			Program3$(7) = Trim(Hex$(Str(dlg.M12)))
			Program3$(8) = Trim(Hex$(Str(dlg.M20)))
			Program3$(9) = Trim(Hex$(Str(dlg.M21)))
			Program3$(10) = Trim(Hex$(Str(dlg.M22)))
		End Select
		Exit Function
stayoptions:
		MsgBox("   Error ECC Locations Settings   ",vbOkOnly,"NAND Flash Setup")
		GoTo FixOptions
End Function

Function DialogFuncSub(DlgItem$, Action%, SuppValue&) As Boolean
'Structure Programx$ {
'	Enabled
'	SecondBootLoader
'	TargetBlock
'	Controller
'	EccGeneration
'	EccLocM10
'	EccLocM11
'	EccLocM12
'	EccLocM20
'	EccLocM21
'	EccLocM22
'	BinaryFile
'}
Select Case Action%
	Case 1 ' Dialog box initialization
		DlgVisible "PgmNumber",False
		DlgText "PgmNumber",Str(CurPgmNum)
		If CurPgmNum = 0 Then
			DlgVisible "SBL",True
		Else
			DlgVisible "SBL",False
		End If
		Select Case CurPgmNum
			Case 0
				If Program0$(1) = "Yes" Then
					DlgValue "SBL",1
				Else
					DlgValue "SBL",0
				End If
				DlgText "BlockNumber",Program0$(2)
				If Program0$(3) = "NF_SLC" Then
					DlgValue "ControllerType",0
				Else
					DlgValue "ControllerType",1
				End If
				If Program0$(4) = "Yes" Then
					DlgValue "EnableEccGen",1
				Else
					DlgValue "EnableEccGen",0
				End If
				DlgValue "M10",Val(Program0$(5))
				DlgValue "M11",Val(Program0$(6))
				DlgValue "M12",Val(Program0$(7))
				DlgValue "M20",Val(Program0$(8))
				DlgValue "M21",Val(Program0$(9))
				DlgValue "M22",Val(Program0$(10))
			Case 1
				DlgValue "SBL",0
				DlgText "BlockNumber",Program0$(2)
				If Program1$(3) = "NF_SLC" Then
					DlgValue "ControllerType",0
				Else
					DlgValue "ControllerType",1
				End If
				If Program1$(4) = "Yes" Then
					DlgValue "EnableEccGen",1
				Else
					DlgValue "EnableEccGen",0
				End If
				DlgValue "M10",Val(Program1$(5))
				DlgValue "M11",Val(Program1$(6))
				DlgValue "M12",Val(Program1$(7))
				DlgValue "M20",Val(Program1$(8))
				DlgValue "M21",Val(Program1$(9))
				DlgValue "M22",Val(Program1$(10))
			Case 2
				DlgValue "SBL",0
				DlgText "BlockNumber",Program0$(2)
				If Program2$(3) = "NF_SLC" Then
					DlgValue "ControllerType",0
				Else
					DlgValue "ControllerType",1
				End If
				If Program2$(4) = "Yes" Then
					DlgValue "EnableEccGen",1
				Else
					DlgValue "EnableEccGen",0
				End If
				DlgValue "M10",Val(Program2$(5))
				DlgValue "M11",Val(Program2$(6))
				DlgValue "M12",Val(Program2$(7))
				DlgValue "M20",Val(Program2$(8))
				DlgValue "M21",Val(Program2$(9))
				DlgValue "M22",Val(Program2$(10))
			Case 3
				DlgValue "SBL",0
				DlgText "BlockNumber",Program0$(2)
				If Program3$(3) = "NF_SLC" Then
					DlgValue "ControllerType",0
				Else
					DlgValue "ControllerType",1
				End If
				If Program3$(4) = "Yes" Then
					DlgValue "EnableEccGen",1
				Else
					DlgValue "EnableEccGen",0
				End If
				DlgValue "M10",Val(Program3$(5))
				DlgValue "M11",Val(Program3$(6))
				DlgValue "M12",Val(Program3$(7))
				DlgValue "M20",Val(Program3$(8))
				DlgValue "M21",Val(Program3$(9))
				DlgValue "M22",Val(Program3$(10))
		End Select
    	Beep
	Case 2 ' Value changing or button pressed
		Select Case DlgItem$
			Case "SBL"
				If SuppValue = 1 Then
					Select Case CurPgmNum
						Case 0
							Program0$(1) = "Yes"
						Case 1
							Program1$(1) = "No"
						Case 2
							Program2$(1) = "No"
						Case 3
							Program3$(1) = "No"
					End Select
					DlgVisible "CtrlrTypeText",False
					DlgVisible "ControllerType",False
					DlgVisible "EnableEccGen",False
					DlgVisible "SBNText1",False
					DlgVisible "BlockNumber",False
					DlgVisible "EccOptions",False
					DlgVisible "M10T",False
					DlgVisible "M11T",False
					DlgVisible "M12T",False
					DlgVisible "M20T",False
					DlgVisible "M21T",False
					DlgVisible "M22T",False
					DlgVisible "M10",False
					DlgVisible "M11",False
					DlgVisible "M12",False
					DlgVisible "M20",False
					DlgVisible "M21",False
					DlgVisible "M22",False
				Else
					Select Case CurPgmNum
						Case 0
							Program0$(1) = "No"
						Case 1
							Program1$(1) = "No"
						Case 2
							Program2$(1) = "No"
						Case 3
							Program3$(1) = "No"
					End Select
					DlgVisible "CtrlrTypeText",True
					DlgVisible "ControllerType",True
					DlgVisible "EnableEccGen",True
					DlgVisible "SBNText1",True
					DlgVisible "BlockNumber",True
					DlgVisible "EccOptions",True
					DlgVisible "M10T",True
					DlgVisible "M11T",True
					DlgVisible "M12T",True
					DlgVisible "M20T",True
					DlgVisible "M21T",True
					DlgVisible "M22T",True
					DlgVisible "M10",True
					DlgVisible "M11",True
					DlgVisible "M12",True
					DlgVisible "M20",True
					DlgVisible "M21",True
					DlgVisible "M22",True
				End If
				DialogFuncSub = True ' Prevent button press from closing the dialog box
			Case "ControllerType"
				If SuppValue = 1 Then
					Select Case CurPgmNum
						Case 0
							Program0$(3) = "NF_MLC"
						Case 1
							Program1$(3) = "NF_MLC"
						Case 2
							Program2$(3) = "NF_MLC"
						Case 3
							Program3$(3) = "NF_MLC"
					End Select
					DlgVisible "EnableEccGen",False
					DlgVisible "EccOptions",False
					DlgVisible "M10T",False
					DlgVisible "M11T",False
					DlgVisible "M12T",False
					DlgVisible "M20T",False
					DlgVisible "M21T",False
					DlgVisible "M22T",False
					DlgVisible "M10",False
					DlgVisible "M11",False
					DlgVisible "M12",False
					DlgVisible "M20",False
					DlgVisible "M21",False
					DlgVisible "M22",False
				Else
					Select Case CurPgmNum
						Case 0
							Program0$(3) = "NF_SLC"
						Case 1
							Program1$(3) = "NF_SLC"
						Case 2
							Program2$(3) = "NF_SLC"
						Case 3
							Program3$(3) = "NF_SLC"
					End Select
					DlgVisible "EnableEccGen",True
					DlgVisible "EccOptions",True
					DlgVisible "M10T",True
					DlgVisible "M11T",True
					DlgVisible "M12T",True
					DlgVisible "M20T",True
					DlgVisible "M21T",True
					DlgVisible "M22T",True
					DlgVisible "M10",True
					DlgVisible "M11",True
					DlgVisible "M12",True
					DlgVisible "M20",True
					DlgVisible "M21",True
					DlgVisible "M22",True
				End If
				DialogFuncSub = True ' Prevent button press from closing the dialog box
			Case "EnableEccGen"
				If SuppValue = 1 Then
					EnableEcc = 1
					Select Case CurPgmNum
						Case 0
							Program0$(4) = "Yes"
						Case 1
							Program1$(4) = "Yes"
						Case 2
							Program2$(4) = "Yes"
						Case 3
							Program3$(4) = "Yes"
					End Select
				Else
					EnableEcc = 0
					Select Case CurPgmNum
						Case 0
							Program0$(4) = "No"
						Case 1
							Program1$(4) = "No"
						Case 2
							Program2$(4) = "No"
						Case 3
							Program3$(4) = "No"
					End Select

				End If
			Case "OK"
				DialogFuncSub = False ' close the dialog box
			End Select
	End Select
End Function

Function EraseNANDFlash
ReErase:
	Begin Dialog UserDialog 320,126,"NAND Flash Erase" ' %GRID:10,7,1,1
		Text 30,21,110,14,"Starting Block #",.Text1
		TextBox 160,14,90,21,.StartBlock
		Text 80,56,60,14,"Length",.Text2
		TextBox 160,49,90,21,.EndBlock
		CancelButton 30,91,90,21
		PushButton 200,91,90,21,"GO!",.EraseGo
	End Dialog
	Dim dlg As UserDialog
	ReturnValue = Dialog(dlg)
	If ReturnValue = 1 Then
		If Trim(dlg.StartBlock) > "" Or Trim(dlg.EndBlock) > "" Then
			'Erase the blocks
			Cfg_PromSelect "DEVICEPAGE"
			Cfg_PromOptions (WorkingPageSize)
			Cfg_PromSelect "DEVICESIZE"
			Cfg_PromOptions (WorkingDeviceSize)
			Cfg_PromSelect "LOCKFEATURE"
			Cfg_PromOptions (WorkingDeviceLock)
			Cfg_PromSelect "ERASE_BLOCKS"
			argument = (Trim(dlg.StartBlock) + ":" + Trim(dlg.EndBlock))
			Cfg_PromErase argument
		Else
			GoTo ReErase
		End If
	End If
End Function

Function RunProgram
'Copy each of the program arrays to the working array and pass the arguments to Seehau
'Once completed the do the actual programming
	Cfg_PromSelect "DEVICEPAGE"
	Cfg_PromOptions (WorkingPageSize)
	Cfg_PromSelect "DEVICESIZE"
	Cfg_PromOptions (WorkingDeviceSize)
	Cfg_PromSelect "LOCKFEATURE"
	Cfg_PromOptions (WorkingDeviceLock)

	For NumOfPrograms = 0 To 3 Step 1
		' check for Enabled or not to avoid not coping and processing an empty Image
		If NumOfPrograms = 0 And Program0$(11) = "" Then	' no bin file disable even if enabled
			Program0$(0) = "No"
		End If
		If NumOfPrograms = 1 And Program1$(11) = "" Then	' no bin file disable even if enabled
			Program1$(0) = "No"
		End If
		If NumOfPrograms = 0 And Program2$(11) = "" Then	' no bin file disable even if enabled
			Program2$(0) = "No"
		End If
		If NumOfPrograms = 0 And Program3$(11) = "" Then	' no bin file disable even if enabled
			Program3$(0) = "No"
		End If
		If NumOfPrograms = 0 And Program0$(0) = "No" Then
			Cfg_PromSelect "NFP0"
			Cfg_PromSelect "ENABLED"
			Cfg_PromOptions "No"
			GoTo skipit
		End If
		If NumOfPrograms = 1 And Program1$(0) = "No" Then
			Cfg_PromSelect "NFP1"
			Cfg_PromSelect "ENABLED"
			Cfg_PromOptions "No"
			GoTo skipit
		End If
		If NumOfPrograms = 2 And Program2$(0) = "No" Then
			Cfg_PromSelect "NFP2"
			Cfg_PromSelect "ENABLED"
			Cfg_PromOptions "No"
			GoTo skipit
		End If
		If NumOfPrograms = 3 And Program3$(0) = "No" Then
			Cfg_PromSelect "NFP3"
			Cfg_PromSelect "ENABLED"
			Cfg_PromOptions "No"
		GoTo skipit
		End If

		For index = 0 To 11 Step 1
			Select Case NumOfPrograms 
				Case 0
					WorkingProgram$(index) = Program0$(index)
				Case 1
					WorkingProgram$(index) =  Program1$(index)
				Case 2
					WorkingProgram$(index) =  Program2$(index)
				Case 3
					WorkingProgram$(index) =  Program3$(index)
			End Select
		Next index
		'Set Global Settings

		'Pass all arguments for each NFP#
		Select Case NumOfPrograms
			Case 0
				Cfg_PromSelect "NFP0"
			Case 1
				Cfg_PromSelect "NFP1"
			Case 2
				Cfg_PromSelect "NFP2"
			Case 3
				Cfg_PromSelect "NFP3"
		End Select
		If WorkingProgram$(0) = "Yes" Then
			Cfg_PromSelect "ENABLED"
			Cfg_PromOptions "Yes"
		Else
			Cfg_PromSelect "ENABLED"
			Cfg_PromOptions "No"
		End If
		Cfg_PromSelect "SECONDBOOT"
		If WorkingProgram$(1) = "" Then
			Cfg_PromOptions "No"
		Else
			Cfg_PromOptions (WorkingProgram$(1))
		End If
		Cfg_PromSelect "TARGETBLOCKPOS"
		If WorkingProgram$(1) = "Yes" Then
			Cfg_PromOptions "0"
			WorkingProgram$(3) = "NF_MLC"
		Else
			Cfg_PromOptions (WorkingProgram$(2))
		End If
		Cfg_PromSelect "CONTROLLER"
		Cfg_PromOptions (WorkingProgram$(3))
		If WorkingProgram$(1) = "No" Then	' if Not SecondaryBoot process Ecc options
			If WorkingProgram$(4) = "Yes" Then
				Cfg_PromSelect "ECCGENERATION"
				Cfg_PromOptions (WorkingProgram$(4))
				Cfg_PromSelect "ECCM1POS0"
				Cfg_PromOptions (WorkingProgram$(5))
				Cfg_PromSelect "ECCM1POS1"
				Cfg_PromOptions (WorkingProgram$(6))
				Cfg_PromSelect "ECCM1POS2"
				Cfg_PromOptions (WorkingProgram$(7))
				Cfg_PromSelect "ECCM2POS0"
				Cfg_PromOptions (WorkingProgram$(8))
				Cfg_PromSelect "ECCM2POS1"
				Cfg_PromOptions (WorkingProgram$(9))
				Cfg_PromSelect "ECCM2POS2"
				Cfg_PromOptions (WorkingProgram$(10))
			Else
				Cfg_PromSelect "ECCGENERATION"
				Cfg_PromOptions "No"
			End If
		End If
		Cfg_PromSelect "BINFILE"
		Cfg_PromOptions (WorkingProgram$(11))

skipit:
	Next NumOfPrograms
'Now let Seehau run programming process
	Cfg_PromSelect "PROGRAM_ALL"
	Cfg_PromErase "Yes"
End Function

Function Check_EccLocations(M10%,M11%,M12%,M20%,M21%,M22%)
	If M10 = M11 Or M10 = M12 Or M10 = M20 Or M10 = M21 Or M10 = M22 Then
		Err.Raise 1
	End If

	If M11 = M12 Or M11 = M20 Or M11 = M21 Or M11 = M22 Then
		Err.Raise 1
	End If

	If M12 = M20 Or M12 = M21 Or M12 = M22 Then
		Err.Raise 1
	End If

	If M20 = M21 Or M20 = M21 Or M20 = M22 Then
		Err.Raise 1
	End If

	If WorkingPageSize = "NF_LARGE" Then
		If M10 = 0 Or M11 = 0 Or M12 = 0 Or M20 = 0 Or M21 = 0 Or M22 = 0 Then
			Err.Raise 1
		End If
	End If

	If WorkingPageSize = "NF_SMALL" Then
		If M10 = 5 Or M11 = 5 Or M12 = 5 Or M20 = 5 Or M21 = 5 Or M22 = 5 Then
			Err.Raise 1
		End If
	End If

End Function

Function Initialize_Arrays
'Pre-fill arrays
	PageSizeArray$(0) = "SMALL"
	PageSizeArray$(1) = "LARGE"

	DeviceSizeArray$(0) = "8MB x 8 (64 Mbits)"
	DeviceSizeArray$(1) = "16MB x 8 (128 Mbits)"
	DeviceSizeArray$(2) = "32MB x 8 (256 Mbits)"
	DeviceSizeArray$(3) = "64MB x 8 (512 Mbits)"
	DeviceSizeArray$(4) = "128MB x 8 (1 Gbits)"
	DeviceSizeArray$(5) = "256MB x 8 (2 Gbits)"
	DeviceSizeArray$(6) = "512MB x 8 (4 Gbits)"
	DeviceSizeArray$(7) = "1GB x 8 (8 Gbits)"
	DeviceSizeArray$(8) = "2GB x 8 (16 Gbits)"
	Pgm_SizeArray$(0) = "NF_8MBYTES_X_8_64MBITS"
	Pgm_SizeArray$(1) = "NF_16MBYTES_X_8_128MBITS"
	Pgm_SizeArray$(2) = "NF_32MBYTES_X_8_256MBITS"
	Pgm_SizeArray$(3) = "NF_64MBYTES_X_8_512MBITS"
	Pgm_SizeArray$(4) = "NF_128MBYTES_X_8_1GBITS"
	Pgm_SizeArray$(5) = "NF_256MBYTES_X_8_2GBITS"
	Pgm_SizeArray$(6) = "NF_512MBYTES_X_8_4GMBITS"
	Pgm_SizeArray$(7) = "NF_1GBYTES_X_8_8GBITS"
	Pgm_SizeArray$(8) = "NF_2GBYTES_X_8_16GBITS"

	ControllerArray$(0) = "SLC"
	ControllerArray$(1) = "MLC"

	For i = 0 To 15 Step 1
		EccLocArray$(i) = Str(i)
	Next i

	NoPreviousCfg = 1

End Function

Function GetSavedSettings

'Globals
	Cfg_PromSelect "DEVICEPAGE"
	WorkingPageSize = Cfg_PromOptions ("")
	Cfg_PromSelect "DEVICESIZE"
	WorkingDeviceSize = Cfg_PromOptions ("")
	Cfg_PromSelect "LOCKFEATURE"
	WorkingDeviceLock = Cfg_PromOptions ("")

'Program Specific Settings


	For i = 0 To 3 Step 1
		Select Case i
			Case 0
				Cfg_PromSelect "NFP0"
			Case 1
				Cfg_PromSelect "NFP1"
			Case 2
				Cfg_PromSelect "NFP2"
			Case 3
				Cfg_PromSelect "NFP3"
		End Select
		Cfg_PromSelect "ENABLED"
		WorkingProgram$(0) = Cfg_PromOptions ("")
		Cfg_PromSelect "SECONDBOOT"
		WorkingProgram$(1) = Cfg_PromOptions ("")
		Cfg_PromSelect "TARGETBLOCKPOS"
		WorkingProgram$(2) = Cfg_PromOptions ("")
		Cfg_PromSelect "CONTROLLER"
		WorkingProgram$(3) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCGENERATION"
		WorkingProgram$(4) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCM1POS0"
		WorkingProgram$(5) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCM1POS1"
		WorkingProgram$(6) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCM1POS2"
		WorkingProgram$(7) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCM2POS0"
		WorkingProgram$(8) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCM2POS1"
		WorkingProgram$(9) = Cfg_PromOptions ("")
		Cfg_PromSelect "ECCM2POS2"
		WorkingProgram$(10) = Cfg_PromOptions ("")
		Cfg_PromSelect "BINFILE"
		WorkingProgram$(11) = Cfg_PromOptions ("")
		For index = 0 To 11 Step 1
			Select Case i
				Case 0
					Program0$(index) = WorkingProgram$(index)
				Case 1
					Program1$(index) = WorkingProgram$(index)
				Case 2
					Program2$(index) = WorkingProgram$(index)
				Case 3
					Program3$(index) = WorkingProgram$(index)
			End Select
		Next index
	Next i
End Function
