OpenCores
URL https://opencores.org/ocsvn/tinycpu/tinycpu/trunk

Subversion Repositories tinycpu

[/] [tinycpu/] [trunk/] [testbench/] [core_tb.vhd] - Blame information for rev 33

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 20 earlz
LIBRARY ieee;
2
USE ieee.std_logic_1164.ALL;
3
USE ieee.numeric_std.ALL;
4
use work.tinycpu.all;
5
 
6
ENTITY core_tb IS
7
END core_tb;
8
 
9
ARCHITECTURE behavior OF core_tb IS
10
 
11
-- Component Declaration for the Unit Under Test (UUT)
12
 
13
  component core is
14
    port(
15
      --memory interface 
16
      MemAddr: out std_logic_vector(15 downto 0); --memory address (in bytes)
17
      MemWW: out std_logic; --memory writeword
18
      MemWE: out std_logic; --memory writeenable
19
      MemIn: in std_logic_vector(15 downto 0);
20
      MemOut: out std_logic_vector(15 downto 0);
21
      --general interface
22
      Clock: in std_logic;
23
      Reset: in std_logic; --When this is high, CPU will reset within 1 clock cycles. 
24
      --Enable: in std_logic; --When this is high, the CPU executes as normal, when low the CPU stops at the next clock cycle(maintaining all state)
25
      Hold: in std_logic; --when high, CPU pauses execution and places Memory interfaces into high impendance state so the memory can be used by other components
26
      HoldAck: out std_logic; --when high, CPU acknowledged hold and buses are in high Z
27
      --todo: port interface
28
 
29
      --debug ports:
30
      DebugIR: out std_logic_vector(15 downto 0); --current instruction
31
      DebugIP: out std_logic_vector(7 downto 0); --current IP
32
      DebugCS: out std_logic_vector(7 downto 0); --current code segment
33
      DebugTR: out std_logic; --current value of TR
34
      DebugR0: out std_logic_vector(7 downto 0)
35
    );
36
  end component;
37
 
38
 
39
  --memory interface 
40
  signal MemAddr: std_logic_vector(15 downto 0); --memory address (in bytes)
41
  signal MemWW: std_logic; --memory writeword
42
  signal MemWE: std_logic; --memory writeenable
43
  signal MemOut: std_logic_vector(15 downto 0);
44 21 earlz
  signal MemIn: std_logic_vector(15 downto 0):=x"0000";
45 20 earlz
  --general interface
46 21 earlz
  signal Reset: std_logic:='0'; --When this is high, CPU will reset within 1 clock cycles. 
47 20 earlz
  --Enable: in std_logic; --When this is high, the CPU executes as normal, when low the CPU stops at the next clock cycle(maintaining all state)
48 21 earlz
  signal Hold: std_logic:='0'; --when high, CPU pauses execution and places Memory interfaces into high impendance state so the memory can be used by other components
49 20 earlz
  signal HoldAck: std_logic; --when high, CPU acknowledged hold and buses are in high Z
50
  --todo: port interface
51
 
52
  --debug ports:
53
  signal DebugIR: std_logic_vector(15 downto 0); --current instruction
54
  signal DebugIP: std_logic_vector(7 downto 0); --current IP
55
  signal DebugCS: std_logic_vector(7 downto 0); --current code segment
56
  signal DebugTR: std_logic; --current value of TR
57
  signal DebugR0: std_logic_vector(7 downto 0);
58
 
59
  signal Clock: std_logic;
60
  constant clock_period : time := 10 ns;
61
 
62
BEGIN
63
 
64
  -- Instantiate the Unit Under Test (UUT)
65
  uut: core PORT MAP (
66
    MemAddr => MemAddr,
67
    MemWW => MemWW,
68
    MemWE => MemWE,
69
    MemOut => MemOut,
70
    MemIn => MemIn,
71
    --general interface
72
    Clock => Clock,
73
    Reset => Reset,
74
    --Enable: in std_logic; --When this is high, the CPU executes as normal, when low the CPU stops at the next clock cycle(maintaining all state)
75
    Hold => Hold,
76
    HoldAck => HoldAck,
77
    DebugIR => DebugIR,
78
    DebugIP => DebugIP,
79
    DebugCS => DebugCS,
80
    DebugTR => DebugTR,
81
    DebugR0 => DebugR0
82
  );
83
 
84
 
85
  -- Clock process definitions
86
  clock_process :process
87
  begin
88
    Clock <= '0';
89
    wait for clock_period/2;
90
    Clock <= '1';
91
    wait for clock_period/2;
92
  end process;
93
 
94
 
95
  -- Stimulus process
96
  stim_proc: process
97
    variable err_cnt: integer :=0;
98
  begin
99
    Reset <= '1';
100
    wait for 20 ns;
101
 
102
    --state tests:
103
    Hold <= '1';
104
    wait for 10 ns;
105
    assert(HoldAck = '1') report "hold state is not acknowledged" severity error;
106
    --assert(MemAddr = "ZZZZZZZZZZZZZZZZ" and MemWW="Z" and MemWE="Z" and MemOut = "ZZZZZZZZZZZZZZZZZZZZ")
107
    --  report "hold state does not set high-Z" severity error;
108
    Hold <= '0';
109
    wait for 10 ns;
110
    assert(HoldAck = '0') report "hold state lasts longer than it should" severity error;
111
 
112
    Reset <= '0';
113
    MemIn <= x"0012"; --mov r0, 0xFF
114 24 earlz
    wait for 20 ns; --fetcher needs two clock cycles to catch up
115 20 earlz
    assert(MemAddr = x"0100") report "Not fetching from correct start address" severity error;
116
    MemIn <= x"00F1"; --mov r0, 0xF1
117
    wait for 10 ns;
118
    assert(MemAddr = x"0102") report "fetcher is not incrementing address" severity error;
119
    assert(DebugIR = x"00F1" and DebugR0 /= x"12") report "IR is not correct. Execution occurs during first fetch";
120
    MemIn <= x"0056";
121
    wait for 10 ns;
122 28 earlz
    assert(DebugR0 = x"56") report "loaded value of R0 is not correct" severity error;
123 24 earlz
    MemIn <= x"0E50"; --mov IP, 0x50
124
    wait for 10 ns;
125
    assert( MemAddr = x"0150") report "mov to IP doesn't work" severity error; --DebugIP uses regOut, so it won't be updated until next clock cycle actually, but it's correct.
126
    MemIn <= x"0020"; --mov r0, 0x20
127 20 earlz
    wait for 10 ns;
128 28 earlz
    assert (MemAddr = x"0152" and DebugIP=x"52") report "fetching is wrong after move to IP" severity error; --DebugIP uses regOut, Fetchaddress uses regIn, so this is correct
129 25 earlz
    MemIn <= x"0160"; --mov r0,0x60 if TR is set
130 24 earlz
    wait for 10 ns; --wait until register write happens
131
    assert(DebugR0 = x"20") report "mov to r0 is wrong after move to IP" severity error;
132 25 earlz
    MemIn <= x"1050"; --mov [r0], 0x50 (r0 is 0x20)
133
    wait for 10 ns;
134 26 earlz
    MemIn <= x"0025"; --mov r0,0x25
135 25 earlz
    assert(DebugR0 = x"20" and DebugTR='0') report "moved to r0 conditional thought TR is 0" severity error;
136
    assert(MemAddr = x"0020" and MemWE='1' and MemWW='0' and MemOut=x"0050") report "Write to memory doesn't work" severity error;
137 27 earlz
    wait for 20 ns; --wait an extra cycle because of WaitForMemory state
138
    MemIn <= x"0235"; --mov r1,0x35
139 25 earlz
    wait for 10 ns;
140 27 earlz
    MemIn <= "0011000000010000"; --compare greater than r0, r1 : TR=r0 > r1
141
    wait for 10 ns;
142
    assert(DebugTR ='0') report "ALU compare is not correct for greater than" severity error;
143
    MemIn <= "0011000000010010"; --TR=r0 < r1
144 29 earlz
    wait for 10 ns;
145 31 earlz
    MemIn <= x"0F20"; --jmp to 0x20 if TR=1
146 29 earlz
    assert(DebugTR='1') report "ALU compare is not correct for less than" severity error;
147 31 earlz
    wait for 10 ns;
148
    assert(DebugIP=x"20") report "conditional TR is not correct after ALU compare" severity error;
149 29 earlz
 
150
    --now test bitwise
151 30 earlz
    MemIn <= x"0E50"; --mov IP, 0x50 -- do this just so we can count IP easily
152
    wait for 10 ns;
153 29 earlz
    MemIn <= x"00F0"; --mov r0, 0xFO
154
    wait for 10 ns;
155
    MemIn <= x"0218"; --mov r1, 0x18
156
    wait for 10 ns;
157
    MemIn <= "0100000000010001"; --or r0, r1 (r0 = r0 or r1)
158
    wait for 10 ns;
159 30 earlz
    wait for 10 ns; --wait for settling
160 29 earlz
    assert(DebugR0 = x"F8") report "ALU OR is not correct" severity error;
161 30 earlz
    assert( MemAddr=x"0156") report "Fetching is wrong after WaitForAlu" severity error;
162
    MemIn <= x"0070"; --mov r0, 0x70 -- for debugging
163
    wait for 10 ns;
164
    assert( MemAddr=x"0158") report "IP increment is wrong after WaitForAlu" severity error;
165 32 earlz
    MemIn <= "0101000000000000"; --push r0
166
    wait for 10 ns;
167
    assert(MemAddr=x"0200" and MemOut=x"0070") report "push is not correct" severity error;
168
    wait for 10 ns;
169 33 earlz
    MemIn <= "0101000001100001"; --mov r0, SP
170
    wait for 10 ns;
171
    assert(Debugr0 = x"02") report "SP is not correct" severity error;
172 32 earlz
 
173
    MemIn <= "0101000000010000"; --pop r0 
174 33 earlz
    assert(MemAddr=x"015C") report "IP increment is wrong after push" severity error;
175 32 earlz
    wait for 10 ns;
176
    MemIn <= x"0020"; --the value to be popped into r0
177
    assert(MemAddr=x"0200") report "Pop is not fetching from correct address" severity error;
178
    wait for 10 ns;
179
    assert(DebugR0=x"20") report "Pop is not assigning to R0 correct" severity error;
180
    MemIn <= x"0040";
181
    wait for 10 ns;
182 29 earlz
 
183 20 earlz
    -- summary of testbench
184
    assert false
185
    report "Testbench of core completed successfully!"
186
    severity note;
187
 
188
    wait;
189
 
190
    -- insert stimulus here 
191
 
192
    wait;
193
  end process;
194
 
195
 
196
END;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.