diff options
author | dianshi <dianshi@main.lv> | 2022-02-14 08:04:19 +0000 |
---|---|---|
committer | dianshi <dianshi@main.lv> | 2022-02-14 08:04:19 +0000 |
commit | 464289c73853f13f6ccc4b584503c4c09988d1a0 (patch) | |
tree | 4a43523d2a721114c585ffce8b07fa3c0001ebcc /alu/v0.1_16bit | |
parent | 76593f10b0754990f154fbb3da958d696a34f2de (diff) | |
download | cpu8_v-master.tar.gz cpu8_v-master.zip |
Diffstat (limited to 'alu/v0.1_16bit')
-rw-r--r-- | alu/v0.1_16bit/Makefile | 9 | ||||
-rw-r--r-- | alu/v0.1_16bit/alu.v | 46 | ||||
-rw-r--r-- | alu/v0.1_16bit/testbench_alu.v | 64 |
3 files changed, 119 insertions, 0 deletions
diff --git a/alu/v0.1_16bit/Makefile b/alu/v0.1_16bit/Makefile new file mode 100644 index 0000000..0459ab3 --- /dev/null +++ b/alu/v0.1_16bit/Makefile @@ -0,0 +1,9 @@ +make: + iverilog -g2005-sv -o alu alu.v + iverilog -g2005-sv -o test_alu testbench_alu.v alu.v + +test: + vvp test_alu + +wave: + gtkwave test_alu.vcd diff --git a/alu/v0.1_16bit/alu.v b/alu/v0.1_16bit/alu.v new file mode 100644 index 0000000..05bf227 --- /dev/null +++ b/alu/v0.1_16bit/alu.v @@ -0,0 +1,46 @@ +module alu( + input [15:0]a,b, + input [2:0]alu_sel, + output [15:0]alu_out, + output carry_out +); + +reg [7:0]alu_result; +wire [8:0]tmp; + +assign alu_out=alu_result; //result from alu +//??? +assign tmp = {1'b0,a}+{1'b0,b}; + + +always @(*) +begin + case (alu_sel) + 4'b0000: //addition + alu_result = a+b; + 4'b0001: //substraction + alu_result = a-b; + 4'b0010: //multiplication + alu_result = ~a; + 4'b0011: //division + alu_result = a<<b; + 4'b0100: //logical shift left + alu_result = a>>b; + 4'b0101: //logical shift right + alu_result = a&b; + 4'b0110: //rotate right + alu_result = a|b; + 4'b0111: //rotate left + begin + if (a<b) alu_result = 16'd1; + else alu_result = 16'd0; + end + default: + alu_result = a+b; + endcase +end + +assign zero = (alu_result==16'd0) ? 1'b1: 1'b0; + + +endmodule
\ No newline at end of file diff --git a/alu/v0.1_16bit/testbench_alu.v b/alu/v0.1_16bit/testbench_alu.v new file mode 100644 index 0000000..9902e5b --- /dev/null +++ b/alu/v0.1_16bit/testbench_alu.v @@ -0,0 +1,64 @@ +`timescale 1ns/1ps + +module testbench_alu; + +reg [7:0]var_1=0; +reg [7:0]var_2=0; +reg [3:0]op_sel; +reg carry; + +//wire val; +//wire out; +wire [7:0]out; + +//assign val=data; +alu uut ( + .a(var_1), + .b(var_2), + .alu_sel(op_sel), + .alu_out(out), + .carry_out(carry) +); + + +initial begin + $display("addition"); + $dumpfile("test_alu.vcd"); + $dumpvars(0,testbench_alu); + var_1=0; + var_2=0; + op_sel=0; + #10 var_1=1; + var_2=0; + + #10 var_1=0; + var_2=1; + + #10 var_1=16; + var_2=16; + + #10 var_1=64; + var_2=64; + + $display("substraction"); + $display("multiplication"); + $display("division"); + $display("logical shift left"); + $display("logical shift right"); + $display("rotate right"); + $display("rotate left"); + $display("and"); + $display("or"); + $display("xor"); + $display("nor"); + $display("nand"); + $display("xnor"); + $display("greater comparison"); + $display("equal comparison"); +end + +initial begin + $monitor("At time %t d1=%h(%0d) d2=%h(%0d) op_sel=%0d out=%h(%0d)",$time,var_1,var_1,var_2,var_2,op_sel,out,out); +end + +endmodule
\ No newline at end of file |