Skip to content

Commit

Permalink
pagemanager: mostly map out structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziemas committed May 3, 2024
1 parent 78917bd commit e4faa56
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 52 deletions.
11 changes: 8 additions & 3 deletions game/overlord/jak3/iso_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ namespace jak3 {
enum class EFileComp { MODE0, MODE1, KNOWN_NOT_BLZO };

struct ISOBuffer {
public:
void AdjustDataLength(int);
void AdvanceCurrentData(int);
void SetDataLength(int length) { m_nDataLength = length; }
int GetDataLength() { return m_nDataLength; }

private:
CPageManager::CPageList* m_pActivePages;
u8* decomp_buffer;
int decompressed_size;

void AdjustDataLength(int);
void AdvanceCurrentData(int);
int m_nDataLength;
};

struct VagDirEntryJak3 {
Expand Down
84 changes: 47 additions & 37 deletions game/overlord/jak3/pagemanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "pagemanager.h"

#include <cstring>

#include "common/util/Assert.h"

#include "game/sce/iop.h"

namespace jak3 {
Expand All @@ -8,32 +12,17 @@ using namespace iop;
static constexpr u32 PAGE_SIZE = 0x8010;
static constexpr u32 NUM_PAGES = 29;

static inline int ctz(u32 x) {
// Isolate lowest bit and subtract 1
x = (x & -x) - 1;

// popcount
x -= (x >> 1) & 0x55555555;
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0f0f0f0f;
x += (x >> 8);
x += (x >> 16);
x &= 0x3f;

return static_cast<int>(x);
}

CPageManager::CPage::CPage(u8* start, u8* end, int page_id) {

Check warning on line 15 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L15

Member variable 'CPage::m_pPageList' is not initialized in the constructor.
m_pNextPage = nullptr;
m_uPageBit = 1 << (page_id & 0x1f);

Check warning on line 17 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L17

Variable 'm_uPageBit' is assigned in constructor body. Consider performing initialization in initialization list.
m_nUnk = 0;
m_nPageState = 0;

Check warning on line 18 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L18

Variable 'm_nPageState' is assigned in constructor body. Consider performing initialization in initialization list.
m_pData = start;
m_pEnd = end;
m_nRefCount = 0;
m_pDataEnd = end;
m_nPageRefCount = 0;
m_nPageID = page_id;
m_nDmaRefCount = 0;
m_nPageDmaRefCount = 0;
m_nAllocState = 0;
m_nUnk2 = 0;
m_nPageState = 0;
}

int CPageManager::CPage::AddRef() {
Expand All @@ -42,9 +31,9 @@ int CPageManager::CPage::AddRef() {
CpuSuspendIntr(&state);

if (m_nAllocState == 1 && m_pPageList != nullptr) {
m_pPageList->m_nRefCount++;
m_nRefCount++;
ret = m_nRefCount;
m_pPageList->m_nListRefCount++;
m_nPageRefCount++;
ret = m_nPageRefCount;
}

CpuResumeIntr(state);
Expand All @@ -58,9 +47,9 @@ int CPageManager::CPage::ReleaseRef() {
CpuSuspendIntr(&state);

if (m_nAllocState == 1 && m_pPageList != nullptr) {
m_pPageList->m_nRefCount--;
m_nRefCount--;
ret = m_nRefCount;
m_pPageList->m_nListRefCount--;
m_nPageRefCount--;
ret = m_nPageRefCount;
}

CpuResumeIntr(state);
Expand All @@ -74,9 +63,9 @@ int CPageManager::CPage::AddDmaRef() {
CpuSuspendIntr(&state);

if (m_nAllocState == 1 && m_pPageList != nullptr) {
m_pPageList->m_nDmaRefCount++;
m_nDmaRefCount++;
ret = m_nDmaRefCount;
m_pPageList->m_nListDmaRefCount++;
m_nPageDmaRefCount++;
ret = m_nPageDmaRefCount;
}

CpuResumeIntr(state);
Expand All @@ -90,9 +79,9 @@ int CPageManager::CPage::ReleaseDmaRef() {
CpuSuspendIntr(&state);

if (m_nAllocState == 1 && m_pPageList != nullptr) {
m_pPageList->m_nDmaRefCount--;
m_nDmaRefCount--;
ret = m_nDmaRefCount;
m_pPageList->m_nListDmaRefCount--;
m_nPageDmaRefCount--;
ret = m_nPageDmaRefCount;
}

CpuResumeIntr(state);
Expand All @@ -101,20 +90,37 @@ int CPageManager::CPage::ReleaseDmaRef() {
}

void CPageManager::CPage::FromPagesCopy(u8* pInPageData, u8* pDest, int nNumBytes) {
int nInputPageLengthLeft;

Check notice on line 93 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L93

The scope of the variable 'nInputPageLengthLeft' can be reduced.
CPage* page = this;

if (nNumBytes <= 0) {
return;
}

}
for (;;) {
nInputPageLengthLeft = (uintptr_t)page->m_pDataEnd + (1 - (uintptr_t)pInPageData);
if (nInputPageLengthLeft <= nNumBytes) {
break;
}

void CPageManager::Initialize() {
m_Cache.Initialize();
if (nNumBytes <= 0) {
nNumBytes -= nInputPageLengthLeft;

Check notice on line 107 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L107

Variable 'nNumBytes' is assigned a value that is never used.
return;
}

ASSERT(m_pNextPage != nullptr);

page = page->m_pNextPage;
pInPageData = page->m_pData;
}

memcpy(pDest, pInPageData, nNumBytes);
}

CPageManager::CCache::CCache() {

Check warning on line 120 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L120

Member variable 'CCache::m_nEventFlag' is not initialized in the constructor.
m_paCache = nullptr;
m_uAllocatedPageMap = 0;

Check warning on line 122 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L122

Variable 'm_uAllocatedPageMap' is assigned in constructor body. Consider performing initialization in initialization list.
m_uFreePageMap = (1 << NUM_PAGES) - 1;
m_Unk = (1 << NUM_PAGES) - 1;

Check warning on line 123 in game/overlord/jak3/pagemanager.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

game/overlord/jak3/pagemanager.cpp#L123

Variable 'm_Unk' is assigned in constructor body. Consider performing initialization in initialization list.
}

int CPageManager::CCache::Initialize() {
Expand All @@ -125,8 +131,8 @@ int CPageManager::CCache::Initialize() {

m_nNumFree = 29;

/* TODO FIXME */
for (auto& pl : m_aPageLists) {
pl = {};
}

int page_idx = 0;
Expand All @@ -141,4 +147,8 @@ int CPageManager::CCache::Initialize() {
return 0;
}

void CPageManager::Initialize() {
m_Cache.Initialize();
}

} // namespace jak3
56 changes: 44 additions & 12 deletions game/overlord/jak3/pagemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class CPageManager {
public:
class CPageList;

enum class AllocState {
EPLAS_FREE,
EPLAS_ALLOCATED,
EPLAS_FILLED,
};

class CPage {
public:
CPage() = default;
Expand All @@ -16,19 +22,28 @@ class CPageManager {
int AddDmaRef();
int ReleaseDmaRef();
void FromPagesCopy(u8* pInPageData, u8* pDest, int nNumBytes);
u8* GetStart() const;
u8* GetEnd() const;
bool IsReferenced() { return m_nPageRefCount != 0 || m_nPageDmaRefCount != 0; }
bool IsFilled() const; // Exists as string in july version, not sure on impl
bool IsAllocated() const;
void SetPageInputState(int state); // enum
int GetPageCacheIndex() const;
CPage* GetNextPage() const;
CPage* GetPrevPage() const;

private:
CPage* m_pNextPage;
u32 m_uPageBit;
u32 m_nUnk;
CPage* m_pPrevPage;
CPageList* m_pPageList;
int m_nPageRefCount;
int m_nPageDmaRefCount;
int m_nAllocState;
u32 m_nPageState; // 3: filled otherwise unk?
u8* m_pData;
u8* m_pEnd; // maybe
int m_nRefCount;
int m_nDmaRefCount;
u8* m_pDataEnd;
int m_nPageID;
int m_nAllocState;
int m_nUnk2;
u32 m_uPageBit;
};

class CPageList {
Expand All @@ -39,10 +54,25 @@ class CPageManager {
int CancelActivePages();
CPage* StepActivePage();
void GarbageCollect();
CPage* GetCurrentActivePage() const;
int GetNumRawPages() const;
int GetFirstRawPage() const;
int GetNumUnsteppedPages() const;
bool IsReferenced() { return m_nListRefCount != 0 || m_nListDmaRefCount != 0; }
int GetNumActivePages() const;

private:
int m_nRefCount;
int m_nDmaRefCount;
CPage* m_pFirstPage;
CPage* m_pLastPage;
CPage* m_pFirstACtivePage;
CPage* m_pLastActivePage;
CPage* m_pCurrentActivePage;
int m_nNumPages;
int m_nNumActivePages;
int m_nNumUnsteppedPages;
int m_nListRefCount;
int m_nListDmaRefCount;
int m_nAllocState;
};

void Initialize();
Expand All @@ -60,14 +90,16 @@ class CPageManager {
int Initialize();

private:
int m_nEventFlag;
void* m_paCache;
CPageList m_aPageLists[29];
CPage m_aPages[29];
u32 m_uAllocatedPageMap;
u32 m_uFreePageMap;
int m_nNumFree;
u32 m_uAllocatedPageMap;
u32 m_uAllocatedListMap;
int m_nEventFlag;
u32 m_Unk;
};

CCache m_Cache;
};

Expand Down

0 comments on commit e4faa56

Please sign in to comment.