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

Subversion Repositories wishbone_bfm

[/] [wishbone_bfm/] [tags/] [arelease/] [rtl/] [io_package.vhd] - Blame information for rev 15

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 amulcock
-------------------------------------------------------------------------------
2
----                                                                       ----
3 10 amulcock
---- WISHBONE Wishbone_BFM IP Core                                         ----
4 2 amulcock
----                                                                       ----
5 10 amulcock
---- This file is part of the Wishbone_BFM project                         ----
6
---- http://www.opencores.org/cores/Wishbone_BFM/                          ----
7 2 amulcock
----                                                                       ----
8
---- Description                                                           ----
9 10 amulcock
---- Implementation of Wishbone_BFM IP core according to                   ----
10
---- Wishbone_BFM IP core specification document.                          ----
11 2 amulcock
----                                                                       ----
12
---- To Do:                                                                ----
13
----    NA                                                                 ----
14
----                                                                       ----
15
---- Author(s):                                                            ----
16
----   Andrew Mulcock, amulcock@opencores.org                              ----
17
----                                                                       ----
18
-------------------------------------------------------------------------------
19
----                                                                       ----
20
---- Copyright (C) 2008 Authors and OPENCORES.ORG                          ----
21
----                                                                       ----
22
---- This source file may be used and distributed without                  ----
23
---- restriction provided that this copyright statement is not             ----
24
---- removed from the file and that any derivative work contains           ----
25
---- the original copyright notice and the associated disclaimer.          ----
26
----                                                                       ----
27
---- This source file is free software; you can redistribute it            ----
28
---- and/or modify it under the terms of the GNU Lesser General            ----
29
---- Public License as published by the Free Software Foundation           ----
30
---- either version 2.1 of the License, or (at your option) any            ----
31
---- later version.                                                        ----
32
----                                                                       ----
33
---- This source is distributed in the hope that it will be                ----
34
---- useful, but WITHOUT ANY WARRANTY; without even the implied            ----
35
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR               ----
36
---- PURPOSE. See the GNU Lesser General Public License for more           ----
37
---- details.                                                              ----
38
----                                                                       ----
39
---- You should have received a copy of the GNU Lesser General             ----
40
---- Public License along with this source; if not, download it            ----
41
---- from http://www.opencores.org/lgpl.shtml                              ----
42
----                                                                       ----
43
-------------------------------------------------------------------------------
44
----                                                                       ----
45
-- CVS Revision History                                                    ----
46
----                                                                       ----
47
-- $Log: not supported by cvs2svn $                                                                   ----
48
----                                                                       ----
49
 
50
 
51
--      Package File Template
52
--
53
--      Purpose: This package defines supplemental types, subtypes, 
54
--               constants, and functions 
55
 
56
 
57
library IEEE;
58
use IEEE.STD_LOGIC_1164.all;
59
 
60
-- -------------------------------------------------------------------------
61
package io_pack is
62
-- -------------------------------------------------------------------------
63
 
64
constant write32_time_out   : integer := 6;    -- number of clocks to wait 
65
                                                -- on w32, before an error
66
constant read32_time_out    : integer := 6;    -- number of clocks to wait 
67
                                                -- on r32, before an error
68
 
69
constant clk_period         : time := 10 ns;    -- period of simulation clock
70
 
71 12 amulcock
constant max_block_size     : integer := 128;   -- maximum number of read or write 
72
                                                -- locations in a block transfer
73
 
74 2 amulcock
type cycle_type is (    unknown,
75
                        bus_rst,
76
                        bus_idle,
77 12 amulcock
                        rd32,  rd16,  rd8,  -- read 
78
                        wr32,  wr16,  wr8,  -- write
79
                        rmw32, rmw16, rmw8, -- read modify write
80
                        bkr32, bkr16, brw8, -- block read
81
                        bkw32, bkw16, bkw8  -- block write
82 2 amulcock
                    );
83
 
84
type bus_cycle is
85
  record
86
     c_type     : cycle_type;
87
     add_o      : std_logic_vector( 31 downto 0);
88
     dat_o      : std_logic_vector( 31 downto 0);
89
     dat_i      : std_logic_vector( 31 downto 0);
90
     we         : std_logic;
91
     stb        : std_logic;
92
     cyc        : std_logic;
93
     ack        : std_logic;
94
     err        : std_logic;
95
     rty        : std_logic;
96
     lock       : std_logic;
97
     sel        : std_logic_vector( 3 downto 0);
98
     clk        : std_logic;
99
  end record;
100
 
101
 
102
 
103
-- define the wishbone bus signal to share 
104
--  with main procedure
105
-- Need to define it as the weekest possible ( 'Z' ) 
106
--  not so that we get a tri state bus, but so that 
107
--  procedures called can over drive the signal in the test bench.
108
--  else test bench gets 'U's.
109
--
110
signal bus_c    : bus_cycle :=
111
            (   unknown,
112
                (others => 'Z'),
113
                (others => 'Z'),
114
                (others => 'Z'),
115
                'Z',
116
                'Z',
117
                'Z',
118
                'Z',
119
                'Z',
120
                'Z',
121
                'Z',
122
                (others => 'Z'),
123
                'Z'
124
            );
125
 
126 12 amulcock
type block_type is array ( max_block_size downto 0 ) of std_logic_vector( 31 downto 0 );
127
 
128
 
129 2 amulcock
-- ----------------------------------------------------------------------
130 8 amulcock
--  to_nibble
131
-- ----------------------------------------------------------------------
132
-- usage to_nibble( slv  ); -- convert 4 bit slv to a character
133
function to_nibble( s:std_logic_vector(3 downto 0)) return character;
134
 
135
 
136
-- ----------------------------------------------------------------------
137
--  to_hex
138
-- ----------------------------------------------------------------------
139
-- usage to_hex( slv  ); -- convert a slv to a string
140
function to_hex( v:std_logic_vector) return string;
141
 
142
 
143
 
144
 
145
 
146
 
147
 
148
 
149
-- ----------------------------------------------------------------------
150 2 amulcock
--  clock_wait
151
-- ----------------------------------------------------------------------
152
-- usage clock_wait( number of cycles, bus_record ); -- wait n number of clock cycles
153
procedure clock_wait(
154
            constant    no_of_clocks  : in    integer;
155
            signal      bus_c         : inout bus_cycle
156
                    );
157
 
158
 
159
-- ----------------------------------------------------------------------
160
--  wb_init
161
-- ----------------------------------------------------------------------
162
-- usage wb_init( bus_record ); -- Initalises the wishbone bus
163
procedure wb_init(
164
            signal   bus_c          : inout bus_cycle
165
                );
166
 
167
 
168
-- ----------------------------------------------------------------------
169
--  wb_rst
170
-- ----------------------------------------------------------------------
171
-- usage wb_rst( 10, RST_sys, bus_record ); -- reset system for 10 clocks
172
procedure wb_rst (
173
            constant no_of_clocks   : in integer;
174
            signal   reset          : out std_logic;
175
            signal   bus_c          : inout bus_cycle
176
                );
177
 
178
 
179
 
180
-- ----------------------------------------------------------------------
181
--  wr_32
182
-- ----------------------------------------------------------------------
183
-- usage wr_32 ( address, data , bus_record )-- write 32 bit data to a 32 bit address
184
procedure wr_32 (
185
            constant address_data   : in std_logic_vector( 31 downto 0);
186
            constant write_data     : in std_logic_vector( 31 downto 0);
187
            signal   bus_c          : inout bus_cycle
188
                );
189
 
190
-- ----------------------------------------------------------------------
191
--  rd_32
192
-- ----------------------------------------------------------------------
193
-- usage rd_32 ( address, data , bus_record )-- read 32 bit data from a 32 bit address
194
procedure rd_32 (
195
            constant address_data   : in std_logic_vector( 31 downto 0);
196 8 amulcock
            variable read_data      : out std_logic_vector( 31 downto 0);
197 2 amulcock
            signal   bus_c          : inout bus_cycle
198
                );
199
 
200
 
201 10 amulcock
-- ----------------------------------------------------------------------
202
--  rmw_32
203
-- ----------------------------------------------------------------------
204
-- usage rmw_32 ( address, read_data, write_data , bus_record )-- read 32 bit data from a 32 bit address
205
--                                                                then write new 32 bit data to that address
206
 
207
procedure rmw_32 (
208
            constant address_data   : in std_logic_vector( 31 downto 0);
209
            variable read_data      : out std_logic_vector( 31 downto 0);
210
            constant write_data     : in std_logic_vector( 31 downto 0);
211
            signal   bus_c          : inout bus_cycle
212
                );
213
 
214
 
215 12 amulcock
-- ----------------------------------------------------------------------
216
--  bkw_32
217
-- ----------------------------------------------------------------------
218
-- usage bkw_32 ( address_array, write_data_array, array_size , bus_record )
219
-- write each data to the coresponding address of the array
220 10 amulcock
 
221 12 amulcock
procedure bkw_32 (
222
            constant address_data   : in block_type;
223
            constant write_data     : in block_type;
224
            constant array_size     : in integer;
225
            signal   bus_c          : inout bus_cycle
226
                );
227 10 amulcock
 
228 12 amulcock
-- ----------------------------------------------------------------------
229
--  bkr_32
230
-- ----------------------------------------------------------------------
231
-- usage bkr_32 ( address_array, read_data_array, array_size , bus_record )
232
-- read from each address data to the coresponding address of the array
233 10 amulcock
 
234 12 amulcock
procedure bkr_32 (
235
            constant address_data   : in block_type;
236
            variable read_data      : out block_type;
237
            constant array_size     : in integer;
238
            signal   bus_c          : inout bus_cycle
239
                ) ;
240
 
241
 
242
 
243 2 amulcock
-- -------------------------------------------------------------------------
244
end io_pack;
245
-- -------------------------------------------------------------------------
246
 
247
 
248
 
249
 
250
 
251
-- -------------------------------------------------------------------------
252
-- -------------------------------------------------------------------------
253
-- -------------------------------------------------------------------------
254
package body io_pack is
255
-- -------------------------------------------------------------------------
256
 
257 8 amulcock
-- ----------------------------------------------------------------------
258
--  to_nibble
259
-- ----------------------------------------------------------------------
260
-- usage to_nibble( slv  ); -- convert 4 bit slv to a character
261
function to_nibble( s:std_logic_vector(3 downto 0)) return character is
262
begin
263
        case s is
264
                when "0000" => return '0';
265
                when "0001" => return '1';
266
                when "0010" => return '2';
267
                when "0011" => return '3';
268
                when "0100" => return '4';
269
                when "0101" => return '5';
270
                when "0110" => return '6';
271
                when "0111" => return '7';
272
                when "1000" => return '8';
273
                when "1001" => return '9';
274
                when "1010" => return 'A';
275
                when "1011" => return 'B';
276
                when "1100" => return 'C';
277
                when "1101" => return 'D';
278
                when "1110" => return 'E';
279
                when "1111" => return 'F';
280
                when others=> return '?';
281
        end case;
282
end function to_nibble;
283 2 amulcock
 
284 8 amulcock
 
285 2 amulcock
-- ----------------------------------------------------------------------
286 8 amulcock
--  to_hex
287
-- ----------------------------------------------------------------------
288
-- usage to_hex( slv  ); -- convert a slv to a string
289
function to_hex( v:std_logic_vector) return string is
290
        constant c:std_logic_vector(v'length+3 downto 1) := "000" & to_x01(v);
291
begin
292
        if v'length < 1 then return ""; end if;
293
        return to_hex(c(v'length downto 5)) & to_nibble(c(4 downto 1));
294
end function to_hex;
295
 
296
 
297
 
298
-- ----------------------------------------------------------------------
299 2 amulcock
--  clock_wait
300
-- ----------------------------------------------------------------------
301
-- usage clock_wait( number of cycles, bus_record ); -- wait n number of clock cycles
302
procedure clock_wait(
303
            constant    no_of_clocks  : in    integer;
304
            signal      bus_c         : inout bus_cycle
305
                    ) is
306
begin
307
 
308
    for n in 1 to no_of_clocks loop
309
        wait until rising_edge( bus_c.clk );
310
    end loop;
311
 
312
end procedure clock_wait;
313
 
314
 
315
 
316
-- --------------------------------------------------------------------
317
-- usage wb_init( bus_record ); -- Initalises the wishbone bus
318
procedure wb_init(
319
            signal   bus_c          : inout bus_cycle
320
                ) is
321
begin
322
 
323
     bus_c.c_type <= bus_idle;
324
     bus_c.add_o <= ( others => '0');
325
     bus_c.dat_o <= ( others => '0');
326
     bus_c.we   <= '0';
327
     bus_c.stb  <= '0';
328
     bus_c.cyc  <= '0';
329
     bus_c.lock <= '0';
330
 
331
     wait until rising_edge( bus_c.clk );   -- allign to next clock
332
 
333
end procedure wb_init;
334
 
335
 
336
-- --------------------------------------------------------------------
337
-- usage wb_rst( 10, RST_sys, bus_record ); -- reset system for 10 clocks
338
procedure wb_rst (
339
            constant no_of_clocks   : in integer;
340
            signal   reset          : out std_logic;
341
            signal   bus_c          : inout bus_cycle
342
                ) is
343
begin
344
     bus_c.c_type <= bus_rst;
345
     bus_c.stb  <= '0';
346
     bus_c.cyc  <= '0';
347
 
348
     reset <= '1';
349
        for n in 1 to no_of_clocks loop
350
            wait until falling_edge( bus_c.clk );
351
        end loop;
352
     reset <= '0';
353
            wait until rising_edge( bus_c.clk);
354
end procedure wb_rst;
355
 
356
-- --------------------------------------------------------------------
357
procedure wr_32 (
358
            constant address_data  : in std_logic_vector( 31 downto 0);
359
            constant write_data    : in std_logic_vector( 31 downto 0);
360
            signal   bus_c         : inout bus_cycle
361
                ) is
362
 
363
variable  bus_write_timer : integer;
364
 
365
begin
366
 
367
    bus_c.c_type    <= wr32;
368
    bus_c.add_o     <= address_data;
369
    bus_c.dat_o     <= write_data;
370
    bus_c.we        <= '1';                 -- write cycle
371
    bus_c.sel       <= ( others => '1');    -- on all four banks
372
    bus_c.cyc       <= '1';
373
    bus_c.stb       <= '1';
374
 
375
    bus_write_timer := 0;
376
 
377
    wait until rising_edge( bus_c.clk );
378
 
379
    while bus_c.ack = '0' loop
380
        bus_write_timer := bus_write_timer + 1;
381
        wait until rising_edge( bus_c.clk );
382
 
383
        exit when bus_write_timer >= write32_time_out;
384
 
385
    end loop;
386
 
387
    bus_c.c_type    <= bus_idle;
388
    bus_c.add_o     <= ( others => '0');
389
    bus_c.dat_o     <= ( others => '0');
390
    bus_c.we        <= '0';
391
    bus_c.sel       <= ( others => '0');
392
    bus_c.cyc       <= '0';
393
    bus_c.stb       <= '0';
394
 
395
 
396
 
397
end procedure wr_32;
398
 
399
 
400
 
401
-- ----------------------------------------------------------------------
402
--  rd_32
403
-- ----------------------------------------------------------------------
404
-- usage rd_32 ( address, data , bus_record )-- read 32 bit data from a 32 bit address
405 8 amulcock
--
406
--  Note: need read data to be a variable to be passed back to calling process;
407
--   If it's a signal, it's one delta out, and in the calling process
408
--    it will have the wrong value, the one after the clock !
409
--
410
 
411 2 amulcock
procedure rd_32 (
412
            constant address_data   : in std_logic_vector( 31 downto 0);
413 8 amulcock
            variable read_data      : out std_logic_vector( 31 downto 0);
414 2 amulcock
            signal   bus_c          : inout bus_cycle
415
                ) is
416
 
417
variable  bus_read_timer : integer;
418
 
419
begin
420
 
421
    bus_c.c_type    <= rd32;
422
    bus_c.add_o     <= address_data;
423
    bus_c.we        <= '0';                 -- read cycle
424
    bus_c.sel       <= ( others => '1');    -- on all four banks
425
    bus_c.cyc       <= '1';
426
    bus_c.stb       <= '1';
427
 
428
    bus_read_timer := 0;
429
 
430
    wait until rising_edge( bus_c.clk );
431
    while bus_c.ack = '0' loop
432
        bus_read_timer := bus_read_timer + 1;
433
        wait until rising_edge( bus_c.clk );
434
 
435
        exit when bus_read_timer >= read32_time_out;
436
 
437
    end loop;
438
 
439 8 amulcock
    read_data       := bus_c.dat_i;
440 2 amulcock
    bus_c.c_type    <= bus_idle;
441
    bus_c.add_o     <= ( others => '0');
442
    bus_c.dat_o     <= ( others => '0');
443
    bus_c.we        <= '0';
444
    bus_c.sel       <= ( others => '0');
445
    bus_c.cyc       <= '0';
446
    bus_c.stb       <= '0';
447
 
448
end procedure rd_32;
449
 
450
 
451 10 amulcock
-- ----------------------------------------------------------------------
452
--  rmw_32
453
-- ----------------------------------------------------------------------
454
-- usage rmw_32 ( address, read_data, write_data , bus_record )-- read 32 bit data from a 32 bit address
455
--                                                                then write new 32 bit data to that address
456
 
457
procedure rmw_32 (
458
            constant address_data   : in std_logic_vector( 31 downto 0);
459
            variable read_data      : out std_logic_vector( 31 downto 0);
460
            constant write_data     : in std_logic_vector( 31 downto 0);
461
            signal   bus_c          : inout bus_cycle
462
                ) is
463
 
464
variable  bus_read_timer : integer;
465
variable  bus_write_timer : integer;
466
 
467
begin
468
-- first read
469
    bus_c.c_type    <= rmw32;
470
    bus_c.add_o     <= address_data;
471
    bus_c.we        <= '0';                 -- read cycle
472
    bus_c.sel       <= ( others => '1');    -- on all four banks
473
    bus_c.cyc       <= '1';
474
    bus_c.stb       <= '1';
475
 
476
    bus_read_timer := 0;
477
 
478
    wait until rising_edge( bus_c.clk );
479
    while bus_c.ack = '0' loop
480
        bus_read_timer := bus_read_timer + 1;
481
        wait until rising_edge( bus_c.clk );
482
 
483
        exit when bus_read_timer >= read32_time_out;
484
 
485
    end loop;
486
 
487
    read_data       := bus_c.dat_i;
488
 
489
-- now write
490
    bus_c.dat_o     <= write_data;
491
    bus_c.we        <= '1';                 -- write cycle
492
 
493
    bus_write_timer := 0;
494
 
495
    wait until rising_edge( bus_c.clk );
496
 
497
    while bus_c.ack = '0' loop
498
        bus_write_timer := bus_write_timer + 1;
499
        wait until rising_edge( bus_c.clk );
500
 
501
        exit when bus_write_timer >= write32_time_out;
502
 
503
    end loop;
504
 
505
    bus_c.c_type    <= bus_idle;
506
    bus_c.add_o     <= ( others => '0');
507
    bus_c.dat_o     <= ( others => '0');
508
    bus_c.we        <= '0';
509
    bus_c.sel       <= ( others => '0');
510
    bus_c.cyc       <= '0';
511
    bus_c.stb       <= '0';
512
 
513
end procedure rmw_32;
514
 
515
 
516 12 amulcock
-- ----------------------------------------------------------------------
517
--  bkw_32
518
-- ----------------------------------------------------------------------
519
-- usage bkw_32 ( address_array, write_data_array, array_size , bus_record )
520
-- write each data to the coresponding address of the array
521
 
522
procedure bkw_32 (
523
            constant address_data   : in block_type;
524
            constant write_data     : in block_type;
525
            constant array_size     : in integer;
526
            signal   bus_c          : inout bus_cycle
527
                ) is
528
variable  bus_write_timer : integer;
529
 
530
begin
531
-- for each element, perform a write 32.
532
 
533
for n in 0 to array_size - 1 loop
534
    bus_c.c_type    <= bkw32;
535
    bus_c.add_o     <= address_data(n);
536
    bus_c.dat_o     <= write_data(n);
537
    bus_c.we        <= '1';                 -- write cycle
538
    bus_c.sel       <= ( others => '1');    -- on all four banks
539
    bus_c.cyc       <= '1';
540
    bus_c.stb       <= '1';
541
 
542
    bus_write_timer := 0;
543
 
544
    wait until rising_edge( bus_c.clk );
545
 
546
        while bus_c.ack = '0' loop
547
            bus_write_timer := bus_write_timer + 1;
548
            wait until rising_edge( bus_c.clk );
549
 
550
            exit when bus_write_timer >= write32_time_out;
551
 
552
        end loop;
553
    bus_c.c_type    <= bus_idle;
554
    bus_c.add_o     <= ( others => '0');
555
    bus_c.dat_o     <= ( others => '0');
556
    bus_c.we        <= '0';
557
    bus_c.sel       <= ( others => '0');
558
    bus_c.cyc       <= '0';
559
    bus_c.stb       <= '0';
560
end loop;
561
 
562
end procedure bkw_32;
563
 
564
-- ----------------------------------------------------------------------
565
--  bkr_32
566
-- ----------------------------------------------------------------------
567
-- usage bkr_32 ( address_array, read_data_array, array_size , bus_record )
568
-- read from each address data to the coresponding address of the array
569
 
570
procedure bkr_32 (
571
            constant address_data   : in block_type;
572
            variable read_data      : out block_type;
573
            constant array_size     : in integer;
574
            signal   bus_c          : inout bus_cycle
575
                ) is
576
variable  bus_read_timer : integer;
577
 
578
begin
579
-- for each element, perform a read  32.
580
 
581
for n in 0 to array_size - 1 loop
582
    bus_c.c_type    <= bkr32;
583
    bus_c.add_o     <= address_data(n);
584
    bus_c.we        <= '0';                 -- read cycle
585
    bus_c.sel       <= ( others => '1');    -- on all four banks
586
    bus_c.cyc       <= '1';
587
    bus_c.stb       <= '1';
588
 
589
    bus_read_timer := 0;
590
 
591
    wait until rising_edge( bus_c.clk );
592
 
593
    while bus_c.ack = '0' loop
594
        bus_read_timer := bus_read_timer + 1;
595
        wait until rising_edge( bus_c.clk );
596
 
597
        exit when bus_read_timer >= read32_time_out;
598
 
599
    end loop;
600
 
601
    read_data(n)    := bus_c.dat_i;
602
    bus_c.c_type    <= bus_idle;
603
    bus_c.add_o     <= ( others => '0');
604
    bus_c.dat_o     <= ( others => '0');
605
    bus_c.we        <= '0';
606
    bus_c.sel       <= ( others => '0');
607
    bus_c.cyc       <= '0';
608
    bus_c.stb       <= '0';
609
end loop;
610
 
611
end procedure bkr_32;
612
 
613
 
614 2 amulcock
-- -------------------------------------------------------------------------
615
end io_pack;
616
-- -------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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