data/full_repos/permissive/48192473/rtl/sound_controller_modules/SoundControllerChannel3.v
1: b'%Warning-WIDTH: data/full_repos/permissive/48192473/rtl/sound_controller_modules/SoundControllerChannel3.v:150: Operator ASSIGNDLY expects 9 bits on the Assign RHS, but Assign RHS\'s SEL generates 8 bits.\n                                                                                                              : ... In instance SoundCtrlChannel3\n   rLength   <= iNR31[7:0];\n             ^~\n                ... Use "/* verilator lint_off WIDTH */" and lint_on around source to disable this message.\n%Warning-WIDTH: data/full_repos/permissive/48192473/rtl/sound_controller_modules/SoundControllerChannel3.v:151: Operator SUB expects 32 or 20 bits on the RHS, but RHS\'s SEL generates 8 bits.\n                                                                                                              : ... In instance SoundCtrlChannel3\n   rLengthCounter  <= 256 - iNR31[7:0];   \n                          ^\n%Warning-WIDTH: data/full_repos/permissive/48192473/rtl/sound_controller_modules/SoundControllerChannel3.v:162: Operator ASSIGNDLY expects 11 bits on the Assign RHS, but Assign RHS\'s SUB generates 32 or 12 bits.\n                                                                                                              : ... In instance SoundCtrlChannel3\n   rSoundFrequency[10:0] <= 2048-{iNR34[2:0],iNR33[7:0]};\n                         ^~\n%Warning-WIDTH: data/full_repos/permissive/48192473/rtl/sound_controller_modules/SoundControllerChannel3.v:163: Operator ASSIGNDLY expects 11 bits on the Assign RHS, but Assign RHS\'s SUB generates 32 or 12 bits.\n                                                                                                              : ... In instance SoundCtrlChannel3\n   rSoundFrequencyCounter <= 2048-{iNR34[2:0],iNR33[7:0]}; \n                          ^~\n%Warning-WIDTH: data/full_repos/permissive/48192473/rtl/sound_controller_modules/SoundControllerChannel3.v:208: Operator SUB expects 32 or 20 bits on the RHS, but RHS\'s VARREF \'rLength\' generates 9 bits.\n                                                                                                              : ... In instance SoundCtrlChannel3\n   rLengthCounter <= 256-rLength;\n                        ^\n%Error: Exiting due to 5 warning(s)\n'
module SoundCtrlChannel3
  (
    input wire		iClock,
    input wire		iReset,
	input wire		iOsc256,
    input wire		iOsc262k,

    input wire [7:0] iNR30,
    input wire [7:0] iNR31,
    input wire [7:0] iNR32,
    input wire [7:0] iNR33,
    input wire [7:0] iNR34,

    output reg [4:0]	oOut, 
    output wire oOnFlag
  );


	reg [8:0] rLength;
 	reg [19:0] rLengthCounter;	
	reg rTimedMode;
 	reg rLengthComplete;

	reg rTone;
	reg [10:0] rSoundFrequency;
	reg [10:0] rSoundFrequencyCounter;

	reg rChannelEnable;
	reg [1:0] rOutLevel;
	reg [3:0] rStep;
	reg [3:0] rStepScaled;

 	wire [4:0] up_value, down_value;

 	reg [4:0] rWaveRamIndex;
	reg rWaveRamHnL;

	reg [7:0] iWaveRam [0:31];






 	

	always @(posedge iClock) begin
		if (iReset || iNR34[7]) begin

			rChannelEnable	<= iNR30[7];

			rLength 		<= iNR31[7:0];
			rLengthCounter 	<= 256 - iNR31[7:0];
			rLengthComplete <= 0;

			rTimedMode 		<= iNR34[6];

			rOutLevel		<= iNR32[6:5];
			rStep			<= iWaveRam[0][3:0];
			rWaveRamIndex	<= 0;
			rWaveRamHnL		<= 1;

			rTone 			<= 0;
			rSoundFrequency[10:0] <= 2048-{iNR34[2:0],iNR33[7:0]};
			rSoundFrequencyCounter <= 2048-{iNR34[2:0],iNR33[7:0]}; 

		end
	end


	always @ (posedge rTone) begin
		if (rChannelEnable) begin
			if (~rWaveRamHnL) begin	
				rStep <= iWaveRam[rWaveRamIndex][7:4];
				rWaveRamHnL <= ~rWaveRamHnL;
			end
			else begin
				rStep <= iWaveRam[rWaveRamIndex][3:0];
				rWaveRamHnL <= ~rWaveRamHnL;
				
				if (rWaveRamIndex == 5'd31) begin
					rWaveRamIndex <= rWaveRamIndex+1;
					rLengthComplete <= rTimedMode;			
				end
				else begin
					rWaveRamIndex <= rWaveRamIndex+1;
				end
			end
		end
		else begin
			rStep <= 4'b0;
		end	
	end


	always @(posedge iOsc262k) begin
		if (rSoundFrequencyCounter ==0) begin
			rSoundFrequencyCounter <= rSoundFrequency;
			rTone <= ~rTone;
		end
		else begin
			rSoundFrequencyCounter <= rSoundFrequencyCounter-1;
		end
	end



	always @(posedge iOsc256) begin
		if (rLengthCounter == 0) begin
			rLengthCounter <= 256-rLength;
			rLengthComplete <= rTimedMode;
		end
		else begin
			rLengthCounter <= rLengthCounter-1;
		end
	end


	assign up_value = 5'd15 + rStepScaled;
	assign down_value = 5'd15 - rStepScaled;

	always @(posedge iClock) begin
		if (rLengthComplete) begin
			oOut[4:0] <= 5'd15;
		end
		else begin
			if (rTone) begin
				oOut[4:0] <= up_value[4:0];
			end
			else begin
				oOut[4:0] <= down_value[4:0];
			end
		end
	end



	always @ (rOutLevel or rStep) begin
		case (rOutLevel[1:0])
			2'd0: rStepScaled = 0;
			2'd1: rStepScaled = rStep;
			2'd2: rStepScaled = rStep >> 1;
			2'd3: rStepScaled = rStep >> 2;
		endcase
	end

assign oOnFlag = rLengthComplete;

endmodule
module SoundCtrlChannel3
  (
    input wire		iClock,
    input wire		iReset,
	input wire		iOsc256,
    input wire		iOsc262k,

    input wire [7:0] iNR30,
    input wire [7:0] iNR31,
    input wire [7:0] iNR32,
    input wire [7:0] iNR33,
    input wire [7:0] iNR34,

    output reg [4:0]	oOut, 
    output wire oOnFlag
  );



	reg [8:0] rLength;
 	reg [19:0] rLengthCounter;	
	reg rTimedMode;
 	reg rLengthComplete;

	reg rTone;
	reg [10:0] rSoundFrequency;
	reg [10:0] rSoundFrequencyCounter;

	reg rChannelEnable;
	reg [1:0] rOutLevel;
	reg [3:0] rStep;
	reg [3:0] rStepScaled;

 	wire [4:0] up_value, down_value;

 	reg [4:0] rWaveRamIndex;
	reg rWaveRamHnL;

	reg [7:0] iWaveRam [0:31];






 	

	always @(posedge iClock) begin
		if (iReset || iNR34[7]) begin

			rChannelEnable	<= iNR30[7];

			rLength 		<= iNR31[7:0];
			rLengthCounter 	<= 256 - iNR31[7:0];
			rLengthComplete <= 0;

			rTimedMode 		<= iNR34[6];

			rOutLevel		<= iNR32[6:5];
			rStep			<= iWaveRam[0][3:0];
			rWaveRamIndex	<= 0;
			rWaveRamHnL		<= 1;

			rTone 			<= 0;
			rSoundFrequency[10:0] <= 2048-{iNR34[2:0],iNR33[7:0]};
			rSoundFrequencyCounter <= 2048-{iNR34[2:0],iNR33[7:0]}; 

		end
	end


	always @ (posedge rTone) begin
		if (rChannelEnable) begin
			if (~rWaveRamHnL) begin	
				rStep <= iWaveRam[rWaveRamIndex][7:4];
				rWaveRamHnL <= ~rWaveRamHnL;
			end
			else begin
				rStep <= iWaveRam[rWaveRamIndex][3:0];
				rWaveRamHnL <= ~rWaveRamHnL;
				
				if (rWaveRamIndex == 5'd31) begin
					rWaveRamIndex <= rWaveRamIndex+1;
					rLengthComplete <= rTimedMode;			
				end
				else begin
					rWaveRamIndex <= rWaveRamIndex+1;
				end
			end
		end
		else begin
			rStep <= 4'b0;
		end	
	end


	always @(posedge iOsc262k) begin
		if (rSoundFrequencyCounter ==0) begin
			rSoundFrequencyCounter <= rSoundFrequency;
			rTone <= ~rTone;
		end
		else begin
			rSoundFrequencyCounter <= rSoundFrequencyCounter-1;
		end
	end



	always @(posedge iOsc256) begin
		if (rLengthCounter == 0) begin
			rLengthCounter <= 256-rLength;
			rLengthComplete <= rTimedMode;
		end
		else begin
			rLengthCounter <= rLengthCounter-1;
		end
	end


	assign up_value = 5'd15 + rStepScaled;
	assign down_value = 5'd15 - rStepScaled;

	always @(posedge iClock) begin
		if (rLengthComplete) begin
			oOut[4:0] <= 5'd15;
		end
		else begin
			if (rTone) begin
				oOut[4:0] <= up_value[4:0];
			end
			else begin
				oOut[4:0] <= down_value[4:0];
			end
		end
	end



	always @ (rOutLevel or rStep) begin
		case (rOutLevel[1:0])
			2'd0: rStepScaled = 0;
			2'd1: rStepScaled = rStep;
			2'd2: rStepScaled = rStep >> 1;
			2'd3: rStepScaled = rStep >> 2;
		endcase
	end

assign oOnFlag = rLengthComplete;

endmodule