' Gambas module file

' Copyright(C) 2011-2014, Michał Głowienka aka eloaders <eloaders@linux.pl>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.

Public Logs As New Logger
Public HTT As String
Public X86_X64 As String
Public VIRT As String
Public instructions As String
Public dec_instructions As String
Public Sub _init()
 Shell "grep 'flags' /proc/cpuinfo | sed -n '1p' | cut -d ':' -f 2" Wait To dec_instructions
 If dec_instructions Like "* mmxext *" Then
  instructions = instructions & "MMX(+), "
 Endif
 If dec_instructions Like "* mmx *" Then
  instructions = instructions & "MMX, " 
 Endif
 If dec_instructions Like "* 3dnowext *" Then
  instructions = instructions & "3DNow!(+), "
 Endif
 If dec_instructions Like "* sse *" Then
  instructions = instructions & "SSE, " 
 Endif
 If dec_instructions Like "* 3dnow *" Then
  instructions = instructions & "3DNow!, "
 Endif
 If dec_instructions Like "* sse2 *" Then
  instructions = instructions & "SSE2, "
 Endif
 If dec_instructions Like "* pni *" Then
  instructions = instructions & "SSE3, "
 Endif
 If dec_instructions Like "* ssse3 *" Then
  instructions = instructions & "SSSE3, "
 Endif
 If dec_instructions Like "* sse4_2 *" Then
  instructions = instructions & "SSE4.2, "
 Endif
 If dec_instructions Like "* sse4_1 *" Then
  instructions = instructions & "SSE4.1, "
 Endif
 If dec_instructions Like "* sse4a *" Then
  instructions = instructions & "SSE4a, "
 Endif
 If dec_instructions Like "* nx *" And Finfosys.vendor_id Like "*Intel*" Then
  instructions = instructions & "XD-Bit, "
 Else
  instructions = instructions & "NX-Bit, "
 Endif
 
 'Instructions for detect AMD64,vmx, ht
 If dec_instructions Like "* lm *" Then
  Finfosys.ToolButton1.Picture = Picture["Data/check_true.png"]
  X86_X64 = "True"
 Else
  Finfosys.ToolButton1.Picture = Picture["Data/check_false.png"]
  X86_X64 = "False"
 Endif

 If dec_instructions Like "* vmx *" Then
  Finfosys.ToolButton2.Picture = Picture["Data/check_true.png"]
  VIRT = "True"
 Else If dec_instructions Like "* svm *" Then
  Finfosys.ToolButton2.Picture = Picture["Data/check_true.png"]
  HTT = "True"
 Else
  Finfosys.ToolButton2.Picture = Picture["Data/check_false.png"]
  HTT = "False"
  VIRT = "False"
 Endif
 If Exist("/dev/kvm") = True Then
  Finfosys.ToolButton4.Picture = Picture["Data/check_true.png"]
 Else
  Finfosys.ToolButton4.Picture = Picture["Data/check_false.png"]
 Endif
 ht()
 HyperTransport()
End

Public Sub ht()
  Dim NUMLOG, NUMPHY As String
  Shell "grep 'processor' /proc/cpuinfo | wc -l" Wait To NUMPHY
  Shell "grep 'cpu cores' /proc/cpuinfo | sed -n '1p' | cut -d ':' -f 2" Wait To NUMLOG
  
  If NUMLOG = Null Then
   Logs("CPU Cores is Null", Logger.Info)
  Else
   NUMLOG = CFloat(Val(Replace(Replace(NUMLOG, " ", ""), "\n", "")))
   NUMPHY = CFloat(Val(Replace(Replace(NUMPHY, " ", ""), "\n", "")))
     If NUMPHY = NUMLOG Then
        Finfosys.ToolButton3.Picture = Picture["Data/check_false.png"]
        Logs("Not detect HT", Logger.Info)
        HTT = "False"
        Else
        Finfosys.ToolButton3.Picture = Picture["Data/check_true.png"]
        Logs("Detected HT", Logger.Info)
        HTT = "True"
     Endif
  Endif
End

Public Sub HyperTransport()
 
 If Finfosys.vendor_id Like "*AuthenticAMD*" And dec_instructions Like "*ht*" Then
  Finfosys.ToolButton3.Picture = Picture["Data/check_true.png"]
  Finfosys.Label99.Text = "Hyper-Transport:"
 Endif
 
End
