Skip to content

Commit

Permalink
Booting partially through xam.xex now, implemented a few syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
google0101-ryan committed Jan 5, 2024
1 parent 2d42614 commit d12b576
Show file tree
Hide file tree
Showing 16 changed files with 762 additions and 17 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set(SOURCES src/memory/memory.cpp
src/main.cpp
src/loader/xex.cpp
src/cpu/CPU.cpp
src/cpu/ops.cpp)
src/cpu/ops.cpp
src/kernel/kernel.cpp
src/kernel/modules/xboxkrnl.cpp
src/vfs/VFS.cpp)

set(AES_SOURCES src/crypto/rijndael-alg-fst.cpp)
set(LZX_SOURCES src/thirdparty/lzxd.cpp
Expand Down
64 changes: 61 additions & 3 deletions src/cpu/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <cassert>
#include "CPU.h"

CPUThread::CPUThread(uint32_t entryPoint, uint32_t stackSize)
CPUThread::CPUThread(uint32_t entryPoint, uint32_t stackSize, XexLoader& ref)
: xexRef(ref)
{
std::memset(&state, 0, sizeof(state));

Expand All @@ -31,11 +32,23 @@ void CPUThread::Run()
state.pc += 4;

printf("0x%08x (0x%08lx): ", instr, state.pc-4);

if (((instr >> 26) & 0x3F) == 10)

if (((instr >> 26) & 0x3F) == 7)
{
mulli(instr);
}
else if (((instr >> 26) & 0x3F) == 10)
{
cmpli(instr);
}
else if (((instr >> 26) & 0x3F) == 11)
{
cmpi(instr);
}
else if (((instr >> 26) & 0x3F) == 12)
{
addic(instr);
}
else if (((instr >> 26) & 0x3F) == 14)
{
addi(instr);
Expand All @@ -48,6 +61,10 @@ void CPUThread::Run()
{
bc(instr);
}
else if (((instr >> 26) & 0x3F) == 17)
{
sc(instr);
}
else if (((instr >> 26) & 0x3F) == 18)
{
branch(instr);
Expand All @@ -56,6 +73,10 @@ void CPUThread::Run()
{
bclr(instr);
}
else if (((instr >> 26) & 0x3F) == 20)
{
rlwimi(instr);
}
else if (((instr >> 26) & 0x3F) == 21)
{
rlwinm(instr);
Expand All @@ -64,6 +85,30 @@ void CPUThread::Run()
{
ori(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 20)
{
lwarx(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 83)
{
mfmsr(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 136)
{
subfe(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 150)
{
stwcx(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 178)
{
mtmsrd(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 266)
{
add(instr);
}
else if (((instr >> 26) & 0x3F) == 31 && ((instr >> 1) & 0x3FF) == 339)
{
mfspr(instr);
Expand All @@ -80,6 +125,10 @@ void CPUThread::Run()
{
lwz(instr);
}
else if (((instr >> 26) & 0x3F) == 34)
{
lbz(instr);
}
else if (((instr >> 26) & 0x3F) == 36)
{
stw(instr);
Expand All @@ -88,6 +137,14 @@ void CPUThread::Run()
{
stwu(instr);
}
else if (((instr >> 26) & 0x3F) == 38)
{
stb(instr);
}
else if (((instr >> 26) & 0x3F) == 44)
{
sth(instr);
}
else if (((instr >> 26) & 0x3F) == 58)
{
ld(instr);
Expand All @@ -109,6 +166,7 @@ void CPUThread::Dump()
printf("r%d\t->\t0x%08lx\n", i, state.regs[i]);
for (int i = 0; i < 7; i++)
printf("cr%d\t->\t%d\n", i, state.GetCR(i));
printf("[%s]\n", state.xer.ca ? "c" : ".");
}

void CPUThread::SetArg(int num, uint64_t value)
Expand Down
28 changes: 27 additions & 1 deletion src/cpu/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdlib>

#include <types.h>
#include <loader/xex.h>

/// @brief Contains most of the CPU state, including registers, SPRs, etc.
/// We declare this in a struct so the scheduler can access it to save/restore CPU state on a context switch
Expand All @@ -14,6 +15,7 @@ typedef struct
uint64_t regs[32]; // These are mostly accessed as single uint32_t registers
uint64_t ctr;
uint64_t lr;
uint64_t msr;

struct
{
Expand Down Expand Up @@ -70,6 +72,11 @@ typedef struct
else if (x > y) SetCR(num, 0x4);
else SetCR(num, 0x2);
}

struct XER
{
bool ca;
} xer;
} cpuState_t;

/// @brief This will represent one of 6 hardware threads running at a time.
Expand All @@ -79,31 +86,50 @@ typedef struct
class CPUThread
{
public:
CPUThread(uint32_t entryPoint, uint32_t stackSize);
CPUThread(uint32_t entryPoint, uint32_t stackSize, XexLoader& ref);

void Run();
void Dump();

void SetArg(int num, uint64_t value);

cpuState_t& GetState() {return state;}
private:
void mulli(uint32_t instruction); // 7
void cmpli(uint32_t instruction); // 10
void cmpi(uint32_t instruction); // 11
void addic(uint32_t instruction); // 12
void addi(uint32_t instruction); // 14
void addis(uint32_t instruction); // 15
void bc(uint32_t instruction); // 16
void sc(uint32_t instruction); // 17
void branch(uint32_t instruction); // 18
void bclr(uint32_t instruction); // 19 16
void rlwimi(uint32_t instruction); // 20
void rlwinm(uint32_t instruction); // 21
void ori(uint32_t instruction); // 24
void lwarx(uint32_t instruction); // 31 20
void cmpl(uint32_t instruction); // 31
void mfmsr(uint32_t instruction); // 31 83
void subfe(uint32_t instruction); // 31 136
void stwcx(uint32_t instruction); // 31 150
void mtmsrd(uint32_t instruction); // 31 178
void add(uint32_t instruction); // 31 266
void mfspr(uint32_t instruction); // 31 339
void or_(uint32_t instruction); // 31 444
void mtspr(uint32_t instruction); // 31 467
void lwz(uint32_t instruction); // 32
void lbz(uint32_t instruction); // 34
void stw(uint32_t instruction); // 36
void stwu(uint32_t instruction); // 37
void stb(uint32_t instruction); // 38
void sth(uint32_t instruction); // 44
void ld(uint32_t instruction); // 58
void std(uint32_t instruction); // 62
private:
bool CondPassed(uint8_t bo, uint8_t bi);
private:
cpuState_t state;

XexLoader& xexRef;
};
Loading

0 comments on commit d12b576

Please sign in to comment.