From d9e78106fb6e9fb60aec20deeadfb702f3f3ef9c Mon Sep 17 00:00:00 2001 From: Tobias Weber Date: Mon, 17 Dec 2018 16:18:21 +0100 Subject: [PATCH] more detailed test of pixel data decoding. --- mupix/Mupix8/tb/GrayCode.C | 21 ++++++ mupix/Mupix8/tb/GrayDecoderTest.vhd | 87 ++++++++++++++++++++++ mupix/Mupix8/tb/PixelAddress.C | 44 +++++++++++ mupix/Mupix8/tb/PixelAddressDecodeTest.vhd | 74 ++++++++++-------- 4 files changed, 197 insertions(+), 29 deletions(-) create mode 100644 mupix/Mupix8/tb/GrayCode.C create mode 100644 mupix/Mupix8/tb/GrayDecoderTest.vhd create mode 100644 mupix/Mupix8/tb/PixelAddress.C diff --git a/mupix/Mupix8/tb/GrayCode.C b/mupix/Mupix8/tb/GrayCode.C new file mode 100644 index 0000000..d8228a9 --- /dev/null +++ b/mupix/Mupix8/tb/GrayCode.C @@ -0,0 +1,21 @@ +#include + +template +inline T graycode_decode(T gray) { + T mask; + for (mask = gray >> 1; mask != 0; mask = mask >> 1) { + gray = gray ^ mask; + } + return gray; +} + +int main() { + + std::fstream outfile("graytest.dat", std::ios::out); + if (outfile.good()) { + for (unsigned i = 0; i < 1024; ++i) { + outfile << i << " " << graycode_decode(i) << "\n"; + } + } + return 0; +} diff --git a/mupix/Mupix8/tb/GrayDecoderTest.vhd b/mupix/Mupix8/tb/GrayDecoderTest.vhd new file mode 100644 index 0000000..9c593ba --- /dev/null +++ b/mupix/Mupix8/tb/GrayDecoderTest.vhd @@ -0,0 +1,87 @@ +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +use std.TextIO.all; + +entity GrayDecoderTest is +end entity; + +architecture sim of GrayDecoderTest is + + constant clk_period : time := 10 ns; + constant nbits : integer := 10; + + signal clk : std_logic := '0'; + signal reset : std_logic := '1'; + signal bypass : std_logic := '0'; + signal gray_in : std_logic_vector(nbits - 1 downto 0) := (others => '0'); + signal bin_out : std_logic_vector(nbits - 1 downto 0); + + component gray_to_binary is + generic(NBITS : integer := 10); + port ( + clk : in std_logic; -- clk input + reset : in std_logic; -- reset input + bypass : in std_logic; -- bypass logic + gray_in : in std_logic_vector (NBITS - 1 downto 0); -- gray counter input + bin_out : out std_logic_vector (NBITS - 1 downto 0) -- binary counter output + ); + end component gray_to_binary; + + signal file_ptr : integer := 0; + signal tbin : std_logic_vector(nbits - 1 downto 0); + + procedure Read_Test_File( + filename : in string; + file_ptr : in integer; + signal tgray : out std_logic_vector(nbits - 1 downto 0); + signal tbin : out std_logic_vector(nbits - 1 downto 0)) is + file data_file : text open read_mode is filename; + variable L : line; + variable tmp1, tmp2 : integer; + begin + report "reading test file"; + for i in 0 to file_ptr loop + readline(data_file, L); + if i = file_ptr then + read(L, tmp1); + read(L, tmp2); + tgray <= std_logic_vector(to_unsigned(tmp1, nbits)); + tbin <= std_logic_vector(to_unsigned(tmp2, nbits)); + end if; + end loop; + end Read_Test_File; + + +begin + + dut : entity work.gray_to_binary + generic map(NBITS => nbits) + port map(clk => clk, + reset => reset, + bypass => bypass, + gray_in => gray_in, + bin_out => bin_out); + + clk_sim : process + begin + clk <= '1'; + wait for clk_period/2; + clk <= '0'; + wait for clk_period/2; + end process clk_sim; + + stimulus : process + begin + wait for 100 ns; + reset <= '0'; + for i in 0 to 2**nbits - 1 loop + Read_Test_File("/home/tobias/PANDA/MuPix/trbsoft/trb3/mupix/Mupix8/tb/graytest.dat", i, gray_in, tbin); + wait for clk_period; + assert bin_out = tbin report "error in gray decoding"; + end loop; + wait; + end process; + +end architecture; diff --git a/mupix/Mupix8/tb/PixelAddress.C b/mupix/Mupix8/tb/PixelAddress.C new file mode 100644 index 0000000..fa5f550 --- /dev/null +++ b/mupix/Mupix8/tb/PixelAddress.C @@ -0,0 +1,44 @@ +#include + +using namespace std; + +struct ColRow { + int column; + int row; +}; + +ColRow ConvertChipAddress(const ColRow& chipAddress) { + ColRow physicalAddress; + physicalAddress.column = chipAddress.column - 128; + if (chipAddress.row >= 240) { + physicalAddress.row = 99 - (255 - chipAddress.row); + } else if (chipAddress.row < 240 && chipAddress.row >= 140) { + physicalAddress.row = chipAddress.row - 40; + } else if (chipAddress.row < 140 && chipAddress.row >= 56) { + physicalAddress.row = chipAddress.row - 56; + } else { + physicalAddress.row = chipAddress.row + 199; + } + return physicalAddress; +} + + +int main(int argc, char* argv[]) { + + ColRow in, out; + std::fstream outfile("pixeldecodetest.dat", std::ios::out); + if (outfile.good()) { + for (unsigned c = 128; c < 256; ++c) { + for (unsigned r = 0; r < 200; ++r) { + in.column = c; + in.row = r; + out = ConvertChipAddress(in); + outfile << in.column << " " << in.row << " " << out.column << " " + << out.row << "\n"; + } + } + } + outfile.close(); + + return 0; +} diff --git a/mupix/Mupix8/tb/PixelAddressDecodeTest.vhd b/mupix/Mupix8/tb/PixelAddressDecodeTest.vhd index 4aab783..eedf065 100644 --- a/mupix/Mupix8/tb/PixelAddressDecodeTest.vhd +++ b/mupix/Mupix8/tb/PixelAddressDecodeTest.vhd @@ -2,6 +2,8 @@ library IEEE; use IEEE.std_logic_1164.all; use IEEE.numeric_std.all; +use std.TextIO.all; + entity PixelAddressDecodeTest is end entity PixelAddressDecodeTest; @@ -37,6 +39,36 @@ architecture sim of PixelAddressDecodeTest is signal col_out : std_logic_vector(column_width - 1 downto 0); signal row_out : std_logic_vector(row_width - 1 downto 0); + signal tcol_out : std_logic_vector(column_width - 1 downto 0); + signal trow_out : std_logic_vector(row_width - 1 downto 0); + + procedure Read_Test_File( + filename : in string; + file_ptr : in integer; + signal tcol_in : out std_logic_vector(column_width - 1 downto 0); + signal trow_in : out std_logic_vector(row_width - 1 downto 0); + signal tcol_out : out std_logic_vector(column_width - 1 downto 0); + signal trow_out : out std_logic_vector(row_width - 1 downto 0)) is + file data_file : text open read_mode is filename; + variable L : line; + variable tmp1, tmp2, tmp3, tmp4 : integer; + begin + report "reading test file"; + for i in 0 to file_ptr loop + readline(data_file, L); + if i = file_ptr then + read(L, tmp1); + read(L, tmp2); + read(L, tmp3); + read(L, tmp4); + tcol_in <= std_logic_vector(to_unsigned(tmp1, column_width)); + trow_in <= std_logic_vector(to_unsigned(tmp2, row_width)); + tcol_out <= std_logic_vector(to_unsigned(tmp3, column_width)); + trow_out <= std_logic_vector(to_unsigned(tmp4, row_width)); + end if; + end loop; + end Read_Test_File; + begin PixelAddressDecode_1 : entity work.PixelAddressDecode @@ -65,37 +97,21 @@ begin stimulus : process is begin wait for 100 ns; - -- test bypass - bypass <= '1'; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(245, row_width)); - wait for clk_period; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(180, row_width)); - wait for clk_period; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(88, row_width)); - wait for clk_period; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(30, row_width)); - wait for clk_period; -- test decoding bypass <= '0'; - ena <= '1'; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(245, row_width)); - wait for clk_period; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(180, row_width)); - wait for clk_period; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(88, row_width)); - wait for clk_period; - col_in <= std_logic_vector(to_unsigned(140, column_width)); - row_in <= std_logic_vector(to_unsigned(30, row_width)); - wait for clk_period; - ena <= '0'; - + for i in 0 to 128*200 - 1 loop + Read_Test_File("/home/tobias/PANDA/MuPix/trbsoft/trb3/mupix/Mupix8/tb/pixeldecodetest.dat", + i, col_in, row_in, tcol_out, trow_out); + wait for clk_period; + ena <= '1'; + if valid_o = '0' then + wait until valid_o = '1'; + ena <= '0'; + assert (col_out = tcol_out and row_out = trow_out) report "error in pixel decoding" severity warning; + wait for clk_period; + end if; + end loop; + wait; end process stimulus; end architecture; -- 2.43.0