Better timing scaling (breaks erista, will fix later)

This commit is contained in:
Lightos1
2025-11-30 21:42:48 +01:00
parent 8b25efe4e5
commit a87f1fbee0
9 changed files with 1468 additions and 710 deletions

View File

@@ -0,0 +1,185 @@
/*
* Copyright (c) Lightos_
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
#include "../oc_common.hpp"
#include "../mtc_timing_value.hpp"
#include "timing_tables.hpp"
namespace ams::ldr::oc::pcv::mariko {
s32 FixEinput(s32 val) {
if (auto patch = FindEinput()) {
return patch->correct;
}
return val;
}
u32 GetRext() {
if (auto r = FindRext()) {
return r->correct;
}
return 0x1A;
}
/* TODO: This function is quite uggly, refactor! */
void CalculateMiscTimings() {
tW2P = 0x2d;
rdv = 0x39 + C.mem_burst_read_latency;
obdly = 0x10000002 + C.mem_burst_write_latency;
einput_duration = 0x1C;
quse_width = 0x8;
for (u32 i = 0; i < g_misc_table_size; i++) {
const auto& e = g_misc_table[i];
if (C.marikoEmcMaxClock >= e.min_freq) {
rdv += e.rdv_inc;
if (e.einput) einput_duration = e.einput;
if (e.quse_width) quse_width = e.quse_width;
obdly += e.obdly_delta;
}
}
if (WL >= 16) tW2P += 6;
if (WL >= 18) tW2P += 8;
s32 einput_calc = quse_width - (0.010182 * (C.marikoEmcMaxClock / 1000.0) - 0.0879) + 0.5;
einput = FixEinput(einput_calc);
rext = GetRext();
}
void CalculateIbdly() {
/* Ibdly is so inconsistent, I am using the most common value and then checking with a lookup table. */
ibdly = 0x1000001D + C.mem_burst_read_latency;
if (auto patch = FindIbdlyPatch()) {
ibdly += patch->adjust;
}
}
void CalculateTWTPDEN() {
tWTPDEN = tW2P + 1 + CEIL(tDQSS_max / tCK_avg) + CEIL(tDQS2DQ_max / tCK_avg) + 6;
if (C.marikoEmcMaxClock >= 2'233'000 && C.marikoEmcMaxClock < 2'533'000) tWTPDEN++;
if (C.marikoEmcMaxClock >= 2'433'000 && C.marikoEmcMaxClock < 2'800'000) tWTPDEN--;
}
void CalculateTR2W() {
tR2W = CEIL(RL_DBI + (tDQSCK_max / tCK_avg) + (BL / 2) - WL + tWPRE + FLOOR(tRPST) + 9.0);
if (auto patch = FindTR2WPatch()) {
tR2W += patch->adjust;
}
}
void CalculateTW2R() {
tW2R = WL + (BL / 2) - 6 + CEIL(tWTR / tCK_avg);
const FreqTW2R* t = FindTW2R();
if (!t) return;
tW2R += t->adjust;
if (t->min_val) {
tW2R = MAX(tW2R, t->min_val);
}
if (t->max_val) {
tW2R = MIN(tW2R, t->max_val);
}
}
void CalculateTW2RDerivedWriteTimings() {
tWTM = WL + (BL / 2) + 1 + CEIL(tW2R / tCK_avg);
tWATM = tWTM + CEIL(tWR / tCK_avg);
}
void CalculateQuse() {
quse = ROUND(0.002266 * (C.marikoEmcMaxClock / 1000.0) + 31.88) + C.mem_burst_read_latency;
if (auto patch = FindQusePatch()) {
quse += patch->adjust;
}
}
void CalculateQrst() {
qrst = 0x00070000;
u32 qrst_calc = ROUND(22.1 - (C.marikoEmcMaxClock / 1000000.0) * 8.0) + C.mem_burst_read_latency;
u32 qrst_low = MAX(static_cast<u32>(0), qrst_calc);
if (C.marikoEmcMaxClock >= 2'533'000) {
qrst = INCREMENT_HIGH_BYTES_BY(qrst, 1);
} else if (C.marikoEmcMaxClock == 2'800'000) {
qrst = SET_HIGH_BYTES(qrst, 6);
}
qrst = SET_LOW_BYTES(qrst, qrst_low);
if (auto patch = FindQrstPatch()) {
qrst = INCREMENT_LOW_BYTES_BY(qrst, patch->adjust);
}
}
void CalculateQsafe() {
qsafe = ROUND((C.marikoEmcMaxClock / 1000.0) / 138.0 + 37.4) + C.mem_burst_read_latency;
if (auto patch = FindQsafePatch()) {
qsafe += patch->adjust;
}
}
void CalculateQpop() {
qpop = FLOOR(((C.marikoEmcMaxClock / 1000.0) - 2133 + 167) / 200.0) + 0x2D + C.mem_burst_read_latency;
if (C.marikoEmcMaxClock >= 3'100'000) qpop = 0x2B; /* TODO: Check if this is actually true. */
else if (C.marikoEmcMaxClock >= 3'133'000) ++qpop;
}
void CalculatePdex2rw() {
double freq_mhz = C.marikoEmcMaxClock / 1000.0;
double pdex_local = (0.011 * freq_mhz) - 1.443;
pdex2rw = static_cast<u32>(ROUND(pdex_local));
if (pdex2rw < 22) pdex2rw = 22;
if (pdex2rw > 33) pdex2rw = 33;
if (auto patch = FindPdex2rwPatch()) {
pdex2rw += patch->adjust;
}
}
void CalculateCke2pden() {
cke2pden = (static_cast<double>((C.marikoEmcMaxClock / 1000.0) * 0.00875) - 0.65);
if (auto patch = FindCke2pdenPatch()) {
cke2pden += patch->adjust;
}
}
void CalculateTimings() {
CalculateMiscTimings();
CalculateIbdly();
CalculateTWTPDEN();
CalculateTR2W();
CalculateTW2R();
CalculateTW2RDerivedWriteTimings();
CalculateQuse();
CalculateQrst();
CalculateQsafe();
CalculateQpop();
CalculatePdex2rw();
CalculateCke2pden();
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) Lightos_
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
#pragma once
namespace ams::ldr::oc::pcv::mariko {
void CalculateTimings();
}

View File

@@ -0,0 +1,309 @@
/*
* Copyright (c) Lightos_
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
#include "../mtc_timing_value.hpp"
#include "timing_tables.hpp"
namespace ams::ldr::oc::pcv::mariko {
const MiscTimings g_misc_table[] = {
{1'866'000, 1, 0x20, 0x9, 0},
{2'133'000, 1, 0x24, 0xA, 0},
{2'166'000, 1, 0, 0, 0},
{2'233'000, 0, 0x25, 0, 0},
{2'300'000, 0, 0x26, 0xB, 0},
{2'333'000, 0, 0x27, 0, 0},
{2'366'000, 1, 0x26, 0xA, 0},
{2'433'000, 0, 0x27, 0, 0},
{2'466'000, 0, 0x2A, 0, 0},
{2'500'000, 0, 0x28, 0xB, 0},
{2'533'000, 0, 0x29, 0, -2},
{2'566'000, 1, 0, 0, 0},
{2'633'000, 0, 0x2A, 0, 0},
{2'700'000, 0, 0x2B, 0xC, 0},
{2'733'000, 0, 0x2C, 0, 0},
{2'766'000, 1, 0x2B, 0xB, 0},
{2'833'000, 0, 0x2C, 0, -2},
{2'866'000, 0, 0, 0, -2},
{2'900'000, 0, 0, 0, -2},
{2'933'000, 0, 0x2E, 0xC, 0},
{2'966'000, 1, 0, 0, 0},
{3'033'000, 0, 0x2F, 0, 0},
{3'100'000, 0, 0, 0, -2},
{3'133'000, 1, 0x31, 0xD, 0},
};
const u32 g_misc_table_size = sizeof(g_misc_table) / sizeof(g_misc_table[0]);
const ReplacePatch g_einput_patches[] = {
{2'133'000, 0x16}, {2'166'000, 0x17}, {2'200'000, 0x17},
{2'233'000, 0x16}, {2'266'000, 0x16}, {2'300'000, 0x15},
{2'333'000, 0x14}, {2'366'000, 0x16}, {2'400'000, 0x16},
{2'433'000, 0x15}, {2'466'000, 0x15}, {2'500'000, 0x14},
{2'533'000, 0x13}, {2'566'000, 0x14}, {2'600'000, 0x14},
{2'633'000, 0x13}, {2'666'000, 0x13}, {2'700'000, 0x12},
{2'733'000, 0x11}, {2'766'000, 0x13}, {2'800'000, 0x13},
{2'833'000, 0x12}, {2'866'000, 0x12}, {2'900'000, 0x12},
{2'933'000, 0x10}, {2'966'000, 0x11}, {3'000'000, 0x11},
{3'033'000, 0x10}, {3'066'000, 0x10}, {3'100'000, 0x10},
{3'133'000, 0x0F},
};
const u32 g_einput_patches_size = sizeof(g_einput_patches) / sizeof(g_einput_patches[0]);
const ReplacePatch *FindEinput() {
for (u32 i = 0; i < g_einput_patches_size; i++)
if (g_einput_patches[i].freq == C.marikoEmcMaxClock)
return &g_einput_patches[i];
return nullptr;
}
const ReplacePatch g_rext_table[] = {
{2'133'000, 0x1A}, {2'166'000, 0x19}, {2'200'000, 0x19},
{2'233'000, 0x19}, {2'266'000, 0x1A}, {2'300'000, 0x1B},
{2'333'000, 0x1B}, {2'366'000, 0x1B}, {2'400'000, 0x1B},
{2'433'000, 0x1B}, {2'466'000, 0x1B}, {2'500'000, 0x1A},
{2'533'000, 0x1C}, {2'566'000, 0x1B}, {2'600'000, 0x17},
{2'633'000, 0x1B}, {2'666'000, 0x1B}, {2'700'000, 0x1C},
{2'733'000, 0x1C}, {2'766'000, 0x1D}, {2'800'000, 0x1D},
{2'833'000, 0x1D}, {2'866'000, 0x1D}, {2'900'000, 0x1D},
{2'933'000, 0x1C}, {2'966'000, 0x1D}, {3'000'000, 0x1D},
{3'033'000, 0x1D}, {3'066'000, 0x1D}, {3'100'000, 0x1D},
{3'133'000, 0x1D},
};
const u32 g_rext_table_size = sizeof(g_rext_table) / sizeof(g_rext_table[0]);
const ReplacePatch *FindRext() {
for (u32 i = 0; i < g_rext_table_size; i++)
if (g_rext_table[i].freq == C.marikoEmcMaxClock)
return &g_rext_table[i];
return nullptr;
}
const FreqTW2R g_tw2r_table[] = {
{2'300'000, 0x2B, 0xFFFFFFFF, 0},
{2'400'000, 0, 0x2B, 0},
{2'500'000, 0x2C, 0xFFFFFFFF, -1},
{2'566'000, 0x2D, 0xFFFFFFFF, -1},
{2'700'000, 0x2E, 0xFFFFFFFF, -1},
{2'800'000, 0x2F, 0xFFFFFFFF, -1},
{2'900'000, 0x30, 0xFFFFFFFF, -1},
{3'000'000, 0x31, 0xFFFFFFFF, -1},
{3'100'000, 0x32, 0xFFFFFFFF, -1},
{0xFFFFFFFF, 0, 0x33, 0},
};
const u32 g_tw2r_table_size = sizeof(g_tw2r_table) / sizeof(g_tw2r_table[0]);
const FreqTW2R *FindTW2R() {
for (u32 i = 0; i < g_tw2r_table_size; i++)
if (C.marikoEmcMaxClock <= g_tw2r_table[i].max_freq)
return &g_tw2r_table[i];
return nullptr;
}
const AdjustPatch g_ibdly_patches[] = {
{2'133'000, -2},
{2'166'000, -1},
{2'200'000, -1},
{2'233'000, -1},
{2'266'000, -1},
{2'300'000, -2},
{2'333'000, -2},
{2'500'000, -1},
{2'533'000, -2},
{2'566'000, -1},
{2'633'000, -1},
{2'666'000, -1},
{2'700'000, -2},
{2'733'000, -2},
{2'833'000, -9},
{2'933'000, -1},
{3'100'000, -9},
{3'133'000, -9},
};
const u32 g_ibdly_table_size = sizeof(g_ibdly_patches) / sizeof(g_ibdly_patches[0]);
const AdjustPatch *FindIbdlyPatch() {
for (u32 i = 0; i < g_ibdly_table_size; i++)
if (g_ibdly_patches[i].freq == C.marikoEmcMaxClock)
return &g_ibdly_patches[i];
return nullptr;
}
const AdjustPatch g_tr2w_patches[] = {
{2'500'000, 1},
{2'533'000, 1},
{2'566'000, 1},
{2'866'000, -1},
{3'100'000, 1},
{3'133'000, 1},
};
const u32 g_tr2w_table_size = sizeof(g_tr2w_patches) / sizeof(g_tr2w_patches[0]);
const AdjustPatch *FindTR2WPatch() {
for (u32 i = 0; i < g_tr2w_table_size; i++)
if (g_tr2w_patches[i].freq == C.marikoEmcMaxClock)
return &g_tr2w_patches[i];
return nullptr;
}
const AdjustPatch g_quse_patches[] = {
{2'133'000, -1},
{2'300'000, -1},
{2'333'000, -1},
{2'366'000, +1},
{2'400'000, +1},
{2'433'000, +1},
{2'466'000, +1},
{2'500'000, -1},
{2'533'000, -1},
{2'700'000, -1},
{2'733'000, -1},
{2'766'000, +1},
{2'800'000, +1},
{2'833'000, +1},
{2'866'000, +1},
{2'900'000, +1},
{2'933'000, -1},
};
const u32 g_quse_table_size = sizeof(g_quse_patches) / sizeof(g_quse_patches[0]);
const AdjustPatch *FindQusePatch() {
for (u32 i = 0; i < g_quse_table_size; i++)
if (g_quse_patches[i].freq == C.marikoEmcMaxClock)
return &g_quse_patches[i];
return nullptr;
}
const AdjustPatch g_qrst_patches[] = {
{2'166'000, 1},
{2'200'000, 1},
{2'233'000, 1},
{2'266'000, 1},
{2'366'000, 2},
{2'400'000, 2},
{2'433'000, 1},
{2'466'000, 2},
{2'500'000, 1},
{2'533'000, -1},
{2'700'000, -1},
{2'733'000, -1},
{2'766'000, 1},
{2'800'000, 4},
{2'833'000, 1},
{2'866'000, 1},
{2'900'000, 1},
{2'933'000, -1},
{2'966'000, 1},
{3'000'000, 1},
{3'100'000, 1},
};
const u32 g_qrst_table_size = sizeof(g_qrst_patches) / sizeof(g_qrst_patches[0]);
const AdjustPatch *FindQrstPatch() {
for (u32 i = 0; i < g_qrst_table_size; i++)
if (g_qrst_patches[i].freq == C.marikoEmcMaxClock)
return &g_qrst_patches[i];
return nullptr;
}
const AdjustPatch g_qsafe_patches[] = {
{2'166'000, 1},
{2'200'000, 1},
{2'500'000, -1},
{2'533'000, -1},
{2'666'000, -1},
{2'700'000, -1},
{2'733'000, -1},
{2'800'000, -1},
{2'833'000, -1},
{2'866'000, -1},
{2'900'000, -1},
{2'933'000, -2},
{2'966'000, -1},
{3'000'000, -1},
{3'033'000, -1},
{3'066'000, -2},
{3'100'000, -2},
};
const u32 g_qsafe_table_size = sizeof(g_qsafe_patches) / sizeof(g_qsafe_patches[0]);
const AdjustPatch *FindQsafePatch() {
for (u32 i = 0; i < g_qsafe_table_size; i++)
if (g_qsafe_patches[i].freq == C.marikoEmcMaxClock)
return &g_qsafe_patches[i];
return nullptr;
}
const AdjustPatch g_pdex2rw_patches[] = {
{2'166'000, 1},
{2'300'000, 1},
{2'333'000, 1},
{2'433'000, 1},
{2'533'000, 0},
{2'633'000, -1},
{2'666'000, -1},
{2'733'000, -1},
{2'766'000, -1},
{2'800'000, -1},
{2'833'000, -1},
{2'933'000, -1},
{3'066'000, 1},
};
const u32 g_pdex2rw_table_size = sizeof(g_pdex2rw_patches) / sizeof(g_pdex2rw_patches[0]);
const AdjustPatch *FindPdex2rwPatch() {
for (u32 i = 0; i < g_pdex2rw_table_size; i++)
if (g_pdex2rw_patches[i].freq == C.marikoEmcMaxClock)
return &g_pdex2rw_patches[i];
return nullptr;
}
const AdjustPatch g_cke2pden_patches[] = {
{2'133'000, 1},
{2'166'000, 1},
{2'266'000, 1},
{2'300'000, 1},
{2'366'000, 1},
{2'400'000, 1},
{2'500'000, 1},
{2'633'000, 1},
{2'733'000, 1},
{2'833'000, 1},
{2'866'000, 1},
{2'966'000, 1},
{3'066'000, 1},
{3'100'000, 1},
};
const u32 g_cke2pden_table_size = sizeof(g_cke2pden_patches) / sizeof(g_cke2pden_patches[0]);
const AdjustPatch *FindCke2pdenPatch() {
for (u32 i = 0; i < g_cke2pden_table_size; i++)
if (g_cke2pden_patches[i].freq == C.marikoEmcMaxClock)
return &g_cke2pden_patches[i];
return nullptr;
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) Lightos_
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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/>.
*/
#pragma once
#include "../mtc_timing_value.hpp"
namespace ams::ldr::oc::pcv::mariko {
struct ReplacePatch {
u32 freq;
u32 correct;
};
extern const ReplacePatch g_einput_patches[];
extern const u32 g_einput_patches_size;
const ReplacePatch *FindEinput();
extern const ReplacePatch g_rext_table[];
extern const u32 g_rext_table_size;
const ReplacePatch *FindRext();
struct AdjustPatch {
u32 freq;
s32 adjust;
};
extern const AdjustPatch g_ibdly_patches[];
extern const u32 g_ibdly_table_size;
const AdjustPatch *FindIbdlyPatch();
extern const AdjustPatch g_tr2w_patches[];
extern const u32 g_tr2w_table_size;
const AdjustPatch *FindTR2WPatch();
extern const AdjustPatch g_quse_patches[];
extern const u32 g_quse_table_size;
const AdjustPatch *FindQusePatch();
extern const AdjustPatch g_qrst_patches[];
extern const u32 q_qrst_table_size;
const AdjustPatch *FindQrstPatch();
extern const AdjustPatch g_qsafe_patches[];
extern const u32 g_qsafe_table_size;
const AdjustPatch *FindQsafePatch();
extern const AdjustPatch g_pdex2rw_patches[];
extern const u32 g_pdex2rw_table_size;
const AdjustPatch *FindPdex2rwPatch();
extern const AdjustPatch g_cke2pden_patches[];
extern const u32 g_cke2pden_table_size;
const AdjustPatch *FindCke2pdenPatch();
struct MiscTimings {
u32 min_freq;
u32 rdv_inc;
u32 einput;
u32 quse_width;
s32 obdly_delta;
};
extern const MiscTimings g_misc_table[];
extern const u32 g_misc_table_size;
struct FreqTW2R {
u32 max_freq;
u32 min_val;
u32 max_val;
s32 adjust;
};
extern const FreqTW2R g_tw2r_table[];
extern const u32 g_tw2r_table_size;
const FreqTW2R *FindTW2R();
}