|
1 | | -# MC UART Server |
| 1 | +# StarrySky MC-Server |
2 | 2 |
|
3 | | -Minimal Minecraft Java Edition 1.8.x server for StarrySky C2 using a UART byte stream. |
| 3 | +`starrysky-mc-server` 是一个给 StarrySky C2 板卡运行的最小 Minecraft Java Edition 1.8.x 服务器。它不是通用 Minecraft 服务端;目标是把协议、地图和传输栈压到 C2 的资源约束里,让本机 Minecraft 客户端通过串口和板卡上的固件交互。 |
4 | 4 |
|
5 | | -## Target |
| 5 | +默认发布配置面向 Minecraft Java 1.8.x / protocol 47,离线模式,单人,启用协议压缩。主机侧 Rust bridge 监听 `127.0.0.1:25565`,把 TCP 字节流转换成项目自定义的 UART link v2 帧;板卡固件在 UART0/SYS_UART/type-c 上接收桥接数据,在 UART1/HP_UART 上输出日志。 |
6 | 6 |
|
7 | | -- Board: StarrySky C2 |
8 | | -- CPU: PicoRV32 RV32IMAC, 72 MHz |
9 | | -- Default bridge data link: UART0/SYS_UART/type-c at 115200 baud |
10 | | -- Default log output: UART1/HP_UART at 115200 baud |
11 | | -- Minecraft protocol: Java Edition 1.8.x, protocol version 47 |
12 | | -- Server mode: offline, no encryption, protocol compression enabled, one player |
13 | | - |
14 | | -## Architecture |
| 7 | +## 运行架构 |
15 | 8 |
|
16 | 9 | ```text |
17 | | -Minecraft client |
| 10 | +Minecraft Java 1.8.x client |
18 | 11 | <-> TCP 127.0.0.1:25565 |
19 | | - <-> Rust bridge |
20 | | - <-> type-c serial port at 115200 baud |
| 12 | + <-> bridge/mc-uart-bridge |
| 13 | + <-> UART link v2 over 115200 baud serial |
21 | 14 | <-> StarrySky C2 UART0/SYS_UART |
22 | | - <-> C server core |
| 15 | + <-> firmware/main.c |
| 16 | + <-> core Minecraft server |
23 | 17 |
|
24 | 18 | Firmware logs |
25 | 19 | -> StarrySky C2 UART1/HP_UART at 115200 baud |
26 | 20 | ``` |
27 | 21 |
|
28 | | -## Commands |
| 22 | +核心分层: |
| 23 | + |
| 24 | +- `core/` 是平台无关 C 代码,包含 Minecraft VarInt/packet writer、服务器状态机、link codec、环形缓冲、默认世界和压缩 chunk 运行时。 |
| 25 | +- `firmware/` 绑定 StarrySky C2 SDK,负责 UART、PSRAM、主循环、link session、日志和复位后的会话清理。 |
| 26 | +- `bridge/` 是 Rust 主机工具,负责 TCP 监听、串口帧解码、PC-to-firmware ACK/重传、速率探测和 movement 包过滤。 |
| 27 | +- `nix/` 是可复现构建入口,默认固件构建会生成压缩地图资产,并导出固件内存占用报告。 |
| 28 | + |
| 29 | +更完整的实现说明见 [docs/README.md](docs/README.md) 和 [docs/architecture.md](docs/architecture.md)。 |
| 30 | + |
| 31 | +## 目标平台与默认配置 |
| 32 | + |
| 33 | +- Board:StarrySky C2。 |
| 34 | +- CPU:PicoRV32 RV32IMAC,72 MHz。 |
| 35 | +- 内存:片上 SRAM 128 KiB,外部 PSRAM 8 MiB。 |
| 36 | +- 默认 bridge UART:UART0/SYS_UART/type-c,115200 baud。 |
| 37 | +- 默认 log UART:UART1/HP_UART,115200 baud。 |
| 38 | +- Minecraft:Java Edition 1.8.x,protocol 47。 |
| 39 | +- 服务端模式:offline,无加密,单人。 |
| 40 | +- 默认地图:3x3 spawn chunks,superflat 风格,离线 zlib 压缩后在启动时复制到 PSRAM。 |
| 41 | + |
| 42 | +## 快速构建 |
| 43 | + |
| 44 | +推荐使用 Nix: |
29 | 45 |
|
30 | 46 | ```bash |
31 | | -nix build |
32 | 47 | nix build .#native-tests |
33 | 48 | nix build .#bridge |
34 | | -nix flake check |
35 | | -nix develop |
| 49 | +nix build .#firmware-c2 |
36 | 50 | ``` |
37 | 51 |
|
38 | | -Inside `nix develop`: |
| 52 | +常用包: |
39 | 53 |
|
40 | 54 | ```bash |
41 | | -make firmware |
42 | | -make test-native |
43 | | -make bridge |
| 55 | +nix build .#firmware-c2 |
| 56 | +nix build .#firmware-c2-aggressive |
| 57 | +nix build .#firmware-c2-legacy |
| 58 | +nix build .#log-debug |
| 59 | +nix build .#bridge-windows |
| 60 | +nix flake check |
44 | 61 | ``` |
45 | 62 |
|
46 | | -Firmware builds print an upstream-style memory usage table in the Nix build |
47 | | -log and install the same table beside the firmware artifacts: |
| 63 | +也可以进入开发 shell 后使用 Make 包装命令: |
48 | 64 |
|
49 | 65 | ```bash |
50 | | -nix build .#firmware-c2 |
51 | | -cat result/memory-report.txt |
| 66 | +nix develop |
| 67 | +make test-native |
| 68 | +make bridge |
| 69 | +make firmware |
52 | 70 | ``` |
53 | 71 |
|
54 | | -The report separates FLASH, SRAM, and PSRAM. The PSRAM row includes explicit |
55 | | -runtime arena usage such as the compressed-map assets copied into PSRAM at |
56 | | -boot. |
| 72 | +固件产物在 `result/` 下: |
57 | 73 |
|
58 | | -## UART Roles and Logging |
59 | | - |
60 | | -Firmware UART configuration lives in `firmware/mc_firmware_config.h`. |
61 | | - |
62 | | -Defaults: |
63 | | - |
64 | | -```c |
65 | | -#define MC_UART0_BAUD 115200u |
66 | | -#define MC_UART1_BAUD 115200u |
67 | | -#define MC_BRIDGE_UART_ID MC_UART_ID_0 |
68 | | -#define MC_LOG_UART_ID MC_UART_ID_1 |
69 | | -#define MC_LOG_LEVEL MC_LOG_INFO |
70 | | -``` |
| 74 | +- `mc_uart_fw.bin`:用于刷写的固件镜像。 |
| 75 | +- `mc_uart_fw.elf` / `mc_uart_fw`:ELF。 |
| 76 | +- `mc_uart_fw.hex`:HEX。 |
| 77 | +- `mc_uart_fw.txt`:反汇编文本。 |
| 78 | +- `memory-report.txt`:FLASH、SRAM、PSRAM 占用报告。 |
71 | 79 |
|
72 | | -`MC_BRIDGE_UART_ID` and `MC_LOG_UART_ID` must be different so firmware logs never share the bridge data link. To reverse the UART responsibilities, set `MC_BRIDGE_UART_ID` to `MC_UART_ID_1` and `MC_LOG_UART_ID` to `MC_UART_ID_0` in a local development override. |
| 80 | +构建与运行细节见 [docs/build-and-run.md](docs/build-and-run.md)。 |
73 | 81 |
|
74 | | -Firmware logging supports `MC_LOG_OFF`, `MC_LOG_INFO`, `MC_LOG_DEBUG`, and `MC_LOG_TRACE`. `INFO` logs boot, link, state, and error events; `DEBUG` adds more detailed packet and byte-flow diagnostics; `TRACE` adds raw received data dumps. |
| 82 | +## 运行 Bridge |
75 | 83 |
|
76 | | -The maintained debug firmware keeps the default compressed-map PSRAM profile and selects `MC_LOG_DEBUG`: |
| 84 | +Linux 示例: |
77 | 85 |
|
78 | 86 | ```bash |
79 | | -nix build .#log-debug |
| 87 | +mc-uart-bridge --serial /dev/ttyUSB0 --baud 115200 --listen 127.0.0.1:25565 |
80 | 88 | ``` |
81 | 89 |
|
82 | | -## Reference Policy |
83 | | - |
84 | | -Do not guess board registers, UART status bits, memory ranges, Minecraft packet IDs, packet fields, or chunk layouts. Check `docs/protocol/references.md` before changing those areas. |
| 90 | +Windows 示例: |
85 | 91 |
|
86 | | -## Flashing |
| 92 | +```powershell |
| 93 | +.\mc-uart-bridge.exe --serial COM5 --baud 115200 --listen 127.0.0.1:25565 |
| 94 | +``` |
87 | 95 |
|
88 | | -Automatic flashing is not part of this project. The build produces firmware artifacts only. |
| 96 | +需要导出 bridge 日志时,PowerShell 推荐把日志写到当前目录: |
89 | 97 |
|
90 | | -## Running the Bridge |
| 98 | +```powershell |
| 99 | +$log = ".\bridge-$(Get-Date -Format yyyyMMdd-HHmmss).log" |
| 100 | +cmd /c ".\mc-uart-bridge.exe --serial COM5 --baud 115200 --listen 127.0.0.1:25565 --verbose 2>&1" | Tee-Object -FilePath $log |
| 101 | +``` |
91 | 102 |
|
92 | | -The bridge and firmware use UART link protocol V2. V2 frames are COBS-encoded |
93 | | -and terminated with `0x00`, so corrupted frames are discarded at the next |
94 | | -delimiter. PC-to-firmware DATA frames use stop-and-wait ACK/retransmission and |
95 | | -absolute ACK credit. Firmware-to-PC DATA frames are not ACKed in this version; |
96 | | -the bridge treats any decode, CRC, or DATA_M2C sequence error as fatal and exits |
97 | | -so link corruption is visible during testing. |
| 103 | +启动 Minecraft Java Edition 1.8.x,添加服务器 `127.0.0.1:25565`。 |
98 | 104 |
|
99 | | -The default `firmware-c2` package uses Minecraft protocol compression, offline |
100 | | -precompressed spawn chunks, and PSRAM runtime chunk storage. Build |
101 | | -`firmware-c2-legacy` only when you need the old SRAM map fallback. |
| 105 | +## 协议和传输 |
102 | 106 |
|
103 | | -After the bootstrap chunks are queued, the firmware sends Play KeepAlive |
104 | | -packets on a fixed interval. KeepAlive ids advance each interval; replies clear |
105 | | -the pending marker for diagnostics, but the firmware does not disconnect the |
106 | | -Minecraft session for missed replies. Reset the board when you need to force a |
107 | | -fresh server session. |
| 107 | +Minecraft 层: |
108 | 108 |
|
109 | | -MCU-to-PC firmware transmit is conservative by default. Build |
110 | | -`firmware-c2-aggressive` to use the validated MCU-to-PC aggressive profile; it |
111 | | -keeps the stable frame size, TX budget, UART burst, and 115200 baud, and lowers |
112 | | -only UART0 TX pacing. |
| 109 | +- 握手和 Status 请求使用普通 Minecraft frame。 |
| 110 | +- Login 阶段默认先发送 `Set Compression`,再发送压缩格式的 `Login Success`。 |
| 111 | +- Play 阶段发送 Join Game、Spawn Position、Time、Health、Player Position And Look、spawn chunks 和 KeepAlive。 |
| 112 | +- KeepAlive id 每次递增;客户端 ACK 只用于诊断,固件不会因为 missed keepalive 主动踢出玩家。 |
113 | 113 |
|
114 | | -PC-to-firmware traffic remains conservative. It calibrates per-byte pacing with |
115 | | -RATE_PROBE frames, waits for the link to settle, warms up at the slowest profile, |
116 | | -then requires three formal probes per profile. A 3/3 profile is stable; a 2/3 |
117 | | -profile is treated as borderline and stops probing. Real DATA is written one |
118 | | -byte at a time using one supported profile slower than the fastest stable or |
119 | | -borderline result, and downshifts on retransmit timeouts. |
| 114 | +UART link v2: |
120 | 115 |
|
121 | | -The bridge drops Minecraft 1.8 serverbound movement packets before forwarding |
122 | | -PC-to-firmware DATA so idle movement spam cannot fill the UART link. KeepAlive |
123 | | -responses and other serverbound packets are preserved. |
| 116 | +- 帧体包含 version、type、flags、seq、ack、payload length、payload 和 CRC16。 |
| 117 | +- 整帧用 COBS 编码,并以 `0x00` 分隔。 |
| 118 | +- PC-to-firmware DATA 使用 stop-and-wait、ACK、绝对 credit 和超时重传。 |
| 119 | +- Firmware-to-PC DATA 当前不做 ACK;bridge 遇到解码、CRC 或 `DATA_M2C sequence mismatch` 会退出,让链路损坏在测试时显性暴露。 |
| 120 | +- Bridge 会丢弃 Minecraft 1.8 serverbound movement 包,避免客户端空闲移动上报填满低速 UART;KeepAlive 回复和非 movement 包会继续转发。 |
124 | 121 |
|
125 | | -The default bridge UART baud remains 115200 for conservative bring-up and for |
126 | | -the validated aggressive profile. If you build custom firmware with a different |
127 | | -`MC_UART0_BAUD` or `MC_UART1_BAUD`, run the bridge with the matching `--baud` |
128 | | -value. |
| 122 | +自定义 UART link v2 的完整格式见 [docs/protocol/uart-link-v2.md](docs/protocol/uart-link-v2.md)。协议来源和具体选择见 [docs/protocol/references.md](docs/protocol/references.md)。 |
129 | 123 |
|
130 | | -```bash |
131 | | -nix build .#firmware-c2 |
132 | | -nix build .#firmware-c2-legacy |
133 | | -nix build .#firmware-c2-aggressive |
134 | | -nix build .#log-debug |
135 | | -``` |
| 124 | +## UART 与日志 |
136 | 125 |
|
137 | | -Use `log-debug` firmware for gameplay tests that need firmware diagnostics. |
138 | | -`MC_LOG_TRACE` is intended only for short local diagnostics because raw frame |
139 | | -dumps can slow the firmware main loop enough to change UART timing. |
| 126 | +固件 UART 配置在 [firmware/mc_firmware_config.h](firmware/mc_firmware_config.h): |
140 | 127 |
|
141 | | -Linux example: |
142 | | - |
143 | | -```bash |
144 | | -mc-uart-bridge --serial /dev/ttyUSB0 --baud 115200 --listen 127.0.0.1:25565 |
| 128 | +```c |
| 129 | +#define MC_UART0_BAUD 115200u |
| 130 | +#define MC_UART1_BAUD 115200u |
| 131 | +#define MC_BRIDGE_UART_ID MC_UART_ID_0 |
| 132 | +#define MC_LOG_UART_ID MC_UART_ID_1 |
| 133 | +#define MC_LOG_LEVEL MC_LOG_INFO |
145 | 134 | ``` |
146 | 135 |
|
147 | | -Windows example: |
148 | | - |
149 | | -```powershell |
150 | | -mc-uart-bridge.exe --serial COM3 --baud 115200 --listen 127.0.0.1:25565 |
151 | | -``` |
| 136 | +`MC_BRIDGE_UART_ID` 和 `MC_LOG_UART_ID` 必须不同,避免日志字节污染 bridge 数据链路。 |
152 | 137 |
|
153 | | -During MCU-to-PC testing, a bridge exit with a serial decode error, CRC error, |
154 | | -or `DATA_M2C sequence mismatch` means the link lost or corrupted bytes. |
| 138 | +日志级别: |
155 | 139 |
|
156 | | -The bridge waits `1000ms` before retransmitting unacknowledged PC-to-firmware |
157 | | -DATA by default. Use `--c2m-retransmit-timeout-ms` only when intentionally |
158 | | -testing PC-to-firmware behavior; the stable and aggressive firmware profiles do |
159 | | -not require changing it. |
| 140 | +- `MC_LOG_OFF`:关闭日志。 |
| 141 | +- `MC_LOG_INFO`:启动、登录、连接复位和关键错误。 |
| 142 | +- `MC_LOG_DEBUG`:额外的帧、KeepAlive、回压和链路诊断。 |
| 143 | +- `MC_LOG_TRACE`:原始数据 dump,仅用于短时诊断,可能改变串口时序。 |
160 | 144 |
|
161 | | -Build a Windows bridge executable from Nix on Linux: |
| 145 | +`log-debug` 包使用默认压缩地图 PSRAM 配置,并把固件日志级别设为 `MC_LOG_DEBUG`: |
162 | 146 |
|
163 | 147 | ```bash |
164 | | -nix build .#bridge-windows |
| 148 | +nix build .#log-debug |
165 | 149 | ``` |
166 | 150 |
|
167 | | -Start Minecraft Java Edition 1.8.x and add server `127.0.0.1:25565`. |
| 151 | +## 参考来源 |
| 152 | + |
| 153 | +Minecraft 1.8、StarrySky C2、ECOS SDK 和本项目协议选择的来源见 [docs/protocol/references.md](docs/protocol/references.md)。 |
0 commit comments