Skip to content

Commit b6178f8

Browse files
committed
Add sortKB integration
1 parent 9b22866 commit b6178f8

File tree

14 files changed

+365
-14
lines changed

14 files changed

+365
-14
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"editor.formatOnSave": true,
77
"editor.defaultFormatter": "esbenp.prettier-vscode"
88
},
9-
"rust-analyzer.linkedProjects": ["rust/mlogv32/Cargo.toml", "rust/demo_no_alloc/Cargo.toml"],
9+
"rust-analyzer.linkedProjects": [
10+
"rust/mlogv32/Cargo.toml",
11+
"rust/demo_no_alloc/Cargo.toml",
12+
"rust/sortKB/Cargo.toml"
13+
],
1014
"rust-analyzer.check.allTargets": false,
1115
"rust-analyzer.cargo.features": "all",
1216
"[python]": {

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ build/%-0.mlog: build/%.bin scripts/bin_to_mlog.py
2727

2828
build/rust/%.bin: FORCE | build/rust
2929
cd rust/$* && cargo robjcopy ../../build/rust/$*.bin
30+
cd rust/$* && cargo objdump --release -- --disassemble > ../../build/rust/$*.dump
3031

3132
build/%.bin: build/%.out
3233
riscv32-unknown-elf-objcopy --output-target binary build/$*.out build/$*.bin

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ This non-standard extension adds instructions to control the mlogv32 processor's
3737

3838
The MLOG instructions are encoded with an I-type instruction format using the _custom-0_ opcode. The zero-extended immediate is used as a minor opcode (funct12) for implementation reasons.
3939

40-
The MLOGSYS instruction is used for simple system controls, including halt, `printchar`, `printflush`, and `drawflush`.
40+
The MLOGSYS instruction is used for simple system controls, including halt, `printchar`, `printflush`, `drawflush`, and sortKB/kbconv integration.
4141

42-
| funct12 | rs1 | name |
43-
| ------- | ------- | ------------ |
44-
| 0 | `00000` | Halt |
45-
| 1 | char | `printchar` |
46-
| 2 | `00000` | `printflush` |
47-
| 3 | `00000` | `drawflush` |
42+
| funct12 | rs1 | rd | name |
43+
| ------- | ------- | ----------------------- | -------------------------- |
44+
| 0 | `00000` | `00000` | Halt |
45+
| 1 | char | `00000` | `printchar` |
46+
| 2 | `00000` | `00000` | `printflush` |
47+
| 3 | `00000` | `00000` | `drawflush` |
48+
| 4 | `00000` | char (0 if no new data) | Read next char from sortKB |
4849

4950
The MLOGDRAW instruction is used for drawing graphics using the Mlog `draw` instruction. Arguments are passed to this instruction using registers a0 to a5 as necessary, and any return value is placed in _rd_. If _rd_ is specified as `00000` in the below table, no value will be written to `rd` in any case.
5051

@@ -128,3 +129,7 @@ Assumes Powershell on Windows.
128129
- `uv sync`
129130
- `.venv\Scripts\activate.ps1`
130131
- Build all Rust projects in `rust/`: `make rust`
132+
133+
## Attribution
134+
135+
- SortKB: https://github.com/BasedUser/mPC

rust/demo_no_alloc/.cargo/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[build]
2-
target = "riscv32i-unknown-none-elf"
2+
target = "riscv32imac-unknown-none-elf"
33
rustflags = [
44
"-C", "link-arg=-Tlink.x",
5+
"-C", "target-feature=-c",
56
]
67

78
[unstable]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
22
channel = "nightly-2024-11-16"
33
components = ["rust-src"]
4-
targets = ["riscv32i-unknown-none-elf"]
4+
targets = ["riscv32imac-unknown-none-elf"]

rust/mlogv32/.cargo/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[build]
2-
target = "riscv32i-unknown-none-elf"
2+
target = "riscv32imac-unknown-none-elf"
33
rustflags = [
44
"-C", "link-arg=-Tlink.x",
5+
"-C", "target-feature=-c",
56
]
67

78
[unstable]

rust/mlogv32/rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
22
channel = "nightly-2024-11-16"
33
components = ["rust-src"]
4-
targets = ["riscv32i-unknown-none-elf"]
4+
targets = ["riscv32imac-unknown-none-elf"]

rust/mlogv32/src/io.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::arch::asm;
1+
use core::{arch::asm, error::Error, fmt::Display};
22

33
#[cfg(feature = "alloc")]
44
use alloc::string::ToString;
@@ -47,3 +47,41 @@ pub fn print_flush() {
4747
);
4848
}
4949
}
50+
51+
#[derive(Debug)]
52+
pub struct ReadCharError {
53+
pub value: u32,
54+
}
55+
56+
impl Display for ReadCharError {
57+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58+
#[cfg(feature = "alloc")]
59+
{
60+
write!(f, "received invalid char from kbconv: {}", self.value)
61+
}
62+
#[cfg(not(feature = "alloc"))]
63+
{
64+
write!(f, "received invalid char from kbconv")
65+
}
66+
}
67+
}
68+
69+
impl Error for ReadCharError {}
70+
71+
pub fn read_char() -> Option<Result<char, ReadCharError>> {
72+
let value: u32;
73+
74+
unsafe {
75+
asm!(
76+
".insn i CUSTOM_0, 0, {}, zero, 4",
77+
out(reg) value,
78+
options(nomem, preserves_flags, nostack),
79+
);
80+
};
81+
82+
if value == 0 {
83+
None
84+
} else {
85+
Some(char::from_u32(value).ok_or(ReadCharError { value }))
86+
}
87+
}

rust/sortKB/.cargo/config.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[build]
2+
target = "riscv32imac-unknown-none-elf"
3+
rustflags = [
4+
"-C", "link-arg=-Tlink.x",
5+
"-C", "target-feature=-c",
6+
]
7+
8+
[unstable]
9+
build-std = ["core", "compiler_builtins"]
10+
11+
[alias]
12+
rbuild = "build --release"
13+
rb = "rbuild"
14+
15+
rsize = "size --release"
16+
17+
robjdump = [
18+
"objdump",
19+
"--release",
20+
"--",
21+
"--disassemble",
22+
"--no-show-raw-insn",
23+
]
24+
rod = "robjdump"
25+
26+
robjcopy = [
27+
"objcopy",
28+
"--release",
29+
"--",
30+
"--output-target", "binary",
31+
]

rust/sortKB/Cargo.lock

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)