配列のビット情報の収集

配列内の1の数をカウント

$countonesは配列内の1の数を数えて返します。

記述例

8ビットの変数a00の1の値を8’b1010_1101 と設定した場合、1の数は5つあるので “5” を返します。

module test()
  logic [7:0] a00 ;
  initial begin 
    a00 = 8'b1010_1101 ; 
    t_print_one(a00) ;
  end 
 
  task t_print_one(logic [7:0] val) ;
    $display("one num = %d",$countones(val)) ;
  endtask
endmodule 

実行結果

one num = 5

配列内の指定した値の数をカウント

$countbitsは指定した値の数を返します。 指定できる値は、0、 1、 x、 z です。

記述例

module test() ;
  logic [7:0] a00 ;
  initial begin
    $display("1st") ;
    a00 = 8'b1010_1101 ;
    t_print_bit(a00) ;
    
    $display("2nd") ;
    a00 = 8'b00xx_01zz ;
    t_print_bit(a00) ;
  end
  task t_print_bit(logic [7:0] val) ;
    $display("0: %d",$countbits(val)) ;
    $display("1: %d",$countbits(val)) ;
    $display("x: %d",$countbits(val)) ;
    $display("z: %d",$countbits(val)) ;
  endtask
endmodule
    

実行結果

1st
0: 3
1: 5
x: 0
z: 0
2nd
0: 3
1: 1
x: 2
z: 2

onehotとonehot0

$onehotは$countonesの結果が1であれば1を返し、そうでなければ0を返します。

$onehot0は$countonesの値が0または1のときに1を返し、そうでなければ0を返します。

記述例

module test() ;
  logic [7:0] a00 ;
  logic [7:0] b00 ;
  logic [7:0] c00 ;

  initial begin
    a00=8'b0101_0011 ;
    b00=8'b0001_0000 ;
    c00=8'b0000_0000 ;

    $display("RESULT onehot0");
    $display("onehot =%d",$onehot(a00)) ;
    $display("onehot =%d",$onehot(b00)) ;

    $display("RESULT onehot0");
    $display("onehot0 =%d",$onehot0(a00)) ;
    $display("onehot0 =%d",$onehot0(b00)) ;
    $display("onehot0 =%d",$onehot0(c00)) ;
  end
endmodule      

実行結果

RESULT onehot
onehot =0
onehot =1
RESULT onhot0
onhot0 =0
onhot0 =1
onhot0 =1

コメント

Copied title and URL