dzamlo / rust-bitfield

This crate provides macros to generate bitfield-like struct.
Apache License 2.0
157 stars 19 forks source link

attempt to shift right with overflow' #35

Closed brandonros closed 1 year ago

brandonros commented 1 year ago

thread 'main' panicked at 'attempt to shift right with overflow', /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/bitfield-0.14.0/src/lib.rs:681:1 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

use bitfield::bitfield;

bitfield! {
    pub struct Movw(u32);
    u32;
    pub get_imm12, set_imm12: 0, 11;
    pub get_Rd, set_Rd: 12, 15;
    pub get_imm4, set_imm4: 16, 19;
    pub get_opcode, set_opcode: 20, 27;
    pub get_cond, set_cond: 28, 31;
}

fn encode_thumb2_movw(immediate: u16) -> u32 {
    println!("encode_thumb2_movw immediate = {immediate:x}");
    let mut movw = Movw(0);
    let imm4 = (immediate >> 12) & 0xF;
    let imm12 = immediate & 0xFFF;
    println!("imm4 = {imm4:x}");
    println!("imm12 = {imm12:x}");
    movw.set_imm4(imm4 as u32);
    movw.set_imm12(imm12 as u32);
    movw.set_Rd(3);
    movw.set_opcode(0b00110000);
    return movw.0;
}

fn main() {
    let instruction = encode_thumb2_movw(0x5678);
    println!("{:032b}", instruction); // This should match the given Instr Bytes
    println!("{:x}", instruction); // Hex representation
}

I'm sure I am doing something wrong....

brandonros commented 1 year ago
bitfield! {
    pub struct Movw(u32);
    u32;
    pub get_imm12, set_imm12: 11, 0;
    pub get_Rd, set_Rd: 15, 12;
    pub get_imm4, set_imm4: 19, 16;
    pub get_opcode, set_opcode: 27, 20;
    pub get_cond, set_cond: 31, 28;
}

I had "start/end" flipped.