' Gambas module file

Public PART_NUMBER As String
Public MEMORY_TYPE As String
Public SPD_REVISION As String
Public MODULE_TYPE As String
Public MANUFACTURER As String
Public DRAM_MANUFACTURER As String
Public FUNDAMENTAL_MEMORY_TYPE As String
Public Assembly_Serial_Number As String
Public Manufacturing_Date As String
Public SDRAM_DEVICE_WIDTH As String
Public $volts As String = "1.5V"
Public Sub List_EEPROM()
 
 Try Finfosys.ComboBox10.List = Dir("/sys/bus/i2c/drivers/eeprom/", "*-****")
 Try Finfosys.ComboBox10.Index = 0

 If Finfosys.ComboBox10.Count = 0 Then
  SETNULL()
  FINALIZE()
 Endif
End

Public Sub _inits()

  Dim hFile As File
  Dim sLine As String
  Dim i As Integer
  Dim spd_data As New String[]
  Dim spd_data_asc As New String[]
  Dim spd_data_str As New String[]
  Dim spd_data_bin As New Variant[]
  Dim module_types_ddr2 As String[] = ["Reserved", "Undefined", "RDIMM", "UDIMM", "SO-DIMM", "Micro-DIMM", "Mini-RDIMM", "Mini-UDIMM"]
  Dim module_types_ddr3 As String[] = ["Undefined", "RDIMM", "UDIMM", "SO-DIMM", "Micro-DIMM", "Mini-RDIMM", "Mini-UDIMM", "Mini-CDIMM", "72b-SO-UDIMM", "72b-SO-RDIMM", "72b-SO-CDIMM", "LRDIMM", "16b-SO-DIMM", "32b-SO-DIMM"]
  Dim module_types_ddr4 As String[] = ["Undefined", "RDIMM", "UDIMM", "SO-DIMM", "LRDIMM"]
  Dim dram_device_type As String[] = ["Reserved", "FPM DRAM", "EDO", "Pipelined Nibble", "SDR SDRAM", "Multiplexed ROM", "DDR SGRAM", "DDR SDRAM", "DDR2 SDRAM", "FB-DIMM", "FB-DIMM Probe", "DDR3 SDRAM"]
  Dim SIZERAM As Integer
  Dim BYTES As Long = 0
  'Clear data before get new
  SETNULL()
  spd_data.Clear
  spd_data_asc.Clear
  spd_data_str.Clear
  'End of clear
  hFile = Open "/sys/bus/i2c/drivers/eeprom/" & Finfosys.ComboBox10.Text & "/eeprom" For Input
  'hFile = Open "/home/michal/Desktop/Hynix_eeprom" For Input
  'hFile = Open "/home/michal/Desktop/Kingston_eeprom" For Input
  'hFile = Open "/home/michal/Pobrane/pavbaranov_eeprom" For Input
  'hFile = Open "/home/michal/Pobrane/n150_eeprom" For Input
  BYTES = 0
  While Not Eof(hFile)
   Line Input #hFile, sLine
    BYTES += Len(sLine)
    'If BYTES > 254 And BYTES < 270 Then
     For i = 0 To Len(sLine)
      spd_data.Add(Hex(Asc(Mid$(sLine, i + 1, 1))))
      spd_data_asc.Add(Asc(Mid$(sLine, i + 1, 1)))
      spd_data_str.Add(Mid$(sLine, i + 1, 1))
      spd_data_bin.Add(Bin(Asc(Mid$(sLine, i + 1, 1))))
     Next
    'Endif
  Wend

   Select Case spd_data[0] 'Try to tetect ram type.
    Case "80"                'byte[0] is 08 or 8 = DDR2
     MEMORY_TYPE = "DDR2"  '
    Case "92"                'byte[0] is 92 = DDR3
     MEMORY_TYPE = "DDR3"  '
    Case "0C"              'byte[0] is 0C FIXME: lower case or upper case
     MEMORY_TYPE = "DDR4"  'is ddr4
    Case Else
     MEMORY_TYPE = "Unknown"  
   End Select
   
   Select Case MEMORY_TYPE
    Case "DDR4"
     SPD_REVISION = Mid(spd_data[1], 1, 1) & "." & Mid(spd_data[1], 2, 1)
     
     For i = 329 To 348
      PART_NUMBER &= spd_data_str[i]
     Next
     
   Select Case (CLong(spd_data_asc[350]) And &h7F&)
    Case 0
     JEDEC.init_jedec_id0_vals()
     Try MANUFACTURER = JEDEC.$jedec_id0_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case 1
     JEDEC.init_jedec_id1_vals()
     Try MANUFACTURER = JEDEC.$jedec_id1_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case 2
     JEDEC.init_jedec_id2_vals()
     Try MANUFACTURER = JEDEC.$jedec_id2_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case 3
     JEDEC.init_jedec_id3_vals()
     Try MANUFACTURER = JEDEC.$jedec_id3_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case 4
     JEDEC.init_jedec_id4_vals()
     Try MANUFACTURER = JEDEC.$jedec_id4_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case 5
     JEDEC.init_jedec_id5_vals()
     Try MANUFACTURER = JEDEC.$jedec_id5_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case 6
     JEDEC.init_jedec_id6_vals()
     Try MANUFACTURER = JEDEC.$jedec_id6_vals[(CLong(spd_data_asc[351]) And &h7F&) - 1]
    Case Else
     MANUFACTURER = "Unknown"
   End Select
     
    Case "DDR3"
   SPD_REVISION = Mid(spd_data[1], 1, 1) & "." & Mid(spd_data[1], 2, 1)

   For i = 128 To 145
    PART_NUMBER &= spd_data_str[i]
   Next
   If spd_data[6] = 1 Then
    $volts &= " tolerant"
   Endif
   If spd_data[6] = 1 Then
    $volts &= ", 1.35V "
   Endif
   If spd_data[6] = 1 Then
    $volts &= ", 1.2X V"
   Endif

   Try FUNDAMENTAL_MEMORY_TYPE = dram_device_type[spd_data_asc[2]]
   Try MODULE_TYPE = module_types_ddr3[spd_data[3]]

   Try Assembly_Serial_Number = Subst("0x&1&2&3&4",
                         spd_data[122],
                         spd_data[123],
                         spd_data[124],
                         spd_data[125])
   Try Manufacturing_Date = Subst("&1-W&2",
                         CInt(2000 + spd_data[120]),
                         spd_data[121])

   Select Case Right(spd_data_bin[7], 3)
    Case "000"
     SDRAM_DEVICE_WIDTH = "4 Bits"
    Case "001"
     SDRAM_DEVICE_WIDTH = "8 Bits"
    Case "1"
     SDRAM_DEVICE_WIDTH = "8 Bits"
    Case "010"
     SDRAM_DEVICE_WIDTH = "16 Bits"
    Case "011"
     SDRAM_DEVICE_WIDTH = "32 Bits"
    Case Else
     SDRAM_DEVICE_WIDTH = "Unknown"
   End Select
   
   Select Case Hex$(CLong(spd_data_asc[117]) And &h7F&)
    Case 0
     JEDEC.init_jedec_id0_vals()
     Try MANUFACTURER = JEDEC.$jedec_id0_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case 1
     JEDEC.init_jedec_id1_vals()
     Try MANUFACTURER = JEDEC.$jedec_id1_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case 2
     JEDEC.init_jedec_id2_vals()
     Try MANUFACTURER = JEDEC.$jedec_id2_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case 3
     JEDEC.init_jedec_id3_vals()
     Try MANUFACTURER = JEDEC.$jedec_id3_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case 4
     JEDEC.init_jedec_id4_vals()
     Try MANUFACTURER = JEDEC.$jedec_id4_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case 5
     JEDEC.init_jedec_id5_vals()
     Try MANUFACTURER = JEDEC.$jedec_id5_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case 6
     JEDEC.init_jedec_id6_vals()
     Try MANUFACTURER = JEDEC.$jedec_id6_vals[(CLong(spd_data_asc[118]) And &h7F&) - 1]
    Case Else
     MANUFACTURER = "Unknown"
   End Select
   
   Select Case Hex$(CLong(spd_data_asc[148]) And &h7F&)
    Case 0
     JEDEC.init_jedec_id0_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id0_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case 1
     JEDEC.init_jedec_id1_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id1_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case 2
     JEDEC.init_jedec_id2_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id2_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case 3
     JEDEC.init_jedec_id3_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id3_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case 4
     JEDEC.init_jedec_id4_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id4_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case 5
     JEDEC.init_jedec_id5_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id5_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case 6
     JEDEC.init_jedec_id6_vals()
     Try DRAM_MANUFACTURER = JEDEC.$jedec_id6_vals[(CLong(spd_data_asc[149]) And &h7F&) - 1]
    Case Else
     DRAM_MANUFACTURER = "Unknown"
   End Select
  
  Case "DDR2"
   
   SPD_REVISION = Mid(spd_data[62], 1, 1) & "." & Mid(spd_data[62], 2, 1)
   Try FUNDAMENTAL_MEMORY_TYPE = dram_device_type[spd_data_asc[2]]
   Try MODULE_TYPE = module_types_ddr2[spd_data[20]]

   For i = 73 To 90
    PART_NUMBER &= spd_data_str[i]
   Next
   Select Case (CLong(spd_data_asc[71]) And &h7F&)
    Case 0
     JEDEC.init_jedec_id0_vals()
     Try MANUFACTURER = JEDEC.$jedec_id0_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case 1
     JEDEC.init_jedec_id1_vals()
     Try MANUFACTURER = JEDEC.$jedec_id1_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case 2
     JEDEC.init_jedec_id2_vals()
     Try MANUFACTURER = JEDEC.$jedec_id2_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case 3
     JEDEC.init_jedec_id3_vals()
     Try MANUFACTURER = JEDEC.$jedec_id3_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case 4
     JEDEC.init_jedec_id4_vals()
     Try MANUFACTURER = JEDEC.$jedec_id4_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case 5
     JEDEC.init_jedec_id5_vals()
     Try MANUFACTURER = JEDEC.$jedec_id5_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case 6
     JEDEC.init_jedec_id6_vals()
     Try MANUFACTURER = JEDEC.$jedec_id6_vals[(CLong(spd_data_asc[64]) And &h7F&) - 1]
    Case Else
     MANUFACTURER = "Unknown"
   End Select
   Try Manufacturing_Date = Subst("&1-W&2",
                         CInt(2000 + spd_data[93]),
                         spd_data[94])
   Try Assembly_Serial_Number = Subst("0x&1&2&3&4",
                         spd_data[95],
                         spd_data[96],
                         spd_data[97],
                         spd_data[98])

  Case Else
   MANUFACTURER = "Unknown"
   DRAM_MANUFACTURER = "Unknown"
   PART_NUMBER = "Unknown"
   MEMORY_TYPE = "Unknown"
   FUNDAMENTAL_MEMORY_TYPE = "Unknown"
   MODULE_TYPE = "Unknown"
   SPD_REVISION = "Unknown"
   Assembly_Serial_Number = "Unknown"
   Manufacturing_Date = "Unknown"
   SDRAM_DEVICE_WIDTH = "Unknown"
  End Select
  FINALIZE()
  Close #hFile
End

Public Sub FINALIZE()
 Finfosys.Label259.Text = "Manufacturer: " & MANUFACTURER
 Finfosys.Label260.Text = "DRAM Manufacturer: " & DRAM_MANUFACTURER
 Finfosys.Label227.Text = "Part number: " & PART_NUMBER
 Finfosys.Label256.Text = "Memory type: " & MEMORY_TYPE
 Finfosys.Label261.Text = "Fundamental Memory type: " & FUNDAMENTAL_MEMORY_TYPE
 Finfosys.Label258.Text = "Module Type: " & MODULE_TYPE
 Finfosys.Label257.Text = "SPD RV: " & SPD_REVISION
 Finfosys.Label262.Text = "Assembly Serial Number: " & Assembly_Serial_Number
 Finfosys.Label263.Text = "Manufacturing Date: " & Manufacturing_Date
 Finfosys.Label264.Text = "SDRAM Device Width: " & SDRAM_DEVICE_WIDTH
End

Public Sub SETNULL()
 SDRAM_DEVICE_WIDTH = Null
 Manufacturing_Date = Null
 Assembly_Serial_Number = Null
 DRAM_MANUFACTURER = Null
 MANUFACTURER = Null
 MODULE_TYPE = Null
 SPD_REVISION = Null
 PART_NUMBER = Null
 MEMORY_TYPE = Null
 FUNDAMENTAL_MEMORY_TYPE = Null
End
