module add_cond2_32(a,b,cin,co,s);

input signed [31:0] a,b;
input signed cin;
output co;
output signed [31:0] s;

wire [31:0] ps;
wire [31:0] t, phn, h5,h4,h3,h2,h1,t4,t3,t2,t1;
wire psb,cinb;
wire [31:0] psn,s0,s1;


// partial sum

partialsum ixpsum(ps[31:0], a[31:0], b[31:0]);

// conditional sum

cond2l ic21 (a[1:0],b[1:0],ps[2:1],s0[2:1],s1[2:1]);
cond2l ic22 (a[3:2],b[3:2],ps[4:3],s0[4:3],s1[4:3]);
cond2l ic23 (a[5:4],b[5:4],ps[6:5],s0[6:5],s1[6:5]);
cond2l ic24 (a[7:6],b[7:6],ps[8:7],s0[8:7],s1[8:7]);
cond2l ic25 (a[9:8],b[9:8],ps[10:9],s0[10:9],s1[10:9]);
cond2l ic26 (a[11:10],b[11:10],ps[12:11],s0[12:11],s1[12:11]);
cond2l ic27 (a[13:12],b[13:12],ps[14:13],s0[14:13],s1[14:13]);
cond2l ic28 (a[15:14],b[15:14],ps[16:15],s0[16:15],s1[16:15]);
cond2l ic29 (a[17:16],b[17:16],ps[18:17],s0[18:17],s1[18:17]);
cond2l ic210 (a[19:18],b[19:18],ps[20:19],s0[20:19],s1[20:19]);
cond2l ic211 (a[21:20],b[21:20],ps[22:21],s0[22:21],s1[22:21]);
cond2l ic212 (a[23:22],b[23:22],ps[24:23],s0[24:23],s1[24:23]);
cond2l ic213 (a[25:24],b[25:24],ps[26:25],s0[26:25],s1[26:25]);
cond2l ic214 (a[27:26],b[27:26],ps[28:27],s0[28:27],s1[28:27]);
cond2l ic215 (a[29:28],b[29:28],ps[30:29],s0[30:29],s1[30:29]);

nor it30 (t[30],a[30],b[30]);
not ixb31 (psn[31],ps[31]);
xorb ins1_31 (s1[31],psn[31],t[30]);


// 1st level H# and T#

h1 ih1i0(a[0],b[0],cin,cin,h1[0]);
h1 ih1i2(a[2],b[2],a[1],b[1],h1[2]);
h1 ih1i4(a[4],b[4],a[3],b[3],h1[4]);
h1 ih1i6(a[6],b[6],a[5],b[5],h1[6]);
h1 ih1i8(a[8],b[8],a[7],b[7],h1[8]);
h1 ih1i10(a[10],b[10],a[9],b[9],h1[10]);
h1 ih1i12(a[12],b[12],a[11],b[11],h1[12]);
h1 ih1i14(a[14],b[14],a[13],b[13],h1[14]);
h1 ih1i16(a[16],b[16],a[15],b[15],h1[16]);
h1 ih1i18(a[18],b[18],a[17],b[17],h1[18]);
h1 ih1i20(a[20],b[20],a[19],b[19],h1[20]);
h1 ih1i22(a[22],b[22],a[21],b[21],h1[22]);
h1 ih1i24(a[24],b[24],a[23],b[23],h1[24]);
h1 ih1i26(a[26],b[26],a[25],b[25],h1[26]);
h1 ih1i28(a[28],b[28],a[27],b[27],h1[28]);
h1 ih1i30(a[30],b[30],a[29],b[29],h1[30]);

t1 it1i1(a[1],b[1],a[0],b[0],t1[1]);
t1 it1i3(a[3],b[3],a[2],b[2],t1[3]);
t1 it1i5(a[5],b[5],a[4],b[4],t1[5]);
t1 it1i7(a[7],b[7],a[6],b[6],t1[7]);
t1 it1i9(a[9],b[9],a[8],b[8],t1[9]);
t1 it1i11(a[11],b[11],a[10],b[10],t1[11]);
t1 it1i13(a[13],b[13],a[12],b[12],t1[13]);
t1 it1i15(a[15],b[15],a[14],b[14],t1[15]);
t1 it1i17(a[17],b[17],a[16],b[16],t1[17]);
t1 it1i19(a[19],b[19],a[18],b[18],t1[19]);
t1 it1i21(a[21],b[21],a[20],b[20],t1[21]);
t1 it1i23(a[23],b[23],a[22],b[22],t1[23]);
t1 it1i25(a[25],b[25],a[24],b[24],t1[25]);
t1 it1i27(a[27],b[27],a[26],b[26],t1[27]);
t1 it1i29(a[29],b[29],a[28],b[28],t1[29]);

// 2nd level H and T

not ih2i0(h2[0],h1[0]);
oai ih2i2(h2[2],h1[0],t1[1],h1[2]);
oai ih2i4(h2[4],h1[2],t1[3],h1[4]);
oai ih2i6(h2[6],h1[4],t1[5],h1[6]);
oai ih2i8(h2[8],h1[6],t1[7],h1[8]);
oai ih2i10(h2[10],h1[8],t1[9],h1[10]);
oai ih2i12(h2[12],h1[10],t1[11],h1[12]);
oai ih2i14(h2[14],h1[12],t1[13],h1[14]);
oai ih2i16(h2[16],h1[14],t1[15],h1[16]);
oai ih2i18(h2[18],h1[16],t1[17],h1[18]);
oai ih2i20(h2[20],h1[18],t1[19],h1[20]);
oai ih2i22(h2[22],h1[20],t1[21],h1[22]);
oai ih2i24(h2[24],h1[22],t1[23],h1[24]);
oai ih2i26(h2[26],h1[24],t1[25],h1[26]);
oai ih2i28(h2[28],h1[26],t1[27],h1[28]);
oai ih2i30(h2[30],h1[28],t1[29],h1[30]);

nor it2i3(t2[3],t1[1],t1[3]);
nor it2i5(t2[5],t1[3],t1[5]);
nor it2i7(t2[7],t1[5],t1[7]);
nor it2i9(t2[9],t1[7],t1[9]);
nor it2i11(t2[11],t1[9],t1[11]);
nor it2i13(t2[13],t1[11],t1[13]);
nor it2i15(t2[15],t1[13],t1[15]);
nor it2i17(t2[17],t1[15],t1[17]);
nor it2i19(t2[19],t1[17],t1[19]);
nor it2i21(t2[21],t1[19],t1[21]);
nor it2i23(t2[23],t1[21],t1[23]);
nor it2i25(t2[25],t1[23],t1[25]);
nor it2i27(t2[27],t1[25],t1[27]);
nor it2i29(t2[29],t1[27],t1[29]);

// Level 3 H# and T#

not ih3i0(h3[0],h2[0]);
not ih3i2(h3[2],h2[2]);
aoi ih3i4(h3[4],h2[0],t2[3],h2[4]);
aoi ih3i6(h3[6],h2[2],t2[5],h2[6]);
aoi ih3i8(h3[8],h2[4],t2[7],h2[8]);
aoi ih3i10(h3[10],h2[6],t2[9],h2[10]);
aoi ih3i12(h3[12],h2[8],t2[11],h2[12]);
aoi ih3i14(h3[14],h2[10],t2[13],h2[14]);
aoi ih3i16(h3[16],h2[12],t2[15],h2[16]);
aoi ih3i18(h3[18],h2[14],t2[17],h2[18]);
aoi ih3i20(h3[20],h2[16],t2[19],h2[20]);
aoi ih3i22(h3[22],h2[18],t2[21],h2[22]);
aoi ih3i24(h3[24],h2[20],t2[23],h2[24]);
aoi ih3i26(h3[26],h2[22],t2[25],h2[26]);
aoi ih3i28(h3[28],h2[24],t2[27],h2[28]);
aoi ih3i30(h3[30],h2[26],t2[29],h2[30]);

nand it3i7(t3[7],t2[3],t2[7]);
nand it3i9(t3[9],t2[5],t2[9]);
nand it3i11(t3[11],t2[7],t2[11]);
nand it3i13(t3[13],t2[9],t2[13]);
nand it3i15(t3[15],t2[11],t2[15]);
nand it3i17(t3[17],t2[13],t2[17]);
nand it3i19(t3[19],t2[15],t2[19]);
nand it3i21(t3[21],t2[17],t2[21]);
nand it3i23(t3[23],t2[19],t2[23]);
nand it3i25(t3[25],t2[21],t2[25]);
nand it3i27(t3[27],t2[23],t2[27]);
nand it3i29(t3[29],t2[25],t2[29]);

// Level 4 H and T

not ih4i0(h4[0],h3[0]);
not ih4i2(h4[2],h3[2]);
not ih4i4(h4[4],h3[4]);
not ih4i6(h4[6],h3[6]);
oai ih4i8(h4[8],h3[0],t3[7],h3[8]);
oai ih4i10(h4[10],h3[2],t3[9],h3[10]);
oai ih4i12(h4[12],h3[4],t3[11],h3[12]);
oai ih4i14(h4[14],h3[6],t3[13],h3[14]);
oai ih4i16(h4[16],h3[8],t3[15],h3[16]);
oai ih4i18(h4[18],h3[10],t3[17],h3[18]);
oai ih4i20(h4[20],h3[12],t3[19],h3[20]);
oai ih4i22(h4[22],h3[14],t3[21],h3[22]);
oai ih4i24(h4[24],h3[16],t3[23],h3[24]);
oai ih4i26(h4[26],h3[18],t3[25],h3[26]);
oai ih4i28(h4[28],h3[20],t3[27],h3[28]);
oai ih4i30(h4[30],h3[22],t3[29],h3[30]);

nor it4i15(t4[15],t3[7],t3[15]);
nor it4i17(t4[17],t3[9],t3[17]);
nor it4i19(t4[19],t3[11],t3[19]);
nor it4i21(t4[21],t3[13],t3[21]);
nor it4i23(t4[23],t3[15],t3[23]);
nor it4i25(t4[25],t3[17],t3[25]);
nor it4i27(t4[27],t3[19],t3[27]);
nor it4i29(t4[29],t3[21],t3[29]);

// Level 5 H# 

not ih5i0(h5[0],h4[0]);
not ih5i2(h5[2],h4[2]);
not ih5i4(h5[4],h4[4]);
not ih5i6(h5[6],h4[6]);
not ih5i8(h5[8],h4[8]);
not ih5i10(h5[10],h4[10]);
not ih5i12(h5[12],h4[12]);
not ih5i14(h5[14],h4[14]);
aoi ih5i16(h5[16],h4[0],t4[15],h4[16]);
aoi ih5i18(h5[18],h4[2],t4[17],h4[18]);
aoi ih5i20(h5[20],h4[4],t4[19],h4[20]);
aoi ih5i22(h5[22],h4[6],t4[21],h4[22]);
aoi ih5i24(h5[24],h4[8],t4[23],h4[24]);
aoi ih5i26(h5[26],h4[10],t4[25],h4[26]);
aoi ih5i28(h5[28],h4[12],t4[27],h4[28]);
aoi ih5i30(h5[30],h4[14],t4[29],h4[30]);

not ibbb0(psb,ps[0]);
not ibbb1(cinb,cin);
mux21 im0(s[0],ps[0],psb,cinb);
mux21 im1(s[1],ps[1],s1[1],h5[0]);
mux21 im2(s[2],s0[2],s1[2],h5[0]);
mux21 im3(s[3],ps[3],s1[3],h5[2]);
mux21 im4(s[4],s0[4],s1[4],h5[2]);
mux21 im5(s[5],ps[5],s1[5],h5[4]);
mux21 im6(s[6],s0[6],s1[6],h5[4]);
mux21 im7(s[7],ps[7],s1[7],h5[6]);
mux21 im8(s[8],s0[8],s1[8],h5[6]);
mux21 im9(s[9],ps[9],s1[9],h5[8]);
mux21 im10(s[10],s0[10],s1[10],h5[8]);
mux21 im11(s[11],ps[11],s1[11],h5[10]);
mux21 im12(s[12],s0[12],s1[12],h5[10]);
mux21 im13(s[13],ps[13],s1[13],h5[12]);
mux21 im14(s[14],s0[14],s1[14],h5[12]);
mux21 im15(s[15],ps[15],s1[15],h5[14]);
mux21 im16(s[16],s0[16],s1[16],h5[14]);
mux21 im17(s[17],ps[17],s1[17],h5[16]);
mux21 im18(s[18],s0[18],s1[18],h5[16]);
mux21 im19(s[19],ps[19],s1[19],h5[18]);
mux21 im20(s[20],s0[20],s1[20],h5[18]);
mux21 im21(s[21],ps[21],s1[21],h5[20]);
mux21 im22(s[22],s0[22],s1[22],h5[20]);
mux21 im23(s[23],ps[23],s1[23],h5[22]);
mux21 im24(s[24],s0[24],s1[24],h5[22]);
mux21 im25(s[25],ps[25],s1[25],h5[24]);
mux21 im26(s[26],s0[26],s1[26],h5[24]);
mux21 im27(s[27],ps[27],s1[27],h5[26]);
mux21 im28(s[28],s0[28],s1[28],h5[26]);
mux21 im29(s[29],ps[29],s1[29],h5[28]);
mux21 im30(s[30],s0[30],s1[30],h5[28]);
mux21 im31(s[31],ps[31],s1[31],h5[30]);

endmodule
