Advertisement
aditya16_cr7

Untitled

Sep 1st, 2020
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //ShifterAndALU.v
  2.  
  3. // select 0 = in0 1 = in1
  4. module mux2to1_3bit(input [2:0] in0, input [2:0] in1, input select, output reg [2:0] muxOut);
  5.   //WRITE CODE HERE
  6.    always @ *
  7.    begin
  8.     case(select)
  9.         1'b0:muxOut=in0;
  10.         1'b1:muxOut=in1;
  11.     endcase
  12.    end
  13. endmodule
  14.  
  15. // select 0 = in0 1 = in1
  16. module mux2to1_8bit(input [7:0] in0, input [7:0] in1, input select, output reg [7:0] muxOut);
  17.    //WRITE CODE HERE
  18.    always @ *
  19.    begin
  20.     case(select)
  21.         1'b0:muxOut=in0;
  22.         1'b1:muxOut=in1;
  23.     endcase
  24.    end
  25. endmodule
  26.  
  27.  
  28. module mux8to1_1bit(input in0, input in1, input in2, input in3, input in4, input in5, input in6, input in7, input[2:0] select,output reg muxOut);
  29.    //WRITE CODE HERE
  30.    always @ *
  31.    begin
  32.     case(select)
  33.         3'b000:muxOut=in0;
  34.         3'b001:muxOut=in1;
  35.         3'b010:muxOut=in2;
  36.         3'b011:muxOut=in3;
  37.         3'b100:muxOut=in4;
  38.         3'b101:muxOut=in5;
  39.         3'b110:muxOut=in6;
  40.         3'b111:muxOut=in7;
  41.        
  42.     endcase
  43.    end
  44.  
  45. endmodule
  46.  
  47. module barrelshifter(input[2:0] shiftAmt, input[7:0] b, input[2:0] oper, output[7:0] shiftOut);
  48.        //WRITE CODE HERE
  49.  
  50.     wire [7:0] s,r;
  51.     wire [2:0] m1, m2, m3;
  52.  
  53.     mux2to1_3bit mux_1(3'b000, oper, shiftAmt[0], m1);
  54.     mux2to1_3bit mux_2(3'b000, oper, shiftAmt[1], m2);
  55.     mux2to1_3bit mux_3(3'b000, oper, shiftAmt[2], m3);
  56.  
  57.     mux8to1_1bit shifter_10(b[0],b[1],b[1],b[1],0,b[7],0,0,m1,s[0]);
  58.     mux8to1_1bit shifter_11(b[1],b[2],b[2],b[2],b[0],b[0],0,0,m1,s[1]);
  59.     mux8to1_1bit shifter_12(b[2],b[3],b[3],b[3],b[1],b[1],0,0,m1,s[2]);
  60.     mux8to1_1bit shifter_13(b[3],b[4],b[4],b[4],b[2],b[2],0,0,m1,s[3]);
  61.     mux8to1_1bit shifter_14(b[4],b[5],b[5],b[5],b[3],b[3],0,0,m1,s[4]);
  62.     mux8to1_1bit shifter_15(b[5],b[6],b[6],b[6],b[4],b[4],0,0,m1,s[5]);
  63.     mux8to1_1bit shifter_16(b[6],b[7],b[7],b[7],b[5],b[5],0,0,m1,s[6]);
  64.     mux8to1_1bit shifter_17(b[7],b[7],0,b[0],b[6],b[6],0,0,m1,s[7]);
  65.  
  66.     mux8to1_1bit shifter_20(s[0],s[2],s[2],s[2],0,s[6],0,0,m2,r[0]);
  67.     mux8to1_1bit shifter_21(s[1],s[3],s[3],s[3],0,s[7],0,0,m2,r[1]);
  68.     mux8to1_1bit shifter_22(s[2],s[4],s[4],s[4],s[0],s[0],0,0,m2,r[2]);
  69.     mux8to1_1bit shifter_23(s[3],s[5],s[5],s[5],s[1],s[1],0,0,m2,r[3]);
  70.     mux8to1_1bit shifter_24(s[4],s[6],s[6],s[6],s[2],s[2],0,0,m2,r[4]);
  71.     mux8to1_1bit shifter_25(s[5],s[7],s[7],s[7],s[3],s[3],0,0,m2,r[5]);
  72.     mux8to1_1bit shifter_26(s[6],s[7],0,s[0],s[4],s[4],0,0,m2,r[6]);
  73.     mux8to1_1bit shifter_27(s[7],s[7],0,s[1],s[5],s[5],0,0,m2,r[7]);
  74.  
  75.     mux8to1_1bit shifter_30(r[0],r[4],r[4],r[4],0,r[4],0,0,m3,shiftOut[0]);
  76.     mux8to1_1bit shifter_31(r[1],r[5],r[5],r[5],0,r[5],0,0,m3,shiftOut[1]);
  77.     mux8to1_1bit shifter_32(r[2],r[6],r[6],r[6],0,r[6],0,0,m3,shiftOut[2]);
  78.     mux8to1_1bit shifter_33(r[3],r[7],r[7],r[7],0,r[7],0,0,m3,shiftOut[3]);
  79.     mux8to1_1bit shifter_34(r[4],r[7],0,r[0],r[0],r[0],0,0,m3,shiftOut[4]);
  80.     mux8to1_1bit shifter_35(r[5],r[7],0,r[1],r[1],r[1],0,0,m3,shiftOut[5]);
  81.     mux8to1_1bit shifter_36(r[6],r[7],0,r[2],r[2],r[2],0,0,m3,shiftOut[6]);
  82.     mux8to1_1bit shifter_37(r[7],r[7],0,r[3],r[3],r[3],0,0,m3,shiftOut[7]);
  83.  
  84.  
  85.  
  86.  
  87. endmodule
  88.  
  89. // Alu operations are: 00 for alu1, 01 for add, 10 for sub(alu1-alu2) , 11 for AND, 100 for OR and 101 for NOT(alu1)
  90. module alu(input [7:0] aluIn1, input [7:0] aluIn2, input [2:0]aluOp, output reg [7:0] aluOut);
  91.        //WRITE CODE HERE
  92.     always @ *
  93.      begin
  94.        case(aluOp)
  95.             3'b000:aluOut=aluIn1;
  96.             3'b001:aluOut=aluIn1 + aluIn2;
  97.             3'b010:aluOut=aluIn1 - aluIn2;
  98.             3'b011:aluOut=aluIn1 & aluIn2;
  99.             3'b100:aluOut=aluIn1 | aluIn2;
  100.             3'b101:aluOut=~aluIn1;
  101.            
  102.         endcase
  103.      end
  104.  
  105.  
  106.  
  107. endmodule
  108.  
  109.  
  110. module shifterAndALU(input [7:0]inp1, input [7:0] inp2, input [2:0]shiftlmm, input selShiftAmt, input [2:0]oper, input selOut, output [7:0] out);
  111.        //WRITE CODE HERE
  112.        wire shiftAmt[2:0];
  113.        mux2to1_3bit shift_value(shiftlmm,inp2[2:0],selShiftAmt,shiftAmt);
  114.        wire aluOut[7:0];
  115.        alu alu_ans(inp1,inp2,oper,aluOut);
  116.        wire shiftOut[7:0];
  117.        barrelshifter shifter_ans(shiftAmt,inp1,oper,shiftOut);
  118.        mux2to1_8bit final_ans(aluOut,shiftOut,selOut,out);
  119.  
  120.  
  121.  
  122.  
  123. endmodule
  124.  
  125. //TestBench ALU
  126. module testbenchALU();
  127.     // Input
  128.     reg [7:0] inp1, inp2;
  129.     reg [2:0] aluOp;
  130.     reg [2:0] shiftlmm;
  131.     reg selShiftAmt, selOut;
  132.     // Output
  133.     wire [7:0] aluOut;
  134.  
  135.     shifterAndALU shifterAndALU_Test (inp1, inp2, shiftlmm, selShiftAmt, aluOp, selOut, aluOut);
  136.  
  137.     initial
  138.         begin
  139.             $dumpfile("testALU.vcd");
  140.         $dumpvars(0,testbenchALU);
  141.             inp1 = 8'd80; //80 in binary is 1010000
  142.             inp2 = 8'd20; //20 in binary is 10100
  143.             shiftlmm = 3'b010;
  144.  
  145.             aluOp=3'd0; selOut = 1'b0;// normal output = 80
  146.  
  147.             #10 aluOp = 3'd0; selOut = 1'b1; selShiftAmt = 1'b1; //No shift output = 80
  148.  
  149.             #10 aluOp=3'd1; selOut = 1'b0;// normal add output => 20 + 80 = 100
  150.  
  151.             #10 aluOp = 3'd1; selOut = 1'b1; selShiftAmt = 1'b1; // arithmetic shift right of 80 by 2 places = 20
  152.  
  153.             #10 aluOp=3'd2; selOut = 1'b0; // normal sub output => 80 - 20 = 60
  154.  
  155.             #10 aluOp = 3'd2; selOut = 1'b1; selShiftAmt = 1'b0; // logical shift right of 80 by 4 places = 0
  156.  
  157.             #10 aluOp=3'd3; selOut = 1'b0;// normal and output => 80 & 20 = 16
  158.  
  159.             #10 aluOp = 3'd3; selOut = 1'b1; selShiftAmt = 1'b0; // Circular Shift Right of 80 by 4 places = 5
  160.  
  161.             #10 aluOp=3'd4; selOut = 1'b0;// normal or output => 80 | 20 = 84
  162.  
  163.             #10 aluOp = 3'd4; selOut = 1'b1; selShiftAmt = 1'b1; // Logical Shift Left of 80 by 2 bits = 64
  164.  
  165.             #10 aluOp=3'd5; selOut = 1'b0; // normal not of 80 = 175
  166.  
  167.             #10 aluOp = 3'd5; selOut = 1'b1; selShiftAmt = 1'b0; // Circular left shift of 80 by 4 bits = 5
  168.  
  169.             #10 inp1=8'd15; inp2=8'd26; aluOp=3'd2; selOut = 1'b0;//sub overflow
  170.             // (15 - 26) = -11 and its a 8 bit number so, 256-11 = 245 output => 245 (since it is unsigned decimal)
  171.  
  172.             #10 inp1=8'd150; inp2=8'd150; aluOp=3'd1; selOut = 1'b0;// add overflow
  173.             //(150+150) = 300 and its a 8 bit number so, 300-256 = 44 output=> 44.
  174.  
  175.             #10 inp1=8'b0000_0000; aluOp=3'd5; selOut = 1'b0;//not overflow
  176.             // not(0) = all 1. Since its a 8 bit number output=>255
  177.  
  178.             #10 $finish;
  179.         end
  180.  
  181. endmodule
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement