Verilogのfunctionの書き方
functionの書き方のサンプル。function ではじまり endfunction で終わる。関数名と同じ変数(?)にセットされた値が返り値となる。
code:instructions.v
// add rd, rs1, rs2
// rd = rs1 + rs2
);
add = {
7'b0, // funct7
rs2,
rs1,
3'b000, // funct3
rd,
7'b0110011 // opCode
};
endfunction
定義したfunction呼び出し方。
code:cpu_test.v
// add $1, $1, $1
instr = add(5'b1, 5'b1, 5'b1);
readData = 32'bX;
n_reset = 1;
assert (
result === 32'h01FE &&
instrAddr === 32'h0004 &&
dataAddr === 32'h01FE &&
writeData === 32'h00FF &&
we === 1'b0
) $display("4 PASSED"); else $display("FAILED %h %h %h %h %b", result, instrAddr, dataAddr, writeData, we);