您的位置:首页 > 编程语言

一段比较有意思的代码——介绍system verilog中的新增幅值语句

2015-08-04 14:21 791 查看
system verilog中新加了很多幅值语句,虽然都只适用于阻塞幅值,但是在某些场合中非常实用。

下面是一段有意思的代码,覆盖了一些用法。

package definitions;
typedef enum logic [2:0] {ADD,SUB,MULT,DIV,SL,SR} opcode_t;
typedef enum logic {UNSIGNED, SIGNED} operand_type_t;
typedef union packed {
logic [23:0] u_data;
logic signed [23:0] s_data;
} data_t;
typedef struct          packed {
opcode_t opc;
operand_type_t op_type;
data_t op_a;
data_t op_b;
} instruction_t;
endpackage // definitions

import definitions::*; // import package into $unit space

module alu (input instruction_t instr, output data_t alu_out);
always_comb begin
if (instr.op_type == SIGNED) begin
alu_out.s_data = instr.op_a.s_data;
unique case (instr.opc)
ADD : alu_out.s_data += instr.op_b.s_data;
SUB : alu_out.s_data -= instr.op_b.s_data;
MULT : alu_out.s_data *= instr.op_b.s_data;
DIV : alu_out.s_data /= instr.op_b.s_data;
SL : alu_out.s_data <<<= 2;
SR : alu_out.s_data >>>= 2;
endcase
176 SystemVerilog for Design
end
else begin
alu_out.u_data = instr.op_a.u_data;
unique case (instr.opc)
ADD : alu_out.u_data += instr.op_b.u_data;
SUB : alu_out.u_data -= instr.op_b.u_data;
MULT : alu_out.u_data *= instr.op_b.u_data;
DIV : alu_out.u_data /= instr.op_b.u_data;
SL : alu_out.u_data <<= 2;
SR : alu_out.u_data >>= 2;
endcase
end
end
endmodule


代码中使用了package,structure以及一些新加的赋值语句。

注意这里使用的是always_comb,因为这些赋值语句都相当于阻塞赋值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: