利用for循环实现对信号的赋值。如下案例中,func_id_vld为512bit,需要根据func_mode_in[1024-1:0]给func_id_vld赋值,func_mode_in每2个bit对应一个func_id_vld,即func_mode_in[1:0]对应func_id_vld[0],func_mode_in[3:2]对应func_id_vld[1],func_mode_in[1023:1022]对应func_id_vld[511], 赋值规则为func_mode_in[1:0]为2‘b01时,func_id_vld[0]为1,以此类推。采用for(int*)类型格式,可以在一个else分支中使用,不需要使用generate,使用更加灵活,使用范围更广。
- input [1024-1:0] func_mode_in;
- reg [512-1:0] func_id_vld;
-
- always@(posedge clk)
- if(~rst_n) begin
- func_id_vld <= 512'b0 ;
- end else begin
- for(int i=0; i<512;i=i+1) begin : func_id_vld_gen
- func_id_vld[i] <= (func_mode_in[2*i+:2]==2'b01) ;
- end
- end
下图所示,采用for(int*)类型格式实现了一个16mux1的循环赋值语句,实现不同条件一下,对debug_test_16mux1的赋值操作,即16mux1的逻辑。
- input [128*16-1:0] debug_test_in;
- input [4-1:0] cfg_16mux1_mode;
- reg [128-1:0] debug_test_16mux1;
-
-
- always@(*) begin
- debug_test_16mux1 = 128'd0;
- for(int i=0; i<16;i=i+1) begin : debug_test_16mux1_gen
- if(cfg_16mux1_mode ==i) begin
- debug_test_16mux1 = debug_test_in[128*i+:128] ;
- break ;
- end
- end
- end
如上for循环语句实现了如下的case语句效果
- always@(*) begin
- case(cfg_16mux1_mode)
- 0 : debug_test_16mux1 = debug_test_in[127 :0 ];
- 1 : debug_test_16mux1 = debug_test_in[255 :128 ];
- 2 : debug_test_16mux1 = debug_test_in[383 :256 ];
- 3 : debug_test_16mux1 = debug_test_in[511 :384 ];
- 4 : debug_test_16mux1 = debug_test_in[639 :512 ];
- 5 : debug_test_16mux1 = debug_test_in[767 :640 ];
- 6 : debug_test_16mux1 = debug_test_in[895 :768 ];
- 7 : debug_test_16mux1 = debug_test_in[1023 :896 ];
- 8 : debug_test_16mux1 = debug_test_in[1151 :1024 ];
- 9 : debug_test_16mux1 = debug_test_in[1279 :1152 ];
- 10: debug_test_16mux1 = debug_test_in[1407 :1280 ];
- 11: debug_test_16mux1 = debug_test_in[1535 :1408 ];
- 12: debug_test_16mux1 = debug_test_in[1663 :1536 ];
- 13: debug_test_16mux1 = debug_test_in[1791 :1664 ];
- 14: debug_test_16mux1 = debug_test_in[1919 :1792 ];
- 15: debug_test_16mux1 = debug_test_in ;
- endcase
-
- end
NOTE:在for(int*)语句中,debug_test_16mux1 = debug_test_in[128*i+127:128*i] ; 会报语法错误,语法错误,只识别debug_test_in[128*i+:128]而无法识别debug_test_in[128*i+127:128*i] ,否则VCS会报语法错误。