| 1 |
-- |
| 2 |
-- |
| 3 |
-- !!! ======================== [ WARNING ] ======================== !!! |
| 4 |
-- |
| 5 |
-- QUICK-AND-DIRTY TESTBENCH ... SCHEDULED FOR CLEAN UP |
| 6 |
-- |
| 7 |
-- !!! ======================== [ WARNING ] ======================== !!! |
| 8 |
-- |
| 9 |
-- |
| 10 |
|
| 11 |
|
| 12 |
library ieee; |
| 13 |
use ieee.std_logic_1164.all; |
| 14 |
use ieee.numeric_std.all; |
| 15 |
|
| 16 |
library std; |
| 17 |
use std.env.all; |
| 18 |
|
| 19 |
entity nios_model is |
| 20 |
generic( |
| 21 |
ADDR_WIDTH : positive := 8; -- Number of address bits in an operation |
| 22 |
DATA_WIDTH : positive := 8 -- Number of data bits in an operation |
| 23 |
); |
| 24 |
port( |
| 25 |
-- Avalon-MM Interface |
| 26 |
clock : out std_logic; |
| 27 |
reset : out std_logic; |
| 28 |
rd_req : out std_logic; |
| 29 |
wr_req : out std_logic; |
| 30 |
addr : out std_logic_vector(ADDR_WIDTH-1 downto 0); |
| 31 |
wr_data : out std_logic_vector(DATA_WIDTH-1 downto 0); |
| 32 |
rd_data : in std_logic_vector(DATA_WIDTH-1 downto 0); |
| 33 |
rd_data_val : in std_logic; |
| 34 |
mem_hold : in std_logic |
| 35 |
); |
| 36 |
end entity; |
| 37 |
|
| 38 |
architecture arch of nios_model is |
| 39 |
|
| 40 |
function half_clk_per( freq : natural ) return time is |
| 41 |
begin |
| 42 |
return ( (0.5 sec) / real(freq) ); |
| 43 |
end function; |
| 44 |
|
| 45 |
constant SYS_CLK_PER : time := half_clk_per( 80e6 ); |
| 46 |
constant SPI_CLK_DIV : positive := 2; |
| 47 |
|
| 48 |
type command_t is ( READ, WRITE ); |
| 49 |
|
| 50 |
type operation_t is record |
| 51 |
cmd : command_t; |
| 52 |
addr : std_logic_vector(ADDR_WIDTH-1 downto 0); |
| 53 |
data : std_logic_vector(DATA_WIDTH-1 downto 0); |
| 54 |
end record; |
| 55 |
|
| 56 |
type operations_t is array(natural range<>) of operation_t; |
| 57 |
|
| 58 |
constant operations : operations_t := ( |
| 59 |
(cmd => READ, addr => 8x"55", data => (others => '1') ), |
| 60 |
(cmd => READ, addr => 8x"7C", data => (others => '1') ), |
| 61 |
(cmd => WRITE, addr => 8x"27", data => 8x"42" ), |
| 62 |
(cmd => READ, addr => 8x"27", data => 8x"42" ), |
| 63 |
(cmd => WRITE, addr => 8x"90", data => 8x"10" ), |
| 64 |
(cmd => WRITE, addr => 8x"91", data => 8x"11" ), |
| 65 |
(cmd => WRITE, addr => 8x"92", data => 8x"12" ), |
| 66 |
(cmd => WRITE, addr => 8x"93", data => 8x"13" ), |
| 67 |
(cmd => WRITE, addr => 8x"A0", data => 8x"20" ), |
| 68 |
(cmd => WRITE, addr => 8x"A1", data => 8x"21" ), |
| 69 |
(cmd => WRITE, addr => 8x"A2", data => 8x"22" ), |
| 70 |
(cmd => WRITE, addr => 8x"A3", data => 8x"23" ), |
| 71 |
(cmd => READ, addr => 8x"10", data => 8x"10" ), |
| 72 |
(cmd => READ, addr => 8x"11", data => 8x"11" ), |
| 73 |
(cmd => READ, addr => 8x"12", data => 8x"12" ), |
| 74 |
(cmd => READ, addr => 8x"13", data => 8x"13" ), |
| 75 |
(cmd => READ, addr => 8x"20", data => 8x"20" ), |
| 76 |
(cmd => READ, addr => 8x"21", data => 8x"21" ), |
| 77 |
(cmd => READ, addr => 8x"22", data => 8x"22" ), |
| 78 |
(cmd => READ, addr => 8x"23", data => 8x"23" ) |
| 79 |
); |
| 80 |
|
| 81 |
type state_t is ( IDLE, RUN, CHECK_READ, NEXT_CMD ); |
| 82 |
|
| 83 |
signal state : state_t := IDLE; |
| 84 |
|
| 85 |
signal sys_clk : std_logic := '1'; |
| 86 |
signal sys_rst : std_logic := '1'; |
| 87 |
|
| 88 |
begin |
| 89 |
|
| 90 |
sys_clk <= not sys_clk after SYS_CLK_PER; |
| 91 |
sys_rst <= '1', '0' after SYS_CLK_PER*5; |
| 92 |
|
| 93 |
clock <= sys_clk; |
| 94 |
reset <= sys_rst; |
| 95 |
|
| 96 |
stim_proc : process( sys_clk, sys_rst ) |
| 97 |
variable op : natural range operations'range := operations'low; |
| 98 |
variable clock_count : natural := 0; |
| 99 |
begin |
| 100 |
if( sys_rst = '1' ) then |
| 101 |
op := operations'low; |
| 102 |
clock_count := 0; |
| 103 |
state <= RUN; |
| 104 |
rd_req <= '0'; |
| 105 |
wr_req <= '0'; |
| 106 |
addr <= (others => '0'); |
| 107 |
wr_data <= (others => '0'); |
| 108 |
elsif( rising_edge(sys_clk) ) then |
| 109 |
|
| 110 |
clock_count := clock_count + 1; |
| 111 |
|
| 112 |
rd_req <= '0'; |
| 113 |
wr_req <= '0'; |
| 114 |
addr <= (others => '0'); |
| 115 |
wr_data <= (others => '0'); |
| 116 |
|
| 117 |
case( state ) is |
| 118 |
|
| 119 |
when IDLE => |
| 120 |
|
| 121 |
if( mem_hold = '0' and clock_count >= operations'length*SPI_CLK_DIV*(ADDR_WIDTH+DATA_WIDTH)+10 ) then |
| 122 |
stop(0); |
| 123 |
end if; |
| 124 |
|
| 125 |
when RUN => |
| 126 |
|
| 127 |
clock_count := 0; |
| 128 |
|
| 129 |
if( mem_hold = '0' ) then |
| 130 |
case (operations(op).cmd) is |
| 131 |
when READ => |
| 132 |
rd_req <= '1'; |
| 133 |
state <= CHECK_READ; |
| 134 |
when WRITE => |
| 135 |
wr_req <= '1'; |
| 136 |
state <= NEXT_CMD; |
| 137 |
end case; |
| 138 |
|
| 139 |
addr <= operations(op).addr; |
| 140 |
wr_data <= operations(op).data; |
| 141 |
|
| 142 |
end if; |
| 143 |
|
| 144 |
when CHECK_READ => |
| 145 |
|
| 146 |
if( rd_data_val = '1' ) then |
| 147 |
assert (operations(op).data = rd_data) report "Read data does not match expected!" severity failure; |
| 148 |
state <= NEXT_CMD; |
| 149 |
end if; |
| 150 |
|
| 151 |
when NEXT_CMD => |
| 152 |
|
| 153 |
if( mem_hold = '0' and clock_count >= 64 ) then |
| 154 |
if( op = operations'high ) then |
| 155 |
state <= IDLE; |
| 156 |
else |
| 157 |
op := op + 1; |
| 158 |
state <= RUN; |
| 159 |
end if; |
| 160 |
end if; |
| 161 |
|
| 162 |
end case; |
| 163 |
|
| 164 |
end if; |
| 165 |
|
| 166 |
end process; |
| 167 |
|
| 168 |
end architecture; |