Advertisement
Guest User

ALU mod

a guest
Feb 22nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module alu(
  2.     input logic [31:0] SrcA,
  3.     input logic [31:0] SrcB,
  4.     input logic [3:0] ALUControl,
  5.     output logic [31:0] ALUResult,
  6.     output logic [3:0] ALUFlags,   // {Neg, Zero, Carry, Overflow}
  7.     input logic carry_in,
  8.     input logic shifter_carry_in
  9.     );
  10.  
  11.     logic neg_flag;
  12.     logic zero_flag;
  13.     logic carry_flag;
  14.     logic overflow_flag;
  15.    
  16.     logic [31:0]carry_extend;
  17.  
  18.     assign ALUFlags = {neg_flag, zero_flag, carry_flag, overflow_flag};
  19.  
  20.     assign zero_flag = ~|ALUResult;
  21.     assign neg_flag = ALUResult[31];
  22.  
  23.     assign carry_extend = {{31{0}}, {carry_in}};
  24.  
  25.     always_comb
  26.     begin
  27.         ALUResult = 32'd0;
  28.  
  29.         case(ALUControl)
  30.         4'b0000 :   // Bitwise AND
  31.         begin
  32.             ALUResult = SrcA & SrcB;
  33.             carry_flag = shifter_carry_in;
  34.         end
  35.  
  36.         4'b0001 :   // Bitwise XOR
  37.         begin
  38.             ALUResult = SrcA ^ SrcB;
  39.             carry_flag = shifter_carry_in;
  40.         end
  41.  
  42.         4'b0010 :   // Subtract
  43.         begin
  44.             {carry_flag, ALUResult} = SrcA - SrcB;
  45.             if (~SrcA[31] & SrcB[31] & ALUResult[31])
  46.                 overflow_flag = 1'b1;
  47.             if (SrcA[31] & ~SrcB[31] & ~ALUResult[31])
  48.                 overflow_flag = 1'b1;
  49.             else
  50.                 overflow_flag = 1'b0;
  51.         end
  52.  
  53.         4'b0011 :   // Reverse Subtract
  54.         begin
  55.             {carry_flag, ALUResult} = SrcB - SrcA;
  56.             // neg - pos -> neg + neg = neg
  57.             if (SrcB[31] & ~SrcA[31] & ~ALUResult[31])
  58.                 overflow_flag = 1'b1;
  59.             // pos - neg -> pos + pos = pos
  60.             else if (~SrcB[31] & SrcA[31] & ~ALUResult[31])
  61.                 overflow_flag = 1'b1;
  62.             else
  63.                 overflow_flag = 1'b0;          
  64.         end
  65.  
  66.         4'b0100 :     //add
  67.         begin
  68.             {carry_flag, ALUResult} = SrcA + SrcB;
  69.             if (~SrcA[31] & ~SrcB[31] & ALUResult[31])
  70.                 overflow_flag = 1'b1;
  71.             else if (SrcA[31] & SrcB[31] & ~ALUResult[31])
  72.                 overflow_flag = 1'b1;
  73.             else
  74.                 overflow_flag = 1'b0;
  75.         end
  76.  
  77.         4'b0101 :     //add with Carry
  78.         begin
  79.             {carry_flag, ALUResult} = SrcA + SrcB + carry_extend;
  80.             if (~SrcA[31] & ~SrcB[31] & ALUResult[31])
  81.                 overflow_flag = 1'b1;
  82.             else if (SrcA[31] & SrcB[31] & ~ALUResult[31])
  83.                 overflow_flag = 1'b1;
  84.             else
  85.                 overflow_flag = 1'b0;
  86.         end
  87.        
  88.         4'b0110 :     //sub with Carry
  89.         begin
  90.             {carry_flag, ALUResult} = ((SrcA - SrcB) - ~carry_extend);
  91.  
  92.             // pos - neg -> pos + pos = pos  
  93.             if (~SrcA[31] & SrcB[31] & ALUResult[31])
  94.                 overflow_flag = 1'b1;
  95.             // neg - pos -> neg + neg = neg
  96.             else if (SrcA[31] & ~SrcB[31] & ~ALUResult[31])
  97.                 overflow_flag = 1'b1;
  98.             else
  99.                 overflow_flag = 1'b0;
  100.         end
  101.         4'b0111 :     //Reverse Sub w/ Carry
  102.         begin
  103.             {carry_flag, ALUResult} = SrcB - SrcA - ~carry_extend;
  104.  
  105.             // neg - pos -> neg + neg = neg
  106.             if (SrcB[31] & ~SrcA[31] & ~ALUResult[31])
  107.                 overflow_flag = 1'b1;
  108.             // pos - neg -> pos + pos = pos
  109.             else if (~SrcB[31] & SrcA[31] & ~ALUResult[31])
  110.                 overflow_flag = 1'b1;
  111.             else
  112.                 overflow_flag = 1'b0;        
  113.         end
  114.         4'b1000 :     // Test
  115.         begin
  116.             ALUResult = SrcA & SrcB;
  117.             carry_flag = shifter_carry_in;
  118.         end
  119.  
  120.         4'b1001 : // Test Equivalence
  121.         begin
  122.             ALUResult = SrcA ^ SrcB;
  123.             carry_flag = shifter_carry_in;          
  124.         end
  125.  
  126.         4'b1010 : // Compare
  127.         begin
  128.             {carry_flag, ALUResult} = SrcA - SrcB;
  129.             if (~SrcA[31] & SrcB[31] & ALUResult[31])
  130.                 overflow_flag = 1'b1;
  131.             if (SrcA[31] & ~SrcB[31] & ~ALUResult[31])
  132.                 overflow_flag = 1'b1;
  133.             else
  134.                 overflow_flag = 1'b0;        
  135.         end
  136.  
  137.         4'b1011 : // Compare Negative
  138.         begin
  139.             {carry_flag, ALUResult} = SrcA + SrcB;
  140.             if (~SrcA[31] & ~SrcB[31] & ALUResult[31])
  141.                 overflow_flag = 1'b1;
  142.             else if (SrcA[31] & SrcB[31] & ~ALUResult[31])
  143.                 overflow_flag = 1'b1;
  144.             else
  145.                 overflow_flag = 1'b0;
  146.         end
  147.  
  148.         4'b1100 : // Bitwise OR
  149.         begin
  150.             ALUResult = SrcA | SrcB;
  151.             carry_flag = shifter_carry_in;
  152.             // carry_flag = shifter_carry_in
  153.         end
  154.  
  155.         4'b1101 : // Shifts
  156.         begin
  157.             ALUResult = SrcB;
  158.             carry_flag = shifter_carry_in;
  159.         end
  160.  
  161.         4'b1110 : // Bitwise Clear
  162.         begin
  163.             ALUResult = SrcA & ~SrcB;
  164.             carry_flag = shifter_carry_in;
  165.         end
  166.  
  167.         4'b1111 : // Bitwise Not
  168.         begin
  169.             ALUResult = ~SrcB;
  170.             carry_flag = shifter_carry_in;
  171.         end                
  172.         default :
  173.         begin
  174.             ALUResult = 32'd0;
  175.             carry_flag = 1'd0;
  176.             overflow_flag = 1'd0;
  177.         end
  178.         endcase
  179.     end
  180.  
  181. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement