Verilog 算術邏輯運算單元( ALU )
程式( ALU ):
`define ADD 2'b00
`define SUB 2'b01
`define AND 2'b10
`define OR 2'b11
module ALU( Data_A, Data_B, OP_Code, Data_Out );
parameter Data_Size = 8;
parameter OP_Code_Size = 2;
input [Data_Size-1:0] Data_A, Data_B;
input [OP_Code_Size-1:0] OP_Code;
output [Data_Size-1:0] Data_Out;
reg [Data_Size-1:0] Data_Out;
always @( Data_A, Data_B, OP_Code ) begin
case( OP_Code )
`ADD: Data_Out <= Data_A + Data_B;
`SUB: Data_Out <= Data_A - Data_B;
`AND: Data_Out <= Data_A & Data_B;
`OR: Data_Out <= Data_A | Data_B;
default: Data_Out <= 0;
endcase
end
endmodule
//========ALU========================
`define ADD 2'b00
`define SUB 2'b01
`define AND 2'b10
`define OR 2'b11
module ALU( Data_A, Data_B, OP_Code, Data_Out );
parameter Data_Size = 8;
parameter OP_Code_Size = 2;
input [Data_Size-1:0] Data_A, Data_B;
input [OP_Code_Size-1:0] OP_Code;
output [Data_Size-1:0] Data_Out;
reg [Data_Size-1:0] Data_Out;
always @( Data_A, Data_B, OP_Code ) begin
case( OP_Code )
`ADD: Data_Out <= Data_A + Data_B;
`SUB: Data_Out <= Data_A - Data_B;
`AND: Data_Out <= Data_A & Data_B;
`OR: Data_Out <= Data_A | Data_B;
default: Data_Out <= 0;
endcase
end
endmodule
//========Test_bench======================
// 時間單位 100ns, 時間精確度10 ps
`timescale 100ns/10ps
module Test_bench;
// input [Data_Size-1:0] Data_A, Data_B;
// input [OP_Code_Size-1:0] OP_Code;
// output [Data_Size-1:0] Data_Out;
parameter Data_Size = 8;
parameter OP_Code_Size = 2;
wire [Data_Size-1:0] Data_Out;
reg [Data_Size-1:0] Data_A = 8'b1010_1111;
reg [Data_Size-1:0] Data_B = 8'b1010_0101;
reg [OP_Code_Size-1:0] OP_Code=2'b00;
ALU DUT( .Data_A(Data_A), .Data_B(Data_B), .OP_Code(OP_Code), .Data_Out(Data_Out) );
// initial程序結構區塊, 產生輸入信號波形
initial begin
$monitor(Data_A, Data_B, OP_Code, Data_Out );
#100; // 100ns
OP_Code=2'b01;
#100; // 200ns
OP_Code=2'b10;
#100; // 300ns
OP_Code=2'b11;
end
initial
begin
#400; // 模擬終止時間 400 ns
$stop;
end
沒有留言:
張貼留言