注意:本文所有内容皆来源于Xilinx工程师,如需转载,请写明出处作者及赛灵思论坛链接并发邮件èï¼æªç»Xilinxåèä½æ人许å¯ï¼ç¦æ¢ç¨ä½åä¸ç¨éã
BY Alan Schuler
åºæ¬èå
å¨ SystemVerilog ä¸ï¼èååªæ¯ä¿¡å·ï¼å¯éè¿ä¸åå称å纵横æ¯æ¥å 以å¼ç¨ã
å ¶å·¥ä½æ¹å¼ä¸ºéè¿ typedef æ¥å£°æèåï¼å¹¶æä¾ä¸åæ è¯ç¬¦ç¨äºå¼ç¨æ¤èåã è¿äºæ è¯ç¬¦ç§°ä¸ºâå段âã
ä¾å¦ï¼
typedef union packed { logic [3:0] a; logic [3:0] b; } union_type; union_type my_union;
以ä¸ä»£ç å建äºä¸ç§æ°ç±»åï¼å为âunion_typeâã
æ¤ç±»åçä½å®½ä¸º 4 ä½ï¼å¯ä½ä¸ºâaâæâbâæ¥å¼ç¨ã
æ¤å¤ï¼ä»£ç æåä¸è¡å建äºä¸ä¸ªæ°ä¿¡å·ï¼å称为âmy_unionâä¸ç±»å为âunion_typeâã
å ¶ä½¿ç¨è¯æ³ä¸ºâ<signal name>.<field_name>âã
ä¾å¦ï¼
always@(posedge clk) begin my_union.a < = in1; end always@(posedge clk) begin out1 < = my_union.a; out2 < = my_union.b; end
å¨ Vivado ä¸è¿è¡æ¤ä»£ç æ¶ï¼åçå¾å¦ä¸æ示ï¼
请注æï¼my_union ä½å®½ä»ä» 为 4 ä½ï¼è以âaâæâbâæ¥å¼ç¨å®ç两项åé åéç¨ç¸åé»è¾ã é对 my_union çåé 使ç¨çæ¯âaâï¼èæ¤èåç读åç»æé对 out1 å out2 ååå«ä½¿ç¨âaâåâbâã
èåå两ç§ç±»åï¼æå (packed) å解å (unpacked)ãå¨ä¸è¿°ç¤ºä¾ä¸ï¼æ们æå®çæ¯æå èåã é»è®¤æ åµä¸ï¼å¦æä¸æå®ç±»åï¼ç¼è¯å¨å°åå®å®éç¨è§£å èåãæå èåä¸è§£å èåçå·®å«å¨äºï¼å¨æå èåä¸ï¼å ¶ä¸æææ è¯ç¬¦é½å¿ é¡»éç¨æå ç±»åï¼å¹¶ä¸å¤§å°å¿ é¡»ç¸åã å¨ä¸è¿°ç¤ºä¾ä¸ï¼âaâåâbâä½å®½å为 4 ä½ã ä½å¦æå ¶ä¸ä¹ä¸ä¸º 4 ä½ï¼èå¦ä¸ä¸ªä¸º 2 ä½ï¼åè¯¥å·¥å ·ä¸å°çæé误ã èå¨è§£å èåä¸ï¼æ è¯ç¬¦å¯éç¨è§£å ç±»å并ä¸å¤§å°æ éç¸åã å æ¤ï¼å¨ä¸è¿° 4 ä½å 2 ä½èå示ä¾ä¸ï¼å é¤âpackedâè¯å¥å°ä½¿è¯¥å·¥å ·è½å¤å¯¹ RTL è¿è¡å®æ´å®¡æ¥ã æ»èè¨ä¹ï¼æå èåå¨ç»¼åå·¥å ·ä¸æåæ¯ææ´ä¸ºå¹¿æ³ï¼å¹¶ä¸æ´ä¾¿äºæ¦å¿µåã 对äºæ¬æä¸çåå 个èå示ä¾ï¼æ们使ç¨çæ¯æå èåï¼ä½ä»æ¤å¤å¼å§ç´è³ææ«ï¼æ们å°å±ç¤ºè§£å èå示ä¾ã
å«å¤ç»´å段çèå
ä¸è¿°ç¤ºä¾åªæ¯ç®åæ¼ç¤ºäºèåçä½ç¨ã 让æ们æ¥çä¸è¾ä¸ºå¤æçèå示ä¾ï¼
typedef union packed { logic [3:0] a; logic [1:0][1:0] b; } union_type; union_type my_union;
åä¸ï¼é¦å 对èåè¿è¡å£°æï¼å¹¶å建类å为âunion_typeâçä¿¡å·ã å·®å«å¨äºï¼å段âaâä½å®½ä¸º 4 ä½ï¼å¦ä¸ä¸ªå段âbâä½å®½åæ ·ä¸º 4 ä½ï¼ä½åè æå为 2 个 2 ä½ç¢éã ç±äºè¿ä¸¤ä¸ªå段大å°ç¸åï¼å¹¶ä¸å段âbâ使ç¨çæ¯æå ç±»åï¼å æ¤è¿æ¯ä¸ä¸ªåæ³çæå èåã
å ¶ç»æå¦ä¸æ示ï¼
为æ¤ç»æåé ç RTL å¦ä¸æ示ï¼
always@(posedge clk) begin my_union.a < = in1; end always@(posedge clk) begin out1 < = my_union.b[0]; out2 < = my_union.b[1]; end
åçå¾å¦ä¸æ示ï¼
å«ç»æçèå
èåè¿å¯é åç»æä¸èµ·ä½¿ç¨ã å°±åæææå èåä¸æ ·ï¼ç»æ大å°å¿ é¡»ä¸èåä¸çä»»ä½å ¶ä»ç±»åç大å°ç¸åã
ä¾å¦ï¼
typedef union packed { logic [9:0] data; struct packed { bit op1; bit [2:0] op2; bit [1:0] op3; bit op4; bit [2:0] op5; } op_modes; } union_type; union_type my_union;
æ¤ RTL ä»ç»çèåå å« 2 个ä½å®½å为 10 ä½çå段ã 第ä¸ä¸ªå段为å为âdataâä¸ä½å®½ä¸º 10 ä½çç¢éã 第äºä¸ªå段éç¨å å« 5 个å段çç»æï¼è¿äºå段ç大å°æ»ååæ ·ä¸º 10 ä½ã
为æ¤å建çç»æå¦ä¸æ示ï¼
ç±äºå½åèåä¸å å«ç»æï¼å æ¤å ¶æ£ç¡®çå¼ç¨æ¹å¼æ¯å¼ç¨èåä¸çç»æï¼
always@(posedge clk) begin my_mult < = my_union.op_modes.op2 * my_union.op_modes.op5; end
解å èå
å¦æèåä¸çå段大å°ä¸åï¼æè å¦æèåä¸çå段æ¬èº«ä½¿ç¨çç±»å为解å ç±»åï¼é£ä¹æ¤ç±»èåé声æ为解å èåã
对äºåä¸ç§æ åµï¼å¦ææå®çèåå å«ä¸å大å°çå段ï¼é£ä¹è¯¥èåæ¬èº«å¤§å°å°è®¾ç½®ä¸ºæ大å段ç大å°ã
ç¤ºä¾ RTLï¼
typedef union { logic [5:0] a; logic [3:0] b; logic c; } union_type; union_type my_union;
è¿æ ·å³å¯å建å¦ä¸æ示ç»æï¼
å«ç»æç解å èå
ä¸æå èåç¸åï¼è§£å èååæ ·å¯ä»¥ä½¿ç¨ç»æã
typdef struct { bit [3:0] a1; bit a2; } s_1; typedef union { logic [7:0] b1; s_1 b2; } union_type union_type my_union;
以ä¸ç¤ºä¾å°å建ä¸ä¸ªå«ä¸¤ä¸ªå段çèåãå ¶ä¸ä¸ä¸ªå段为ä½å®½ 8 ä½çç¢éâb1âï¼å¦ä¸ä¸ªå段为ä½å®½ 5 ä½çç»æï¼æ¤ç»æç±ä¸ä¸ªä½å®½ 4 ä½çç¢é a1 åä¸ä¸ªä½å®½ 1 ä½çç¢é a2 ç»æã
æ¤èåå°ä½ä¸ºä½å®½ 8 ä½çç¢éæ¥å建ï¼å¦ä¸æ示ï¼
åä¸ï¼ç±äºèåä¸å å«ç»æï¼å æ¤éæå¦ä¸æ¹å¼æ¥å¼ç¨ä¿¡å·ï¼
always@(posedge clk) begin my_union.b1 <= in1; out1 <= my_union.b2.a1; out2 <= my_union.b2.a2; end
æ¥æºï¼èµçµæ论å