多重Switchを使いif文の実装

7 views (last 30 days)
maruti
maruti on 3 Dec 2019
Answered: Shoumei on 3 Dec 2019
Switchブロックを使いif文を実装していますが、
条件が同じ幅で増加する条件でSwitchブロックを何個も並列で繋げるのはさすがにきれいには見えない、
読みにくくなるため質問します。
下記のif文を例としたら簡単に実装する方法は何がありますか?
always@(posedge clk or negedge resetn)begin
if(~resetn)begin
x<=4'b0000;
end
if(a==1)
x<=4'b0001;
else if(a==2)
x<=4'b0001;
else if(a==3)
x<=4'b0010;
else if(a==4)
x<=4'b0100;
else if(a==5)
x<=4'b1000;
else if(a==6)
x<=4'b1001;
.....
end
このように同じ幅を持つ条件がある場合ただ単にSwitchブロックを並ばせるしかないのでしょうか?

Accepted Answer

Shoumei
Shoumei on 3 Dec 2019
HDL Coderのご質問でしょうか?ご質問または製品欄に何の製品の質問か書いておいたほうが良いですよ。
MATLAB Functionブロックを使うと良いです。
こんなMATLABコードから
function x = fcn(a)
if a ==1
x = fi(1, 0, 4, 0);
elseif a==2
x = fi(1, 0, 4, 0);
elseif a==3
x = fi(2, 0, 4, 0);
elseif a==4
x = fi(4, 0, 4, 0);
elseif a==5
x = fi(8, 0, 4, 0);
else
x = fi(9, 0, 4, 0);
end
こんなVerilogが生成できます。
always @(a) begin
if (a == 8'd1) begin
x_1 = 4'b0001;
end
else if (a == 8'd2) begin
x_1 = 4'b0001;
end
else if (a == 8'd3) begin
x_1 = 4'b0010;
end
else if (a == 8'd4) begin
x_1 = 4'b0100;
end
else if (a == 8'd5) begin
x_1 = 4'b1000;
end
else begin
x_1 = 4'b1001;
end
 end
リセットが必要であれば、FFの記述をpersistentを使って追加しましょう。

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!