概要
windowsでiverilogやってみた。
練習問題、やってみた。
練習問題
2bit、cpuを書け。
サンプルコード
module cpu2(input clk, input rst, output [1:0] outport);
reg [1:0] a,
c;
reg [1:0] pc;
reg [1:0] mem[1:0];
wire [1:0] code;
assign code = mem[pc];
always @(posedge clk, negedge rst)
begin
if (!rst)
begin
mem[0] <= 2'h0; //a++
mem[1] <= 2'h1; //if (a > 0) pc = 0
a <= 0;
c <= 0;
pc <= 0;
end
else
begin
case (code)
2'h0:
begin
a <= a + 1;
end
2'h1:
begin
if (a > 0)
begin
a <= 0;
pc <= 0;
end
end
endcase
pc = pc + 1;
end
end
initial
begin
$monitor("%d %d %d", a, c, pc);
end
endmodule
module test;
reg clk,
rst;
wire [1:0] outport;
cpu2 u(.clk(clk), .rst(rst), .outport(outport));
initial
begin
clk = 0;
rst = 1;
#2
rst = 0;
#2
rst = 1;
#20
$finish;
end
always
#1
clk = ~clk;
initial
begin
//$monitor("%d", outport);
end
endmodule
実行結果
>vvp a.out
x x x
0 0 0
1 0 1
0 0 0
1 0 1
0 0 0
1 0 1
0 0 0
1 0 1
0 0 0
1 0 1
0 0 0
cpu20.v:59: $finish called at 24 (1s)
以上。