-- Rung 5 testbench: srg320's P65C816 (MiSTer SNES core) run on -- SingleStepTests initial states, its bus logged cycle by cycle. -- -- The core's registers are internal, so each case is loaded by running code: -- after a reset, a loader program in a bank-0 scratch memory (written by -- tests/p65c816.py) sets D, DBR, S, P, A, X, Y and E and ends with a JML to -- the case's PBR:PC. From the opcode fetch after that JML's, reads and -- writes go to the case's own memory and every cycle is written out: -- C case start --
-- E case end (next opcode fetch, or the -- held cycle of WAI or STP) -- X ... the loader never reached the case -- The bus pins are combinational from the core's microcode step, and it -- latches D_IN at the clock edge that ends the cycle, so each cycle is read -- at the falling edge and answered before the rising one. -- -- Stimulus, one case per three lines, decimal: -- -- (block move = 1 for MVN/MVP: the move repeats its opcode fetch, so the case -- runs exactly cycles instead of stopping at the next fetch) --
... (loader memory, bank 0: vectors, program) --
... (the case's initial RAM, 24-bit addresses) library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; use std.textio.all; entity tb_p65c816 is generic (stimulus : string := "stimulus.txt"; output : string := "bus.txt"); end entity; architecture sim of tb_p65c816 is signal clk, rst_n : std_logic := '0'; signal d_in, d_out : std_logic_vector(7 downto 0) := (others => '0'); signal a_out : std_logic_vector(23 downto 0); signal we_n, rdy_out, vpa, vda, mlb_n, vpb_n : std_logic; signal done : boolean := false; type bank_t is array (0 to 65535) of integer range 0 to 255; constant SLOTS : natural := 8192; type addrs_t is array (0 to SLOTS - 1) of integer; type bytes_t is array (0 to SLOTS - 1) of integer range 0 to 255; begin dut : entity work.P65C816 port map ( CLK => clk, RST_N => rst_n, CE => '1', RDY_IN => '1', NMI_N => '1', IRQ_N => '1', ABORT_N => '1', D_IN => d_in, D_OUT => d_out, A_OUT => a_out, WE => we_n, RDY_OUT => rdy_out, VPA => vpa, VDA => vda, MLB => mlb_n, VPB => vpb_n); clock : process begin while not done loop clk <= '0'; wait for 5 ns; clk <= '1'; wait for 5 ns; end loop; wait; end process; drive : process file input : text open read_mode is stimulus; file log : text open write_mode is output; variable l, o : line; variable id, loader_pairs, case_pairs, jml, cap, block_move, address, value, n : integer; variable loader : bank_t; variable case_addr : addrs_t; variable case_byte : bytes_t; variable used : natural; variable testing, after_jml, found : boolean; variable cycles : natural; impure function lookup(a : integer) return integer is begin for i in 0 to used - 1 loop if case_addr(i) = a then return case_byte(i); end if; end loop; return 0; end function; procedure store(a : integer; v : integer) is begin for i in 0 to used - 1 loop if case_addr(i) = a then case_byte(i) := v; return; end if; end loop; case_addr(used) := a; case_byte(used) := v; used := used + 1; end procedure; begin while not endfile(input) loop readline(input, l); read(l, id); read(l, loader_pairs); read(l, case_pairs); read(l, jml); read(l, cap); read(l, block_move); loader := (others => 0); readline(input, l); for i in 1 to loader_pairs loop read(l, address); read(l, value); loader(address) := value; end loop; used := 0; readline(input, l); for i in 1 to case_pairs loop read(l, address); read(l, value); store(address, value); end loop; -- Reset, then run the loader and the case. rst_n <= '0'; for i in 1 to 3 loop wait until falling_edge(clk); end loop; rst_n <= '1'; testing := false; after_jml := false; cycles := 0; n := 0; write(o, string'("C ")); write(o, id); writeline(log, o); loop wait until falling_edge(clk); address := to_integer(unsigned(a_out)); if vda = '1' and vpa = '1' then -- an opcode fetch if testing and block_move = 0 then exit; -- the next instruction: the case is over elsif after_jml then testing := true; elsif address = jml then after_jml := true; end if; end if; if we_n = '0' then value := to_integer(unsigned(d_out)); if testing then store(address, value); else loader(address mod 65536) := value; end if; else if testing then value := lookup(address); else value := loader(address mod 65536); end if; d_in <= std_logic_vector(to_unsigned(value, 8)); end if; if testing then write(o, address); write(o, ' '); write(o, value); write(o, ' '); write(o, std_logic'image(we_n)(2)); write(o, ' '); write(o, std_logic'image(vda)(2)); write(o, ' '); write(o, std_logic'image(vpa)(2)); write(o, ' '); write(o, std_logic'image(vpb_n)(2)); write(o, ' '); write(o, std_logic'image(mlb_n)(2)); writeline(log, o); cycles := cycles + 1; exit when cycles >= cap; -- WAI or STP has dropped the clock enable: this cycle is -- the one the chip holds, logged once. exit when rdy_out = '0'; else n := n + 1; -- loader cycles are not logged, only counted if n > 1000 then write(o, string'("X loader did not reach the case")); writeline(log, o); exit; end if; end if; end loop; write(o, string'("E ")); write(o, id); writeline(log, o); end loop; done <= true; wait; end process; end architecture;