Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @nx.js/clkrst package #163

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-bikes-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nx.js/clkrst": major
---

Initial release
7 changes: 7 additions & 0 deletions docs/app/toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
TbClockBolt,
TbGlobe,
TbBrowser,
TbBraces,
Expand All @@ -17,6 +18,12 @@ export function Toggle() {
url: '/runtime',
icon: <TbGlobe size={24} />,
},
{
title: '@nx.js/clkrst',
description: 'Overclocking API',
url: '/clkrst',
icon: <TbClockBolt size={24} />,
},
{
title: '@nx.js/constants',
description: 'Constants and enums',
Expand Down
17 changes: 17 additions & 0 deletions docs/content/clkrst/examples.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Examples
---

```typescript title="src/main.ts"
import { openSession, PcvModuleId } from '@nx.js/clkrst';

// get CPU clock rate
const session = openSession(PcvModuleId.CpuBus);
console.log(session.getClockRate());

// get list of possible CPU clock rates
const { rates } = session.getPossibleClockRates();

// overclock CPU **TO THE MAX**
session.setClockRate(rates[rates.length - 1]);
```
35 changes: 35 additions & 0 deletions docs/content/clkrst/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Installation
---

<InstallTabs items={['npm', 'pnpm', 'yarn', 'bun']}>
<Tab value='npm'>

```bash
npm install @nx.js/clkrst
```

</Tab>
<Tab value="pnpm">

```bash
pnpm add @nx.js/clkrst
```

</Tab>
<Tab value='yarn'>

```bash
yarn add @nx.js/clkrst
```

</Tab>
<Tab value='bun'>

```bash
bun add @nx.js/clkrst
```

</Tab>

</InstallTabs>
4 changes: 4 additions & 0 deletions docs/content/clkrst/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"pages": ["--- Usage ---", "index", "examples", "--- API Reference ---", "...api"]
}
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"devDependencies": {
"@nx.js/constants": "workspace:*",
"@nx.js/clkrst": "workspace:*",
"@nx.js/http": "workspace:*",
"@nx.js/repl": "workspace:*",
"@types/mdx": "^2.0.13",
Expand Down
41 changes: 41 additions & 0 deletions packages/clkrst/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@nx.js/clkrst",
"version": "0.0.0",
"description": "`clkrst` service IPC wrapper for nx.js",
"repository": {
"type": "git",
"url": "https://github.com/TooTallNate/nx.js.git",
"directory": "packages/clkrst"
},
"homepage": "https://nxjs.n8.io/clkrst",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"test": "vitest",
"docs": "typedoc && ../../type-aliases-meta.sh"
},
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"dependencies": {
"@nx.js/constants": "^0.2.0"
},
"devDependencies": {
"@nx.js/runtime": "workspace:*",
"vite": "^5.4.2",
"vitest": "^2.0.5"
},
"keywords": [
"nx.js",
"switch",
"clkrst",
"overclock",
"cpu",
"gpu"
],
"author": "Nathan Rajlich <[email protected]>",
"license": "MIT"
}
77 changes: 77 additions & 0 deletions packages/clkrst/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { SfBufferAttr } from '@nx.js/constants';
import { PcvClockRatesListType, PcvModuleId } from './types';

export * from './types';

const clkrst = new Switch.Service('clkrst');

export function openSession(moduleId: PcvModuleId, unk = 3) {
//Result clkrstOpenSession(ClkrstSession* session_out, PcvModuleId module_id, u32 unk) {
// const struct {
// u32 module_id;
// u32 unk;
// } in = { module_id, unk };
// return serviceDispatchIn(&g_clkrstSrv, 0, in,
// .out_num_objects = 1,
// .out_objects = &session_out->s,
// );
//}
const sessionSrv = new Switch.Service();
const inArr = new Uint32Array([moduleId, unk]);
clkrst.dispatchIn(0, inArr.buffer, {
outObjects: [sessionSrv],
});
return new ClkrstSession(sessionSrv);
}

export class ClkrstSession {
#srv: Switch.Service;

constructor(srv: Switch.Service) {
this.#srv = srv;
}

getClockRate() {
//Result clkrstGetClockRate(ClkrstSession* session, u32 *out_hz) {
// return serviceDispatchOut(&session->s, 8, *out_hz);
//}
const outHz = new Uint32Array(1);
this.#srv.dispatchOut(8, outHz.buffer);
return outHz[0];
}

getPossibleClockRates(maxCount = 20): {
type: PcvClockRatesListType;
rates: Uint32Array;
} {
//Result clkrstGetPossibleClockRates(ClkrstSession *session, u32 *rates, s32 max_count, PcvClockRatesListType *out_type, s32 *out_count) {
// struct {
// s32 type;
// s32 count;
// } out;
//
// Result rc = serviceDispatchInOut(&session->s, 10, max_count, out,
// .buffer_attrs = { SfBufferAttr_Out | SfBufferAttr_HipcAutoSelect, },
// .buffers = { { rates, max_count * sizeof(u32) }, }
// );
//}
const rates = new Uint32Array(maxCount);
const inArr = new Int32Array([rates.length]);
const out = new Int32Array(2);
this.#srv.dispatchInOut(10, inArr.buffer, out.buffer, {
bufferAttrs: [SfBufferAttr.Out | SfBufferAttr.HipcAutoSelect],
buffers: [rates.buffer],
});
return {
type: out[0],
rates: rates.slice(0, out[1]),
};
}

setClockRate(hz: number) {
//Result clkrstSetClockRate(ClkrstSession* session, u32 hz) {
// return serviceDispatchIn(&session->s, 7, hz);
//}
this.#srv.dispatchIn(7, new Uint32Array([hz]).buffer);
}
}
98 changes: 98 additions & 0 deletions packages/clkrst/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/// Module id returned by [8.0.0+] pcv services
/// See also: https://switchbrew.org/wiki/PCV_services#Modules
export enum PcvModuleId {
CpuBus = 0x40000001,
GPU = 0x40000002,
I2S1 = 0x40000003,
I2S2 = 0x40000004,
I2S3 = 0x40000005,
PWM = 0x40000006,
I2C1 = 0x02000001,
I2C2 = 0x02000002,
I2C3 = 0x02000003,
I2C4 = 0x02000004,
I2C5 = 0x02000005,
I2C6 = 0x02000006,
SPI1 = 0x07000000,
SPI2 = 0x07000001,
SPI3 = 0x07000002,
SPI4 = 0x07000003,
DISP1 = 0x40000011,
DISP2 = 0x40000012,
ISP = 0x40000013,
VI = 0x40000014,
SDMMC1 = 0x40000015,
SDMMC2 = 0x40000016,
SDMMC3 = 0x40000017,
SDMMC4 = 0x40000018,
OWR = 0x40000019,
CSITE = 0x4000001a,
TSEC = 0x4000001b,
MSELECT = 0x4000001c,
HDA2CODEC_2X = 0x4000001d,
ACTMON = 0x4000001e,
I2C_SLOW = 0x4000001f,
SOR1 = 0x40000020,
SATA = 0x40000021,
HDA = 0x40000022,
XUSB_CORE_HOST = 0x40000023,
XUSB_FALCON = 0x40000024,
XUSB_FS = 0x40000025,
XUSB_CORE_DEV = 0x40000026,
XUSB_SS_HOSTDEV = 0x40000027,
UARTA = 0x03000001,
UARTB = 0x35000405,
UARTC = 0x3500040f,
UARTD = 0x37000001,
HOST1X = 0x4000002c,
ENTROPY = 0x4000002d,
SOC_THERM = 0x4000002e,
VIC = 0x4000002f,
NVENC = 0x40000030,
NVJPG = 0x40000031,
NVDEC = 0x40000032,
QSPI = 0x40000033,
VI_I2C = 0x40000034,
TSECB = 0x40000035,
APE = 0x40000036,
ACLK = 0x40000037,
UARTAPE = 0x40000038,
EMC = 0x40000039,
PLLE0_0 = 0x4000003a,
PLLE0_1 = 0x4000003b,
DSI = 0x4000003c,
MAUD = 0x4000003d,
DPAUX1 = 0x4000003e,
MIPI_CAL = 0x4000003f,
UART_FST_MIPI_CAL = 0x40000040,
OSC = 0x40000041,
SCLK = 0x40000042,
SOR_SAFE = 0x40000043,
XUSB_SS = 0x40000044,
XUSB_HOST = 0x40000045,
XUSB_DEV = 0x40000046,
EXTPERIPH1 = 0x40000047,
AHUB = 0x40000048,
HDA2HDMICODEC = 0x40000049,
PLLP5 = 0x4000004a,
USBD = 0x4000004b,
USB2 = 0x4000004c,
PCIE = 0x4000004d,
AFI = 0x4000004e,
PCIEXCLK = 0x4000004f,
PEX_USB_UPHY = 0x40000050,
XUSB_PADCTL = 0x40000051,
APBDMA = 0x40000052,
USB2_TRK = 0x40000053,
PLLE0_2 = 0x40000054,
PLLE0_3 = 0x40000055,
CEC = 0x40000056,
EXTPERIPH2 = 0x40000057,
}

// Clock list type returned by GetPossibleClockRates
export enum PcvClockRatesListType {
Invalid = 0,
Discrete = 1,
Range = 2,
}
18 changes: 18 additions & 0 deletions packages/clkrst/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "es2022",
"declaration": true,
"sourceMap": true,
"moduleResolution": "Bundler",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"types": [
"@nx.js/runtime"
]
},
"include": [
"src/**/*.ts"
]
}
7 changes: 7 additions & 0 deletions packages/clkrst/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.json"],
"name": "@nx.js/clkrst",
"entryPoints": ["./src/index.ts"],
"out": "docs"
}
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading