You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改SystemVerilog流运算符实现字节转32位字的字节序反转?

SystemVerilog字节队列转32位字队列的流运算符修改方案

问题说明

现有一段SystemVerilog测试代码,目的是将字节队列转换为32位字队列。当前使用{>>32{myByteQueue}}流运算符得到的是大端字节序结果,但需要输出小端字节序的32位字(每个字的字节顺序反转),且单独使用{<<32{myByteQueue}}无法得到预期结果。

修改方案

替换原流运算语句为双层流运算符组合:

myWordQueue = {<<8{>>32{myByteQueue}}};

原理解释

  • >>32:将字节队列按每4个连续字节为一组,拼接成32位字,得到大端字节序的字(比如前4个字节44,94,03,00会被拼为44940300)。
  • <<8:对每个32位字内部的8位字节单元进行反转,而非对整个32位的所有位反转。这样每个大端字会被转换为小端格式(44940300反转字节后变为00039444)。

修改后的完整代码

module tb();
  logic [31:0] myWordQueue[$];
  byte  myByteQueue [$];

  initial begin
    $display("starting");
    
    myByteQueue[0] = 8'h44;
    myByteQueue[1] = 8'h94;
    myByteQueue[2] = 8'h03;
    myByteQueue[3] = 8'h00;
    myByteQueue[4] = 8'hb8;
    myByteQueue[5] = 8'h99;
    myByteQueue[6] = 8'h03;
    myByteQueue[7] = 8'h00;
    
    myWordQueue = {<<8{>>32{myByteQueue}}}; // 修改流运算符
    
    $display("myWordQueue[0] = %h", myWordQueue[0] );
    $display("myWordQueue[1] = %h", myWordQueue[1] );
  end
endmodule    

预期输出

# starting
# myWordQueue[0] = 00039444
# myWordQueue[1] = 000399b8

内容的提问来源于stack exchange,提问作者Mikef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:57:40