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

Subversion Repositories 8051

[/] [8051/] [trunk/] [rtl/] [verilog/] [oc8051_divide.v] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 simont
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// divide for 8051 Core                                         ////
4
////                                                              ////
5
//// This file is part of the 8051 cores project                  ////
6
//// http://www.opencores.org/cores/8051/                         ////
7
////                                                              ////
8
//// Description                                                  ////
9 4 markom
//// Two cycle implementation of division used in alu.v           ////
10 2 simont
////                                                              ////
11
//// To Do:                                                       ////
12 4 markom
////  check if compiler does proper optimizations of the code     ////
13 2 simont
////                                                              ////
14
//// Author(s):                                                   ////
15
//// - Simon Teran, simont@opencores.org                          ////
16 4 markom
//// - Marko Mlinar, markom@opencores.org                         ////
17 2 simont
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2001 Authors and OPENCORES.ORG                 ////
21
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE. See the GNU Lesser General Public License for more  ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from http://www.opencores.org/lgpl.shtml                     ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44
//
45
// ver: 1
46
//
47 4 markom
// ver: 2 markom
48
// changed nonsynthesizable version to two cycle divison
49 2 simont
 
50
// synopsys translate_off
51
`include "oc8051_timescale.v"
52
// synopsys translate_on
53
 
54 4 markom
module oc8051_divide (clk, rst, enable, src1, src2, des1, des2, desOv);
55 2 simont
//
56
// this module is part of alu
57 4 markom
// clk          (in)
58
// rst          (in)
59
// enable       (in)  starts divison
60 2 simont
// src1         (in)  first operand
61
// src2         (in)  second operand
62
// des1         (out) first result
63
// des2         (out) second result
64
// desOv        (out) Overflow output
65
//
66
 
67 4 markom
input clk, rst, enable;
68 2 simont
input [7:0] src1, src2;
69
output desOv;
70
output [7:0] des1, des2;
71
 
72 4 markom
// wires
73
reg desOv;
74
reg div0, div1, div2, div3;
75
reg [7:0] rem1, rem2, rem3;
76
reg [15:0] cmp0, cmp1, cmp2, cmp3;
77
reg [7:0] div_out, rem_out;
78
wire [7:0] div, rem;
79 2 simont
 
80 4 markom
// real registers
81
reg cycle;
82
reg [3:0] tmp_div;
83
reg [7:0] tmp_rem;
84 2 simont
 
85 4 markom
assign rem = cycle ? tmp_rem : src1;
86 2 simont
 
87 4 markom
//
88
// in clock cycle 0 we first calculate four MSB bits,
89
// and four LSB in cycle 1
90
always @(src2 or tmp_div or rem or cycle)
91
begin
92
  if (src2 == 8'b0000_0000) begin
93
    desOv <= 1'b1;
94
    div_out <= 8'hxxxx_xxxx;
95
    rem_out <= 8'hxxxx_xxxx;
96
  end else begin
97
    desOv <= 1'b0;
98 2 simont
 
99 4 markom
    /* This logic is very much redundant, but it should be optimized by
100
       synthesizer */
101
    cmp3 <= src2 << (cycle ? 3'h7 : 3'h3);
102
    cmp2 <= src2 << (cycle ? 3'h6 : 3'h2);
103
    cmp1 <= src2 << (cycle ? 3'h5 : 3'h1);
104
    cmp0 <= src2 << (cycle ? 3'h4 : 3'h0);
105
    div3 <= cmp3 <= rem;
106
    div2 <= cmp2 <= rem3;
107
    div1 <= cmp1 <= rem2;
108
    div0 <= cmp0 <= rem1;
109
    rem3 <= rem - (div3 ? cmp3 : 8'h0);
110
    rem2 <= rem3 - (div2 ? cmp2 : 8'h0);
111
    rem1 <= rem2 - (div1 ? cmp1 : 8'h0);
112
    rem_out <= rem1 - (div0 ? cmp0 : 8'h0);
113
    div_out <= {tmp_div, div3, div2, div1, div0};
114
  end
115
end
116 2 simont
 
117 4 markom
//
118
// divider works in two clock cycles -- 0 and 1
119
always @(posedge clk or posedge rst)
120
begin
121
  if (rst) begin
122
    cycle <= #1 1'b0;
123
    tmp_div <= #1 4'h0;
124
    tmp_rem <= #1 8'h0;
125
  end else begin
126
    if (enable && !cycle) cycle <= #1 1'b1;
127
    else cycle <= #1 1'b0;
128
    tmp_div <= #1 div_out[3:0];
129
    tmp_rem <= #1 rem_out;
130
  end
131
end
132 2 simont
 
133 4 markom
//
134
// assign outputs
135
assign des1 = rem_out;
136
assign des2 = div_out;
137 2 simont
 
138 4 markom
endmodule
139 2 simont
 

powered by: WebSVN 2.1.0

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