aidanozohor1810

Untitled

Dec 6th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module alu(
  2.         oe,
  3.         opcode,
  4.         in1,
  5.         in2,
  6.         carry,
  7.         out,
  8.         flags
  9.     );
  10.  
  11. `define ADC                 0
  12. `define SBB1                1
  13. `define SBB2                2
  14. `define NOT                 3
  15. `define AND                 4
  16. `define OR                  5
  17. `define XOR                 6
  18. `define SHL                 7
  19. `define SHR                 8
  20. `define SAR                 9
  21.  
  22. parameter width = 16;
  23. parameter flags_width = 5;
  24.  
  25. input                       oe;
  26. input [3:0]                 opcode;
  27. input [width-1 : 0]         in1;
  28. input [width-1 : 0]         in2;
  29. input                       carry;
  30. output[width-1 : 0]         out;
  31. output[flags_width-1 : 0]   flags;  // P, S, Z, O, C
  32.  
  33. reg [width-1 : 0]           result;
  34. reg                         p, s, z, o, c;
  35.  
  36. always @(*) begin
  37.     case(opcode)
  38.         `ADC: begin
  39.             {c, result} = in1 + in2 + carry;
  40.             o = (in1[width-1] == in2[width-1]) && (in1[width-1] != result[width-1]);
  41.         end
  42.  
  43.         `SBB1: begin
  44.             {c, result} = in1 - in2 - carry;
  45.             o = (in1[width-1] != in2[width-1]) && (in1[width-1] != result[width-1]);
  46.         end
  47.  
  48.         `SBB2: begin
  49.             {c, result} = in2 - in1 - carry;
  50.             o = (in2[width-1] != in1[width-1]) && (in2[width-1] != result[width-1]);
  51.         end
  52.  
  53.         `AND: begin
  54.             result = in1 & in2;
  55.             c = 0;
  56.             o = 0;
  57.         end
  58.  
  59.         `OR: begin
  60.             result = in1 | in2;
  61.             c = 0;
  62.             o = 0;
  63.         end
  64.  
  65.         `XOR: begin
  66.             result = in1 ^ in2;
  67.             c = 0;
  68.             o = 0;
  69.         end
  70.  
  71.         `NOT: begin
  72.             result = ~(in1 | in2);
  73.             c = 0;
  74.             o = 0;
  75.         end
  76.  
  77.         `SHL: begin
  78.             result = (in1 << 1) | (in2 << 1);
  79.             c = in1[width-1] | in2[width - 1];
  80.             o = result[width-1] != c;
  81.         end
  82.  
  83.         `SHR: begin
  84.             result = (in1 >> 1) | (in2 >> 1);
  85.             c = in1[0] | in2[0];
  86.             o = in1[width-1] | in2[width-1];
  87.         end
  88.  
  89.         `SAR: begin
  90.             result = {in1[width-1], in1[width-1:1]} | {in2[width-1], in2[width-1:1]};
  91.             c = in1[0] | in2[0];
  92.             o = 0;
  93.         end
  94.  
  95.         default: begin
  96.             result = 0;
  97.             c = 0;
  98.             o = 0;
  99.         end
  100.     endcase
  101.  
  102.     z = result == 0;
  103.     s = result[width-1];
  104.     p = ~^result;
  105. end
  106.  
  107. assign out = oe ? result : 0;
  108. assign flags = {p, s, z, o, c};
  109.  
  110. endmodule
Add Comment
Please, Sign In to add comment