Skip to content

Commit

Permalink
Revise most services to follow these guidelines:
Browse files Browse the repository at this point in the history
- Each service must have xyzInit/xyzExit (with that name)
- xyzInit/xyzExit use reference counting
- xyzExit returns void
- The utilities in <3ds/result.h> are used instead of manual error checking
- The intrinsics in <3ds/synchronization.h> are used instead of inline asm
- Other miscellaneous changes
  - APT now uses a lightweight lock instead of a mutex
  - Initial handle parameters in PTMU were killed
  - Explicit init'ion to 0 or NULL has been removed for global variables
    since they end up on .bss anyway
  - MIC hasn't been touched because it must be rewritten first
  - CFGNOR needs a slight touch before converting
  - SOC is still to be cleaned up
  • Loading branch information
fincs committed Nov 7, 2015
1 parent e01dfbc commit 2797540
Show file tree
Hide file tree
Showing 45 changed files with 786 additions and 750 deletions.
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/ac.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Result acInit(void);

/// Exits AC.
Result acExit(void);
void acExit(void);

/**
* @brief Gets the current Wifi status.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/am.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef struct
Result amInit(void);

/// Exits AM.
Result amExit(void);
void amExit(void);

/// Gets the current AM session handle.
Handle *amGetSessionHandle(void);
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/cam.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Result camInit(void);
*
* This will internally call CAMU_DriverFinalize and close the handle of the service.
*/
Result camExit(void);
void camExit(void);

/**
* Begins capture on the specified camera port.
Expand Down
4 changes: 2 additions & 2 deletions libctru/include/3ds/services/cfgu.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ typedef enum
} CFG_Langage;

/// Initializes CFGU.
Result initCfgu(void);
Result cfguInit(void);

/// Exits CFGU.
Result exitCfgu(void);
void cfguExit(void);

/**
* @brief Gets the system's region from secure info.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/csnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Result CSND_Reset(void);
Result csndInit(void);

/// Exits CSND.
Result csndExit(void);
void csndExit(void);

/**
* @brief Adds a command to the list, returning a buffer to write arguments to.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Result dspInit(void);
* @brief Closes the dsp service.
* @note This will also unload the DSP binary.
*/
Result dspExit(void);
void dspExit(void);

/**
* @brief Checks if a headphone is inserted.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ typedef struct
Result fsInit(void);

/// Exits FS.
Result fsExit(void);
void fsExit(void);

/**
* @brief Gets the current FS session handle.
Expand Down
10 changes: 5 additions & 5 deletions libctru/include/3ds/services/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
* @param sharedmem_addr Address of the shared memory block to use.
* @param sharedmem_size Size of the shared memory block.
*/
Result IRU_Initialize(u32 *sharedmem_addr, u32 sharedmem_size);
Result iruInit(u32 *sharedmem_addr, u32 sharedmem_size);

/// Shuts down IRU.
Result IRU_Shutdown(void);
void iruExit(void);

/**
* @brief Gets the IRU service handle.
* @return The IRU service handle.
*/
Handle IRU_GetServHandle(void);
Handle iruGetServHandle(void);

/**
* @brief Sends IR data.
* @param buf Buffer to send data from.
* @param size Size of the buffer.
* @param wait Whether to wait for the data to be sent.
*/
Result IRU_SendData(u8 *buf, u32 size, u32 wait);
Result iruSendData(u8 *buf, u32 size, bool wait);

/**
* @brief Receives IR data.
Expand All @@ -37,7 +37,7 @@ Result IRU_SendData(u8 *buf, u32 size, u32 wait);
* @param transfercount Pointer to write the bytes read to.
* @param wait Whether to wait for the data to be received.
*/
Result IRU_RecvData(u8 *buf, u32 size, u8 flag, u32 *transfercount, u32 wait);
Result iruRecvData(u8 *buf, u32 size, u8 flag, u32 *transfercount, bool wait);

/**
* @brief Sets the IR bit rate.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/mvd.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void mvdstdGenerateDefaultConfig(mvdstdConfig *config, u32 input_width, u32 inpu
Result mvdstdInit(mvdstdMode mode, mvdstdTypeInput input_type, mvdstdTypeOutput output_type, u32 size);

/// Shuts down MVDSTD.
Result mvdstdShutdown(void);
void mvdstdExit(void);

/**
* @brief Sets the current configuration of MVDSTD.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/news.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Result newsInit(void);

/// Exits NEWS.
Result newsExit(void);
void newsExit(void);

/**
* @brief Adds a notification to the home menu Notifications applet.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/ns.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Result nsInit(void);

/// Exits NS.
Result nsExit(void);
void nsExit(void);

/**
* @brief Launches a title.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/pm.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Result pmInit(void);

/// Exits PM.
Result pmExit(void);
void pmExit(void);

/**
* @brief Launches a title.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/ps.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef enum
Result psInit(void);

/// Exits PS.
Result psExit(void);
void psExit(void);

/**
* @brief Encrypts/Decrypts AES data. Does not support AES CCM.
Expand Down
19 changes: 7 additions & 12 deletions libctru/include/3ds/services/ptm.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,43 @@
Result ptmInit(void);

/// Exits PTM.
Result ptmExit(void);
void ptmExit(void);

/// Initializes ptm:sysm.
Result ptmSysmInit(void);

/// Exits ptm:sysm.
Result ptmSysmExit(void);
void ptmSysmExit(void);

/**
* @brief Gets the system's current shell state.
* @param servhandle Optional pointer to the handle to use.
* @param out Pointer to write the current shell state to. (0 = closed, 1 = open)
*/
Result PTMU_GetShellState(Handle* servhandle, u8 *out);
Result PTMU_GetShellState(u8 *out);

/**
* @brief Gets the system's current battery level.
* @param servhandle Optional pointer to the handle to use.
* @param out Pointer to write the current battery level to. (0-5)
*/
Result PTMU_GetBatteryLevel(Handle* servhandle, u8 *out);
Result PTMU_GetBatteryLevel(u8 *out);

/**
* @brief Gets the system's current battery charge state.
* @param servhandle Optional pointer to the handle to use.
* @param out Pointer to write the current battery charge state to. (0 = not charging, 1 = charging)
*/
Result PTMU_GetBatteryChargeState(Handle* servhandle, u8 *out);
Result PTMU_GetBatteryChargeState(u8 *out);

/**
* @brief Gets the system's current pedometer state.
* @param servhandle Optional pointer to the handle to use.
* @param out Pointer to write the current pedometer state to. (0 = not counting, 1 = counting)
*/
Result PTMU_GetPedometerState(Handle* servhandle, u8 *out);
Result PTMU_GetPedometerState(u8 *out);

/**
* @brief Gets the pedometer's total step count.
* @param servhandle Optional pointer to the handle to use.
* @param steps Pointer to write the total step count to.
*/
Result PTMU_GetTotalStepCount(Handle* servhandle, u32 *steps);
Result PTMU_GetTotalStepCount(u32 *steps);

/**
* @brief Configures the New 3DS' CPU clock speed and L2 cache.
Expand Down
2 changes: 1 addition & 1 deletion libctru/include/3ds/services/y2r.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Result y2rInit(void);
*
* This will internally call Y2RU_DriverFinalize and close the handle of the service.
*/
Result y2rExit(void);
void y2rExit(void);


/**
Expand Down
2 changes: 1 addition & 1 deletion libctru/source/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void gfxWriteFramebufferInfo(gfxScreen_t screen)
framebufferInfoHeader[0x1]=1;
}

void (*screenFree)(void *) = NULL;
static void (*screenFree)(void *) = NULL;

void gfxInit(GSP_FramebufferFormats topFormat, GSP_FramebufferFormats bottomFormat, bool vrambuffers)
{
Expand Down
1 change: 1 addition & 0 deletions libctru/source/ndsp/ndsp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <3ds/types.h>
#include <3ds/result.h>
#include <3ds/svc.h>
#include <3ds/os.h>
#include <3ds/synchronization.h>
Expand Down
43 changes: 21 additions & 22 deletions libctru/source/ndsp/ndsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ static Result ndspInitialize(bool resume)
Result rc;

rc = ndspLoadComponent();
if (rc) return rc;
if (R_FAILED(rc)) return rc;

rc = svcCreateEvent(&irqEvent, 1);
if (rc) goto _fail1;
if (R_FAILED(rc)) goto _fail1;

rc = DSP_RegisterInterruptEvents(irqEvent, 2, 2);
if (rc) goto _fail2;
if (R_FAILED(rc)) goto _fail2;

rc = DSP_GetSemaphoreHandle(&dspSem);
if (rc) goto _fail3;
if (R_FAILED(rc)) goto _fail3;

DSP_SetSemaphoreMask(0x2000);

Expand Down Expand Up @@ -383,7 +383,7 @@ void ndspUseComponent(const void* binary, u32 size, u16 progMask, u16 dataMask)
componentFree = false;
}

bool ndspFindAndLoadComponent(void)
static bool ndspFindAndLoadComponent(void)
{
extern Handle __get_handle_from_list(const char* name);
Result rc;
Expand All @@ -401,19 +401,19 @@ bool ndspFindAndLoadComponent(void)
FS_path path = { PATH_CHAR, sizeof(dsp_filename), (u8*)dsp_filename };

rc = FSUSER_OpenFileDirectly(&rsrc, arch, path, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
if (rc) break;
if (R_FAILED(rc)) break;

u64 size = 0;
rc = FSFILE_GetSize(rsrc, &size);
if (rc) { FSFILE_Close(rsrc); break; }
if (R_FAILED(rc)) { FSFILE_Close(rsrc); break; }

bin = malloc(size);
if (!bin) { FSFILE_Close(rsrc); break; }

u32 dummy = 0;
rc = FSFILE_Read(rsrc, &dummy, 0, bin, size);
FSFILE_Close(rsrc);
if (rc) { free(bin); return false; }
if (R_FAILED(rc)) { free(bin); return false; }

componentBin = bin;
componentSize = size;
Expand All @@ -428,7 +428,7 @@ bool ndspFindAndLoadComponent(void)
extern u32 fake_heap_end;
u32 mapAddr = (fake_heap_end+0xFFF) &~ 0xFFF;
rc = svcMapMemoryBlock(rsrc, mapAddr, 0x3, 0x3);
if (rc) break;
if (R_FAILED(rc)) break;

componentSize = *(u32*)(mapAddr + 0x104);
bin = malloc(componentSize);
Expand All @@ -450,39 +450,39 @@ static int ndspRefCount = 0;
Result ndspInit(void)
{
Result rc = 0;
if (ndspRefCount++) return 0;
if (AtomicPostIncrement(&ndspRefCount)) return 0;

if (!componentBin && !ndspFindAndLoadComponent())
{
rc = 1018 | (41 << 10) | (4 << 21) | (27 << 27);
rc = MAKERESULT(RL_PERMANENT, RS_NOTFOUND, 41, RD_NOT_FOUND);
goto _fail0;
}

LightLock_Init(&ndspMutex);
ndspInitMaster();
ndspiInitChn();

rc = initCfgu();
if (rc==0)
rc = cfguInit();
if (R_SUCCEEDED(rc))
{
u8 outMode;
rc = CFGU_GetConfigInfoBlk2(sizeof(outMode), 0x70001, &outMode);
if (rc==0)
if (R_SUCCEEDED(rc))
ndspMaster.outputMode = outMode;
exitCfgu();
cfguExit();
}

rc = dspInit();
if (rc) return rc;
if (R_FAILED(rc)) return rc;

rc = ndspInitialize(false);
if (rc) goto _fail1;
if (R_FAILED(rc)) goto _fail1;

rc = svcCreateEvent(&sleepEvent, 0);
if (rc) goto _fail2;
if (R_FAILED(rc)) goto _fail2;

rc = svcCreateThread(&ndspThread, ndspThreadMain, 0x0, (u32*)(&ndspThreadStack[NDSP_THREAD_STACK_SIZE/8]), 0x31, -2);
if (rc) goto _fail3;
if (R_FAILED(rc)) goto _fail3;

aptHook(&aptCookie, ndspAptHook, NULL);
return 0;
Expand All @@ -499,14 +499,13 @@ Result ndspInit(void)
componentBin = NULL;
}
_fail0:
ndspRefCount--;
AtomicDecrement(&ndspRefCount);
return rc;
}

void ndspExit(void)
{
if (!ndspRefCount) return;
if (--ndspRefCount) return;
if (AtomicDecrement(&ndspRefCount)) return;
if (!bDspReady) return;
ndspThreadRun = false;
if (bSleeping)
Expand Down
3 changes: 2 additions & 1 deletion libctru/source/os.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <3ds/types.h>
#include <3ds/result.h>
#include <3ds/os.h>
#include <3ds/svc.h>
#include <3ds/services/ptm.h>
Expand Down Expand Up @@ -138,7 +139,7 @@ const char* osStrError(u32 error) {

void __ctru_speedup_config(void)
{
if (ptmSysmInit()==0)
if (R_SUCCEEDED(ptmSysmInit()))
{
PTMSYSM_ConfigureNew3DSCPU(__ctru_speedup ? 3 : 0);
ptmSysmExit();
Expand Down
Loading

0 comments on commit 2797540

Please sign in to comment.