assert_spec x86

if {[have_include "power_on/qemu"]} {
	puts "\nAudio_in test running on Qemu is not supported.\n"
	exit 0
}

if {[have_spec linux]} {
	puts"\nAudio_in test running on Linux is not supported.\n"
	exit 0
}

# select use of 'Audio_in/Audio_out' or 'Record/Play' sessions
proc use_record_play_sessions { } { return 1 }

proc build_targets { } {

	set targets {
		core init timer
		driver/acpi driver/platform app/pci_decode server/report_rom
		driver/audio
	}

	if {[use_record_play_sessions]} {
		lappend targets server/record_play_mixer
	} else {
		lappend targets test/audio_in
	}
}

build [build_targets]

create_boot_directory

proc audio_driver_config_attr { } {

	if {[use_record_play_sessions]} {
		return {report_mixer="yes" record_play="yes"}
	} else {
		return {report_mixer="yes"}
	}
}

proc record_play_start_nodes { } {

	if {![use_record_play_sessions]} { return "" }

	return {
	<start name="mixer">
		<resource name="RAM" quantum="2M"/>
		<resource name="CPU" quantum="20"/>
		<binary name="record_play_mixer"/>
		<provides> <service name="Record"/> <service name="Play"/> </provides>
		<config jitter_ms="10">

			<mix name="left">  <play label_suffix="left" /> </mix>
			<mix name="right"> <play label_suffix="right"/> </mix>

			<policy label_suffix="left"   record="left"  volume="1.0"/>
			<policy label_suffix="right"  record="right" volume="1.0"/>
		</config>
	</start>
	}
}

proc audio_in_out_start_nodes { } {

	if {[use_record_play_sessions]} { return "" }

	return {
	<start name="test-audio_in">
		<resource name="RAM" quantum="8M"/>
	</start>
	}
}

install_config {
<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>
	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Timer"/></provides>
	</start>

	<start name="report_rom">
		<resource name="RAM" quantum="2M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config>
			<policy label="pci_decode -> system" report="acpi -> acpi"/>
			<policy label="platform -> devices"  report="pci_decode -> devices"/>
		</config>
	</start>

	<start name="acpi" caps="350">
		<resource name="RAM" quantum="4M"/>
		<route>
			<service name="Report"> <child name="report_rom"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>

	<start name="pci_decode" caps="350">
		<resource name="RAM" quantum="2M"/>
		<route>
			<service name="Report">             <child name="report_rom"/> </service>
			<service name="ROM" label="system"> <child name="report_rom"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>

	<start name="platform" caps="100" managing_system="yes">
		<resource name="RAM" quantum="1M"/>
		<provides>
			<service name="Platform"/>
		</provides>
		<route>
			<service name="ROM" label="devices"> <child name="report_rom"/> </service>
			<service name="Timer">   <child name="timer"/> </service>
			<any-service> <parent/> </any-service>
		</route>
		<config>
			<policy label="audio -> "> <pci class="AUDIO"/> <pci class="HDAUDIO"/> </policy>
		</config>
	</start>

	<start name="audio">
		<binary name="pci_audio"/>
		<resource name="RAM" quantum="2M"/>
		<provides>
			<service name="Audio_out"/>
			<service name="Audio_in"/>
		</provides>
		<config } [audio_driver_config_attr] {>
			<mixer field="outputs.master" value="128"/>
			<mixer field="record.adc-0:1_source" value="sel2"/>
			<mixer field="record.adc-0:1" value="128"/>
			<mixer field="record.enable" value="on"/>
		</config>
	</start>

	} [record_play_start_nodes] {
	} [audio_in_out_start_nodes] {

</config>}

build_boot_image [build_artifacts]

run_genode_until forever
