// This module is a test fixture for the codec for testing the codec // interface. This produces a file on the output that can be turned into // a WAV file, or checked against an input file. `timescale 1ns / 1ns module codec_tf(/*AUTOARG*/ // Inputs clk, reset, mclk, bick, lrck, sdit, pdn ); input clk, reset; // System clock input mclk, bick, lrck; // Codec clocks input sdit; // Serial data input input pdn; // Power down pin parameter FILENAME = "outfile.dat"; parameter MAXSIZE = 100000; // Enough for 2 seconds of audio parameter MCLKDIVIDER = 3; parameter BICKDIVIDER = 6; parameter LRCKDIVIDER = 64; integer file; initial begin file = $fopen(FILENAME); if (file == 0) begin $display("Cannot open output file"); $stop; end end reg [31:0] sh_data_left, sh_data_right;// Shift registers reg [15:0] data_left, data_right;// Captured data always @(posedge bick) begin if (pdn) begin if (lrck) begin sh_data_left <= (sh_data_left << 1) | sdit; end else begin sh_data_right <= (sh_data_right << 1) | sdit; end end end always @(negedge lrck) begin if (pdn) begin data_left <= sh_data_left[31:16]; end end always @(posedge lrck) begin if (pdn) begin data_right = sh_data_right[31:16]; // Note that data_right has been assigned immediately so we can write it $fdisplay(file, "%h %h", data_left, data_right); if (lrck_count > MAXSIZE) begin $stop; end end end // Count clocks for speed check integer clk_count; integer mclk_count; integer bick_count; integer lrck_count; always @(posedge clk) begin if (reset) begin clk_count <= 0; end else begin clk_count <= clk_count + 1; end end always @(posedge mclk or posedge reset) begin if (reset) begin mclk_count <= 0; end else begin mclk_count <= mclk_count + 1; end if (mclk_count < (clk_count/MCLKDIVIDER) - 2) begin $display("Error: mclk running too slow"); $stop; end else if (mclk_count > (clk_count/MCLKDIVIDER) + 2) begin $display("Error: mclk running too fast"); $stop; end end always @(posedge bick or posedge reset) begin if (reset) begin bick_count <= 0; end else begin bick_count <= bick_count + 1; end if (bick_count < (clk_count/(MCLKDIVIDER*BICKDIVIDER)) - 2) begin $display("Error: mick running too slow"); $stop; end else if (bick_count > (clk_count/(MCLKDIVIDER*BICKDIVIDER)) + 2) begin $display("Error: mick running too fast"); $stop; end end always @(posedge lrck or posedge reset) begin if (reset) begin lrck_count <= 0; end else begin lrck_count <= lrck_count + 1; end if (lrck_count < (clk_count/(MCLKDIVIDER*BICKDIVIDER*LRCKDIVIDER)) - 2) begin $display("Error: lrck running too slow"); $stop; end else if (lrck_count > (clk_count/(MCLKDIVIDER*BICKDIVIDER*LRCKDIVIDER)) + 2) begin $display("Error: lrck running too fast"); $stop; end end endmodule // codec_tf