1 |
2 |
specular |
//=======================================================================
|
2 |
|
|
// Project Monophony
|
3 |
|
|
// Wire-Frame 3D Graphics Accelerator IP Core
|
4 |
|
|
//
|
5 |
|
|
// File:
|
6 |
|
|
// fm_geo_persdiv.v
|
7 |
|
|
//
|
8 |
|
|
// Abstract:
|
9 |
|
|
// Perspective division module. 22-bit floating point number
|
10 |
|
|
//
|
11 |
|
|
// Author:
|
12 |
9 |
specular |
// Kenji Ishimaru (info.info.wf3d@gmail.com)
|
13 |
2 |
specular |
//
|
14 |
|
|
//======================================================================
|
15 |
|
|
//
|
16 |
|
|
// Copyright (c) 2015, Kenji Ishimaru
|
17 |
|
|
// All rights reserved.
|
18 |
|
|
//
|
19 |
|
|
// Redistribution and use in source and binary forms, with or without
|
20 |
|
|
// modification, are permitted provided that the following conditions are met:
|
21 |
|
|
//
|
22 |
|
|
// -Redistributions of source code must retain the above copyright notice,
|
23 |
|
|
// this list of conditions and the following disclaimer.
|
24 |
|
|
// -Redistributions in binary form must reproduce the above copyright notice,
|
25 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
26 |
|
|
// and/or other materials provided with the distribution.
|
27 |
|
|
//
|
28 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
29 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
30 |
|
|
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
31 |
|
|
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
32 |
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
33 |
|
|
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
34 |
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
35 |
|
|
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
36 |
|
|
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
37 |
|
|
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
38 |
|
|
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
39 |
|
|
//
|
40 |
|
|
// Revision History
|
41 |
|
|
|
42 |
|
|
`include "fm_3d_define.v"
|
43 |
|
|
|
44 |
|
|
module fm_geo_persdiv (
|
45 |
|
|
input clk_core,
|
46 |
|
|
input rst_x,
|
47 |
|
|
output o_state,
|
48 |
|
|
// vertex input
|
49 |
|
|
input i_en,
|
50 |
|
|
output o_ack,
|
51 |
|
|
input [21:0] i_vx,
|
52 |
|
|
input [21:0] i_vy,
|
53 |
|
|
input [21:0] i_vw,
|
54 |
|
|
input [5:0] i_outcode,
|
55 |
|
|
// vertex output
|
56 |
|
|
output o_en,
|
57 |
|
|
input i_ack,
|
58 |
|
|
output [5:0] o_outcode,
|
59 |
|
|
output [21:0] o_vx,
|
60 |
|
|
output [21:0] o_vy
|
61 |
|
|
);
|
62 |
|
|
localparam P_IDLE = 'd0;
|
63 |
|
|
localparam P_WRECIP0 = 'd1;
|
64 |
|
|
localparam P_X_IN = 'd2; // recip is ready,
|
65 |
|
|
localparam P_Y_IN = 'd3;
|
66 |
|
|
localparam P_WMUL0 = 'd4;
|
67 |
|
|
localparam P_WMUL1 = 'd5; // x/w is ready
|
68 |
|
|
localparam P_WMUL2 = 'd6; // y/w is ready
|
69 |
|
|
localparam P_DONE = 'd7;
|
70 |
|
|
|
71 |
|
|
//////////////////////////////////
|
72 |
|
|
// regs
|
73 |
|
|
//////////////////////////////////
|
74 |
|
|
reg [3:0] r_state;
|
75 |
|
|
reg [21:0] r_vx; // share input/result
|
76 |
|
|
reg [21:0] r_vy; // share input/result
|
77 |
|
|
reg [21:0] r_ivw;
|
78 |
|
|
reg [5:0] r_outcode;
|
79 |
|
|
|
80 |
|
|
//////////////////////////////////
|
81 |
|
|
// wire
|
82 |
|
|
//////////////////////////////////
|
83 |
|
|
wire [21:0] w_fmul_a;
|
84 |
|
|
wire [21:0] w_ivc;
|
85 |
|
|
wire [21:0] w_ivw;
|
86 |
|
|
wire [21:0] w_recip_out;
|
87 |
|
|
|
88 |
|
|
wire w_set_outcode;
|
89 |
|
|
wire w_set_ivw;
|
90 |
|
|
wire w_set_vx_in;
|
91 |
|
|
wire w_set_vy_in;
|
92 |
|
|
wire w_set_vx_out;
|
93 |
|
|
wire w_set_vy_out;
|
94 |
|
|
//////////////////////////////////
|
95 |
|
|
// assign
|
96 |
|
|
//////////////////////////////////
|
97 |
|
|
assign o_state = (r_state == P_IDLE);
|
98 |
|
|
assign w_set_outcode = (r_state == P_IDLE);
|
99 |
|
|
assign w_fmul_a = (r_state == P_X_IN) ? r_vx : r_vy;
|
100 |
|
|
assign w_set_vx_in = (r_state == P_IDLE);
|
101 |
|
|
assign w_set_vy_in = (r_state == P_IDLE);
|
102 |
|
|
assign w_set_vx_out = (r_state == P_WMUL1);
|
103 |
|
|
assign w_set_vy_out = (r_state == P_WMUL2);
|
104 |
|
|
assign w_set_ivw = (r_state == P_X_IN);
|
105 |
|
|
assign w_ivc = (w_set_ivw) ? w_ivw : r_ivw;
|
106 |
|
|
assign o_vx = r_vx;
|
107 |
|
|
assign o_vy = r_vy;
|
108 |
|
|
assign o_en = (r_state == P_DONE);
|
109 |
|
|
assign o_ack = (r_state == P_IDLE);
|
110 |
|
|
assign o_outcode = r_outcode;
|
111 |
|
|
|
112 |
|
|
//////////////////////////////////
|
113 |
|
|
// always
|
114 |
|
|
//////////////////////////////////
|
115 |
|
|
|
116 |
|
|
always @(posedge clk_core) begin
|
117 |
|
|
if (w_set_outcode) r_outcode <= i_outcode;
|
118 |
|
|
if (w_set_ivw) r_ivw <= w_ivw;
|
119 |
|
|
if (w_set_vx_in) r_vx <= i_vx;
|
120 |
|
|
else if (w_set_vx_out) r_vx <= w_recip_out;
|
121 |
|
|
if (w_set_vy_in) r_vy <= i_vy;
|
122 |
|
|
if (w_set_vy_out) r_vy <= w_recip_out;
|
123 |
|
|
end
|
124 |
|
|
|
125 |
|
|
`ifdef D3D_SYNC_RESET
|
126 |
|
|
always @(posedge clk_core) begin
|
127 |
|
|
`else
|
128 |
|
|
always @(posedge clk_core or negedge rst_x) begin
|
129 |
|
|
`endif
|
130 |
|
|
if (rst_x == `D3D_RESET_POL) begin
|
131 |
|
|
r_state <= P_IDLE;
|
132 |
|
|
end else begin
|
133 |
|
|
case (r_state)
|
134 |
|
|
P_IDLE: begin
|
135 |
|
|
if (i_en) r_state <= P_WRECIP0;
|
136 |
|
|
end
|
137 |
|
|
P_WRECIP0: r_state <= P_X_IN;
|
138 |
|
|
P_X_IN: r_state <= P_Y_IN;
|
139 |
|
|
P_Y_IN: r_state <= P_WMUL0;
|
140 |
|
|
P_WMUL0: r_state <= P_WMUL1;
|
141 |
|
|
P_WMUL1: r_state <= P_WMUL2;
|
142 |
|
|
P_WMUL2: r_state <= P_DONE;
|
143 |
|
|
P_DONE: begin
|
144 |
|
|
if (i_ack) r_state <= P_IDLE;
|
145 |
|
|
end
|
146 |
|
|
endcase
|
147 |
|
|
end
|
148 |
|
|
end
|
149 |
|
|
|
150 |
|
|
//////////////////////////////////
|
151 |
|
|
// module instance
|
152 |
|
|
//////////////////////////////////
|
153 |
|
|
fm_3d_frcp u_frcp (
|
154 |
|
|
.clk_core(clk_core),
|
155 |
|
|
.i_en(1'b1),
|
156 |
|
|
.i_a(i_vw),
|
157 |
|
|
.o_c(w_ivw)
|
158 |
|
|
);
|
159 |
|
|
|
160 |
|
|
fm_3d_fmul u_fmul (
|
161 |
|
|
.clk_core(clk_core),
|
162 |
|
|
.i_en(1'b1),
|
163 |
|
|
.i_a(w_fmul_a),
|
164 |
|
|
.i_b(w_ivc),
|
165 |
|
|
.o_c(w_recip_out)
|
166 |
|
|
);
|
167 |
|
|
|
168 |
|
|
endmodule
|