Advertisement
aidanozo

Untitled

Nov 13th, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module alu #(
  2.     parameter p_data_width = 6, // 6 for FPGA testing, 16 for Simulation and inside the CPU
  3.     parameter p_flags_width = 5
  4. )(
  5.     output wire [(p_data_width-1):0] o_w_out,
  6.     output wire [(p_flags_width-1):0] o_w_flags,
  7.     input wire [(p_data_width-1):0] i_w_op1,
  8.     input wire [(p_data_width-1):0] i_w_op2,
  9.     input wire [3:0] i_w_opcode,
  10.     input wire i_w_carry,
  11.     input wire i_w_oe
  12. );
  13.  
  14. localparam ADC  = 4'd0;
  15. localparam SBB1 = 4'd1;
  16. localparam SBB2 = 4'd2;
  17. localparam NOT  = 4'd3;
  18. localparam AND  = 4'd4;
  19. localparam OR   = 4'd5;
  20. localparam XOR  = 4'd6;
  21. localparam SHL  = 4'd7;
  22. localparam SHR  = 4'd8;
  23. localparam SAR  = 4'd9;
  24.  
  25. reg [(p_data_width-1) : 0]  l_r_result;
  26. reg l_r_parity;
  27. reg l_r_sign;
  28. reg l_r_zero;
  29. reg l_r_overflow;
  30. reg l_r_carry;
  31.  
  32. always @(*) begin
  33.     case(i_w_opcode)
  34.         ADC: begin
  35.             {l_r_carry, l_r_result} = i_w_op1 + i_w_op2 + i_w_carry;
  36.             l_r_overflow = (i_w_op1[p_data_width-1] == i_w_op2[p_data_width-1]) &&
  37.                            (i_w_op1[p_data_width-1] != l_r_result[p_data_width-1]);
  38.         end
  39.  
  40.         SBB1: begin
  41.             {l_r_carry, l_r_result} = i_w_op1 - i_w_op2 - i_w_carry;
  42.             l_r_overflow = (i_w_op1[p_data_width-1] != i_w_op2[p_data_width-1]) &&
  43.                            (i_w_op1[p_data_width-1] != l_r_result[p_data_width-1]);
  44.         end
  45.  
  46.         SBB2: begin
  47.             {l_r_carry, l_r_result} = i_w_op2 - i_w_op1 - i_w_carry;
  48.             l_r_overflow = (i_w_op2[p_data_width-1] != i_w_op1[p_data_width-1]) &&
  49.                            (i_w_op2[p_data_width-1] != l_r_result[p_data_width-1]);
  50.         end
  51.  
  52.         AND: begin
  53.             l_r_result = i_w_op1 & i_w_op2;
  54.             l_r_carry = 0;
  55.             l_r_overflow = 0;
  56.         end
  57.  
  58.         OR: begin
  59.             l_r_result = i_w_op1 | i_w_op2;
  60.             l_r_carry = 0;
  61.             l_r_overflow = 0;
  62.         end
  63.  
  64.         XOR: begin
  65.             l_r_result = i_w_op1 ^ i_w_op2;
  66.             l_r_carry = 0;
  67.             l_r_overflow = 0;
  68.         end
  69.  
  70.         NOT: begin
  71.             l_r_result = ~(i_w_op1 | i_w_op2);
  72.             l_r_carry = 0;
  73.             l_r_overflow = 0;
  74.         end
  75.  
  76.         SHL: begin
  77.             l_r_result = (i_w_op1 << 1) | (i_w_op2 << 1);
  78.             l_r_carry = i_w_op1[p_data_width-1] | i_w_op2[p_data_width - 1];
  79.             l_r_overflow = l_r_result[p_data_width-1] != l_r_carry;
  80.         end
  81.  
  82.         SHR: begin
  83.             l_r_result = (i_w_op1 >> 1) | (i_w_op2 >> 1);
  84.             l_r_carry = i_w_op1[0] | i_w_op2[0];
  85.             l_r_overflow = i_w_op1[p_data_width-1] | i_w_op2[p_data_width-1];
  86.         end
  87.  
  88.         SAR: begin
  89.             l_r_result = {i_w_op1[p_data_width-1], i_w_op1[p_data_width-1:1]} |
  90.                          {i_w_op2[p_data_width-1], i_w_op2[p_data_width-1:1]};
  91.             l_r_carry = i_w_op1[0] | i_w_op2[0];
  92.             l_r_overflow = 0;
  93.         end
  94.  
  95.         default: begin
  96.             l_r_result = 0;
  97.             l_r_carry = 0;
  98.             l_r_overflow = 0;
  99.         end
  100.     endcase
  101.  
  102.     l_r_zero = l_r_result == 0;
  103.     l_r_sign = l_r_result[p_data_width-1];
  104.     l_r_parity = ~^l_r_result;
  105. end
  106.  
  107. assign o_w_out = i_w_oe ? l_r_result : {p_data_width{1'b0}};
  108. assign o_w_flags = {l_r_parity, l_r_sign, l_r_zero, l_r_overflow, l_r_carry};
  109.  
  110. endmodule
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement