1 # This stage is intended to do most of the work of executing the Arithmetic
2 # instructions. This would be like the additions, compares, and sign-extension
3 # as well as carry and overflow generation. This module
4 # however should not gate the carry or overflow, that's up to the
6 from nmigen
import (Module
, Signal
, Cat
, Repl
, Mux
, Const
)
7 from nmutil
.pipemodbase
import PipeModBase
8 from nmutil
.extend
import exts
9 from soc
.fu
.alu
.pipe_data
import ALUInputData
, ALUOutputData
10 from ieee754
.part
.partsig
import PartitionedSignal
11 from soc
.decoder
.power_enums
import InternalOp
14 class ALUMainStage(PipeModBase
):
15 def __init__(self
, pspec
):
16 super().__init
__(pspec
, "main")
19 return ALUInputData(self
.pspec
)
22 return ALUOutputData(self
.pspec
) # TODO: ALUIntermediateData
24 def elaborate(self
, platform
):
27 carry_out
, o
, cr0
= self
.o
.xer_co
, self
.o
.o
, self
.o
.cr0
28 a
, b
, op
= self
.i
.a
, self
.i
.b
, self
.i
.ctx
.op
30 # check if op is 32-bit, and get sign bit from operand a
31 is_32bit
= Signal(reset_less
=True)
32 sign_bit
= Signal(reset_less
=True)
33 comb
+= is_32bit
.eq(op
.is_32bit
)
34 comb
+= sign_bit
.eq(Mux(is_32bit
, a
[31], a
[63]))
36 # little trick: do the add using only one add (not 2)
37 add_a
= Signal(a
.width
+ 2, reset_less
=True)
38 add_b
= Signal(a
.width
+ 2, reset_less
=True)
39 add_output
= Signal(a
.width
+ 2, reset_less
=True)
40 with m
.If((op
.insn_type
== InternalOp
.OP_ADD
) |
41 (op
.insn_type
== InternalOp
.OP_CMP
)):
42 # in bit 0, 1+carry_in creates carry into bit 1 and above
43 comb
+= add_a
.eq(Cat(self
.i
.carry_in
, a
, Const(0, 1)))
44 comb
+= add_b
.eq(Cat(Const(1, 1), b
, Const(0, 1)))
45 comb
+= add_output
.eq(add_a
+ add_b
)
47 ##########################
48 # main switch-statement for handling arithmetic operations
50 with m
.Switch(op
.insn_type
):
52 with m
.Case(InternalOp
.OP_CMP
):
53 # this is supposed to be inverted (b-a, not a-b)
54 # however we have a trick: instead of adding either 2x 64-bit
55 # MUXes to invert a and b, or messing with a 64-bit output,
56 # swap +ve and -ve test in the *output* stage using an XOR gate
57 comb
+= o
.eq(add_output
[1:-1])
60 with m
.Case(InternalOp
.OP_ADD
):
61 # bit 0 is not part of the result, top bit is the carry-out
62 comb
+= o
.eq(add_output
[1:-1])
63 comb
+= carry_out
.data
[0].eq(add_output
[-1]) # XER.CO
65 # see microwatt OP_ADD code
66 # https://bugs.libre-soc.org/show_bug.cgi?id=319#c5
67 comb
+= carry_out
.data
[1].eq(add_output
[33] ^
68 (a
[32] ^ b
[32])) # XER.CO32
70 #### exts (sign-extend) ####
71 with m
.Case(InternalOp
.OP_EXTS
):
72 with m
.If(op
.data_len
== 1):
73 comb
+= o
.eq(exts(a
, 8, 64))
74 with m
.If(op
.data_len
== 2):
75 comb
+= o
.eq(exts(a
, 16, 64))
76 with m
.If(op
.data_len
== 4):
77 comb
+= o
.eq(exts(a
, 32, 64))
80 with m
.Case(InternalOp
.OP_CMPEQB
):
81 eqs
= Signal(8, reset_less
=True)
82 src1
= Signal(8, reset_less
=True)
83 comb
+= src1
.eq(a
[0:8])
85 comb
+= eqs
[i
].eq(src1
== b
[8*i
:8*(i
+1)])
86 comb
+= cr0
.data
.eq(Cat(Const(0, 2), eqs
.any(), Const(0, 1)))
88 ###### sticky overflow and context, both pass-through #####
90 comb
+= self
.o
.xer_so
.data
.eq(self
.i
.so
)
91 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)