copyleft: gplv3 added and set repo to public
[calu.git] / cpu / src / pipeline_tb.vhd
1 --   `Deep Thought', a softcore CPU implemented on a FPGA
2 --
3 --  Copyright (C) 2010 Markus Hofstaetter <markus.manrow@gmx.at>
4 --  Copyright (C) 2010 Martin Perner <e0725782@student.tuwien.ac.at>
5 --  Copyright (C) 2010 Stefan Rebernig <stefan.rebernig@gmail.com>
6 --  Copyright (C) 2010 Manfred Schwarz <e0725898@student.tuwien.ac.at>
7 --  Copyright (C) 2010 Bernhard Urban <lewurm@gmail.com>
8 --
9 --  This program is free software: you can redistribute it and/or modify
10 --  it under the terms of the GNU General Public License as published by
11 --  the Free Software Foundation, either version 3 of the License, or
12 --  (at your option) any later version.
13 --
14 --  This program is distributed in the hope that it will be useful,
15 --  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 --  GNU General Public License for more details.
18 --
19 --  You should have received a copy of the GNU General Public License
20 --  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 library IEEE;
23 use IEEE.std_logic_1164.all;
24 use IEEE.numeric_std.all;
25
26 use work.common_pkg.all;
27 use work.core_pkg.all;
28 use work.extension_pkg.all;
29 -------------------------------------------------------------------------------
30 -- ENTITY
31 -------------------------------------------------------------------------------
32 entity pipeline_tb is
33
34 end pipeline_tb;
35
36
37 -------------------------------------------------------------------------------
38 -- ARCHITECTURE
39 -------------------------------------------------------------------------------
40 architecture behavior of pipeline_tb is
41
42         constant cc : time := 20 ns;        -- test clock period
43         constant SYS_CLOCK_FREQ : integer := 50000000;
44         constant BAUD_COUNT : integer := SYS_CLOCK_FREQ/115200;
45
46                 signal sys_clk_pin : std_logic;
47                 signal sys_res_n_pin : std_logic;
48                 --Data input
49                 
50                 signal dummy : std_logic;
51
52                 signal jump_result_pin : instruction_addr_t;
53                 signal prediction_result_pin : instruction_addr_t;
54                 signal branch_prediction_bit_pin : std_logic;
55                 signal alu_jump_bit_pin : std_logic;
56                 signal instruction_pin : instruction_word_t;
57                 signal prog_cnt : instruction_addr_t;
58
59                 signal reg_w_addr_pin : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
60                 signal reg_wr_data_pin : gp_register_t;
61                 signal reg_we_pin : std_logic;
62                 signal to_next_stage_pin : dec_op;
63
64                  signal result_pin : gp_register_t;--reg
65                  signal result_addr_pin : gp_addr_t;--reg
66                  signal addr_pin : word_t; --memaddr
67                  signal data_pin : gp_register_t; --mem data --ureg
68                  signal alu_jump_pin : std_logic;--reg
69                  signal brpr_pin  : std_logic;  --reg
70                  signal wr_en_pin : std_logic;--regop --reg
71                  signal dmem_pin  : std_logic;--memop
72                  signal dmem_wr_en_pin : std_logic;
73                  signal hword_pin  : std_logic;
74                  signal byte_s_pin, tx_pin, rx_pin : std_logic;
75                                  
76                                   signal gpm_in_pin : extmod_rec;
77                                  signal gpm_out_pin : gp_register_t;
78                  signal nop_pin : std_logic;
79
80                  signal cycle_cnt : integer;
81
82                 signal sseg0, sseg1, sseg2, sseg3 : std_logic_vector(0 to 6);
83                 signal int_req_pin : interrupt_t;
84
85                 signal new_im_data :std_logic;
86                 signal im_addr, im_data : gp_register_t;
87
88 begin
89
90 --              instruction_ram : r_w_ram
91 --              generic map (
92 --                      PHYS_INSTR_ADDR_WIDTH,
93 --                      WORD_WIDTH
94 --              )
95 --              
96 --              port map (
97 --                      sys_clk,
98 --                      instr_w_addr(PHYS_INSTR_ADDR_WIDTH-1 downto 0),
99 --                      instr_r_addr_nxt(PHYS_INSTR_ADDR_WIDTH-1 downto 0),
100 --                      instr_we,
101 --                      instr_wr_data,
102 --                      instr_rd_data
103 --              );
104
105         fetch_st : fetch_stage
106                 generic map (
107         
108                         '0',
109                         '1'
110                 )
111                 
112                 port map (
113                 --System inputs
114                         clk => sys_clk_pin, --: in std_logic;
115                         reset => sys_res_n_pin, --: in std_logic;
116                         s_reset => '1',
117                 --Data inputs
118                         jump_result => jump_result_pin, --: in instruction_addr_t;
119                         prediction_result => prediction_result_pin, --: in instruction_addr_t;
120                         branch_prediction_bit => branch_prediction_bit_pin,  --: in std_logic;
121                         alu_jump_bit => alu_jump_bit_pin, --: in std_logic;
122                         new_im_data_in => new_im_data,
123                         im_addr => im_addr,
124                         im_data => im_data,
125
126                 --Data outputs
127                         instruction => instruction_pin, --: out instruction_word_t
128                         prog_cnt => prog_cnt,
129                         int_req => int_req_pin
130                 );      
131
132         decode_st : decode_stage
133                 generic map (
134                         -- active reset value
135                         '0',
136                         -- active logic value
137                         '1'
138                         
139                         )
140                 port map (
141                 --System inputs
142                         clk => sys_clk_pin, --: in std_logic;
143                         reset => sys_res_n_pin, -- : in std_logic;
144
145                 --Data inputs
146                         instruction => instruction_pin, --: in instruction_word_t;
147                         prog_cnt => prog_cnt,
148                         reg_w_addr => reg_w_addr_pin, --: in std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
149                         reg_wr_data => reg_wr_data_pin, --: in gp_register_t;
150                         reg_we => reg_we_pin, --: in std_logic;
151                         nop => nop_pin,
152
153                 --Data outputs
154                         branch_prediction_res => prediction_result_pin, --: instruction_word_t;
155                         branch_prediction_bit => branch_prediction_bit_pin, --: std_logic
156                         to_next_stage => to_next_stage_pin
157                         
158                 );
159           exec_st : execute_stage
160                 generic map('0')
161                 port map(sys_clk_pin, sys_res_n_pin,to_next_stage_pin,reg_wr_data_pin, reg_we_pin, reg_w_addr_pin, gpm_in_pin, result_pin, result_addr_pin,addr_pin,
162                 data_pin, alu_jump_pin,brpr_pin, wr_en_pin, dmem_pin,dmem_wr_en_pin,hword_pin,byte_s_pin, gpm_out_pin);
163
164           writeback_st : writeback_stage
165                 generic map('0', '1', "altera",50)
166                 port map(sys_clk_pin, sys_res_n_pin, result_pin, result_addr_pin, addr_pin, data_pin, alu_jump_pin, brpr_pin, 
167                 wr_en_pin, dmem_pin, dmem_wr_en_pin, hword_pin, byte_s_pin,
168                 reg_wr_data_pin, reg_we_pin, reg_w_addr_pin, jump_result_pin, alu_jump_bit_pin, tx_pin, rx_pin, new_im_data, im_addr, im_data, sseg0, sseg1, sseg2, sseg3, int_req_pin);
169
170
171
172
173         nop_pin <= (alu_jump_bit_pin);-- xor brpr_pin);
174
175 -------------------------------------------------------------------------------
176 -- generate simulation clock
177 -------------------------------------------------------------------------------
178   CLKGEN : process
179   begin
180     sys_clk_pin <= '1';
181     wait for cc/2;
182     sys_clk_pin <= '0';
183     wait for cc/2;
184   end process CLKGEN;
185   
186
187   cnt : process(sys_clk_pin, sys_res_n_pin)
188
189   begin
190
191         if (sys_res_n_pin = '0') then
192                 cycle_cnt <= 0;
193         elsif (sys_clk_pin'event and sys_clk_pin = '1') then
194                 cycle_cnt <= cycle_cnt + 1;
195         end if;
196
197   end process cnt;
198 -------------------------------------------------------------------------------
199 -- test the design
200 -------------------------------------------------------------------------------
201   TEST_IT : process
202
203     -- wait for n clock cycles
204     procedure icwait(cycles : natural) is
205     begin
206       for i in 1 to cycles loop
207         wait until sys_clk_pin = '1' and sys_clk_pin'event;
208       end loop;
209     end;
210
211         procedure txd(trans_data : in std_logic_vector) is
212         begin
213                 for i in 0 to 9 loop
214                         rx_pin <= trans_data(i);
215                         report "bit: " & std_logic'image(trans_data(i));
216                         dummy <= not dummy;
217                         wait on dummy;
218                         -- icwait(BAUD_COUNT);
219                         icwait(50);
220                 end loop;
221         end txd;
222
223         
224   begin
225     -----------------------------------------------------------------------------
226     -- initial reset
227     -----------------------------------------------------------------------------
228         sys_res_n_pin <= '0';
229         rx_pin <= '1';
230 --      reg_w_addr_pin <= (others => '0');
231 --      reg_wr_data_pin <= (others => '0');
232 --      reg_we_pin <= '0';
233
234         icwait(10);
235         dummy <= '1';
236         sys_res_n_pin <= '1';
237         wait until sys_res_n_pin = '1';
238         
239         icwait(10);
240
241         txd("0000100101");
242         icwait(600);
243         icwait(600);
244
245         txd("0000100101");
246         icwait(600000000);
247
248     ---------------------------------------------------------------------------
249     -- exit testbench
250     ---------------------------------------------------------------------------
251     assert false
252       report "Test finished"
253       severity error;
254
255   end process test_it;
256
257 end behavior;
258
259
260 -------------------------------------------------------------------------------
261 -- configuration
262 -------------------------------------------------------------------------------
263 configuration pipeline_conf_beh of pipeline_tb is
264   for behavior
265     for fetch_st : fetch_stage use entity work.fetch_stage(behav);
266     end for;
267     for decode_st : decode_stage use entity work.decode_stage(behav);
268     end for;
269     for exec_st : execute_stage use entity work.execute_stage(behav);
270     end for;
271     for writeback_st : writeback_stage use entity work.writeback_stage(behav);
272     end for;
273
274   end for;
275 end pipeline_conf_beh;