//--------------------------------------------------------------------------------------------------
//
// Title       : PWMTranslator
// Design      : U4
// Author      : SJensen
// Company     : MBARI
//
//-------------------------------------------------------------------------------------------------
//
// File        : c:\Projects\genosensor\2g\CPLDs\Rvavle\Rvalve\U4\src\PWMTranslator.v
// Generated   : Mon May  3 12:21:16 2004
// From        : interface description file
// By          : Itf2Vhdl ver. 1.20
//
//-------------------------------------------------------------------------------------------------
//
// Description : 
//
//-------------------------------------------------------------------------------------------------
`timescale 1ps / 1ps

//{{ Section below this comment is automatically maintained
//   and may be overwritten
//{module {PWMTranslator}}
module PWMTranslator ( Reset ,PWMsource ,DRVCTLw ,Mclk ,MTRPWR ,Motor0_Phase_A_HIN ,Motor0_Phase_B_HIN ,Motor1_Phase_A_HIN ,inDATA ,Motor1_Phase_B_HIN ,Motor2_Phase_A_HIN ,Motor2_Phase_B_HIN ,Motor3_Phase_A_HIN ,Motor0_Phase_A_LIN ,Motor3_Phase_B_HIN ,Motor0_Phase_B_LIN ,Motor1_Phase_A_LIN ,Motor1_Phase_B_LIN ,Motor2_Phase_A_LIN ,Motor2_Phase_B_LIN ,Motor3_Phase_A_LIN ,Motor3_Phase_B_LIN ,MTRPWR_n ,DRVMTRCTLw );

input DRVMTRCTLw ;
wire DRVMTRCTLw ;
input [7:0] inDATA ;
wire [7:0] inDATA ;
input Reset ;
wire Reset ;
input PWMsource ;
wire PWMsource ;
input DRVCTLw ;
wire DRVCTLw ;
input Mclk ;
wire Mclk ;

output MTRPWR ;
wire MTRPWR ;
output Motor0_Phase_A_HIN ;
wire Motor0_Phase_A_HIN ;
output Motor1_Phase_A_HIN ;
wire Motor1_Phase_A_HIN ;
output Motor0_Phase_B_HIN ;
wire Motor0_Phase_B_HIN ;
output Motor1_Phase_B_HIN ;
wire Motor1_Phase_B_HIN ;
output Motor2_Phase_A_HIN ;
wire Motor2_Phase_A_HIN ;
output Motor2_Phase_B_HIN ;
wire Motor2_Phase_B_HIN ;
output Motor3_Phase_A_HIN ;
wire Motor3_Phase_A_HIN ;
output Motor3_Phase_B_HIN ;
wire Motor3_Phase_B_HIN ;
output Motor0_Phase_A_LIN ;
wire Motor0_Phase_A_LIN ;
output Motor1_Phase_A_LIN ;
wire Motor1_Phase_A_LIN ;
output Motor0_Phase_B_LIN ;
wire Motor0_Phase_B_LIN ;
output Motor1_Phase_B_LIN ;
wire Motor1_Phase_B_LIN ;
output Motor2_Phase_A_LIN ;
wire Motor2_Phase_A_LIN ;
output Motor2_Phase_B_LIN ;
wire Motor2_Phase_B_LIN ;
output Motor3_Phase_A_LIN ;
wire Motor3_Phase_A_LIN ;
output Motor3_Phase_B_LIN ;
wire Motor3_Phase_B_LIN ;
output MTRPWR_n ;
wire MTRPWR_n ;

//}} End of automatically maintained section

// -- Enter your statements here -- //

reg MTRPWRr;

//reg's hold the information how to control the motor assign follows:
//Mx1, Mx0 = 00 - Motor Off, no braking and not driven, Low Power
//Mx1, Mx0 = 01 - Motor Driven FWD if PWM high, else coast FWD
//Mx1, Mx0 = 10 - Motor Driven REV if PWM high, else coast REV
//Mx1, Mx0 = 11 - Motor initial Brake, without regard to PWM 
reg M31,M30,M21,M20,M11,M10,M01,M00;

function A_HIN;
input PWM;
input M1;
input M0;
A_HIN = (PWM & !M1 & M0);
endfunction

function B_HIN;
input PWM;
input M1;
input M0;
B_HIN = (PWM & M1 & !M0);
endfunction

function A_LIN;
input PWM;
input M1;
input M0;
A_LIN = (!M1 & !M0) | (PWM & !M1) | (!PWM & !M0);
endfunction

function B_LIN;
input PWM;
input M1;
input M0;
B_LIN = (!M1 & !M0) | (!PWM & !M1) | (PWM & !M0);
endfunction

assign Motor0_Phase_A_HIN = A_HIN(PWMsource, M01, M00);
assign Motor0_Phase_B_HIN = B_HIN(PWMsource, M01, M00);
assign Motor0_Phase_A_LIN = A_LIN(PWMsource, M01, M00);
assign Motor0_Phase_B_LIN = B_LIN(PWMsource, M01, M00);

assign Motor1_Phase_A_HIN = A_HIN(PWMsource, M11, M10);
assign Motor1_Phase_B_HIN = B_HIN(PWMsource, M11, M10);
assign Motor1_Phase_A_LIN = A_LIN(PWMsource, M11, M10);
assign Motor1_Phase_B_LIN = B_LIN(PWMsource, M11, M10);

assign Motor2_Phase_A_HIN = A_HIN(PWMsource, M21, M20);
assign Motor2_Phase_B_HIN = B_HIN(PWMsource, M21, M20);
assign Motor2_Phase_A_LIN = A_LIN(PWMsource, M21, M20);
assign Motor2_Phase_B_LIN = B_LIN(PWMsource, M21, M20);

assign Motor3_Phase_A_HIN = A_HIN(PWMsource, M31, M30);
assign Motor3_Phase_B_HIN = B_HIN(PWMsource, M31, M30);
assign Motor3_Phase_A_LIN = A_LIN(PWMsource, M31, M30);
assign Motor3_Phase_B_LIN = B_LIN(PWMsource, M31, M30);

assign MTRPWR = MTRPWRr;
assign MTRPWR_n = !MTRPWRr;

always @ (negedge Mclk)			
	begin
		if (Reset) {M31,M30,M21,M20,M11,M10,M01,M00} <= 8'b0;
		else if (DRVMTRCTLw) {M31,M30,M21,M20,M11,M10,M01,M00 } <= inDATA;
		else {M31,M30,M21,M20,M11,M10,M01,M00 } <= {M31,M30,M21,M20,M11,M10,M01,M00 };
		
		if (Reset) MTRPWRr <= 1'b0;
		else if (DRVCTLw) MTRPWRr <= inDATA[2];
		else MTRPWRr <= MTRPWRr;
	end
endmodule
