概要
windowsでiverilogやってみた。
練習問題、やってみた。
練習問題
AtCoderを解け。
文字列内の、'1' の個数をカウントする
サンプルコード
module test;
integer stdin = 'h8000_0000;
integer a,
b,
c,
ch,
v,
p;
reg [7:0] pc;
reg [7:0] str[0:9];
initial
begin
pc <= 10;
v = 0;
p = 0;
end
always #1
begin
case(pc)
10:
begin
ch = $fgetc(stdin);
pc <= 20;
end
20:
begin
str[v] = ch;
if (ch == 'ha)
begin
pc <= 30;
end
else
begin
v++;
pc <= 10;
end
end
30:
begin
ch = str[v];
if (ch == 'h31)
begin
p++;
end
pc <= 40;
end
40:
begin
if (v == 0)
begin
pc <= 50;
end
else
begin
v--;
pc <= 30;
end
end
50:
begin
$write("%d", p);
pc <= 60;
end
60:
begin
$finish(0);
end
endcase
end
initial
begin
//$monitor(" %d", pc);
end
endmodule
実行結果
>vvp a.out
111
3
以上。