From f1b4780e2c29545f999a32d082d53d981cde1eab Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 15 Oct 2026 22:03:59 -0600 Subject: [PATCH] Add Rust BM1362 wire frame encoders --- src/bm1362.rs | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/bm1366.rs | 30 +------- src/bm13xx.rs | 57 +++++++++++++++ src/lib.rs | 2 + 4 files changed, 254 insertions(+), 27 deletions(-) create mode 100644 src/bm1362.rs create mode 100644 src/bm13xx.rs diff --git a/src/bm1362.rs b/src/bm1362.rs new file mode 100644 index 0000000..536b081 --- /dev/null +++ b/src/bm1362.rs @@ -0,0 +1,192 @@ +// SPDX-License-Identifier: MIT +//! BM1362 frame encoding from captured command and job traffic. +//! +//! These pure encoders do not initialize a chain. PLL fields, voltage, and +//! model-specific operating points still require silicon-backed validation. + +use crate::bm13xx::{crc16, crc5}; + +pub const JOB_BYTES: usize = 88; +pub const CHIP_ADDRESS_STEP: u8 = 2; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ModelGeometry { + pub chips_per_chain: u8, + pub ticket_mask: u8, +} + +pub const S19J_PRO: ModelGeometry = ModelGeometry { + chips_per_chain: 126, + ticket_mask: 0x7f, +}; +pub const S19J: ModelGeometry = ModelGeometry { + chips_per_chain: 108, + ticket_mask: 0x7f, +}; +pub const S19_88: ModelGeometry = ModelGeometry { + chips_per_chain: 88, + ticket_mask: 0x3f, +}; + +impl ModelGeometry { + /// Addresses for an observed two-address-step enumeration walk. + pub fn chip_addresses(self) -> impl Iterator { + (0..self.chips_per_chain).map(|index| index * CHIP_ADDRESS_STEP) + } +} + +fn short_command(code: u8, address: u8, register: u8) -> [u8; 7] { + let mut frame = [0x55, 0xaa, code, 0x05, address, register, 0]; + frame[6] = crc5(&frame[2..6]); + frame +} + +fn long_command(code: u8, address: u8, register: u8, value: u32) -> [u8; 11] { + let mut frame = [0_u8; 11]; + frame[..6].copy_from_slice(&[0x55, 0xaa, code, 0x09, address, register]); + frame[6..10].copy_from_slice(&value.to_be_bytes()); + frame[10] = crc5(&frame[2..10]); + frame +} + +pub fn chain_inactive() -> [u8; 7] { + short_command(0x53, 0, 0) +} + +pub fn set_chip_address(address: u8) -> [u8; 7] { + short_command(0x40, address, 0) +} + +pub fn read_register(address: u8, register: u8) -> [u8; 7] { + short_command(0x42, address, register) +} + +pub fn read_register_all(register: u8) -> [u8; 7] { + short_command(0x52, 0, register) +} + +pub fn write_register(address: u8, register: u8, value: u32) -> [u8; 11] { + long_command(0x41, address, register, value) +} + +pub fn write_register_all(register: u8, value: u32) -> [u8; 11] { + long_command(0x51, 0, register, value) +} + +/// Raw header fields in BM1362 wire order. The caller owns work-ID allocation +/// and must supply roots and hashes in the order established by its capture. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct Job { + pub work_id: u8, + pub num_midstates: u8, + pub start_nonce: u32, + pub nbits: u32, + pub ntime: u32, + pub merkle_root: [u8; 32], + pub previous_hash: [u8; 32], + pub version: u32, +} + +/// Encode the 88-byte SEND_JOB form with the true-length tag observed on wire. +pub fn encode_job(job: &Job) -> [u8; JOB_BYTES] { + let mut frame = [0_u8; JOB_BYTES]; + frame[..6].copy_from_slice(&[0x55, 0xaa, 0x21, 0x56, job.work_id, job.num_midstates]); + frame[6..10].copy_from_slice(&job.start_nonce.to_le_bytes()); + frame[10..14].copy_from_slice(&job.nbits.to_le_bytes()); + frame[14..18].copy_from_slice(&job.ntime.to_le_bytes()); + frame[18..50].copy_from_slice(&job.merkle_root); + frame[50..82].copy_from_slice(&job.previous_hash); + frame[82..86].copy_from_slice(&job.version.to_le_bytes()); + let checksum = crc16(&frame[2..86]); + frame[86..88].copy_from_slice(&checksum.to_be_bytes()); + frame +} + +/// BT8D for a 25 MHz input clock with BCLK_SEL=0. A model must still choose +/// the actual target baud and confirm switching on silicon. +pub fn bt8d_for_baud(baud: u32) -> Option { + if baud == 0 { + return None; + } + let divider = 25_000_000_u64 / (8 * u64::from(baud)); + if divider == 0 { + return Some(0); + } + u8::try_from(divider - 1).ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn captured_command_frames() { + assert_eq!(chain_inactive(), [0x55, 0xaa, 0x53, 5, 0, 0, 3]); + assert_eq!(set_chip_address(0), [0x55, 0xaa, 0x40, 5, 0, 0, 0x1c]); + assert_eq!(set_chip_address(8), [0x55, 0xaa, 0x40, 5, 8, 0, 7]); + assert_eq!(read_register_all(0), [0x55, 0xaa, 0x52, 5, 0, 0, 0x0a]); + assert_eq!( + read_register(0x40, 0x1c), + [0x55, 0xaa, 0x42, 5, 0x40, 0x1c, 0x0b] + ); + assert_eq!( + write_register_all(0x80, 0), + [0x55, 0xaa, 0x51, 9, 0, 0x80, 0, 0, 0, 0, 0x1c] + ); + assert_eq!( + write_register(0x40, 0x18, 0x7a31), + [0x55, 0xaa, 0x41, 9, 0x40, 0x18, 0, 0, 0x7a, 0x31, 0x11] + ); + } + + #[test] + fn model_address_limits() { + assert_eq!(S19J_PRO.chip_addresses().last(), Some(0xfa)); + assert_eq!(S19J.chip_addresses().last(), Some(0xd6)); + assert_eq!(S19_88.chip_addresses().last(), Some(0xae)); + } + + #[test] + fn job_frame_layout() { + let mut merkle_root = [0_u8; 32]; + let mut previous_hash = [0_u8; 32]; + for index in 0..32 { + merkle_root[index] = 0xa0 + index as u8; + previous_hash[index] = 0x10 + index as u8; + } + let frame = encode_job(&Job { + work_id: 8, + num_midstates: 1, + start_nonce: 0, + nbits: 0x1a1f_2c34, + ntime: 0x5e9b_3cd4, + merkle_root, + previous_hash, + version: 0x2000_0000, + }); + assert_eq!( + &frame[..18], + &[ + 0x55, 0xaa, 0x21, 0x56, 8, 1, 0, 0, 0, 0, 0x34, 0x2c, 0x1f, 0x1a, 0xd4, 0x3c, 0x9b, + 0x5e + ] + ); + assert_eq!(&frame[18..50], &merkle_root); + assert_eq!(&frame[50..82], &previous_hash); + assert_eq!(&frame[82..86], &[0, 0, 0, 0x20]); + assert_eq!(&frame[86..88], &[0xc2, 0xbd]); + assert_eq!( + u16::from_be_bytes([frame[86], frame[87]]), + crc16(&frame[2..86]) + ); + } + + #[test] + fn baud_dividers() { + assert_eq!(bt8d_for_baud(0), None); + assert_eq!(bt8d_for_baud(3_000_000), Some(0)); + assert_eq!(bt8d_for_baud(115_200), Some(26)); + assert_eq!(bt8d_for_baud(1), None); + assert_eq!(bt8d_for_baud(u32::MAX), Some(0)); + } +} diff --git a/src/bm1366.rs b/src/bm1366.rs index 432edc1..d3cdeb7 100644 --- a/src/bm1366.rs +++ b/src/bm1366.rs @@ -4,6 +4,9 @@ pub const WORK_BYTES: usize = 88; pub const REPORT_BYTES: usize = 11; +pub use crate::bm13xx::crc16; +use crate::bm13xx::crc5; + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct NonceReport { pub nonce: u32, @@ -20,33 +23,6 @@ pub enum ReportError { Checksum, } -pub fn crc16(bytes: &[u8]) -> u16 { - let mut value = 0xffff_u16; - for byte in bytes { - value ^= u16::from(*byte) << 8; - for _ in 0..8 { - value = if value & 0x8000 != 0 { - (value << 1) ^ 0x1021 - } else { - value << 1 - }; - } - } - value -} - -fn crc5(bytes: &[u8]) -> u8 { - let mut value = 0x1f_u8; - for byte in bytes { - for shift in (0..8).rev() { - let feedback = ((value >> 4) ^ (byte >> shift)) & 1; - value = ((value << 1) | feedback) & 0x1f; - value ^= feedback << 2; - } - } - value -} - fn reverse_words(input: &[u8]) -> [u8; 32] { let mut output = [0_u8; 32]; for index in 0..8 { diff --git a/src/bm13xx.rs b/src/bm13xx.rs new file mode 100644 index 0000000..c07d1ec --- /dev/null +++ b/src/bm13xx.rs @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: MIT +//! Shared BM13xx wire checksums. This module never generates mining nonces. + +/// CRC5 used by command and nonce-report frames, initial value 0x1f. +pub fn crc5(bytes: &[u8]) -> u8 { + let mut state = 0x1f_u8; + for &byte in bytes { + for bit in (0..8).rev() { + let feedback = ((state >> 4) ^ (byte >> bit)) & 1; + state = ((state << 1) | feedback) & 0x1f; + state ^= feedback << 2; + } + } + state +} + +/// CRC16-CCITT-FALSE used by 88-byte SEND_JOB frames. +pub fn crc16(bytes: &[u8]) -> u16 { + let mut state = 0xffff_u16; + for &byte in bytes { + state ^= u16::from(byte) << 8; + for _ in 0..8 { + state = if state & 0x8000 == 0 { + state << 1 + } else { + (state << 1) ^ 0x1021 + }; + } + } + state +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn captured_command_checksums() { + let vectors: &[(&[u8], u8)] = &[ + (&[0x53, 0x05, 0, 0], 0x03), + (&[0x40, 0x05, 0, 0], 0x1c), + (&[0x52, 0x05, 0, 0], 0x0a), + (&[0x40, 0x05, 8, 0], 0x07), + (&[0x42, 0x05, 0x40, 0x1c], 0x0b), + (&[0x51, 0x09, 0, 0x80, 0, 0, 0, 0], 0x1c), + (&[0x41, 0x09, 0x40, 0x18, 0, 0, 0x7a, 0x31], 0x11), + ]; + for &(body, expected) in vectors { + assert_eq!(crc5(body), expected); + } + } + + #[test] + fn standard_crc16_check() { + assert_eq!(crc16(b"123456789"), 0x29b1); + } +} diff --git a/src/lib.rs b/src/lib.rs index e0fbae5..9c45c35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,9 @@ //! An entry in the target matrix is a development obligation, not a claim that //! the pairing already mines. Hardware backends are supplied by SDK images. +pub mod bm1362; pub mod bm1366; +pub mod bm13xx; pub mod difficulty; pub mod hal_wire; pub mod miner;