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

Subversion Repositories sd_mmc_emulator

[/] [sd_mmc_emulator/] [trunk/] [rtl/] [mmc_test_pack.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jclaytons
--------------------------------------------------------------------------
2
--------------------------------------------------------------------------
3
-- Package containing SD Card interface modules,
4
-- and related support modules.
5
--
6
-- 15-Aug-2017 RMR Backdoor opcodes not needed, SPI debugger still at 3000040
7
-- xx-Jun-2017 RMR Changed SPI debugger to 'w 03000040' so backdoor opcode could use 3000030 
8
-- 31-Mar-2017 RMR Added SPI debug capability to 'w 03000030'
9
--
10
library IEEE;
11
use IEEE.STD_LOGIC_1164.ALL;
12
use IEEE.NUMERIC_STD.ALL;
13
 
14
library work;
15
use work.sd_card_pack.all;
16
use work.sd_host_pack.all;
17
 
18
package mmc_test_pack is
19
 
20
  component mmc_test_cmd_rx
21
  port(
22
    -- Asynchronous reset
23
    sys_rst_n   : in  std_logic;
24
    -- SD/MMC card command signals
25
    sd_clk_i    : in  std_logic;
26
    sd_cmd_i    : in  std_logic;
27
    -- Command outputs
28
    cmd_raw_o   : out unsigned(47 downto 0);
29
    -- Status and done indicator
30
    cmd_done_o  : out std_logic;
31
    crc_err_o   : out std_logic;
32
    stop_err_o  : out std_logic
33
  );
34
  end component;
35
 
36
  component mmc_tester
37
  generic (
38
    SYS_CLK_RATE        : real; -- The clock rate at which the FPGA runs
39
    SYS_LEDS            : natural; -- Number of LED outputs on the board
40
    SYS_SWITCHES        : natural; -- Number of Switch inputs on the board
41
    EXT_CSD_INIT_FILE   : string; -- Initial contents of EXT_CSD
42
    HOST_RAM_ADR_BITS   : natural; -- Determines amount of BRAM in MMC host
43
    MMC_FIFO_DEPTH      : integer;
44
    MMC_FILL_LEVEL_BITS : integer; -- Should be at least int(floor(log2(FIFO_DEPTH))+1.0)
45
    RSP_FILL_LEVEL_BITS : integer;
46
    MMC_RAM_ADR_BITS    : integer
47
  );
48
  port (
49
 
50
    -- Asynchronous reset
51
    sys_rst_n      : in  std_logic;
52
    sys_clk        : in  std_logic;
53
 
54
    -- Asynchronous serial interface
55
    cmd_i          : in  std_logic;
56
    resp_o         : out std_logic;
57
 
58
    -- Board related
59
    switch_i       : in  unsigned(SYS_SWITCHES-1 downto 0);
60
    led_o          : out unsigned(SYS_LEDS-1 downto 0);
61
 
62
    -- Interface for SD/MMC traffic logging
63
    -- via asynchronous serial transmission
64
    tlm_send_i     : in  std_logic;
65
    tlm_o          : out std_logic;
66
 
67
    -- Tester Function Enables
68
    slave_en_i     : in  std_logic; -- OR with register bit
69
    host_en_i      : in  std_logic; -- OR with register bit
70
 
71
    -- SD/MMC card signals
72
    mmc_clk_i      : in  std_logic;
73
    mmc_clk_o      : out std_logic;
74
    mmc_clk_oe_o   : out std_logic;
75
    mmc_cmd_i      : in  std_logic;
76
    mmc_cmd_o      : out std_logic;
77
    mmc_cmd_oe_o   : out std_logic;
78
    mmc_dat_i      : in  unsigned(7 downto 0);
79
    mmc_dat_o      : out unsigned(7 downto 0);
80
    mmc_dat_oe_o   : out std_logic;
81
    mmc_od_mode_o  : out std_logic; -- Open drain mode
82
    mmc_dat_siz_o  : out unsigned(1 downto 0);
83
 
84
    dbg_spi_data0_o     : out unsigned(7 downto 0);
85
    dbg_spi_data1_o     : out unsigned(7 downto 0);
86
    dbg_spi_data2_o     : out unsigned(7 downto 0);
87
    dbg_spi_data3_o     : out unsigned(7 downto 0);
88
    dbg_spi_data4_o     : out unsigned(7 downto 0);
89
    dbg_spi_data5_o     : out unsigned(7 downto 0);
90
    dbg_spi_data6_o     : out unsigned(7 downto 0);
91
    dbg_spi_data7_o     : out unsigned(7 downto 0);
92
    dbg_spi_data8_o     : out unsigned(7 downto 0);
93
    dbg_spi_data9_o     : out unsigned(7 downto 0);
94
    dbg_spi_dataA_o     : out unsigned(7 downto 0);
95
    dbg_spi_dataB_o     : out unsigned(7 downto 0);
96
    dbg_spi_dataC_o     : out unsigned(7 downto 0);
97
    dbg_spi_dataD_o     : out unsigned(7 downto 0);
98
    dbg_spi_bytes_io    : inout unsigned(3 downto 0); --bytes to send
99
    dbg_spi_start_o     : out std_logic;
100
    dbg_spi_device_o    : out unsigned(2 downto 0); --1=VGA, 2=SYN, 3=DDS, 4=ZMON
101
    dbg_spi_busy_i      : in  std_logic;
102
    dbg_enables_o       : out unsigned(15 downto 0);  --toggle various enables/wires
103
 
104
    ------ Connect MMC fifos to opcode processor ----------
105
    -- Read from MMC fifo connections
106
    opc_fif_dat_o       : out unsigned( 7 downto 0);    -- MMC opcode fifo
107
    opc_fif_ren_i       : in  std_logic;                -- mmc fifo read enable
108
    opc_fif_mt_o        : out std_logic;                -- mmc opcode fifo empty
109
    opc_rd_cnt_o        : out unsigned(MMC_FILL_LEVEL_BITS-1 downto 0); -- mmc opcode fifo fill level 
110
    opc_rd_reset_i      : in  std_logic;                -- Synchronous mmc opcode fifo reset
111
    -- Write to MMC fifo connections
112
    opc_rspf_dat_i      : in  unsigned( 7 downto 0);    -- MMC response fifo
113
    opc_rspf_we_i       : in  std_logic;                -- response fifo write line             
114
    opc_rspf_mt_o       : out std_logic;                -- response fifo empty
115
    opc_rspf_fl_o       : out std_logic;                -- response fifo full
116
    opc_rspf_reset_i    : in std_logic;                 -- Synchronous mmc response fifo reset
117
    opc_rspf_cnt_o      : out unsigned(MMC_FILL_LEVEL_BITS-1 downto 0); -- mmc response fifo fill level 
118
 
119
    -- UART debugger can show these values
120
    opc_oc_cnt_i        : in  unsigned(31 downto 0);    -- count of opcodes processed
121
    opc_status1_i       : in  unsigned(31 downto 0);    -- LS 16 bits=opc status, MS 16-bits=opc_state
122
    opc_status2_i       : in  unsigned(31 downto 0);    -- rsp_fifo_count__opc_fifo_count    
123
    opc_status3_i       : in  unsigned(31 downto 0);    -- LS 16 bits=MS 8 bits=RSP fifo level, LS 8 bits=OPC fifo level
124
    sys_status4_i       : in  unsigned(31 downto 0);    -- system frequency setting in Hertz
125
    sys_status5_i       : in  unsigned(31 downto 0);    -- MS 16 bits=SYN_STAT pin,1=PLL_LOCK, 0=not, LS 12 bits=system power, dBm x 10
126
    sys_status6_i       : in  unsigned(31 downto 0)     -- LS 16 bits: PTN_Status__PTN_Busy(running) 
127
  );
128
  end component;
129
 
130
end mmc_test_pack;
131
 
132
package body mmc_test_pack is
133
end mmc_test_pack;
134
 
135
-------------------------------------------------------------------------------
136
-- SD/MMC Testing Command Receiver
137
-------------------------------------------------------------------------------
138
--
139
-- Author: John Clayton
140
-- Update: July 29, 2016 Copied code from sd_card_cmd_rx, and modified
141
--                       the description and interface to better suit
142
--                       test needs, e.g. capturing commands and replies
143
--                       on an SD/MMC bus.
144
--
145
-- Description
146
-------------------------------------------------------------------------------
147
-- This module is meant to be part of a system that tests, or snoops on
148
-- an SD/MMC card.
149
--
150
-- This module clocks incoming serial bits from the cmd signal into a 48 bit
151
-- shift register.  It starts when a '0' (start) bit is found, and then shifts
152
-- in 47 additional bits.  The expected format of the command is:
153
--
154
-- 0 x [index] [arg] [crc] 1
155
--
156
-- Where:
157
--   x     = direction bit. '1'=host to card, '0'=reply from card.
158
--   index = 6 bits
159
--   arg   = 32 bits
160
--   crc   = 7  bits
161
--
162
-- The test command receiver checks that the last bit is a '1' (stop) bit,
163
-- in the expected position (bit 47; start bit is bit 0).
164
-- The seven bits immediately prior to the stop bit are
165
-- checked using a CRC-7 code.
166
--
167
-- If any of the checks does not pass, the associated error bits are set.
168
-- Regardless of errors, however, the received data bits are presented
169
-- at the cmd_raw_o output.  The cmd_rx_done_o output is pulsed high for
170
-- one clock cycle to notify the downstream entities that newly received
171
-- data is available.
172
--
173
-- In the interest of simplicity and in an effort to remain practical,
174
-- only 48 bit responses are captured correctly by this unit.  For R2
175
-- responses, which are 136 bits long, only the first 48 bits are presented.
176
-- Because the R2 type responses are 136 bits long, specific logic is
177
-- included to try and detect them, and prevent errors from being flagged
178
-- by their occurrence.  The first eight bits of an R2 response are
179
-- "00111111".  Unfortunately, the first eight bits of a 48 bit R3
180
-- response are also "00111111".  Fortunately, the CRC field of an R3
181
-- response is always set to "1111111".  So, the logic checks for these
182
-- conditions and behaves accordingly.  In the case of an R2 response, that
183
-- includes ignoring further activity on the command line for another 88
184
-- clocks after the initial 48 bits are received...
185
--
186
-- Note that this receiver runs entirely within the sd_clk_i clock domain.
187
-- Therefore, care must be taken when using the outputs.  A FIFO can form
188
-- a natural "clock domain boundary crossing" or the user may need to
189
-- implement other special handshaking to safely transfer signals into a
190
-- different clock domain.
191
--
192
 
193
library IEEE;
194
use IEEE.STD_LOGIC_1164.ALL;
195
use IEEE.NUMERIC_STD.ALL;
196
 
197
library work;
198
use work.ucrc_pack.all;
199
 
200
entity mmc_test_cmd_rx is
201
  port (
202
    -- Asynchronous reset
203
    sys_rst_n   : in  std_logic;
204
    -- SD/MMC card command signals
205
    sd_clk_i    : in  std_logic;
206
    sd_cmd_i    : in  std_logic;
207
    -- Command outputs
208
    cmd_raw_o   : out unsigned(47 downto 0);
209
    -- Status and done indicator
210
    cmd_done_o  : out std_logic;
211
    crc_err_o   : out std_logic;
212
    stop_err_o  : out std_logic
213
  );
214
end mmc_test_cmd_rx;
215
 
216
architecture beh of mmc_test_cmd_rx is
217
 
218
  -- CRC related
219
signal crc_clk        : std_logic;
220
signal crc_clr        : std_logic;
221
signal crc_match      : std_logic;
222
signal crc_val        : unsigned(6 downto 0);
223
  -- Related to the incoming command
224
signal sd_cmd_r1      : std_logic;
225
signal counter        : unsigned(5 downto 0);
226
signal rx_sr          : unsigned(47 downto 0);
227
signal cmd_raw_l      : unsigned(47 downto 0);
228
signal ignore_counter : unsigned(6 downto 0);
229
 
230
begin
231
 
232
--------------------------------------------
233
-- CRC generator
234
 
235
  crc0 : ucrc_ser
236
  generic map (
237
    POLYNOMIAL => "0001001",
238
    INIT_VALUE => "0000000"
239
  )
240
  port map (
241
    -- System clock and asynchronous reset
242
    sys_clk    => crc_clk,
243
    sys_rst_n  => sys_rst_n,
244
    sys_clk_en => '1',
245
 
246
    -- Input and Control
247
    clear_i    => crc_clr,
248
    data_i     => rx_sr(0),
249
    flush_i    => '0',
250
 
251
    -- Output
252
    match_o    => crc_match,
253
    crc_o      => crc_val
254
  );
255
  -- Falling edge was used for a while...
256
  -- Rising edge is now being used, per the specification.
257
  --crc_clk <= not sd_clk_i;
258
  crc_clk <= sd_clk_i;
259
  crc_clr <= '1' when counter=0 else '0';
260
 
261
 
262
--------------------------------------------
263
-- Command receiver
264
 
265
command_rx_proc : process(sys_rst_n, sd_clk_i)
266
begin
267
  if (sys_rst_n='0') then
268
    ignore_counter <= (others=>'0');
269
    counter <= (others=>'0');
270
    rx_sr <= (others=>'0');
271
    sd_cmd_r1 <= '1';
272
    cmd_raw_l <= (others=>'1');
273
    cmd_done_o  <= '0';
274
    crc_err_o   <= '0';
275
    stop_err_o  <= '0';
276
--  elsif (sd_clk_i'event and sd_clk_i='0') then -- falling edge is used, when data is stable.
277
  elsif (sd_clk_i'event and sd_clk_i='1') then -- rising edge is used, per the specification.
278
    -- Default values
279
    cmd_done_o  <= '0';
280
    crc_err_o   <= '0';
281
    stop_err_o  <= '0';
282
 
283
    -- Metastability mitigation (synchronization) flip flops
284
    sd_cmd_r1 <= sd_cmd_i;
285
    -- The shift register is the second synchronization flip flop
286
    rx_sr(0)  <= sd_cmd_r1;
287
    rx_sr(47 downto 1) <= rx_sr(46 downto 0);
288
    -- Decrement the counter when it is non-zero
289
    if (counter>0) then
290
      counter <= counter-1;
291
    end if;
292
    -- Decrement the ignore counter when it is non-zero
293
    if (ignore_counter>0) then
294
      ignore_counter <= ignore_counter-1;
295
    end if;
296
    -- Load the counter when a start bit is seen
297
    if (ignore_counter=0 and counter=0 and sd_cmd_r1='0') then
298
      counter <= to_unsigned(48,counter'length);
299
    end if;
300
    -- Store the output when the counter is expiring
301
    if (counter=1) then
302
      cmd_raw_l <= rx_sr;
303
      cmd_done_o <= '1';
304
      -- Determine if this is a long R2 type response
305
      if (rx_sr(47 downto 40)="00111111" and rx_sr(7 downto 1)/="1111111") then
306
        ignore_counter <= to_unsigned(88,ignore_counter'length);
307
        -- No errors are generated for long R2 responses
308
      else
309
        -- Perform error checks
310
        if (rx_sr(0)='0') then
311
          stop_err_o <= '1';
312
        end if;
313
        if (crc_match='0') then
314
          crc_err_o <= '1';
315
        end if;
316
      end if;
317
    end if;
318
  end if;
319
end process;
320
 
321
-- Split out fields of the command
322
cmd_raw_o   <= cmd_raw_l;
323
 
324
end beh;
325
 
326
 
327
 
328
---------------------------------------------------------------------------------------
329
--
330
-- Author: John Clayton
331
-- Date  : May 16, 2016
332
-- Update: 05/16/16 Wrote the description, began writing code
333
--
334
-- Description
335
---------------------------------------------------------------------------------------
336
-- This is a collection of modules put together which allow testing of an MMC system.
337
-- In particular, this module contains an MMC host, plus an "MMC data pipe" which
338
-- impersonates an MMC.  
339
--
340
-- In order to interact with this collection of MMC modules in hardware, an 
341
-- "async_syscon" serial terminal command interpreter is provided, having a 32-bit
342
-- address and a 32-bit data bus.  There is an automatic baud rate synchronization
343
-- unit, which synchronizes by looking for the 0x0D character, so that simply
344
-- pressing "Enter" in a serial terminal program should bring up the link at the
345
-- correct speed, anything in the range 19200 to 921600 Baud should work.
346
-- The parity setting is "None" and hardware handshaking is not supported.
347
--
348
-- Because of the different modules present in this MMC tester, it is helpful to
349
-- summarize the memory map here:
350
--
351
-- Address          Length     Function
352
-- --------------   --------   --------------------------------------------
353
-- 0x0300_0000      0x10       MMC host registers
354
-- 0x0300_0010      0x10       MMC slave registers
355
-- 0x0300_0020      0x10       MMC tester registers
356
-- 0x0400_0000      0x4000     MMC host RAM
357
-- 0x0500_0000      0x10000    MMC slave RAM
358
--
359
-- Note that the size of the MMC slave RAM can be modified by setting the
360
-- constant MMC_RAM_ADR_BITS within this module.  Similarly, the size of the
361
-- MMC host RAM can be modified by setting the constant HOST_RAM_ADR_BITS.
362
--
363
-- For a description of the MMC host registers, please refer to the
364
-- description given in the sd_controller_8bit_bram entity.
365
-- The code is contained in the "sd_host_pack.vhd" file.
366
--
367
-- For a description of the MMC slave registers, please refer to the
368
-- description given in the sd_card_emulator, which is an entity instantiated
369
-- inside the mmc_data_pipe unit.
370
-- The code is contained in the "sd_card_pack.vhd" file.
371
--
372
-- A detailed description of the local registers is given here, below.
373
--
374
-- Wouldn't it be nice to have a block diagram showing all of the modules
375
-- or cores instantiated within this one?  Yes, I believe it would be nice.
376
-- However, in lieu of a nice ASCII-art pictorial drawing, a few lines of
377
-- descriptive text will be better than nothing.  Here goes!
378
------------------------------------------------------------------------------
379
-- Brief Description of local test functions:
380
--
381
--   Within this unit, there is steering logic that selects how the SD/MMC bus
382
--   is driven and used, with regard to the tri-state output enables.  Actual
383
--   tri-state buffers are to be implemented at the top level of the FPGA
384
--   by means of the output enable signals.
385
--
386
--   The steering logic allows the SD/MMC host to communicate directly with
387
--   the sd_card_emulator and RAM/FIFOs contained in the mmc_data_pipe, or it
388
--   can also be set up to route the signals from the MMC controller host to
389
--   an external SD/MMC card.
390
--
391
--   In like manner, it can be set up so that an external SD/MMC host can talk
392
--   to the local mmc_data_pipe unit.
393
--
394
--   A third option is to disable both the local sd_controller_8bit_bram
395
--   and the local mmc_data_pipe unit from the bus.  In this configuration,
396
--   the user of this MMC tester is enabled to "snoop" on the activity and
397
--   traffic occurring between an external SD/MMC host and its attached card.
398
--   This requires, of course, some appropriate "interposer" cabling to give
399
--   the FPGA access to monitor the signals passing along the external SD/MMC
400
--   bus being monitored.
401
--
402
--   In order to keep track of commands on the SD/MMC bus for test and debug,
403
--   there is a dedicated, stand alone command receiver separate from the MMC
404
--   host and slave units.  This receiver looks for start bits on the SD/MMC
405
--   cmd line, and then captures the contents of the transfer, whether it be
406
--   from the host to the card, or a cmd response from the card to the host.
407
--   Some responses from an SD/MMC card are 136 bits long, but most responses
408
--   are 48 bits long.  In the interest of expediency, this special receiver
409
--   only captures the first 48 bits of a command transfer.  It does, however,
410
--   refrain from generating error flags for 136 bit long responses, by
411
--   checking the command index field, which is "111111b" for 136 bit long
412
--   responses.
413
--
414
--   For test and debug purposes, there are some counters, and data capture
415
--   mechanisms:
416
--
417
--     1. A counter that counts command transfer events, filtered based on
418
--        register settings.
419
--     2. A data transfer counter, which simply increments each time a data
420
--        transfer is started.  It is reset whenever a new host to card
421
--        command transfer of the selected command index type is detected.
422
--        In reality there are two such counters, a 32 bit one that is
423
--        accessible via registers, and an 8-bit one ("tlm_d_count") that is
424
--        used in the telemetry output.  The telemetry one is a relative
425
--        count; it gets cleared at every command transfer event.
426
--     3. An SD/MMC traffic reporting "telemetry sender" that emits a stream
427
--        of information meant to be used as a timestamped archive record
428
--        of SD/MMC activity.  A new record is emitted each time the command
429
--        transfer counter increments.  The telemetry records are 12 byte
430
--        bursts meant to be captured in a FIFO external to this module, or
431
--        sent out via a UART.
432
--
433
--   The format of the telemetry record burst is:
434
--      Byte Number     Contents       Purpose
435
--      -----------     --------       -----------------------------------------
436
--           0          0xEB           Synchronization character
437
--           1          frame ID       A modulo-256 incrementing value
438
--           2          tlm_d_count    Data transfers completed, modulo 256
439
--          3-5         time           time, in microseconds
440
--          6-11        cmd            48 bits of command or reply
441
--
442
------------------------------------------------------------------------------
443
-- Local Registers
444
--
445
-- 0x0300_0020  R0   LED outputs register (READ/WRITE)
446
--                   This register contains a number of bits meant for
447
--                   controlling LEDs on the test board, just for fun.  The
448
--                   number of bits implemented in this register is set by
449
--                   the SYS_LEDS generic, which should be set in the range
450
--                   [1..32].  For SYS_LEDS settings less than 32, the unused
451
--                   bits are the most significant bits, which read as '0'.
452
--
453
-- 0x0300_0021  R1   Switch inputs register (READ ONLY)
454
--                   This register allows reading the status of the switch_i
455
--                   inputs on the test board.  The number of bits implemented
456
--                   in this register is set by the SYS_SWITCHES generic,
457
--                   which should be set in the range [1..32].  For
458
--                   SYS_SWITCHES settings less than 32, the unused bits are
459
--                   the most significant bits, which read as '0'.
460
--
461
-- 0x0300_0022  R2   MMC Tester Mode (READ/WRITE)
462
--                   Bit [0] = Slave Enable Reg.  Setting this bit causes the
463
--                             internal MMC_data_pipe slave to be active
464
--                             on the SD/MMC bus.
465
--                   Bit [1] = Host Enable Reg.  Setting this bit causes the
466
--                             internal sd_controller_8bit_bram unit to
467
--                             be active on the SD/MMC bus.
468
--                   Bits [3..2] = "00" (Reserved)
469
--                   Bit [4] = Slave Enabled (READ ONLY).  This bit is the
470
--                             logical OR of bit[0] and the slave_en_i input.
471
--                             Thus we see that the MMC_data_pipe slave can
472
--                             be made active by the signal input, or the
473
--                             register bit.
474
--                   Bit [5] = Host Enabled (READ ONLY).  This bit is the
475
--                             logical OR of bit[1] and the host_en_i input.
476
--                             Thus we see that the sd_controller_8bit_bram
477
--                             host unit can be made active by the signal
478
--                             input, or the register bit.
479
--                   Bits [7..6] = "00" (Reserved)
480
--                   Bit [8] = Telemetry log register read access enable.
481
--                             When this bit is set, then telemetry log
482
--                             FIFO contents can be read out via register
483
--                             R10, and the asynchronous serial transmitter
484
--                             is disabled.  When clear, the asynchronous
485
--                             serial transmitter has access to the telemetry
486
--                             log FIFO instead of register R10.
487
--
488
-- 0x0300_0023  R3   Test Command Receiver Filter (READ/WRITE)
489
--                   Bits [7:0] determine what type of command creates a
490
--                   "command event."
491
--                   Except for the setting of 0x80, setting bit [7] means
492
--                   "capture and count everything," including all command
493
--                   indices, in both directions.
494
--                   Example settings:
495
--                     0x81..0xFF => Capture and count everything.
496
--                     0x80 => Capture and count nothing. Nada. Zilch.
497
--                     0x7F => Capture and count all host-to-card commands
498
--                     0x3F => Capture and count all card-to-host responses
499
--                     0x59 => Capture and count CMD25 from host-to-card only
500
--                     0x28 => Capture and count CMD40 R5 responses only
501
--                     0x27 => Capture and count CMD39 R4 responses only
502
--                     0x19 => Capture and count CMD25 R1 responses only
503
--                     0x00 => Capture and count CMD0 R1 responses only
504
--
505
--   To explain more clearly how these filter settings work, it is helpful to
506
--   recall that the first eight bits of the command or response are:
507
--
508
--     [0][d][cccccc]
509
--
510
--     Where: 0 = start bit (always low)
511
--            d = direction bit (1 for host to card, 0 for card response)
512
--            cccccc = command index, ranging from 0 to 56, (or 63 for
513
--                     long responses and/or 48-bit R3 OCR responses.)
514
--
515
--     Therefore, it becomes clear that the operation of the filter is
516
--     not perfect.  For example, there is no way to act on only R3
517
--     responses.  However, there is really no need for a perfect
518
--     filter.  This one is, well, you know, good enough!
519
--
520
-- 0x0300_0024  R4   Error-free Command Events (READ/WRITE)
521
--                   Writing to this register resets it back to zero.
522
--
523
-- 0x0300_0025  R5   Command Events with CRC error (READ/WRITE)
524
--                   Writing to this register resets it back to zero.
525
--
526
-- 0x0300_0026  R6   Command Events with stop bit error (READ/WRITE)
527
--                   Writing to this register resets it back to zero.
528
--
529
-- 0x0300_0027  R7   Number of data transfers completed(READ/WRITE)
530
--                   No means are provided for the tester to determine
531
--                   what direction the data is flowing over the SD/MMC
532
--                   data lines.  Therefore, this register simply contains
533
--                   the total number of transfers started in both
534
--                   directions.  Writing to this register clears the count
535
--                   back to zero.  Note that this 32-bit counter is distinct
536
--                   from the eight bit "tlm_d_count" data transfer counter
537
--                   in the telemetry, which is a relative count, cleared at
538
--                   every command transfer event.
539
--
540
-- 0x0300_0028  R8   reg_dbus_size (READ/WRITE)
541
--                     0 => 1 bit data bus transfers
542
--                     1 => 4 bit data bus transfers
543
--                     2 => 8 bit data bus transfers
544
--                   In order for the tester to determine when a data transfer
545
--                   is completed, so that it can count valid start bits, it
546
--                   is required to know the data bus size.  Logic is present
547
--                   to decode when a host-to-card command of index 6 (CMD6),
548
--                   the SWITCH command, is seen with an argument 0x03B70s00,
549
--                   where s contains the new data bus size.  This 2-bit
550
--                   setting is then stored in the reg_dbus_size register,
551
--                   and used to set the "dstart_wait" parameter used for
552
--                   ignoring data bus activity during an active data
553
--                   transfer.  The reg_dbus_size starts out at "00b", and
554
--                   is expected to change to "01b" for 4-bit SD activity,
555
--                   and to "10b" for 8-bit MMC activity.
556
--                   Writing to this register simply clears it back to zero.
557
--
558
-- 0x0300_0029  R9   SD/MMC traffic log FIFO fill level (READ/WRITE)
559
--                   This register returns the number of bytes stored in the
560
--                   FIFO, awaiting commission to the log archive or telemetry
561
--                   link.  Writing to this register clears out the FIFO.
562
--
563
-- 0x0300_002A  R10  SD/MMC traffic log FIFO read port (READ ONLY)
564
--                   Telemetry bytes can be read out of the traffic log FIFO
565
--                   whenever enabled in R2 bit 8.  For convenience in reading
566
--                   the data, the bytes are packed into 32-bit words.  If R2
567
--                   bit 8 is clear, then reading this register returns
568
--                   0x55555555.
569
--
570
-- 0x0300_002C  R12  S4 Enables/lines
571
--
572
-- 0x0300_002D  R13  MMC_data_pipe write data FIFO fill level (READ/WRITE)
573
--                   Reading this register returns the number of bytes in
574
--                   the MMC slave write data FIFO.  The FIFO holds data
575
--                   that are meant to flow from the MMC slave to the host.
576
--                   Writing to this register clears the MMC slave
577
--                   write data FIFO.
578
--
579
-- 0x0300_002E  R14  MMC_data_pipe read data FIFO fill level (READ/WRITE)
580
--                   Reading this register returns the number of bytes in
581
--                   the MMC slave read data FIFO.  The FIFO holds data
582
--                   that have been sent from the host to the MMC slave.
583
--                   Writing to this register clears the MMC slave
584
--                   read data FIFO.
585
--
586
-- 0x0300_002F  R15  MMC_data_pipe FIFO data (READ/WRITE)
587
--                   Writing to this address loads another byte into
588
--                   the MMC slave write data FIFO, thereby enqueueing
589
--                   it to be read by the host from the MMC slave.
590
--                   However, if the write data FIFO is full, then nothing
591
--                   happens, and the byte is thrown into the "bit bucket."
592
--                   Reading from this address removes another byte
593
--                   of data from the MMC slave read data FIFO, which
594
--                   was previously delivered from the host to the
595
--                   MMC slave.
596
--                   However, if the read data FIFO is empty, then no valid
597
--                   data is actually delivered.
598
--
599
-- 0x0300_0030  R16  Rd:Opcode processor status, 8 lsbs, Wr:SPI data, device, #bytes, 14 bytes data
600
--
601
-- 0x3000_0031  R17  Opcode processor opcode counter, 32 bits
602
--
603
-- 0x3000_0032  R18  Opcode processor internal state, 8 bits
604
--
605
-- 0x3000_0033  R19  Opcode processor overall system state, 16 bits
606
--
607
-- 0x3000_0034  R20  Opcode processor overall system mode, 32 bits
608
--
609
-- 0x3000_0035  R21  Frequency processor status, 16 msbs, power processor status, 16 lsbs
610
--
611
-- 0x3000_0036  R22  Phase processor status, 16 msbs, pulse processor status, 16 lsbs
612
--
613
-- 0x3000_0037  R23  Pattern processor status, 16 msbs, opc_response_ready flag, 16 lsbs 
614
--
615
------------------------------------------------------------------------------
616
-- Further Discussion, for the enquiring minds that want to know...
617
-- The below explanations might in some way repeat what was stated above,
618
-- but could still be useful.
619
--
620
-- Register 2, bit[0] : "slave enable"
621
--   When high, this bit causes the MMC slave to receive commands, and respond to
622
--   them.  When this bit is enabled, the MMC slave will drive signals onto mmc_cmd_o
623
--   and mmc_dat_o, including driving the mmc_cmd_oe_o and mmc_dat_oe_o lines when
624
--   appropriate.  When this bit is low, the MMC slave cmd input will be held high,
625
--   and the MMC slave will not have anything to do.
626
--
627
-- Register 2, bit[1] : "host enable"
628
--   When high, this bit causes the MMC host to send commands, and receive responses
629
--   to them.  When this bit is enabled, the MMC host can drive signals onto mmc_cmd_o
630
--   and mmc_dat_o, including driving the mmc_cmd_oe_o and mmc_dat_oe_o lines when
631
--   appropriate.  When this bit is low, the MMC host cmd output will be cut off
632
--   from affecting mmc_cmd_o, and the MMC host will not be able to influence the
633
--   MMC bus signals.
634
--
635
-- If both of these bits are low, then the only function active in this core is to
636
-- monitor the mmc commands which occur on the MMC bus due to the action of any
637
-- outside MMC hosts, such as card readers, which may send commands to an MMC card
638
-- on the bus.
639
-- Register 3 can be used to filter on a certain host command index.
640
--
641
-- With "host_enable" set, the tester can be used to communicate with a real
642
-- external SD/MMC device, perhaps for the purpose of reading or writing some
643
-- sectors, or reading the device registers including the CID, CSD and the 512
644
-- byte EXT_CSD.
645
--
646
-- With "slave_enable" set, on the other hand, an MMC slave can be used with an
647
-- external SD/MMC card reader.  The MMC slave in this case uses a small amount
648
-- of RAM, plus a pair of FIFO buffers.  Writing data to the card at addresses
649
-- below the upper boundary of the RAM, simply places the data into RAM.  Beyond
650
-- the RAM upper boundary, the data goes into the write FIFO.  Reading is handled
651
-- the same way.
652
--
653
-- An alternative to using the register bits to enable the slave and host, is to
654
-- use the input signals "slave_en_i" and "host_en_i".  These signals are simply
655
-- logically ORed with the register bits.
656
--
657
-- There are no "buried tri-states" present within this module.  Therefore, each
658
-- signal, or related bus of signals, has a drive signal provided to allow the
659
-- tri-state connections to be made at the top level.  The one for the mmc_clk
660
-- had to be provided because this module can both give and receive an MMC
661
-- clock signal.
662
--
663
-- An asynchronous system controller (async_syscon) is provided here for ease
664
-- of debugging by humans.  A person can access the entire memory map of this
665
-- tester by means of simple command line style commands, using an asynchronous
666
-- serial terminal.
667
--
668
 
669
library IEEE;
670
use IEEE.STD_LOGIC_1164.ALL;
671
use IEEE.NUMERIC_STD.ALL;
672
 
673
library work;
674
use work.dds_pack.all;
675
use work.fifo_pack.all;
676
use work.convert_pack.all;
677
use work.sd_card_pack.all;
678
use work.sd_host_pack.all;
679
use work.flancter_pack.all;
680
use work.mmc_test_pack.all;
681
use work.block_ram_pack.all;
682
use work.auto_baud_pack.all;
683
use work.uart_sqclk_pack.all;
684
use work.async_syscon_pack.all;
685
 
686
  entity mmc_tester is
687
  generic (
688
    SYS_CLK_RATE        : real    := 50000000.0; -- The clock rate at which the FPGA runs
689
    SYS_LEDS            : natural := 8; -- Number of LED outputs on the board
690
    SYS_SWITCHES        : natural := 4; -- Number of Switch inputs on the board
691
    EXT_CSD_INIT_FILE   : string  := "ext_csd_init.txt"; -- Initial contents of EXT_CSD
692
    HOST_RAM_ADR_BITS   : natural := 14; -- Determines amount of BRAM in MMC host
693
    MMC_FIFO_DEPTH      : integer := 2048;
694
    MMC_FILL_LEVEL_BITS : integer := 14; -- Should be at least int(floor(log2(FIFO_DEPTH))+1.0)
695
    RSP_FILL_LEVEL_BITS : integer := 10;
696
    MMC_RAM_ADR_BITS    : integer := 14  -- 16 Kilobytes
697
  );
698
  port (
699
 
700
    -- Asynchronous reset
701
    sys_rst_n      : in  std_logic;
702
    sys_clk        : in  std_logic;
703
 
704
    -- Asynchronous serial interface
705
    cmd_i          : in  std_logic;
706
    resp_o         : out std_logic;
707
 
708
    -- Board related
709
    switch_i       : in  unsigned(SYS_SWITCHES-1 downto 0);
710
    led_o          : out unsigned(SYS_LEDS-1 downto 0);
711
 
712
    -- Interface for SD/MMC traffic logging
713
    -- via asynchronous serial transmission
714
    tlm_send_i     : in  std_logic;
715
    tlm_o          : out std_logic;
716
 
717
    -- Tester Function Enables
718
    slave_en_i     : in  std_logic; -- OR with register bit
719
    host_en_i      : in  std_logic; -- OR with register bit
720
 
721
    -- SD/MMC card signals
722
    mmc_clk_i      : in  std_logic;
723
    mmc_clk_o      : out std_logic;
724
    mmc_clk_oe_o   : out std_logic;
725
    mmc_cmd_i      : in  std_logic;
726
    mmc_cmd_o      : out std_logic;
727
    mmc_cmd_oe_o   : out std_logic;
728
    mmc_dat_i      : in  unsigned( 7 downto 0);
729
    mmc_dat_o      : out unsigned( 7 downto 0);
730
    mmc_dat_oe_o   : out std_logic;
731
    mmc_od_mode_o  : out std_logic; -- Open drain mode
732
    mmc_dat_siz_o  : out unsigned(1 downto 0);
733
 
734
    -- SPI debugging connections
735
    dbg_spi_data0_o     : out unsigned(7 downto 0);
736
    dbg_spi_data1_o     : out unsigned(7 downto 0);
737
    dbg_spi_data2_o     : out unsigned(7 downto 0);
738
    dbg_spi_data3_o     : out unsigned(7 downto 0);
739
    dbg_spi_data4_o     : out unsigned(7 downto 0);
740
    dbg_spi_data5_o     : out unsigned(7 downto 0);
741
    dbg_spi_data6_o     : out unsigned(7 downto 0);
742
    dbg_spi_data7_o     : out unsigned(7 downto 0);
743
    dbg_spi_data8_o     : out unsigned(7 downto 0);
744
    dbg_spi_data9_o     : out unsigned(7 downto 0);
745
    dbg_spi_dataA_o     : out unsigned(7 downto 0);
746
    dbg_spi_dataB_o     : out unsigned(7 downto 0);
747
    dbg_spi_dataC_o     : out unsigned(7 downto 0);
748
    dbg_spi_dataD_o     : out unsigned(7 downto 0);
749
    dbg_spi_bytes_io    : inout unsigned(3 downto 0); --bytes to send
750
    dbg_spi_start_o     : out std_logic;
751
    dbg_spi_device_o    : out unsigned(2 downto 0); --1=VGA, 2=SYN, 3=DDS, 4=ZMON
752
    dbg_spi_busy_i      : in  std_logic;            --top level is writing SPI bytes
753
    dbg_enables_o       : out unsigned(15 downto 0); --toggle various enables/wires
754
 
755
    -- connect opcode processor to mmc fifo's    
756
    -- MMC read fifo to opcode processor
757
    opc_fif_dat_o       : out unsigned( 7 downto 0);     -- MMC opcode fifo
758
    opc_fif_ren_i       : in std_logic;                  -- mmc fifo read enable
759
    opc_fif_mt_o        : out std_logic;                 -- mmc opcode fifo empty
760
    opc_rd_cnt_o        : out unsigned(MMC_FILL_LEVEL_BITS-1 downto 0); -- mmc opcode fifo fill level 
761
    opc_rd_reset_i      : in std_logic;                  -- Synchronous mmc opcode fifo reset
762
    -- MMC write fifo from opcode processor
763
    opc_rspf_dat_i      : in  unsigned( 7 downto 0);     -- MMC response fifo
764
    opc_rspf_we_i       : in std_logic;                  -- response fifo write line             
765
    opc_rspf_mt_o       : out std_logic;                 -- response fifo empty
766
    opc_rspf_fl_o       : out std_logic;                 -- response fifo full
767
    opc_rspf_reset_i    : in std_logic;                  -- Synchronous mmc response fifo reset
768
    opc_rspf_cnt_o      : out unsigned(MMC_FILL_LEVEL_BITS-1 downto 0); -- mmc response fifo fill level 
769
 
770
    -- Debugging
771
    opc_oc_cnt_i   : in  unsigned(31 downto 0);         -- LS 16 bits=count of opcodes processed, MS 16 bits=opc fifo level
772
    opc_status1_i  : in  unsigned(31 downto 0);         -- LS 16 bits=opc status, MS 16-bits=opc_state
773
    opc_status2_i  : in  unsigned(31 downto 0);         -- rsp_fifo_count__opc_fifo_count
774
    opc_status3_i  : in  unsigned(31 downto 0);         -- MS 16 bits=MS 8 bits=RSP fifo level, LS 8 bits=OPC fifo level
775
    sys_status4_i  : in  unsigned(31 downto 0);         -- system frequency setting in Hertz
776
    sys_status5_i  : in  unsigned(31 downto 0);         -- MS 16 bits=SYN_STAT pin,1=PLL_LOCK, 0=not, LS 12 bits=system power, dBm x 10
777
    sys_status6_i  : in  unsigned(31 downto 0)          -- LS 16 bits: PTN_Status__PTN_Busy(running) 
778
  );
779
  end mmc_tester;
780
 
781
architecture beh of mmc_tester is
782
 
783
  -- Constants
784
 
785
    -- async_syscon related
786
  constant FPGA_PARITY     : integer :=         0; -- 0=none, 1=even, 2=odd
787
  constant CMD_LINE_SIZE   : natural :=       128; -- Number of bytes in CMD buffer
788
  constant ADR_DIGITS      : natural :=         8;
789
  constant DAT_DIGITS      : natural :=         8;
790
  constant QTY_DIGITS      : natural :=         4;
791
  constant WDOG_VALUE      : natural :=      2000;
792
  constant DAT_SIZE        : natural := 4*DAT_DIGITS;
793
  constant ADR_SIZE        : natural := 4*ADR_DIGITS;
794
 
795
    -- Telemetry related
796
  constant TLM_FIFO_DEPTH  : natural := 16384;
797
 
798
  -- Signals
799
 
800
    -- autobaud related
801
  signal baud_clk         : std_logic;
802
  signal baud_lock        : std_logic;
803
  signal parity           : unsigned(1 downto 0);
804
 
805
    -- async_syscon related
806
  signal syscon_rst       : std_logic;
807
  signal fpga_rst_n       : std_logic;
808
  signal fpga_rst         : std_logic;
809
  signal syscon_dat_rd    : unsigned(DAT_SIZE-1 downto 0);
810
  signal syscon_dat_wr    : unsigned(DAT_SIZE-1 downto 0);
811
  signal syscon_cyc       : std_logic;
812
  signal syscon_err       : std_logic;
813
  signal syscon_ack       : std_logic;
814
  signal syscon_adr       : unsigned(ADR_SIZE-1 downto 0);
815
  signal syscon_we        : std_logic;
816
 
817
  signal master_bg        : std_logic;
818
  signal master_adr       : unsigned(ADR_SIZE-1 downto 0);
819
  signal master_cyc       : std_logic;
820
  signal master_we        : std_logic;
821
  signal master_dat_wr    : unsigned(DAT_SIZE-1 downto 0);
822
 
823
    -- related to decoding the address bus
824
  signal h_reg_sel        : std_logic;
825
  signal h_reg_ack        : std_logic;
826
  signal h_reg_dat_rd     : unsigned(31 downto 0);
827
  signal s_reg_sel        : std_logic;
828
  signal s_reg_ack        : std_logic;
829
  signal s_reg_dat_rd     : unsigned(31 downto 0);
830
  signal h_ram_sel        : std_logic;
831
  signal h_ram_ack        : std_logic;
832
  signal h_ram_dat_rd     : unsigned(7 downto 0);
833
  signal s_ram_sel        : std_logic;
834
  signal s_ram_ack        : std_logic;
835
  signal s_ram_dat_rd     : unsigned(7 downto 0);
836
  signal s_ram_we         : std_logic;
837
  signal s_fif_sel        : std_logic;
838
 
839
    -------------------------------------------------------
840
    -- mmc data pipe to opcode processor interface fifo's
841
  --signal s_fif_dat_rd     : unsigned(7 downto 0);       -- opcode fifo data from mmc
842
  --signal s_fif_rd         : std_logic;                  -- opcode fifo read enable
843
  --signal s_fif_rd_empty   : std_logic;
844
  --signal s_fif_rd_full    : std_logic;
845
  --signal s_fif_dat_wr     : unsigned(7 downto 0);       -- response fifo to mmc
846
  signal s_fif_wr         : std_logic;                  -- response fifo write enable
847
  --signal s_fif_wr_empty   : std_logic;
848
  --signal s_fif_wr_full    : std_logic;
849
    ------------------------------------------------------
850
 
851
  signal t_reg_sel        : std_logic; -- Test registers
852
  signal o_reg_sel        : std_logic; -- Opcode processor registers
853
  signal o_reg_ack        : std_logic; -- Opcode processor register ack
854
  signal o_reg_dat_rd     : unsigned(31 downto 0); -- Opcode processor register read data
855
  signal r_reg_sel        : std_logic; -- SPI debug registers
856
  signal r_reg_ack        : std_logic; -- SPI debug register ack
857
  signal r_reg_dat_rd     : unsigned(31 downto 0); -- SPI debug register read data
858
  signal t_reg_sel_r1     : std_logic;
859
  signal t_reg_ack        : std_logic; -- Test register ack
860
  signal t_reg_dat_rd     : unsigned(31 downto 0); -- Test register read data
861
 
862
    -- relating to system side BRAM and FIFO access
863
  signal h_ram_we           : std_logic;
864
  --signal s_fif_dat_wr_clear : std_logic;
865
  --signal s_fif_dat_rd_clear : std_logic;
866
 
867
    -- MMC host BRAM interface related
868
  signal host_bram_clk    : std_logic;
869
  signal host_bram_dat_wr : unsigned(7 downto 0);
870
  signal host_bram_dat_rd : unsigned(7 downto 0);
871
  signal host_bram_adr    : unsigned(31 downto 0);
872
  signal host_bram_we     : std_logic;
873
  signal host_bram_cyc    : std_logic;
874
 
875
    -- MMC related
876
  signal host_cmd_i       : std_logic;
877
  signal host_cmd_o       : std_logic;
878
  signal host_cmd_oe_o    : std_logic;
879
  signal host_dat_i       : unsigned(7 downto 0);
880
  signal host_dat_o       : unsigned(7 downto 0);
881
  signal host_dat_oe_o    : std_logic;
882
  signal host_dat_siz_o   : unsigned(1 downto 0);
883
  signal host_clk_o_pad   : std_logic;
884
  signal slave_clk_i      : std_logic;
885
  signal slave_cmd_i      : std_logic;
886
  signal slave_cmd_o      : std_logic;
887
  signal slave_cmd_oe_o   : std_logic;
888
  signal slave_od_mode_o  : std_logic;
889
  signal slave_dat_i      : unsigned(7 downto 0);
890
  signal slave_dat_o      : unsigned(7 downto 0);
891
  signal slave_dat_oe_o   : std_logic;
892
  signal slave_dat_siz_o  : unsigned(1 downto 0);
893
  signal test_cmd_i       : std_logic;
894
 
895
    -- local registers
896
  signal led_reg          : unsigned(SYS_LEDS-1 downto 0);
897
  signal slave_en_reg     : std_logic;
898
  signal host_en_reg      : std_logic;
899
  signal slave_enable     : std_logic;
900
  signal host_enable      : std_logic;
901
 
902
    -- local signals for the RX command counter
903
  signal t_rx_cmd_filter       : unsigned(7 downto 0);
904
  signal t_rx_cmd_done         : std_logic;
905
  signal t_rx_cmd_crc_err      : std_logic;
906
  signal t_rx_cmd_stop_err     : std_logic;
907
  signal reg_gdcount_clear     : std_logic;
908
  signal gdcount_clear         : std_logic;
909
  signal reg_crc_bdcount_clear : std_logic;
910
  signal crc_bdcount_clear     : std_logic;
911
  signal reg_stp_bdcount_clear : std_logic;
912
  signal stp_bdcount_clear     : std_logic;
913
  signal reg_dat_count_clear   : std_logic;
914
  signal dat_count_clear       : std_logic;
915
  signal t_rx_cmd_raw          : unsigned(47 downto 0);
916
  signal t_rx_cmd_gdcount      : unsigned(31 downto 0);
917
  signal t_rx_cmd_crc_bdcount  : unsigned(31 downto 0);
918
  signal t_rx_cmd_stp_bdcount  : unsigned(31 downto 0);
919
  signal t_rx_dat_count        : unsigned(31 downto 0);
920
  signal t_rx_capture          : std_logic;
921
 
922
    -- local signals for the data transfer start detector
923
  signal reg_dbus_size_clear   : std_logic;
924
  signal dbus_size_clear       : std_logic;
925
  signal reg_dbus_size         : unsigned(1 downto 0);
926
  signal dstart_wait           : unsigned(12 downto 0);
927
  signal dstart_wait_reading   : unsigned(12 downto 0);
928
  signal dstart_wait_writing   : unsigned(12 downto 0);
929
  signal write_active          : std_logic;
930
  signal dbus_active_count     : unsigned(12 downto 0);
931
  signal mmc_dat_r1            : unsigned(7 downto 0);
932
  signal mmc_dat_r2            : unsigned(7 downto 0);
933
 
934
    -- local signals for SD/MMC logging telemetry sender
935
  signal tlm_d_count           : unsigned(7 downto 0);
936
  signal tlm_fid               : unsigned(7 downto 0); -- Telemetry "Frame ID"
937
  signal tlm_1us_pulse         : std_logic;
938
  signal tlm_time              : unsigned(23 downto 0);
939
  signal tlm_tstamp            : unsigned(23 downto 0);
940
  signal tlm_fifo_fill_level   : unsigned(15 downto 0);
941
  signal tlm_fifo_clear        : std_logic;
942
  signal mmc_tlm_start         : std_logic; -- mmc_clk_i domain
943
  signal tlm_start             : std_logic; -- sys_clk domain
944
  signal tlm_fifo_reg_access   : std_logic;
945
  signal tlm_fifo_dat_rd       : std_logic;
946
  signal tlm_fifo_dat          : unsigned(7 downto 0);
947
  signal tlm_r10_dat           : unsigned(31 downto 0);
948
  signal tlm_code_byte         : unsigned(7 downto 0);
949
  signal tlm_r10_count         : unsigned(2 downto 0);
950
  signal tlm_tx_we             : std_logic;
951
  signal tlm_tx_done           : std_logic;
952
  signal tlm_stage             : unsigned(4 downto 0);
953
  signal tlm_keinplatz         : std_logic;
954
  signal tlm_fifo_leer         : std_logic;
955
  signal tlm_log_dat           : unsigned(7 downto 0);
956
  signal tlm_log_dat_we        : std_logic;
957
 
958
    -- local signals for the MMC data pipe
959
  --signal s_fif_dat_rd_level    : unsigned(MMC_FILL_LEVEL_BITS-1 downto 0);
960
  --signal s_fif_dat_wr_level    : unsigned(MMC_FILL_LEVEL_BITS-1 downto 0);  -- for debugging?
961
 
962
  -- SPI debugging vars
963
  signal dbg_spi_count         : unsigned(3 downto 0); --down counter
964
  signal dbg_spi_state         : integer;
965
  signal dbg_spi_start_l       : std_logic; -- local copy of dbg_spi_start_o
966
 
967
begin
968
 
969
  ------------------------------
970
  -- This module generates a serial BAUD clock automatically.
971
  -- The unit synchronizes on the carriage return character, so the user
972
  -- only needs to press the "enter" key for serial communications to start
973
  -- working, no matter what BAUD rate and clk_i frequency are used!
974
  auto_baud1 : auto_baud_with_tracking
975
    generic map(
976
      CLOCK_FACTOR    =>            1,  -- Output is this factor times the baud rate
977
      FPGA_CLKRATE    => SYS_CLK_RATE,  -- FPGA system clock rate
978
      MIN_BAUDRATE    =>       9600.0,  -- Minimum expected incoming Baud rate
979
      DELTA_THRESHOLD =>          200   -- Measurement filter constraint.  Smaller = tougher.
980
    )
981
    port map(
982
 
983
      sys_rst_n    => sys_rst_n,
984
      sys_clk      => sys_clk,
985
      sys_clk_en   => '1',
986
 
987
      -- rate and parity
988
      rx_parity_i  => parity, -- 0=none, 1=even, 2=odd
989
 
990
      -- serial input
991
      rx_stream_i  => cmd_i,
992
 
993
      -- Output
994
      baud_lock_o  => baud_lock,
995
      baud_clk_o   => baud_clk
996
    );
997
 
998
  parity <= to_unsigned(FPGA_PARITY,parity'length);
999
 
1000
  syscon1 : async_syscon
1001
    generic map (
1002
      ECHO_COMMANDS   =>               1, -- set nonzero to echo back command characters
1003
      ADR_DIGITS      =>      ADR_DIGITS, -- # of hex digits for address
1004
      DAT_DIGITS      =>      DAT_DIGITS, -- # of hex digits for data
1005
      QTY_DIGITS      =>      QTY_DIGITS, -- # of hex digits for quantity
1006
      CMD_BUFFER_SIZE =>   CMD_LINE_SIZE, -- # of chars in the command buffer
1007
      WATCHDOG_VALUE  =>      WDOG_VALUE, -- # of sys_clks before ack is expected
1008
      DISPLAY_FIELDS  =>               4  -- # of fields/line
1009
    )
1010
    port map (
1011
 
1012
      sys_rst_n    => sys_rst_n,
1013
      sys_clk      => sys_clk,
1014
      sys_clk_en   => '1',
1015
 
1016
      -- rate and parity
1017
      parity_i     => parity,
1018
      baud_clk_i   => baud_clk,
1019
      baud_lock_i  => baud_lock,
1020
 
1021
      -- Serial IO
1022
      cmd_i        => cmd_i,
1023
      resp_o       => resp_o,
1024
      cmd_done_o   => open,
1025
 
1026
      -- Master Bus IO
1027
      master_bg_i  => master_bg,
1028
      master_adr_i => master_adr,
1029
      master_dat_i => master_dat_wr,
1030
      master_dat_o => open, -- There is no other master to read data...
1031
      master_stb_i => master_cyc,
1032
      master_we_i  => master_we,
1033
      master_br_o  => open, -- async_syscon is the only master in this design.
1034
 
1035
      -- System Bus IO
1036
      ack_i        => syscon_ack,
1037
      err_i        => syscon_err,
1038
      dat_i        => syscon_dat_rd,
1039
      dat_o        => syscon_dat_wr,
1040
      rst_o        => syscon_rst,
1041
      stb_o        => open,
1042
      cyc_o        => syscon_cyc,
1043
      adr_o        => syscon_adr,
1044
      we_o         => syscon_we
1045
    );
1046
 
1047
  -- Combine the input reset with the async_syscon bus reset
1048
  fpga_rst_n <= '0' when (sys_rst_n='0' or syscon_rst='1') else '1';
1049
  fpga_rst   <= '1' when (sys_rst_n='0' or syscon_rst='1') else '0';
1050
 
1051
  -- Since there is no bus master besides the async_syscon, take care of those signals
1052
  master_bg     <= '1';
1053
  master_cyc    <= '0';
1054
  master_dat_wr <= (others=>'0');
1055
  master_we     <= '0';
1056
  master_adr    <= (others=>'0');
1057
 
1058
  h_reg_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 4)=16#0300000# else '0';
1059
  s_reg_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 4)=16#0300001# else '0';
1060
  t_reg_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 4)=16#0300002# else '0';
1061
  o_reg_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 4)=16#0300003# else '0';
1062
  r_reg_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 4)=16#0300004# else '0';
1063
  h_ram_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 24)=16#04# and syscon_adr(23 downto HOST_RAM_ADR_BITS)=0 else '0';
1064
  s_ram_sel <= '1' when syscon_cyc='1' and syscon_adr(31 downto 24)=16#05# and syscon_adr(23 downto MMC_RAM_ADR_BITS)=0 else '0';
1065
 
1066
  syscon_dat_rd <= h_reg_dat_rd              when h_reg_sel='1' else
1067
                   s_reg_dat_rd              when s_reg_sel='1' else
1068
                   t_reg_dat_rd              when t_reg_sel='1' else
1069
                   o_reg_dat_rd              when o_reg_sel='1' else
1070
                   r_reg_dat_rd              when r_reg_sel='1' else
1071
                   u_resize(h_ram_dat_rd,32) when h_ram_sel='1' else
1072
                   u_resize(s_ram_dat_rd,32) when s_ram_sel='1' else
1073
                   str2u("12340000",32);
1074
 
1075
  syscon_ack <= h_reg_ack when h_reg_sel='1' else
1076
                s_reg_ack when s_reg_sel='1' else
1077
                t_reg_ack when t_reg_sel='1' else
1078
                o_reg_ack when o_reg_sel='1' else
1079
                r_reg_ack when r_reg_sel='1' else
1080
                h_ram_ack when h_ram_sel='1' else
1081
                s_ram_ack when s_ram_sel='1' else
1082
                '0';
1083
 
1084
  syscon_err <= '0' when h_reg_sel='1' or s_reg_sel='1' or t_reg_sel='1' or
1085
                 o_reg_sel='1' or r_reg_sel='1' or h_ram_sel='1' or s_ram_sel='1' else '1';
1086
 
1087
  -- Select data for Local Register Reads
1088
  with to_integer(syscon_adr(3 downto 0)) select
1089
  t_reg_dat_rd <=
1090
    u_resize(led_reg,32)                           when 16#0#,
1091
    u_resize(switch_i,32)                          when 16#1#,
1092
    to_unsigned(0,23) & tlm_fifo_reg_access &
1093
      "00" & host_enable & slave_enable &
1094
      "00" & host_en_reg & slave_en_reg            when 16#2#,
1095
    u_resize(t_rx_cmd_filter,32)                   when 16#3#,
1096
    u_resize(t_rx_cmd_gdcount,32)                  when 16#4#,
1097
    u_resize(t_rx_cmd_crc_bdcount,32)              when 16#5#,
1098
    u_resize(t_rx_cmd_stp_bdcount,32)              when 16#6#,
1099
    u_resize(t_rx_dat_count,32)                    when 16#7#,
1100
    u_resize(reg_dbus_size,32)                     when 16#8#,
1101
    u_resize(tlm_fifo_fill_level,32)               when 16#9#,
1102
    u_resize(tlm_r10_dat,32)                       when 16#A#,
1103
    --u_resize(s_fif_dat_rd,32)                      when 16#F#,
1104
    str2u("51514343",32)                           when others;
1105
 
1106
  with to_integer(syscon_adr(3 downto 0)) select
1107
  o_reg_dat_rd <=
1108
    sys_status6_i                                  when 16#9#,      -- LS 16 bits=
1109
    sys_status5_i                                  when 16#A#,      -- LS 12 bits=system power, dBm x 10
1110
    sys_status4_i                                  when 16#B#,      -- system frequency setting in Hertz
1111
    opc_status3_i                                  when 16#C#,      -- 1st_opcode__last_opcode in lower 16 bits
1112
    opc_status2_i                                  when 16#D#,      -- rsp_fifo_count__opc_fifo_count
1113
    opc_status1_i                                  when 16#E#,      -- opc_state__opc_status
1114
    u_resize(opc_oc_cnt_i,32)                      when 16#F#,      -- opcodes processed
1115
  str2u("51514343",32)                             when others;
1116
 
1117
 -- Handle Local Register Writes
1118
  process(sys_rst_n,sys_clk)
1119
  begin
1120
    if (sys_rst_n='0') then
1121
      --led_reg <= (others=>'0');
1122
      t_rx_cmd_filter  <= (others=>'1'); -- Default is to count everything
1123
      reg_gdcount_clear <= '0';
1124
      reg_crc_bdcount_clear <= '0';
1125
      reg_stp_bdcount_clear <= '0';
1126
      reg_dat_count_clear <= '0';
1127
      reg_dbus_size_clear <= '0';
1128
      --s_fif_dat_wr_clear <= '0';
1129
      --s_fif_dat_rd_clear <= '0';
1130
      t_reg_sel_r1 <= '0';
1131
      host_en_reg <= '0';
1132
      slave_en_reg <= '0';
1133
      tlm_fifo_reg_access <= '0';
1134
 
1135
      -- SPI debugging
1136
      dbg_spi_bytes_io <= to_unsigned(0, dbg_spi_bytes_io'length);
1137
      dbg_spi_start_l <= '0';
1138
      dbg_spi_device_o <= to_unsigned(0, dbg_spi_device_o'length);
1139
      dbg_enables_o <= to_unsigned(0, dbg_enables_o'length);
1140
      dbg_spi_state <= 0;
1141
 
1142
    elsif (sys_clk'event and sys_clk='1') then
1143
      -- Default values
1144
      reg_gdcount_clear <= '0';
1145
      reg_crc_bdcount_clear <= '0';
1146
      reg_stp_bdcount_clear <= '0';
1147
      reg_dat_count_clear <= '0';
1148
      reg_dbus_size_clear <= '0';
1149
      --s_fif_dat_wr_clear <= '0';
1150
      --s_fif_dat_rd_clear <= '0';
1151
      -- Register writes have the highest priority
1152
      t_reg_sel_r1 <= t_reg_sel;
1153
      if (t_reg_sel='1' and t_reg_sel_r1='0' and syscon_we='1') then
1154
        case to_integer(syscon_adr(3 downto 0)) is
1155
          when 16#0# =>
1156
            --led_reg <= syscon_dat_wr(SYS_LEDS-1 downto 0);
1157
          when 16#2# =>
1158
            slave_en_reg <= syscon_dat_wr(0);
1159
            host_en_reg  <= syscon_dat_wr(1);
1160
            tlm_fifo_reg_access <= syscon_dat_wr(8);
1161
          when 16#3# =>
1162
            t_rx_cmd_filter <= syscon_dat_wr(7 downto 0);
1163
          -- Clearing the counters must be done in the mmc_clk domain...
1164
          -- Flancters are used to pass the signals into the other clock
1165
          -- domain.
1166
          when 16#4# =>
1167
            reg_gdcount_clear <= '1';
1168
          when 16#5# =>
1169
            reg_crc_bdcount_clear <= '1';
1170
          when 16#6# =>
1171
            reg_stp_bdcount_clear <= '1';
1172
          when 16#7# =>
1173
            reg_dat_count_clear <= '1';
1174
          when 16#8# =>
1175
            reg_dbus_size_clear <= '1';
1176
          -- Register 9 writes implemented at the FIFO
1177
          when 16#C# =>
1178
             dbg_enables_o <= syscon_dat_wr(15 downto 0);
1179
          --when 16#E# =>
1180
            --opc_rspf_reset_i <= syscon_dat_wr(0);     --s_fif_dat_wr_clear <= syscon_dat_wr(0);
1181
            --opc_rd_reset_i <= syscon_dat_wr(1);     --s_fif_dat_rd_clear <= syscon_dat_wr(1);
1182
          when others =>
1183
            null;
1184
        end case;
1185
      end if;
1186
 
1187
--      if (o_reg_sel='1' and syscon_we='1') then
1188
--        case to_integer(syscon_adr(3 downto 0)) is
1189
--          when 16#0# =>
1190
--            bkd_opc_load_new <= '0';
1191
--            bkd_opc_dat0_o <= syscon_dat_wr(31 downto 0);
1192
--          when 16#1# =>
1193
--            bkd_opc_dat1_o <= syscon_dat_wr(31 downto 0);
1194
--          when 16#F# =>
1195
--            bkd_opc_datF_o <= syscon_dat_wr(31 downto 0);
1196
--            bkd_opc_load_new <= '1';
1197
--          when others =>
1198
--            null;
1199
--          end case;
1200
--      elsif (bkd_opc_load_ack='1') then
1201
--        bkd_opc_load_new <= '0';
1202
--      end if;            
1203
 
1204
      -- spi debug, SPI debug register writes, 03000040 x y z ...
1205
      if (r_reg_sel='1' and syscon_we='1') then
1206
        case to_integer(syscon_adr(3 downto 0)) is
1207
          when 16#0# =>
1208
            dbg_spi_device_o <= syscon_dat_wr(2 downto 0);
1209
            dbg_spi_start_l <= '0';
1210
          when 16#1# =>
1211
            dbg_spi_bytes_io <= syscon_dat_wr(3 downto 0);
1212
            dbg_spi_count <= to_unsigned(1, dbg_spi_count'length);
1213
          when 16#2# =>
1214
            dbg_spi_data0_o <= syscon_dat_wr(7 downto 0);
1215
            if(dbg_spi_count = dbg_spi_bytes_io) then
1216
              dbg_spi_start_l <= '1';
1217
            else
1218
              dbg_spi_count <= dbg_spi_count + 1;
1219
            end if;
1220
          when 16#3# =>
1221
            dbg_spi_data1_o <= syscon_dat_wr(7 downto 0);
1222
            if(dbg_spi_count = dbg_spi_bytes_io) then
1223
              dbg_spi_start_l <= '1';
1224
            else
1225
              dbg_spi_count <= dbg_spi_count + 1;
1226
            end if;
1227
          when 16#4# =>
1228
            dbg_spi_data2_o <= syscon_dat_wr(7 downto 0);
1229
            if(dbg_spi_count = dbg_spi_bytes_io) then
1230
              dbg_spi_start_l <= '1';
1231
            else
1232
              dbg_spi_count <= dbg_spi_count + 1;
1233
            end if;
1234
          when 16#5# =>
1235
            dbg_spi_data3_o <= syscon_dat_wr(7 downto 0);
1236
            if(dbg_spi_count = dbg_spi_bytes_io) then
1237
              dbg_spi_start_l <= '1';
1238
            else
1239
              dbg_spi_count <= dbg_spi_count + 1;
1240
            end if;
1241
          when 16#6# =>
1242
            dbg_spi_data4_o <= syscon_dat_wr(7 downto 0);
1243
            if(dbg_spi_count = dbg_spi_bytes_io) then
1244
              dbg_spi_start_l <= '1';
1245
            else
1246
              dbg_spi_count <= dbg_spi_count + 1;
1247
            end if;
1248
          when 16#7# =>
1249
            dbg_spi_data5_o <= syscon_dat_wr(7 downto 0);
1250
            if(dbg_spi_count = dbg_spi_bytes_io) then
1251
              dbg_spi_start_l <= '1';
1252
            else
1253
              dbg_spi_count <= dbg_spi_count + 1;
1254
            end if;
1255
          when 16#8# =>
1256
            dbg_spi_data6_o <= syscon_dat_wr(7 downto 0);
1257
            if(dbg_spi_count = dbg_spi_bytes_io) then
1258
              dbg_spi_start_l <= '1';
1259
            else
1260
              dbg_spi_count <= dbg_spi_count + 1;
1261
            end if;
1262
          when 16#9# =>
1263
            dbg_spi_data7_o <= syscon_dat_wr(7 downto 0);
1264
            if(dbg_spi_count = dbg_spi_bytes_io) then
1265
              dbg_spi_start_l <= '1';
1266
            else
1267
              dbg_spi_count <= dbg_spi_count + 1;
1268
            end if;
1269
          when 16#A# =>
1270
            dbg_spi_data8_o <= syscon_dat_wr(7 downto 0);
1271
            if(dbg_spi_count = dbg_spi_bytes_io) then
1272
              dbg_spi_start_l <= '1';
1273
            else
1274
              dbg_spi_count <= dbg_spi_count + 1;
1275
            end if;
1276
          when 16#B# =>
1277
            dbg_spi_data9_o <= syscon_dat_wr(7 downto 0);
1278
            if(dbg_spi_count = dbg_spi_bytes_io) then
1279
              dbg_spi_start_l <= '1';
1280
            else
1281
              dbg_spi_count <= dbg_spi_count + 1;
1282
            end if;
1283
          when 16#C# =>
1284
            dbg_spi_dataA_o <= syscon_dat_wr(7 downto 0);
1285
            if(dbg_spi_count = dbg_spi_bytes_io) then
1286
              dbg_spi_start_l <= '1';
1287
            else
1288
              dbg_spi_count <= dbg_spi_count + 1;
1289
            end if;
1290
          when 16#D# =>
1291
            dbg_spi_dataB_o <= syscon_dat_wr(7 downto 0);
1292
            if(dbg_spi_count = dbg_spi_bytes_io) then
1293
              dbg_spi_start_l <= '1';
1294
            else
1295
              dbg_spi_count <= dbg_spi_count + 1;
1296
            end if;
1297
          when 16#E# =>
1298
            dbg_spi_dataC_o <= syscon_dat_wr(7 downto 0);
1299
            if(dbg_spi_count = dbg_spi_bytes_io) then
1300
              dbg_spi_start_l <= '1';
1301
            else
1302
              dbg_spi_count <= dbg_spi_count + 1;
1303
            end if;
1304
          when 16#F# =>
1305
            dbg_spi_dataD_o <= syscon_dat_wr(7 downto 0);
1306
            dbg_spi_start_l <= '1';
1307
          when others =>
1308
            null;
1309
          end case;
1310
      end if;
1311
 
1312
      --If Debug SPI just started, clear start pulse, device #
1313
      if(dbg_spi_start_l = '1' and dbg_spi_busy_i = '1') then
1314
        dbg_spi_start_l <= '0';     -- clear start
1315
      end if;
1316
 
1317
    end if;
1318
  end process;
1319
  -- Provide test register acknowledge
1320
  t_reg_ack <= '1' when syscon_adr(3 downto 0)=10 and tlm_r10_count=1 else
1321
               '1' when syscon_adr(3 downto 0)/=10 and t_reg_sel='1' and t_reg_sel_r1='0' else
1322
               '0';
1323
  o_reg_ack <= o_reg_sel;
1324
  r_reg_ack <= r_reg_sel;
1325
 
1326
  -- assign output based on local value
1327
  dbg_spi_start_o <= dbg_spi_start_l;
1328
 
1329
  -- Provide led_reg as output to external LEDs
1330
  led_o <= led_reg;
1331
 
1332
  -- Handle Reads from TLM FIFO
1333
  -- Data is shifted in on counts 5,4,3,2.
1334
  -- The acknowledge pulse is given on count 1.
1335
  -- Count 0 is the quiescent condition.
1336
  process(sys_rst_n,sys_clk)
1337
  begin
1338
    if (sys_rst_n='0') then
1339
      tlm_r10_dat   <= (others=>'0');
1340
      tlm_r10_count <= (others=>'0');
1341
    elsif (sys_clk'event and sys_clk='1') then
1342
      -- Handle the "byte shift" register counter
1343
      if (tlm_r10_count>0) then
1344
        tlm_r10_count <= tlm_r10_count-1;
1345
      end if;
1346
      -- Implement the "byte shift" data register
1347
      if (tlm_r10_count<6 and tlm_r10_count>1) then
1348
        if (tlm_fifo_reg_access='1') then
1349
          tlm_r10_dat <= tlm_r10_dat(23 downto 0) & tlm_fifo_dat;
1350
        else
1351
          tlm_r10_dat <= tlm_r10_dat(23 downto 0) & "01010101";
1352
        end if;
1353
      end if;
1354
      -- Charge the countdown when a read is initiated
1355
      if (t_reg_sel='1' and t_reg_sel_r1='0' and syscon_we='0' and syscon_adr(3 downto 0)=16#A#) then
1356
        tlm_r10_count <= to_unsigned(6,tlm_r10_count'length);
1357
      end if;
1358
    end if;
1359
  end process;
1360
 
1361
 
1362
  t_rx_gdcount_reset : flancter_rising_pulseout
1363
  port map(
1364
    async_rst_n => sys_rst_n,
1365
    set_clk     => sys_clk,
1366
    set         => reg_gdcount_clear,
1367
    reset_clk   => mmc_clk_i,
1368
    reset       => gdcount_clear,
1369
    pulse_s_o   => open,
1370
    pulse_r_o   => gdcount_clear,
1371
    flag_o      => open
1372
  );
1373
 
1374
  t_rx_crc_bdcount_reset : flancter_rising_pulseout
1375
  port map(
1376
    async_rst_n => sys_rst_n,
1377
    set_clk     => sys_clk,
1378
    set         => reg_crc_bdcount_clear,
1379
    reset_clk   => mmc_clk_i,
1380
    reset       => crc_bdcount_clear,
1381
    pulse_s_o   => open,
1382
    pulse_r_o   => crc_bdcount_clear,
1383
    flag_o      => open
1384
  );
1385
 
1386
  t_rx_stp_bdcount_reset : flancter_rising_pulseout
1387
  port map(
1388
    async_rst_n => sys_rst_n,
1389
    set_clk     => sys_clk,
1390
    set         => reg_stp_bdcount_clear,
1391
    reset_clk   => mmc_clk_i,
1392
    reset       => stp_bdcount_clear,
1393
    pulse_s_o   => open,
1394
    pulse_r_o   => stp_bdcount_clear,
1395
    flag_o      => open
1396
  );
1397
 
1398
  t_rx_dat_count_reset : flancter_rising_pulseout
1399
  port map(
1400
    async_rst_n => sys_rst_n,
1401
    set_clk     => sys_clk,
1402
    set         => reg_dat_count_clear,
1403
    reset_clk   => mmc_clk_i,
1404
    reset       => dat_count_clear,
1405
    pulse_s_o   => open,
1406
    pulse_r_o   => dat_count_clear,
1407
    flag_o      => open
1408
  );
1409
 
1410
  reg_dbus_size_reset : flancter_rising_pulseout
1411
  port map(
1412
    async_rst_n => sys_rst_n,
1413
    set_clk     => sys_clk,
1414
    set         => reg_dbus_size_clear,
1415
    reset_clk   => mmc_clk_i,
1416
    reset       => dbus_size_clear,
1417
    pulse_s_o   => open,
1418
    pulse_r_o   => dbus_size_clear,
1419
    flag_o      => open
1420
  );
1421
 
1422
  -- Use a separate command receiver to count the number of received
1423
  -- commands.  A register setting allows counting only commands of
1424
  -- a certain index and/or direction, otherwise all commands can be counted.
1425
  -- Separate counts of good/bad commands are kept.
1426
  test_cmd_receiver : mmc_test_cmd_rx
1427
  port map(
1428
    -- Asynchronous reset
1429
    sys_rst_n   => sys_rst_n,
1430
    -- SD/MMC card command signals
1431
    sd_clk_i    => mmc_clk_i,
1432
    sd_cmd_i    => test_cmd_i,
1433
    -- Command outputs
1434
    cmd_raw_o   => t_rx_cmd_raw,
1435
    -- Status and done indicator
1436
    cmd_done_o  => t_rx_cmd_done,
1437
    crc_err_o   => t_rx_cmd_crc_err,
1438
    stop_err_o  => t_rx_cmd_stop_err
1439
  );
1440
  -- Telemetry code byte currently only shows the direction of the transfer
1441
  tlm_code_byte <= "11111111" when t_rx_cmd_raw(47 downto 46)="01" else "00000000";
1442
 
1443
  -- Handle counting received commands, whether good or bad
1444
  -- Also, count data transfer starts
1445
  process(sys_rst_n,mmc_clk_i)
1446
  begin
1447
    if (sys_rst_n='0') then
1448
      t_rx_cmd_gdcount <= (others=>'0');
1449
      t_rx_cmd_crc_bdcount <= (others=>'0');
1450
      t_rx_cmd_stp_bdcount <= (others=>'0');
1451
      t_rx_dat_count <= (others=>'0');
1452
      dbus_active_count <= (others=>'0');
1453
      reg_dbus_size <= "00";
1454
      mmc_dat_r1 <= (others=>'0');
1455
      mmc_dat_r2 <= (others=>'0');
1456
      tlm_d_count <= (others=>'0');
1457
      mmc_tlm_start <= '0';
1458
    elsif (mmc_clk_i'event and mmc_clk_i='1') then
1459
      -- Default values
1460
      mmc_tlm_start <= '0';
1461
      -- Data counters are incremented at each data transfer "start bit"
1462
      -- An attempt is made to track the current data bus size, by catching
1463
      -- CMD6 (SWITCH) commands which write to the EXT_CSD contents at
1464
      -- location offset 183d (0xB7).  Based on the current bus size, a
1465
      -- dstart_wait value is derived, and used to ignore activity on the
1466
      -- SD/MMC data bus during the transfer, thereby guaranteeing that only
1467
      -- real start bits are counted.
1468
      if (t_rx_cmd_done='1' and (t_rx_cmd_raw(47 downto 40)=16#46#)
1469
          and t_rx_cmd_crc_err='0' and t_rx_cmd_stop_err='0') then
1470
        if (t_rx_cmd_raw(31 downto 24)=16#B7#) then
1471
          -- Try to handle all EXT_CSD access modes...
1472
          if (t_rx_cmd_raw(33 downto 32)="11") then -- Write byte mode
1473
            reg_dbus_size <= t_rx_cmd_raw(17 downto 16);
1474
          end if;
1475
          if (t_rx_cmd_raw(33 downto 32)="10") then -- Clear bits mode
1476
            reg_dbus_size <= reg_dbus_size and not t_rx_cmd_raw(17 downto 16);
1477
          end if;
1478
          if (t_rx_cmd_raw(33 downto 32)="01") then -- Set bits mode
1479
            reg_dbus_size <= reg_dbus_size or t_rx_cmd_raw(17 downto 16);
1480
          end if;
1481
        end if;
1482
      end if;
1483
      -- Store previous data, for SD/MMC data bus start bit detection
1484
      mmc_dat_r1 <= mmc_dat_i;
1485
      mmc_dat_r2 <= mmc_dat_r1;
1486
      -- Handle decrementing the data active count down
1487
      if (dbus_active_count>0) then
1488
        dbus_active_count <= dbus_active_count-1;
1489
      end if;
1490
      -- Detect start bits, according to reg_dbus_size setting
1491
      -- The dstart_wait value is used to ignore further activity until
1492
      -- after the data transfer is completed.
1493
      if (dbus_active_count=0) then
1494
        if (reg_dbus_size=0) then
1495
          if (mmc_dat_r2(0)='1' and mmc_dat_r1(0)='0') then
1496
            t_rx_dat_count <= t_rx_dat_count+1;
1497
            tlm_d_count <= tlm_d_count+1;
1498
            dbus_active_count <= dstart_wait;
1499
          end if;
1500
        end if;
1501
        if (reg_dbus_size=1) then
1502
          if (mmc_dat_r2(3 downto 0)="1111" and mmc_dat_r1(3 downto 0)="0000") then
1503
            t_rx_dat_count <= t_rx_dat_count+1;
1504
            tlm_d_count <= tlm_d_count+1;
1505
            dbus_active_count <= dstart_wait;
1506
          end if;
1507
        end if;
1508
        if (reg_dbus_size=2) then
1509
          if (mmc_dat_r2="11111111" and mmc_dat_r1="00000000") then
1510
            t_rx_dat_count <= t_rx_dat_count+1;
1511
            tlm_d_count <= tlm_d_count+1;
1512
            dbus_active_count <= dstart_wait;
1513
          end if;
1514
        end if;
1515
      end if;
1516
      if (t_rx_capture='1') then
1517
        mmc_tlm_start <= '1';
1518
        if (t_rx_cmd_crc_err='0' and t_rx_cmd_stop_err='0') then
1519
          t_rx_cmd_gdcount <= t_rx_cmd_gdcount+1;
1520
        else
1521
          if (t_rx_cmd_crc_err='1') then
1522
            t_rx_cmd_crc_bdcount <= t_rx_cmd_crc_bdcount+1;
1523
          end if;
1524
          if (t_rx_cmd_stop_err='1') then
1525
            t_rx_cmd_stp_bdcount <= t_rx_cmd_stp_bdcount+1;
1526
          end if;
1527
        end if;
1528
      end if;
1529
      -- Clearing the counters is given priority over incrementing them
1530
      if (gdcount_clear='1') then
1531
        t_rx_cmd_gdcount <= (others=>'0');
1532
      end if;
1533
      if (crc_bdcount_clear='1') then
1534
        t_rx_cmd_crc_bdcount <= (others=>'0');
1535
      end if;
1536
      if (stp_bdcount_clear='1') then
1537
        t_rx_cmd_stp_bdcount <= (others=>'0');
1538
      end if;
1539
      if (dat_count_clear='1') then
1540
        t_rx_dat_count <= (others=>'0');
1541
      end if;
1542
      -- Clearing the reg_dbus_size is done in this clock domain
1543
      if (dbus_size_clear='1') then
1544
        reg_dbus_size <= (others=>'0');
1545
      end if;
1546
    end if;
1547
  end process;
1548
  -- Create a signal that, when high, indicates that the SD/MMC command
1549
  -- event filter criteria have been met.
1550
--  t_rx_capture <= '1' when (t_rx_cmd_done='1' and t_rx_cmd_filter>"10000000") else
1551
--                  '1' when (t_rx_cmd_done='1' and t_rx_cmd_filter=t_rx_cmd_raw(47 downto 40)) else
1552
--                  '0';
1553
-- John C sent this capture filter bugfix 24-Jun-2017
1554
    t_rx_capture <= '1' when (t_rx_cmd_done='1' and t_rx_cmd_filter>"10000000") else
1555
                  '1' when (t_rx_cmd_done='1' and t_rx_cmd_filter(6)=t_rx_cmd_raw(46) and t_rx_cmd_filter(5 downto 0)="111111") else
1556
                  '1' when (t_rx_cmd_done='1' and t_rx_cmd_filter=t_rx_cmd_raw(47 downto 40)) else
1557
                  '0';
1558
 
1559
  -- Derive dstart_wait values, used in detecting valid data transfer
1560
  -- start bits.  Tune these values as needed, based on the sector
1561
  -- size.
1562
  write_active <= '1' when ((t_rx_cmd_raw(45 downto 40)=24) or (t_rx_cmd_raw(45 downto 40)=25)) else '0';
1563
  dstart_wait <= dstart_wait_writing when (write_active='1') else dstart_wait_reading;
1564
  -- When reading, include sector, plus start, CRC-16 and stop bits
1565
  with (reg_dbus_size) select
1566
  dstart_wait_reading <=
1567
    to_unsigned(4096+2+16,dstart_wait'length) when "00",
1568
    to_unsigned(1024+2+16,dstart_wait'length) when "01",
1569
    to_unsigned( 512+2+16,dstart_wait'length) when "10",
1570
    to_unsigned( 512+2+16,dstart_wait'length) when others;
1571
  -- When writing, include sector, plus start, CRC-16, stop and CRC response token (2 turnaround+5 bits)
1572
  with (reg_dbus_size) select
1573
  dstart_wait_writing <=
1574
    to_unsigned(4096+2+16+7,dstart_wait'length) when "00",
1575
    to_unsigned(1024+2+16+7,dstart_wait'length) when "01",
1576
    to_unsigned( 512+2+16+7,dstart_wait'length) when "10",
1577
    to_unsigned( 512+2+16+7,dstart_wait'length) when others;
1578
 
1579
  -------------------------------------------------------------------------
1580
  -- SD/MMC transaction telemetry sending logic
1581
  -- For logging activity on the SD/MMC bus
1582
 
1583
  -- Create a 24 bit time counter with 1 microsecond resolution
1584
  -- This is done in the system clock domain.
1585
  tlm_1us_unit : dds_constant_squarewave
1586
    generic map(
1587
      OUTPUT_FREQ  => 1000000.0,    -- Desired output frequency
1588
      SYS_CLK_RATE => SYS_CLK_RATE, -- underlying clock rate
1589
      ACC_BITS     => 32 -- Bit width of DDS phase accumulator
1590
    )
1591
    port map(
1592
      sys_rst_n    => sys_rst_n,
1593
      sys_clk      => sys_clk,
1594
      sys_clk_en   => '1',
1595
 
1596
      -- Output
1597
      pulse_o      => tlm_1us_pulse,
1598
      squarewave_o => open
1599
    );
1600
  process(sys_rst_n,sys_clk)
1601
  begin
1602
    if (sys_rst_n='0') then
1603
      tlm_time <= (others=>'0');
1604
    elsif (sys_clk'event and sys_clk='1') then
1605
      if (tlm_1us_pulse='1') then
1606
        tlm_time <= tlm_time+1;
1607
      end if;
1608
    end if;
1609
  end process;
1610
  -- Implement a small FIFO to accomodate the "burstiness" of the
1611
  -- telemetry data, as compared with the more plodding consumption
1612
  -- of it at the other end...
1613
  tlm_fifo_unit : swiss_army_fifo
1614
  generic map(
1615
    USE_BRAM         => 1, -- Set to nonzero value for BRAM, zero for distributed RAM
1616
    WIDTH            => 8,
1617
    DEPTH            => TLM_FIFO_DEPTH,
1618
    FILL_LEVEL_BITS  => tlm_fifo_fill_level'length, -- Should be at least int(floor(log2(DEPTH))+1.0)
1619
    PF_FULL_POINT    => TLM_FIFO_DEPTH-16,
1620
    PF_FLAG_POINT    => (TLM_FIFO_DEPTH/2),
1621
    PF_EMPTY_POINT   => 1
1622
  )
1623
  port map(
1624
    sys_rst_n       => sys_rst_n, -- Asynchronous
1625
    sys_clk         => sys_clk,
1626
    sys_clk_en      => '1',
1627
 
1628
    reset_i         => tlm_fifo_clear, -- Synchronous
1629
 
1630
    fifo_wr_i       => tlm_log_dat_we,
1631
    fifo_din        => tlm_log_dat,
1632
 
1633
    fifo_rd_i       => tlm_fifo_dat_rd,
1634
    fifo_dout       => tlm_fifo_dat,
1635
 
1636
    fifo_fill_level => tlm_fifo_fill_level,
1637
    fifo_full       => open,
1638
    fifo_empty      => tlm_fifo_leer,
1639
    fifo_pf_full    => tlm_keinplatz,
1640
    fifo_pf_flag    => open,
1641
    fifo_pf_empty   => open
1642
  );
1643
  tlm_fifo_clear <= '1' when (t_reg_sel='1' and t_reg_sel_r1='0' and syscon_we='1' and syscon_adr(3 downto 0)=16#9#) else '0';
1644
  tlm_fifo_dat_rd <= '1' when (tlm_fifo_reg_access='1' and tlm_r10_count>2) else
1645
                     '1' when (tlm_fifo_reg_access='0' and tlm_tx_we='1') else
1646
                     '0';
1647
  -- Create an asynchronous transmitter that sends out a stream of
1648
  -- telemetry information, when enabled.
1649
  -- The baud clock pulse feeding the async_syscon unit is also used here
1650
  tlm_tx_unit : async_tx_sqclk
1651
    port map(
1652
      sys_rst_n    => sys_rst_n,
1653
      sys_clk      => sys_clk,
1654
      sys_clk_en   => baud_lock, -- Do not transmit unless Baud rate is settled.
1655
 
1656
      -- rate and parity
1657
      tx_parity_i  => "00", -- 0=none, 1=even, 2 or 3=odd
1658
      tx_clk_i     => baud_clk,
1659
 
1660
      -- serial output
1661
      tx_stream    => tlm_o,
1662
 
1663
      -- control and status
1664
      tx_wr_i      => tlm_tx_we,        -- Starts Transmit
1665
      tx_dat_i     => tlm_fifo_dat,
1666
      tx_done_o    => tlm_tx_done
1667
    );
1668
  -- Automatically read out the contents of the FIFO, when enabled
1669
  tlm_tx_we <= '1' when baud_lock='1' and tlm_tx_done='1' and tlm_send_i='1'
1670
                   and tlm_fifo_leer='0' and tlm_fifo_reg_access='0' else '0';
1671
 
1672
  -- This is the sequencer that coordinates the creation of a new
1673
  -- telemetry log record, to be stuffed into the telemetry log FIFO
1674
  process(sys_rst_n,sys_clk)
1675
  begin
1676
    if (sys_rst_n='0') then
1677
      tlm_stage  <= (others=>'0');
1678
      tlm_tstamp <= (others=>'0');
1679
      tlm_fid    <= (others=>'1');
1680
    elsif (sys_clk'event and sys_clk='1') then
1681
      -- Handle the sequence step down counter
1682
      if (tlm_stage>0) then
1683
        tlm_stage <= tlm_stage-1;
1684
      end if;
1685
      -- Catch start signal.  If the FIFO has room
1686
      -- for at least one more complete record, then
1687
      -- capture timestamp and begin.
1688
      if (tlm_start='1' and tlm_keinplatz='0') then
1689
        tlm_tstamp <= tlm_time;
1690
        tlm_fid <= tlm_fid+1;
1691
        tlm_stage <= to_unsigned(16,tlm_stage'length);
1692
      end if;
1693
    end if;
1694
  end process;
1695
  -- When active, write a new data byte every clock cycle
1696
  tlm_log_dat_we <= '1' when tlm_stage>0 else '0';
1697
 
1698
  -- Select data for Local Register Reads
1699
  with to_integer(tlm_stage) select
1700
  tlm_log_dat <=
1701
    to_unsigned(16#FE#,8)             when 16,
1702
    to_unsigned(16#6B#,8)             when 15,
1703
    to_unsigned(16#28#,8)             when 14,
1704
    to_unsigned(16#40#,8)             when 13,
1705
    tlm_fid                           when 12,
1706
    tlm_tstamp(23 downto 16)          when 11,
1707
    tlm_tstamp(15 downto 8)           when 10,
1708
    tlm_tstamp( 7 downto 0)           when  9,
1709
    tlm_d_count                       when  8,
1710
    tlm_code_byte                     when  7,
1711
    "00" & t_rx_cmd_raw(45 downto 40) when  6, -- Upper 2 MSBs determine tlm_code_byte
1712
    t_rx_cmd_raw(39 downto 32)        when  5,
1713
    t_rx_cmd_raw(31 downto 24)        when  4,
1714
    t_rx_cmd_raw(23 downto 16)        when  3,
1715
    t_rx_cmd_raw(15 downto  8)        when  2,
1716
    t_rx_cmd_raw( 7 downto  0)        when  1,
1717
    to_unsigned(16#55#,8)             when others;
1718
 
1719
  -- Use safe clock domain crossing for the signal that starts the process
1720
  -- of emitting a new telemetry record, so we don't miss anything.
1721
  tlm_start_pulser : flancter_rising_pulseout
1722
  port map(
1723
    async_rst_n => sys_rst_n,
1724
    set_clk     => mmc_clk_i,
1725
    set         => mmc_tlm_start,
1726
    reset_clk   => sys_clk,
1727
    reset       => tlm_start,
1728
    pulse_s_o   => open,
1729
    pulse_r_o   => tlm_start,
1730
    flag_o      => open
1731
  );
1732
 
1733
 
1734
  -------------------------------------------------------------------------
1735
  -- Create a Block RAM which the SD/MMC controller uses as a data
1736
  -- storage area.
1737
  -- The 'A' port is attached to the simulation bus controller
1738
  -- The 'B' port is attached to the mmc controller
1739
  h_ram_we <= '1' when h_ram_sel='1' and syscon_we='1' else '0';
1740
  host_0_bram_0 : swiss_army_ram
1741
    generic map(
1742
      USE_BRAM  => 1, -- Set to nonzero value for BRAM, zero for distributed RAM
1743
      WRITETHRU => 1, -- Set to nonzero value for writethrough mode
1744
      USE_FILE  => 1, -- Set to nonzero value to use INIT_FILE
1745
      INIT_VAL  => 16#00#, -- Value used when INIT_FILE is not used
1746
      INIT_SEL  => 0, -- Selects which segment of (larger) INIT_FILE to use
1747
      INIT_FILE => "host_ram_init.txt", -- ASCII hexadecimal initialization file name
1748
      FIL_WIDTH => 32, -- Bit width of init file lines
1749
      ADR_WIDTH => HOST_RAM_ADR_BITS,
1750
      DAT_WIDTH =>  8
1751
    )
1752
    port map(
1753
      clk_a    => sys_clk,
1754
      adr_a_i  => syscon_adr(13 downto 0),
1755
      we_a_i   => h_ram_we,
1756
      en_a_i   => std_logic'('1'),
1757
      dat_a_i  => syscon_dat_wr(7 downto 0),
1758
      dat_a_o  => h_ram_dat_rd,
1759
 
1760
      clk_b    => host_bram_clk,
1761
      adr_b_i  => host_bram_adr(HOST_RAM_ADR_BITS-1 downto 0),
1762
      we_b_i   => host_bram_we,
1763
      en_b_i   => std_logic'('1'),
1764
      dat_b_i  => host_bram_dat_wr,
1765
      dat_b_o  => host_bram_dat_rd
1766
    );
1767
-- system side BRAM ack signal needs to be delayed by 1 cycle,
1768
-- to allow for the BRAM to respond to the given address.
1769
process(sys_rst_n,sys_clk)
1770
begin
1771
  if (sys_rst_n='0') then
1772
    h_ram_ack <= '0';
1773
  elsif (sys_clk'event and sys_clk='1') then
1774
    h_ram_ack <= h_ram_sel;
1775
  end if;
1776
end process;
1777
 
1778
  sd_host_0 : sd_controller_8bit_bram
1779
  port map(
1780
    -- WISHBONE common
1781
    wb_clk_i     => sys_clk,
1782
    wb_rst_i     => fpga_rst,
1783
    -- WISHBONE slave (register interface)
1784
    wb_dat_i     => syscon_dat_wr,
1785
    wb_dat_o     => h_reg_dat_rd,
1786
    wb_adr_i     => syscon_adr(3 downto 0),
1787
    wb_we_i      => syscon_we,
1788
    wb_cyc_i     => h_reg_sel,
1789
    wb_ack_o     => h_reg_ack,
1790
    -- Dedicated BRAM port without acknowledge.
1791
    -- Access cycles must complete immediately.
1792
    -- (data to cross clock domains by this dual-ported BRAM)
1793
    bram_clk_o   => host_bram_clk, -- Same as sd_clk_o_pad
1794
    bram_dat_o   => host_bram_dat_wr,
1795
    bram_dat_i   => host_bram_dat_rd,
1796
    bram_adr_o   => host_bram_adr,
1797
    bram_we_o    => host_bram_we,
1798
    bram_cyc_o   => host_bram_cyc,
1799
    --SD Card Interface
1800
    sd_cmd_i     => host_cmd_i,
1801
    sd_cmd_o     => host_cmd_o,
1802
    sd_cmd_oe_o  => host_cmd_oe_o,
1803
    sd_dat_i     => host_dat_i,
1804
    sd_dat_o     => host_dat_o,
1805
    sd_dat_oe_o  => host_dat_oe_o,
1806
    sd_dat_siz_o => host_dat_siz_o,
1807
    sd_clk_o_pad => host_clk_o_pad,
1808
    -- Interrupt outputs
1809
    int_cmd_o    => open,
1810
    int_data_o   => open
1811
  );
1812
 
1813
  mmc_slave : mmc_data_pipe
1814
  generic map(
1815
    EXT_CSD_INIT_FILE => "ext_csd_init.txt", -- Initial contents of EXT_CSD
1816
    FIFO_DEPTH        => MMC_FIFO_DEPTH,
1817
    FILL_LEVEL_BITS   => MMC_FILL_LEVEL_BITS, --s_fif_dat_wr_level'length, -- Should be at least int(floor(log2(FIFO_DEPTH))+1.0)
1818
    RAM_ADR_WIDTH     => MMC_RAM_ADR_BITS
1819
  )
1820
  port map(
1821
 
1822
    -- Asynchronous reset
1823
    sys_rst_n     => fpga_rst_n,
1824
    sys_clk       => sys_clk,
1825
 
1826
    -- Bus interface
1827
    adr_i         => syscon_adr(3 downto 0),
1828
    sel_i         => s_reg_sel,
1829
    we_i          => syscon_we,
1830
    dat_i         => syscon_dat_wr,
1831
    dat_o         => s_reg_dat_rd,
1832
    ack_o         => s_reg_ack,
1833
 
1834
    -- SD/MMC card signals
1835
    mmc_clk_i     => slave_clk_i,
1836
    mmc_cmd_i     => slave_cmd_i,
1837
    mmc_cmd_o     => slave_cmd_o,
1838
    mmc_cmd_oe_o  => slave_cmd_oe_o,
1839
    mmc_od_mode_o => slave_od_mode_o, -- Open drain mode
1840
    mmc_dat_i     => slave_dat_i,
1841
    mmc_dat_o     => slave_dat_o,
1842
    mmc_dat_oe_o  => slave_dat_oe_o,
1843
    mmc_dat_siz_o => slave_dat_siz_o,
1844
 
1845
    -- Data Pipe FIFOs
1846
    wr_clk_i      => sys_clk,
1847
    wr_clk_en_i   => '1',
1848
    wr_reset_i    => opc_rspf_reset_i,          --s_fif_dat_wr_clear,  -- Synchronous
1849
    wr_en_i       => opc_rspf_we_i,             --s_fif_wr,
1850
    wr_dat_i      => opc_rspf_dat_i,            --s_fif_dat_wr,  --syscon_dat_wr(7 downto 0),
1851
    wr_fifo_level => opc_rspf_cnt_o,            --s_fif_dat_wr_level,
1852
    wr_fifo_full  => opc_rspf_fl_o,             --s_fif_wr_full,
1853
    wr_fifo_empty => opc_rspf_mt_o,             --s_fif_wr_empty,
1854
 
1855
    rd_clk_i      => sys_clk,
1856
    rd_clk_en_i   => '1',
1857
    rd_reset_i    => opc_rd_reset_i,        --s_fif_dat_rd_clear,  -- Synchronous
1858
    rd_en_i       => opc_fif_ren_i,         --s_fif_rd,
1859
    rd_dat_o      => opc_fif_dat_o,         --s_fif_dat_rd,
1860
    rd_fifo_level => opc_rd_cnt_o,          --s_fif_dat_rd_level,
1861
    rd_fifo_full  => open,                  --s_fif_rd_full,
1862
    rd_fifo_empty => opc_fif_mt_o,          --s_fif_rd_empty,
1863
 
1864
    -- Data Pipe RAM
1865
    ram_clk_i     => sys_clk,
1866
    ram_clk_en_i  => '1',
1867
    ram_adr_i     => syscon_adr(MMC_RAM_ADR_BITS-1 downto 0),
1868
    ram_we_i      => s_ram_we,
1869
    ram_dat_i     => syscon_dat_wr(7 downto 0),
1870
    ram_dat_o     => s_ram_dat_rd
1871
  );
1872
  s_fif_sel <= '1' when syscon_cyc='1' and t_reg_sel='1' and syscon_adr(3 downto 0)=16#F# else '0';
1873
--  s_fif_rd  <= '1' when syscon_we='0' and s_fif_sel='1' else '0';
1874
--  s_fif_we  <= '1' when syscon_we='1' and s_fif_sel='1' else '0';
1875
  s_ram_we  <= '1' when syscon_we='1' and s_ram_sel='1' else '0';
1876
 
1877
-- system side BRAM ack signal needs to be delayed by 1 cycle,
1878
-- to allow for the BRAM to respond to the given address.
1879
process(sys_rst_n,sys_clk)
1880
begin
1881
  if (sys_rst_n='0') then
1882
    s_ram_ack <= '0';
1883
  elsif (sys_clk'event and sys_clk='1') then
1884
    s_ram_ack <= s_ram_sel;
1885
  end if;
1886
end process;
1887
 
1888
-- Formulate function enable signals
1889
  host_enable  <= host_en_i  or host_en_reg;
1890
  slave_enable <= slave_en_i or slave_en_reg;
1891
 
1892
-- Use output enables to steer MMC signaling
1893
 
1894
  mmc_clk_o     <= host_clk_o_pad;
1895
  mmc_clk_oe_o  <= '1' when host_enable='1' else '0';
1896
  mmc_cmd_o     <= (host_cmd_o or not host_enable) and (slave_cmd_o or not slave_enable);
1897
  mmc_cmd_oe_o  <= (host_cmd_oe_o and host_enable) or (slave_cmd_oe_o and slave_enable);
1898
  mmc_dat_o     <= host_dat_o   when (host_dat_oe_o='1' and host_enable='1') else
1899
                   slave_dat_o  when (slave_dat_oe_o='1' and slave_enable='1') else
1900
                   (others=>'1');
1901
  mmc_dat_oe_o  <= (host_dat_oe_o and host_enable) or (slave_dat_oe_o and slave_enable);
1902
  mmc_od_mode_o <= slave_od_mode_o when slave_enable='1' else '0'; -- Open drain mode
1903
  mmc_dat_siz_o <= host_dat_siz_o  when (host_dat_oe_o='1' and host_enable='1') else
1904
                   slave_dat_siz_o when (slave_dat_oe_o='1' and slave_enable='1') else
1905
                   (others=>'0');
1906
 
1907
  host_cmd_i  <= mmc_cmd_i when host_enable='1' and host_cmd_oe_o='0' else '1';
1908
  slave_cmd_i <= mmc_cmd_i when slave_enable='1' and slave_cmd_oe_o='0' else '1';
1909
  host_dat_i  <= mmc_dat_i when host_enable='1' and host_dat_oe_o='0' else (others=>'1');
1910
  slave_dat_i <= mmc_dat_i when slave_enable='1' and slave_dat_oe_o='0' else (others=>'1');
1911
  slave_clk_i <= mmc_clk_i;
1912
 
1913
  test_cmd_i  <= mmc_cmd_i;
1914
 
1915
end beh;

powered by: WebSVN 2.1.0

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