k0b0's record.

Computer Engineering, Arts and Books

Introduction to SystemVerilog. Combination circuit (decoder, priority circuit)

組合わせ回路

 SystemVerilogで簡単な組合わせ回路を記述してみる。

デコーダ

デコーダ(decoder3to8.sv)

 3入力8出力のデコーダの記述。SystemVerilogのalways_comb文+case文を使って処理を記述。入出力信号のデータ型はlogicとしている。出力信号に関してはregでも問題はない(always_comb文を使っているためwireだとエラーとなる)。

/* decoder3to8 */
module decoder3to8 (input  logic [2:0] a,
                    output logic [7:0] y);

always_comb
begin
    case (a)
	3'b000 :  y = 8'b00000001;
	3'b001 :  y = 8'b00000010;
	3'b010 :  y = 8'b00000100;
	3'b011 :  y = 8'b00001000;
	3'b100 :  y = 8'b00010000;
	3'b101 :  y = 8'b00100000;
	3'b110 :  y = 8'b01000000;
	3'b111 :  y = 8'b10000000;
	default : y = 8'bxxxxxxxx;
    endcase
end
endmodule

優先回路

優先回路(priorityckt.sv)

 4bit入出力の優先回路。always_comb文+if文を用いて記述。

/* priorityckt.sv */
module priorityckt (input  logic [3:0] a,
                    output logic [3:0] y);

always_comb
begin
    if (a[3])      y = 4'b1000;
    else if (a[2]) y = 4'b0100;
    else if (a[1]) y = 4'b0010;
    else if (a[0]) y = 4'b0001;
    else           y = 4'b0000;
end
endmodule

優先回路(ドントケア使用)(prioritydntcr.sv)

 ドントケアを使用した優先回路をalways_comb + casez文で記述(casez文はドントケアを取り扱えるcase文)。casez文中の"?"はドントケアを表す。

/* prioritydntcr.sv */
module prioritycntcr (input  logic [3:0] a,
                      output logic [3:0] y);

always_comb
begin
    casez(a)
        4'b1??? : y = 4'b1000;
        4'b01?? : y = 4'b0100;
        4'b001? : y = 4'b0010;
        4'b0001 : y = 4'b0001;
	default : y = 4'b0000;
    endcase
end
endmodule