概要
windowsでiverilogやってみた。
2bitCPU、書いてみた。
cpuの仕様
レジスタマシン
- レジスタ
アキュムレータa-2bit
プログラムカウンタ2bit
キャリー1bit - メモリー
2bit2アドレス - 命令2bit
h0--a = a + 1
h1--if (c > 0) pc = 0
サンプルコード
module cpu2(input clk, input rst, output [1:0] outport);
reg [1:0] a;
reg c;
reg [1:0] pc;
reg [1:0] mem[1:0];
wire [1:0] code;
assign code = mem[pc];
assign op = code[3:2];
always @(posedge clk, negedge rst)
begin
if (!rst)
begin
mem[0] <= 4'h0; //a++
mem[1] <= 4'h1; //if (c > 0) pc = 0
a <= 2'h0;
c <= 0;
pc <= 2'h0;
end
else
begin
case (op)
2'h0://a++
begin
end
2'h1://if (c > 0) pc = 0
begin
if (c > 0)
pc = 0;
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
0 0 1
0 0 2
0 0 3
0 0 0
0 0 1
0 0 2
0 0 3
0 0 0
0 0 1
0 0 2
cpu20.v:56: $finish called at 24 (1s)
以上。