📄 calcmvnormal.v
字号:
module calcMvNormal
(
input clk, input reset,
input wire [17:0] xu, input wire [17:0] yu, input wire [17:0] zu,
input wire [17:0] xv, input wire [17:0] yv, input wire [17:0] zv,
input wire [17:0] xw, input wire [17:0] yw, input wire [17:0] zw,
input wire [17:0] x, input wire [17:0] y, input wire [17:0] z,
output wire [17:0] Out0,
output wire [17:0] Out1,
output wire [17:0] Out2,
output reg done,
input [17:0] det
);
wire [17:0] xu_x, xv_y, xw_z, yu_x, yv_y, yw_z, zu_x, zv_y, zw_z, sum0, sum1, sum2;
wire [17:0] OutFinal, Out, inv1, inv2, inv3, mult1, mult2, mult3;
reg [17:0] xu_xr, xv_yr, yu_xr, yv_yr, zu_xr, zv_yr, xw_zr, yw_zr, zw_zr, sum0r, sum1r, sum2r;
reg [4:0] state;
reg [17:0] Outr, Out0r, Out1r, Out2r, inv3r, inv1r, inv2r, mult1r, mult2r, mult3r, mult3rbuff;
assign Out0 = (state == exit) ? Out0r : 18'b0;
assign Out1 = (state == exit) ? Out1r : 18'b0;
assign Out2 = (state == exit) ? Out2r : 18'b0;
/* Matrix multiplication to find Mv * Vertex Matrix
MV(Transpose)
[ xu xv xw ]
[ yu yv yw ]
[ zu zv zw ]
*/
/*
FINDING INVERSE NORMAL
a1 a2 b1 b2 c1 c2 d1 d2 e1 e2 f1 f2
State 1 [ zw*yv - zv*yw -(zw*xv - zv*xw) yw*xv - yv*xw ] [x]
State 2 [-(zw*yu - zu*yv) zw*xu - zu*xw -(yw*xu - yu*xw)] [y]
State 3 [ zv*yu - zu*yv -(zv*xu - zu*xv) yv*xu - yu*xv ] [z]
*/
/* Stage 0: Determine components for each row */
wire [17:0] a, a1, a2, b, b1, b2, c, c1, c2, d, d1, d2, e, e1, e2, f, f1, f2;
reg [17:0] ar, br, cr, dr, er, fr;
//use same multipliers to do different multiplies depending on state of state machine.
//this conserves multipliers
assign a1 = (state == 5'd0) ? zw : (state == 5'd1) ? zw : zv;
assign a2 = (state == 5'd0) ? yv : (state == 5'd1) ? yu : yu;
assign b1 = (state == 5'd0) ? {~zv[17],zv[16:0]} : (state == 5'd1) ? {~zu[17],zu[16:0]} : {~zu[17],zu[16:0]};
assign b2 = (state == 5'd0) ? yw : (state == 5'd1) ? yv : yv;
assign c1 = (state == 5'd0) ? zw : (state == 5'd1) ? zw : zv;
assign c2 = (state == 5'd0) ? xv : (state == 5'd1) ? xu : xu;
assign d1 = (state == 5'd0) ? {~zv[17],zv[16:0]} : (state == 5'd1) ? {~zu[17],zu[16:0]} : {~zu[17],zu[16:0]};
assign d2 = (state == 5'd0) ? xw : (state == 5'd1) ? xw : xv;
assign e1 = (state == 5'd0) ? yw : (state == 5'd1) ? yw : yv;
assign e2 = (state == 5'd0) ? xv : (state == 5'd1) ? xu : xu;
assign f1 = (state == 5'd0) ? {~yv[17],yv[16:0]} : (state == 5'd1) ? {~yu[17],yu[16:0]} : {~yu[17],yu[16:0]};
assign f2 = (state == 5'd0) ? xw : (state == 5'd1) ? xw : xv;
/* Stage 0 multiply components of each column*/
fpmult inst1(a, a1, a2);
fpmult inst2(b, b1, b2);
fpmult inst3(c, c1, c2);
fpmult inst4(d, d1, d2);
fpmult inst5(e, e1, e2);
fpmult inst6(f, f1, f2);
/* Stage 1: Add components calculated in each row. Pass onto next Part */
fpadd isnt1i(inv1, ar, br);
fpadd inst2i(inv2, cr, dr);
fpadd inst3i(inv3, er, fr);
/* Part 2 */
/* Stage 2 */
fpmult instg(mult1, inv1r, x);
fpmult insth(mult2, inv2r, y);
fpmult insti(mult3, inv3r, z);
/* Stage 3 */
fpadd instp(sum0, mult1r, mult2r);
/* Stage 4 */
fpadd instt(Out, sum0r, mult3rbuff);
/* Stage 5 */
fpmult instff(OutFinal, Outr, det);
//2 more cycles for 3rd output to be calculated
parameter exit = 5'd15;
always@(posedge clk) begin
if(reset) begin
ar <= 18'b0;
br <= 18'b0;
cr <= 18'b0;
dr <= 18'b0;
er <= 18'b0;
fr <= 18'b0;
inv1r <= 18'b0;
inv2r <= 18'b0;
inv3r <= 18'b0;
mult1r <= 18'b0;
mult2r <= 18'b0;
mult3r <= 18'b0;
mult3rbuff <= 18'b0;
sum0r <= 18'b0;
Out0r <= 18'b0;
Out1r <= 18'b0;
Out2r <= 18'b0;
state <= 5'd0;
done <= 1'b0;
end
else begin
case(state)
0: begin //store first set of calculations
ar <= a;
br <= b;
cr <= c;
dr <= d;
er <= e;
fr <= f;
state <= 5'd1;
done <= 1'b0;
end
1: begin //store 2nd set of calculations
ar <= a;
br <= b;
cr <= c;
dr <= d;
er <= e;
fr <= f;
inv1r <= inv1;
inv2r <= {~inv2[17],inv2[16:0]};
inv3r <= inv3;
state <= 5'd2;
end
2: begin //store 3rd set of calculations
ar <= a;
br <= b;
cr <= c;
dr <= d;
er <= e;
fr <= f;
inv1r <= {~inv1[17],inv1[16:0]};
inv2r <= inv2;
inv3r <= {~inv3[17],inv3[16:0]};
mult1r <= mult1;
mult2r <= mult2;
mult3r <= mult3;
mult3rbuff <= mult3r;
state <= 5'd3;
end
3: begin //store 4th set of calculations
inv1r <= inv1;
inv2r <= {~inv2[17],inv2[16:0]};
inv3r <= inv3;
mult1r <= mult1;
mult2r <= mult2;
mult3r <= mult3;
mult3rbuff <= mult3r;
sum0r <= sum0;
state <= 5'd4;
end
4: begin //store 5th set of calculations
mult1r <= mult1;
mult2r <= mult2;
mult3r <= mult3;
mult3rbuff <= mult3r;
sum0r <= sum0;
Outr <= Out;
state <= 5'd5;
end
5: begin //store 6th set of calculations
mult3rbuff <= mult3r;
sum0r <= sum0;
Out0r <= OutFinal;
Outr <= Out;
state <= 5'd6;
end
6: begin //store 7th set of calculations
Out1r <= OutFinal;
Outr <= Out;
state <= 5'd7;
end
7: begin //store final set of calculations
Out2r <= OutFinal;
state <= exit;
end
exit: begin //output result
done <= 1'b1;
end
endcase
end
end
endmodule
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -