1 |
2 |
ja_rd |
--------------------------------------------------------------------------------
|
2 |
|
|
-- light52_tb.vhdl --
|
3 |
|
|
--------------------------------------------------------------------------------
|
4 |
|
|
-- This test bench simulates the execution of some program (whose object code
|
5 |
|
|
-- is in package obj_code_pkg, in the form of a memory init constant) and logs
|
6 |
|
|
-- the execution to a text file called 'hw_sim_log.txt' (light52_tb_pkg.vhdl).
|
7 |
|
|
--
|
8 |
|
|
-- This test bench does no actual tests on the core. Instead, the simulation log
|
9 |
|
|
-- is meant to be matched against the simulation log produced by running the
|
10 |
|
|
-- same program on the software simulator B51 (also included with this project).
|
11 |
|
|
--
|
12 |
|
|
-- This will catch errors in the implementation of the CPU if the simulated
|
13 |
|
|
-- program has anough coverage -- the opcode tester is meant to cover all CPU
|
14 |
|
|
-- opcodes in many (not all) of their corner cases.
|
15 |
|
|
-- This scheme will not help in catching errors in the peripheral modules,
|
16 |
|
|
-- mainly because the current version of B51 does not simulate them.
|
17 |
|
|
--
|
18 |
|
|
--------------------------------------------------------------------------------
|
19 |
|
|
-- Copyright (C) 2012 Jose A. Ruiz
|
20 |
|
|
--
|
21 |
|
|
-- This source file may be used and distributed without
|
22 |
|
|
-- restriction provided that this copyright statement is not
|
23 |
|
|
-- removed from the file and that any derivative work contains
|
24 |
|
|
-- the original copyright notice and the associated disclaimer.
|
25 |
|
|
--
|
26 |
|
|
-- This source file is free software; you can redistribute it
|
27 |
|
|
-- and/or modify it under the terms of the GNU Lesser General
|
28 |
|
|
-- Public License as published by the Free Software Foundation;
|
29 |
|
|
-- either version 2.1 of the License, or (at your option) any
|
30 |
|
|
-- later version.
|
31 |
|
|
--
|
32 |
|
|
-- This source is distributed in the hope that it will be
|
33 |
|
|
-- useful, but WITHOUT ANY WARRANTY; without even the implied
|
34 |
|
|
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
35 |
|
|
-- PURPOSE. See the GNU Lesser General Public License for more
|
36 |
|
|
-- details.
|
37 |
|
|
--
|
38 |
|
|
-- You should have received a copy of the GNU Lesser General
|
39 |
|
|
-- Public License along with this source; if not, download it
|
40 |
|
|
-- from http://www.opencores.org/lgpl.shtml
|
41 |
|
|
--------------------------------------------------------------------------------
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
library ieee;
|
45 |
|
|
use ieee.std_logic_1164.all;
|
46 |
|
|
use ieee.std_logic_arith.all;
|
47 |
|
|
use ieee.std_logic_unsigned.all;
|
48 |
|
|
use std.textio.all;
|
49 |
|
|
|
50 |
|
|
use work.light52_pkg.all;
|
51 |
|
|
use work.obj_code_pkg.all;
|
52 |
|
|
use work.light52_tb_pkg.all;
|
53 |
|
|
use work.txt_util.all;
|
54 |
|
|
|
55 |
|
|
entity light52_tb is
|
56 |
|
|
generic (BCD : boolean := true);
|
57 |
|
|
end;
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
architecture testbench of light52_tb is
|
61 |
|
|
|
62 |
|
|
--------------------------------------------------------------------------------
|
63 |
|
|
-- Simulation parameters
|
64 |
|
|
-- FIXME these should be in parameter package
|
65 |
|
|
|
66 |
|
|
-- Simulated clock period is the same as the usual target, the DE-1 board
|
67 |
|
|
constant T : time := 20 ns; -- 50MHz
|
68 |
|
|
constant SIMULATION_LENGTH : integer := 400000;
|
69 |
|
|
|
70 |
|
|
--------------------------------------------------------------------------------
|
71 |
|
|
-- MPU interface
|
72 |
|
|
|
73 |
|
|
signal clk : std_logic := '0';
|
74 |
|
|
signal reset : std_logic := '1';
|
75 |
|
|
|
76 |
|
|
signal p0_out : std_logic_vector(7 downto 0);
|
77 |
|
|
signal p1_out : std_logic_vector(7 downto 0);
|
78 |
|
|
signal p2_in : std_logic_vector(7 downto 0);
|
79 |
|
|
signal p3_in : std_logic_vector(7 downto 0);
|
80 |
|
|
|
81 |
|
|
signal external_irq : std_logic_vector(7 downto 0);
|
82 |
|
|
|
83 |
|
|
signal txd, rxd : std_logic;
|
84 |
|
|
|
85 |
|
|
--------------------------------------------------------------------------------
|
86 |
|
|
-- Logging signals & simulation control
|
87 |
|
|
|
88 |
|
|
-- Asserted high to disable the clock and terminate the simulation.
|
89 |
|
|
signal done : std_logic := '0';
|
90 |
|
|
|
91 |
|
|
-- Log file
|
92 |
|
|
file log_file: TEXT open write_mode is "hw_sim_log.txt";
|
93 |
|
|
-- Console output log file
|
94 |
|
|
file con_file: TEXT open write_mode is "hw_sim_console_log.txt";
|
95 |
|
|
-- Info record needed by the logging fuctions
|
96 |
|
|
signal log_info : t_log_info;
|
97 |
|
|
|
98 |
|
|
begin
|
99 |
|
|
|
100 |
|
|
---- UUT instantiation ---------------------------------------------------------
|
101 |
|
|
|
102 |
|
|
uut: entity work.light52_mcu
|
103 |
|
|
generic map (
|
104 |
|
|
IMPLEMENT_BCD_INSTRUCTIONS => BCD,
|
105 |
|
|
CODE_ROM_SIZE => work.obj_code_pkg.XCODE_SIZE,
|
106 |
|
|
XDATA_RAM_SIZE => work.obj_code_pkg.XDATA_SIZE,
|
107 |
|
|
OBJ_CODE => work.obj_code_pkg.object_code
|
108 |
|
|
)
|
109 |
|
|
port map (
|
110 |
|
|
clk => clk,
|
111 |
|
|
reset => reset,
|
112 |
|
|
|
113 |
|
|
txd => txd,
|
114 |
|
|
rxd => rxd,
|
115 |
|
|
|
116 |
|
|
external_irq => external_irq,
|
117 |
|
|
|
118 |
|
|
p0_out => p0_out,
|
119 |
|
|
p1_out => p1_out,
|
120 |
|
|
p2_in => p2_in,
|
121 |
|
|
p3_in => p3_in
|
122 |
|
|
);
|
123 |
|
|
|
124 |
|
|
-- UART is looped back in the test bench.
|
125 |
|
|
rxd <= txd;
|
126 |
|
|
|
127 |
|
|
-- I/O ports are looped back and otherwise unused.
|
128 |
|
|
p2_in <= p0_out;
|
129 |
|
|
p3_in <= p1_out;
|
130 |
|
|
|
131 |
|
|
-- External IRQ inputs are tied to port P1 for test purposes
|
132 |
|
|
external_irq <= p1_out;
|
133 |
|
|
|
134 |
|
|
---- Master clock: free running clock used as main module clock ------------
|
135 |
|
|
run_master_clock:
|
136 |
|
|
process(done, clk)
|
137 |
|
|
begin
|
138 |
|
|
if done = '0' then
|
139 |
|
|
clk <= not clk after T/2;
|
140 |
|
|
end if;
|
141 |
|
|
end process run_master_clock;
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
---- Main simulation process: reset MCU and wait for fixed period ----------
|
145 |
|
|
|
146 |
|
|
drive_uut:
|
147 |
|
|
process
|
148 |
|
|
begin
|
149 |
|
|
-- Leave reset asserted for a few clock cycles...
|
150 |
|
|
reset <= '1';
|
151 |
|
|
wait for T*4;
|
152 |
|
|
reset <= '0';
|
153 |
|
|
|
154 |
|
|
-- ...and wait for the test to hit a termination condition (evaluated by
|
155 |
|
|
-- function log_cpu_activity) or to just timeout.
|
156 |
|
|
wait for T*SIMULATION_LENGTH;
|
157 |
|
|
|
158 |
|
|
-- If we arrive here, the simulation timed out (termination conditions
|
159 |
|
|
-- trigger a failed assertion).
|
160 |
|
|
-- So print a timeout message and quit.
|
161 |
|
|
print("TB timed out.");
|
162 |
|
|
done <= '1';
|
163 |
|
|
wait;
|
164 |
|
|
|
165 |
|
|
end process drive_uut;
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
-- Logging process: launch logger functions --------------------------------
|
169 |
|
|
log_execution:
|
170 |
|
|
process
|
171 |
|
|
begin
|
172 |
|
|
-- Log cpu activity until done='1'.
|
173 |
|
|
log_cpu_activity(clk, reset, done, "/uut",
|
174 |
|
|
log_info, work.obj_code_pkg.XCODE_SIZE, "log_info",
|
175 |
|
|
X"0000", log_file, con_file);
|
176 |
|
|
|
177 |
|
|
-- Flush console log file when finished.
|
178 |
|
|
log_flush_console(log_info, con_file);
|
179 |
|
|
|
180 |
|
|
wait;
|
181 |
|
|
end process log_execution;
|
182 |
|
|
|
183 |
|
|
end architecture testbench;
|