// This module assigns a memory bank to the camera and the monitor // When the camera reaches the end of a frame, it is switched to // the next frame and the next time the monitor gets to the end of // a frame, it is switched to the next frame. Since the monitor runs // faster than the camera, it will display the same bank for multiple // frames. module bankselect (clk, cameraFrame, monitorFrame, cameraBank, monitorBank) ; input cameraFrame ; // Goes high when camera has finished a frame input monitorFrame ; // Goes high when monitor has finished a frame input clk ; output [1:0] cameraBank ; // Bank address used by the camera to store a frame output [1:0] monitorBank ;// Bank address used by the monitor reg [1:0] cameraBank ; reg [1:0] monitorBank ; reg [1:0] previousCameraBank ; // Monitor uses the previous camera frame reg nextCameraBank ; // Control signal that loads cameraBank with next value reg nextMonitorBank ; // Control signal that loads monitorBank with next value // State register and state assignments reg [1:0] state, nextState; parameter cameraWait = 0, cameraDone = 1, monitorWait = 2, monitorDone = 3; // Note that the state machine has no reset - it is self starting // Note that we do not use delayed assignment when using previousCameraBank // If we change the value in the first if statement, we want to use the new // value in the second if. (Currently this never happens because nextCameraBank // and nextMonitorBank are never asserted at the same time.) always @(posedge clk) begin state <= nextState; if (nextCameraBank) begin previousCameraBank = cameraBank; // Save current camera bank cameraBank <= cameraBank + 1; // Move to next bank end if (nextMonitorBank) monitorBank = previousCameraBank; // Move to next bank end always @(state or cameraFrame or monitorFrame) begin // Default values for outputs nextMonitorBank = 0; nextCameraBank = 0; nextState = state; // Stay in the same state by default case (state) // Wait for camera to get to the end of a frame // We don't come to this state until cameraFrame is turned off cameraWait: begin if (cameraFrame) begin nextState = cameraDone; nextCameraBank = 1; // Go to the next camera bank end end // Camera is at the end of a frame cameraDone: begin if (!cameraFrame) nextState = monitorWait; else if (monitorFrame) begin nextState = monitorDone; nextMonitorBank = 1; end end // Camera has started a new frame but we are still waiting for the monitor // to finish so we can move it along monitorWait: begin if (monitorFrame) begin nextState = cameraWait; // we know cameraFrame = 0 nextMonitorBank = 1; end end // Both the camera and monitor have finished their frames and have // moved to the next bank. // We need to wait for the camera to deassert cameraFrame monitorDone: begin if (!cameraFrame) nextState = cameraWait; end endcase end endmodule