-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLZ.v
142 lines (130 loc) · 3.02 KB
/
CLZ.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
module CLZ(
input [31:0] rs1,
output [31:0] rd
);
//Only use least 5 bit in rd.
assign rd[31:5]=27'd0;
//First stage
wire or_upper_16,or_down_16;
assign or_upper_16=|rs1[31:16];
assign or_down_16=|rs1[15:0];
assign condition_1={or_upper_16,or_down_16};
wire [15:0] result_stage1;
always@(*)
begin
case(condition_1)
2'b00:
begin
//Still need to define the result of all 0.
rd[4]=1'b0;
result_stage1=rs1[15:0];
end
2'b11,2'b10:
begin
rd[4]=1'b0;
result_stage1=rs1[31:16];
end
2'b01:
begin
rd[4]=1'b1;
result_stage1=rs1[15:0];
end
end
//Second stage
wire or_upper_8,or_down_8;
assign or_upper_8=|result_stage1[16:8];
assign or_down_8=|rs1[7:0];
assign condition_2={or_upper_8,or_down_8};
wire [7:0] result_stage2;
always@(*)
begin
case(condition_2)
2'b00:
begin
//Still need to define the result of all 0.
rd[3]=1'b0;
result_stage2=result_stage[7:0];
end
2'b11,2'b10:
begin
rd[3]=1'b0;
result_stage2=result_stage1[15:8];
end
2'b01:
begin
rd[3]=1'b1;
result_stage2=result_stage1[7:0];
end
end
//Third stage
wire or_upper_4,or_down_4;
assign or_upper_4=|result_stage2[7:4];
assign or_down_4=|result_stage2[3:0];
assign condition_3={or_upper_4,or_down_4};
wire [3:0] result_stage3;
always@(*)
begin
case(condition_3)
2'b00:
begin
//Still need to define the result of all 0.
rd[2]=1'b0;
result_stage3=result_stage2[3:0];
end
2'b11,2'b10:
begin
rd[2]=1'b0;
result_stage3=result_stage2[7:4];
end
2'b01:
begin
rd[2]=1'b1;
result_stage3=result_stage2[3:0];
end
end
//Fourth stage
wire or_upper_2,or_down_2;
assign or_upper_2=|result_stage3[3:2];
assign or_down_2=|result_stage3[1:0];
assign condition_2={or_upper_2,or_down_2};
wire [3:0] result_stage4;
always@(*)
begin
case(condition_4)
2'b00:
begin
//Still need to define the result of all 0.
rd[1]=1'b0;
result_stage4=result_stage3[1:0];
end
2'b11,2'b10:
begin
rd[1]=1'b0;
result_stage4=result_stage3[3:2];
end
2'b01:
begin
rd[1]=1'b0;
result_stage4=result_stage3[1:0];
end
end
//Fifth stage
assign condition_1=result_stage4;
always@(*)
begin
case(condition_1)
2'b00:
begin
//Still need to define the result of all 0.
rd[0]=1'b0;
end
2'b11,2'b10:
begin
rd[0]=1'b0;
end
2'b01:
begin
rd[0]=1'b1;
end
end
endmodule