Skip to content

Commit

Permalink
add ALU designs
Browse files Browse the repository at this point in the history
  • Loading branch information
kentaroy47 committed Nov 25, 2021
1 parent d8c084c commit 33edf96
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions 04_counter/counter_tb.sv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@


// 4b リセット付きレジスタのテストベンチ
// 0-255まで数えるカウンタのテストベンチ
module countertest;
// テスト信号を定義
reg clk;
reg rst;
wire [7:0] out;
wire [7:0] out; // 0-255のため8bit必要ですね

counter u1(.*);

Expand Down
32 changes: 19 additions & 13 deletions 05_alu/alu.sv
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
module adder(
input logic [31:0] a,
input logic [31:0] b,
output logic [31:0] out,
);
output logic [31:0] out);

assign out = a + b;
endmodule

module sub(
input logic [31:0] a,
input logic [31:0] b,
output logic [31:0] out,
output logic [31:0] out
);

assign out = a - b;
Expand All @@ -20,7 +19,7 @@ endmodule
module multiplier(
input logic [31:0] a,
input logic [31:0] b,
output logic [31:0] out,
output logic [31:0] out
);

assign out = a * b;
Expand All @@ -29,7 +28,7 @@ endmodule
module divider(
input logic [31:0] a,
input logic [31:0] b,
output logic [31:0] out,
output logic [31:0] out
);

assign out = a / b;
Expand All @@ -38,17 +37,24 @@ endmodule
module alu(
input logic [31:0] a,
input logic [31:0] b,
input logic [2:0] sel,
input logic [1:0] sel,
output logic [31:0] out
);

logic [31:0] a1
adder add(.a (a), .b (b), .out (a1))
// それぞれの演算器をモジュールとして呼び出し
logic [31:0] y1;
adder add(.a (a), .b (b), .out (y1));

logic [31:0] y2;
sub sub2(.a (a), .b (b), .out (y2));

logic [31:0] a2
sub sub2(.a (a), .b (b), .out (a2))
logic [31:0] y3;
multiplier mul(.a (a), .b (b), .out (y3));

logic [31:0] a3
multiplier mul(.a (a), .b (b), .out (a3))
logic [31:0] y4;
divider div(.a (a), .b (b), .out (y4));

if ()
// 一つの出力を選択
assign out = sel[1] ? (sel[0] ? y4 : y3)
: (sel[0] ? y2 : y1);
endmodule
28 changes: 28 additions & 0 deletions 05_alu/alu_tb.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ALUのテストベンチ
module alutest;
// テスト信号を定義
reg [31:0] a;
reg [31:0] b;
reg [1:0] sel;
wire [31:0] out;

alu u1(.*);

// 信号の動きを宣言
// 初期値
initial begin
sel = 0;
a = 0;
b = 0;
#300 $stop; // 終了時間の定義。これがないと延々に回ってしまう!
end

// 周期変動
// ノンブロッキング代入することで遷移を同時にする。
always #1
a = a + 1;
always #5
b = b + 1;
always #30
sel = sel + 1;
endmodule

0 comments on commit 33edf96

Please sign in to comment.