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

Subversion Repositories turbo8051

[/] [turbo8051/] [trunk/] [rtl/] [8051/] [oc8051_divide.v] - Blame information for rev 76

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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