OpenCores
URL https://opencores.org/ocsvn/m1_core/m1_core/trunk

Subversion Repositories m1_core

[/] [m1_core/] [trunk/] [hdl/] [rtl/] [m1_core/] [m1_alu.v] - Blame information for rev 64

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 33 fafa1971
/*
2 64 albert.wat
 * M1 Arithmetic-Logic Unit
3 33 fafa1971
 *
4
 * Simple RTL-level ALU with Alternating Bit Protocol (ABP) interface.
5
 */
6
 
7
`include "m1_defs.vh"
8
 
9
// Combinational ALU with 32-bit operands
10
module m1_alu(
11 48 fafa1971
    input[31:0] a_i,             // Operand A
12
    input[31:0] b_i,             // Operand B
13 33 fafa1971
    input[4:0] func_i,           // Function to be performed
14
    input signed_i,              // Operation is signed
15 48 fafa1971
    output reg[32:0] result_o    // 33-bit result (uppermost bit is the carry)
16 33 fafa1971
  );
17
 
18
  // ALU Logic
19
  always @(a_i or b_i or func_i or signed_i) begin
20
    case(func_i)
21 48 fafa1971
 
22
      // Shift instructions
23
      `ALU_OP_SLL: result_o = {1'b0, a_i << b_i[4:0]};
24
      `ALU_OP_SRL: result_o = {1'b0, a_i >> b_i[4:0]};
25
      `ALU_OP_SRA: result_o = {1'b0, {{32{a_i[31]}}, a_i } >> b_i[4:0]};
26
 
27
      // Arithmetical instructions
28
      `ALU_OP_ADD: if(signed_i) result_o = a_i + b_i;  // Result may include a carry bit
29
                   else result_o = {1'b0,  a_i + b_i};
30
      `ALU_OP_SUB: if(signed_i) result_o = a_i - b_i;  // Result may include a carry bit
31
                   else result_o = {1'b0,  a_i - b_i};
32
 
33
      // Logical instructions
34
      `ALU_OP_AND: result_o = {1'b0, a_i & b_i};
35
      `ALU_OP_OR:  result_o = {1'b0, a_i | b_i};
36
      `ALU_OP_XOR: result_o = {1'b0, a_i ^ b_i};
37
      `ALU_OP_NOR: result_o = {1'b0, ~(a_i | b_i)};
38
 
39
      // Conditional instructions
40
      `ALU_OP_SEQ: result_o = (a_i == b_i) ? 33'b1 : 33'b0;
41
      `ALU_OP_SNE: result_o = (a_i != b_i) ? 33'b1 : 33'b0;
42
      `ALU_OP_SLT: if(signed_i) result_o = ({~a_i[31],a_i[30:0]} <  {~b_i[31],b_i[30:0]}) ? 33'b1 : 33'b0;
43
                   else result_o = (a_i < b_i) ? 33'b1 : 33'b0;
44
      `ALU_OP_SLE: if(signed_i) result_o = ({~a_i[31],a_i[30:0]} <= {~b_i[31],b_i[30:0]}) ? 33'b1 : 33'b0;
45
                   else result_o = (a_i <= b_i) ? 33'b1 : 33'b0;
46
      `ALU_OP_SGT: if(signed_i) result_o = ({~a_i[31],a_i[30:0]} >  {~b_i[31],b_i[30:0]}) ? 33'b1 : 33'b0;
47
                   else result_o = (a_i > b_i) ? 33'b1 : 33'b0;
48
      `ALU_OP_SGE: if(signed_i) result_o = ({~a_i[31],a_i[30:0]} >= {~b_i[31],b_i[30:0]}) ? 33'b1 : 33'b0;
49
                   else result_o = (a_i >= b_i) ? 33'b1 : 33'b0;
50
 
51
      default: result_o = 33'b0;
52 33 fafa1971
    endcase
53
  end
54
 
55
endmodule
56
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.