ALU
This commit is contained in:
9
Makefile
9
Makefile
@@ -1,6 +1,15 @@
|
||||
clean:
|
||||
-rm *.vcd *.vvp
|
||||
|
||||
alu_tb: alu_tb.vcd
|
||||
gtkwave alu_tb.vcd &
|
||||
|
||||
alu_tb.vcd: alu_tb.vvp
|
||||
vvp alu_tb.vvp
|
||||
|
||||
alu_tb.vvp: alu.v alu_tb.v
|
||||
iverilog -o alu_tb.vvp alu_tb.v
|
||||
|
||||
inc16_tb: inc16_tb.vcd
|
||||
gtkwave inc16_tb.vcd &
|
||||
|
||||
|
||||
49
alu.v
Normal file
49
alu.v
Normal file
@@ -0,0 +1,49 @@
|
||||
`ifndef _alu_v
|
||||
`define _alu_v
|
||||
|
||||
`include "not16.v"
|
||||
`include "mux4way16.v"
|
||||
`include "add16.v"
|
||||
`include "and16.v"
|
||||
`include "or8way.v"
|
||||
`include "nor.v"
|
||||
|
||||
module ALU (input [15:0] x,
|
||||
input [15:0] y,
|
||||
input zx,
|
||||
input nx,
|
||||
input zy,
|
||||
input ny,
|
||||
input f,
|
||||
input no,
|
||||
output [15:0] out,
|
||||
output zr,
|
||||
output ng);
|
||||
|
||||
wire [15:0] x_inv, x_arg;
|
||||
wire [15:0] y_inv, y_arg;
|
||||
wire [15:0] sum, prod;
|
||||
wire [15:0] sum_inv, prod_inv;
|
||||
wire zr0, zr1;
|
||||
|
||||
Not16 u1 (.in(x), .out(x_inv));
|
||||
Mux4Way16 u2 (.a(x), .b(x_inv), .c(16'h0000), .d(16'hFFFF), .sel({zx, nx}), .out(x_arg));
|
||||
|
||||
Not16 u3 (.in(y), .out(y_inv));
|
||||
Mux4Way16 u4 (.a(y), .b(y_inv), .c(16'h0000), .d(16'hFFFF), .sel({zy, ny}), .out(y_arg));
|
||||
|
||||
Add16 u5 (.a(x_arg), .b(y_arg), .out(sum));
|
||||
And16 u6 (.a(x_arg), .b(y_arg), .out(prod));
|
||||
Not16 u7 (.in(sum), .out(sum_inv));
|
||||
Not16 u8 (.in(prod), .out(prod_inv));
|
||||
Mux4Way16 u9 (.a(prod), .b(sum), .c(prod_inv), .d(sum_inv), .sel({no, f}), .out(out));
|
||||
|
||||
Or8Way u10 (.in(out[15:8]), .out(zr0));
|
||||
Or8Way u11 (.in(out[7:0]), .out(zr1));
|
||||
Nor u12 (.a(zr0), .b(zr1), .out(zr));
|
||||
|
||||
assign ng=out[15];
|
||||
|
||||
endmodule
|
||||
|
||||
`endif
|
||||
79
alu_tb.v
Normal file
79
alu_tb.v
Normal file
@@ -0,0 +1,79 @@
|
||||
`include "alu.v"
|
||||
|
||||
module ALU_test;
|
||||
reg [15:0] x, y;
|
||||
reg zx, nx, zy, ny, f, no;
|
||||
wire [15:0] out;
|
||||
wire zr, ng;
|
||||
|
||||
integer i;
|
||||
|
||||
initial begin
|
||||
$dumpfile("alu_tb.vcd");
|
||||
$dumpvars;
|
||||
|
||||
x=16'hAA55;
|
||||
y=16'h5555;
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b101010;
|
||||
#1; // should get 0
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b111111;
|
||||
#1; // should get 1
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b111010;
|
||||
#1; // should get -1
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b001100;
|
||||
#1; // should return 0xAA55
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b110000;
|
||||
#1; // should return 0x5555
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b001101;
|
||||
#1; // should return 0x55AA
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b110001;
|
||||
#1; // should return 0xAAAA
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b001111;
|
||||
#1; // should return 0x55AB
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b110011;
|
||||
#1; // should return 0xAAAB
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b011111;
|
||||
#1; // should return 0xAA56
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b110111;
|
||||
#1; // should return 0x5556
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b001110;
|
||||
#1; // should return 0xAA54
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b110010;
|
||||
#1; // should return 0x5554
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b000010;
|
||||
#1; // should return 0xFFAA
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b010011;
|
||||
#1; // should return 0x5500
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b110010;
|
||||
#1; // should return 0x5554
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b000111;
|
||||
#1; // should return 0xAB00
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b000000;
|
||||
#1; // should return 0x0055
|
||||
|
||||
{zx, nx, zy, ny, f, no}=6'b010101;
|
||||
#1; // should return 0xFF55
|
||||
|
||||
$finish();
|
||||
end
|
||||
|
||||
ALU u1(.x(x), .y(y), .zx(zx), .nx(nx), .zy(zy), .ny(ny), .f(f), .no(no), .out(out), .zr(zr), .ng(ng));
|
||||
endmodule
|
||||
Reference in New Issue
Block a user