Skip to content

Commit e08a9c8

Browse files
authored
Add @nx.js/clkrst package (#163)
1 parent e5bd2d5 commit e08a9c8

File tree

12 files changed

+329
-0
lines changed

12 files changed

+329
-0
lines changed

.changeset/sharp-bikes-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nx.js/clkrst": major
3+
---
4+
5+
Initial release

docs/app/toggle.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
TbClockBolt,
23
TbGlobe,
34
TbBrowser,
45
TbBraces,
@@ -17,6 +18,12 @@ export function Toggle() {
1718
url: '/runtime',
1819
icon: <TbGlobe size={24} />,
1920
},
21+
{
22+
title: '@nx.js/clkrst',
23+
description: 'Overclocking API',
24+
url: '/clkrst',
25+
icon: <TbClockBolt size={24} />,
26+
},
2027
{
2128
title: '@nx.js/constants',
2229
description: 'Constants and enums',

docs/content/clkrst/examples.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Examples
3+
---
4+
5+
```typescript title="src/main.ts"
6+
import { openSession, PcvModuleId } from '@nx.js/clkrst';
7+
8+
// get CPU clock rate
9+
const session = openSession(PcvModuleId.CpuBus);
10+
console.log(session.getClockRate());
11+
12+
// get list of possible CPU clock rates
13+
const { rates } = session.getPossibleClockRates();
14+
15+
// overclock CPU **TO THE MAX**
16+
session.setClockRate(rates[rates.length - 1]);
17+
```

docs/content/clkrst/index.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Installation
3+
---
4+
5+
<InstallTabs items={['npm', 'pnpm', 'yarn', 'bun']}>
6+
<Tab value='npm'>
7+
8+
```bash
9+
npm install @nx.js/clkrst
10+
```
11+
12+
</Tab>
13+
<Tab value="pnpm">
14+
15+
```bash
16+
pnpm add @nx.js/clkrst
17+
```
18+
19+
</Tab>
20+
<Tab value='yarn'>
21+
22+
```bash
23+
yarn add @nx.js/clkrst
24+
```
25+
26+
</Tab>
27+
<Tab value='bun'>
28+
29+
```bash
30+
bun add @nx.js/clkrst
31+
```
32+
33+
</Tab>
34+
35+
</InstallTabs>

docs/content/clkrst/meta.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"root": true,
3+
"pages": ["--- Usage ---", "index", "examples", "--- API Reference ---", "...api"]
4+
}

docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"@nx.js/constants": "workspace:*",
23+
"@nx.js/clkrst": "workspace:*",
2324
"@nx.js/http": "workspace:*",
2425
"@nx.js/repl": "workspace:*",
2526
"@types/mdx": "^2.0.13",

packages/clkrst/package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@nx.js/clkrst",
3+
"version": "0.0.0",
4+
"description": "`clkrst` service IPC wrapper for nx.js",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/TooTallNate/nx.js.git",
8+
"directory": "packages/clkrst"
9+
},
10+
"homepage": "https://nxjs.n8.io/clkrst",
11+
"main": "dist/index.js",
12+
"scripts": {
13+
"build": "tsc",
14+
"test": "vitest",
15+
"docs": "typedoc && ../../type-aliases-meta.sh"
16+
},
17+
"files": [
18+
"dist"
19+
],
20+
"publishConfig": {
21+
"access": "public"
22+
},
23+
"dependencies": {
24+
"@nx.js/constants": "^0.2.0"
25+
},
26+
"devDependencies": {
27+
"@nx.js/runtime": "workspace:*",
28+
"vite": "^5.4.2",
29+
"vitest": "^2.0.5"
30+
},
31+
"keywords": [
32+
"nx.js",
33+
"switch",
34+
"clkrst",
35+
"overclock",
36+
"cpu",
37+
"gpu"
38+
],
39+
"author": "Nathan Rajlich <[email protected]>",
40+
"license": "MIT"
41+
}

packages/clkrst/src/index.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { SfBufferAttr } from '@nx.js/constants';
2+
import { PcvClockRatesListType, PcvModuleId } from './types';
3+
4+
export * from './types';
5+
6+
const clkrst = new Switch.Service('clkrst');
7+
8+
export function openSession(moduleId: PcvModuleId, unk = 3) {
9+
//Result clkrstOpenSession(ClkrstSession* session_out, PcvModuleId module_id, u32 unk) {
10+
// const struct {
11+
// u32 module_id;
12+
// u32 unk;
13+
// } in = { module_id, unk };
14+
// return serviceDispatchIn(&g_clkrstSrv, 0, in,
15+
// .out_num_objects = 1,
16+
// .out_objects = &session_out->s,
17+
// );
18+
//}
19+
const sessionSrv = new Switch.Service();
20+
const inArr = new Uint32Array([moduleId, unk]);
21+
clkrst.dispatchIn(0, inArr.buffer, {
22+
outObjects: [sessionSrv],
23+
});
24+
return new ClkrstSession(sessionSrv);
25+
}
26+
27+
export class ClkrstSession {
28+
#srv: Switch.Service;
29+
30+
constructor(srv: Switch.Service) {
31+
this.#srv = srv;
32+
}
33+
34+
getClockRate() {
35+
//Result clkrstGetClockRate(ClkrstSession* session, u32 *out_hz) {
36+
// return serviceDispatchOut(&session->s, 8, *out_hz);
37+
//}
38+
const outHz = new Uint32Array(1);
39+
this.#srv.dispatchOut(8, outHz.buffer);
40+
return outHz[0];
41+
}
42+
43+
getPossibleClockRates(maxCount = 20): {
44+
type: PcvClockRatesListType;
45+
rates: Uint32Array;
46+
} {
47+
//Result clkrstGetPossibleClockRates(ClkrstSession *session, u32 *rates, s32 max_count, PcvClockRatesListType *out_type, s32 *out_count) {
48+
// struct {
49+
// s32 type;
50+
// s32 count;
51+
// } out;
52+
//
53+
// Result rc = serviceDispatchInOut(&session->s, 10, max_count, out,
54+
// .buffer_attrs = { SfBufferAttr_Out | SfBufferAttr_HipcAutoSelect, },
55+
// .buffers = { { rates, max_count * sizeof(u32) }, }
56+
// );
57+
//}
58+
const rates = new Uint32Array(maxCount);
59+
const inArr = new Int32Array([rates.length]);
60+
const out = new Int32Array(2);
61+
this.#srv.dispatchInOut(10, inArr.buffer, out.buffer, {
62+
bufferAttrs: [SfBufferAttr.Out | SfBufferAttr.HipcAutoSelect],
63+
buffers: [rates.buffer],
64+
});
65+
return {
66+
type: out[0],
67+
rates: rates.slice(0, out[1]),
68+
};
69+
}
70+
71+
setClockRate(hz: number) {
72+
//Result clkrstSetClockRate(ClkrstSession* session, u32 hz) {
73+
// return serviceDispatchIn(&session->s, 7, hz);
74+
//}
75+
this.#srv.dispatchIn(7, new Uint32Array([hz]).buffer);
76+
}
77+
}

packages/clkrst/src/types.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/// Module id returned by [8.0.0+] pcv services
2+
/// See also: https://switchbrew.org/wiki/PCV_services#Modules
3+
export enum PcvModuleId {
4+
CpuBus = 0x40000001,
5+
GPU = 0x40000002,
6+
I2S1 = 0x40000003,
7+
I2S2 = 0x40000004,
8+
I2S3 = 0x40000005,
9+
PWM = 0x40000006,
10+
I2C1 = 0x02000001,
11+
I2C2 = 0x02000002,
12+
I2C3 = 0x02000003,
13+
I2C4 = 0x02000004,
14+
I2C5 = 0x02000005,
15+
I2C6 = 0x02000006,
16+
SPI1 = 0x07000000,
17+
SPI2 = 0x07000001,
18+
SPI3 = 0x07000002,
19+
SPI4 = 0x07000003,
20+
DISP1 = 0x40000011,
21+
DISP2 = 0x40000012,
22+
ISP = 0x40000013,
23+
VI = 0x40000014,
24+
SDMMC1 = 0x40000015,
25+
SDMMC2 = 0x40000016,
26+
SDMMC3 = 0x40000017,
27+
SDMMC4 = 0x40000018,
28+
OWR = 0x40000019,
29+
CSITE = 0x4000001a,
30+
TSEC = 0x4000001b,
31+
MSELECT = 0x4000001c,
32+
HDA2CODEC_2X = 0x4000001d,
33+
ACTMON = 0x4000001e,
34+
I2C_SLOW = 0x4000001f,
35+
SOR1 = 0x40000020,
36+
SATA = 0x40000021,
37+
HDA = 0x40000022,
38+
XUSB_CORE_HOST = 0x40000023,
39+
XUSB_FALCON = 0x40000024,
40+
XUSB_FS = 0x40000025,
41+
XUSB_CORE_DEV = 0x40000026,
42+
XUSB_SS_HOSTDEV = 0x40000027,
43+
UARTA = 0x03000001,
44+
UARTB = 0x35000405,
45+
UARTC = 0x3500040f,
46+
UARTD = 0x37000001,
47+
HOST1X = 0x4000002c,
48+
ENTROPY = 0x4000002d,
49+
SOC_THERM = 0x4000002e,
50+
VIC = 0x4000002f,
51+
NVENC = 0x40000030,
52+
NVJPG = 0x40000031,
53+
NVDEC = 0x40000032,
54+
QSPI = 0x40000033,
55+
VI_I2C = 0x40000034,
56+
TSECB = 0x40000035,
57+
APE = 0x40000036,
58+
ACLK = 0x40000037,
59+
UARTAPE = 0x40000038,
60+
EMC = 0x40000039,
61+
PLLE0_0 = 0x4000003a,
62+
PLLE0_1 = 0x4000003b,
63+
DSI = 0x4000003c,
64+
MAUD = 0x4000003d,
65+
DPAUX1 = 0x4000003e,
66+
MIPI_CAL = 0x4000003f,
67+
UART_FST_MIPI_CAL = 0x40000040,
68+
OSC = 0x40000041,
69+
SCLK = 0x40000042,
70+
SOR_SAFE = 0x40000043,
71+
XUSB_SS = 0x40000044,
72+
XUSB_HOST = 0x40000045,
73+
XUSB_DEV = 0x40000046,
74+
EXTPERIPH1 = 0x40000047,
75+
AHUB = 0x40000048,
76+
HDA2HDMICODEC = 0x40000049,
77+
PLLP5 = 0x4000004a,
78+
USBD = 0x4000004b,
79+
USB2 = 0x4000004c,
80+
PCIE = 0x4000004d,
81+
AFI = 0x4000004e,
82+
PCIEXCLK = 0x4000004f,
83+
PEX_USB_UPHY = 0x40000050,
84+
XUSB_PADCTL = 0x40000051,
85+
APBDMA = 0x40000052,
86+
USB2_TRK = 0x40000053,
87+
PLLE0_2 = 0x40000054,
88+
PLLE0_3 = 0x40000055,
89+
CEC = 0x40000056,
90+
EXTPERIPH2 = 0x40000057,
91+
}
92+
93+
// Clock list type returned by GetPossibleClockRates
94+
export enum PcvClockRatesListType {
95+
Invalid = 0,
96+
Discrete = 1,
97+
Range = 2,
98+
}

packages/clkrst/tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"target": "es2022",
5+
"declaration": true,
6+
"sourceMap": true,
7+
"moduleResolution": "Bundler",
8+
"forceConsistentCasingInFileNames": true,
9+
"strict": true,
10+
"skipLibCheck": true,
11+
"types": [
12+
"@nx.js/runtime"
13+
]
14+
},
15+
"include": [
16+
"src/**/*.ts"
17+
]
18+
}

0 commit comments

Comments
 (0)