Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Verilog by hii ( 7 years ago )
module ALU (A, B, sel, Cin, Y, Zero);
input [32 - 1 : 0] A, B;
input [3 : 0] sel;
input Cin;
output [32 - 1 : 0] Y;
output Zero;
wire Out_AND , Out_OR , Out_NOT , Out_XOR , Out_XNOR , Out_NOR ;
wire [ 32 - 1 : 0 ] Out_DEC ;
wire [ 5 - 1 : 0 ] Out_ARB ;
wire [ 32 - 1 : 0 ] Out_SUB = A - B ;
wire first_bit_of_A = A[ 0 ] ;
wire last_bit_of_A = A[ 31 ] ;
AND AND_1 ( .a( A[ 0 ] ) , .b( B[ 0 ] ) , .c( Out_AND ) ) ;
OR OR_1 ( .a( A[ 0 ] ) , .b( B[ 0 ] ) , .c( Out_OR ) ) ;
NOT NOT_1 ( .a( A[ 0 ] ) , .b( Out_NOT ) ) ;
XOR XOR_1 ( .a( A[ 0 ] ) , .b( B[ 0 ] ) , .c( Out_XOR ) ) ;
XNOR XNOR_1 ( .a( A[ 0 ] ) , .b( B[ 0 ] ) , .c( Out_XNOR ) ) ;
NOR NOR_1 ( .a( A[ 0 ] ) , .b( B[ 0 ] ) , .c( Out_NOR ) ) ;
Decoder DEC_1 ( .a( A[ 5 - 1 : 0 ] ) , .b( Out_DEC[ 32 - 1 : 0 ] ) ) ;
Arbiter ARB_1 ( .r( A[ 5 - 1 : 0 ] ) , .g( Out_ARB[ 5 - 1 : 0 ] ) ) ;
always @( * ) begin
case( sel )
4'b0000 : Y = { 31'b0 , Out_AND } ;
4'b0001 : Y = { 31'b0 , Out_OR } ;
4'b0010 : Y = { 31'b0 , Out_NOT } ;
4'b0011 : Y = { 31'b0 , Out_XOR } ;
4'b0100 : Y = { 31'b0 , Out_XNOR } ;
4'b0101 : Y = { 31'b0 , Out_NOR } ;
4'b0110 : Y = A + B + Cin ;
4'b0111 : Y = Out_SUB ;
4'b1000 : begin
if ( Out_SUB[ 31 ] ) begin
Y = 1 + ( ~ Out_SUB[ 31 : 0 ] ) ;
end
else begin
Y = Out_SUB[ 31 : 0 ] ;
end
end
4'b1001 : Y = A[ 15 : 0 ] * B[ 15 : 0 ] ;
4'b1010 : Y = A << 1'b1 ;
4'b1011 : Y = ( A << 1'b1 ) | first_bit_of_A ;
4'b1100 : Y = ( A >> 1'b1 ) ;
4'b1101 : Y = ( A >> 1'b1 ) | last_bit_of_A ;
4'b1110 : Y = Out_DEC[ 31 : 0 ] ;
4'b1111 : Y = Out_ARB[ 5 - 1 : 0 ] ;
endcase
end
endmodule
module Arbiter(r, g); // find-first-one unit
input [5 - 1 : 0] r ;
output[5 - 1 : 0] g ;
reg [ 5 - 1 : 0 ] g ;
always @ (*) begin
casex( r )
5'b00000 : g = 5'b00000 ;
5'bxxxx1 : g = 5'b00001 ;
5'bxxx10 : g = 5'b00010 ;
5'bxx100 : g = 5'b00100 ;
5'bx1000 : g = 5'b01000 ;
5'b10000 : g = 5'b10000 ;
default : g = 5'bxxxxx ;
endcase
end
endmodule
module Decoder(a, b); // binary to one-hot decoder
input [5 - 1 : 0] a;
output[32- 1 : 0] b;
wire [ 32 - 1 : 0 ] b = 1 << a ;
endmodule
module AND (a, b, c);
input a, b;
output c;
wire nand0 ;
nand nand_0 ( nand0 , a , b ) ;
nand nand_ans ( c , nand0 , nand0 ) ;
endmodule
module OR (a, b, c);
input a, b;
output c;
wire nand0 , nand1 ;
nand nand_0 ( nand0 , a , a ) ;
nand nand_1 ( nand1 , b , b ) ;
nand nand_ans ( c , nand0 , nand1 ) ;
endmodule
module NOT (a, b);
input a;
output b;
nand nand_ans ( b , a , a ) ;
endmodule
module XOR (a, b, c);
input a, b;
output c;
wire nand0 , nand1 , nand2 , nand3 ;
nand nand_0 ( nand0 , a , a ) ;
nand nand_1 ( nand1 , nand0 , b ) ;
nand nand_2 ( nand2 , b , b ) ;
nand nand_3 ( nand3 , nand2 , a ) ;
nand nand_ans ( c , nand1 , nand3 ) ;
endmodule
module XNOR (a, b, c);
input a, b;
output c;
wire nand0 , nand1 , nand2 , nand3 ;
nand nand_0 ( nand0 , a , a ) ;
nand nand_1 ( nand1 , b , b ) ;
nand nand_2 ( nand2 , nand0 , nand1 ) ;
nand nand_3 ( nand3 , a , b ) ;
nand nand_ans ( c , nand2 , nand3 ) ;
endmodule
module NOR (a, b, c);
input a, b;
output c;
wire nand0 , nand1 , nand2 , nand3 ;
nand nand_0 ( nand0 , a , a ) ;
nand nand_1 ( nand1 , b , b ) ;
nand nand_2 ( nand2 , nand0 , nand1 ) ;
nand nand_ans ( c , nand2 , nand2 ) ;
endmodule
Revise this Paste