punk@13: // The MIT License punk@13: punk@13: // Copyright (c) 2009 Massachusetts Institute of Technology punk@13: punk@13: // Permission is hereby granted, free of charge, to any person obtaining a copy punk@13: // of this software and associated documentation files (the "Software"), to deal punk@13: // in the Software without restriction, including without limitation the rights punk@13: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell punk@13: // copies of the Software, and to permit persons to whom the Software is punk@13: // furnished to do so, subject to the following conditions: punk@13: punk@13: // The above copyright notice and this permission notice shall be included in punk@13: // all copies or substantial portions of the Software. punk@13: punk@13: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR punk@13: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, punk@13: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE punk@13: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER punk@13: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, punk@13: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN punk@13: // THE SOFTWARE. punk@13: punk@13: // Author: Kermin Fleming kfleming@mit.edu punk@13: punk@13: import Connectable::*; punk@13: import GetPut::*; punk@13: import ClientServer::*; punk@13: import FIFO::*; punk@15: import SpecialFIFOs::*; punk@52: import Vector::*; punk@13: punk@13: //AWB includes punk@13: `include "asim/provides/low_level_platform_interface.bsh" punk@13: `include "asim/provides/soft_connections.bsh" punk@13: `include "asim/provides/common_services.bsh" punk@13: punk@13: //Local includes punk@13: `include "asim/provides/audio_pipe_types.bsh" //provides Audio Pipeline interface punk@33: `include "asim/provides/path_types.bsh" punk@13: `include "asim/provides/core.bsh" punk@43: `include "asim/provides/mixer.bsh" punk@15: `include "asim/provides/processor_library.bsh" punk@15: `include "asim/provides/fpga_components.bsh" punk@51: `include "asim/dict/VDEV_SCRATCH.bsh" punk@33: punk@13: `include "asim/rrr/remote_client_stub_AUDIOCORERRR.bsh" punk@36: `include "asim/rrr/remote_server_stub_AUDIOCORERRR.bsh" punk@13: punk@54: `define MAX_VOICES 2 punk@54: punk@13: module [CONNECTED_MODULE] mkConnectedApplication (); punk@54: punk@54: Vector#(`MAX_VOICES, Volume) channelVols = replicate(127); punk@54: Mixer mixer <- mkMixer(`MAX_VOICES, channelVols); //should be max voices but 2 for now punk@68: punk@68: Reg#(Bit#(32)) cycle <- mkReg(0); punk@68: Reg#(Bit#(32)) sampleCountIn <-mkReg(0); punk@68: Reg#(Bit#(32)) sampleCountOut <-mkReg(0); punk@68: Vector#(`MAX_VOICES, Reg#(Bit#(32))) pc <-replicateM(mkReg(0)); //tracks pcs from cores for debug purposes punk@68: Vector#(`MAX_VOICES, Reg#(Bool)) channelFini <- replicateM(mkReg(False)); punk@33: punk@53: // Services Samples punk@53: ClientStub_AUDIOCORERRR client_stub <- mkClientStub_AUDIOCORERRR(); punk@48: punk@36: //----------------------------------------------------------- punk@36: // Debug port punk@36: punk@36: ServerStub_AUDIOCORERRR server_stub <- mkServerStub_AUDIOCORERRR(); punk@36: punk@54: // Create Cores punk@54: Vector#(`MAX_VOICES, Core) cores; punk@54: for (Integer n = 0; n < `MAX_VOICES; n = n + 1) punk@54: begin punk@54: case (n) punk@54: 0 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYA); punk@54: 1 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYB); punk@68: 2 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYC); punk@68: 3 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYD); punk@68: 4 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYE); punk@68: 5 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYF); punk@68: 6 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYG); punk@68: 7 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYH); punk@68: 8 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYI); punk@68: 9 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYJ); punk@68: 10 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYK); punk@68: 11 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYL); punk@68: 12 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYM); punk@68: 13 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYN); punk@68: 14 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYO); punk@68: 15 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYP); punk@68: 16 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYQ); punk@68: 17 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYR); punk@68: 18 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYS); punk@68: 19 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYT); punk@68: 20 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYU); punk@68: 21 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYV); punk@68: 22 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYW); punk@68: 23 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYX); punk@68: 24 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYY); punk@68: 25 : cores[n] <- mkCore(`VDEV_SCRATCH_MEMORYZ); punk@54: endcase punk@54: end punk@68: punk@68: function Integer countFiniVoices(); punk@68: Integer count = 0; punk@68: for (Integer i = 0; i < `MAX_VOICES; i = i + 1) punk@68: if (channelFini[i]) count = count + 1; punk@68: return count; punk@68: endfunction punk@68: punk@13: // this is for the tracing punk@13: rule printCycles; punk@13: cycle <= cycle+1; punk@13: $fdisplay(stderr, " => Cycle = %d", cycle); punk@13: endrule punk@13: punk@68: // get the pc for trace purposes punk@68: rule getPC; punk@68: for (Integer i = 0; i < `MAX_VOICES; i = i + 1) punk@68: begin punk@68: let val <- cores[i].pc.get(); punk@68: pc[i] <= val; punk@68: end punk@68: endrule punk@68: punk@53: // Send to Mixer punk@53: // Right now this is sorta retarded in that I pass from the output fifo into a new fifo punk@53: // But I have to mod a bunch of things to fix this and I am not sure I understand punk@53: // things well enough to do this quickly. So here it is as it is for now. punk@54: rule mix; punk@54: for (Integer i = 0; i < `MAX_VOICES; i = i + 1) punk@54: begin punk@54: let coreOut <- cores[i].sampleOutput.get(); punk@68: mixer.toMixer(AudioStream {voice : fromInteger(i), data:coreOut.data}); punk@54: end punk@68: endrule punk@68: punk@48: rule feedOutput; punk@53: let pipeOut <- mixer.mainOut.get(); punk@53: punk@13: AudioProcessorControl endOfFileTag = EndOfFile; punk@13: AudioProcessorControl sampleTag = Data; punk@13: punk@68: sampleCountOut <= sampleCountOut+1; punk@53: punk@68: $display("PIPE writes sample %d", sampleCountOut); punk@53: case (pipeOut) matches punk@52: tagged EndOfFile: punk@52: begin punk@52: client_stub.makeRequest_SendProcessedStream(zeroExtend(pack(endOfFileTag)),?); punk@68: for (Integer i = 1; i < `MAX_VOICES; i = i+1) channelFini[i] <= False; punk@68: //This will report conflict with punk@68: //the other setting of channelFini at input except they are mutually punk@68: //exclusive since this is only hit after all input finished punk@52: end punk@52: tagged Sample .sample: punk@52: client_stub.makeRequest_SendProcessedStream(zeroExtend(pack(sampleTag)), zeroExtend(pack(sample))); punk@25: endcase punk@53: endrule punk@13: punk@52: // Can generally just stick with the EOF but since I have two Cores no mixer... punk@52: /*(* conservative_implicit_conditions *) punk@52: rule sendTerminate; punk@52: Bool done = True; punk@52: for (Integer i = 1; i < 2; i = i+1) punk@52: done = ac_fini[i] && done; punk@52: punk@52: if (done) punk@52: client_stub.makeRequest_SendTerminate(zeroExtend(pack(1))); punk@52: punk@52: endrule punk@52: */ punk@52: punk@36: //***** SERVER Side ***** punk@68: // this requires me to switch the processor to handle this which I will do later. punk@68: // When a file has finished, want the system to pass invalid audiostreams punk@68: rule fillFinished; punk@68: for (Integer i = 1; i < `MAX_VOICES; i = i+1) punk@68: begin punk@68: if (channelFini[i]) punk@68: cores[i].sampleInput.put(AudioStream {voice : fromInteger(i), data: tagged Invalid}); punk@68: end punk@68: endrule punk@68: punk@52: (* conservative_implicit_conditions *) punk@36: rule feedInput; punk@36: let command <- server_stub.acceptRequest_SendUnprocessedStream(); punk@36: AudioProcessorControl ctrl = unpack(truncate(command.ctrl)); rlm@41: punk@52: VoiceId channel = unpack(truncate(command.channel)); punk@68: // $display("PIPE: Incoming sample to channel %x", channel); rlm@41: punk@52: AudioProcessorUnit inSample; punk@52: punk@52: if(ctrl == EndOfFile) punk@52: begin punk@43: $display("lsp: PIPE received EOF "); punk@68: inSample = tagged EndOfFile; punk@68: channelFini[channel] <= True; punk@36: end punk@36: else punk@36: begin punk@43: // $display("lsp: PIPE received Data "); punk@52: inSample = tagged Sample unpack(truncate(command.sample)); punk@52: end punk@52: punk@68: if (!channelFini[channel]) punk@68: cores[channel].sampleInput.put(AudioStream {voice: channel, data: tagged Valid inSample }); punk@68: else $display("PIPE ERROR: Trying to send to Stream officially done"); punk@52: punk@68: endrule punk@68: punk@68: (* conservative_implicit_conditions *) punk@68: rule handleCPUToHost; punk@68: let req <- server_stub.acceptRequest_ReadCPUToHost(); punk@68: case (req) punk@68: 1: server_stub.sendResponse_ReadCPUToHost(cycle); punk@68: 2: server_stub.sendResponse_ReadCPUToHost(sampleCountIn); punk@68: 3: server_stub.sendResponse_ReadCPUToHost(sampleCountOut); punk@68: 4: server_stub.sendResponse_ReadCPUToHost(fromInteger(countFiniVoices())); punk@68: default : if (req < 10 + `MAX_VOICES) punk@68: server_stub.sendResponse_ReadCPUToHost(pc[req-10]); punk@68: endcase punk@68: endrule punk@68: punk@13: endmodule