

Function Rom_Selector()
    
    SetClearColor( 0, 0, 150 )
    ClearScreen() 
    
    //default_folder$ = GetFolder() 
    Dim rom_names[100] as string
    keysDown=0
    count = 0
    selection_counter=0
    SetFolder( "/media/roms" ) 
    rom_names[ count ]=GetFirstFile()
    
    for t=0 to 100 
          //Print(Spaces(2)+fn$)
          Inc count
          rom_names[ count ] = GetNextFile()
          if len(rom_names[ count ])<1 then exit
    next t
    SetFolder( "" )    
    selection$=""
    
    while selection$=""
	    //print("Chip8 emulator/interpeter coded in App game kit.")
	    print("You have " + str(count) + " rom files in folder."+chr(10)+"left/right to scroll   Down to select")
	    for t=selection_counter to count-1
	        if t=selection_counter
	           print("-> "+str(t)+" "+rom_names[ t ]+" <-")
	        else
			   print("   "+str(t)+" "+rom_names[ t ])
			endif
	    next t
	    	    
	    if keysDown =0 and GetRawKeyState( 37 )=1 
		   inc selection_counter
	       inc keysDown
	    endif
	    if keysDown =0 and GetRawKeyState( 39 )=1 
		   dec selection_counter
	       inc keysDown
	    endif
	    if selection_counter<0 then selection_counter=0
	    if selection_counter>count-1 then selection_counter=count-1
	    if GetRawKeyState( 37 ) + GetRawKeyState( 39 ) = 0 then keysDown=0
	    if keysDown =0 and GetRawKeyState( 40 )=1 
	       selection$="/media/roms/"+rom_names[ selection_counter ]
	       exit
	    endif
	    sync()
	endwhile
    SetClearColor( 0, 0, 0 )
    ClearScreen()
    sync()
endfunction selection$



function process_Rom_File(fname$)
    
    // open file
    fid = OpenToRead(fname$)
 
    // make sure it opened
    if fid <= 0
    // something that indicates a problem
    // exit function indicating error
       exitfunction 0
    endif
 
    dim tempRom[10000]
    t=0
   
    while FileEOF(fid) = 0
          tempRom[t]=  ReadByte(fid)                                     // && val("0x0FFF",Read_as_Hex) // val("0xFF",Read_as_Hex)
          inc t
    endwhile
    // close the file
    CloseFile(fid)
   
    for ro=0 to t-1
        memory[512+ro] = tempRom[ro]                                     // && val("0xff",16)
    next ro
    undim tempRom[]
   
endfunction 1
