merge shit, for easier merging later

This commit is contained in:
Arisotura 2021-08-31 01:07:56 +02:00
commit 43223f0a90
175 changed files with 59936 additions and 1085 deletions

View File

@ -0,0 +1,26 @@
trigger:
- master
pool:
name: Default
demands:
- agent.name -equals MacStadium-ARM64-Mac
workspace:
clean: all
steps:
- script: mkdir $(Pipeline.Workspace)/build
displayName: 'Create build environment'
- script: arch -arm64 cmake $(Build.SourcesDirectory) -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_PREFIX_PATH="$(brew --prefix qt@6);$(brew --prefix libarchive)" -DMACOS_BUNDLE_LIBS=ON -DMACOS_BUILD_DMG=ON -DUSE_QT6=ON
displayName: 'Configure'
workingDirectory: $(Pipeline.Workspace)/build
- script: arch -arm64 make -j$(sysctl -n hw.logicalcpu)
displayName: 'Make'
workingDirectory: $(Pipeline.Workspace)/build
- publish: $(Pipeline.Workspace)/build/melonDS.dmg
artifact: melonDS.dmg

View File

@ -51,5 +51,5 @@ jobs:
&& cp melonDS dist
- uses: actions/upload-artifact@v1
with:
name: melonDS
name: melonDS-ubuntu-aarch64
path: ${{runner.workspace}}/build/dist

View File

@ -33,5 +33,5 @@ jobs:
cp melonDS dist
- uses: actions/upload-artifact@v1
with:
name: melonDS
name: melonDS-ubuntu-x86_64
path: ${{runner.workspace}}/build/dist

View File

@ -43,5 +43,5 @@ jobs:
- uses: actions/upload-artifact@v1
with:
name: melonDS
name: melonDS-windows-x86_64
path: ${{runner.workspace}}\build\melonDS.exe

View File

@ -67,6 +67,13 @@ endif()
if (ENABLE_JIT)
add_definitions(-DJIT_ENABLED)
option(ENABLE_JIT_PROFILING "Enable JIT profiling with VTune" OFF)
if (ENABLE_JIT_PROFILING)
include(cmake/FindVTune.cmake)
add_definitions(-DJIT_PROFILING_ENABLED)
endif()
endif()
if (CMAKE_BUILD_TYPE STREQUAL Release)
@ -121,6 +128,13 @@ if (ENABLE_LTO)
endif()
endif()
find_program(CCACHE "ccache")
if (CCACHE)
message(STATUS "Using CCache to speed up compilation")
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE})
endif()
option(BUILD_QT_SDL "Build Qt/SDL frontend" ON)
add_subdirectory(src)

View File

@ -8,8 +8,9 @@
<br>
<a href="https://github.com/Arisotura/melonDS/actions?query=workflow%3A%22CMake+Build+%28Windows+x86-64%29%22+event%3Apush"><img src="https://img.shields.io/github/workflow/status/Arisotura/melonDS/CMake%20Build%20(Windows%20x86-64)?label=Windows%20x86-64&logo=GitHub"></img></a>
<a href="https://github.com/Arisotura/melonDS/actions?query=workflow%3A%22CMake+Build+%28Ubuntu+x86-64%29%22+event%3Apush"><img src="https://img.shields.io/github/workflow/status/Arisotura/melonDS/CMake%20Build%20(Ubuntu%20x86-64)?label=Linux%20x86-64&logo=GitHub"></img></a>
<a href="https://dev.azure.com/melonDS/melonDS/_build?definitionId=1&repositoryFilter=1&branchFilter=2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2"><img src="https://img.shields.io/azure-devops/build/melonDS/7c9c08a1-669f-42a4-bef4-a6c74eadf723/1/master?label=macOS%20x86-64&logo=Azure%20Pipelines"></img></a>
<a href="https://github.com/Arisotura/melonDS/actions?query=workflow%3A%22CMake+Build+%28Ubuntu+aarch64%29%22+event%3Apush"><img src="https://img.shields.io/github/workflow/status/Arisotura/melonDS/CMake%20Build%20(Ubuntu%20aarch64)?label=Linux%20ARM64&logo=GitHub"></img></a>
<a href="https://dev.azure.com/melonDS/melonDS/_build?definitionId=1&repositoryFilter=1&branchFilter=2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2%2C2"><img src="https://img.shields.io/azure-devops/build/melonDS/7c9c08a1-669f-42a4-bef4-a6c74eadf723/1/master?label=macOS%20x86-64&logo=Azure%20Pipelines"></img></a>
<a href="https://dev.azure.com/melonDS/melonDS/_build?definitionId=2&_a=summary&repositoryFilter=1&branchFilter=2%2C2%2C2%2C2%2C2"><img src="https://img.shields.io/azure-devops/build/melonDS/7c9c08a1-669f-42a4-bef4-a6c74eadf723/2/master?label=macOS%20ARM64&logo=Azure%20Pipelines"></img></a>
</p>
DS emulator, sorta
@ -49,8 +50,6 @@ As for the rest, the interface should be pretty straightforward. If you have a q
```
3. Compile:
```bash
mkdir -p build
cd build
cmake ..
make -j$(nproc --all)
```

5
cmake/FindVTune.cmake Normal file
View File

@ -0,0 +1,5 @@
find_path(VTUNE_PATH "")
include_directories("${VTUNE_PATH}/include")
link_directories("${VTUNE_PATH}/lib64")

150
contributing.md Normal file
View File

@ -0,0 +1,150 @@
# Contributor guide for melonDS
Please follow a style as documented here. Note that this guide was not always enforced, so some parts of the code violate it.
Files should use 4 spaces for indentation.
```c++
// for single line comments prefer C++ style
/*
for multiline comments
both C style comments
*/
// as well as
// C++ style comments are possible
// when including headers from the C standard library use their C names (so don't use cstdio/cstring, ...)
#include <stdio.h>
// namespaces in PascalCase
namespace Component
{ // for all constructs curly braces go onto the following line
// the content of namespaces should not be indented
int GlobalVariable; // in PascalCase
// function names should use PascalCase, parameters camelCase:
void Test(int someParam)
{
int variable = someParam * 2; // local variables in camelCase
// you can slightly vary the spacing around operators:
int variable2 = someParam*2 + 1;
// but avoid something like this: someParam* 2+ 3
for (int i = 0; i < variable; i++) // always a space between if/for/while and the braces
{
// not using curly braces is allowed
// if the body of the if/for/while is simple:
if ((i % 2) == 0)
printf("%d\n", i); // no space between the function name and the braces
}
}
// defines should always be in CAPITALISED_SNAKE_CASE
#ifdef SOME_CONFIGURATION
// the content of #ifdef sections should not be indented
// the only exception being otherwise hard to read nested sections of #ifdef/#if/#defines
// as e.g. in ARMJIT_Memory.cpp
// prefer #ifdef/#ifndef, only use if defined(...) for complex expressions like this:
#if defined(THIS) || defined(THAT)
// ...
#endif
class MyClass // PascalCase
{
public: // access specfications are not indented
void Test(int param) // for methods the same rules apply as for functions
{
}
private:
int MemberVariable; // PascalCase, no prefix
};
#endif
#endif
enum
{
// enums should always have a common prefix in camelCase
// separated by an underscore with the item name
// which has to be in PascalCase
enumPrefix_FirstElement,
enumPrefix_SecondElement,
enumPrefix_ThirdElement,
enumPrefix_FourthElement,
};
}
```
Some additional notes:
* Keep the definition and initialisation of local variables in one place and keep the scope of local variables as small as possible.
**That means avoid code like this**:
```cpp
void ColorConvert(u32* dst, u16* vram)
{
u16 color;
u8 r, g, b;
int i;
for (i = 0; i < 256; i++)
{
color = vram[i];
r = (color & 0x001F) << 1;
g = (color & 0x03E0) >> 4;
b = (color & 0x7C00) >> 9;
dst[i] = r | (g << 8) | (b << 16);
}
}
```
**Do this instead:**
```cpp
void ColorConvert(u32* dst, u16* vram)
{
for (int i = 0; i < 256; i++)
{
u16 color = vram[i];
u8 r = (color & 0x001F) << 1;
u8 g = (color & 0x03E0) >> 4;
u8 b = (color & 0x7C00) >> 9;
dst[i] = r | (g << 8) | (b << 16);
}
}
```
* For integer types preferably use the explictly typed ones. We have short aliases for them defined in types.h (for unsigned types: `u8`, `u16`, `u32`, `u16`. For signed `s8`, `s16`, `s32`, `s64`). In some situations like loop variables, using `int` is possible as well.
* Don't overdo object oriented programming. Always try to use a simpler construct first, only use a polymorphic class if a namespace with functions in it doesn't cut it.
* In doubt put a namespace around your part of the code.
* C style strings (and the associated functions from the C standard library) are used in most places. We are thinking about changing this, as C strings are a bit of a hassle to deal with, but for the time being this is what we use.
* Only the C standard IO is used (so use `printf`, `fopen`, … Do not use `std::cout`/`std::ostream`, …).
* Complex C++ containers can be used (`std::vector`, `std::list`, `std::map`, …). `std::array` is usually not used, unless necessary so that the container can be used with other C++ constructs (e.g. `<algorithm>`). Only use them if a C array doesn't cut it.
* File names should be in PascalCase
* If a header file is called MyHeader.h it should be guarded with an ifdef like this:
```cpp
#ifndef MYHEADER_H
#define MYHEADER_H
// ...
#endif
```
* And at last, if you have any questions, visit us on IRC (see the readme)!

View File

@ -22,5 +22,17 @@
<true/>
<key>NSMicrophoneUsageDescription</key>
<string>We need microphone access so you can use the emulated DS microphone</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>nds</string>
<string>srl</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
</dict>
</array>
</dict>
</plist>

View File

@ -1,6 +1,6 @@
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file alias="melon-icon">icon/melon_32x32.png</file>
<file alias="melon-icon">icon/melon_256x256.png</file>
</qresource>
</RCC>
</RCC>

View File

@ -2,7 +2,7 @@
Name=melonDS
GenericName=Nintendo DS Emulator
Comment=A fast and accurate Nintendo DS emulator.
Exec=melonDS
Exec=melonDS %f
Type=Application
Categories=Game;Emulator;
Terminal=false

View File

@ -17,6 +17,7 @@
*/
#include <stdio.h>
#include <algorithm>
#include "NDS.h"
#include "DSi.h"
#include "ARM.h"
@ -392,74 +393,71 @@ void ARM::RestoreCPSR()
void ARM::UpdateMode(u32 oldmode, u32 newmode)
{
u32 temp;
#define SWAP(a, b) temp = a; a = b; b = temp;
if ((oldmode & 0x1F) == (newmode & 0x1F)) return;
switch (oldmode & 0x1F)
{
case 0x11:
SWAP(R[8], R_FIQ[0]);
SWAP(R[9], R_FIQ[1]);
SWAP(R[10], R_FIQ[2]);
SWAP(R[11], R_FIQ[3]);
SWAP(R[12], R_FIQ[4]);
SWAP(R[13], R_FIQ[5]);
SWAP(R[14], R_FIQ[6]);
std::swap(R[8], R_FIQ[0]);
std::swap(R[9], R_FIQ[1]);
std::swap(R[10], R_FIQ[2]);
std::swap(R[11], R_FIQ[3]);
std::swap(R[12], R_FIQ[4]);
std::swap(R[13], R_FIQ[5]);
std::swap(R[14], R_FIQ[6]);
break;
case 0x12:
SWAP(R[13], R_IRQ[0]);
SWAP(R[14], R_IRQ[1]);
std::swap(R[13], R_IRQ[0]);
std::swap(R[14], R_IRQ[1]);
break;
case 0x13:
SWAP(R[13], R_SVC[0]);
SWAP(R[14], R_SVC[1]);
std::swap(R[13], R_SVC[0]);
std::swap(R[14], R_SVC[1]);
break;
case 0x17:
SWAP(R[13], R_ABT[0]);
SWAP(R[14], R_ABT[1]);
std::swap(R[13], R_ABT[0]);
std::swap(R[14], R_ABT[1]);
break;
case 0x1B:
SWAP(R[13], R_UND[0]);
SWAP(R[14], R_UND[1]);
std::swap(R[13], R_UND[0]);
std::swap(R[14], R_UND[1]);
break;
}
switch (newmode & 0x1F)
{
case 0x11:
SWAP(R[8], R_FIQ[0]);
SWAP(R[9], R_FIQ[1]);
SWAP(R[10], R_FIQ[2]);
SWAP(R[11], R_FIQ[3]);
SWAP(R[12], R_FIQ[4]);
SWAP(R[13], R_FIQ[5]);
SWAP(R[14], R_FIQ[6]);
std::swap(R[8], R_FIQ[0]);
std::swap(R[9], R_FIQ[1]);
std::swap(R[10], R_FIQ[2]);
std::swap(R[11], R_FIQ[3]);
std::swap(R[12], R_FIQ[4]);
std::swap(R[13], R_FIQ[5]);
std::swap(R[14], R_FIQ[6]);
break;
case 0x12:
SWAP(R[13], R_IRQ[0]);
SWAP(R[14], R_IRQ[1]);
std::swap(R[13], R_IRQ[0]);
std::swap(R[14], R_IRQ[1]);
break;
case 0x13:
SWAP(R[13], R_SVC[0]);
SWAP(R[14], R_SVC[1]);
std::swap(R[13], R_SVC[0]);
std::swap(R[14], R_SVC[1]);
break;
case 0x17:
SWAP(R[13], R_ABT[0]);
SWAP(R[14], R_ABT[1]);
std::swap(R[13], R_ABT[0]);
std::swap(R[14], R_ABT[1]);
break;
case 0x1B:
SWAP(R[13], R_UND[0]);
SWAP(R[14], R_UND[1]);
std::swap(R[13], R_UND[0]);
std::swap(R[14], R_UND[1]);
break;
}

View File

@ -661,7 +661,10 @@ void A_MOV_REG_LSL_IMM_DBG(ARM* cpu)
(cpu->NextInstr[0] & 0xFF000000) == 0xEA000000 && // branch
(cpu->NextInstr[1] & 0xFFFF) == 0x6464)
{
u32 addr = cpu->R[15] + 2;
// GBATek says the two bytes after the 2nd ID are _reserved_ for flags
// but since they serve no purpose ATTOW, we can skip them
u32 addr = cpu->R[15] + 4; // Skip 2nd ID and flags
// TODO: Pass flags to NocashPrint
NDS::NocashPrint(cpu->Num, addr);
}
}
@ -1527,7 +1530,10 @@ void T_MOV_HIREG(ARM* cpu)
(cpu->NextInstr[0] & 0xF800) == 0xE000 && // branch
(cpu->NextInstr[1] & 0xFFFF) == 0x6464)
{
u32 addr = cpu->R[15] + 2;
// GBATek says the two bytes after the 2nd ID are _reserved_ for flags
// but since they serve no purpose ATTOW, we can skip them
u32 addr = cpu->R[15] + 4; // Skip 2nd ID and flags
// TODO: Pass flags to NocashPrint
NDS::NocashPrint(cpu->Num, addr);
}
}

View File

@ -43,9 +43,6 @@
#include "Wifi.h"
#include "NDSCart.h"
#if defined(__APPLE__) && defined(__aarch64__)
#include <pthread.h>
#endif
#include "ARMJIT_x64/ARMJIT_Offsets.h"
static_assert(offsetof(ARM, CPSR) == ARM_CPSR_offset, "");
@ -320,9 +317,7 @@ void Init()
void DeInit()
{
#if defined(__APPLE__) && defined(__aarch64__)
pthread_jit_write_protect_np(false);
#endif
JitEnableWrite();
ResetBlockCache();
ARMJIT_Memory::DeInit();
@ -331,9 +326,7 @@ void DeInit()
void Reset()
{
#if defined(__APPLE__) && defined(__aarch64__)
pthread_jit_write_protect_np(false);
#endif
JitEnableWrite();
ResetBlockCache();
ARMJIT_Memory::Reset();
@ -645,6 +638,8 @@ void CompileBlock(ARM* cpu)
u32 lr;
bool hasLink = false;
bool hasMemoryInstr = false;
do
{
r15 += thumb ? 2 : 4;
@ -707,6 +702,10 @@ void CompileBlock(ARM* cpu)
}
instrs[i].Info = ARMInstrInfo::Decode(thumb, cpu->Num, instrs[i].Instr);
hasMemoryInstr |= thumb
? (instrs[i].Info.Kind >= ARMInstrInfo::tk_LDR_PCREL && instrs[i].Info.Kind <= ARMInstrInfo::tk_STMIA)
: (instrs[i].Info.Kind >= ARMInstrInfo::ak_STR_REG_LSL && instrs[i].Info.Kind <= ARMInstrInfo::ak_STM);
cpu->R[15] = r15;
cpu->CurInstr = instrs[i].Instr;
cpu->CodeCycles = instrs[i].CodeCycles;
@ -780,7 +779,8 @@ void CompileBlock(ARM* cpu)
JIT_DEBUGPRINT("merged BL\n");
}
if (instrs[i].Info.Branches() && Config::JIT_BranchOptimisations)
if (instrs[i].Info.Branches() && Config::JIT_BranchOptimisations
&& instrs[i].Info.Kind != (thumb ? ARMInstrInfo::tk_SVC : ARMInstrInfo::ak_SVC))
{
bool hasBranched = cpu->R[15] != r15;
@ -846,6 +846,7 @@ void CompileBlock(ARM* cpu)
if (!hasBranched && cond < 0xE && i + 1 < Config::JIT_MaxBlockSize)
{
JIT_DEBUGPRINT("block lengthened by untaken branch\n");
instrs[i].Info.EndBlock = false;
instrs[i].BranchFlags |= branch_FollowCondNotTaken;
}
@ -912,13 +913,10 @@ void CompileBlock(ARM* cpu)
block->StartAddrLocal = localAddr;
FloodFillSetFlags(instrs, i - 1, 0xF);
#if defined(__APPLE__) && defined(__aarch64__)
pthread_jit_write_protect_np(false);
#endif
block->EntryPoint = JITCompiler->CompileBlock(cpu, thumb, instrs, i);
#if defined(__APPLE__) && defined(__aarch64__)
pthread_jit_write_protect_np(true);
#endif
JitEnableWrite();
block->EntryPoint = JITCompiler->CompileBlock(cpu, thumb, instrs, i, hasMemoryInstr);
JitEnableExecute();
JIT_DEBUGPRINT("block start %p\n", block->EntryPoint);
}
@ -1162,4 +1160,20 @@ void ResetBlockCache()
JITCompiler->Reset();
}
void JitEnableWrite()
{
#if defined(__APPLE__) && defined(__aarch64__)
if (__builtin_available(macOS 11.0, *))
pthread_jit_write_protect_np(false);
#endif
}
void JitEnableExecute()
{
#if defined(__APPLE__) && defined(__aarch64__)
if (__builtin_available(macOS 11.0, *))
pthread_jit_write_protect_np(true);
#endif
}
}

View File

@ -24,6 +24,10 @@
#include "ARM.h"
#include "ARM_InstrInfo.h"
#if defined(__APPLE__) && defined(__aarch64__)
#include <pthread.h>
#endif
namespace ARMJIT
{
@ -48,6 +52,8 @@ void ResetBlockCache();
JitBlockEntry LookUpBlock(u32 num, u64* entries, u32 offset, u32 addr);
bool SetupExecutableRegion(u32 num, u32 blockAddr, u64*& entry, u32& start, u32& size);
void JitEnableWrite();
void JitEnableExecute();
}
extern "C" void ARM_Dispatch(ARM* cpu, ARMJIT::JitBlockEntry entry);

View File

@ -324,16 +324,29 @@ void Compiler::Comp_Arithmetic(int op, bool S, ARM64Reg rd, ARM64Reg rn, Op2 op2
UBFX(W2, RCPSR, 29, 1);
if (S)
{
CVInGPR = true;
ADDS(W1, rn, W2);
CSET(W2, CC_CS);
CSET(W3, CC_VS);
if (op2.IsImm)
ADDSI2R(rd, W1, op2.Imm, W0);
{
CVInGPR = true;
ADDS(W1, rn, W2);
CSET(W2, CC_CS);
CSET(W3, CC_VS);
if (op2.IsImm)
ADDSI2R(rd, W1, op2.Imm, W0);
else
ADDS(rd, W1, op2.Reg.Rm, op2.ToArithOption());
CSINC(W2, W2, WZR, CC_CC);
CSINC(W3, W3, WZR, CC_VC);
}
else
ADDS(rd, W1, op2.Reg.Rm, op2.ToArithOption());
CSINC(W2, W2, WZR, CC_CC);
CSINC(W3, W3, WZR, CC_VC);
{
if (op2.Reg.ShiftAmount > 0)
{
MOV(W0, op2.Reg.Rm, op2.ToArithOption());
op2 = Op2(W0, ST_LSL, 0);
}
CMP(W2, 1);
ADCS(rd, rn, op2.Reg.Rm);
}
}
else
{
@ -346,25 +359,38 @@ void Compiler::Comp_Arithmetic(int op, bool S, ARM64Reg rd, ARM64Reg rn, Op2 op2
break;
case 0x6: // SBC
UBFX(W2, RCPSR, 29, 1);
// W1 = -op2 - 1
if (op2.IsImm)
MOVI2R(W1, ~op2.Imm);
else
ORN(W1, WZR, op2.Reg.Rm, op2.ToArithOption());
if (S)
if (S && !op2.IsImm)
{
CVInGPR = true;
ADDS(W1, W2, W1);
CSET(W2, CC_CS);
CSET(W3, CC_VS);
ADDS(rd, rn, W1);
CSINC(W2, W2, WZR, CC_CC);
CSINC(W3, W3, WZR, CC_VC);
if (op2.Reg.ShiftAmount > 0)
{
MOV(W0, op2.Reg.Rm, op2.ToArithOption());
op2 = Op2(W0, ST_LSL, 0);
}
CMP(W2, 1);
SBCS(rd, rn, op2.Reg.Rm);
}
else
{
ADD(W1, W2, W1);
ADD(rd, rn, W1);
// W1 = -op2 - 1
if (op2.IsImm)
MOVI2R(W1, ~op2.Imm);
else
ORN(W1, WZR, op2.Reg.Rm, op2.ToArithOption());
if (S)
{
CVInGPR = true;
ADDS(W1, W2, W1);
CSET(W2, CC_CS);
CSET(W3, CC_VS);
ADDS(rd, rn, W1);
CSINC(W2, W2, WZR, CC_CC);
CSINC(W3, W3, WZR, CC_VC);
}
else
{
ADD(W1, W2, W1);
ADD(rd, rn, W1);
}
}
break;
case 0x7: // RSC
@ -533,21 +559,7 @@ void Compiler::A_Comp_ALUMovOp()
}
else
{
// ORR with shifted operand has cycles latency
if (op2.Reg.ShiftAmount > 0)
{
switch (op2.Reg.ShiftType)
{
case ST_LSL: LSL(rd, op2.Reg.Rm, op2.Reg.ShiftAmount); break;
case ST_LSR: LSR(rd, op2.Reg.Rm, op2.Reg.ShiftAmount); break;
case ST_ASR: ASR(rd, op2.Reg.Rm, op2.Reg.ShiftAmount); break;
case ST_ROR: ROR(rd, op2.Reg.Rm, op2.Reg.ShiftAmount); break;
}
}
else
{
MOV(rd, op2.Reg.Rm, op2.ToArithOption());
}
MOV(rd, op2.Reg.Rm, op2.ToArithOption());
}
}

View File

@ -27,7 +27,7 @@ namespace ARMJIT
{
template <typename T>
void jumpToTrampoline(T* cpu, u32 addr, bool changeCPSR)
void JumpToTrampoline(T* cpu, u32 addr, bool changeCPSR)
{
cpu->JumpTo(addr, changeCPSR);
}
@ -301,7 +301,7 @@ void Compiler::Comp_JumpTo(Arm64Gen::ARM64Reg addr, bool switchThumb, bool resto
bool cpsrDirty = CPSRDirty;
SaveCPSR();
SaveCycles();
PushRegs(restoreCPSR);
PushRegs(restoreCPSR, true);
if (switchThumb)
MOV(W1, addr);
@ -315,11 +315,11 @@ void Compiler::Comp_JumpTo(Arm64Gen::ARM64Reg addr, bool switchThumb, bool resto
MOV(X0, RCPU);
MOVI2R(W2, restoreCPSR);
if (Num == 0)
QuickCallFunction(X3, jumpToTrampoline<ARMv5>);
QuickCallFunction(X3, JumpToTrampoline<ARMv5>);
else
QuickCallFunction(X3, jumpToTrampoline<ARMv4>);
QuickCallFunction(X3, JumpToTrampoline<ARMv4>);
PopRegs(restoreCPSR);
PopRegs(restoreCPSR, true);
LoadCycles();
LoadCPSR();
if (CurInstr.Cond() < 0xE)

View File

@ -33,10 +33,6 @@ extern char __start__;
#include <stdlib.h>
#ifdef __APPLE__
#include <pthread.h>
#endif
using namespace Arm64Gen;
extern "C" void ARM_Ret();
@ -58,9 +54,14 @@ namespace ARMJIT
template <>
const ARM64Reg RegisterCache<Compiler, ARM64Reg>::NativeRegAllocOrder[] =
{W19, W20, W21, W22, W23, W24, W25, W26};
{
W19, W20, W21, W22, W23, W24, W25,
W8, W9, W10, W11, W12, W13, W14, W15
};
template <>
const int RegisterCache<Compiler, ARM64Reg>::NativeRegsAvailable = 8;
const int RegisterCache<Compiler, ARM64Reg>::NativeRegsAvailable = 15;
const BitSet32 CallerSavedPushRegs({W8, W9, W10, W11, W12, W13, W14, W15});
const int JitMemSize = 16 * 1024 * 1024;
#ifndef __SWITCH__
@ -164,44 +165,55 @@ void Compiler::A_Comp_MSR()
MOV(W2, RCPSR);
MOV(X0, RCPU);
PushRegs(true);
QuickCallFunction(X3, (void*)&UpdateModeTrampoline);
PopRegs(true);
PushRegs(true, true);
QuickCallFunction(X3, UpdateModeTrampoline);
PopRegs(true, true);
}
}
}
void Compiler::PushRegs(bool saveHiRegs)
void Compiler::PushRegs(bool saveHiRegs, bool saveRegsToBeChanged, bool allowUnload)
{
BitSet32 loadedRegs(RegCache.LoadedRegs);
if (saveHiRegs)
{
if (Thumb || CurInstr.Cond() == 0xE)
BitSet32 hiRegsLoaded(RegCache.LoadedRegs & 0x7F00);
for (int reg : hiRegsLoaded)
{
BitSet16 hiRegsLoaded(RegCache.LoadedRegs & 0x7F00);
for (int reg : hiRegsLoaded)
if (Thumb || CurInstr.Cond() == 0xE)
RegCache.UnloadRegister(reg);
else
SaveReg(reg, RegCache.Mapping[reg]);
// prevent saving the register twice
loadedRegs[reg] = false;
}
else
}
for (int reg : loadedRegs)
{
if (CallerSavedPushRegs[RegCache.Mapping[reg]]
&& (saveRegsToBeChanged || !((1<<reg) & CurInstr.Info.DstRegs && !((1<<reg) & CurInstr.Info.SrcRegs))))
{
BitSet16 hiRegsDirty(RegCache.LoadedRegs & 0x7F00);
for (int reg : hiRegsDirty)
if ((Thumb || CurInstr.Cond() == 0xE) && !((1 << reg) & (CurInstr.Info.DstRegs|CurInstr.Info.SrcRegs)) && allowUnload)
RegCache.UnloadRegister(reg);
else
SaveReg(reg, RegCache.Mapping[reg]);
}
}
}
void Compiler::PopRegs(bool saveHiRegs)
void Compiler::PopRegs(bool saveHiRegs, bool saveRegsToBeChanged)
{
if (saveHiRegs)
BitSet32 loadedRegs(RegCache.LoadedRegs);
for (int reg : loadedRegs)
{
if (!Thumb && CurInstr.Cond() != 0xE)
if ((saveHiRegs && reg >= 8 && reg < 15)
|| (CallerSavedPushRegs[RegCache.Mapping[reg]]
&& (saveRegsToBeChanged || !((1<<reg) & CurInstr.Info.DstRegs && !((1<<reg) & CurInstr.Info.SrcRegs)))))
{
BitSet16 hiRegsLoaded(RegCache.LoadedRegs & 0x7F00);
for (int reg : hiRegsLoaded)
LoadReg(reg, RegCache.Mapping[reg]);
LoadReg(reg, RegCache.Mapping[reg]);
}
}
}
@ -250,7 +262,7 @@ Compiler::Compiler()
u64 alignedSize = (((u64)JitMem + sizeof(JitMem)) & ~(pageSize - 1)) - (u64)pageAligned;
#ifdef __APPLE__
pageAligned = (u8*)mmap(NULL, 1024*1024*16, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS | MAP_JIT,-1, 0);
pthread_jit_write_protect_np(false);
JitEnableWrite();
#else
mprotect(pageAligned, alignedSize, PROT_EXEC | PROT_READ | PROT_WRITE);
#endif
@ -267,6 +279,7 @@ Compiler::Compiler()
}
/*
W4 - whether the register was written to
W5 - mode
W1 - reg num
W3 - in/out value of reg
@ -358,7 +371,7 @@ Compiler::Compiler()
{
for (int reg = 0; reg < 32; reg++)
{
if (!(reg == W4 || (reg >= W19 && reg <= W26)))
if (!(reg == W4 || (reg >= W8 && reg <= W15) || (reg >= W19 && reg <= W25)))
continue;
ARM64Reg rdMapped = (ARM64Reg)reg;
PatchedStoreFuncs[consoleType][num][size][reg] = GetRXPtr();
@ -371,7 +384,7 @@ Compiler::Compiler()
{
MOV(W1, rdMapped);
}
ABI_PushRegisters({30});
ABI_PushRegisters(BitSet32({30}) | CallerSavedPushRegs);
if (consoleType == 0)
{
switch ((8 << size) | num)
@ -397,7 +410,7 @@ Compiler::Compiler()
}
}
ABI_PopRegisters({30});
ABI_PopRegisters(BitSet32({30}) | CallerSavedPushRegs);
RET();
for (int signextend = 0; signextend < 2; signextend++)
@ -405,7 +418,7 @@ Compiler::Compiler()
PatchedLoadFuncs[consoleType][num][size][signextend][reg] = GetRXPtr();
if (num == 0)
MOV(X1, RCPU);
ABI_PushRegisters({30});
ABI_PushRegisters(BitSet32({30}) | CallerSavedPushRegs);
if (consoleType == 0)
{
switch ((8 << size) | num)
@ -430,7 +443,7 @@ Compiler::Compiler()
case 9: QuickCallFunction(X3, SlowRead7<u8, 1>); break;
}
}
ABI_PopRegisters({30});
ABI_PopRegisters(BitSet32({30}) | CallerSavedPushRegs);
if (size == 32)
MOV(rdMapped, W0);
else if (signextend)
@ -668,12 +681,13 @@ void Compiler::Comp_BranchSpecialBehaviour(bool taken)
{
RegCache.PrepareExit();
ADD(RCycles, RCycles, ConstantCycles);
if (ConstantCycles)
ADD(RCycles, RCycles, ConstantCycles);
QuickTailCall(X0, ARM_Ret);
}
}
JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount)
JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount, bool hasMemInstr)
{
if (JitMemMainSize - GetCodeOffset() < 1024 * 16)
{
@ -695,6 +709,9 @@ JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[]
RegCache = RegisterCache<Compiler, ARM64Reg>(this, instrs, instrsCount, true);
CPSRDirty = false;
if (hasMemInstr)
MOVP2R(RMemBase, Num == 0 ? ARMJIT_Memory::FastMem9Start : ARMJIT_Memory::FastMem7Start);
for (int i = 0; i < instrsCount; i++)
{
CurInstr = instrs[i];
@ -816,7 +833,8 @@ JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[]
RegCache.Flush();
ADD(RCycles, RCycles, ConstantCycles);
if (ConstantCycles)
ADD(RCycles, RCycles, ConstantCycles);
QuickTailCall(X0, ARM_Ret);
FlushIcache();

View File

@ -32,6 +32,7 @@
namespace ARMJIT
{
const Arm64Gen::ARM64Reg RMemBase = Arm64Gen::X26;
const Arm64Gen::ARM64Reg RCPSR = Arm64Gen::W27;
const Arm64Gen::ARM64Reg RCycles = Arm64Gen::W28;
const Arm64Gen::ARM64Reg RCPU = Arm64Gen::X29;
@ -99,8 +100,8 @@ public:
Compiler();
~Compiler();
void PushRegs(bool saveHiRegs);
void PopRegs(bool saveHiRegs);
void PushRegs(bool saveHiRegs, bool saveRegsToBeChanged, bool allowUnload = true);
void PopRegs(bool saveHiRegs, bool saveRegsToBeChanged);
Arm64Gen::ARM64Reg MapReg(int reg)
{
@ -108,7 +109,7 @@ public:
return RegCache.Mapping[reg];
}
JitBlockEntry CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount);
JitBlockEntry CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount, bool hasMemInstr);
bool CanCompile(bool thumb, u16 kind);
@ -184,7 +185,7 @@ public:
void T_Comp_BL_LONG_2();
void T_Comp_BL_Merged();
s32 Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode);
s32 Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode, bool skipLoadingRn);
void Comp_Mul_Mla(bool S, bool mla, Arm64Gen::ARM64Reg rd, Arm64Gen::ARM64Reg rm, Arm64Gen::ARM64Reg rs, Arm64Gen::ARM64Reg rn);

View File

@ -194,13 +194,11 @@ void Compiler::Comp_MemAccess(int rd, int rn, Op2 offset, int size, int flags)
ptrdiff_t memopStart = GetCodeOffset();
LoadStorePatch patch;
assert((rdMapped >= W19 && rdMapped <= W26) || rdMapped == W4);
assert((rdMapped >= W8 && rdMapped <= W15) || (rdMapped >= W19 && rdMapped <= W25) || rdMapped == W4);
patch.PatchFunc = flags & memop_Store
? PatchedStoreFuncs[NDS::ConsoleType][Num][__builtin_ctz(size) - 3][rdMapped]
: PatchedLoadFuncs[NDS::ConsoleType][Num][__builtin_ctz(size) - 3][!!(flags & memop_SignExtend)][rdMapped];
MOVP2R(X7, Num == 0 ? ARMJIT_Memory::FastMem9Start : ARMJIT_Memory::FastMem7Start);
// take a chance at fastmem
if (size > 8)
ANDI2R(W1, W0, addressMask);
@ -208,11 +206,11 @@ void Compiler::Comp_MemAccess(int rd, int rn, Op2 offset, int size, int flags)
ptrdiff_t loadStorePosition = GetCodeOffset();
if (flags & memop_Store)
{
STRGeneric(size, rdMapped, size > 8 ? X1 : X0, X7);
STRGeneric(size, rdMapped, size > 8 ? X1 : X0, RMemBase);
}
else
{
LDRGeneric(size, flags & memop_SignExtend, rdMapped, size > 8 ? X1 : X0, X7);
LDRGeneric(size, flags & memop_SignExtend, rdMapped, size > 8 ? X1 : X0, RMemBase);
if (size == 32 && !addrIsStatic)
{
UBFIZ(W0, W0, 3, 2);
@ -230,12 +228,16 @@ void Compiler::Comp_MemAccess(int rd, int rn, Op2 offset, int size, int flags)
if (addrIsStatic)
func = ARMJIT_Memory::GetFuncForAddr(CurCPU, staticAddress, flags & memop_Store, size);
PushRegs(false, false);
if (func)
{
if (flags & memop_Store)
MOV(W1, rdMapped);
QuickCallFunction(X2, (void (*)())func);
PopRegs(false, false);
if (!(flags & memop_Store))
{
if (size == 32)
@ -314,6 +316,8 @@ void Compiler::Comp_MemAccess(int rd, int rn, Op2 offset, int size, int flags)
}
}
PopRegs(false, false);
if (!(flags & memop_Store))
{
if (size == 32)
@ -461,7 +465,7 @@ void Compiler::T_Comp_MemSPRel()
Comp_MemAccess(CurInstr.T_Reg(8), 13, Op2(offset), 32, load ? 0 : memop_Store);
}
s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode)
s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode, bool skipLoadingRn)
{
IrregularCycles = true;
@ -470,7 +474,8 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
if (regsCount == 0)
return 0; // actually not the right behaviour TODO: fix me
if (regsCount == 1 && !usermode && RegCache.LoadedRegs & (1 << *regs.begin()))
int firstReg = *regs.begin();
if (regsCount == 1 && !usermode && RegCache.LoadedRegs & (1 << firstReg) && !(firstReg == rn && skipLoadingRn))
{
int flags = 0;
if (store)
@ -479,7 +484,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
flags |= memop_SubtractOffset;
Op2 offset = preinc ? Op2(4) : Op2(0);
Comp_MemAccess(*regs.begin(), rn, offset, 32, flags);
Comp_MemAccess(firstReg, rn, offset, 32, flags);
return decrement ? -4 : 4;
}
@ -515,8 +520,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
ptrdiff_t fastPathStart = GetCodeOffset();
ptrdiff_t loadStoreOffsets[8];
MOVP2R(X1, Num == 0 ? ARMJIT_Memory::FastMem9Start : ARMJIT_Memory::FastMem7Start);
ADD(X1, X1, X0);
ADD(X1, RMemBase, X0);
u32 offset = 0;
BitSet16::Iterator it = regs.begin();
@ -536,12 +540,16 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
loadStoreOffsets[i++] = GetCodeOffset();
if (store)
{
STR(INDEX_UNSIGNED, first, X1, offset);
else
}
else if (!(reg == rn && skipLoadingRn))
{
LDR(INDEX_UNSIGNED, first, X1, offset);
if (!(RegCache.LoadedRegs & (1 << reg)) && !store)
SaveReg(reg, first);
if (!(RegCache.LoadedRegs & (1 << reg)))
SaveReg(reg, first);
}
offset += 4;
}
@ -555,13 +563,23 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
ARM64Reg first = W3, second = W4;
if (RegCache.LoadedRegs & (1 << reg))
first = MapReg(reg);
{
if (!(reg == rn && skipLoadingRn))
first = MapReg(reg);
}
else if (store)
{
LoadReg(reg, first);
}
if (RegCache.LoadedRegs & (1 << nextReg))
second = MapReg(nextReg);
{
if (!(nextReg == rn && skipLoadingRn))
second = MapReg(nextReg);
}
else if (store)
{
LoadReg(nextReg, second);
}
loadStoreOffsets[i++] = GetCodeOffset();
if (store)
@ -655,6 +673,8 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
}
}
PushRegs(false, false, !compileFastPath);
ADD(X1, SP, 0);
MOVI2R(W2, regsCount);
@ -680,6 +700,8 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
}
}
PopRegs(false, false);
if (!store)
{
if (usermode && !regs[15] && (regs & BitSet16(0x7f00)))
@ -698,20 +720,23 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
LDR(INDEX_UNSIGNED, W3, SP, i * 8);
MOVI2R(W1, reg - 8);
BL(WriteBanked);
FixupBranch alreadyWritten = CBNZ(W4);
if (RegCache.LoadedRegs & (1 << reg))
MOV(MapReg(reg), W3);
else
SaveReg(reg, W3);
SetJumpTarget(alreadyWritten);
if (!(reg == rn && skipLoadingRn))
{
FixupBranch alreadyWritten = CBNZ(W4);
if (RegCache.LoadedRegs & (1 << reg))
MOV(MapReg(reg), W3);
else
SaveReg(reg, W3);
SetJumpTarget(alreadyWritten);
}
}
else if (!usermode && nextReg != regs.end())
{
ARM64Reg first = W3, second = W4;
if (RegCache.LoadedRegs & (1 << reg))
if (RegCache.LoadedRegs & (1 << reg) && !(reg == rn && skipLoadingRn))
first = MapReg(reg);
if (RegCache.LoadedRegs & (1 << *nextReg))
if (RegCache.LoadedRegs & (1 << *nextReg) && !(*nextReg == rn && skipLoadingRn))
second = MapReg(*nextReg);
LDP(INDEX_SIGNED, EncodeRegTo64(first), EncodeRegTo64(second), SP, i * 8);
@ -726,8 +751,11 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
}
else if (RegCache.LoadedRegs & (1 << reg))
{
ARM64Reg mapped = MapReg(reg);
LDR(INDEX_UNSIGNED, mapped, SP, i * 8);
if (!(reg == rn && skipLoadingRn))
{
ARM64Reg mapped = MapReg(reg);
LDR(INDEX_UNSIGNED, mapped, SP, i * 8);
}
}
else
{
@ -771,13 +799,13 @@ void Compiler::A_Comp_LDM_STM()
ARM64Reg rn = MapReg(CurInstr.A_Reg(16));
s32 offset = Comp_MemAccessBlock(CurInstr.A_Reg(16), regs, !load, pre, !add, usermode);
if (load && writeback && regs[CurInstr.A_Reg(16)])
writeback = Num == 0
? (!(regs & ~BitSet16(1 << CurInstr.A_Reg(16)))) || (regs & ~BitSet16((2 << CurInstr.A_Reg(16)) - 1))
: false;
if (writeback)
&& (!(regs & ~BitSet16(1 << CurInstr.A_Reg(16)))) || (regs & ~BitSet16((2 << CurInstr.A_Reg(16)) - 1));
s32 offset = Comp_MemAccessBlock(CurInstr.A_Reg(16), regs, !load, pre, !add, usermode, load && writeback);
if (writeback && offset)
{
if (offset > 0)
ADD(rn, rn, offset);
@ -799,12 +827,15 @@ void Compiler::T_Comp_PUSH_POP()
}
ARM64Reg sp = MapReg(13);
s32 offset = Comp_MemAccessBlock(13, regs, !load, !load, !load, false);
s32 offset = Comp_MemAccessBlock(13, regs, !load, !load, !load, false, false);
if (offset > 0)
if (offset)
{
if (offset > 0)
ADD(sp, sp, offset);
else
SUB(sp, sp, -offset);
}
}
void Compiler::T_Comp_LDMIA_STMIA()
@ -813,10 +844,12 @@ void Compiler::T_Comp_LDMIA_STMIA()
ARM64Reg rb = MapReg(CurInstr.T_Reg(8));
bool load = CurInstr.Instr & (1 << 11);
u32 regsCount = regs.Count();
s32 offset = Comp_MemAccessBlock(CurInstr.T_Reg(8), regs, !load, false, false, false);
if (!load || !regs[CurInstr.T_Reg(8)])
bool writeback = !load || !regs[CurInstr.T_Reg(8)];
s32 offset = Comp_MemAccessBlock(CurInstr.T_Reg(8), regs, !load, false, false, false, load && writeback);
if (writeback && offset)
{
if (offset > 0)
ADD(rb, rb, offset);

View File

@ -584,7 +584,7 @@ bool MapAtAddress(u32 addr)
return false;
u8* states = num == 0 ? MappingStatus9 : MappingStatus7;
printf("mapping mirror %x, %x %x %d %d\n", mirrorStart, mirrorSize, memoryOffset, region, num);
//printf("mapping mirror %x, %x %x %d %d\n", mirrorStart, mirrorSize, memoryOffset, region, num);
bool isExecutable = ARMJIT::CodeMemRegions[region];
u32 dtcmStart = NDS::ARM9->DTCMBase;
@ -650,7 +650,7 @@ bool MapAtAddress(u32 addr)
#if defined(__SWITCH__)
if (!hasCode)
{
printf("trying to map %x (size: %x) from %x\n", mirrorStart + sectionOffset, sectionSize, sectionOffset + memoryOffset + OffsetsPerRegion[region]);
//printf("trying to map %x (size: %x) from %x\n", mirrorStart + sectionOffset, sectionSize, sectionOffset + memoryOffset + OffsetsPerRegion[region]);
bool succeded = MapIntoRange(mirrorStart + sectionOffset, num, sectionOffset + memoryOffset + OffsetsPerRegion[region], sectionSize);
assert(succeded);
}

View File

@ -165,7 +165,7 @@ void Compiler::Comp_JumpTo(Gen::X64Reg addr, bool restoreCPSR)
bool cpsrDirty = CPSRDirty;
SaveCPSR();
PushRegs(restoreCPSR);
PushRegs(restoreCPSR, true);
MOV(64, R(ABI_PARAM1), R(RCPU));
MOV(32, R(ABI_PARAM2), R(addr));
@ -178,7 +178,7 @@ void Compiler::Comp_JumpTo(Gen::X64Reg addr, bool restoreCPSR)
else
CALL((void*)&ARMv4JumpToTrampoline);
PopRegs(restoreCPSR);
PopRegs(restoreCPSR, true);
LoadCPSR();
// in case this instruction is skipped

View File

@ -22,6 +22,7 @@
#include "../Config.h"
#include <assert.h>
#include <stdarg.h>
#include "../dolphin/CommonFuncs.h"
@ -64,7 +65,7 @@ const BitSet32 CallerSavedPushRegs({R10, R11});
const BitSet32 CallerSavedPushRegs({R9, R10, R11});
#endif
void Compiler::PushRegs(bool saveHiRegs)
void Compiler::PushRegs(bool saveHiRegs, bool saveRegsToBeChanged, bool allowUnload)
{
BitSet32 loadedRegs(RegCache.LoadedRegs);
@ -83,17 +84,26 @@ void Compiler::PushRegs(bool saveHiRegs)
}
for (int reg : loadedRegs)
if (BitSet32(1 << RegCache.Mapping[reg]) & ABI_ALL_CALLER_SAVED)
SaveReg(reg, RegCache.Mapping[reg]);
{
if (CallerSavedPushRegs[RegCache.Mapping[reg]]
&& (saveRegsToBeChanged || !((1<<reg) & CurInstr.Info.DstRegs && !((1<<reg) & CurInstr.Info.SrcRegs))))
{
if ((Thumb || CurInstr.Cond() == 0xE) && !((1 << reg) & (CurInstr.Info.DstRegs|CurInstr.Info.SrcRegs)) && allowUnload)
RegCache.UnloadRegister(reg);
else
SaveReg(reg, RegCache.Mapping[reg]);
}
}
}
void Compiler::PopRegs(bool saveHiRegs)
void Compiler::PopRegs(bool saveHiRegs, bool saveRegsToBeChanged)
{
BitSet32 loadedRegs(RegCache.LoadedRegs);
for (int reg : loadedRegs)
{
if ((saveHiRegs && reg >= 8 && reg < 15)
|| BitSet32(1 << RegCache.Mapping[reg]) & ABI_ALL_CALLER_SAVED)
|| (CallerSavedPushRegs[RegCache.Mapping[reg]]
&& (saveRegsToBeChanged || !((1<<reg) & CurInstr.Info.DstRegs && !((1<<reg) & CurInstr.Info.SrcRegs)))))
{
LoadReg(reg, RegCache.Mapping[reg]);
}
@ -205,14 +215,14 @@ void Compiler::A_Comp_MSR()
AND(32, R(RSCRATCH2), val);
OR(32, R(RCPSR), R(RSCRATCH2));
PushRegs(true);
PushRegs(true, true);
MOV(32, R(ABI_PARAM3), R(RCPSR));
MOV(32, R(ABI_PARAM2), R(RSCRATCH3));
MOV(64, R(ABI_PARAM1), R(RCPU));
CALL((void*)&UpdateModeTrampoline);
PopRegs(true);
PopRegs(true, true);
}
}
}
@ -289,6 +299,10 @@ Compiler::Compiler()
SetJumpTarget(und);
MOV(32, R(RSCRATCH3), MComplex(RCPU, RSCRATCH2, SCALE_4, offsetof(ARM, R_UND)));
RET();
#ifdef JIT_PROFILING_ENABLED
CreateMethod("ReadBanked", ReadBanked);
#endif
}
{
// RSCRATCH mode
@ -332,6 +346,10 @@ Compiler::Compiler()
MOV(32, MComplex(RCPU, RSCRATCH2, SCALE_4, offsetof(ARM, R_UND)), R(RSCRATCH3));
CLC();
RET();
#ifdef JIT_PROFILING_ENABLED
CreateMethod("WriteBanked", WriteBanked);
#endif
}
for (int consoleType = 0; consoleType < 2; consoleType++)
@ -392,6 +410,10 @@ Compiler::Compiler()
ABI_PopRegistersAndAdjustStack(CallerSavedPushRegs, 8);
RET();
#ifdef JIT_PROFILING_ENABLED
CreateMethod("FastMemStorePatch%d_%d_%d", PatchedStoreFuncs[consoleType][num][size][reg], num, size, reg);
#endif
for (int signextend = 0; signextend < 2; signextend++)
{
PatchedLoadFuncs[consoleType][num][size][signextend][reg] = GetWritableCodePtr();
@ -430,6 +452,10 @@ Compiler::Compiler()
else
MOVZX(32, 8 << size, rdMapped, R(RSCRATCH));
RET();
#ifdef JIT_PROFILING_ENABLED
CreateMethod("FastMemLoadPatch%d_%d_%d_%d", PatchedLoadFuncs[consoleType][num][size][signextend][reg], num, size, reg, signextend);
#endif
}
}
}
@ -654,12 +680,35 @@ void Compiler::Comp_SpecialBranchBehaviour(bool taken)
{
RegCache.PrepareExit();
ADD(32, MDisp(RCPU, offsetof(ARM, Cycles)), Imm32(ConstantCycles));
if (ConstantCycles)
ADD(32, MDisp(RCPU, offsetof(ARM, Cycles)), Imm32(ConstantCycles));
JMP((u8*)&ARM_Ret, true);
}
}
JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount)
#ifdef JIT_PROFILING_ENABLED
void Compiler::CreateMethod(const char* namefmt, void* start, ...)
{
if (iJIT_IsProfilingActive())
{
va_list args;
va_start(args, start);
char name[64];
vsprintf(name, namefmt, args);
va_end(args);
iJIT_Method_Load method = {0};
method.method_id = iJIT_GetNewMethodID();
method.method_name = name;
method.method_load_address = start;
method.method_size = GetWritableCodePtr() - (u8*)start;
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, (void*)&method);
}
}
#endif
JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount, bool hasMemoryInstr)
{
if (NearSize - (GetCodePtr() - NearStart) < 1024 * 32) // guess...
{
@ -793,9 +842,14 @@ JitBlockEntry Compiler::CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[]
RegCache.Flush();
ADD(32, MDisp(RCPU, offsetof(ARM, Cycles)), Imm32(ConstantCycles));
if (ConstantCycles)
ADD(32, MDisp(RCPU, offsetof(ARM, Cycles)), Imm32(ConstantCycles));
JMP((u8*)ARM_Ret, true);
#ifdef JIT_PROFILING_ENABLED
CreateMethod("JIT_Block_%d_%d_%08X", (void*)res, Num, Thumb, instrs[0].Addr);
#endif
/*FILE* codeout = fopen("codeout", "a");
fprintf(codeout, "beginning block argargarg__ %x!!!", instrs[0].Addr);
fwrite((u8*)res, GetWritableCodePtr() - (u8*)res, 1, codeout);

View File

@ -25,6 +25,10 @@
#include "../ARMJIT_Internal.h"
#include "../ARMJIT_RegisterCache.h"
#ifdef JIT_PROFILING_ENABLED
#include <jitprofiling.h>
#endif
#include <unordered_map>
namespace ARMJIT
@ -79,7 +83,7 @@ public:
void Reset();
JitBlockEntry CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount);
JitBlockEntry CompileBlock(ARM* cpu, bool thumb, FetchedInstr instrs[], int instrsCount, bool hasMemoryInstr);
void LoadReg(int reg, Gen::X64Reg nativeReg);
void SaveReg(int reg, Gen::X64Reg nativeReg);
@ -163,7 +167,7 @@ public:
memop_SubtractOffset = 1 << 4
};
void Comp_MemAccess(int rd, int rn, const Op2& op2, int size, int flags);
s32 Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode);
s32 Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode, bool skipLoadingRn);
bool Comp_MemLoadLiteral(int size, bool signExtend, int rd, u32 addr);
void Comp_ArithTriOp(void (Compiler::*op)(int, const Gen::OpArg&, const Gen::OpArg&),
@ -192,8 +196,8 @@ public:
Gen::FixupBranch CheckCondition(u32 cond);
void PushRegs(bool saveHiRegs);
void PopRegs(bool saveHiRegs);
void PushRegs(bool saveHiRegs, bool saveRegsToBeChanged, bool allowUnload = true);
void PopRegs(bool saveHiRegs, bool saveRegsToBeChanged);
Gen::OpArg MapReg(int reg)
{
@ -230,6 +234,10 @@ public:
u8* RewriteMemAccess(u8* pc);
#ifdef JIT_PROFILING_ENABLED
void CreateMethod(const char* namefmt, void* start, ...);
#endif
u8* FarCode;
u8* NearCode;
u32 FarSize;

View File

@ -266,7 +266,7 @@ void Compiler::Comp_MemAccess(int rd, int rn, const Op2& op2, int size, int flag
}
else
{
PushRegs(false);
PushRegs(false, false);
void* func = NULL;
if (addrIsStatic)
@ -283,7 +283,7 @@ void Compiler::Comp_MemAccess(int rd, int rn, const Op2& op2, int size, int flag
ABI_CallFunction((void (*)())func);
PopRegs(false);
PopRegs(false, false);
if (!(flags & memop_Store))
{
@ -370,7 +370,7 @@ void Compiler::Comp_MemAccess(int rd, int rn, const Op2& op2, int size, int flag
}
}
PopRegs(false);
PopRegs(false, false);
if (!(flags & memop_Store))
{
@ -399,14 +399,15 @@ void Compiler::Comp_MemAccess(int rd, int rn, const Op2& op2, int size, int flag
}
}
s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode)
s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc, bool decrement, bool usermode, bool skipLoadingRn)
{
int regsCount = regs.Count();
if (regsCount == 0)
return 0; // actually not the right behaviour TODO: fix me
if (regsCount == 1 && !usermode && RegCache.LoadedRegs & (1 << *regs.begin()))
int firstReg = *regs.begin();
if (regsCount == 1 && !usermode && RegCache.LoadedRegs & (1 << firstReg) && !(firstReg == rn && skipLoadingRn))
{
int flags = 0;
if (store)
@ -415,7 +416,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
flags |= memop_SubtractOffset;
Op2 offset = preinc ? Op2(4) : Op2(0);
Comp_MemAccess(*regs.begin(), rn, offset, 32, flags);
Comp_MemAccess(firstReg, rn, offset, 32, flags);
return decrement ? -4 : 4;
}
@ -482,7 +483,10 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
{
if (RegCache.LoadedRegs & (1 << reg))
{
MOV(32, MapReg(reg), mem);
if (!(reg == rn && skipLoadingRn))
MOV(32, MapReg(reg), mem);
else
MOV(32, R(RSCRATCH), mem); // just touch the memory
}
else
{
@ -508,7 +512,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
if (!store)
{
PushRegs(false);
PushRegs(false, false, !compileFastPath);
MOV(32, R(ABI_PARAM1), R(RSCRATCH4));
MOV(32, R(ABI_PARAM3), Imm32(regsCount));
@ -529,7 +533,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
case 3: CALL((void*)&SlowBlockTransfer7<false, 1>); break;
}
PopRegs(false);
PopRegs(false, false);
if (allocOffset)
ADD(64, R(RSP), Imm8(allocOffset));
@ -548,12 +552,15 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
MOV(32, R(RSCRATCH2), Imm32(reg - 8));
POP(RSCRATCH3);
CALL(WriteBanked);
FixupBranch sucessfulWritten = J_CC(CC_NC);
if (RegCache.LoadedRegs & (1 << reg))
MOV(32, R(RegCache.Mapping[reg]), R(RSCRATCH3));
else
SaveReg(reg, RSCRATCH3);
SetJumpTarget(sucessfulWritten);
if (!(reg == rn && skipLoadingRn))
{
FixupBranch sucessfulWritten = J_CC(CC_NC);
if (RegCache.LoadedRegs & (1 << reg))
MOV(32, R(RegCache.Mapping[reg]), R(RSCRATCH3));
else
SaveReg(reg, RSCRATCH3);
SetJumpTarget(sucessfulWritten);
}
}
else if (!(RegCache.LoadedRegs & (1 << reg)))
{
@ -562,6 +569,10 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
POP(RSCRATCH);
SaveReg(reg, RSCRATCH);
}
else if (reg == rn && skipLoadingRn)
{
ADD(64, R(RSP), Imm8(8));
}
else
{
POP(MapReg(reg).GetSimpleReg());
@ -606,7 +617,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
if (allocOffset)
SUB(64, R(RSP), Imm8(allocOffset));
PushRegs(false);
PushRegs(false, false, !compileFastPath);
MOV(32, R(ABI_PARAM1), R(RSCRATCH4));
if (allocOffset)
@ -628,7 +639,7 @@ s32 Compiler::Comp_MemAccessBlock(int rn, BitSet16 regs, bool store, bool preinc
ADD(64, R(RSP), stackAlloc <= INT8_MAX ? Imm8(stackAlloc) : Imm32(stackAlloc));
PopRegs(false);
PopRegs(false, false);
}
if (compileFastPath)
@ -748,14 +759,14 @@ void Compiler::A_Comp_LDM_STM()
OpArg rn = MapReg(CurInstr.A_Reg(16));
s32 offset = Comp_MemAccessBlock(CurInstr.A_Reg(16), regs, !load, pre, !add, usermode);
if (load && writeback && regs[CurInstr.A_Reg(16)])
writeback = Num == 0
? (!(regs & ~BitSet16(1 << CurInstr.A_Reg(16)))) || (regs & ~BitSet16((2 << CurInstr.A_Reg(16)) - 1))
: false;
if (writeback)
ADD(32, rn, offset >= INT8_MIN && offset < INT8_MAX ? Imm8(offset) : Imm32(offset));
&& (!(regs & ~BitSet16(1 << CurInstr.A_Reg(16)))) || (regs & ~BitSet16((2 << CurInstr.A_Reg(16)) - 1));
s32 offset = Comp_MemAccessBlock(CurInstr.A_Reg(16), regs, !load, pre, !add, usermode, load && writeback);
if (writeback && offset)
ADD(32, rn, Imm32(offset));
}
void Compiler::T_Comp_MemImm()
@ -825,9 +836,10 @@ void Compiler::T_Comp_PUSH_POP()
}
OpArg sp = MapReg(13);
s32 offset = Comp_MemAccessBlock(13, regs, !load, !load, !load, false);
s32 offset = Comp_MemAccessBlock(13, regs, !load, !load, !load, false, false);
ADD(32, sp, Imm8(offset)); // offset will be always be in range since PUSH accesses 9 regs max
if (offset)
ADD(32, sp, Imm8(offset)); // offset will be always be in range since PUSH accesses 9 regs max
}
void Compiler::T_Comp_LDMIA_STMIA()
@ -836,9 +848,11 @@ void Compiler::T_Comp_LDMIA_STMIA()
OpArg rb = MapReg(CurInstr.T_Reg(8));
bool load = CurInstr.Instr & (1 << 11);
s32 offset = Comp_MemAccessBlock(CurInstr.T_Reg(8), regs, !load, false, false, false);
bool writeback = !load || !regs[CurInstr.T_Reg(8)];
if (!load || !regs[CurInstr.T_Reg(8)])
s32 offset = Comp_MemAccessBlock(CurInstr.T_Reg(8), regs, !load, false, false, false, load && writeback);
if (writeback && offset)
ADD(32, rb, Imm8(offset));
}

View File

@ -526,7 +526,7 @@ Info Decode(bool thumb, u32 num, u32 instr)
if (data & A_LoadMem)
{
if (res.SrcRegs == (1 << 15))
res.SpecialKind = special_LoadLiteral;
res.SpecialKind = special_LoadLiteral;
else
res.SpecialKind = special_LoadMem;
}
@ -536,6 +536,11 @@ Info Decode(bool thumb, u32 num, u32 instr)
u16 set = (instr & 0xFFFF);
res.NotStrictlyNeeded |= set & ~(res.SrcRegs|res.DstRegs|(1<<15));
res.DstRegs |= set;
// when the instruction is executed not in usermode a banked register in memory will be written to
// but the unbanked register will still be allocated, so it is expected to carry the proper value
// thus it is a source register
if (instr & (1<<22))
res.SrcRegs |= set & 0x7F00;
}
if (res.Kind == ak_STM)
{

View File

@ -19,7 +19,9 @@ add_library(core STATIC
DSi.cpp
DSi_AES.cpp
DSi_Camera.cpp
DSi_DSP.cpp
DSi_I2C.cpp
DSi_NAND.cpp
DSi_NDMA.cpp
DSi_NWifi.cpp
DSi_SD.cpp
@ -46,6 +48,13 @@ add_library(core STATIC
Wifi.cpp
WifiAP.cpp
fatfs/diskio.c
fatfs/ff.c
fatfs/ffsystem.c
fatfs/ffunicode.c
fatfs/ffconf.h
sha1/sha1.c
tiny-AES-c/aes.c
xxhash/xxhash.c
)
@ -101,6 +110,10 @@ if (ENABLE_JIT)
endif()
endif()
add_subdirectory(teakra EXCLUDE_FROM_ALL)
target_link_libraries(core teakra)
if (ENABLE_OGLRENDERER)
find_package(PkgConfig REQUIRED)
pkg_check_modules(EPOXY REQUIRED epoxy)
@ -120,3 +133,7 @@ else()
target_link_libraries(core rt)
endif()
endif()
if (ENABLE_JIT_PROFILING)
target_link_libraries(core jitprofiling)
endif()

View File

@ -42,6 +42,7 @@ int DSiSDEnable;
char DSiSDPath[1024];
int RandomizeMAC;
int AudioBitrate;
#ifdef JIT_ENABLED
int JIT_Enable = false;
@ -67,6 +68,7 @@ ConfigEntry ConfigFile[] =
{"DSiSDPath", 1, DSiSDPath, 0, "", 1023},
{"RandomizeMAC", 0, &RandomizeMAC, 0, NULL, 0},
{"AudioBitrate", 0, &AudioBitrate, 0, NULL, 0},
#ifdef JIT_ENABLED
{"JIT_Enable", 0, &JIT_Enable, 0, NULL, 0},

View File

@ -55,6 +55,7 @@ extern int DSiSDEnable;
extern char DSiSDPath[1024];
extern int RandomizeMAC;
extern int AudioBitrate;
#ifdef JIT_ENABLED
extern int JIT_Enable;

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,9 @@ namespace DSi
{
extern u16 SCFG_BIOS;
extern u16 SCFG_Clock9;
extern u32 SCFG_EXT[2];
extern u8 ARM9iBIOS[0x10000];
extern u8 ARM7iBIOS[0x10000];
@ -57,6 +60,7 @@ bool Init();
void DeInit();
void Reset();
void SetupDirectBoot();
void SoftReset();
bool LoadBIOS();

View File

@ -31,6 +31,7 @@ namespace DSi_AES
u32 Cnt;
u32 BlkCnt;
u32 RemExtra;
u32 RemBlocks;
bool OutputFlush;
@ -106,6 +107,7 @@ void Reset()
Cnt = 0;
BlkCnt = 0;
RemExtra = 0;
RemBlocks = 0;
OutputFlush = false;
@ -154,6 +156,22 @@ void Reset()
}
void ProcessBlock_CCM_Extra()
{
u8 data[16];
u8 data_rev[16];
*(u32*)&data[0] = InputFIFO.Read();
*(u32*)&data[4] = InputFIFO.Read();
*(u32*)&data[8] = InputFIFO.Read();
*(u32*)&data[12] = InputFIFO.Read();
Swap16(data_rev, data);
for (int i = 0; i < 16; i++) CurMAC[i] ^= data_rev[i];
AES_ECB_encrypt(&Ctx, CurMAC);
}
void ProcessBlock_CCM_Decrypt()
{
u8 data[16];
@ -272,13 +290,14 @@ void WriteCnt(u32 val)
if (!(oldcnt & (1<<31)) && (val & (1<<31)))
{
// transfer start (checkme)
RemExtra = (AESMode < 2) ? (BlkCnt & 0xFFFF) : 0;
RemBlocks = BlkCnt >> 16;
OutputMACDue = false;
if (AESMode == 0 && (!(val & (1<<20)))) printf("AES: CCM-DECRYPT MAC FROM WRFIFO, TODO\n");
if (RemBlocks > 0)
if ((RemBlocks > 0) || (RemExtra > 0))
{
u8 key[16];
u8 iv[16];
@ -288,8 +307,6 @@ void WriteCnt(u32 val)
if (AESMode < 2)
{
if (BlkCnt & 0xFFFF) printf("AES: CCM EXTRA LEN TODO\n");
u32 maclen = (val >> 16) & 0x7;
if (maclen < 1) maclen = 1;
@ -325,8 +342,8 @@ void WriteCnt(u32 val)
}
}
//printf("AES CNT: %08X / mode=%d key=%d inDMA=%d outDMA=%d blocks=%d\n",
// val, AESMode, (val >> 26) & 0x3, InputDMASize, OutputDMASize, RemBlocks);
//printf("AES CNT: %08X / mode=%d key=%d inDMA=%d outDMA=%d blocks=%d (BLKCNT=%08X)\n",
// val, AESMode, (val >> 26) & 0x3, InputDMASize, OutputDMASize, RemBlocks, BlkCnt);
}
void WriteBlkCnt(u32 val)
@ -380,7 +397,7 @@ void WriteInputFIFO(u32 val)
void CheckInputDMA()
{
if (RemBlocks == 0) return;
if (RemBlocks == 0 && RemExtra == 0) return;
if (InputFIFO.Level() <= InputDMASize)
{
@ -402,22 +419,34 @@ void CheckOutputDMA()
void Update()
{
while (InputFIFO.Level() >= 4 && OutputFIFO.Level() <= 12 && RemBlocks > 0)
if (RemExtra > 0)
{
switch (AESMode)
while (InputFIFO.Level() >= 4 && RemExtra > 0)
{
case 0: ProcessBlock_CCM_Decrypt(); break;
case 1: ProcessBlock_CCM_Encrypt(); break;
case 2:
case 3: ProcessBlock_CTR(); break;
ProcessBlock_CCM_Extra();
RemExtra--;
}
}
RemBlocks--;
if (RemExtra == 0)
{
while (InputFIFO.Level() >= 4 && OutputFIFO.Level() <= 12 && RemBlocks > 0)
{
switch (AESMode)
{
case 0: ProcessBlock_CCM_Decrypt(); break;
case 1: ProcessBlock_CCM_Encrypt(); break;
case 2:
case 3: ProcessBlock_CTR(); break;
}
RemBlocks--;
}
}
CheckOutputDMA();
if (RemBlocks == 0)
if (RemBlocks == 0 && RemExtra == 0)
{
if (AESMode == 0)
{
@ -443,7 +472,16 @@ void Update()
AES_CTR_xcrypt_buffer(&Ctx, CurMAC, 16);
Swap16(OutputMAC, CurMAC);
OutputMACDue = true;
if (OutputFIFO.Level() <= 12)
{
OutputFIFO.Write(*(u32*)&OutputMAC[0]);
OutputFIFO.Write(*(u32*)&OutputMAC[4]);
OutputFIFO.Write(*(u32*)&OutputMAC[8]);
OutputFIFO.Write(*(u32*)&OutputMAC[12]);
}
else
OutputMACDue = true;
// CHECKME
Cnt &= ~(1<<21);
@ -485,16 +523,13 @@ void WriteMAC(u32 offset, u32 val, u32 mask)
//printf("AES: MAC: "); _printhex(MAC, 16);
}
void DeriveNormalKey(u32 slot)
void DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey)
{
const u8 key_const[16] = {0xFF, 0xFE, 0xFB, 0x4E, 0x29, 0x59, 0x02, 0x58, 0x2A, 0x68, 0x0F, 0x5F, 0x1A, 0x4F, 0x3E, 0x79};
u8 tmp[16];
//printf("slot%d keyX: ", slot); _printhex(KeyX[slot], 16);
//printf("slot%d keyY: ", slot); _printhex(KeyY[slot], 16);
for (int i = 0; i < 16; i++)
tmp[i] = KeyX[slot][i] ^ KeyY[slot][i];
tmp[i] = keyX[i] ^ keyY[i];
u32 carry = 0;
for (int i = 0; i < 16; i++)
@ -506,9 +541,7 @@ void DeriveNormalKey(u32 slot)
ROL16(tmp, 42);
//printf("derive normalkey %d\n", slot); _printhex(tmp, 16);
memcpy(KeyNormal[slot], tmp, 16);
memcpy(normalkey, tmp, 16);
}
void WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask)
@ -539,67 +572,8 @@ void WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask)
if (offset >= 0xC)
{
DeriveNormalKey(slot);
DeriveNormalKey(KeyX[slot], KeyY[slot], KeyNormal[slot]);
}
}
// utility
void GetModcryptKey(u8* romheader, u8* key)
{
if ((romheader[0x01C] & 0x04) || (romheader[0x1BF] & 0x80))
{
// dev key
memcpy(key, &romheader[0x000], 16);
return;
}
u8 oldkeys[16*3];
memcpy(&oldkeys[16*0], KeyX[0], 16);
memcpy(&oldkeys[16*1], KeyY[0], 16);
memcpy(&oldkeys[16*2], KeyNormal[0], 16);
KeyX[0][8] = romheader[0x00C];
KeyX[0][9] = romheader[0x00D];
KeyX[0][10] = romheader[0x00E];
KeyX[0][11] = romheader[0x00F];
KeyX[0][12] = romheader[0x00F];
KeyX[0][13] = romheader[0x00E];
KeyX[0][14] = romheader[0x00D];
KeyX[0][15] = romheader[0x00C];
memcpy(KeyY[0], &romheader[0x350], 16);
DeriveNormalKey(0);
memcpy(key, KeyNormal[0], 16);
memcpy(KeyX[0], &oldkeys[16*0], 16);
memcpy(KeyY[0], &oldkeys[16*1], 16);
memcpy(KeyNormal[0], &oldkeys[16*2], 16);
}
void ApplyModcrypt(u8* data, u32 len, u8* key, u8* iv)
{
u8 key_rev[16], iv_rev[16];
u8 data_rev[16];
u8 oldkeys[16*2];
memcpy(&oldkeys[16*0], Ctx.RoundKey, 16);
memcpy(&oldkeys[16*1], Ctx.Iv, 16);
Swap16(key_rev, key);
Swap16(iv_rev, iv);
AES_init_ctx_iv(&Ctx, key_rev, iv_rev);
for (u32 i = 0; i < len; i += 16)
{
Swap16(data_rev, &data[i]);
AES_CTR_xcrypt_buffer(&Ctx, data_rev, 16);
Swap16(&data[i], data_rev);
}
memcpy(Ctx.RoundKey, &oldkeys[16*0], 16);
memcpy(Ctx.Iv, &oldkeys[16*1], 16);
}
}

View File

@ -46,8 +46,8 @@ void WriteKeyNormal(u32 slot, u32 offset, u32 val, u32 mask);
void WriteKeyX(u32 slot, u32 offset, u32 val, u32 mask);
void WriteKeyY(u32 slot, u32 offset, u32 val, u32 mask);
void GetModcryptKey(u8* romheader, u8* key);
void ApplyModcrypt(u8* data, u32 len, u8* key, u8* iv);
void Swap16(u8* dst, u8* src);
void DeriveNormalKey(u8* keyX, u8* keyY, u8* normalkey);
}

592
src/DSi_DSP.cpp Normal file
View File

@ -0,0 +1,592 @@
/*
Copyright 2020 PoroCYon
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include "teakra/include/teakra/teakra.h"
#include "DSi.h"
#include "DSi_DSP.h"
#include "FIFO.h"
#include "NDS.h"
namespace DSi_DSP
{
Teakra::Teakra* TeakraCore;
bool SCFG_RST;
u16 DSP_PADR;
u16 DSP_PCFG;
u16 DSP_PSTS;
u16 DSP_PSEM;
u16 DSP_PMASK;
u16 DSP_PCLEAR;
u16 DSP_CMD[3];
u16 DSP_REP[3];
u64 DSPTimestamp;
FIFO<u16, 16> PDATAReadFifo/*, *PDATAWriteFifo*/;
int PDataDMALen = 0;
constexpr u32 DataMemoryOffset = 0x20000; // from Teakra memory_interface.h
// NOTE: ^ IS IN DSP WORDS, NOT IN BYTES!
u16 GetPSTS()
{
u16 r = DSP_PSTS & (1<<9); // this is the only sticky bit
//r &= ~((1<<2)|(1<<7)); // we support instant resets and wrfifo xfers
r |= (1<<8); // write fifo is always empty (inf. speed)
if ( PDATAReadFifo.IsFull ()) r |= 1<<5;
if (!PDATAReadFifo.IsEmpty()) r |=(1<<6)|(1<<0);
if (!TeakraCore->SendDataIsEmpty(0)) r |= 1<<13;
if (!TeakraCore->SendDataIsEmpty(1)) r |= 1<<14;
if (!TeakraCore->SendDataIsEmpty(2)) r |= 1<<15;
if ( TeakraCore->RecvDataIsReady(0)) r |= 1<<10;
if ( TeakraCore->RecvDataIsReady(1)) r |= 1<<11;
if ( TeakraCore->RecvDataIsReady(2)) r |= 1<<12;
return r;
}
void IrqRep0()
{
if (DSP_PCFG & (1<< 9)) NDS::SetIRQ(0, NDS::IRQ_DSi_DSP);
}
void IrqRep1()
{
if (DSP_PCFG & (1<<10)) NDS::SetIRQ(0, NDS::IRQ_DSi_DSP);
}
void IrqRep2()
{
if (DSP_PCFG & (1<<11)) NDS::SetIRQ(0, NDS::IRQ_DSi_DSP);
}
void IrqSem()
{
DSP_PSTS |= 1<<9;
// apparently these are always fired?
NDS::SetIRQ(0, NDS::IRQ_DSi_DSP);
}
void AudioCb(std::array<s16, 2> frame)
{
// TODO
}
bool Init()
{
TeakraCore = new Teakra::Teakra();
SCFG_RST = false;
if (!TeakraCore) return false;
TeakraCore->SetRecvDataHandler(0, IrqRep0);
TeakraCore->SetRecvDataHandler(1, IrqRep1);
TeakraCore->SetRecvDataHandler(2, IrqRep2);
TeakraCore->SetSemaphoreHandler(IrqSem);
// these happen instantaneously and without too much regard for bus aribtration
// rules, so, this might have to be changed later on
Teakra::AHBMCallback cb;
cb.read8 = DSi::ARM9Read8;
cb.write8 = DSi::ARM9Write8;
cb.read16 = DSi::ARM9Read16;
cb.write16 = DSi::ARM9Write16;
cb.read32 = DSi::ARM9Read32;
cb.write32 = DSi::ARM9Write32;
TeakraCore->SetAHBMCallback(cb);
TeakraCore->SetAudioCallback(AudioCb);
//PDATAReadFifo = new FIFO<u16>(16);
//PDATAWriteFifo = new FIFO<u16>(16);
return true;
}
void DeInit()
{
//if (PDATAWriteFifo) delete PDATAWriteFifo;
if (TeakraCore) delete TeakraCore;
//PDATAReadFifo = NULL;
//PDATAWriteFifo = NULL;
TeakraCore = NULL;
}
void Reset()
{
DSPTimestamp = 0;
DSP_PADR = 0;
DSP_PCFG = 0;
DSP_PSTS = 0;
DSP_PSEM = 0;
DSP_PMASK = 0xff;
DSP_PCLEAR = 0;
DSP_CMD[2] = DSP_CMD[1] = DSP_CMD[0] = 0;
DSP_REP[2] = DSP_REP[1] = DSP_REP[0] = 0;
PDataDMALen = 0;
PDATAReadFifo.Clear();
//PDATAWriteFifo->Clear();
TeakraCore->Reset();
NDS::CancelEvent(NDS::Event_DSi_DSP);
}
bool IsRstReleased()
{
return SCFG_RST;
}
void SetRstLine(bool release)
{
SCFG_RST = release;
Reset();
DSPTimestamp = NDS::ARM9Timestamp; // only start now!
}
void OnMBKCfg(char bank, u32 slot, u8 oldcfg, u8 newcfg, u8* nwrambacking)
{
if (bank != 'B' && bank != 'C')
{
printf("WTF?? (DSP MBK recfg, nonsense bank '%c')\n", bank);
return;
}
bool olddsp = (oldcfg & 3) >= 2, // was mapped to the DSP
newdsp = (newcfg & 3) >= 2; // will be mapped to the DSP
// nothing changes
if (olddsp == newdsp)
return;
const u8* src;
u8* dst;
if (newdsp)
{
// need to map stuff to DSP memory (== Teakra-owned memory) from NWRAM
src = nwrambacking;
dst = &TeakraCore->GetDspMemory()[((newcfg >> 2) & 7) << 15];
if (bank == 'C') // C: DSP data (B: DSP code)
dst += DataMemoryOffset*2;
}
else //if (olddsp)
{
// it was mapped to the DSP, but now we have to copy it out, back to NWRAM
src = &TeakraCore->GetDspMemory()[((oldcfg >> 2) & 7) << 15];
dst = nwrambacking;
if (bank == 'C') // C: DSP data (B: DSP code)
src += DataMemoryOffset*2;
}
memcpy(dst, src, 1<<15); // 1 full slot
}
inline bool IsDSPCoreEnabled()
{
return (DSi::SCFG_Clock9 & (1<<1)) && SCFG_RST && (DSP_PCFG & (1<<0));
}
bool DSPCatchUp()
{
//asm volatile("int3");
if (!IsDSPCoreEnabled())
{
// nothing to do, but advance the current time so that we don't do an
// unreasonable amount of cycles when rst is released
if (DSPTimestamp < NDS::ARM9Timestamp)
DSPTimestamp = NDS::ARM9Timestamp;
return false;
}
u64 curtime = NDS::ARM9Timestamp;
if (DSPTimestamp >= curtime) return true; // ummmm?!
u64 backlog = curtime - DSPTimestamp;
while (backlog & (1uLL<<32)) // god I hope this never happens
{
Run((u32)(backlog & ((1uLL<<32)-1)));
backlog = curtime - DSPTimestamp;
}
Run((u32)backlog);
return true;
}
void DSPCatchUpU32(u32 _) { DSPCatchUp(); }
void PDataDMAWrite(u16 wrval)
{
u32 addr = DSP_PADR;
switch (DSP_PCFG & (7<<12)) // memory region select
{
case 0<<12: // data
addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16;
TeakraCore->DataWriteA32(addr, wrval);
break;
case 1<<12: // mmio
TeakraCore->MMIOWrite(addr & 0x7FF, wrval);
break;
case 5<<12: // program
addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16;
TeakraCore->ProgramWrite(addr, wrval);
break;
case 7<<12:
addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16;
// only do stuff when AHBM is configured correctly
if (TeakraCore->AHBMGetDmaChannel(0) == 0 && TeakraCore->AHBMGetDirection(0) == 1/*W*/)
{
switch (TeakraCore->AHBMGetUnitSize(0))
{
case 0: /* 8bit */ DSi::ARM9Write8 (addr, (u8)wrval); break;
case 1: /* 16 b */ TeakraCore->AHBMWrite16(addr, wrval); break;
// does it work like this, or should it first buffer two u16's
// until it has enough data to write to the actual destination?
// -> this seems to be correct behavior!
case 2: /* 32 b */ TeakraCore->AHBMWrite32(addr, wrval); break;
}
}
break;
default: return;
}
if (DSP_PCFG & (1<<1)) // auto-increment
++DSP_PADR; // overflows and stays within a 64k 'page' // TODO: is this +1 or +2?
NDS::SetIRQ(0, NDS::IRQ_DSi_DSP); // wrfifo empty
}
// TODO: FIFO interrupts! (rd full, nonempty)
u16 PDataDMARead()
{
u16 r = 0;
u32 addr = DSP_PADR;
switch (DSP_PCFG & (7<<12)) // memory region select
{
case 0<<12: // data
addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16;
r = TeakraCore->DataReadA32(addr);
break;
case 1<<12: // mmio
r = TeakraCore->MMIORead(addr & 0x7FF);
break;
case 5<<12: // program
addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16;
r = TeakraCore->ProgramRead(addr);
break;
case 7<<12:
addr |= (u32)TeakraCore->DMAChan0GetDstHigh() << 16;
// only do stuff when AHBM is configured correctly
if (TeakraCore->AHBMGetDmaChannel(0) == 0 && TeakraCore->AHBMGetDirection(0) == 0/*R*/)
{
switch (TeakraCore->AHBMGetUnitSize(0))
{
case 0: /* 8bit */ r = DSi::ARM9Read8 (addr); break;
case 1: /* 16 b */ r = TeakraCore->AHBMRead16(addr); break;
case 2: /* 32 b */ r = (u16)TeakraCore->AHBMRead32(addr); break;
}
}
break;
default: return r;
}
if (DSP_PCFG & (1<<1)) // auto-increment
++DSP_PADR; // overflows and stays within a 64k 'page' // TODO: is this +1 or +2?
return r;
}
void PDataDMAFetch()
{
if (!PDataDMALen) return;
PDATAReadFifo.Write(PDataDMARead());
if (PDataDMALen > 0) --PDataDMALen;
}
void PDataDMAStart()
{
switch ((DSP_PSTS & (3<<2)) >> 2)
{
case 0: PDataDMALen = 1; break;
case 1: PDataDMALen = 8; break;
case 2: PDataDMALen =16; break;
case 3: PDataDMALen =-1; break;
}
// fill a single fifo
int amt = PDataDMALen;
if (amt < 0) amt = 16;
for (int i = 0; i < amt; ++i)
PDataDMAFetch();
NDS::SetIRQ(0, NDS::IRQ_DSi_DSP);
}
void PDataDMACancel()
{
PDataDMALen = 0;
PDATAReadFifo.Clear();
}
u16 PDataDMAReadMMIO()
{
u16 ret;
if (!PDATAReadFifo.IsEmpty())
ret = PDATAReadFifo.Read();
// aha, there's more to come
if (PDataDMALen != 0)
{
int left = 16 - PDATAReadFifo.Level();
if (PDataDMALen > 0 && PDataDMALen < left)
left = PDataDMALen;
for (int i = 0; i < left; ++i)
PDataDMAFetch();
ret = PDATAReadFifo.Read();
}
else
{
// ah, crap
ret = 0; // TODO: is this actually 0, or just open bus?
}
if (!PDATAReadFifo.IsEmpty() || PDATAReadFifo.IsFull())
NDS::SetIRQ(0, NDS::IRQ_DSi_DSP);
return ret;
}
u8 Read8(u32 addr)
{printf("DSP READ8 %08X\n", addr);
if (!(DSi::SCFG_EXT[0] & (1<<18)))
return 0;
if (!DSPCatchUp()) return 0;
addr &= 0x3F; // mirroring wheee
// ports are a bit weird, 16-bit regs in 32-bit spaces
switch (addr)
{
// no 8-bit PDATA read
// no DSP_PADR read
case 0x08: return DSP_PCFG & 0xFF;
case 0x09: return DSP_PCFG >> 8;
case 0x0C: return GetPSTS() & 0xFF;
case 0x0D: return GetPSTS() >> 8;
case 0x10: return DSP_PSEM & 0xFF;
case 0x11: return DSP_PSEM >> 8;
case 0x14: return DSP_PMASK & 0xFF;
case 0x15: return DSP_PMASK >> 8;
// no DSP_PCLEAR read
case 0x1C: return TeakraCore->GetSemaphore() & 0xFF; // SEM
case 0x1D: return TeakraCore->GetSemaphore() >> 8;
}
return 0;
}
u16 Read16(u32 addr)
{printf("DSP READ16 %08X\n", addr);
if (!(DSi::SCFG_EXT[0] & (1<<18)))
return 0;
if (!DSPCatchUp()) return 0;
addr &= 0x3E; // mirroring wheee
// ports are a bit weird, 16-bit regs in 32-bit spaces
switch (addr)
{
case 0x00: return PDataDMAReadMMIO();
// no DSP_PADR read
case 0x08: return DSP_PCFG;
case 0x0C: return GetPSTS();
case 0x10: return DSP_PSEM;
case 0x14: return DSP_PMASK;
// no DSP_PCLEAR read
case 0x1C: return TeakraCore->GetSemaphore(); // SEM
case 0x20: return DSP_CMD[0];
case 0x28: return DSP_CMD[1];
case 0x30: return DSP_CMD[2];
case 0x24:
{
u16 r = TeakraCore->RecvData(0);
return r;
}
case 0x2C:
{
u16 r = TeakraCore->RecvData(1);
return r;
}
case 0x34:
{
u16 r = TeakraCore->RecvData(2);
return r;
}
}
return 0;
}
u32 Read32(u32 addr)
{
if (!(DSi::SCFG_EXT[0] & (1<<18))) return 0;
addr &= 0x3C;
return Read16(addr); // *shrug* (doesn't do anything unintended due to the
// 4byte spacing between regs while they're all 16bit)
}
void Write8(u32 addr, u8 val)
{printf("DSP WRITE8 %08X %02X\n", addr, val);
if (!(DSi::SCFG_EXT[0] & (1<<18))) return;
if (!DSPCatchUp()) return;
addr &= 0x3F;
switch (addr)
{
// no 8-bit PDATA or PADR writes
case 0x08:
DSP_PCFG = (DSP_PCFG & 0xFF00) | (val << 0);
break;
case 0x09:
DSP_PCFG = (DSP_PCFG & 0x00FF) | (val << 8);
break;
// no PSTS writes
// no 8-bit semaphore writes
// no 8-bit CMDx writes
// no REPx writes
}
}
void Write16(u32 addr, u16 val)
{printf("DSP WRITE16 %08X %04X\n", addr, val);
if (!(DSi::SCFG_EXT[0] & (1<<18))) return;
if (!DSPCatchUp()) return;
addr &= 0x3E;
switch (addr)
{
case 0x00: PDataDMAWrite(val); break;
case 0x04: DSP_PADR = val; break;
case 0x08:
DSP_PCFG = val;
if (DSP_PCFG & (1<<4))
PDataDMAStart();
else
PDataDMACancel();
break;
// no PSTS writes
case 0x10:
DSP_PSEM = val;
TeakraCore->SetSemaphore(val);
break;
case 0x14:
DSP_PMASK = val;
TeakraCore->MaskSemaphore(val);
break;
case 0x18: // PCLEAR
TeakraCore->ClearSemaphore(val);
if (TeakraCore->GetSemaphore() == 0)
DSP_PSTS &= ~(1<<9);
break;
// SEM not writable
case 0x20: // CMD0
DSP_CMD[0] = val;
TeakraCore->SendData(0, val);
break;
case 0x28: // CMD1
DSP_CMD[1] = val;
TeakraCore->SendData(1, val);
break;
case 0x30: // CMD2
DSP_CMD[2] = val;
TeakraCore->SendData(2, val);
break;
// no REPx writes
}
}
void Write32(u32 addr, u32 val)
{
if (!(DSi::SCFG_EXT[0] & (1<<18))) return;
addr &= 0x3C;
Write16(addr, val & 0xFFFF);
}
void Run(u32 cycles)
{
if (!IsDSPCoreEnabled())
{
DSPTimestamp += cycles;
return;
}
TeakraCore->Run(cycles);
DSPTimestamp += cycles;
NDS::ScheduleEvent(NDS::Event_DSi_DSP, false,
16384/*from citra (TeakraSlice)*/, DSPCatchUpU32, 0);
}
void DoSavestate(Savestate* file)
{
file->Section("DSPi");
PDATAReadFifo.DoSavestate(file);
file->Var64(&DSPTimestamp);
file->Var32((u32*)&PDataDMALen);
file->Var16(&DSP_PADR);
file->Var16(&DSP_PCFG);
file->Var16(&DSP_PSTS);
file->Var16(&DSP_PSEM);
file->Var16(&DSP_PMASK);
file->Var16(&DSP_PCLEAR);
file->Var16(&DSP_CMD[0]);
file->Var16(&DSP_CMD[1]);
file->Var16(&DSP_CMD[2]);
file->Var16(&DSP_REP[0]);
file->Var16(&DSP_REP[1]);
file->Var16(&DSP_REP[2]);
file->Var8((u8*)&SCFG_RST);
}
}

71
src/DSi_DSP.h Normal file
View File

@ -0,0 +1,71 @@
/*
Copyright 2020 PoroCYon
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef DSI_DSP_H
#define DSI_DSP_H
// TODO: for actual sound output
// * audio callbacks
// * SNDEXCNT
namespace DSi_DSP
{
extern u16 DSP_PDATA;
extern u16 DSP_PADR;
extern u16 DSP_PCFG;
extern u16 DSP_PSTS;
extern u16 DSP_PSEM;
extern u16 DSP_PMASK;
extern u16 DSP_PCLEAR;
extern u16 DSP_SEM;
extern u16 DSP_CMD[3];
extern u16 DSP_REP[3];
bool Init();
void DeInit();
void Reset();
// TODO: needs to be called!
// however, no DSi savestate stuff seems to be actually implemented?!
void DoSavestate(Savestate* file);
// SCFG_RST bit0
bool IsRstReleased();
void SetRstLine(bool release);
// apply NWRAM settings
void OnMBKCfg(char bank, u32 slot, u8 oldcfg, u8 newcfg, u8* nwrambacking);
// DSP_* regs (0x040043xx) (NOTE: checks SCFG_EXT)
u8 Read8(u32 addr);
void Write8(u32 addr, u8 val);
u16 Read16(u32 addr);
void Write16(u32 addr, u16 val);
u32 Read32(u32 addr);
void Write32(u32 addr, u32 val);
// NOTE: checks SCFG_CLK9
void Run(u32 cycles);
}
#endif // DSI_DSP_H

View File

@ -72,6 +72,8 @@ void Reset()
Registers[0x81] = 0x64;
}
u8 GetBootFlag() { return Registers[0x70]; }
void Start()
{
//printf("BPTWL: start\n");

View File

@ -19,6 +19,13 @@
#ifndef DSI_I2C_H
#define DSI_I2C_H
namespace DSi_BPTWL
{
u8 GetBootFlag();
}
namespace DSi_I2C
{

1130
src/DSi_NAND.cpp Normal file

File diff suppressed because it is too large Load Diff

59
src/DSi_NAND.h Normal file
View File

@ -0,0 +1,59 @@
/*
Copyright 2016-2021 Arisotura
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef DSI_NAND_H
#define DSI_NAND_H
#include "types.h"
#include "NDS_Header.h"
#include <vector>
#include <string>
namespace DSi_NAND
{
enum
{
TitleData_PublicSav,
TitleData_PrivateSav,
TitleData_BannerSav,
};
bool Init(FILE* nand, u8* es_keyY);
void DeInit();
void GetIDs(u8* emmc_cid, u64& consoleid);
void ReadHardwareInfo(u8* dataS, u8* dataN);
void ReadUserData(u8* data);
void PatchTSC();
void ListTitles(u32 category, std::vector<u32>& titlelist);
bool TitleExists(u32 category, u32 titleid);
void GetTitleInfo(u32 category, u32 titleid, u32& version, NDSHeader* header, NDSBanner* banner);
bool ImportTitle(const char* appfile, u8* tmd, bool readonly);
void DeleteTitle(u32 category, u32 titleid);
u32 GetTitleDataMask(u32 category, u32 titleid);
bool ImportTitleData(u32 category, u32 titleid, int type, const char* file);
bool ExportTitleData(u32 category, u32 titleid, int type, const char* file);
}
#endif // DSI_NAND_H

View File

@ -81,6 +81,11 @@ void DoSavestate(Savestate* file)
// TODO!!
}
void SetMode(u8 mode)
{
TSCMode = mode;
}
void SetTouchCoords(u16 x, u16 y)
{
if (TSCMode == 0x00)

View File

@ -29,6 +29,9 @@ void DeInit();
void Reset();
void DoSavestate(Savestate* file);
// 00=DS-mode 01=normal
void SetMode(u8 mode);
void SetTouchCoords(u16 x, u16 y);
void MicInputFrame(s16* data, int samples);

View File

@ -1061,6 +1061,12 @@ void FinishFrame(u32 lines)
AssignFramebuffers();
TotalScanlines = lines;
if (GPU3D::AbortFrame)
{
GPU3D::RestartFrame();
GPU3D::AbortFrame = false;
}
}
void StartScanline(u32 line)
@ -1180,6 +1186,7 @@ void SetVCount(u16 val)
// 3D engine seems to give up on the current frame in that situation, repeating the last two scanlines
// TODO: also check the various DMA types that can be involved
GPU3D::AbortFrame |= NextVCount != val;
NextVCount = val;
}

View File

@ -275,6 +275,8 @@ u32 FlushAttributes;
std::unique_ptr<GPU3D::Renderer3D> CurrentRenderer = {};
bool AbortFrame;
bool Init()
{
return true;
@ -380,6 +382,8 @@ void Reset()
ResetRenderingState();
RenderXPos = 0;
AbortFrame = false;
}
void DoSavestate(Savestate* file)
@ -621,6 +625,8 @@ void DoSavestate(Savestate* file)
file->Bool32(&UseShininessTable);
file->VarArray(ShininessTable, 128*sizeof(u8));
file->Bool32(&AbortFrame);
}
@ -2611,27 +2617,34 @@ u32 ScrolledLine[256];
u32* GetLine(int line)
{
u32* rawline = CurrentRenderer->GetLine(line);
if (RenderXPos == 0) return rawline;
// apply X scroll
if (RenderXPos & 0x100)
if (!AbortFrame)
{
int i = 0, j = RenderXPos;
for (; j < 512; i++, j++)
ScrolledLine[i] = 0;
for (j = 0; i < 256; i++, j++)
ScrolledLine[i] = rawline[j];
u32* rawline = CurrentRenderer->GetLine(line);
if (RenderXPos == 0) return rawline;
// apply X scroll
if (RenderXPos & 0x100)
{
int i = 0, j = RenderXPos;
for (; j < 512; i++, j++)
ScrolledLine[i] = 0;
for (j = 0; i < 256; i++, j++)
ScrolledLine[i] = rawline[j];
}
else
{
int i = 0, j = RenderXPos;
for (; j < 256; i++, j++)
ScrolledLine[i] = rawline[j];
for (; i < 256; i++)
ScrolledLine[i] = 0;
}
}
else
{
int i = 0, j = RenderXPos;
for (; j < 256; i++, j++)
ScrolledLine[i] = rawline[j];
for (; i < 256; i++)
ScrolledLine[i] = 0;
memset(ScrolledLine, 0, 256*4);
}
return ScrolledLine;

View File

@ -97,6 +97,8 @@ extern u16 RenderXPos;
extern std::array<Polygon*,2048> RenderPolygonRAM;
extern u32 RenderNumPolygons;
extern bool AbortFrame;
extern u64 Timestamp;
bool Init();

View File

@ -18,6 +18,7 @@
#include "GPU3D_Soft.h"
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include "NDS.h"
@ -756,11 +757,10 @@ void SoftRenderer::RenderShadowMaskScanline(RendererPolygon* rp, s32 y)
rp->SlopeR.EdgeParams_YMajor(&l_edgelen, &l_edgecov);
rp->SlopeL.EdgeParams_YMajor(&r_edgelen, &r_edgecov);
s32 tmp;
tmp = xstart; xstart = xend; xend = tmp;
tmp = wl; wl = wr; wr = tmp;
tmp = zl; zl = zr; zr = tmp;
tmp = (s32)l_filledge; l_filledge = r_filledge; r_filledge = (bool)tmp;
std::swap(xstart, xend);
std::swap(wl, wr);
std::swap(zl, zr);
std::swap(l_filledge, r_filledge);
}
else
{
@ -820,7 +820,7 @@ void SoftRenderer::RenderShadowMaskScanline(RendererPolygon* rp, s32 y)
continue;
if (!fnDepthTest(DepthBuffer[pixeladdr], z, dstattr))
StencilBuffer[256*(y&0x1) + x] |= 0x1;
StencilBuffer[256*(y&0x1) + x] = 1;
if (dstattr & 0x3)
{
@ -977,11 +977,10 @@ void SoftRenderer::RenderPolygonScanline(RendererPolygon* rp, s32 y)
rp->SlopeR.EdgeParams_YMajor(&l_edgelen, &l_edgecov);
rp->SlopeL.EdgeParams_YMajor(&r_edgelen, &r_edgecov);
s32 tmp;
tmp = xstart; xstart = xend; xend = tmp;
tmp = wl; wl = wr; wr = tmp;
tmp = zl; zl = zr; zr = tmp;
tmp = (s32)l_filledge; l_filledge = r_filledge; r_filledge = (bool)tmp;
std::swap(xstart, xend);
std::swap(wl, wr);
std::swap(zl, zr);
std::swap(l_filledge, r_filledge);
}
else
{
@ -1066,7 +1065,7 @@ void SoftRenderer::RenderPolygonScanline(RendererPolygon* rp, s32 y)
// against the pixel underneath
if (!fnDepthTest(DepthBuffer[pixeladdr], z, dstattr))
{
if (!(dstattr & 0x3)) continue;
if (!(dstattr & 0x3) || pixeladdr >= BufferSize) continue;
pixeladdr += BufferSize;
dstattr = AttrBuffer[pixeladdr];
@ -1162,7 +1161,7 @@ void SoftRenderer::RenderPolygonScanline(RendererPolygon* rp, s32 y)
// against the pixel underneath
if (!fnDepthTest(DepthBuffer[pixeladdr], z, dstattr))
{
if (!(dstattr & 0x3)) continue;
if (!(dstattr & 0x3) || pixeladdr >= BufferSize) continue;
pixeladdr += BufferSize;
dstattr = AttrBuffer[pixeladdr];
@ -1237,7 +1236,7 @@ void SoftRenderer::RenderPolygonScanline(RendererPolygon* rp, s32 y)
// against the pixel underneath
if (!fnDepthTest(DepthBuffer[pixeladdr], z, dstattr))
{
if (!(dstattr & 0x3)) continue;
if (!(dstattr & 0x3) || pixeladdr >= BufferSize) continue;
pixeladdr += BufferSize;
dstattr = AttrBuffer[pixeladdr];
@ -1646,7 +1645,7 @@ void SoftRenderer::RenderPolygons(bool threaded, Polygon** polygons, int npolys)
void SoftRenderer::VCount144()
{
if (RenderThreadRunning.load(std::memory_order_relaxed))
if (RenderThreadRunning.load(std::memory_order_relaxed) && !GPU3D::AbortFrame)
Platform::Semaphore_Wait(Sema_RenderDone);
}

View File

@ -18,6 +18,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "Config.h"
#include "NDS.h"
#include "ARM.h"
@ -165,7 +166,6 @@ bool Running;
bool RunningGame;
void DivDone(u32 param);
void SqrtDone(u32 param);
void RunTimer(u32 tid, s32 cycles);
@ -352,93 +352,86 @@ void SetupDirectBoot()
{
if (ConsoleType == 1)
{
printf("!! DIRECT BOOT NOT SUPPORTED IN DSI MODE\n");
return;
DSi::SetupDirectBoot();
}
u32 bootparams[8];
memcpy(bootparams, &NDSCart::CartROM[0x20], 8*4);
printf("ARM9: offset=%08X entry=%08X RAM=%08X size=%08X\n",
bootparams[0], bootparams[1], bootparams[2], bootparams[3]);
printf("ARM7: offset=%08X entry=%08X RAM=%08X size=%08X\n",
bootparams[4], bootparams[5], bootparams[6], bootparams[7]);
MapSharedWRAM(3);
u32 arm9start = 0;
// load the ARM9 secure area
if (bootparams[0] >= 0x4000 && bootparams[0] < 0x8000)
else
{
u8 securearea[0x800];
NDSCart::DecryptSecureArea(securearea);
MapSharedWRAM(3);
for (u32 i = 0; i < 0x800; i+=4)
u32 arm9start = 0;
// load the ARM9 secure area
if (NDSCart::Header.ARM9ROMOffset >= 0x4000 && NDSCart::Header.ARM9ROMOffset < 0x8000)
{
ARM9Write32(bootparams[2]+i, *(u32*)&securearea[i]);
arm9start += 4;
u8 securearea[0x800];
NDSCart::DecryptSecureArea(securearea);
for (u32 i = 0; i < 0x800; i+=4)
{
ARM9Write32(NDSCart::Header.ARM9RAMAddress+i, *(u32*)&securearea[i]);
arm9start += 4;
}
}
// CHECKME: firmware seems to load this in 0x200 byte chunks
for (u32 i = arm9start; i < NDSCart::Header.ARM9Size; i+=4)
{
u32 tmp = *(u32*)&NDSCart::CartROM[NDSCart::Header.ARM9ROMOffset+i];
ARM9Write32(NDSCart::Header.ARM9RAMAddress+i, tmp);
}
for (u32 i = 0; i < NDSCart::Header.ARM7Size; i+=4)
{
u32 tmp = *(u32*)&NDSCart::CartROM[NDSCart::Header.ARM7ROMOffset+i];
ARM7Write32(NDSCart::Header.ARM7RAMAddress+i, tmp);
}
for (u32 i = 0; i < 0x170; i+=4)
{
u32 tmp = *(u32*)&NDSCart::CartROM[i];
ARM9Write32(0x027FFE00+i, tmp);
}
ARM9Write32(0x027FF800, NDSCart::CartID);
ARM9Write32(0x027FF804, NDSCart::CartID);
ARM9Write16(0x027FF808, NDSCart::Header.HeaderCRC16);
ARM9Write16(0x027FF80A, NDSCart::Header.SecureAreaCRC16);
ARM9Write16(0x027FF850, 0x5835);
ARM9Write32(0x027FFC00, NDSCart::CartID);
ARM9Write32(0x027FFC04, NDSCart::CartID);
ARM9Write16(0x027FFC08, NDSCart::Header.HeaderCRC16);
ARM9Write16(0x027FFC0A, NDSCart::Header.SecureAreaCRC16);
ARM9Write16(0x027FFC10, 0x5835);
ARM9Write16(0x027FFC30, 0xFFFF);
ARM9Write16(0x027FFC40, 0x0001);
ARM7BIOSProt = 0x1204;
SPI_Firmware::SetupDirectBoot(false);
}
// CHECKME: firmware seems to load this in 0x200 byte chunks
for (u32 i = arm9start; i < bootparams[3]; i+=4)
{
u32 tmp = *(u32*)&NDSCart::CartROM[bootparams[0]+i];
ARM9Write32(bootparams[2]+i, tmp);
}
for (u32 i = 0; i < bootparams[7]; i+=4)
{
u32 tmp = *(u32*)&NDSCart::CartROM[bootparams[4]+i];
ARM7Write32(bootparams[6]+i, tmp);
}
for (u32 i = 0; i < 0x170; i+=4)
{
u32 tmp = *(u32*)&NDSCart::CartROM[i];
ARM9Write32(0x027FFE00+i, tmp);
}
ARM9Write32(0x027FF800, NDSCart::CartID);
ARM9Write32(0x027FF804, NDSCart::CartID);
ARM9Write16(0x027FF808, *(u16*)&NDSCart::CartROM[0x15E]);
ARM9Write16(0x027FF80A, *(u16*)&NDSCart::CartROM[0x6C]);
ARM9Write16(0x027FF850, 0x5835);
ARM9Write32(0x027FFC00, NDSCart::CartID);
ARM9Write32(0x027FFC04, NDSCart::CartID);
ARM9Write16(0x027FFC08, *(u16*)&NDSCart::CartROM[0x15E]);
ARM9Write16(0x027FFC0A, *(u16*)&NDSCart::CartROM[0x6C]);
ARM9Write16(0x027FFC10, 0x5835);
ARM9Write16(0x027FFC30, 0xFFFF);
ARM9Write16(0x027FFC40, 0x0001);
ARM9->CP15Write(0x910, 0x0300000A);
ARM9->CP15Write(0x911, 0x00000020);
ARM9->CP15Write(0x100, ARM9->CP15Read(0x100) | 0x00050000);
// hax
ARM9->CP15Write(0x502, 0x33333363);
ARM9->CP15Write(0x503, 0x33333363);
ARM9->R[12] = bootparams[1];
ARM9->R[12] = NDSCart::Header.ARM9EntryAddress;
ARM9->R[13] = 0x03002F7C;
ARM9->R[14] = bootparams[1];
ARM9->R[14] = NDSCart::Header.ARM9EntryAddress;
ARM9->R_IRQ[0] = 0x03003F80;
ARM9->R_SVC[0] = 0x03003FC0;
ARM7->R[12] = bootparams[5];
ARM7->R[12] = NDSCart::Header.ARM7EntryAddress;
ARM7->R[13] = 0x0380FD80;
ARM7->R[14] = bootparams[5];
ARM7->R[14] = NDSCart::Header.ARM7EntryAddress;
ARM7->R_IRQ[0] = 0x0380FF80;
ARM7->R_SVC[0] = 0x0380FFC0;
ARM9->JumpTo(bootparams[1]);
ARM7->JumpTo(bootparams[5]);
ARM9->JumpTo(NDSCart::Header.ARM9EntryAddress);
ARM7->JumpTo(NDSCart::Header.ARM7EntryAddress);
PostFlag9 = 0x01;
PostFlag7 = 0x01;
@ -454,10 +447,6 @@ void SetupDirectBoot()
SPU::SetBias(0x200);
SetWifiWaitCnt(0x0030);
ARM7BIOSProt = 0x1204;
SPI_Firmware::SetupDirectBoot();
}
void Reset()
@ -606,12 +595,25 @@ void Reset()
RTC::Reset();
Wifi::Reset();
// The SOUNDBIAS register does nothing on DSi
SPU::SetApplyBias(ConsoleType == 0);
bool degradeAudio = true;
if (ConsoleType == 1)
{
DSi::Reset();
KeyInput &= ~(1 << (16+6));
degradeAudio = false;
}
if (Config::AudioBitrate == 1) // Always 10-bit
degradeAudio = true;
else if (Config::AudioBitrate == 2) // Always 16-bit
degradeAudio = false;
SPU::SetDegrade10Bit(degradeAudio);
AREngine::Reset();
}
@ -1435,10 +1437,7 @@ u64 GetSysClockCycles(int num)
void NocashPrint(u32 ncpu, u32 addr)
{
// addr: u16 flags (TODO: research? libnds doesn't use those)
// addr+2: debug string
addr += 2;
// addr: debug string
ARM* cpu = ncpu ? (ARM*)ARM7 : (ARM*)ARM9;
u8 (*readfn)(u32) = ncpu ? NDS::ARM7Read8 : NDS::ARM9Read8;
@ -1865,7 +1864,7 @@ void debug(u32 param)
//for (int i = 0; i < 9; i++)
// printf("VRAM %c: %02X\n", 'A'+i, GPU::VRAMCNT[i]);
FILE*
/*FILE*
shit = fopen("debug/construct.bin", "wb");
fwrite(ARM9->ITCM, 0x8000, 1, shit);
for (u32 i = 0x02000000; i < 0x02400000; i+=4)
@ -1878,23 +1877,23 @@ void debug(u32 param)
u32 val = ARM7Read32(i);
fwrite(&val, 4, 1, shit);
}
fclose(shit);
fclose(shit);*/
/*FILE*
shit = fopen("debug/power9.bin", "wb");
FILE*
shit = fopen("debug/directboot9.bin", "wb");
for (u32 i = 0x02000000; i < 0x04000000; i+=4)
{
u32 val = DSi::ARM9Read32(i);
fwrite(&val, 4, 1, shit);
}
fclose(shit);
shit = fopen("debug/power7.bin", "wb");
shit = fopen("debug/directboot7.bin", "wb");
for (u32 i = 0x02000000; i < 0x04000000; i+=4)
{
u32 val = DSi::ARM7Read32(i);
fwrite(&val, 4, 1, shit);
}
fclose(shit);*/
fclose(shit);
}
@ -2846,6 +2845,14 @@ u8 ARM9IORead8(u32 addr)
{
return GPU3D::Read8(addr);
}
// NO$GBA debug register "Emulation ID"
if(addr >= 0x04FFFA00 && addr < 0x04FFFA10)
{
// FIX: GBATek says this should be padded with spaces
static char const emuID[16] = "melonDS " MELONDS_VERSION;
auto idx = addr - 0x04FFFA00;
return (u8)(emuID[idx]);
}
printf("unknown ARM9 IO read8 %08X %08X\n", addr, ARM9->R[15]);
return 0;
@ -2972,6 +2979,12 @@ u16 ARM9IORead16(u32 addr)
case 0x04000300: return PostFlag9;
case 0x04000304: return PowerControl9;
case 0x04004000:
case 0x04004004:
case 0x04004010:
// shut up logging for DSi registers
return 0;
}
if ((addr >= 0x04000000 && addr < 0x04000060) || (addr == 0x0400006C))
@ -3104,6 +3117,17 @@ u32 ARM9IORead32(u32 addr)
case 0x04100010:
if (!(ExMemCnt[0] & (1<<11))) return NDSCart::ReadROMData();
return 0;
case 0x04004000:
case 0x04004004:
case 0x04004010:
// shut up logging for DSi registers
return 0;
// NO$GBA debug register "Clock Cycles"
// Since it's a 64 bit reg. the CPU will access it in two parts:
case 0x04FFFA20: return (u32)(GetSysClockCycles(0) & 0xFFFFFFFF);
case 0x04FFFA24: return (u32)(GetSysClockCycles(0) >> 32);
}
if ((addr >= 0x04000000 && addr < 0x04000060) || (addr == 0x0400006C))
@ -3318,10 +3342,14 @@ void ARM9IOWrite16(u32 addr, u16 val)
case 0x040001BA: ROMSeed1[4] = val & 0x7F; return;
case 0x04000204:
ExMemCnt[0] = val;
ExMemCnt[1] = (ExMemCnt[1] & 0x007F) | (val & 0xFF80);
SetGBASlotTimings();
return;
{
u16 oldVal = ExMemCnt[0];
ExMemCnt[0] = val;
ExMemCnt[1] = (ExMemCnt[1] & 0x007F) | (val & 0xFF80);
if ((oldVal ^ ExMemCnt[0]) & 0xFF)
SetGBASlotTimings();
return;
}
case 0x04000208: IME[0] = val & 0x1; UpdateIRQ(0); return;
case 0x04000210: IE[0] = (IE[0] & 0xFFFF0000) | val; UpdateIRQ(0); return;
@ -3532,6 +3560,34 @@ void ARM9IOWrite32(u32 addr, u32 val)
case 0x04100010:
if (!(ExMemCnt[0] & (1<<11))) NDSCart::WriteROMData(val);
return;
// NO$GBA debug register "String Out (raw)"
case 0x04FFFA10:
{
char output[1024] = { 0 };
char ch = '.';
for (size_t i = 0; i < 1023 && ch != '\0'; i++)
{
ch = NDS::ARM9Read8(val + i);
output[i] = ch;
}
printf("%s", output);
return;
}
// NO$GBA debug registers "String Out (with parameters)" and "String Out (with parameters, plus linefeed)"
case 0x04FFFA14:
case 0x04FFFA18:
{
bool appendLF = 0x04FFFA18 == addr;
NocashPrint(0, val);
if(appendLF)
printf("\n");
return;
}
// NO$GBA debug register "Char Out"
case 0x04FFFA1C: printf("%" PRIu32, val); return;
}
if (addr >= 0x04000000 && addr < 0x04000060)
@ -3999,9 +4055,13 @@ void ARM7IOWrite16(u32 addr, u16 val)
return;
case 0x04000204:
ExMemCnt[1] = (ExMemCnt[1] & 0xFF80) | (val & 0x007F);
SetGBASlotTimings();
return;
{
u16 oldVal = ExMemCnt[1];
ExMemCnt[1] = (ExMemCnt[1] & 0xFF80) | (val & 0x007F);
if ((ExMemCnt[1] ^ oldVal) & 0xFF)
SetGBASlotTimings();
return;
}
case 0x04000206:
SetWifiWaitCnt(val);
return;

View File

@ -50,6 +50,7 @@ enum
Event_DSi_CamTransfer,
Event_DSi_RAMSizeChange,
Event_DSi_DSP,
Event_MAX
};

View File

@ -58,6 +58,9 @@ u32 CartID;
bool CartIsHomebrew;
bool CartIsDSi;
NDSHeader Header;
NDSBanner Banner;
CartCommon* Cart;
u32 Key1_KeyBuf[0x412];
@ -173,15 +176,6 @@ void Key2_Encrypt(u8* data, u32 len)
}
void ApplyModcrypt(u32 addr, u32 len, u8* iv)
{return;
u8 key[16];
DSi_AES::GetModcryptKey(&CartROM[0], key);
DSi_AES::ApplyModcrypt(&CartROM[addr], len, key, iv);
}
CartCommon::CartCommon(u8* rom, u32 len, u32 chipid)
{
ROM = rom;
@ -190,6 +184,7 @@ CartCommon::CartCommon(u8* rom, u32 len, u32 chipid)
u8 unitcode = ROM[0x12];
IsDSi = (unitcode & 0x02) != 0;
DSiBase = *(u16*)&ROM[0x92] << 19;
}
CartCommon::~CartCommon()
@ -200,12 +195,14 @@ void CartCommon::Reset()
{
CmdEncMode = 0;
DataEncMode = 0;
DSiMode = false;
}
void CartCommon::SetupDirectBoot()
{
CmdEncMode = 2;
DataEncMode = 2;
DSiMode = false; // TODO!!
}
void CartCommon::DoSavestate(Savestate* file)
@ -214,6 +211,7 @@ void CartCommon::DoSavestate(Savestate* file)
file->Var32(&CmdEncMode);
file->Var32(&DataEncMode);
file->Bool32(&DSiMode);
}
void CartCommon::LoadSave(const char* path, u32 type)
@ -263,13 +261,15 @@ int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
case 0x3C:
CmdEncMode = 1;
Key1_InitKeycode(false, *(u32*)&ROM[0xC], 2, 2);
DSiMode = false;
return 0;
case 0x3D:
if (IsDSi)
{
CmdEncMode = 11;
CmdEncMode = 1;
Key1_InitKeycode(true, *(u32*)&ROM[0xC], 1, 2);
DSiMode = true;
}
return 0;
@ -277,7 +277,7 @@ int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
return 0;
}
}
else if (CmdEncMode == 1 || CmdEncMode == 11)
else if (CmdEncMode == 1)
{
// decrypt the KEY1 command as needed
// (KEY2 commands do not need decrypted because KEY2 is handled entirely by hardware,
@ -306,14 +306,13 @@ int CartCommon::ROMCommandStart(u8* cmd, u8* data, u32 len)
case 0x20:
{
u32 addr = (cmddec[2] & 0xF0) << 8;
if (CmdEncMode == 11)
if (DSiMode)
{
// the DSi region starts with 0x3000 unreadable bytes
// similarly to how the DS region starts at 0x1000 with 0x3000 unreadable bytes
// these contain data for KEY1 crypto
u32 dsiregion = *(u16*)&ROM[0x92] << 19;
addr -= 0x1000;
addr += dsiregion;
addr += DSiBase;
}
ReadROM(addr, 0x1000, data, 0);
}
@ -594,9 +593,15 @@ void CartRetail::ReadROM_B7(u32 addr, u32 len, u8* data, u32 offset)
if (addr < 0x8000)
addr = 0x8000 + (addr & 0x1FF);
// TODO: protect DSi secure area
// also protect DSi region if not unlocked
// and other security shenanigans
if (IsDSi && (addr >= DSiBase))
{
// for DSi carts:
// * in DSi mode: block the first 0x3000 bytes of the DSi area
// * in DS mode: block the entire DSi area
if ((!DSiMode) || (addr < (DSiBase+0x3000)))
addr = 0x8000 + (addr & 0x1FF);
}
memcpy(data+offset, ROM+addr, len);
}
@ -887,8 +892,9 @@ void CartRetailNAND::LoadSave(const char* path, u32 type)
int CartRetailNAND::ImportSRAM(const u8* data, u32 length)
{
CartRetail::ImportSRAM(data, length);
int ret = CartRetail::ImportSRAM(data, length);
BuildSRAMID();
return ret;
}
int CartRetailNAND::ROMCommandStart(u8* cmd, u8* data, u32 len)
@ -1117,6 +1123,8 @@ u8 CartRetailIR::SPIWrite(u8 val, u32 pos, bool last)
case 0x08: // ID
return 0xAA;
}
return 0;
}
@ -1495,8 +1503,11 @@ void DecryptSecureArea(u8* out)
// * .srl ROMs (VC dumps) have encrypted secure areas but have precomputed
// decryption data at 0x1000 (and at the beginning of the DSi region if any)
u32 gamecode = *(u32*)&CartROM[0x0C];
u32 arm9base = *(u32*)&CartROM[0x20];
u32 gamecode = (u32)Header.GameCode[3] << 24 |
(u32)Header.GameCode[2] << 16 |
(u32)Header.GameCode[1] << 8 |
(u32)Header.GameCode[0];
u32 arm9base = Header.ARM9ROMOffset;
memcpy(out, &CartROM[arm9base], 0x800);
@ -1523,11 +1534,17 @@ void DecryptSecureArea(u8* out)
bool LoadROMCommon(u32 filelength, const char *sram, bool direct)
{
u32 gamecode;
memcpy(&gamecode, CartROM + 0x0C, 4);
printf("Game code: %c%c%c%c\n", gamecode&0xFF, (gamecode>>8)&0xFF, (gamecode>>16)&0xFF, gamecode>>24);
memcpy(&Header, CartROM, sizeof(Header));
memcpy(&Banner, CartROM + Header.BannerOffset, sizeof(Banner));
u8 unitcode = CartROM[0x12];
printf("Game code: %.4s\n", Header.GameCode);
u32 gamecode = (u32)Header.GameCode[3] << 24 |
(u32)Header.GameCode[2] << 16 |
(u32)Header.GameCode[1] << 8 |
(u32)Header.GameCode[0];
u8 unitcode = Header.UnitCode;
CartIsDSi = (unitcode & 0x02) != 0;
ROMListEntry romparams;

View File

@ -20,6 +20,7 @@
#define NDSCART_H
#include "types.h"
#include "NDS_Header.h"
namespace NDSCart
{
@ -55,6 +56,8 @@ protected:
u32 ROMLength;
u32 ChipID;
bool IsDSi;
bool DSiMode;
u32 DSiBase;
u32 CmdEncMode;
u32 DataEncMode;
@ -191,6 +194,9 @@ extern u32 CartROMSize;
extern u32 CartID;
extern NDSHeader Header;
extern NDSBanner Banner;
bool Init();
void DeInit();
void Reset();

213
src/NDS_Header.h Normal file
View File

@ -0,0 +1,213 @@
/*
Copyright 2016-2021 Arisotura, WaluigiWare64
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef NDS_HEADER_H
#define NDS_HEADER_H
#include "types.h"
// Consult GBATEK for info on what these are
struct NDSHeader
{
char GameTitle[12];
char GameCode[4];
char MakerCode[2];
u8 UnitCode;
u8 EncryptionSeedSelect;
u8 CardSize;
u8 Reserved1[7];
u8 DSiCryptoFlags;
u8 NDSRegion;
u8 ROMVersion;
u8 Autostart;
u32 ARM9ROMOffset;
u32 ARM9EntryAddress;
u32 ARM9RAMAddress;
u32 ARM9Size;
u32 ARM7ROMOffset;
u32 ARM7EntryAddress;
u32 ARM7RAMAddress;
u32 ARM7Size;
u32 FNTOffset;
u32 FNTSize;
u32 FATOffset;
u32 FATSize;
u32 ARM9OverlayOffset;
u32 ARM9OverlaySize;
u32 ARM7OverlayOffset;
u32 ARM7OverlaySize;
u32 NormalCommandSettings;
u32 Key1CommandSettings;
u32 BannerOffset;
u16 SecureAreaCRC16;
u16 SecureAreaDelay;
// GBATEK lists the following two with a question mark
u32 ARM9AutoLoadListAddress;
u32 ARM7AutoLoadListAddress;
u64 SecureAreaDisable;
u32 ROMSize; // excluding DSi area
u32 HeaderSize;
// GBATEK lists the following two with a question mark
u32 DSiARM9ParamTableOffset;
u32 DSiARM7ParamTableOffset;
// expressed in 0x80000-byte units
u16 NDSRegionEnd;
u16 DSiRegionStart;
// specific to NAND games
u16 NANDROMEnd;
u16 NANDRWStart;
u8 Reserved2[40];
u8 NintendoLogo[156];
u16 NintendoLogoCRC16;
u16 HeaderCRC16;
u32 DebugROMOffset;
u32 DebugSize;
u32 DebugRAMAddress;
u32 Reserved4;
u8 Reserved5[16];
u32 DSiMBKSlots[5]; // global MBK1..MBK5 settings
u32 DSiARM9MBKAreas[3]; // local MBK6..MBK8 settings for ARM9
u32 DSiARM7MBKAreas[3]; // local MBK6..MBK8 settings for ARM7
u8 DSiMBKWriteProtect[3]; // global MBK9 setting
u8 DSiWRAMCntSetting; // global WRAMCNT setting
u32 DSiRegionMask;
u32 DSiPermissions[2];
u8 Reserved6[3];
u8 AppFlags; // flags at 1BF
u32 DSiARM9iROMOffset;
u32 Reserved7;
u32 DSiARM9iRAMAddress;
u32 DSiARM9iSize;
u32 DSiARM7iROMOffset;
u32 DSiSDMMCDeviceList;
u32 DSiARM7iRAMAddress;
u32 DSiARM7iSize;
u32 DSiDigestNTROffset;
u32 DSiDigestNTRSize;
u32 DSiDigestTWLOffset;
u32 DSiDigestTWLSize;
u32 DSiDigestSecHashtblOffset;
u32 DSiDigestSecHashtblSize;
u32 DSiDigestBlkHashtblOffset;
u32 DSiDigestBlkHashtblSize;
u32 DSiDigestSecSize; // sector size in bytes
u32 DSiDigestBlkSecCount; // sectors per block
u32 DSiBannerSize;
// ???
u8 DSiShared0Size;
u8 DSiShared1Size;
u8 DSiEULARatings;
u8 DSiUseRatings;
u32 DSiTotalROMSize;
u8 DSiShared2Size;
u8 DSiShared3Size;
u8 DSiShared4Size;
u8 DSiShared5Size;
// ???
u32 DSiARM9iParamTableOffset;
u32 DSiARM7iParamTableOffset;
u32 DSiModcrypt1Offset;
u32 DSiModcrypt1Size;
u32 DSiModcrypt2Offset;
u32 DSiModcrypt2Size;
u32 DSiTitleIDLow;
u32 DSiTitleIDHigh;
u32 DSiPublicSavSize;
u32 DSiPrivateSavSize;
u8 Reserved8[176];
u8 DSiAgeRatingFlags[16];
// 0x300 - hashes (SHA1-HMAC)
u8 DSiARM9Hash[20];
u8 DSiARM7Hash[20];
u8 DSiDigestMasterHash[20];
u8 BannerHash[20];
u8 DSiARM9iHash[20];
u8 DSiARM7iHash[20];
u8 HeaderBinariesHash[20]; // 0x160-byte header + ARM9/ARM7 binaries
u8 ARM9OverlayHash[20]; // ARM9 overlay and NitroFAT
u8 DSiARM9NoSecureHash[20]; // ARM9 binary without secure area
u8 Reserved9[2636];
// reserved and unchecked region at 0xE00
u8 Reserved10[384];
u8 HeaderSignature[128]; // RSA-SHA1 across 0x000..0xDFF
};
static_assert(sizeof(NDSHeader) == 4096, "NDSHeader is not 4096 bytes!");
struct NDSBanner
{
u16 Version;
u16 CRC16[4];
u8 Reserved1[22];
u8 Icon[512];
u16 Palette[16];
char16_t JapaneseTitle[128];
char16_t EnglishTitle[128];
char16_t FrenchTitle[128];
char16_t GermanTitle[128];
char16_t ItalianTitle[128];
char16_t SpanishTitle[128];
char16_t ChineseTitle[128];
char16_t KoreanTitle[128];
u8 Reserved2[2048];
u8 DSiIcon[8][512];
u16 DSiPalette[8][16];
u16 DSiSequence[64];
};
static_assert(sizeof(NDSBanner) == 9152, "NDSBanner is not 9152 bytes!");
#endif //NDS_HEADER_H

View File

@ -21,6 +21,7 @@
#include <stdlib.h>
#include "Config.h"
#include "NDS.h"
#include "DSi.h"
#include "SPI.h"
#include "DSi_SPI_TSC.h"
#include "Platform.h"
@ -167,36 +168,32 @@ void Reset()
UserSettings = userdata;
// TODO evetually: do this in DSi mode
if (NDS::ConsoleType == 0)
// fix touchscreen coords
*(u16*)&Firmware[userdata+0x58] = 0;
*(u16*)&Firmware[userdata+0x5A] = 0;
Firmware[userdata+0x5C] = 0;
Firmware[userdata+0x5D] = 0;
*(u16*)&Firmware[userdata+0x5E] = 255<<4;
*(u16*)&Firmware[userdata+0x60] = 191<<4;
Firmware[userdata+0x62] = 255;
Firmware[userdata+0x63] = 191;
// disable autoboot
//Firmware[userdata+0x64] &= 0xBF;
*(u16*)&Firmware[userdata+0x72] = CRC16(&Firmware[userdata], 0x70, 0xFFFF);
if (Config::RandomizeMAC)
{
// fix touchscreen coords
*(u16*)&Firmware[userdata+0x58] = 0;
*(u16*)&Firmware[userdata+0x5A] = 0;
Firmware[userdata+0x5C] = 0;
Firmware[userdata+0x5D] = 0;
*(u16*)&Firmware[userdata+0x5E] = 255<<4;
*(u16*)&Firmware[userdata+0x60] = 191<<4;
Firmware[userdata+0x62] = 255;
Firmware[userdata+0x63] = 191;
// replace MAC address with random address
Firmware[0x36] = 0x00;
Firmware[0x37] = 0x09;
Firmware[0x38] = 0xBF;
Firmware[0x39] = rand()&0xFF;
Firmware[0x3A] = rand()&0xFF;
Firmware[0x3B] = rand()&0xFF;
// disable autoboot
//Firmware[userdata+0x64] &= 0xBF;
*(u16*)&Firmware[userdata+0x72] = CRC16(&Firmware[userdata], 0x70, 0xFFFF);
if (Config::RandomizeMAC)
{
// replace MAC address with random address
Firmware[0x36] = 0x00;
Firmware[0x37] = 0x09;
Firmware[0x38] = 0xBF;
Firmware[0x39] = rand()&0xFF;
Firmware[0x3A] = rand()&0xFF;
Firmware[0x3B] = rand()&0xFF;
*(u16*)&Firmware[0x2A] = CRC16(&Firmware[0x2C], *(u16*)&Firmware[0x2C], 0x0000);
}
*(u16*)&Firmware[0x2A] = CRC16(&Firmware[0x2C], *(u16*)&Firmware[0x2C], 0x0000);
}
printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
@ -233,16 +230,30 @@ void DoSavestate(Savestate* file)
file->Var32(&Addr);
}
void SetupDirectBoot()
void SetupDirectBoot(bool dsi)
{
NDS::ARM9Write32(0x027FF864, 0);
NDS::ARM9Write32(0x027FF868, *(u16*)&Firmware[0x20] << 3);
if (dsi)
{
for (u32 i = 0; i < 6; i += 2)
DSi::ARM9Write16(0x02FFFCF4, *(u16*)&Firmware[0x36+i]); // MAC address
NDS::ARM9Write16(0x027FF874, *(u16*)&Firmware[0x26]);
NDS::ARM9Write16(0x027FF876, *(u16*)&Firmware[0x04]);
// checkme
DSi::ARM9Write16(0x02FFFCFA, *(u16*)&Firmware[0x3C]); // enabled channels
for (u32 i = 0; i < 0x70; i += 4)
NDS::ARM9Write32(0x027FFC80+i, *(u32*)&Firmware[UserSettings+i]);
for (u32 i = 0; i < 0x70; i += 4)
DSi::ARM9Write32(0x02FFFC80+i, *(u32*)&Firmware[UserSettings+i]);
}
else
{
NDS::ARM9Write32(0x027FF864, 0);
NDS::ARM9Write32(0x027FF868, *(u16*)&Firmware[0x20] << 3); // user settings offset
NDS::ARM9Write16(0x027FF874, *(u16*)&Firmware[0x26]); // CRC16 for data/gfx
NDS::ARM9Write16(0x027FF876, *(u16*)&Firmware[0x04]); // CRC16 for GUI/wifi code
for (u32 i = 0; i < 0x70; i += 4)
NDS::ARM9Write32(0x027FFC80+i, *(u32*)&Firmware[UserSettings+i]);
}
}
u8 GetConsoleType() { return Firmware[0x1D]; }

View File

@ -24,7 +24,7 @@
namespace SPI_Firmware
{
void SetupDirectBoot();
void SetupDirectBoot(bool dsi);
u32 FixFirmwareLength(u32 originalLength);

View File

@ -18,6 +18,7 @@
#include <stdio.h>
#include <string.h>
#include <cmath>
#include "Platform.h"
#include "NDS.h"
#include "DSi.h"
@ -27,7 +28,6 @@
// SPU TODO
// * capture addition modes, overflow bugs
// * channel hold
// * 'length less than 4' glitch
namespace SPU
{
@ -62,6 +62,12 @@ const s16 PSGTable[8][8] =
{-0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF}
};
// audio interpolation is an improvement upon the original hardware
// (which performs no interpolation)
int InterpType;
s16 InterpCos[0x100];
s16 InterpCubic[0x100][4];
const u32 OutputBufferSize = 2*2048;
s16 OutputBackbuffer[2 * OutputBufferSize];
u32 OutputBackbufferWritePosition;
@ -75,6 +81,8 @@ Platform::Mutex* AudioLock;
u16 Cnt;
u8 MasterVolume;
u16 Bias;
bool ApplyBias;
bool Degrade10Bit;
Channel* Channels[16];
CaptureUnit* Capture[2];
@ -90,6 +98,32 @@ bool Init()
AudioLock = Platform::Mutex_Create();
InterpType = 0;
// generate interpolation tables
// values are 1:1:14 fixed-point
float m_pi = std::acos(-1.0f);
for (int i = 0; i < 0x100; i++)
{
float ratio = (i * m_pi) / 255.0f;
ratio = 1.0f - std::cos(ratio);
InterpCos[i] = (s16)(ratio * 0x2000);
}
for (int i = 0; i < 0x100; i++)
{
s32 i1 = i << 6;
s32 i2 = (i * i) >> 2;
s32 i3 = (i * i * i) >> 10;
InterpCubic[i][0] = -i3 + 2*i2 - i1;
InterpCubic[i][1] = i3 - 2*i2 + 0x4000;
InterpCubic[i][2] = -i3 + i2 + i1;
InterpCubic[i][3] = i3 - i2;
}
return true;
}
@ -148,11 +182,26 @@ void DoSavestate(Savestate* file)
}
void SetInterpolation(int type)
{
InterpType = type;
}
void SetBias(u16 bias)
{
Bias = bias;
}
void SetApplyBias(bool enable)
{
ApplyBias = enable;
}
void SetDegrade10Bit(bool enable)
{
Degrade10Bit = enable;
}
Channel::Channel(u32 num)
{
@ -202,6 +251,7 @@ void Channel::DoSavestate(Savestate* file)
file->Var8((u8*)&KeyOn);
file->Var32(&Timer);
file->Var32((u32*)&Pos);
file->VarArray(PrevSample, sizeof(PrevSample));
file->Var16((u16*)&CurSample);
file->Var16(&NoiseVal);
@ -215,7 +265,7 @@ void Channel::DoSavestate(Savestate* file)
file->Var32(&FIFOWritePos);
file->Var32(&FIFOReadOffset);
file->Var32(&FIFOLevel);
file->VarArray(FIFO, 8*4);
file->VarArray(FIFO, sizeof(FIFO));
}
void Channel::FIFO_BufferData()
@ -269,6 +319,9 @@ void Channel::Start()
Pos = -3;
NoiseVal = 0x7FFF;
PrevSample[0] = 0;
PrevSample[1] = 0;
PrevSample[2] = 0;
CurSample = 0;
FIFOReadPos = 0;
@ -444,6 +497,16 @@ s32 Channel::Run()
{
Timer = TimerReload + (Timer - 0x10000);
// for optional interpolation: save previous samples
// the interpolated audio will be delayed by a couple samples,
// but it's easier to deal with this way
if ((type < 3) && (InterpType != 0))
{
PrevSample[2] = PrevSample[1];
PrevSample[1] = PrevSample[0];
PrevSample[0] = CurSample;
}
switch (type)
{
case 0: NextSample_PCM8(); break;
@ -455,6 +518,34 @@ s32 Channel::Run()
}
s32 val = (s32)CurSample;
// interpolation (emulation improvement, not a hardware feature)
if ((type < 3) && (InterpType != 0))
{
s32 samplepos = ((Timer - TimerReload) * 0x100) / (0x10000 - TimerReload);
if (samplepos > 0xFF) samplepos = 0xFF;
switch (InterpType)
{
case 1: // linear
val = ((val * samplepos) +
(PrevSample[0] * (0xFF-samplepos))) >> 8;
break;
case 2: // cosine
val = ((val * InterpCos[samplepos]) +
(PrevSample[0] * InterpCos[0xFF-samplepos])) >> 14;
break;
case 3: // cubic
val = ((PrevSample[2] * InterpCubic[samplepos][0]) +
(PrevSample[1] * InterpCubic[samplepos][1]) +
(PrevSample[0] * InterpCubic[samplepos][2]) +
(val * InterpCubic[samplepos][3])) >> 14;
break;
}
}
val <<= VolumeShift;
val *= Volume;
return val;
@ -710,12 +801,28 @@ void Mix(u32 dummy)
rightoutput = ((s64)rightoutput * MasterVolume) >> 7;
leftoutput >>= 8;
rightoutput >>= 8;
// Add SOUNDBIAS value
// The value used by all commercial games is 0x200, so we subtract that so it won't offset the final sound output.
if (ApplyBias)
{
leftoutput += (Bias << 6) - 0x8000;
rightoutput += (Bias << 6) - 0x8000;
}
if (leftoutput < -0x8000) leftoutput = -0x8000;
else if (leftoutput > 0x7FFF) leftoutput = 0x7FFF;
rightoutput >>= 8;
if (rightoutput < -0x8000) rightoutput = -0x8000;
else if (rightoutput > 0x7FFF) rightoutput = 0x7FFF;
// The original DS and DS lite degrade the output from 16 to 10 bit before output
if (Degrade10Bit)
{
leftoutput &= 0xFFFFFFC0;
rightoutput &= 0xFFFFFFC0;
}
// OutputBufferFrame can never get full because it's
// transfered to OutputBuffer at the end of the frame
OutputBackbuffer[OutputBackbufferWritePosition ] = leftoutput >> 1;

View File

@ -31,7 +31,12 @@ void Stop();
void DoSavestate(Savestate* file);
// 0=none 1=linear 2=cosine 3=cubic
void SetInterpolation(int type);
void SetBias(u16 bias);
void SetDegrade10Bit(bool enable);
void SetApplyBias(bool enable);
void Mix(u32 dummy);
@ -73,6 +78,7 @@ public:
bool KeyOn;
u32 Timer;
s32 Pos;
s16 PrevSample[3];
s16 CurSample;
u16 NoiseVal;

View File

@ -1607,7 +1607,21 @@ void ARM64XEmitter::BICS(ARM64Reg Rd, ARM64Reg Rn, ARM64Reg Rm, ArithOption Shif
void ARM64XEmitter::MOV(ARM64Reg Rd, ARM64Reg Rm, ArithOption Shift)
{
ORR(Rd, Is64Bit(Rd) ? ZR : WZR, Rm, Shift);
if (Shift.GetType() == ArithOption::TYPE_SHIFTEDREG)
{
switch (Shift.GetShiftType())
{
case ST_LSL: LSL(Rd, Rm, Shift.GetShiftAmount()); break;
case ST_LSR: LSR(Rd, Rm, Shift.GetShiftAmount()); break;
case ST_ASR: ASR(Rd, Rm, Shift.GetShiftAmount()); break;
case ST_ROR: ROR(Rd, Rm, Shift.GetShiftAmount()); break;
default: ASSERT_MSG(DYNA_REC, false, "Invalid shift type"); break;
}
}
else
{
ORR(Rd, Is64Bit(Rd) ? ZR : WZR, Rm, Shift);
}
}
void ARM64XEmitter::MOV(ARM64Reg Rd, ARM64Reg Rm)

View File

@ -469,6 +469,8 @@ public:
}
TypeSpecifier GetType() const { return m_type; }
ARM64Reg GetReg() const { return m_destReg; }
ShiftType GetShiftType() const { return m_shifttype; }
u32 GetShiftAmount() const { return m_shift; }
u32 GetData() const
{
switch (m_type)

359
src/fatfs/00history.txt Normal file
View File

@ -0,0 +1,359 @@
----------------------------------------------------------------------------
Revision history of FatFs module
----------------------------------------------------------------------------
R0.00 (February 26, 2006)
Prototype.
R0.01 (April 29, 2006)
The first release.
R0.02 (June 01, 2006)
Added FAT12 support.
Removed unbuffered mode.
Fixed a problem on small (<32M) partition.
R0.02a (June 10, 2006)
Added a configuration option (_FS_MINIMUM).
R0.03 (September 22, 2006)
Added f_rename().
Changed option _FS_MINIMUM to _FS_MINIMIZE.
R0.03a (December 11, 2006)
Improved cluster scan algorithm to write files fast.
Fixed f_mkdir() creates incorrect directory on FAT32.
R0.04 (February 04, 2007)
Added f_mkfs().
Supported multiple drive system.
Changed some interfaces for multiple drive system.
Changed f_mountdrv() to f_mount().
R0.04a (April 01, 2007)
Supported multiple partitions on a physical drive.
Added a capability of extending file size to f_lseek().
Added minimization level 3.
Fixed an endian sensitive code in f_mkfs().
R0.04b (May 05, 2007)
Added a configuration option _USE_NTFLAG.
Added FSINFO support.
Fixed DBCS name can result FR_INVALID_NAME.
Fixed short seek (<= csize) collapses the file object.
R0.05 (August 25, 2007)
Changed arguments of f_read(), f_write() and f_mkfs().
Fixed f_mkfs() on FAT32 creates incorrect FSINFO.
Fixed f_mkdir() on FAT32 creates incorrect directory.
R0.05a (February 03, 2008)
Added f_truncate() and f_utime().
Fixed off by one error at FAT sub-type determination.
Fixed btr in f_read() can be mistruncated.
Fixed cached sector is not flushed when create and close without write.
R0.06 (April 01, 2008)
Added fputc(), fputs(), fprintf() and fgets().
Improved performance of f_lseek() on moving to the same or following cluster.
R0.07 (April 01, 2009)
Merged Tiny-FatFs as a configuration option. (_FS_TINY)
Added long file name feature. (_USE_LFN)
Added multiple code page feature. (_CODE_PAGE)
Added re-entrancy for multitask operation. (_FS_REENTRANT)
Added auto cluster size selection to f_mkfs().
Added rewind option to f_readdir().
Changed result code of critical errors.
Renamed string functions to avoid name collision.
R0.07a (April 14, 2009)
Septemberarated out OS dependent code on reentrant cfg.
Added multiple sector size feature.
R0.07c (June 21, 2009)
Fixed f_unlink() can return FR_OK on error.
Fixed wrong cache control in f_lseek().
Added relative path feature.
Added f_chdir() and f_chdrive().
Added proper case conversion to extended character.
R0.07e (November 03, 2009)
Septemberarated out configuration options from ff.h to ffconf.h.
Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.
Fixed name matching error on the 13 character boundary.
Added a configuration option, _LFN_UNICODE.
Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
R0.08 (May 15, 2010)
Added a memory configuration option. (_USE_LFN = 3)
Added file lock feature. (_FS_SHARE)
Added fast seek feature. (_USE_FASTSEEK)
Changed some types on the API, XCHAR->TCHAR.
Changed .fname in the FILINFO structure on Unicode cfg.
String functions support UTF-8 encoding files on Unicode cfg.
R0.08a (August 16, 2010)
Added f_getcwd(). (_FS_RPATH = 2)
Added sector erase feature. (_USE_ERASE)
Moved file lock semaphore table from fs object to the bss.
Fixed f_mkfs() creates wrong FAT32 volume.
R0.08b (January 15, 2011)
Fast seek feature is also applied to f_read() and f_write().
f_lseek() reports required table size on creating CLMP.
Extended format syntax of f_printf().
Ignores duplicated directory separators in given path name.
R0.09 (September 06, 2011)
f_mkfs() supports multiple partition to complete the multiple partition feature.
Added f_fdisk().
R0.09a (August 27, 2012)
Changed f_open() and f_opendir() reject null object pointer to avoid crash.
Changed option name _FS_SHARE to _FS_LOCK.
Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
R0.09b (January 24, 2013)
Added f_setlabel() and f_getlabel().
R0.10 (October 02, 2013)
Added selection of character encoding on the file. (_STRF_ENCODE)
Added f_closedir().
Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
Added forced mount feature with changes of f_mount().
Improved behavior of volume auto detection.
Improved write throughput of f_puts() and f_printf().
Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
Fixed f_write() can be truncated when the file size is close to 4GB.
Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error.
R0.10a (January 15, 2014)
Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
Added a configuration option of minimum sector size. (_MIN_SS)
2nd argument of f_rename() can have a drive number and it will be ignored.
Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10)
Fixed f_close() invalidates the file object without volume lock.
Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10)
Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07)
R0.10b (May 19, 2014)
Fixed a hard error in the disk I/O layer can collapse the directory entry.
Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07)
R0.10c (November 09, 2014)
Added a configuration option for the platforms without RTC. (_FS_NORTC)
Changed option name _USE_ERASE to _USE_TRIM.
Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b)
Fixed a potential problem of FAT access that can appear on disk error.
Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08)
R0.11 (February 09, 2015)
Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND)
Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c)
Fixed _FS_NORTC option does not work properly. (appeared at R0.10c)
R0.11a (September 05, 2015)
Fixed wrong media change can lead a deadlock at thread-safe configuration.
Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE)
Removed some code pages actually not exist on the standard systems. (_CODE_PAGE)
Fixed errors in the case conversion teble of code page 437 and 850 (ff.c).
Fixed errors in the case conversion teble of Unicode (cc*.c).
R0.12 (April 12, 2016)
Added support for exFAT file system. (_FS_EXFAT)
Added f_expand(). (_USE_EXPAND)
Changed some members in FINFO structure and behavior of f_readdir().
Added an option _USE_CHMOD.
Removed an option _WORD_ACCESS.
Fixed errors in the case conversion table of Unicode (cc*.c).
R0.12a (July 10, 2016)
Added support for creating exFAT volume with some changes of f_mkfs().
Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed.
f_forward() is available regardless of _FS_TINY.
Fixed f_mkfs() creates wrong volume. (appeared at R0.12)
Fixed wrong memory read in create_name(). (appeared at R0.12)
Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD.
R0.12b (September 04, 2016)
Made f_rename() be able to rename objects with the same name but case.
Fixed an error in the case conversion teble of code page 866. (ff.c)
Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12)
Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12)
Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12)
Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12)
Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12)
Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12)
R0.12c (March 04, 2017)
Improved write throughput at the fragmented file on the exFAT volume.
Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN.
Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12)
Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c)
R0.13 (May 21, 2017)
Changed heading character of configuration keywords "_" to "FF_".
Removed ASCII-only configuration, FF_CODE_PAGE = 1. Use FF_CODE_PAGE = 437 instead.
Added f_setcp(), run-time code page configuration. (FF_CODE_PAGE = 0)
Improved cluster allocation time on stretch a deep buried cluster chain.
Improved processing time of f_mkdir() with large cluster size by using FF_USE_LFN = 3.
Improved NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous.
Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12)
Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c)
Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c)
R0.13a (October 14, 2017)
Added support for UTF-8 encoding on the API. (FF_LFN_UNICODE = 2)
Added options for file name output buffer. (FF_LFN_BUF, FF_SFN_BUF).
Added dynamic memory allocation option for working buffer of f_mkfs() and f_fdisk().
Fixed f_fdisk() and f_mkfs() create the partition table with wrong CHS parameters. (appeared at R0.09)
Fixed f_unlink() can cause lost clusters at fragmented file on the exFAT volume. (appeared at R0.12c)
Fixed f_setlabel() rejects some valid characters for exFAT volume. (appeared at R0.12)
R0.13b (April 07, 2018)
Added support for UTF-32 encoding on the API. (FF_LFN_UNICODE = 3)
Added support for Unix style volume ID. (FF_STR_VOLUME_ID = 2)
Fixed accesing any object on the exFAT root directory beyond the cluster boundary can fail. (appeared at R0.12c)
Fixed f_setlabel() does not reject some invalid characters. (appeared at R0.09b)
R0.13c (October 14, 2018)
Supported stdint.h for C99 and later. (integer.h was included in ff.h)
Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12)
Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12)
Fixed f_getcwd() cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b)
R0.14 (October 14, 2019)
Added support for 64-bit LBA and GUID partition table (FF_LBA64 = 1)
Changed some API functions, f_mkfs() and f_fdisk().
Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters.
Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters.
Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12)
Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12)
R0.14a (December 5, 2020)
Limited number of recursive calls in f_findnext().
Fixed old floppy disks formatted with MS-DOS 2.x and 3.x cannot be mounted.
Fixed some compiler warnings.
R0.14b (April 17, 2021)
Made FatFs uses standard library <string.h> for copy, compare and search instead of built-in string functions.
Added support for long long integer and floating point to f_printf(). (FF_STRF_LLI and FF_STRF_FP)
Made path name parser ignore the terminating separator to allow "dir/".
Improved the compatibility in Unix style path name feature.
Fixed the file gets dead-locked when f_open() failed with some conditions. (appeared at R0.12a)
Fixed f_mkfs() can create wrong exFAT volume due to a timing dependent error. (appeared at R0.12)
Fixed code page 855 cannot be set by f_setcp().
Fixed some compiler warnings.

21
src/fatfs/00readme.txt Normal file
View File

@ -0,0 +1,21 @@
FatFs Module Source Files R0.14b
FILES
00readme.txt This file.
00history.txt Revision history.
ff.c FatFs module.
ffconf.h Configuration file of FatFs module.
ff.h Common include file for FatFs and application module.
diskio.h Common include file for FatFs and disk I/O module.
diskio.c An example of glue function to attach existing disk I/O module to FatFs.
ffunicode.c Optional Unicode utility functions.
ffsystem.c An example of optional O/S related functions.
Low level disk I/O module is not included in this archive because the FatFs
module is only a generic file system layer and it does not depend on any specific
storage device. You need to provide a low level disk I/O module written to
control the storage device that attached to the target system.

24
src/fatfs/LICENSE.txt Normal file
View File

@ -0,0 +1,24 @@
FatFs License
FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files.
/*----------------------------------------------------------------------------/
/ FatFs - Generic FAT Filesystem Module Rx.xx /
/-----------------------------------------------------------------------------/
/
/ Copyright (C) 20xx, ChaN, all right reserved.
/
/ FatFs module is an open source software. Redistribution and use of FatFs in
/ source and binary forms, with or without modification, are permitted provided
/ that the following condition is met:
/
/ 1. Redistributions of source code must retain the above copyright notice,
/ this condition and the following disclaimer.
/
/ This software is provided by the copyright holder and contributors "AS IS"
/ and any warranties related to this software are DISCLAIMED.
/ The copyright owner or contributors be NOT LIABLE for any damages caused
/ by use of this software.
/----------------------------------------------------------------------------*/
Therefore FatFs license is one of the BSD-style licenses, but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, do not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses include GNU GPL. When you redistribute the FatFs source code with changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license.

134
src/fatfs/diskio.c Normal file
View File

@ -0,0 +1,134 @@
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
static ff_disk_read_cb ReadCb;
static ff_disk_write_cb WriteCb;
static DSTATUS Status = STA_NOINIT | STA_NODISK;
void ff_disk_open(ff_disk_read_cb readcb, ff_disk_write_cb writecb)
{
if (!readcb) return;
ReadCb = readcb;
WriteCb = writecb;
Status &= ~STA_NODISK;
if (!writecb) Status |= STA_PROTECT;
else Status &= ~STA_PROTECT;
}
void ff_disk_close()
{
ReadCb = (void*)0;
WriteCb = (void*)0;
Status &= ~STA_PROTECT;
Status |= STA_NODISK;
}
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
return Status;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
Status &= ~STA_NOINIT;
return Status;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
if (Status & (STA_NOINIT | STA_NODISK)) return RES_NOTRDY;
if (!ReadCb) return RES_ERROR;
UINT res = ReadCb(buff, sector, count);
if (res != count) return RES_ERROR;
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
if (Status & (STA_NOINIT | STA_NODISK)) return RES_NOTRDY;
if (Status & STA_PROTECT) return RES_WRPRT;
if (!WriteCb) return RES_ERROR;
UINT res = WriteCb(buff, sector, count);
if (res != count) return RES_ERROR;
return RES_OK;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
switch (cmd)
{
case 0: // sync
// TODO: fflush?
return RES_OK;
}
//printf("disk_ioctl(%02X, %02X, %p)\n", pdrv, cmd, buff);
return RES_PARERR;
}

77
src/fatfs/diskio.h Normal file
View File

@ -0,0 +1,77 @@
/*-----------------------------------------------------------------------/
/ Low level disk interface modlue include file (C)ChaN, 2019 /
/-----------------------------------------------------------------------*/
#ifndef _DISKIO_DEFINED
#define _DISKIO_DEFINED
#ifdef __cplusplus
extern "C" {
#endif
/* Status of Disk Functions */
typedef BYTE DSTATUS;
/* Results of Disk Functions */
typedef enum {
RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */
} DRESULT;
/*---------------------------------------*/
/* Prototypes for disk control functions */
DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
/* Disk Status Bits (DSTATUS) */
#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */
/* Command code for disk_ioctrl fucntion */
/* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */
/* Generic command (Not used by FatFs) */
#define CTRL_POWER 5 /* Get/Set power status */
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
#define CTRL_EJECT 7 /* Eject media */
#define CTRL_FORMAT 8 /* Create physical format on the media */
/* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE 10 /* Get card type */
#define MMC_GET_CSD 11 /* Get CSD */
#define MMC_GET_CID 12 /* Get CID */
#define MMC_GET_OCR 13 /* Get OCR */
#define MMC_GET_SDSTAT 14 /* Get SD status */
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
/* ATA/CF specific ioctl command */
#define ATA_GET_REV 20 /* Get F/W revision */
#define ATA_GET_MODEL 21 /* Get model name */
#define ATA_GET_SN 22 /* Get serial number */
#ifdef __cplusplus
}
#endif
#endif

6982
src/fatfs/ff.c Normal file

File diff suppressed because it is too large Load Diff

431
src/fatfs/ff.h Normal file
View File

@ -0,0 +1,431 @@
/*----------------------------------------------------------------------------/
/ FatFs - Generic FAT Filesystem module R0.14b /
/-----------------------------------------------------------------------------/
/
/ Copyright (C) 2021, ChaN, all right reserved.
/
/ FatFs module is an open source software. Redistribution and use of FatFs in
/ source and binary forms, with or without modification, are permitted provided
/ that the following condition is met:
/ 1. Redistributions of source code must retain the above copyright notice,
/ this condition and the following disclaimer.
/
/ This software is provided by the copyright holder and contributors "AS IS"
/ and any warranties related to this software are DISCLAIMED.
/ The copyright owner or contributors be NOT LIABLE for any damages caused
/ by use of this software.
/
/----------------------------------------------------------------------------*/
#ifndef FF_DEFINED
#define FF_DEFINED 86631 /* Revision ID */
#ifdef __cplusplus
extern "C" {
#endif
#include "ffconf.h" /* FatFs configuration options */
#if FF_DEFINED != FFCONF_DEF
#error Wrong configuration file (ffconf.h).
#endif
/* Integer types used for FatFs API */
#if defined(_WIN32) /* Windows VC++ (for development only) */
#define FF_INTDEF 2
#include <windows.h>
typedef unsigned __int64 QWORD;
#include <float.h>
#define isnan(v) _isnan(v)
#define isinf(v) (!_finite(v))
#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */
#define FF_INTDEF 2
#include <stdint.h>
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */
typedef uint16_t WORD; /* 16-bit unsigned integer */
typedef uint32_t DWORD; /* 32-bit unsigned integer */
typedef uint64_t QWORD; /* 64-bit unsigned integer */
typedef WORD WCHAR; /* UTF-16 character type */
#else /* Earlier than C99 */
#define FF_INTDEF 1
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */
typedef unsigned short WORD; /* 16-bit unsigned integer */
typedef unsigned long DWORD; /* 32-bit unsigned integer */
typedef WORD WCHAR; /* UTF-16 character type */
#endif
/* Type of file size and LBA variables */
#if FF_FS_EXFAT
#if FF_INTDEF != 2
#error exFAT feature wants C99 or later
#endif
typedef QWORD FSIZE_t;
#if FF_LBA64
typedef QWORD LBA_t;
#else
typedef DWORD LBA_t;
#endif
#else
#if FF_LBA64
#error exFAT needs to be enabled when enable 64-bit LBA
#endif
typedef DWORD FSIZE_t;
typedef DWORD LBA_t;
#endif
/* Type of path name strings on FatFs API (TCHAR) */
#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
typedef WCHAR TCHAR;
#define _T(x) L ## x
#define _TEXT(x) L ## x
#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
typedef char TCHAR;
#define _T(x) u8 ## x
#define _TEXT(x) u8 ## x
#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
typedef DWORD TCHAR;
#define _T(x) U ## x
#define _TEXT(x) U ## x
#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
#error Wrong FF_LFN_UNICODE setting
#else /* ANSI/OEM code in SBCS/DBCS */
typedef char TCHAR;
#define _T(x) x
#define _TEXT(x) x
#endif
/* Definitions of volume management */
#if FF_MULTI_PARTITION /* Multiple partition configuration */
typedef struct {
BYTE pd; /* Physical drive number */
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
} PARTITION;
extern PARTITION VolToPart[]; /* Volume - Partition mapping table */
#endif
#if FF_STR_VOLUME_ID
#ifndef FF_VOLUME_STRS
extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */
#endif
#endif
/* Filesystem object structure (FATFS) */
typedef struct {
BYTE fs_type; /* Filesystem type (0:not mounted) */
BYTE pdrv; /* Associated physical drive */
BYTE n_fats; /* Number of FATs (1 or 2) */
BYTE wflag; /* win[] flag (b0:dirty) */
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
WORD id; /* Volume mount ID */
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
WORD csize; /* Cluster size [sectors] */
#if FF_MAX_SS != FF_MIN_SS
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
#endif
#if FF_USE_LFN
WCHAR* lfnbuf; /* LFN working buffer */
#endif
#if FF_FS_EXFAT
BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
#endif
#if FF_FS_REENTRANT
FF_SYNC_t sobj; /* Identifier of sync object */
#endif
#if !FF_FS_READONLY
DWORD last_clst; /* Last allocated cluster */
DWORD free_clst; /* Number of free clusters */
#endif
#if FF_FS_RPATH
DWORD cdir; /* Current directory start cluster (0:root) */
#if FF_FS_EXFAT
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
#endif
#endif
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
DWORD fsize; /* Size of an FAT [sectors] */
LBA_t volbase; /* Volume base sector */
LBA_t fatbase; /* FAT base sector */
LBA_t dirbase; /* Root directory base sector/cluster */
LBA_t database; /* Data base sector */
#if FF_FS_EXFAT
LBA_t bitbase; /* Allocation bitmap base sector */
#endif
LBA_t winsect; /* Current sector appearing in the win[] */
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
} FATFS;
/* Object ID and allocation information (FFOBJID) */
typedef struct {
FATFS* fs; /* Pointer to the hosting volume of this object */
WORD id; /* Hosting volume mount ID */
BYTE attr; /* Object attribute */
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
#if FF_FS_EXFAT
DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */
#endif
#if FF_FS_LOCK
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
#endif
} FFOBJID;
/* File object structure (FIL) */
typedef struct {
FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
BYTE flag; /* File status flags */
BYTE err; /* Abort flag (error code) */
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */
#if !FF_FS_READONLY
LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */
#endif
#if FF_USE_FASTSEEK
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
#endif
#if !FF_FS_TINY
BYTE buf[FF_MAX_SS]; /* File private data read/write window */
#endif
} FIL;
/* Directory object structure (DIR) */
typedef struct {
FFOBJID obj; /* Object identifier */
DWORD dptr; /* Current read/write offset */
DWORD clust; /* Current cluster */
LBA_t sect; /* Current sector (0:Read operation has terminated) */
BYTE* dir; /* Pointer to the directory item in the win[] */
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
#if FF_USE_LFN
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
#endif
#if FF_USE_FIND
const TCHAR* pat; /* Pointer to the name matching pattern */
#endif
} DIR;
/* File information structure (FILINFO) */
typedef struct {
FSIZE_t fsize; /* File size */
WORD fdate; /* Modified date */
WORD ftime; /* Modified time */
BYTE fattrib; /* File attribute */
#if FF_USE_LFN
TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */
TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
#else
TCHAR fname[12 + 1]; /* File name */
#endif
} FILINFO;
/* Format parameter structure (MKFS_PARM) */
typedef struct {
BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
BYTE n_fat; /* Number of FATs */
UINT align; /* Data area alignment (sector) */
UINT n_root; /* Number of root directory entries */
DWORD au_size; /* Cluster size (byte) */
} MKFS_PARM;
/* File function return code (FRESULT) */
typedef enum {
FR_OK = 0, /* (0) Succeeded */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
FR_INT_ERR, /* (2) Assertion failed */
FR_NOT_READY, /* (3) The physical drive cannot work */
FR_NO_FILE, /* (4) Could not find the file */
FR_NO_PATH, /* (5) Could not find the path */
FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
FR_NOT_ENABLED, /* (12) The volume has no work area */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
} FRESULT;
/*--------------------------------------------------------------*/
/* FatFs module application interface */
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
FRESULT f_close (FIL* fp); /* Close an open file object */
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */
FRESULT f_truncate (FIL* fp); /* Truncate the file */
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
FRESULT f_closedir (DIR* dp); /* Close an open directory */
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */
FRESULT f_chdir (const TCHAR* path); /* Change current directory */
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */
FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */
FRESULT f_setcp (WORD cp); /* Set current code page */
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
#define f_error(fp) ((fp)->err)
#define f_tell(fp) ((fp)->fptr)
#define f_size(fp) ((fp)->obj.objsize)
#define f_rewind(fp) f_lseek((fp), 0)
#define f_rewinddir(dp) f_readdir((dp), 0)
#define f_rmdir(path) f_unlink(path)
#define f_unmount(path) f_mount(0, path, 0)
/*--------------------------------------------------------------*/
/* Additional user defined functions */
/* RTC function */
#if !FF_FS_READONLY && !FF_FS_NORTC
DWORD get_fattime (void);
#endif
/* LFN support functions */
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */
DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */
#endif
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
void* ff_memalloc (UINT msize); /* Allocate memory block */
void ff_memfree (void* mblock); /* Free memory block */
#endif
/* Sync functions */
#if FF_FS_REENTRANT
int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */
int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */
void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */
int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */
#endif
/*--------------------------------------------------------------*/
/* Flags and offset address */
/* File access mode and open method flags (3rd argument of f_open) */
#define FA_READ 0x01
#define FA_WRITE 0x02
#define FA_OPEN_EXISTING 0x00
#define FA_CREATE_NEW 0x04
#define FA_CREATE_ALWAYS 0x08
#define FA_OPEN_ALWAYS 0x10
#define FA_OPEN_APPEND 0x30
/* Fast seek controls (2nd argument of f_lseek) */
#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
/* Format options (2nd argument of f_mkfs) */
#define FM_FAT 0x01
#define FM_FAT32 0x02
#define FM_EXFAT 0x04
#define FM_ANY 0x07
#define FM_SFD 0x08
/* Filesystem type (FATFS.fs_type) */
#define FS_FAT12 1
#define FS_FAT16 2
#define FS_FAT32 3
#define FS_EXFAT 4
/* File attribute bits for directory entry (FILINFO.fattrib) */
#define AM_RDO 0x01 /* Read only */
#define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */
#define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */
// extra additions for interfacing with melonDS
typedef UINT (*ff_disk_read_cb)(BYTE* buff, LBA_t sector, UINT count);
typedef UINT (*ff_disk_write_cb)(BYTE* buff, LBA_t sector, UINT count);
void ff_disk_open(ff_disk_read_cb readcb, ff_disk_write_cb writecb);
void ff_disk_close();
#ifdef __cplusplus
}
#endif
#endif /* FF_DEFINED */

301
src/fatfs/ffconf.h Normal file
View File

@ -0,0 +1,301 @@
/*---------------------------------------------------------------------------/
/ FatFs Functional Configurations
/---------------------------------------------------------------------------*/
#define FFCONF_DEF 86631 /* Revision ID */
/*---------------------------------------------------------------------------/
/ Function Configurations
/---------------------------------------------------------------------------*/
#define FF_FS_READONLY 0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
/ and optional writing functions as well. */
#define FF_FS_MINIMIZE 0
/* This option defines minimization level to remove some basic API functions.
/
/ 0: Basic functions are fully enabled.
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
/ are removed.
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 3: f_lseek() function is removed in addition to 2. */
#define FF_USE_FIND 0
/* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
#define FF_USE_MKFS 1
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define FF_USE_FASTSEEK 0
/* This option switches fast seek function. (0:Disable or 1:Enable) */
#define FF_USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */
#define FF_USE_CHMOD 1
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
#define FF_USE_LABEL 0
/* This option switches volume label functions, f_getlabel() and f_setlabel().
/ (0:Disable or 1:Enable) */
#define FF_USE_FORWARD 0
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
#define FF_USE_STRFUNC 0
#define FF_PRINT_LLI 0
#define FF_PRINT_FLOAT 0
#define FF_STRF_ENCODE 0
/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and
/ f_printf().
/
/ 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect.
/ 1: Enable without LF-CRLF conversion.
/ 2: Enable with LF-CRLF conversion.
/
/ FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2
makes f_printf() support floating point argument. These features want C99 or later.
/ When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character
/ encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE
/ to be read/written via those functions.
/
/ 0: ANSI/OEM in current CP
/ 1: Unicode in UTF-16LE
/ 2: Unicode in UTF-16BE
/ 3: Unicode in UTF-8
*/
/*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations
/---------------------------------------------------------------------------*/
#define FF_CODE_PAGE 932
/* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure.
/
/ 437 - U.S.
/ 720 - Arabic
/ 737 - Greek
/ 771 - KBL
/ 775 - Baltic
/ 850 - Latin 1
/ 852 - Latin 2
/ 855 - Cyrillic
/ 857 - Turkish
/ 860 - Portuguese
/ 861 - Icelandic
/ 862 - Hebrew
/ 863 - Canadian French
/ 864 - Arabic
/ 865 - Nordic
/ 866 - Russian
/ 869 - Greek 2
/ 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS)
/ 950 - Traditional Chinese (DBCS)
/ 0 - Include all code pages above and configured by f_setcp()
*/
#define FF_USE_LFN 1
#define FF_MAX_LFN 255
/* The FF_USE_LFN switches the support for LFN (long file name).
/
/ 0: Disable LFN. FF_MAX_LFN has no effect.
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
/ 2: Enable LFN with dynamic working buffer on the STACK.
/ 3: Enable LFN with dynamic working buffer on the HEAP.
/
/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
/ requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
/ additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
/ The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
/ be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
/ specification.
/ When use stack for the working buffer, take care on stack overflow. When use heap
/ memory for the working buffer, memory management functions, ff_memalloc() and
/ ff_memfree() exemplified in ffsystem.c, need to be added to the project. */
#define FF_LFN_UNICODE 2
/* This option switches the character encoding on the API when LFN is enabled.
/
/ 0: ANSI/OEM in current CP (TCHAR = char)
/ 1: Unicode in UTF-16 (TCHAR = WCHAR)
/ 2: Unicode in UTF-8 (TCHAR = char)
/ 3: Unicode in UTF-32 (TCHAR = DWORD)
/
/ Also behavior of string I/O functions will be affected by this option.
/ When LFN is not enabled, this option has no effect. */
#define FF_LFN_BUF 255
#define FF_SFN_BUF 12
/* This set of options defines size of file name members in the FILINFO structure
/ which is used to read out directory items. These values should be suffcient for
/ the file names to read. The maximum possible length of the read file name depends
/ on character encoding. When LFN is not enabled, these options have no effect. */
#define FF_FS_RPATH 0
/* This option configures support for relative path.
/
/ 0: Disable relative path and remove related functions.
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1.
*/
/*---------------------------------------------------------------------------/
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/
#define FF_VOLUMES 1
/* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 0
#define FF_VOLUME_STRS "fat"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
/ logical drives. Number of items must not be less than FF_VOLUMES. Valid
/ characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
/ compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
/ not defined, a user defined volume string table needs to be defined as:
/
/ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
*/
#define FF_MULTI_PARTITION 0
/* This option switches support for multiple volumes on the physical drive.
/ By default (0), each logical drive number is bound to the same physical drive
/ number and only an FAT volume found on the physical drive will be mounted.
/ When this function is enabled (1), each logical drive number can be bound to
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
/ funciton will be available. */
#define FF_MIN_SS 512
#define FF_MAX_SS 512
/* This set of options configures the range of sector size to be supported. (512,
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
/ harddisk, but a larger value may be required for on-board flash memory and some
/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
/ for variable sector size mode and disk_ioctl() function needs to implement
/ GET_SECTOR_SIZE command. */
#define FF_LBA64 0
/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
/ To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */
#define FF_MIN_GPT 0x10000000
/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and
/ f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */
#define FF_USE_TRIM 0
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
/ disk_ioctl() function. */
/*---------------------------------------------------------------------------/
/ System Configurations
/---------------------------------------------------------------------------*/
#define FF_FS_TINY 0
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
/ Instead of private sector buffer eliminated from the file object, common sector
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */
#define FF_FS_EXFAT 0
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
#define FF_FS_NORTC 0
#define FF_NORTC_MON 1
#define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2020
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
/ added to the project to read current time form real-time clock. FF_NORTC_MON,
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
/ These options have no effect in read-only configuration (FF_FS_READONLY = 1). */
#define FF_FS_NOFSINFO 0
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
/ option, and f_getfree() function at first time after volume mount will force
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
/
/ bit0=0: Use free cluster count in the FSINFO if available.
/ bit0=1: Do not trust free cluster count in the FSINFO.
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
*/
#define FF_FS_LOCK 0
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
/ is 1.
/
/ 0: Disable file lock function. To avoid volume corruption, application program
/ should avoid illegal open, remove and rename to the open objects.
/ >0: Enable file lock function. The value defines how many files/sub-directories
/ can be opened simultaneously under file lock control. Note that the file
/ lock control is independent of re-entrancy. */
/* #include <somertos.h> // O/S definitions */
#define FF_FS_REENTRANT 0
#define FF_FS_TIMEOUT 1000
#define FF_SYNC_t HANDLE
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
/ module itself. Note that regardless of this option, file access to different
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
/ to the same volume is under control of this function.
/
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
/ function, must be added to the project. Samples are available in
/ option/syscall.c.
/
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick.
/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
/ included somewhere in the scope of ff.h. */
/*--- End of configuration options ---*/

124
src/fatfs/ffsystem.c Normal file
View File

@ -0,0 +1,124 @@
/*------------------------------------------------------------------------*/
/* Sample Code of OS Dependent Functions for FatFs */
/* (C)ChaN, 2018 */
/*------------------------------------------------------------------------*/
#define _POSIX_THREAD_SAFE_FUNCTIONS
#include <time.h>
#include "ff.h"
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
/*------------------------------------------------------------------------*/
/* Allocate a memory block */
/*------------------------------------------------------------------------*/
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
UINT msize /* Number of bytes to allocate */
)
{
return malloc(msize); /* Allocate a new memory block with POSIX API */
}
/*------------------------------------------------------------------------*/
/* Free a memory block */
/*------------------------------------------------------------------------*/
void ff_memfree (
void* mblock /* Pointer to the memory block to free (nothing to do if null) */
)
{
free(mblock); /* Free the memory block with POSIX API */
}
#endif
#if FF_FS_REENTRANT /* Mutal exclusion */
/*------------------------------------------------------------------------*/
/* Create a Synchronization Object */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to create a new
/ synchronization object for the volume, such as semaphore and mutex.
/ When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
*/
//const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
BYTE vol, /* Corresponding volume (logical drive number) */
FF_SYNC_t* sobj /* Pointer to return the created sync object */
)
{
}
/*------------------------------------------------------------------------*/
/* Delete a Synchronization Object */
/*------------------------------------------------------------------------*/
/* This function is called in f_mount() function to delete a synchronization
/ object that created with ff_cre_syncobj() function. When a 0 is returned,
/ the f_mount() function fails with FR_INT_ERR.
*/
int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
)
{
}
/*------------------------------------------------------------------------*/
/* Request Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on entering file functions to lock the volume.
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
*/
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
FF_SYNC_t sobj /* Sync object to wait */
)
{
}
/*------------------------------------------------------------------------*/
/* Release Grant to Access the Volume */
/*------------------------------------------------------------------------*/
/* This function is called on leaving file functions to unlock the volume.
*/
void ff_rel_grant (
FF_SYNC_t sobj /* Sync object to be signaled */
)
{
}
#endif
DWORD get_fattime()
{
// TODO: return melonDS time instead of RTC??
time_t timestamp = time(NULL);
struct tm timedata;
localtime_r(&timestamp, &timedata);
DWORD ret;
ret = (timedata.tm_sec >> 1);
ret |= (timedata.tm_min << 5);
ret |= (timedata.tm_hour << 11);
ret |= (timedata.tm_mday << 16);
ret |= ((timedata.tm_mon + 1) << 21);
ret |= ((timedata.tm_year - 80) << 25);
return ret;
}

15593
src/fatfs/ffunicode.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,8 @@
#include "types.h"
#include <vector>
namespace Frontend
{
@ -84,6 +86,9 @@ int LoadROM(const u8 *romdata, u32 romlength, const char *archivefilename, const
// simulating ejection of the cartridge
void UnloadROM(int slot);
void ROMIcon(u8 (&data)[512], u16 (&palette)[16], u32* iconRef);
void AnimatedROMIcon(u8 (&data)[8][512], u16 (&palette)[8][16], u16 (&sequence)[64], u32 (&animatedTexRef)[32 * 32 * 64], std::vector<int> &animatedSequenceRef);
// reset execution of the current ROM
int Reset();
@ -151,7 +156,7 @@ int GetScreenTransforms(float* out, int* kind);
// de-transform the provided host display coordinates to get coordinates
// on the bottom screen
bool GetTouchCoords(int& x, int& y);
bool GetTouchCoords(int& x, int& y, bool clamp);
// initialize the audio utility

View File

@ -19,6 +19,8 @@
#include <stdio.h>
#include <string.h>
#include <utility>
#ifdef ARCHIVE_SUPPORT_ENABLED
#include "ArchiveUtil.h"
#endif
@ -461,6 +463,72 @@ int LoadROM(const char* file, int slot)
}
}
void ROMIcon(u8 (&data)[512], u16 (&palette)[16], u32* iconRef)
{
int index = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 8; k++)
{
for (int l = 0; l < 8; l++)
{
u8 pal_index = index % 2 ? data[index/2] >> 4 : data[index/2] & 0x0F;
u8 r = ((palette[pal_index] >> 0) & 0x1F) * 255 / 31;
u8 g = ((palette[pal_index] >> 5) & 0x1F) * 255 / 31;
u8 b = ((palette[pal_index] >> 10) & 0x1F) * 255 / 31;
u8 a = pal_index ? 255: 0;
u32* row = &iconRef[256 * i + 32 * k + 8 * j];
row[l] = (a << 24) | (r << 16) | (g << 8) | b;
index++;
}
}
}
}
}
#define SEQ_FLIPV(i) ((i & 0b1000000000000000) >> 15)
#define SEQ_FLIPH(i) ((i & 0b0100000000000000) >> 14)
#define SEQ_PAL(i) ((i & 0b0011100000000000) >> 11)
#define SEQ_BMP(i) ((i & 0b0000011100000000) >> 8)
#define SEQ_DUR(i) ((i & 0b0000000011111111) >> 0)
void AnimatedROMIcon(u8 (&data)[8][512], u16 (&palette)[8][16], u16 (&sequence)[64], u32 (&animatedTexRef)[32 * 32 * 64], std::vector<int> &animatedSequenceRef)
{
for (int i = 0; i < 64; i++)
{
if (!sequence[i])
break;
u32* frame = &animatedTexRef[32 * 32 * i];
ROMIcon(data[SEQ_BMP(sequence[i])], palette[SEQ_PAL(sequence[i])], frame);
if (SEQ_FLIPH(sequence[i]))
{
for (int x = 0; x < 32; x++)
{
for (int y = 0; y < 32/2; y++)
{
std::swap(frame[x * 32 + y], frame[x * 32 + (32 - 1 - y)]);
}
}
}
if (SEQ_FLIPV(sequence[i]))
{
for (int x = 0; x < 32/2; x++)
{
for (int y = 0; y < 32; y++)
{
std::swap(frame[x * 32 + y], frame[(32 - 1 - x) * 32 + y]);
}
}
}
for (int j = 0; j < SEQ_DUR(sequence[i]); j++)
animatedSequenceRef.push_back(i);
}
}
void UnloadROM(int slot)
{
if (slot == ROMSlot_NDS)

View File

@ -37,6 +37,7 @@ bool TopEnable;
bool BotEnable;
bool HybEnable;
int HybScreen;
int HybPrevTouchScreen; // 0:unknown, 1:buttom screen, 2:hybrid screen
void M23_Identity(float* m)
{
@ -138,6 +139,7 @@ void SetupScreenLayout(int screenWidth, int screenHeight,
HybScreen = swapScreens ? 1 : 0;
swapScreens = false;
topAspect = botAspect = 1;
HybPrevTouchScreen = 0;
}
float refpoints[6][2] =
@ -348,6 +350,9 @@ void SetupScreenLayout(int screenWidth, int screenHeight,
float primScale = std::min(screenWidth / primHSize, screenHeight / primVSize);
float secScale = 1.f;
if (integerScale)
primScale = floorf(primScale);
if (layout == 0)
{
if (screenHeight - primVSize * primScale < secVSize)
@ -365,8 +370,8 @@ void SetupScreenLayout(int screenWidth, int screenHeight,
if (integerScale)
{
primScale = floor(primScale);
secScale = floor(secScale);
primScale = floorf(primScale);
secScale = floorf(secScale);
}
M23_Scale(primMtx, primScale);
@ -464,33 +469,81 @@ int GetScreenTransforms(float* out, int* kind)
return num;
}
bool GetTouchCoords(int& x, int& y)
bool GetTouchCoords(int& x, int& y, bool clamp)
{
if (BotEnable)
if (HybEnable && HybScreen == 1)
{
float vx = x;
float vy = y;
float hvx = x;
float hvy = y;
M23_Transform(TouchMtx, vx, vy);
M23_Transform(HybTouchMtx, hvx, hvy);
if (clamp)
{
if (HybPrevTouchScreen == 1)
{
x = std::clamp((int)vx, 0, 255);
y = std::clamp((int)vy, 0, 191);
return true;
}
if (HybPrevTouchScreen == 2)
{
x = std::clamp((int)hvx, 0, 255);
y = std::clamp((int)hvy, 0, 191);
return true;
}
}
else
{
if (vx >= 0 && vx < 256 && vy >= 0 && vy < 192)
{
HybPrevTouchScreen = 1;
x = (int)vx;
y = (int)vy;
return true;
}
if (hvx >= 0 && hvx < 256 && hvy >= 0 && hvy < 192)
{
HybPrevTouchScreen = 2;
x = (int)hvx;
y = (int)hvy;
return true;
}
}
}
else if (BotEnable)
{
float vx = x;
float vy = y;
M23_Transform(TouchMtx, vx, vy);
x = (int)vx;
y = (int)vy;
if (clamp)
{
x = std::clamp((int)vx, 0, 255);
y = std::clamp((int)vy, 0, 191);
if (vx >= 0 && vx < 256 && vy >= 0 && vy < 192)
return true;
}
else if (HybEnable && HybScreen == 1)
{
float vx = x;
float vy = y;
}
else
{
if (vx >= 0 && vx < 256 && vy >= 0 && vy < 192)
{
x = (int)vx;
y = (int)vy;
M23_Transform(HybTouchMtx, vx, vy);
x = (int)vx;
y = (int)vy;
if (x >= 0 && x < 256 && y >= 0 && y < 192)
return true;
return true;
}
}
}
return false;

View File

@ -38,8 +38,21 @@ AudioSettingsDialog::AudioSettingsDialog(QWidget* parent) : QDialog(parent), ui(
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
oldInterp = Config::AudioInterp;
oldBitrate = Config::AudioBitrate;
oldVolume = Config::AudioVolume;
ui->cbInterpolation->addItem("None");
ui->cbInterpolation->addItem("Linear");
ui->cbInterpolation->addItem("Cosine");
ui->cbInterpolation->addItem("Cubic");
ui->cbInterpolation->setCurrentIndex(Config::AudioInterp);
ui->cbBitrate->addItem("Automatic");
ui->cbBitrate->addItem("10-bit");
ui->cbBitrate->addItem("16-bit");
ui->cbBitrate->setCurrentIndex(Config::AudioBitrate);
ui->slVolume->setValue(Config::AudioVolume);
grpMicMode = new QButtonGroup(this);
@ -73,11 +86,33 @@ void AudioSettingsDialog::on_AudioSettingsDialog_accepted()
void AudioSettingsDialog::on_AudioSettingsDialog_rejected()
{
Config::AudioInterp = oldInterp;
Config::AudioBitrate = oldBitrate;
Config::AudioVolume = oldVolume;
closeDlg();
}
void AudioSettingsDialog::on_cbBitrate_currentIndexChanged(int idx)
{
// prevent a spurious change
if (ui->cbBitrate->count() < 3) return;
Config::AudioBitrate = ui->cbBitrate->currentIndex();
emit updateAudioSettings();
}
void AudioSettingsDialog::on_cbInterpolation_currentIndexChanged(int idx)
{
// prevent a spurious change
if (ui->cbInterpolation->count() < 4) return;
Config::AudioInterp = ui->cbInterpolation->currentIndex();
emit updateAudioSettings();
}
void AudioSettingsDialog::on_slVolume_valueChanged(int val)
{
Config::AudioVolume = val;

View File

@ -51,10 +51,15 @@ public:
currentDlg = nullptr;
}
signals:
void updateAudioSettings();
private slots:
void on_AudioSettingsDialog_accepted();
void on_AudioSettingsDialog_rejected();
void on_cbInterpolation_currentIndexChanged(int idx);
void on_cbBitrate_currentIndexChanged(int idx);
void on_slVolume_valueChanged(int val);
void onChangeMicMode(int mode);
void on_btnMicWavBrowse_clicked();
@ -62,6 +67,8 @@ private slots:
private:
Ui::AudioSettingsDialog* ui;
int oldInterp;
int oldBitrate;
int oldVolume;
QButtonGroup* grpMicMode;
};

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>482</width>
<height>230</height>
<height>256</height>
</rect>
</property>
<property name="sizePolicy">
@ -29,14 +29,14 @@
<string>Audio output</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Volume:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="2" column="1">
<widget class="QSlider" name="slVolume">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Controls the volume of the audio output.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
@ -52,6 +52,34 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Interpolation:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbInterpolation">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Applies interpolation to audio samples for better quality. Option &amp;quot;None&amp;quot; is accurate to DS hardware.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Bitrate:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="cbBitrate">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The bitrate of audio playback. If set to &quot;Automatic&quot; this will be 10-bit for DS mode and 16-bit for DSi mode.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>

View File

@ -10,6 +10,8 @@ SET(SOURCES_QT_SDL
AudioSettingsDialog.cpp
WifiSettingsDialog.cpp
InterfaceSettingsDialog.cpp
ROMInfoDialog.cpp
TitleManagerDialog.cpp
Input.cpp
LAN_PCap.cpp
LAN_Socket.cpp
@ -45,11 +47,12 @@ if (USE_QT6)
set(Qt6Core_DIR ${QT6_STATIC_BASE}Core)
set(Qt6Gui_DIR ${QT6_STATIC_BASE}Gui)
set(Qt6Widgets_DIR ${QT6_STATIC_BASE}Widgets)
set(Qt6Network_DIR ${QT6_STATIC_BASE}Network)
set(Qt6OpenGL_DIR ${QT6_STATIC_BASE}OpenGL)
set(Qt6OpenGLWidgets_DIR ${QT6_STATIC_BASE}OpenGLWidgets)
endif()
find_package(Qt6 COMPONENTS Core Gui Widgets OpenGL OpenGLWidgets REQUIRED)
set(QT_LINK_LIBS Qt6::Core Qt6::Gui Qt6::Widgets Qt6::OpenGL Qt6::OpenGLWidgets)
find_package(Qt6 COMPONENTS Core Gui Widgets Network OpenGL OpenGLWidgets REQUIRED)
set(QT_LINK_LIBS Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Network Qt6::OpenGL Qt6::OpenGLWidgets)
else()
if (BUILD_STATIC AND QT5_STATIC_DIR)
set(QT5_STATIC_BASE ${QT5_STATIC_DIR}/lib/cmake/Qt5)
@ -57,9 +60,10 @@ else()
set(Qt5Core_DIR ${QT5_STATIC_BASE}Core)
set(Qt5Gui_DIR ${QT5_STATIC_BASE}Gui)
set(Qt5Widgets_DIR ${QT5_STATIC_BASE}Widgets)
set(Qt5Network_DIR ${QT5_STATIC_BASE}Network)
endif()
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED)
set(QT_LINK_LIBS Qt5::Core Qt5::Gui Qt5::Widgets)
find_package(Qt5 COMPONENTS Core Gui Widgets Network REQUIRED)
set(QT_LINK_LIBS Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Network)
endif()
set(CMAKE_AUTOMOC ON)

View File

@ -284,7 +284,7 @@ void EmuSettingsDialog::on_btnDLDISDBrowse_clicked()
QString file = QFileDialog::getOpenFileName(this,
"Select DLDI SD image...",
EmuDirectory,
"Image files (*.bin *.rom *.img);;Any file (*.*)");
"Image files (*.bin *.rom *.img *.dmg);;Any file (*.*)");
if (file.isEmpty()) return;
@ -320,7 +320,7 @@ void EmuSettingsDialog::on_btnDSiSDBrowse_clicked()
QString file = QFileDialog::getOpenFileName(this,
"Select DSi SD image...",
EmuDirectory,
"Image files (*.bin *.rom *.img);;Any file (*.*)");
"Image files (*.bin *.rom *.img *.dmg);;Any file (*.*)");
if (file.isEmpty()) return;

View File

@ -354,21 +354,21 @@
<item row="2" column="0">
<widget class="QCheckBox" name="chkJITBranchOptimisations">
<property name="text">
<string>Branch Optimisations</string>
<string>Branch optimisations</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="chkJITLiteralOptimisations">
<property name="text">
<string>Literal Optimisations</string>
<string>Literal optimisations</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="chkJITFastMemory">
<property name="text">
<string>Fast Memory</string>
<string>Fast memory</string>
</property>
</widget>
</item>

View File

@ -52,6 +52,7 @@ const int hk_general[] =
{
HK_Pause,
HK_Reset,
HK_FrameStep,
HK_FastForward,
HK_FastForwardToggle,
HK_FullscreenToggle,
@ -64,6 +65,7 @@ const char* hk_general_labels[] =
{
"Pause/resume",
"Reset",
"Frame step",
"Fast forward",
"Toggle FPS limit",
"Toggle Fullscreen",
@ -90,7 +92,7 @@ InputConfigDialog::InputConfigDialog(QWidget* parent) : QDialog(parent), ui(new
addonsJoyMap[i] = Config::HKJoyMapping[hk_addons[i]];
}
for (int i = 0; i < 8; i++)
for (int i = 0; i < 9; i++)
{
hkGeneralKeyMap[i] = Config::HKKeyMapping[hk_general[i]];
hkGeneralJoyMap[i] = Config::HKJoyMapping[hk_general[i]];
@ -98,7 +100,7 @@ InputConfigDialog::InputConfigDialog(QWidget* parent) : QDialog(parent), ui(new
populatePage(ui->tabInput, 12, dskeylabels, keypadKeyMap, keypadJoyMap);
populatePage(ui->tabAddons, 2, hk_addons_labels, addonsKeyMap, addonsJoyMap);
populatePage(ui->tabHotkeysGeneral, 8, hk_general_labels, hkGeneralKeyMap, hkGeneralJoyMap);
populatePage(ui->tabHotkeysGeneral, 9, hk_general_labels, hkGeneralKeyMap, hkGeneralJoyMap);
int njoy = SDL_NumJoysticks();
if (njoy > 0)
@ -181,7 +183,7 @@ void InputConfigDialog::on_InputConfigDialog_accepted()
Config::HKJoyMapping[hk_addons[i]] = addonsJoyMap[i];
}
for (int i = 0; i < 8; i++)
for (int i = 0; i < 9; i++)
{
Config::HKKeyMapping[hk_general[i]] = hkGeneralKeyMap[i];
Config::HKJoyMapping[hk_general[i]] = hkGeneralJoyMap[i];

View File

@ -64,7 +64,7 @@ private:
int keypadKeyMap[12], keypadJoyMap[12];
int addonsKeyMap[2], addonsJoyMap[2];
int hkGeneralKeyMap[8], hkGeneralJoyMap[8];
int hkGeneralKeyMap[9], hkGeneralJoyMap[9];
};

View File

@ -69,6 +69,7 @@ int DirectLAN;
int SavestateRelocSRAM;
int AudioInterp;
int AudioVolume;
int MicInputType;
char MicWavPath[1024];
@ -124,6 +125,7 @@ ConfigEntry PlatformConfigFile[] =
{"HKKey_SwapScreens", 0, &HKKeyMapping[HK_SwapScreens], -1, NULL, 0},
{"HKKey_SolarSensorDecrease", 0, &HKKeyMapping[HK_SolarSensorDecrease], -1, NULL, 0},
{"HKKey_SolarSensorIncrease", 0, &HKKeyMapping[HK_SolarSensorIncrease], -1, NULL, 0},
{"HKKey_FrameStep", 0, &HKKeyMapping[HK_FrameStep], -1, NULL, 0},
{"HKJoy_Lid", 0, &HKJoyMapping[HK_Lid], -1, NULL, 0},
{"HKJoy_Mic", 0, &HKJoyMapping[HK_Mic], -1, NULL, 0},
@ -135,6 +137,7 @@ ConfigEntry PlatformConfigFile[] =
{"HKJoy_SwapScreens", 0, &HKJoyMapping[HK_SwapScreens], -1, NULL, 0},
{"HKJoy_SolarSensorDecrease", 0, &HKJoyMapping[HK_SolarSensorDecrease], -1, NULL, 0},
{"HKJoy_SolarSensorIncrease", 0, &HKJoyMapping[HK_SolarSensorIncrease], -1, NULL, 0},
{"HKJoy_FrameStep", 0, &HKJoyMapping[HK_FrameStep], -1, NULL, 0},
{"JoystickID", 0, &JoystickID, 0, NULL, 0},
@ -162,8 +165,8 @@ ConfigEntry PlatformConfigFile[] =
{"GL_ScaleFactor", 0, &GL_ScaleFactor, 1, NULL, 0},
{"GL_BetterPolygons", 0, &GL_BetterPolygons, 0, NULL, 0},
{"LimitFPS", 0, &LimitFPS, 0, NULL, 0},
{"AudioSync", 0, &AudioSync, 1, NULL, 0},
{"LimitFPS", 0, &LimitFPS, 1, NULL, 0},
{"AudioSync", 0, &AudioSync, 0, NULL, 0},
{"ShowOSD", 0, &ShowOSD, 1, NULL, 0},
{"ConsoleType", 0, &ConsoleType, 0, NULL, 0},
@ -175,6 +178,7 @@ ConfigEntry PlatformConfigFile[] =
{"SavStaRelocSRAM", 0, &SavestateRelocSRAM, 0, NULL, 0},
{"AudioInterp", 0, &AudioInterp, 0, NULL, 0},
{"AudioVolume", 0, &AudioVolume, 256, NULL, 0},
{"MicInputType", 0, &MicInputType, 1, NULL, 0},
{"MicWavPath", 1, MicWavPath, 0, "", 1023},
@ -197,7 +201,7 @@ ConfigEntry PlatformConfigFile[] =
{"MouseHide", 0, &MouseHide, 0, NULL, 0},
{"MouseHideSeconds", 0, &MouseHideSeconds, 5, NULL, 0},
{"PauseLostFocus", 0, &PauseLostFocus, 0, NULL, 0},
{"", -1, NULL, 0, NULL, 0}
};

View File

@ -33,6 +33,7 @@ enum
HK_SwapScreens,
HK_SolarSensorDecrease,
HK_SolarSensorIncrease,
HK_FrameStep,
HK_MAX
};
@ -84,6 +85,7 @@ extern int DirectLAN;
extern int SavestateRelocSRAM;
extern int AudioInterp;
extern int AudioVolume;
extern int MicInputType;
extern char MicWavPath[1024];

View File

@ -0,0 +1,145 @@
/*
Copyright 2016-2021 Arisotura, WaluigiWare64
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include "ROMInfoDialog.h"
#include "ui_ROMInfoDialog.h"
#include <QFileDialog>
#include "NDS.h"
#include "NDSCart.h"
#include "Platform.h"
#include "Config.h"
#include "PlatformConfig.h"
QString IntToHex(u64 num)
{
return ("0x" + QString::number(num, 16).toUpper());
}
QString QStringBytes(u64 num)
{
return (QString::number(num) + " bytes");
}
ROMInfoDialog* ROMInfoDialog::currentDlg = nullptr;
ROMInfoDialog::ROMInfoDialog(QWidget* parent) : QDialog(parent), ui(new Ui::ROMInfoDialog)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
u32 iconData[32 * 32];
Frontend::ROMIcon(NDSCart::Banner.Icon, NDSCart::Banner.Palette, iconData);
iconImage = QImage(reinterpret_cast<unsigned char*>(iconData), 32, 32, QImage::Format_ARGB32).copy();
ui->iconImage->setPixmap(QPixmap::fromImage(iconImage));
if (NDSCart::Banner.Version == 0x103)
{
u32 animatedIconData[32 * 32 * 64] = {0};
Frontend::AnimatedROMIcon(NDSCart::Banner.DSiIcon, NDSCart::Banner.DSiPalette, NDSCart::Banner.DSiSequence, animatedIconData, animatedSequence);
for (int i = 0; i < 64; i++)
{
if (animatedIconData[32 * 32 * i] == 0)
break;
animatedIconImages.push_back(QPixmap::fromImage(QImage(reinterpret_cast<unsigned char*>(&animatedIconData[32 * 32 * i]), 32, 32, QImage::Format_ARGB32).copy()));
}
iconTimeline = new QTimeLine(animatedSequence.size() / 60 * 1000, this);
iconTimeline->setFrameRange(0, animatedSequence.size() - 1);
iconTimeline->setLoopCount(0);
iconTimeline->setEasingCurve(QEasingCurve::Linear);
connect(iconTimeline, &QTimeLine::frameChanged, this, &ROMInfoDialog::iconSetFrame);
iconTimeline->start();
}
else
{
ui->dsiIconImage->setPixmap(QPixmap::fromImage(iconImage));
}
ui->iconTitle->setText(QString::fromUtf16(NDSCart::Banner.EnglishTitle));
ui->japaneseTitle->setText(QString::fromUtf16(NDSCart::Banner.JapaneseTitle, 128));
ui->englishTitle->setText(QString::fromUtf16(NDSCart::Banner.EnglishTitle, 128));
ui->frenchTitle->setText(QString::fromUtf16(NDSCart::Banner.FrenchTitle, 128));
ui->germanTitle->setText(QString::fromUtf16(NDSCart::Banner.GermanTitle, 128));
ui->italianTitle->setText(QString::fromUtf16(NDSCart::Banner.ItalianTitle, 128));
ui->spanishTitle->setText(QString::fromUtf16(NDSCart::Banner.SpanishTitle, 128));
if (NDSCart::Banner.Version > 1)
ui->chineseTitle->setText(QString::fromUtf16(NDSCart::Banner.ChineseTitle));
else
ui->chineseTitle->setText("None");
if (NDSCart::Banner.Version > 2)
ui->koreanTitle->setText(QString::fromUtf16(NDSCart::Banner.KoreanTitle));
else
ui->koreanTitle->setText("None");
ui->gameTitle->setText(QString::fromLatin1(NDSCart::Header.GameTitle, 12));
ui->gameCode->setText(QString::fromLatin1(NDSCart::Header.GameCode, 4));
ui->makerCode->setText(QString::fromLatin1(NDSCart::Header.MakerCode, 2));
ui->cardSize->setText(QString::number(128 << NDSCart::Header.CardSize) + " KB");
ui->arm9RomOffset->setText(IntToHex(NDSCart::Header.ARM9ROMOffset));
ui->arm9EntryAddress->setText(IntToHex(NDSCart::Header.ARM9EntryAddress));
ui->arm9RamAddress->setText(IntToHex(NDSCart::Header.ARM9RAMAddress));
ui->arm9Size->setText(QStringBytes(NDSCart::Header.ARM9Size));
ui->arm7RomOffset->setText(IntToHex(NDSCart::Header.ARM7ROMOffset));
ui->arm7EntryAddress->setText(IntToHex(NDSCart::Header.ARM7EntryAddress));
ui->arm7RamAddress->setText(IntToHex(NDSCart::Header.ARM7RAMAddress));
ui->arm7Size->setText(QStringBytes(NDSCart::Header.ARM7Size));
ui->fntOffset->setText(IntToHex(NDSCart::Header.FNTOffset));
ui->fntSize->setText(QStringBytes(NDSCart::Header.FNTSize));
ui->fatOffset->setText(IntToHex(NDSCart::Header.FATOffset));
ui->fatSize->setText(QStringBytes(NDSCart::Header.FATSize));
}
ROMInfoDialog::~ROMInfoDialog()
{
delete ui;
}
void ROMInfoDialog::done(int r)
{
QDialog::done(r);
closeDlg();
}
void ROMInfoDialog::on_saveIconButton_clicked()
{
QString filename = QFileDialog::getSaveFileName(this,
"Save Icon",
Config::LastROMFolder,
"PNG Images (*.png)");
if (filename.isEmpty())
return;
iconImage.save(filename, "PNG");
}
void ROMInfoDialog::iconSetFrame(int frame)
{
ui->dsiIconImage->setPixmap(animatedIconImages[animatedSequence[frame]]);
}

View File

@ -0,0 +1,75 @@
/*
Copyright 2016-2021 Arisotura, WaluigiWare64
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef ROMINFODIALOG_H
#define ROMINFODIALOG_H
#include <QDialog>
#include <QTimeLine>
#include <QPixmap>
#include <QImage>
#include "types.h"
#include "FrontendUtil.h"
namespace Ui { class ROMInfoDialog; }
class ROMInfoDialog;
class ROMInfoDialog : public QDialog
{
Q_OBJECT
public:
explicit ROMInfoDialog(QWidget* parent);
~ROMInfoDialog();
static ROMInfoDialog* currentDlg;
static ROMInfoDialog* openDlg(QWidget* parent)
{
if (currentDlg)
{
currentDlg->activateWindow();
return currentDlg;
}
currentDlg = new ROMInfoDialog(parent);
currentDlg->open();
return currentDlg;
}
static void closeDlg()
{
currentDlg = nullptr;
}
private slots:
void done(int r);
void on_saveIconButton_clicked();
void iconSetFrame(int frame);
private:
Ui::ROMInfoDialog* ui;
QImage iconImage;
QTimeLine* iconTimeline;
std::vector<QPixmap> animatedIconImages;
std::vector<int> animatedSequence;
};
#endif // ROMINFODIALOG_H

View File

@ -0,0 +1,831 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ROMInfoDialog</class>
<widget class="QDialog" name="ROMInfoDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>427</width>
<height>434</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>ROM info - melonDS</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QGroupBox" name="titles">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Titles</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Japanese title:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="japaneseTitle">
<property name="text">
<string>[japanese title]</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>English title:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="englishTitle">
<property name="text">
<string>[english title]</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>French title:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="frenchTitle">
<property name="text">
<string>[french title]</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_5">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>German title:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="germanTitle">
<property name="text">
<string>[german title]</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_6">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Italian title:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="italianTitle">
<property name="text">
<string>[italian title]</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_7">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Spanish title:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="spanishTitle">
<property name="text">
<string>[spanish title]</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_25">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Chinese title:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="chineseTitle">
<property name="text">
<string>[chinese title]</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_23">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Korean title:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="koreanTitle">
<property name="text">
<string>[korean title]</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="2" column="1">
<widget class="QGroupBox" name="arm9AndArm7Binaries">
<property name="title">
<string>ARM7 and ARM9 binaries</string>
</property>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_11">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM9 ROM offset: </string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="arm9RomOffset">
<property name="text">
<string>[arm9 rom offset]</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_12">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM9 entry address:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="arm9EntryAddress">
<property name="text">
<string>[arm9 entry address]</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_14">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM9 RAM address:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="arm9RamAddress">
<property name="text">
<string>[arm9 ram address]</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_16">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM9 size:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="arm9Size">
<property name="text">
<string>[arm9 size]</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_15">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM7 ROM offset: </string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="arm7RomOffset">
<property name="text">
<string>[arm7 rom offset]</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_13">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM7 entry address:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="arm7EntryAddress">
<property name="text">
<string>[arm7 entry address]</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_18">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM7 RAM address:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="arm7RamAddress">
<property name="text">
<string>[arm7 ram address]</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_17">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>ARM7 size:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="arm7Size">
<property name="text">
<string>[arm7 size]</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="1">
<widget class="QGroupBox" name="filesystem">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Filesystem</string>
</property>
<layout class="QFormLayout" name="formLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="label_19">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>FNT offset:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="fntOffset">
<property name="text">
<string>[fnt offset]</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_20">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>FNT size:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="fntSize">
<property name="text">
<string>[fnt size]</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_21">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>FAT offset:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="fatOffset">
<property name="text">
<string>[fat offset]</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_22">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>FAT size:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="fatSize">
<property name="text">
<string>[fat size]</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="generalInfo">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>General info</string>
</property>
<layout class="QFormLayout" name="formLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Game title:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="gameTitle">
<property name="text">
<string>[game title]</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Game code:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="gameCode">
<property name="text">
<string>[game code]</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_9">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Maker code:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="makerCode">
<property name="text">
<string>[maker code]</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_10">
<property name="font">
<font>
<underline>false</underline>
</font>
</property>
<property name="text">
<string>Card size:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="cardSize">
<property name="text">
<string>[card size]</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_2">
<layout class="QGridLayout" name="gridLayout_9">
<item row="0" column="1">
<widget class="QGroupBox" name="iconBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">#iconBox {
border: 1px solid black;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 white,
stop: 1 lightgrey);
}
#titleBox {
border: 0.5px solid grey;
background-color: #fbfbfb;
}
#iconTitle {
color: black;
}
</string>
</property>
<property name="title">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="topMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item row="0" column="1">
<widget class="QLabel" name="iconImage">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="text">
<string>[icon]</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QGroupBox" name="titleBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="iconTitle">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
<pointsize>8</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>[title]</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="3">
<widget class="QGroupBox" name="dsiIconBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">#dsiIconBox {
border-radius: 4px;
background-color: #515151;
}
#dsiIconImage {
background-color: white;
}</string>
</property>
<property name="title">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_10">
<property name="leftMargin">
<number>8</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>8</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="dsiIconImage">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>32</width>
<height>32</height>
</size>
</property>
<property name="text">
<string>[dsi icon]</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>55</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="4">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="saveIconButton">
<property name="text">
<string>Save icon</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ROMInfoDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ROMInfoDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -0,0 +1,142 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TitleImportDialog</class>
<widget class="QDialog" name="TitleImportDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>495</width>
<height>202</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Import title - melonDS</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="2">
<widget class="QPushButton" name="btnTmdBrowse">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QRadioButton" name="rbTmdFromNUS">
<property name="text">
<string>Download from NUS</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="3">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Metadata (TMD):</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QRadioButton" name="rbTmdFromFile">
<property name="text">
<string>From file:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="txtAppFile"/>
</item>
<item row="7" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QLabel" name="label_4">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Executable:</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QLabel" name="label_3">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="txtTmdFile"/>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="btnAppBrowse">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
<item row="6" column="0" colspan="3">
<widget class="QCheckBox" name="cbReadOnly">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Makes the title executable and TMD read-only. Prevents DSi system utilities from deleting them.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Make title files read-only</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>TitleImportDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>TitleImportDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -0,0 +1,557 @@
/*
Copyright 2016-2021 Arisotura
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <QFileDialog>
#include <QMenu>
#include "types.h"
#include "Platform.h"
#include "Config.h"
#include "PlatformConfig.h"
#include "FrontendUtil.h"
#include "DSi_NAND.h"
#include "TitleManagerDialog.h"
#include "ui_TitleManagerDialog.h"
#include "ui_TitleImportDialog.h"
FILE* TitleManagerDialog::curNAND = nullptr;
TitleManagerDialog* TitleManagerDialog::currentDlg = nullptr;
extern char* EmuDirectory;
TitleManagerDialog::TitleManagerDialog(QWidget* parent) : QDialog(parent), ui(new Ui::TitleManagerDialog)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
ui->lstTitleList->setIconSize(QSize(32, 32));
const u32 category = 0x00030004;
std::vector<u32> titlelist;
DSi_NAND::ListTitles(category, titlelist);
for (std::vector<u32>::iterator it = titlelist.begin(); it != titlelist.end(); it++)
{
u32 titleid = *it;
createTitleItem(category, titleid);
}
ui->lstTitleList->sortItems();
ui->btnImportTitleData->setEnabled(false);
ui->btnExportTitleData->setEnabled(false);
ui->btnDeleteTitle->setEnabled(false);
{
QMenu* menu = new QMenu(ui->btnImportTitleData);
actImportTitleData[0] = menu->addAction("public.sav");
actImportTitleData[0]->setData(QVariant(DSi_NAND::TitleData_PublicSav));
connect(actImportTitleData[0], &QAction::triggered, this, &TitleManagerDialog::onImportTitleData);
actImportTitleData[1] = menu->addAction("private.sav");
actImportTitleData[1]->setData(QVariant(DSi_NAND::TitleData_PrivateSav));
connect(actImportTitleData[1], &QAction::triggered, this, &TitleManagerDialog::onImportTitleData);
actImportTitleData[2] = menu->addAction("banner.sav");
actImportTitleData[2]->setData(QVariant(DSi_NAND::TitleData_BannerSav));
connect(actImportTitleData[2], &QAction::triggered, this, &TitleManagerDialog::onImportTitleData);
ui->btnImportTitleData->setMenu(menu);
}
{
QMenu* menu = new QMenu(ui->btnExportTitleData);
actExportTitleData[0] = menu->addAction("public.sav");
actExportTitleData[0]->setData(QVariant(DSi_NAND::TitleData_PublicSav));
connect(actExportTitleData[0], &QAction::triggered, this, &TitleManagerDialog::onExportTitleData);
actExportTitleData[1] = menu->addAction("private.sav");
actExportTitleData[1]->setData(QVariant(DSi_NAND::TitleData_PrivateSav));
connect(actExportTitleData[1], &QAction::triggered, this, &TitleManagerDialog::onExportTitleData);
actExportTitleData[2] = menu->addAction("banner.sav");
actExportTitleData[2]->setData(QVariant(DSi_NAND::TitleData_BannerSav));
connect(actExportTitleData[2], &QAction::triggered, this, &TitleManagerDialog::onExportTitleData);
ui->btnExportTitleData->setMenu(menu);
}
}
TitleManagerDialog::~TitleManagerDialog()
{
delete ui;
}
void TitleManagerDialog::createTitleItem(u32 category, u32 titleid)
{
u32 version;
NDSHeader header;
NDSBanner banner;
DSi_NAND::GetTitleInfo(category, titleid, version, &header, &banner);
u32 icondata[32*32];
Frontend::ROMIcon(banner.Icon, banner.Palette, icondata);
QImage iconimg((const uchar*)icondata, 32, 32, QImage::Format_ARGB32);
QIcon icon(QPixmap::fromImage(iconimg.copy()));
// TODO: make it possible to select other languages?
QString title = QString::fromUtf16(banner.EnglishTitle, 128);
title.replace("\n", " · ");
char gamecode[5];
*(u32*)&gamecode[0] = *(u32*)&header.GameCode[0];
gamecode[4] = '\0';
char extra[128];
sprintf(extra, "\n(title ID: %s · %08x/%08x · version %08x)", gamecode, category, titleid, version);
QListWidgetItem* item = new QListWidgetItem(title + QString(extra));
item->setIcon(icon);
item->setData(Qt::UserRole, QVariant((qulonglong)(((u64)category<<32) | (u64)titleid)));
item->setData(Qt::UserRole+1, QVariant(header.DSiPublicSavSize)); // public.sav size
item->setData(Qt::UserRole+2, QVariant(header.DSiPrivateSavSize)); // private.sav size
item->setData(Qt::UserRole+3, QVariant((u32)((header.AppFlags & 0x04) ? 0x4000 : 0))); // banner.sav size
ui->lstTitleList->addItem(item);
}
bool TitleManagerDialog::openNAND()
{
FILE* bios7i = Platform::OpenLocalFile(Config::DSiBIOS7Path, "rb");
if (!bios7i)
return false;
u8 es_keyY[16];
fseek(bios7i, 0x8308, SEEK_SET);
fread(es_keyY, 16, 1, bios7i);
fclose(bios7i);
curNAND = Platform::OpenLocalFile(Config::DSiNANDPath, "r+b");
if (!curNAND)
return false;
if (!DSi_NAND::Init(curNAND, es_keyY))
{
fclose(curNAND);
curNAND = nullptr;
return false;
}
return true;
}
void TitleManagerDialog::closeNAND()
{
if (curNAND)
{
DSi_NAND::DeInit();
fclose(curNAND);
curNAND = nullptr;
}
}
void TitleManagerDialog::done(int r)
{
QDialog::done(r);
closeDlg();
}
void TitleManagerDialog::on_btnImportTitle_clicked()
{
TitleImportDialog* importdlg = new TitleImportDialog(this, importAppPath, importTmdData, importReadOnly);
importdlg->open();
connect(importdlg, &TitleImportDialog::finished, this, &TitleManagerDialog::onImportTitleFinished);
importdlg->show();
}
void TitleManagerDialog::onImportTitleFinished(int res)
{
if (res != QDialog::Accepted) return;
u32 titleid[2];
titleid[0] = (importTmdData[0x18C] << 24) | (importTmdData[0x18D] << 16) | (importTmdData[0x18E] << 8) | importTmdData[0x18F];
titleid[1] = (importTmdData[0x190] << 24) | (importTmdData[0x191] << 16) | (importTmdData[0x192] << 8) | importTmdData[0x193];
// remove anything that might hinder the install
DSi_NAND::DeleteTitle(titleid[0], titleid[1]);
bool importres = DSi_NAND::ImportTitle(importAppPath.toStdString().c_str(), importTmdData, importReadOnly);
if (!importres)
{
// remove a potential half-completed install
DSi_NAND::DeleteTitle(titleid[0], titleid[1]);
QMessageBox::critical(this,
"Import title - melonDS",
"An error occured while installing the title to the NAND.\nCheck that your NAND dump is valid.");
}
else
{
// it worked, wee!
createTitleItem(titleid[0], titleid[1]);
ui->lstTitleList->sortItems();
}
}
void TitleManagerDialog::on_btnDeleteTitle_clicked()
{
QListWidgetItem* cur = ui->lstTitleList->currentItem();
if (!cur) return;
if (QMessageBox::question(this,
"Delete title - melonDS",
"The title and its associated data will be permanently deleted. Are you sure?",
QMessageBox::StandardButtons(QMessageBox::Yes|QMessageBox::No),
QMessageBox::No) != QMessageBox::Yes)
return;
u64 titleid = cur->data(Qt::UserRole).toULongLong();
DSi_NAND::DeleteTitle((u32)(titleid >> 32), (u32)titleid);
delete cur;
}
void TitleManagerDialog::on_lstTitleList_currentItemChanged(QListWidgetItem* cur, QListWidgetItem* prev)
{
if (!cur)
{
ui->btnImportTitleData->setEnabled(false);
ui->btnExportTitleData->setEnabled(false);
ui->btnDeleteTitle->setEnabled(false);
}
else
{
ui->btnImportTitleData->setEnabled(true);
ui->btnExportTitleData->setEnabled(true);
ui->btnDeleteTitle->setEnabled(true);
u32 val;
val = cur->data(Qt::UserRole+1).toUInt();
actImportTitleData[0]->setEnabled(val != 0);
actExportTitleData[0]->setEnabled(val != 0);
val = cur->data(Qt::UserRole+2).toUInt();
actImportTitleData[1]->setEnabled(val != 0);
actExportTitleData[1]->setEnabled(val != 0);
val = cur->data(Qt::UserRole+3).toUInt();
actImportTitleData[2]->setEnabled(val != 0);
actExportTitleData[2]->setEnabled(val != 0);
}
}
void TitleManagerDialog::onImportTitleData()
{
int type = ((QAction*)sender())->data().toInt();
QListWidgetItem* cur = ui->lstTitleList->currentItem();
if (!cur)
{
printf("what??\n");
return;
}
u32 wantedsize;
switch (type)
{
case DSi_NAND::TitleData_PublicSav: wantedsize = cur->data(Qt::UserRole+1).toUInt(); break;
case DSi_NAND::TitleData_PrivateSav: wantedsize = cur->data(Qt::UserRole+2).toUInt(); break;
case DSi_NAND::TitleData_BannerSav: wantedsize = cur->data(Qt::UserRole+3).toUInt(); break;
default:
printf("what??\n");
return;
}
QString file = QFileDialog::getOpenFileName(this,
"Select file to import...",
EmuDirectory,
"Title data files (*.sav);;Any file (*.*)");
if (file.isEmpty()) return;
FILE* f = fopen(file.toStdString().c_str(), "rb");
if (!f)
{
QMessageBox::critical(this,
"Import title data - melonDS",
"Could not open data file.\nCheck that the file is accessible.");
return;
}
fseek(f, 0, SEEK_END);
u64 len = ftell(f);
fclose(f);
if (len != wantedsize)
{
QMessageBox::critical(this,
"Import title data - melonDS",
QString("Cannot import this data file: size is incorrect (expected: %1 bytes).").arg(wantedsize));
return;
}
u64 titleid = cur->data(Qt::UserRole).toULongLong();
bool res = DSi_NAND::ImportTitleData((u32)(titleid >> 32), (u32)titleid, type, file.toStdString().c_str());
if (!res)
{
QMessageBox::critical(this,
"Import title data - melonDS",
"Failed to import the data file. Check that your NAND is accessible and valid.");
}
}
void TitleManagerDialog::onExportTitleData()
{
int type = ((QAction*)sender())->data().toInt();
QListWidgetItem* cur = ui->lstTitleList->currentItem();
if (!cur)
{
printf("what??\n");
return;
}
QString exportname;
u32 wantedsize;
switch (type)
{
case DSi_NAND::TitleData_PublicSav:
exportname = "/public.sav";
wantedsize = cur->data(Qt::UserRole+1).toUInt();
break;
case DSi_NAND::TitleData_PrivateSav:
exportname = "/private.sav";
wantedsize = cur->data(Qt::UserRole+2).toUInt();
break;
case DSi_NAND::TitleData_BannerSav:
exportname = "/banner.sav";
wantedsize = cur->data(Qt::UserRole+3).toUInt();
break;
default:
printf("what??\n");
return;
}
QString file = QFileDialog::getSaveFileName(this,
"Select path to export to...",
QString(EmuDirectory) + exportname,
"Title data files (*.sav);;Any file (*.*)");
if (file.isEmpty()) return;
u64 titleid = cur->data(Qt::UserRole).toULongLong();
bool res = DSi_NAND::ExportTitleData((u32)(titleid >> 32), (u32)titleid, type, file.toStdString().c_str());
if (!res)
{
QMessageBox::critical(this,
"Export title data - melonDS",
"Failed to Export the data file. Check that the destination directory is writable.");
}
}
TitleImportDialog::TitleImportDialog(QWidget* parent, QString& apppath, u8* tmd, bool& readonly)
: QDialog(parent), ui(new Ui::TitleImportDialog), appPath(apppath), tmdData(tmd), readOnly(readonly)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
grpTmdSource = new QButtonGroup(this);
grpTmdSource->addButton(ui->rbTmdFromFile, 0);
grpTmdSource->addButton(ui->rbTmdFromNUS, 1);
connect(grpTmdSource, SIGNAL(buttonClicked(int)), this, SLOT(onChangeTmdSource(int)));
grpTmdSource->button(0)->setChecked(true);
}
TitleImportDialog::~TitleImportDialog()
{
delete ui;
}
void TitleImportDialog::accept()
{
QString path;
FILE* f;
bool tmdfromfile = (grpTmdSource->checkedId() == 0);
path = ui->txtAppFile->text();
f = fopen(path.toStdString().c_str(), "rb");
if (!f)
{
QMessageBox::critical(this,
"Import title - melonDS",
"Could not open executable file.\nCheck that the path is correct and that the file is accessible.");
return;
}
fseek(f, 0x230, SEEK_SET);
fread(titleid, 8, 1, f);
fclose(f);
if (titleid[1] != 0x00030004)
{
QMessageBox::critical(this,
"Import title - melonDS",
"Executable file is not a DSiWare title.");
return;
}
if (tmdfromfile)
{
path = ui->txtTmdFile->text();
f = fopen(path.toStdString().c_str(), "rb");
if (!f)
{
QMessageBox::critical(this,
"Import title - melonDS",
"Could not open metadata file.\nCheck that the path is correct and that the file is accessible.");
return;
}
fread(tmdData, 0x208, 1, f);
fclose(f);
u32 tmdtitleid[2];
tmdtitleid[0] = (tmdData[0x18C] << 24) | (tmdData[0x18D] << 16) | (tmdData[0x18E] << 8) | tmdData[0x18F];
tmdtitleid[1] = (tmdData[0x190] << 24) | (tmdData[0x191] << 16) | (tmdData[0x192] << 8) | tmdData[0x193];
if (tmdtitleid[1] != titleid[0] || tmdtitleid[0] != titleid[1])
{
QMessageBox::critical(this,
"Import title - melonDS",
"Title ID in metadata file does not match executable file.");
return;
}
}
if (DSi_NAND::TitleExists(titleid[1], titleid[0]))
{
if (QMessageBox::question(this,
"Import title - melonDS",
"The selected title is already installed. Overwrite it?",
QMessageBox::StandardButtons(QMessageBox::Yes|QMessageBox::No),
QMessageBox::No) != QMessageBox::Yes)
return;
}
if (!tmdfromfile)
{
network = new QNetworkAccessManager(this);
char url[256];
sprintf(url, "http://nus.cdn.t.shop.nintendowifi.net/ccs/download/%08x%08x/tmd", titleid[1], titleid[0]);
QNetworkRequest req;
req.setUrl(QUrl(url));
netreply = network->get(req);
connect(netreply, &QNetworkReply::finished, this, &TitleImportDialog::tmdDownloaded);
setEnabled(false);
}
else
{
appPath = ui->txtAppFile->text();
readOnly = ui->cbReadOnly->isChecked();
QDialog::accept();
}
}
void TitleImportDialog::tmdDownloaded()
{
bool good = false;
if (netreply->error() != QNetworkReply::NoError)
{
QMessageBox::critical(this,
"Import title - melonDS",
QString("An error occurred while trying to download the metadata file:\n\n") + netreply->errorString());
}
else if (netreply->bytesAvailable() < 2312)
{
QMessageBox::critical(this,
"Import title - melonDS",
"NUS returned a malformed metadata file.");
}
else
{
netreply->read((char*)tmdData, 520);
u32 tmdtitleid[2];
tmdtitleid[0] = (tmdData[0x18C] << 24) | (tmdData[0x18D] << 16) | (tmdData[0x18E] << 8) | tmdData[0x18F];
tmdtitleid[1] = (tmdData[0x190] << 24) | (tmdData[0x191] << 16) | (tmdData[0x192] << 8) | tmdData[0x193];
if (tmdtitleid[1] != titleid[0] || tmdtitleid[0] != titleid[1])
{
QMessageBox::critical(this,
"Import title - melonDS",
"NUS returned a malformed metadata file.");
}
else
good = true;
}
netreply->deleteLater();
setEnabled(true);
if (good)
{
appPath = ui->txtAppFile->text();
readOnly = ui->cbReadOnly->isChecked();
QDialog::accept();
}
}
void TitleImportDialog::on_btnAppBrowse_clicked()
{
QString file = QFileDialog::getOpenFileName(this,
"Select title executable...",
EmuDirectory,
"DSiWare executables (*.app *.nds *.dsi *.srl);;Any file (*.*)");
if (file.isEmpty()) return;
ui->txtAppFile->setText(file);
}
void TitleImportDialog::on_btnTmdBrowse_clicked()
{
QString file = QFileDialog::getOpenFileName(this,
"Select title metadata...",
EmuDirectory,
"DSiWare metadata (*.tmd);;Any file (*.*)");
if (file.isEmpty()) return;
ui->txtTmdFile->setText(file);
}
void TitleImportDialog::onChangeTmdSource(int id)
{
bool pathenable = (id==0);
ui->txtTmdFile->setEnabled(pathenable);
ui->btnTmdBrowse->setEnabled(pathenable);
}

View File

@ -0,0 +1,133 @@
/*
Copyright 2016-2021 Arisotura
This file is part of melonDS.
melonDS 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.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef TITLEMANAGERDIALOG_H
#define TITLEMANAGERDIALOG_H
#include <QDialog>
#include <QMessageBox>
#include <QListWidget>
#include <QButtonGroup>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
namespace Ui
{
class TitleManagerDialog;
class TitleImportDialog;
}
class TitleManagerDialog;
class TitleImportDialog;
class TitleManagerDialog : public QDialog
{
Q_OBJECT
public:
explicit TitleManagerDialog(QWidget* parent);
~TitleManagerDialog();
static FILE* curNAND;
static bool openNAND();
static void closeNAND();
static TitleManagerDialog* currentDlg;
static TitleManagerDialog* openDlg(QWidget* parent)
{
if (currentDlg)
{
currentDlg->activateWindow();
return currentDlg;
}
if (!openNAND())
{
QMessageBox::critical(parent,
"DSi title manager - melonDS",
"Failed to mount the DSi NAND. Check that your NAND dump is accessible and valid.");
return nullptr;
}
currentDlg = new TitleManagerDialog(parent);
currentDlg->open();
return currentDlg;
}
static void closeDlg()
{
currentDlg = nullptr;
closeNAND();
}
private slots:
void done(int r);
void on_btnImportTitle_clicked();
void onImportTitleFinished(int res);
void on_btnDeleteTitle_clicked();
void on_lstTitleList_currentItemChanged(QListWidgetItem* cur, QListWidgetItem* prev);
void onImportTitleData();
void onExportTitleData();
private:
Ui::TitleManagerDialog* ui;
QString importAppPath;
u8 importTmdData[0x208];
bool importReadOnly;
QAction* actImportTitleData[3];
QAction* actExportTitleData[3];
void createTitleItem(u32 category, u32 titleid);
};
class TitleImportDialog : public QDialog
{
Q_OBJECT
public:
explicit TitleImportDialog(QWidget* parent, QString& apppath, u8* tmd, bool& readonly);
~TitleImportDialog();
private slots:
void accept() override;
void tmdDownloaded();
void on_btnAppBrowse_clicked();
void on_btnTmdBrowse_clicked();
void onChangeTmdSource(int id);
private:
Ui::TitleImportDialog* ui;
QButtonGroup* grpTmdSource;
QNetworkAccessManager* network;
QNetworkReply* netreply;
QString& appPath;
u8* tmdData;
bool& readOnly;
u32 titleid[2];
};
#endif // TITLEMANAGERDIALOG_H

View File

@ -0,0 +1,133 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TitleManagerDialog</class>
<widget class="QDialog" name="TitleManagerDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>608</width>
<height>466</height>
</rect>
</property>
<property name="windowTitle">
<string>DSi title manager - melonDS</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btnImportTitle">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Import a DSiware title to your emulated DSi system.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Import title...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnImportTitleData">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Import data (save, banner...) for the selected title.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Import title data</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnExportTitleData">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Export the data (save, banner...) associated with the selected title.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Export title data</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnDeleteTitle">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Permanently delete the selected title and its associated data from your emulated DSi.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Delete title</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="lstTitleList"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>TitleManagerDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>303</x>
<y>448</y>
</hint>
<hint type="destinationlabel">
<x>303</x>
<y>232</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>TitleManagerDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>303</x>
<y>448</y>
</hint>
<hint type="destinationlabel">
<x>303</x>
<y>232</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -144,12 +144,12 @@ void WifiSettingsDialog::on_cbxDirectAdapter_currentIndexChanged(int sel)
LAN_PCap::AdapterData* adapter = &LAN_PCap::Adapters[sel];
char tmp[64];
sprintf(tmp, "MAC: %02X:%02X:%02X:%02X:%02X:%02X",
sprintf(tmp, "%02X:%02X:%02X:%02X:%02X:%02X",
adapter->MAC[0], adapter->MAC[1], adapter->MAC[2],
adapter->MAC[3], adapter->MAC[4], adapter->MAC[5]);
ui->lblAdapterMAC->setText(QString(tmp));
sprintf(tmp, "IP: %d.%d.%d.%d",
sprintf(tmp, "%d.%d.%d.%d",
adapter->IP_v4[0], adapter->IP_v4[1],
adapter->IP_v4[2], adapter->IP_v4[3]);
ui->lblAdapterIP->setText(QString(tmp));

View File

@ -61,7 +61,7 @@
<item row="3" column="0" rowspan="3" colspan="2">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Direct Mode Settings</string>
<string>Direct mode settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
@ -127,7 +127,7 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Indirect mode uses libslirp. It requires no extra setup and is easy to use.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Indirect Mode (uses libslirp, recommended)</string>
<string>Indirect mode (uses libslirp, recommended)</string>
</property>
</widget>
</item>

View File

@ -57,6 +57,8 @@
#include "AudioSettingsDialog.h"
#include "WifiSettingsDialog.h"
#include "InterfaceSettingsDialog.h"
#include "ROMInfoDialog.h"
#include "TitleManagerDialog.h"
#include "types.h"
#include "version.h"
@ -65,6 +67,7 @@
#include "OSD.h"
#include "NDS.h"
#include "NDSCart.h"
#include "GBACart.h"
#include "GPU.h"
#include "SPU.h"
@ -104,6 +107,8 @@ u32 micExtBufferWritePos;
u32 micWavLength;
s16* micWavBuffer;
void micCallback(void* data, Uint8* stream, int len);
void audioCallback(void* data, Uint8* stream, int len)
{
@ -141,6 +146,40 @@ void audioCallback(void* data, Uint8* stream, int len)
}
void micOpen()
{
if (Config::MicInputType != 1)
{
micDevice = 0;
return;
}
SDL_AudioSpec whatIwant, whatIget;
memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
whatIwant.freq = 44100;
whatIwant.format = AUDIO_S16LSB;
whatIwant.channels = 1;
whatIwant.samples = 1024;
whatIwant.callback = micCallback;
micDevice = SDL_OpenAudioDevice(NULL, 1, &whatIwant, &whatIget, 0);
if (!micDevice)
{
printf("Mic init failed: %s\n", SDL_GetError());
}
else
{
SDL_PauseAudioDevice(micDevice, 0);
}
}
void micClose()
{
if (micDevice)
SDL_CloseAudioDevice(micDevice);
micDevice = 0;
}
void micLoadWav(const char* name)
{
SDL_AudioSpec format;
@ -157,64 +196,67 @@ void micLoadWav(const char* name)
const u64 dstfreq = 44100;
if (format.format == AUDIO_S16 || format.format == AUDIO_U16)
int srcinc = format.channels;
len /= ((SDL_AUDIO_BITSIZE(format.format) / 8) * srcinc);
micWavLength = (len * dstfreq) / format.freq;
if (micWavLength < 735) micWavLength = 735;
micWavBuffer = new s16[micWavLength];
float res_incr = len / (float)micWavLength;
float res_timer = 0;
int res_pos = 0;
for (int i = 0; i < micWavLength; i++)
{
int srcinc = format.channels;
len /= (2 * srcinc);
u16 val = 0;
micWavLength = (len * dstfreq) / format.freq;
if (micWavLength < 735) micWavLength = 735;
micWavBuffer = new s16[micWavLength];
float res_incr = len / (float)micWavLength;
float res_timer = 0;
int res_pos = 0;
for (int i = 0; i < micWavLength; i++)
switch (SDL_AUDIO_BITSIZE(format.format))
{
u16 val = ((u16*)buf)[res_pos];
if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000;
case 8:
val = buf[res_pos] << 8;
break;
micWavBuffer[i] = val;
case 16:
if (SDL_AUDIO_ISBIGENDIAN(format.format))
val = (buf[res_pos*2] << 8) | buf[res_pos*2 + 1];
else
val = (buf[res_pos*2 + 1] << 8) | buf[res_pos*2];
break;
res_timer += res_incr;
while (res_timer >= 1.0)
case 32:
if (SDL_AUDIO_ISFLOAT(format.format))
{
res_timer -= 1.0;
res_pos += srcinc;
u32 rawval;
if (SDL_AUDIO_ISBIGENDIAN(format.format))
rawval = (buf[res_pos*4] << 24) | (buf[res_pos*4 + 1] << 16) | (buf[res_pos*4 + 2] << 8) | buf[res_pos*4 + 3];
else
rawval = (buf[res_pos*4 + 3] << 24) | (buf[res_pos*4 + 2] << 16) | (buf[res_pos*4 + 1] << 8) | buf[res_pos*4];
float fval = *(float*)&rawval;
s32 ival = (s32)(fval * 0x8000);
ival = std::clamp(ival, -0x8000, 0x7FFF);
val = (s16)ival;
}
else if (SDL_AUDIO_ISBIGENDIAN(format.format))
val = (buf[res_pos*4] << 8) | buf[res_pos*4 + 1];
else
val = (buf[res_pos*4 + 3] << 8) | buf[res_pos*4 + 2];
break;
}
if (SDL_AUDIO_ISUNSIGNED(format.format))
val ^= 0x8000;
micWavBuffer[i] = val;
res_timer += res_incr;
while (res_timer >= 1.0)
{
res_timer -= 1.0;
res_pos += srcinc;
}
}
else if (format.format == AUDIO_S8 || format.format == AUDIO_U8)
{
int srcinc = format.channels;
len /= srcinc;
micWavLength = (len * dstfreq) / format.freq;
if (micWavLength < 735) micWavLength = 735;
micWavBuffer = new s16[micWavLength];
float res_incr = len / (float)micWavLength;
float res_timer = 0;
int res_pos = 0;
for (int i = 0; i < micWavLength; i++)
{
u16 val = buf[res_pos] << 8;
if (SDL_AUDIO_ISUNSIGNED(format.format)) val ^= 0x8000;
micWavBuffer[i] = val;
res_timer += res_incr;
while (res_timer >= 1.0)
{
res_timer -= 1.0;
res_pos += srcinc;
}
}
}
else
printf("bad WAV format %08X\n", format.format);
SDL_FreeWAV(buf);
}
@ -281,6 +323,7 @@ EmuThread::EmuThread(QObject* parent) : QThread(parent)
connect(this, SIGNAL(windowEmuStop()), mainWindow, SLOT(onEmuStop()));
connect(this, SIGNAL(windowEmuPause()), mainWindow->actPause, SLOT(trigger()));
connect(this, SIGNAL(windowEmuReset()), mainWindow->actReset, SLOT(trigger()));
connect(this, SIGNAL(windowEmuFrameStep()), mainWindow->actFrameStep, SLOT(trigger()));
connect(this, SIGNAL(windowLimitFPSChange()), mainWindow->actLimitFramerate, SLOT(trigger()));
connect(this, SIGNAL(screenLayoutChange()), mainWindow->panel, SLOT(onScreenLayoutChanged()));
connect(this, SIGNAL(windowFullscreenToggle()), mainWindow, SLOT(onFullscreenToggled()));
@ -343,6 +386,7 @@ void EmuThread::run()
videoSettingsDirty = false;
videoSettings.Soft_Threaded = Config::Threaded3D != 0;
videoSettings.GL_ScaleFactor = Config::GL_ScaleFactor;
videoSettings.GL_BetterPolygons = Config::GL_BetterPolygons;
#ifdef OGLRENDERER_ENABLED
if (hasOGL)
@ -359,6 +403,8 @@ void EmuThread::run()
GPU::InitRenderer(videoRenderer);
GPU::SetRenderSettings(videoRenderer, videoSettings);
SPU::SetInterpolation(Config::AudioInterp);
Input::Init();
u32 nframes = 0;
@ -377,28 +423,12 @@ void EmuThread::run()
if (Input::HotkeyPressed(HK_Pause)) emit windowEmuPause();
if (Input::HotkeyPressed(HK_Reset)) emit windowEmuReset();
if (Input::HotkeyPressed(HK_FrameStep)) emit windowEmuFrameStep();
if (Input::HotkeyPressed(HK_FullscreenToggle)) emit windowFullscreenToggle();
if (Input::HotkeyPressed(HK_SwapScreens)) emit swapScreensToggle();
/*if (GBACart::CartInserted && GBACart::HasSolarSensor)
{
if (Input::HotkeyPressed(HK_SolarSensorDecrease))
{
if (GBACart_SolarSensor::LightLevel > 0) GBACart_SolarSensor::LightLevel--;
char msg[64];
sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
OSD::AddMessage(0, msg);
}
if (Input::HotkeyPressed(HK_SolarSensorIncrease))
{
if (GBACart_SolarSensor::LightLevel < 10) GBACart_SolarSensor::LightLevel++;
char msg[64];
sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
OSD::AddMessage(0, msg);
}
}*/
if (Input::HotkeyPressed(HK_SolarSensorDecrease))
{
int level = GBACart::SetInput(GBACart::Input_SolarSensorDown, true);
@ -420,9 +450,10 @@ void EmuThread::run()
}
}
if (EmuRunning == 1)
if (EmuRunning == 1 || EmuRunning == 3)
{
EmuStatus = 1;
if (EmuRunning == 3) EmuRunning = 2;
// update render settings if needed
if (videoSettingsDirty)
@ -634,7 +665,7 @@ void EmuThread::emuRun()
// checkme
emit windowEmuStart();
if (audioDevice) SDL_PauseAudioDevice(audioDevice, 0);
if (micDevice) SDL_PauseAudioDevice(micDevice, 0);
micOpen();
}
void EmuThread::emuPause()
@ -647,7 +678,7 @@ void EmuThread::emuPause()
while (EmuStatus != 2);
if (audioDevice) SDL_PauseAudioDevice(audioDevice, 1);
if (micDevice) SDL_PauseAudioDevice(micDevice, 1);
micClose();
}
void EmuThread::emuUnpause()
@ -660,7 +691,7 @@ void EmuThread::emuUnpause()
EmuRunning = PrevEmuStatus;
if (audioDevice) SDL_PauseAudioDevice(audioDevice, 0);
if (micDevice) SDL_PauseAudioDevice(micDevice, 0);
micOpen();
}
void EmuThread::emuStop()
@ -669,7 +700,13 @@ void EmuThread::emuStop()
EmuPause = 0;
if (audioDevice) SDL_PauseAudioDevice(audioDevice, 1);
if (micDevice) SDL_PauseAudioDevice(micDevice, 1);
micClose();
}
void EmuThread::emuFrameStep()
{
if (EmuPause < 1) emit windowEmuPause();
EmuRunning = 3;
}
bool EmuThread::emuIsRunning()
@ -750,7 +787,7 @@ void ScreenHandler::screenOnMousePress(QMouseEvent* event)
int x = event->pos().x();
int y = event->pos().y();
if (Frontend::GetTouchCoords(x, y))
if (Frontend::GetTouchCoords(x, y, false))
{
touching = true;
NDS::TouchScreen(x, y);
@ -781,15 +818,68 @@ void ScreenHandler::screenOnMouseMove(QMouseEvent* event)
int x = event->pos().x();
int y = event->pos().y();
Frontend::GetTouchCoords(x, y);
if (Frontend::GetTouchCoords(x, y, true))
NDS::TouchScreen(x, y);
}
// clamp to screen range
if (x < 0) x = 0;
else if (x > 255) x = 255;
if (y < 0) y = 0;
else if (y > 191) y = 191;
void ScreenHandler::screenHandleTablet(QTabletEvent* event)
{
event->accept();
NDS::TouchScreen(x, y);
switch(event->type())
{
case QEvent::TabletPress:
case QEvent::TabletMove:
{
int x = event->x();
int y = event->y();
if (Frontend::GetTouchCoords(x, y, event->type()==QEvent::TabletMove))
{
touching = true;
NDS::TouchScreen(x, y);
}
}
break;
case QEvent::TabletRelease:
if (touching)
{
NDS::ReleaseScreen();
touching = false;
}
break;
}
}
void ScreenHandler::screenHandleTouch(QTouchEvent* event)
{
event->accept();
switch(event->type())
{
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
if (event->touchPoints().length() > 0)
{
QPointF lastPosition = event->touchPoints().first().lastPos();
int x = (int)lastPosition.x();
int y = (int)lastPosition.y();
if (Frontend::GetTouchCoords(x, y, event->type()==QEvent::TouchUpdate))
{
touching = true;
NDS::TouchScreen(x, y);
}
}
break;
case QEvent::TouchEnd:
if (touching)
{
NDS::ReleaseScreen();
touching = false;
}
break;
}
}
void ScreenHandler::showCursor()
@ -818,6 +908,8 @@ ScreenPanelNative::ScreenPanelNative(QWidget* parent) : QWidget(parent)
touching = false;
setAttribute(Qt::WA_AcceptTouchEvents);
OSD::Init(nullptr);
}
@ -896,6 +988,23 @@ void ScreenPanelNative::mouseMoveEvent(QMouseEvent* event)
screenOnMouseMove(event);
}
void ScreenPanelNative::tabletEvent(QTabletEvent* event)
{
screenHandleTablet(event);
}
bool ScreenPanelNative::event(QEvent* event)
{
if (event->type() == QEvent::TouchBegin
|| event->type() == QEvent::TouchEnd
|| event->type() == QEvent::TouchUpdate)
{
screenHandleTouch((QTouchEvent*)event);
return true;
}
return QWidget::event(event);
}
void ScreenPanelNative::onScreenLayoutChanged()
{
setMinimumSize(screenGetMinSize());
@ -907,6 +1016,7 @@ ScreenPanelGL::ScreenPanelGL(QWidget* parent) : QOpenGLWidget(parent)
{
touching = false;
setAttribute(Qt::WA_AcceptTouchEvents);
}
ScreenPanelGL::~ScreenPanelGL()
@ -1107,6 +1217,23 @@ void ScreenPanelGL::mouseMoveEvent(QMouseEvent* event)
screenOnMouseMove(event);
}
void ScreenPanelGL::tabletEvent(QTabletEvent* event)
{
screenHandleTablet(event);
}
bool ScreenPanelGL::event(QEvent* event)
{
if (event->type() == QEvent::TouchBegin
|| event->type() == QEvent::TouchEnd
|| event->type() == QEvent::TouchUpdate)
{
screenHandleTouch((QTouchEvent*)event);
return true;
}
return QWidget::event(event);
}
void ScreenPanelGL::onScreenLayoutChanged()
{
setMinimumSize(screenGetMinSize());
@ -1144,6 +1271,10 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
sigaction(SIGINT, &sa, 0);
#endif
oldW = Config::WindowWidth;
oldH = Config::WindowHeight;
oldMax = Config::WindowMaximized!=0;
setWindowTitle("melonDS " MELONDS_VERSION);
setAttribute(Qt::WA_DeleteOnClose);
setAcceptDrops(true);
@ -1156,11 +1287,11 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
connect(actOpenROM, &QAction::triggered, this, &MainWindow::onOpenFile);
actOpenROM->setShortcut(QKeySequence(QKeySequence::StandardKey::Open));
actOpenROMArchive = menu->addAction("Open ROM inside Archive...");
actOpenROMArchive = menu->addAction("Open ROM inside archive...");
connect(actOpenROMArchive, &QAction::triggered, this, &MainWindow::onOpenFileArchive);
actOpenROMArchive->setShortcut(QKeySequence(Qt::Key_O | Qt::CTRL | Qt::SHIFT));
recentMenu = menu->addMenu("Open Recent");
recentMenu = menu->addMenu("Open recent");
for (int i = 0; i < 10; ++i)
{
char* item = Config::RecentROMList[i];
@ -1233,6 +1364,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
actStop = menu->addAction("Stop");
connect(actStop, &QAction::triggered, this, &MainWindow::onStop);
actFrameStep = menu->addAction("Frame step");
connect(actFrameStep, &QAction::triggered, this, &MainWindow::onFrameStep);
menu->addSeparator();
actEnableCheats = menu->addAction("Enable cheats");
@ -1240,7 +1374,15 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
connect(actEnableCheats, &QAction::triggered, this, &MainWindow::onEnableCheats);
actSetupCheats = menu->addAction("Setup cheat codes");
actSetupCheats->setMenuRole(QAction::NoRole);
connect(actSetupCheats, &QAction::triggered, this, &MainWindow::onSetupCheats);
menu->addSeparator();
actROMInfo = menu->addAction("ROM info");
connect(actROMInfo, &QAction::triggered, this, &MainWindow::onROMInfo);
actTitleManager = menu->addAction("Manage DSi titles");
connect(actTitleManager, &QAction::triggered, this, &MainWindow::onOpenTitleManager);
}
{
QMenu* menu = menubar->addMenu("Config");
@ -1421,7 +1563,11 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
resize(Config::WindowWidth, Config::WindowHeight);
show();
if (oldMax)
showMaximized();
else
show();
createScreenPanel();
for (int i = 0; i < 9; i++)
@ -1435,12 +1581,15 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
actPause->setEnabled(false);
actReset->setEnabled(false);
actStop->setEnabled(false);
actFrameStep->setEnabled(false);
actSetupCheats->setEnabled(false);
actTitleManager->setEnabled(strlen(Config::DSiNANDPath) > 0);
actEnableCheats->setChecked(Config::EnableCheats != 0);
actROMInfo->setEnabled(false);
actSavestateSRAMReloc->setChecked(Config::SavestateRelocSRAM != 0);
actScreenRotation[Config::ScreenRotation]->setChecked(true);
@ -1532,13 +1681,30 @@ void MainWindow::resizeEvent(QResizeEvent* event)
int w = event->size().width();
int h = event->size().height();
if (mainWindow != nullptr && !mainWindow->isFullScreen())
if (!isFullScreen())
{
// this is ugly
// thing is, when maximizing the window, we first receive the resizeEvent
// with a new size matching the screen, then the changeEvent telling us that
// the maximized flag was updated
oldW = Config::WindowWidth;
oldH = Config::WindowHeight;
oldMax = isMaximized();
Config::WindowWidth = w;
Config::WindowHeight = h;
}
}
// TODO: detect when the window gets maximized!
void MainWindow::changeEvent(QEvent* event)
{
if (isMaximized() && !oldMax)
{
Config::WindowWidth = oldW;
Config::WindowHeight = oldH;
}
Config::WindowMaximized = isMaximized() ? 1:0;
}
void MainWindow::keyPressEvent(QKeyEvent* event)
@ -2086,6 +2252,8 @@ void MainWindow::onLoadState()
if (slot > 0) sprintf(msg, "State loaded from slot %d", slot);
else sprintf(msg, "State loaded from file");
OSD::AddMessage(0, msg);
actUndoStateLoad->setEnabled(true);
}
else
{
@ -2197,6 +2365,13 @@ void MainWindow::onStop()
NDS::Stop();
}
void MainWindow::onFrameStep()
{
if (!RunningSomething) return;
emuThread->emuFrameStep();
}
void MainWindow::onEnableCheats(bool checked)
{
Config::EnableCheats = checked?1:0;
@ -2216,6 +2391,15 @@ void MainWindow::onCheatsDialogFinished(int res)
emuThread->emuUnpause();
}
void MainWindow::onROMInfo()
{
ROMInfoDialog* dlg = ROMInfoDialog::openDlg(this);
}
void MainWindow::onOpenTitleManager()
{
TitleManagerDialog* dlg = TitleManagerDialog::openDlg(this);
}
void MainWindow::onOpenEmuSettings()
{
@ -2231,6 +2415,9 @@ void MainWindow::onEmuSettingsDialogFinished(int res)
if (EmuSettingsDialog::needsReset)
onReset();
if (!RunningSomething)
actTitleManager->setEnabled(strlen(Config::DSiNANDPath) > 0);
}
void MainWindow::onOpenInputConfig()
@ -2255,11 +2442,26 @@ void MainWindow::onOpenVideoSettings()
void MainWindow::onOpenAudioSettings()
{
AudioSettingsDialog* dlg = AudioSettingsDialog::openDlg(this);
connect(dlg, &AudioSettingsDialog::updateAudioSettings, this, &MainWindow::onUpdateAudioSettings);
connect(dlg, &AudioSettingsDialog::finished, this, &MainWindow::onAudioSettingsFinished);
}
void MainWindow::onUpdateAudioSettings()
{
SPU::SetInterpolation(Config::AudioInterp);
if (Config::AudioBitrate == 0)
SPU::SetDegrade10Bit(NDS::ConsoleType == 0);
else
SPU::SetDegrade10Bit(Config::AudioBitrate == 1);
}
void MainWindow::onAudioSettingsFinished(int res)
{
micClose();
SPU::SetInterpolation(Config::AudioInterp);
if (Config::MicInputType == 3)
{
micLoadWav(Config::MicWavPath);
@ -2275,6 +2477,8 @@ void MainWindow::onAudioSettingsFinished(int res)
else
Frontend::Mic_SetExternalBuffer(NULL, 0);
}
micOpen();
}
void MainWindow::onOpenWifiSettings()
@ -2466,9 +2670,13 @@ void MainWindow::onEmuStart()
actPause->setChecked(false);
actReset->setEnabled(true);
actStop->setEnabled(true);
actFrameStep->setEnabled(true);
actImportSavefile->setEnabled(true);
actSetupCheats->setEnabled(true);
actTitleManager->setEnabled(false);
actROMInfo->setEnabled(true);
}
void MainWindow::onEmuStop()
@ -2486,8 +2694,12 @@ void MainWindow::onEmuStop()
actPause->setEnabled(false);
actReset->setEnabled(false);
actStop->setEnabled(false);
actFrameStep->setEnabled(false);
actSetupCheats->setEnabled(false);
actTitleManager->setEnabled(strlen(Config::DSiNANDPath) > 0);
actROMInfo->setEnabled(false);
}
void MainWindow::onUpdateVideoSettings(bool glchange)
@ -2529,7 +2741,24 @@ void emuStop()
OSD::AddMessage(0xFFC040, "Shutdown");
}
MelonApplication::MelonApplication(int& argc, char** argv)
: QApplication(argc, argv)
{
setWindowIcon(QIcon(":/melon-icon"));
}
bool MelonApplication::event(QEvent *event)
{
if (event->type() == QEvent::FileOpen)
{
QFileOpenEvent *openEvent = static_cast<QFileOpenEvent*>(event);
emuThread->emuPause();
mainWindow->loadROM(openEvent->file());
}
return QApplication::event(event);
}
int main(int argc, char** argv)
{
@ -2540,8 +2769,7 @@ int main(int argc, char** argv)
Platform::Init(argc, argv);
QApplication melon(argc, argv);
melon.setWindowIcon(QIcon(":/melon-icon"));
MelonApplication melon(argc, argv);
// http://stackoverflow.com/questions/14543333/joystick-wont-work-using-sdl
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
@ -2571,6 +2799,7 @@ int main(int argc, char** argv)
);
SANITIZE(Config::ScreenVSyncInterval, 1, 20);
SANITIZE(Config::GL_ScaleFactor, 1, 16);
SANITIZE(Config::AudioInterp, 0, 3);
SANITIZE(Config::AudioVolume, 0, 256);
SANITIZE(Config::MicInputType, 0, 3);
SANITIZE(Config::ScreenRotation, 0, 3);
@ -2612,21 +2841,7 @@ int main(int argc, char** argv)
SDL_PauseAudioDevice(audioDevice, 1);
}
memset(&whatIwant, 0, sizeof(SDL_AudioSpec));
whatIwant.freq = 44100;
whatIwant.format = AUDIO_S16LSB;
whatIwant.channels = 1;
whatIwant.samples = 1024;
whatIwant.callback = micCallback;
micDevice = SDL_OpenAudioDevice(NULL, 1, &whatIwant, &whatIget, 0);
if (!micDevice)
{
printf("Mic init failed: %s\n", SDL_GetError());
}
else
{
SDL_PauseAudioDevice(micDevice, 1);
}
micDevice = 0;
memset(micExtBuffer, 0, sizeof(micExtBuffer));
@ -2697,7 +2912,7 @@ int main(int argc, char** argv)
Frontend::DeInit_ROM();
if (audioDevice) SDL_CloseAudioDevice(audioDevice);
if (micDevice) SDL_CloseAudioDevice(micDevice);
micClose();
SDL_DestroyCond(audioSync);
SDL_DestroyMutex(audioSyncLock);

View File

@ -19,6 +19,7 @@
#ifndef MAIN_H
#define MAIN_H
#include <QApplication>
#include <QThread>
#include <QWidget>
#include <QWindow>
@ -55,6 +56,7 @@ public:
void emuPause();
void emuUnpause();
void emuStop();
void emuFrameStep();
bool emuIsRunning();
@ -72,11 +74,12 @@ signals:
void windowEmuStop();
void windowEmuPause();
void windowEmuReset();
void windowEmuFrameStep();
void windowLimitFPSChange();
void screenLayoutChange();
void windowFullscreenToggle();
void swapScreensToggle();
@ -110,12 +113,15 @@ protected:
void screenOnMouseRelease(QMouseEvent* event);
void screenOnMouseMove(QMouseEvent* event);
void screenHandleTablet(QTabletEvent* event);
void screenHandleTouch(QTouchEvent* event);
float screenMatrix[Frontend::MaxScreenTransforms][6];
int screenKind[Frontend::MaxScreenTransforms];
int numScreens;
bool touching;
void showCursor();
};
@ -137,6 +143,8 @@ protected:
void mouseReleaseEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void tabletEvent(QTabletEvent* event) override;
bool event(QEvent* event) override;
private slots:
void onScreenLayoutChanged();
@ -168,6 +176,8 @@ protected:
void mouseReleaseEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void tabletEvent(QTabletEvent* event) override;
bool event(QEvent* event) override;
private slots:
void onScreenLayoutChanged();
@ -180,6 +190,14 @@ private:
GLuint screenTexture;
};
class MelonApplication : public QApplication
{
Q_OBJECT
public:
MelonApplication(int &argc, char** argv);
bool event(QEvent* event) override;
};
class MainWindow : public QMainWindow
{
@ -191,11 +209,15 @@ public:
bool hasOGL;
QOpenGLContext* getOGLContext();
void loadROM(QString filename);
void loadROM(QByteArray *romData, QString archiveFileName, QString romFileName);
void onAppStateChanged(Qt::ApplicationState state);
protected:
void resizeEvent(QResizeEvent* event) override;
void changeEvent(QEvent* event) override;
void keyPressEvent(QKeyEvent* event) override;
void keyReleaseEvent(QKeyEvent* event) override;
@ -221,9 +243,12 @@ private slots:
void onPause(bool checked);
void onReset();
void onStop();
void onFrameStep();
void onEnableCheats(bool checked);
void onSetupCheats();
void onCheatsDialogFinished(int res);
void onROMInfo();
void onOpenTitleManager();
void onOpenEmuSettings();
void onEmuSettingsDialogFinished(int res);
@ -231,6 +256,7 @@ private slots:
void onInputConfigFinished(int res);
void onOpenVideoSettings();
void onOpenAudioSettings();
void onUpdateAudioSettings();
void onAudioSettingsFinished(int res);
void onOpenWifiSettings();
void onWifiSettingsFinished(int res);
@ -258,23 +284,24 @@ private slots:
void onEmuStop();
void onUpdateVideoSettings(bool glchange);
void onFullscreenToggled();
private:
QList<QString> recentFileList;
QMenu *recentMenu;
void updateRecentFilesMenu();
void loadROM(QString filename);
void loadROM(QByteArray *romData, QString archiveFileName, QString romFileName);
QString pickAndExtractFileFromArchive(QString archiveFileName, QByteArray *romBuffer);
void createScreenPanel();
QString loadErrorStr(int error);
bool pausedManually;
bool pausedManually = false;
int oldW, oldH;
bool oldMax;
public:
QWidget* panel;
@ -293,8 +320,11 @@ public:
QAction* actPause;
QAction* actReset;
QAction* actStop;
QAction* actFrameStep;
QAction* actEnableCheats;
QAction* actSetupCheats;
QAction* actROMInfo;
QAction* actTitleManager;
QAction* actEmuSettings;
QAction* actInputConfig;

9
src/sha1/sha1.hpp Normal file
View File

@ -0,0 +1,9 @@
#ifndef SHA1_HPP
#define SHA1_HPP
extern "C"
{
#include "sha1.h"
}
#endif

40
src/teakra/.gitignore vendored Normal file
View File

@ -0,0 +1,40 @@
# Build directory
[Bb]uild/
cmake-build/
doc-build/
test/
# Generated source files
src/common/scm_rev.cpp
.travis.descriptor.json
# Project/editor files
*.swp
.idea/
.vs/
.vscode/
# *nix related
# Common convention for backup or temporary files
*~
# Visual Studio CMake settings
CMakeSettings.json
# OSX global filetypes
# Created by Finder or Spotlight in directories for various OS functionality (indexing, etc)
.DS_Store
.AppleDouble
.LSOverride
.Spotlight-V100
.Trashes
# Windows global filetypes
Thumbs.db
teakra.out
teakra.out.*
# cmake and ctest temporary files
/Testing

33
src/teakra/.travis.yml Normal file
View File

@ -0,0 +1,33 @@
language: cpp
matrix:
include:
- env: NAME="Linux Build"
os: linux
dist: xenial
sudo: false
cache:
directories:
- $HOME/assets
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-7
- g++-7
script: ./.travis/linux-build.sh
- env: NAME="macOS Build"
os: osx
sudo: false
osx_image: xcode10
cache:
directories:
- $HOME/assets
script: ./.travis/macos-build.sh
- env: NAME="Windows Build"
os: windows
cache:
directories:
- $HOME/assets
script: ./.travis/windows-build.sh

89
src/teakra/CMakeLists.txt Normal file
View File

@ -0,0 +1,89 @@
cmake_minimum_required(VERSION 3.8)
project(teakra CXX)
# Determine if we're built as a subproject (using add_subdirectory)
# or if this is the master project.
set(MASTER_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(MASTER_PROJECT ON)
endif()
option(TEAKRA_WARNINGS_AS_ERRORS "Warnings as errors" ${MASTER_PROJECT})
option(TEAKRA_BUILD_TOOLS "Build tools" ${MASTER_PROJECT})
option(TEAKRA_BUILD_UNIT_TESTS "Build unit tests" ${MASTER_PROJECT})
option(TEAKRA_RUN_TESTS "Run Teakra accuracy tests" OFF)
# Set hard requirements for C++
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Warn on CMake API deprecations
set(CMAKE_WARN_DEPRECATED ON)
# Disable in-source builds
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
message(SEND_ERROR "In-source builds are not allowed.")
endif()
# Add the module directory to the list of paths
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules")
# Compiler flags
if (MSVC)
set(TEAKRA_CXX_FLAGS
/std:c++latest # CMAKE_CXX_STANDARD as no effect on MSVC until CMake 3.10.
/W3
/permissive- # Stricter C++ standards conformance
/MP
/Zi
/Zo
/EHsc
/Zc:throwingNew # Assumes new never returns null
/Zc:inline # Omits inline functions from object-file output
/DNOMINMAX
/D_CRT_SECURE_NO_WARNINGS)
if (TEAKRA_WARNINGS_AS_ERRORS)
list(APPEND TEAKRA_CXX_FLAGS
/WX)
endif()
else()
set(TEAKRA_CXX_FLAGS
-Wall
-Wextra
-Wcast-qual
-pedantic
-pedantic-errors
-Wfatal-errors
-Wno-missing-braces
-Wno-unused-parameter)
if (TEAKRA_WARNINGS_AS_ERRORS)
list(APPEND TEAKRA_CXX_FLAGS
-Werror)
endif()
endif()
# Prefer the -pthread flag on Linux.
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
enable_testing()
if (NOT TEAKRA_TEST_ASSETS_DIR)
set(TEAKRA_TEST_ASSETS_DIR "${CMAKE_CURRENT_BINARY_DIR}")
endif()
# External libraries
add_subdirectory(externals)
# Teakra project files
add_subdirectory(src)
# Teakra tests
if (TEAKRA_BUILD_UNIT_TESTS)
add_subdirectory(tests)
endif()

View File

@ -0,0 +1,17 @@
# This function should be passed a name of an existing target. It will automatically generate
# file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
# one in the filesystem.
function(create_target_directory_groups target_name)
# Place any files that aren't in the source list in a separate group so that they don't get in
# the way.
source_group("Other Files" REGULAR_EXPRESSION ".")
get_target_property(target_sources "${target_name}" SOURCES)
foreach(file_name IN LISTS target_sources)
get_filename_component(dir_name "${file_name}" PATH)
# Group names use '\' as a separator even though the entire rest of CMake uses '/'...
string(REPLACE "/" "\\" group_name "${dir_name}")
source_group("${group_name}" FILES "${file_name}")
endforeach()
endfunction()

21
src/teakra/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Weiyi Wang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

21
src/teakra/README.md Normal file
View File

@ -0,0 +1,21 @@
# Teakra
[![Build Status](https://travis-ci.com/wwylele/teakra.svg?branch=master)](https://travis-ci.com/wwylele/teakra)
[![Build status](https://ci.appveyor.com/api/projects/status/mxr5tg4v8dafyqec/branch/master?svg=true)](https://ci.appveyor.com/project/wwylele/teakra/branch/master)
Emulator, (dis-)assembler, tools and documentation for XpertTeak, the DSP used by DSi/3DS.
Many thanks to Martin Korth and many other contributers for their help and their [excellent GBATEK doc](http://problemkaputt.de/gbatek.htm#dsixpertteakdsp)!
## Contents
Please refer to README.md in the following directories for their detail.
- `src` contains main source code for compiling portable libraries/tools. [Detailed documentation](src/README.md) for the Teak architecture and for peripherals is also here.
- `include` contains the header for the emulator and the disassembler libraries.
- `dsptester` contains the source code of a 3DS tool that tests processor instructions and registers
- `dspmemorytester` contains the source code of another 3DS tool that tests memory read/write, MMIO and DMA.
## General Information of the XpertTeak
The XpertTeak DSP consists of a Teak-family architecture processor, and peripheral components including DMA, interrupt controller and audio input/output ports etc. The exact architecture of the processor is still unclear. GBATEK states that the architecture is TeakLite II, the successor of the TeakLite architecture. Their evidence is the TeakLite II disassembler bundled in RealView Developer Suite. However, a Teak family debugger from [here](https://www.lauterbach.com) shows that the "TEAK(-REVA, -REVB, DEV-A0, -RTL2_0)" contains very similar registers and instruction set described in GBATEK, while the "TeakLite-II" contains very different registers and instructions. This shows that the architecture is likely the original Teak, introduced along with TeakLite as a "non-Lite" expansion to it.
DSi and 3DS both include XpertTeak. However, their uses of XpertTeak are pretty different. Most DSi games don't use it at all. It's used by the "Nintendo DSi Sound" and "Nintendo Zone" system utilities, and by the "Cooking Coach" cartridge (according to GBATEK), where it appears to be intended for audio/video decoding. On the contrary, 3DS games all use XpertTeak for audio decoding and output.

32
src/teakra/appveyor.yml Normal file
View File

@ -0,0 +1,32 @@
# shallow clone
clone_depth: 5
environment:
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
cmake_generator: "Visual Studio 15 2017 Win64"
platform:
- x64
configuration:
- Release
install:
- git submodule update --init --recursive
before_build:
- mkdir build
- cd build
- cmake .. -G "%cmake_generator%" -DTEAKRA_TEST_ASSETS_DIR="%USERPROFILE%\assets" -DTEAKRA_RUN_TESTS=ON
- cd ..
cache:
- '%USERPROFILE%\assets'
build:
project: build/teakra.sln
parallel: true
test_script:
- cd build && ctest -VV -C Release && cd ..

5
src/teakra/externals/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,5 @@
if (TEAKRA_BUILD_UNIT_TESTS)
add_library(catch INTERFACE)
target_include_directories(catch INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/catch>)
endif()

Some files were not shown because too many files have changed in this diff Show More