      TITLE   "single scheme- discrete notching SDF5.0 Decoder Series 4+ w/HORN SELECTIONS"
----------------------------------------------------------------------------
 PROJECT_ID:	1-00<ENTER ID HERE and below>   3/25/25
 <ROAD> <PRIME MOVER> <CLASS> <MODEL>  	
 <h>.SPJ Version 4.7
ASM:		D4hc3d3.asm	
SDF:		<ROAD> D4hc3d3 <updatedate>.SDF
BASE CODE:	D4hc3d3 -- Discrete Notching,  3 Channel, SDF Version 3.0
MEMORY:	512K bytes required
SDF5.0:	Requires -- 	 #INCLUDE	../SFX3_snd_cmd.INC
					and	 #INCLUDE	../SFX3_snd_macs.inc
=====================================================================
Author:  John McMasters
	Custom sound scheme. MAR, 2025 NOT for resale.
	(C) Copyright John McMasters 2025 used with permission.
SOUND SET:	<PRIME MOVER> <MODEL> <CLASS> <TYPE> <h>
CHANNELS:	3
SERIES:	4 
CV60=<0>=DIESEL SOUND SCHEME <Enter Scheme 2 digits>
See Demo Sound Sample
https://www.youtube.com/watch?v=<video>
at:
https://www.youtube.com/channel/UCo03Fs3_U43yz_hLkZUUyOA
DO NOT use hyphens in replacement arguments (above) or file names. Use underscores instead.
see <ENTER HERE> in ASM file -- Modify these constants for your custom model 
----------------------------------------------------------------------------
   <ROAD> <CLASS> <MODEL> <TYPE> 
 
 Specifications:
  <CLASS SPECIFICATIONS HERE>
<replace here from ASM and then replace semi-colons with blank>
----------------------------------------------------------------------------
	INTRODUCTION
----------------------------------------------------------------------------
  This is the Sound Definition Language (SDL) file for a project that can be
  used for a 16bit SMALL (512K) FLASH MEMORY sound decoder for a typical locomotive. 
----------------------------------------------------------------------------
Features:
	Bells -- bells may be chained to play for simulation of rope pulls
	Low Idle -- Optional, Idle run is trimmed, also optional ALCO stumble
	Fans -- High and low alternate random
	Estop -- F12 will trigger shutdown
	Auto Vary Notch	--	motor sounds vary: idle blip, load sensing
	Variable Dynamics -- F5 will notch up to run 4 and play dynamic fan, speed varied by throttle setting
	Horns/Whistles -- Selectable, playable, signal toots
		Grade crossing horn plays rear horn sounds going backwards
		Analog Playable Horn/whistle/horn (horn runs that had the transition to higher pressure/ very loud modes)
	Brake Interlock	--F10 will lock motor at standstill until released
		Integrated automatic braking sounds on SF3
		All brakes are controlled by SF10 (formerly SF3) so that servo motor decoder braking could be syncronized
		EStop  on F12 On 	(disables motor movement until F12 OFF)
	Manual Notch Run mode 
	3 Channels (1 primary/motor, 3 high priority/whistle/horn, 3 braking bell/air/fan/misc, auxilary/road/blowers/pumps, CLANKS,SNIFTER,SLIPS/road, blowers/pumps)
 	Auto rod clank cyl blow down (steam cocks open)
 	Coast after deceleration for variable cycles depending on speed
 	Coal grate,  Coal chute,
 	Water Tank Fill, Stoker, shovel, Steam Greaser, Oil Pump, Empty Ash Pan
	Function-activated cylinder cocks sound effect at any speed step
	Manual chuff mode (equivalent to setting the steam throttle i.e. Jonson Bar
	with F26+ and F27- as well as automatic...)
	Tracton Motors
	

COLD STARTUP FUNCTIONS:
	Play Startup Sounds: F10 (set brake) + F28
	Toggle F12 On/Off	
 ----------------------------------------------------------------------------
	DESIGN NOTES 
 ----------------------------------------------------------------------------
 NOTE: Make sure that the model CV133 Steam Chuff/CAM config, 128=>EXT cam, 
 	1-200=>DRIVER dia in inches[63]
 	and CV134 Gear ratio are adjusted so that THERE ARE 4 STANDARD NON-ARTICULATED
 	CHUFFS PER REVOLUTION.
 
 CV9 sets the motor frequency in Khz from 4-50KHz. 
 CV10 trims motor tach gain for BEMF, 
 CV10:127 doubles tachometer sensitivity and will slow 1% step (CV19:0 /64) from 
 about 1 scale mph to around 0.5mph. 
 The range of CV134/gear ratio is increaased so you can go from 10/32 to 96/32 ratio
 	 (96=3:1 chuff rate/per driver rev)
 
 The new autochuff uses the actual motor speed directly (BEMF) instead of the S6 method 
 	that uses inferred pwm_SOUND speed as chuff source number for rate. 
 	This means the chuffs follow motor speed as the loco loads up and slows down.  
 	To get most effect on a grade/high load, you would lower CV57 so the motor slows 
 	under load, since the BEMF will not stabilize as much.
 	When BEMF is OFF [CV57 =0] or if CV129.1 = 1 then the old default S6 type pwm value 
 	will be used
 
 BEMF (Motor Controls)
 	CV65=101 KICK START
 	CV3=8 ACCELERATION MOMENTUM (How long it takes to speed up to selected throttle value)
 	CV4=6 DECELERATION MOMENTUM (How long it takes to slow down to selected throttle value)
 	CV9=0 DRIVE FREQ.
 	CV55=128 EMF STATIC
 	CV56=80 EMF DYNAMIC
 	CV57=8 (bits 0-3) BEMF (EMF DROOP) (How quick does decoder make the motor react to load)
 	CV129=0 (1=PW6, 0=PW7) POWER (Selects logic to follow changes in motor speed)
	
SET -- SCALE_MIN,2,0	Range Volume Scale 1-14 to a value between 2-15
						Drifting volume will diminish to this level.

Limits:
	One SPJ project SDF of multiple schemes can run up to 64Kbytes of code.
	A single scheme has a 4Kbyte GOTO instruction branch range. (e.g. 2K 2-byte or 1K 4-byte instructions)
	That means that a BRANCH_TO an address beyond the range limit will not execute...


Function Key Mapping:
 DECODERS WITH XF MAPPING CAN REMAP FUNCTION KEYS WITHOUT CODE CHANGES.
 
 This version (v7) includes CODE sections for mapping functions to default to other manufactures F0-F28 keys. 
 Or you can edit the TRIG include file, re-assemble and make your own custom function mapping.
 
 HOW LEGACY FUNCTION KEY MAPPING WORKS
 Mapping functions to default to other manufactures F0-F28 keys:
 Mapping is done by using symbolic constants to define the hex value for each function key. 
 For example TRIG_WHISTLES_ON is a value of 0x09. 
 The hex value 0x09 is the physical decoder function key (F2 in this case). 
 This is the same as the standard include for TRIG_SF2	 EQU  0x09     byte 1, bit1.  
 And the new symbolic constant is TRIG_WHISTLES_ON	 EQU  0x09     2-F2: byte 1, bit1. 
 The base code CAN use the remapped symbolic name for all functions. 
So any change to the physical assignment is masked.
Whereas TRIG_SF4	 EQU 0x0B now has a symbolic value of TRIG_OPENCYLS_ON EQU 0x0B 4-F4: CYL COCKS
One of the re-mapped values is TRIG_SAFETY_ON	 EQU  0x24 25-F25: BLOWOFF
This means that when F25 is toggled the SAFETY BLOWOFF sound function will play.
I have annotated eac EQU statement with the physical decoder button number 
for example: 2-F2: byte 1, bit1
See D8YTrigV8.INC for F-Key values. The ASM code has the INCLUDE for this file name.

----------------------------------
NOTES:
 notch volume routine is improved when the volume scale is set by
	other external events (deceleration, start from standing, acceleration, etc.)
	VARY_LOAD is used to implement the effect

----------------------------------
     FUNCTION KEYS DEFINED:   
----------------------------------
(A) Function Key Usage: 
F0- Lights  [disables start-up sounds when on during initialization]
F1- Bell (Bells1-4 may be chained)
F2- Horn/Whistle	CV150 selects Horn/Whistle type	(Playable when bit7 of CV150 is on)	-- Shorter sounds when SF3 ON
F3- Coupler / Short Whistle
F4- Emergency Lights / Reserved for light function (turn on rear light, etc.)
F5- Dynamic brake fans when moving		
F6- Ditchlights (Front -- flashing)
F7-	Alt Grade Crossing
F8- Mute control 1st time =192, 2nd time=32, 3rd time=60, 4th time=01, 5th time=192
F9-  UnCouple / Coupler disconnect	[Auto-coupler/brake set by  CV151 max spd]
		Lift ring and connect plays with F9 
F10- BRAKE TO STOP Independent Brake/Train Brake	[Leave F3 OFF for auto brakes] 
			[F10 on will enable servo motor stop.]
			Toggle OFF plays directional toots WHEN F3 ON
		1) Slow servo motor to stop using CV3,CV4 momentum values (where
			larger value (50) takes longer to slow/accelerate)
		2) Lock motor at standstill to simulate engine brake set. Servo
			will not resume until F10 is toggled off.
		3) While brake is set toggle F3 on to begin uncoupling. SF3 will play 
			lift ring noise in preparation for disconnect. 
		4) Releasing the brake and advancing the throttle will play coupler
			disconnect when F3 is already on.
		Brake rate is determined by BEMF and CV3,4
	 	Auto Coupler coupler clank and BRAKE if F3 is ON,  is dir chng and last PK spd below e.g. CV151
			[Auto-coupler/brake set by  CV151 max spd] SF3 ON for un-coupler
			[Leave F3 on for auto coupler]	
 	F10 OFF -- Release Brake [not F10 Apply else Release] 
F11- Train Brake (moving) / Hand Brake 
F12- EStop when moving and shut-down (disables motor movement until OFF)
F13- Quill /  Ditchlights (Rear -- flashing)
F14- Compressor 
F15- Engine Compartment Sounds / OPTION
F16- Air Dryer 
F17- Traction Blower 
F18- Sanding Valve 
F19- Short Air Let Off / Replace Horn with Whistle 
F20- High Fan/ Air features disable    
F21- Straight to manual notch1
F22- User Sound
F23- Flanges (continuous when moving) 
F24- Reverser Center
F25- Enables AUX generator / HEP 
F26- Manual Notching Up 
		Leave on for increased load
		Leave SF26 OFF to use run trim option: [will increment 
		notches on acceleration events 
		SF27 will decrement run trim.
F27- Notch down	-- Leave ON for decreased load
		Toggle SF27 ON Is notch down for EVENT, 
			integrate -VE in SPEED_DELTA
		SF27 will decrement run trim (i.e., use to select lower trim value).
		Wheel-slip, when CV155 is not 0 and SF3 ON
		Manual Notching Down / Coast
F28- Straight to Manual Notch8

	==================================  
(B) Diesel Sound FX defined CV's: 	[decoder CV8=8  reset value in brackets]
CV3   Momentum acceleration rate, 0-31, 31=max, (F10 uses after braking to stop) [30]
CV4   Momentum deceleration rate, 0-31, 31=max, (F10 uses for braking to stop) [30]
CV7   Version ID 51=Older decoders, 16=Newer SFX2 decoders
CV8   SCV_RESET  Reset Decoder values to factory =129=Digitrax
CV9   Drive Freq 0-16 [0]
CV10  TACH 0,127 Ver3 default0= Normal 1/1, 127= Half 1/2
CV19  Advanced Consist [0]
CV21  Advanced Consist F0-F8 [248] 
CV22  Advanced Consist F3-F12 [52] 
CV23  Acceleration Mass 0-255 [30] 
CV24  Deceleration Mass 0-255 [30] 
CV54  Road or Switching Mode [64,0,1]
CV55  EMF STATIC 0-255 [128]
CV56  EMF DYNAMIC 0-127 [80]
CV57  BEMF (bits 0-3)(EMF DROOP) [8] (How quick does decoder make the motor react to load)
CV58  Master Volume, 0-15, 0= max, (F8 used for Mute) [11] 
CV60  Select Scheme, 0=Diesel 0-9	[0] Opt Reset ALL CVs
CV64  Logic select CV, 0=Aux Logic output 64=Normal LED outputs	[64]
CV65  Kick Start 0-16 (special SDF Ver3 101)
CV105 SCV_USER_SDF_SUB_ID		CV105 	[xx] Sub ID is SUB id or project version
CV106 SCV_USER_SDF_FIX_ID		CV106 	[xx] FIX ID is change id or project release
CV129 SNDCV_CONFIGA POWER (1=PW6, 0=PW7)  [0](Selects logic to follow changes in motor speed)
CV132 Engine Notch rate						64-192	[128]
CV133 DRIVER Chuff/CAM config, 128=>EXT cam, 1-200=>DRIVER dia in inches [60]
CV134 Gear ratio trim, 32=100% ratio,	[41]  Range of CV134/gear ratio is increaased so you can go from 10/32 to 96/32 ratio (96=3:1 chuff rate/per driver rev)
CV135 vol level when MUTE action is triggered, e.g. F8=ON	[30]
CV139 Set to trigger distance event			0-64	[30]
CV140 PRIME mover Diesel/chuff exhaust vol	0-64	[50]
CV141 Bell Volume							0-64	[36]
CV142 WHISTLE/HORN vol						0-64	[42]
CV143 AIR feature volume, DRYER, compressor, brake release Air sounds Volume 0-64 [49]
CV144 Engine brake volume 					0-64	[55]
CV145 Miscellaneous volume 					0-64	[51] 
CV146 Delay (24mS counts) between Bells 1-100	[01] 
CV147 Time between DRYER cycles Users normally reset DRYER rate to 10 or more[27] 
CV148 Time between Air pump/Compressor cycles	[30] 
CV149 Air pump/Compressor run time				[52] 
CV150 Horn/Whistle Selector,0=std ,  1/7 =Alternate, +128 for Playable volume [0]
			HORN SELECTED CV150:
						Decimal	HEX			Bit
											7654 3210
				1		0		0x00		0000 0000
				2		1		0x01		0000 0001
				3		2		0x02		0000 0010
				4		3		0x03		0000 0011
				5		4		0x04		0000 0100
				6		5		0x05		0000 0101
				7		6		0x06		0000 0110
				PLAYABLE:
				1		128		0x80		1000 0000
				2		129		0x81		1000 0001
				3		130		0x82		1000 0010
				4		131		0x83		1000 0011
				5		132		0x84		1000 0100
						Decimal	HEX			Bit
CV151 speed limit FOR AUTOMATIC COUPLER/BRAKE on DIR chng. about 1/2MPH per unit
CV152 defines USER code for this SDF
CV153 is SUB id
CV154 Delay brake apply / speed at which brake sounds begin to play	1-100 [12] 
CV155 Notching mode 0=AUTO, 1=SEMIAUTO, 2=MANUAL NOTCHES	[01] /STEAM Slip Mode: 0=AUTO, 1=SEMIAUTO, 2=MANUAL 						[01]
CV156 delay for HORN start in 27mS VALUES
CV157 is FOR bell selection 
	BELL SELECTED CV157:
						Decimal	HEX			Bit
											7654 3210
				1		0		0x00		0000 0000
				2		1		0x01		0000 0001
				3		2		0x02		0000 0010
				4		3		0x03		0000 0011
				5		4		0x04		0000 0100
CV158 DIGITRAX RESERVED
CV159 Grate Shaker Volume							0-64	[64]
CV160 VARIANT id or project version					0-254	[000]
CV161 Coal Stoker run time							0-254	[20]							
CV162 Air Letoff / Steam Water Injector Volume 		0-64	[25]
CV163 Safety Run Time/ how often to check boiler pressure		0-64	[32]
CV164 Level in the guage/ Water Pump Cycle Time, percent full	0-64	 [19]
CV165 add fuel to firebox at 150lbs/ Stoker/oil pump run time	0-254	[20]	
CV166 Cutoff Speed for wheel slip chuffs			4-9	[9]
CV167 Cutoff Speed for Auto Cyl. Close Speed		0-64	[7]
CV168 Sander Volume									0-64	[32]
CV169 Cutoff Speed for high speed chuffs			40-99	[48]	
CV170 Cutoff Speed LOW	skips brake sounds			4-39	[12]
CV171 AUX GEN VOLUME								0-64	[40]
CV172 FAN MOTOR VOLUME								0-64	[30]
CV173 STEAM blowdown volume							0-64	[50]
CV174 AIR DRYER Air sounds Volume 					0-64	[49] 
CV175 Air compressor volume							0-64	[40]
CV176 TRACTION MOTOR 								0-64	[30]			
CV177 Coupler Slack Volume							0-64	[31]
CV178 Wheel Flange 									0-64	[40]
CV179 Dynamic brake / Dynamo volume 				0-64	[55]
CV180 Steam blow down/Safety vol					0-64	[59]
CV181 Power Reverser volume							0-64	[40]
CV182 brake release Air sounds Volume 				0-64 	[32] 
CV183 Water fill Volume								0-64	[54]
CV184 Steam cock blow down threshold 				1-64	[24]
CV185 bit values to disable features1:				0-128	[0]
		bit0 disable  HEP diesel =0 AUX GEN	=1			=1		
		bit1 disable hard automatic braking 			=2
		bit2 disable start up sounds					=4
		bit3 disable Fans								=8
		bit4 disable toots on direction change			=16
		bit5 disable grade crossing random play 		=32
		bit6 disable bell								=64
		bit7 Gen1-Gen2 Startup Sounds Opt				=128
CV186 bit values to disable additional features2 	0-128	[0]
		bit0 enable Diesel Low Idle=1		Disabled=0	=1=Enabled
		bit1 disable Chain bells 						1-4	=2 Single Bell =0
		bit2 disable STOKER change to OIL Pump			=4
		bit3 disable AUTO VARY NOTCH					=8
		bit4 disable Shaft Driven Compressor 			=16
		bit5 disable ALCO Idle Stumble=32 Disable=0		=32
		bit6 disable Auto REAR horn signals: 0=Off, 64=On	=64
		bit7 disable Dual Prime Movers				0=Off 128=Dual On
CV187 bit values to disable additional features3 	0-128 [0]
		bit0 disable Short Opt Horn/whistle	 Enabled=0,   =1=disabled
		bit1 disable DEMO ALL HornS/whistles Enabled=on=0,=2=disabled
		bit2 disable Bell on Idle Exit				=4
		bit3 disable Motor Lock  					=8
		bit4 disable Start up Muted					=16
		bit5 disable Dynamic BRAKES					=32
		bit6 disable Inverse Backup Direction		=64
		bit7 disable Auto Wheel Slip				=128
CV188	BIT values to disable additional features4	0-254 [0] 
		bit0 disable Traction Motor 		= 1 Disabled
		bit1 disable HILO HORN				= 2 Disabled
		bit2 disable Horn Delay 0=Disabled	= 4 Enabled
 		bit3 disable Disable Articulated DE-SYNC	=8
		bit4 disable Disable 3rd Cyl Chuffs			=16
		bit5 disable Notused						=32
		bit6 disable Disable/Enable Gen1 Transition	=64	=on=Disable
		bit7 disable Disable/ENABLE CV RESET ALL	=128 BIT OFF WILL ALLOW RESET
------- BIT7 OF CV188 WILL DISABLE FUTURE RESETS >>>
------- TOGGLE OFF FOR FUTURE RESET::::::::::::
CV189 Water Alarm Volume							0-64 [45]
CV190 Cutoff Speed for high speed chuffs / Minimum Speed Slip 40-99	[48]	
CV191 Drift Volume	PRIME MOVER COASTING			0-64 [49]
CV192 Cutoff Speed for BRAKING						4-9	[9]
CV193 REAR ENGINE Volume	 						0-64	[50]
CV194 Minimum Speed to Slip 						0-32	[3]
CV200-255 RESERVED FOR OEM USE
(C) General Notes
Project sounds are amplified to the limit. When lower power is supplied while on
a test bench using SoundLoader louder sounds will exceed the speaker output range (see formula). 
Running at full power on the main track in real use, sound levels may be increase significantly.
This project was tested at Master Volume CV58=11. Horn Volume was tested at CV142>50. 
Prime mover Volume was tested at CV140>50. However,
Default Master Volume CV58=9 THIS SHOULD BE 7 OR LESS FOR TESTING WITH SOUNDLOADER - JohnM 3/23/17

Set CV60 to 0 for this project.
The SDXH167 16bit Series 4+ SFX decoders allow downloading of 8,12 and 16 bit wave files and up to 4 
voices defined in the SDF project definition embedded in SPJ file. 

For CV155=00 ["Automatic Notching"] when CV60=0 to 5 (Diesel),  the Prime mover lowest Notch setting is set by
the current throttle speed setting. F26 (ON) can increase the Notch and F27 (ON) will decrease the Prime mover
to the minimum notch set by current throttle setting.



Cam triggered exhaust sounds (e.g. steam) are only configured for 8 or 16 bit wav file downloads.
			 notes:
 The last 24mar23? dmf has the first CAM trig as A pos polarity immediately as digital speed goes from 0 to moving code. 
 This will be the first chuff in positive trig sense, and following chuffs will then modulate based on motor BEMF voltage reading, unless you make cv[129] bit1 value 1.
 This means the phase will always start at chuff 1...is that a problem? I don’t think so...
 
 You can use the normalization of gear ratio cv[134]/32 or driver diameter cv[133]/60 to adjust the "correct" chuff rate. I did a 1mph (1x5280'x12" per 3600 secs) , calculation of a 60" driver with 4 chuffs, but may not have enuf Kentucky windage....
 
 The driver is assumed as 60" and gear ratio is [0 or 32] for 100% or 1:1 gear rate.
 I can trim the values behind this.
 Once you get above about 45% throttle even if we pitch bend up (shorten) the chuffs, they merge and center-pack on both channels.