Skip to content

Instantly share code, notes, and snippets.

@writzx
Last active July 19, 2023 06:53
Show Gist options
  • Save writzx/d6c3427867eb9ea45b52233e9a9a2118 to your computer and use it in GitHub Desktop.
Save writzx/d6c3427867eb9ea45b52233e9a9a2118 to your computer and use it in GitHub Desktop.
Modified ryzenAdj PS Script for applying config periodically without monitoring. No dependency on inpoutx64.dll (no problem with anticheats).
Param([Parameter(Mandatory=$false)][switch]$noGUI)
$Error.Clear()
################################################################################
#### Configuration Start
################################################################################
# WARNING: Use at your own risk!
$pathToRyzenAdjDlls = Split-Path -Parent $PSCommandPath
$monitorPowerSlider = $true
# Rename inpoutx64.dll to inpoutx64.dll.bak to disable loading
# To avoid problems with anticheat
$renameInpoutx64Dll = $true
function doAdjust_ACmode {
$Script:repeatWaitTimeSeconds = 10
adjust "fast_limit" 35000
adjust "stapm_limit" 35000
adjust "slow_limit" 25000
#adjust "slow_time" 30
adjust "tctl_temp" 85
# adjust "max_socclk_freq" 2700
adjust "dgpu_skin_temp_limit" 87
#adjust "apu_skin_temp_limit" 50
#adjust "vrmmax_current" 100000
#replace any_other_field with additional adjustments. Name is equal to RyzenAdj options but it uses _ instead of -
#adjust "any_other_field" 1234
#custom code, for example set fan controll back to auto
#values (WriteRegister: 47, FanSpeedResetValue:128) extracted from similar devices at https://github.com/hirschmann/nbfc/blob/master/Configs/
#Start-Process -NoNewWindow -Wait -filePath "C:\Program Files (x86)\NoteBook FanControl\ec-probe.exe" -ArgumentList("write", "47", "128")
if($Script:acSlider -eq $Script:betterBattery){
#put adjustments for energie saving slider position here:
enable "power_saving" #add 10s boost delay for usage on cable to reduce idle power consumtion
}
}
function doAdjust_BatteryMode {
$Script:repeatWaitTimeSeconds = 60
adjust "stapm_limit" 35000
adjust "fast_limit" 26000
adjust "slow_limit" 10000
# adjust "max_socclk_freq" 2700
adjust "tctl_temp" 72
adjust "dgpu_skin_temp_limit" 82
if($Script:dcSlider -eq $Script:betterBattery){
#put adjustments for energie saving slider position here: for example disable fan to save power
#Start-Process -NoNewWindow -Wait -filePath "C:\Program Files (x86)\NoteBook FanControl\ec-probe.exe" -ArgumentList("write", "47", "0")
}
if($Script:dcSlider -eq $Script:bestPerformance){
#put adjustments for highest performance slider position here:
enable "max_performance" #removes 10s boost delay on battery
doAdjust_ACmode #set limits from cable mode on battery
}
}
################################################################################
#### Configuration End
################################################################################
$env:PATH += ";$pathToRyzenAdjDlls"
$NL = $([System.Environment]::NewLine);
if($noGUI){ $showErrorPopupsDuringInit = $false }
$apiHeader = @'
[DllImport("libryzenadj.dll")] public static extern IntPtr init_ryzenadj();
[DllImport("libryzenadj.dll")] public static extern int set_stapm_limit(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_fast_limit(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_slow_limit(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_slow_time(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_stapm_time(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_tctl_temp(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_vrm_current(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_vrmsoc_current(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_vrmmax_current(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_vrmsocmax_current(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_psi0_current(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_psi0soc_current(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_max_gfxclk_freq(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_min_gfxclk_freq(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_max_socclk_freq(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_min_socclk_freq(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_max_fclk_freq(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_min_fclk_freq(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_max_vcn(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_min_vcn(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_max_lclk(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_min_lclk(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_prochot_deassertion_ramp(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_apu_skin_temp_limit(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_dgpu_skin_temp_limit(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_apu_slow_limit(IntPtr ry, [In]uint value);
[DllImport("libryzenadj.dll")] public static extern int set_power_saving(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern int set_max_performance(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern int refresh_table(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern IntPtr get_table_values(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_stapm_limit(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_stapm_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_stapm_time(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_fast_limit(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_fast_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_slow_limit(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_slow_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_apu_slow_limit(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_apu_slow_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrm_current(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrm_current_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrmsoc_current(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrmsoc_current_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrmmax_current(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrmmax_current_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrmsocmax_current(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_vrmsocmax_current_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_tctl_temp(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_tctl_temp_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_apu_skin_temp_limit(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_apu_skin_temp_value(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_dgpu_skin_temp_limit(IntPtr ry);
[DllImport("libryzenadj.dll")] public static extern float get_dgpu_skin_temp_value(IntPtr ry);
[DllImport("kernel32.dll")] public static extern uint GetModuleFileName(IntPtr hModule, [Out]StringBuilder lpFilename, [In]int nSize);
[DllImport("kernel32.dll")] public static extern Boolean GetSystemPowerStatus(out SystemPowerStatus sps);
public struct SystemPowerStatus {
public Byte ACLineStatus;
public Byte BatteryFlag;
public Byte BatteryLifePercent;
public Byte Reserved1;
public Int32 BatteryLifeTime;
public Int32 BatteryFullLifeTime;
}
public static String getExpectedWinRing0DriverFilepath(){
StringBuilder fileName = new StringBuilder(255);
GetModuleFileName(IntPtr.Zero, fileName, fileName.Capacity);
return Path.GetDirectoryName(fileName.ToString()) + "\\WinRing0x64.sys";
}
public static String getDllImportErrors(){
try {
Marshal.PrelinkAll(typeof(adj));
} catch (Exception e) {
return e.Message;
}
return "";
}
'@
if(-not ([System.Management.Automation.PSTypeName]'ryzen.adj').Type){
Add-Type -MemberDefinition $apiHeader -Namespace 'ryzen' -Name 'adj' -UsingNamespace ('System.Text', 'System.IO')
}
Add-Type -AssemblyName System.Windows.Forms
function showErrorMsg ([String] $msg){
if($showErrorPopupsDuringInit){
[void][System.Windows.Forms.MessageBox]::Show($msg, $PSCommandPath,
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error)
}
}
$dllImportErrors = [ryzen.adj]::getDllImportErrors();
if($dllImportErrors -or $Error){
Write-Error $dllImportErrors
showErrorMsg "Problem with using libryzenadj.dll$NL$NL$($Error -join $NL)"
exit 1
}
$winring0DriverFilepath = [ryzen.adj]::getExpectedWinRing0DriverFilepath()
if(!(Test-Path $winring0DriverFilepath)) { Copy-Item -Path $pathToRyzenAdjDlls\WinRing0x64.sys -Destination $winring0DriverFilepath }
$ry = [ryzen.adj]::init_ryzenadj()
if($ry -eq 0){
$msg = "RyzenAdj could not get initialized.$($NL)Reason can be found inside Powershell$($NL)"
if($psISE) { $msg += "It is not possible to see the error reason inside ISE, you need to test it in PowerShell Console" }
showErrorMsg "$msg$NL$NL$($Error -join $NL)"
exit 1
}
function adjust ([String] $fieldName, [uInt32] $value) {
$res = Invoke-Expression "[ryzen.adj]::set_$fieldName($ry, $value)"
switch ($res) {
0 {
if($debugMode) { Write-Host "set $fieldName to $value" }
return
}
-1 { Write-Error "set_$fieldName is not supported on this family"}
-3 { Write-Error "set_$fieldName is not supported on this SMU"}
-4 { Write-Error "set_$fieldName is rejected by SMU"}
default { Write-Error "set_$fieldName did fail with $res"}
}
}
function enable ([String] $fieldName) {
$res = Invoke-Expression "[ryzen.adj]::set_$fieldName($ry)"
switch ($res) {
0 {
if($debugMode) { Write-Host "enable $fieldName"}
return
}
-1 { Write-Error "set_$fieldName is not supported on this family"}
-3 { Write-Error "set_$fieldName is not supported on this SMU"}
-4 { Write-Error "set_$fieldName is rejected by SMU"}
default { Write-Error "set_$fieldName did fail with $res"}
}
}
function testConfiguration {
Write-Host -NoNewline "Testing config..."
if($Script:systemPowerStatus.ACLineStatus){
doAdjust_BatteryMode
doAdjust_ACmode
} else {
doAdjust_ACmode
doAdjust_BatteryMode
}
if ($Error) {
Write-Host "error!"
} else {
Write-Host "success!"
}
if($Error -and $showErrorPopupsDuringInit){
$answer = [System.Windows.Forms.MessageBox]::Show("Your Adjustment configuration did not work.$NL$NL$($Error -join $NL)", $PSCommandPath,
[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore,
[System.Windows.Forms.MessageBoxIcon]::Warning)
$Error.Clear()
if($answer -eq "Abort"){ exit 1 }
if($answer -eq "Retry"){ testConfiguration }
}
}
if(-not $Script:repeatWaitTimeSeconds) { $Script:repeatWaitTimeSeconds = 10 }
$powerkey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\")
$Script:betterBattery = "961cc777-2547-4f9d-8174-7d86181b8a7a"
$Script:betterPerformance = "00000000-0000-0000-0000-000000000000"
$Script:bestPerformance = "ded574b5-45a0-4f42-8737-46345c09c238"
$Script:acSlider = $powerkey.GetValue("ActiveOverlayACPowerScheme")
$Script:dcSlider = $powerkey.GetValue("ActiveOverlayDCPowerScheme")
$systemPowerStatus = New-Object ryzen.adj+SystemPowerStatus
[void][ryzen.adj]::GetSystemPowerStatus([ref]$systemPowerStatus)
$pathToInpoutx64 = "$pathToRyzenAdjDlls\inpoutx64.dll"
if($renameInpoutx64Dll -and (Test-Path -Path $pathToInpoutx64 -PathType Leaf)) {
Write-Host "Renaming inpoutx64.dll to inpoutx64.dll.bak..."
Move-Item -Path $pathToInpoutx64 -Destination "$pathToInpoutx64.bak" -Force
}
testConfiguration
if($monitorPowerSlider){
$processType = "Monitor PowerSlider changes and apply settings"
} else {
$processType = "Apply settings"
}
Write-Host "$processType every $Script:repeatWaitTimeSeconds seconds..."
$index = 0
function loop {
$Script:index = $Script:index + 1
if($monitorPowerSlider -and ($Script:acSlider -ne $powerkey.GetValue("ActiveOverlayACPowerScheme") -or $Script:dcSlider -ne $powerkey.GetValue("ActiveOverlayDCPowerScheme"))){
Write-Host "Power Slider changed"
$Script:acSlider = $powerkey.GetValue("ActiveOverlayACPowerScheme")
$Script:dcSlider = $powerkey.GetValue("ActiveOverlayDCPowerScheme")
}
[void][ryzen.adj]::GetSystemPowerStatus([ref]$systemPowerStatus)
$oldWait = $Script:repeatWaitTimeSeconds
Write-Host -NoNewline "$index`: "
if($systemPowerStatus.ACLineStatus){
Write-Host "Applying AC Mode settings..."
doAdjust_ACmode
} else {
Write-Host "Applying Battery Mode settings..."
doAdjust_BatteryMode
}
if($oldWait -ne $Script:repeatWaitTimeSeconds ) { Write-Host "$processType every $Script:repeatWaitTimeSeconds seconds..." }
Start-Sleep $Script:repeatWaitTimeSeconds
}
while ($true) { loop }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment