一段比較有意思的代碼——介紹system verilog中的新增幅值語句

system verilog中新加了不少幅值語句,雖然都只適用於阻塞幅值,可是在某些場合中很是實用。spa

下面是一段有意思的代碼,覆蓋了一些用法。code

 1 package definitions;
 2     typedef enum logic [2:0] {ADD,SUB,MULT,DIV,SL,SR} opcode_t;
 3     typedef enum logic {UNSIGNED, SIGNED} operand_type_t;
 4     typedef union packed {
 5         logic [23:0] u_data;
 6         logic signed [23:0] s_data;
 7     } data_t;
 8     typedef struct          packed {
 9         opcode_t opc;
10         operand_type_t op_type;
11         data_t op_a;
12         data_t op_b;
13     } instruction_t;
14 endpackage // definitions
15     
16     import definitions::*; // import package into $unit space
17     
18 module alu (input instruction_t instr, output data_t alu_out);
19     always_comb begin
20         if (instr.op_type == SIGNED) begin
21             alu_out.s_data = instr.op_a.s_data;
22             unique case (instr.opc)
23                 ADD : alu_out.s_data += instr.op_b.s_data;
24                 SUB : alu_out.s_data -= instr.op_b.s_data;
25                 MULT : alu_out.s_data *= instr.op_b.s_data;
26                 DIV : alu_out.s_data /= instr.op_b.s_data;
27                 SL : alu_out.s_data <<<= 2;
28                 SR : alu_out.s_data >>>= 2;
29             endcase
30             176 SystemVerilog for Design
31               end
32         else begin
33             alu_out.u_data = instr.op_a.u_data;
34             unique case (instr.opc)
35                 ADD : alu_out.u_data += instr.op_b.u_data;
36                 SUB : alu_out.u_data -= instr.op_b.u_data;
37                 MULT : alu_out.u_data *= instr.op_b.u_data;
38                 DIV : alu_out.u_data /= instr.op_b.u_data;
39                 SL : alu_out.u_data <<= 2;
40                 SR : alu_out.u_data >>= 2;
41             endcase
42         end
43     end
44 endmodule

代碼中使用了package,structure以及一些新加的賦值語句。blog

 

注意這裏使用的是always_comb,由於這些賦值語句都至關於阻塞賦值。input

相關文章
相關標籤/搜索