§ Конвейерный метод
Этот модуль весьма неэффективен.
1module div
2#(
3 parameter W = 8,
4 parameter S = 8
5)
6(
7 input wire clock,
8 input wire block,
9 input wire [(W-1):0] a,
10 input wire [(W-1):0] b,
11 output wire [(W-1):0] q,
12 output wire [(W-1):0] r
13);
14
15genvar i;
16
17
18wire [(W-1):0] ao[S];
19wire [(W-1):0] bo[S];
20wire [(W-1):0] ro[S];
21wire [(W-1):0] qo[S];
22wire [(W-1):0] wz = 1'b0;
23
24assign q = qo[S-1];
25assign r = ro[S-1];
26
27
28div_stage #(.W(W)) DivUnit
29(
30 .clock (clock),
31 .block (block),
32 .ai(a), .bi(b), .ri(wz), .qi(wz),
33 .ao(ao[0]), .bo(bo[0]), .ro(ro[0]), .qo(qo[0])
34);
35
36generate
37
38
39for (i = 0; i < S-1; i=i+1) begin : DataX
40div_stage #(.W(W)) DivUnit
41(
42 .clock (clock),
43 .block (block),
44 .ai(ao[i]), .bi(bo[i]), .ri(ro[i]), .qi(qo[i]),
45 .ao(ao[i+1]), .bo(bo[i+1]), .ro(ro[i+1]), .qo(qo[i+1])
46);
47end
48
49endgenerate
50
51endmodule
52
53module div_stage
54#(parameter W = 8)
55(
56 input wire clock,
57 input wire block,
58
59 input wire [(W-1):0] ai,
60 input wire [(W-1):0] bi,
61 input wire [(W-1):0] ri,
62 input wire [(W-1):0] qi,
63
64 output reg [(W-1):0] ao,
65 output reg [(W-1):0] bo,
66 output reg [(W-1):0] ro,
67 output reg [(W-1):0] qo
68);
69
70
71always @(posedge clock)
72if (block == 1'b0) begin
73
74 ao <= {ai[(W-2):0], 1'b0};
75 ro <= sub[(W+1)] ? op1 : sub[(W-1):0];
76 qo <= {qi[(W-2):0], ~sub[(W+1)]};
77 bo <= bi;
78
79end
80
81
82wire [(W-1):0] op1 = {ri[(W-2):0], ai[(W-1)]};
83wire [(W+1):0] sub = op1 - bi;
84
85endmodule