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

Subversion Repositories sd_mmc_emulator

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jclaytons
--------------------------------------------------------------------------
2
-- Package containing SD Card interface modules,
3
-- and related support modules.
4
--
5
 
6
library IEEE;
7
use IEEE.STD_LOGIC_1164.ALL;
8
use IEEE.NUMERIC_STD.ALL;
9
 
10
package sd_card_pack is
11
 
12
  component sd_card_cmd_rx
13
  port(
14
    -- Asynchronous reset
15
    sys_rst_n   : in  std_logic;
16
    -- SD/MMC card command signals
17
    sd_clk_i    : in  std_logic;
18
    sd_cmd_i    : in  std_logic;
19
    -- Command outputs
20
    cmd_raw_o   : out unsigned(47 downto 0);
21
    cmd_index_o : out unsigned(5 downto 0);
22
    cmd_arg_o   : out unsigned(31 downto 0);
23
    -- Status and done indicator
24
    cmd_done_o  : out std_logic;
25
    crc_err_o   : out std_logic;
26
    dir_err_o   : out std_logic;
27
    stop_err_o  : out std_logic
28
  );
29
  end component;
30
 
31
  component sd_card_responder
32
  generic (
33
    N_CR          : integer; -- Number of clocks between respond_i and response start
34
    RESP_PYLD_LEN : integer;
35
    CRC_OFFSET    : integer; -- Start CRC calculation after this many bits
36
    CRC_SEND_ONES : integer
37
  );
38
  port (
39
    -- Asynchronous reset
40
    sys_rst_n     : in  std_logic;
41
    -- SD/MMC card command signals
42
    sd_clk_i      : in  std_logic;
43
    sd_cmd_o      : out std_logic;
44
    sd_cmd_oe_o   : out std_logic;
45
    -- Response inputs
46
    resp_index_i  : in  unsigned(5 downto 0);
47
    resp_pyld_i   : in  unsigned(RESP_PYLD_LEN-1 downto 0);
48
    respond_i     : in  std_logic;
49
    -- Status and done indicator
50
    done_o        : out std_logic; -- A one clock long pulse
51
    busy_o        : out std_logic
52
  );
53
  end component;
54
 
55
  component sd_card_data_unit
56
  generic (
57
    BLK_PRG_TIME  : integer; -- Number of clocks to program a sector in FLASH (emulated)
58
    BLKSIZE_W     : integer;
59
    BLKCNT_W      : integer
60
  );
61
  port(
62
    sd_clk_i      : in  std_logic;
63
    sys_rst_n     : in  std_logic;
64
    --Tx Fifo
65
    tx_dat_i      : in  unsigned(7 downto 0);
66
    tx_dat_rd_o   : out std_logic;
67
    --Rx Fifo
68
    rx_dat_o      : out unsigned(7 downto 0);
69
    rx_dat_we_o   : out std_logic;
70
    --SD data
71
    sd_dat_i      : in  unsigned(7 downto 0);
72
    sd_dat_o      : out unsigned(7 downto 0);
73
    sd_dat_oe_o   : out  std_logic;
74
    --Control signals
75
    blksize_i     : in  unsigned(BLKSIZE_W-1 downto 0);
76
    bus_size_i    : in  unsigned(1 downto 0);
77
    blkcnt_i      : in  unsigned(BLKCNT_W-1 downto 0);
78
    continuous_i  : in  std_logic;
79
    d_stop_i      : in  std_logic;
80
    d_read_i      : in  std_logic;
81
    d_write_i     : in  std_logic;
82
    bustest_w_i   : in  std_logic;
83
    bustest_r_i   : in  std_logic;
84
    sd_dat_busy_o : out std_logic;
85
    fsm_busy_o    : out std_logic;
86
    crc_ok_o      : out std_logic
87
  );
88
  end component;
89
 
90
  component sd_card_emulator
91
  generic (
92
    USE_R4_RESPONSE      : integer; -- Fast I/O read/write (app specific)
93
    USE_R5_RESPONSE      : integer; -- Interrupt Request Mode
94
    EXT_CSD_INIT_FILE    : string; -- Initial contents of EXT_CSD
95
    OCR_USE_DUAL_VOLTAGE : integer;
96
    OCR_USE_SECTOR_MODE  : integer;
97
    CID_MID    : unsigned( 7 downto 0); -- Manufacturer ID
98
    CID_OID    : unsigned( 7 downto 0); -- OEM ID
99
    CID_CBX    : unsigned( 1 downto 0); -- 0=Card, 1=BGA, 2=Package On Package
100
    CID_PNM    : unsigned(47 downto 0); -- Product Name, 6 ASCII chars
101
    CID_PRV    : unsigned( 7 downto 0); -- Product Rev (2 BCD digits, e.g. 6.2=0x62)
102
    CID_PSN    : unsigned(31 downto 0); -- Product serial number
103
    CID_MDT    : unsigned( 7 downto 0); -- Manufacture Date (Jan=1, 1997=0, e.g. Apr. 2000=0x43)
104
    DEF_STAT   : unsigned(31 downto 0); -- Read Write, R_0
105
    CSD_WORD_3 : unsigned(31 downto 0); -- Read only
106
    CSD_WORD_2 : unsigned(31 downto 0); -- Read only
107
    CSD_WORD_1 : unsigned(31 downto 0); -- Read only
108
    CSD_WORD_0 : unsigned(31 downto 0); -- (31:16) is read only, (15:0) is R_1 default (R/W)
109
    DEF_R_Z    : unsigned(31 downto 0)  -- Value returned for nonexistent registers
110
  );
111
  port (
112
 
113
    -- Asynchronous reset
114
    sys_rst_n     : in  std_logic;
115
    sys_clk       : in  std_logic;
116
 
117
    -- Bus interface
118
    adr_i         : in  unsigned(3 downto 0);
119
    sel_i         : in  std_logic;
120
    we_i          : in  std_logic;
121
    dat_i         : in  unsigned(31 downto 0);
122
    dat_o         : out unsigned(31 downto 0);
123
    ack_o         : out std_logic;
124
 
125
    -- SD/MMC card signals
126
    sd_clk_i      : in  std_logic;
127
    sd_cmd_i      : in  std_logic;
128
    sd_cmd_o      : out std_logic;
129
    sd_cmd_oe_o   : out std_logic;
130
    sd_od_mode_o  : out std_logic; -- Open drain mode
131
    sd_dat_i      : in  unsigned(7 downto 0);
132
    sd_dat_o      : out unsigned(7 downto 0);
133
    sd_dat_oe_o   : out std_logic;
134
    sd_dat_siz_o  : out unsigned(1 downto 0);
135
 
136
    -- Data FIFO interface
137
    buf_adr_o     : out unsigned(31 downto 0);
138
    buf_dat_o     : out unsigned(7 downto 0);
139
    buf_dat_we_o  : out std_logic;
140
    buf_dat_i     : in  unsigned(7 downto 0);
141
    buf_dat_rd_o  : out std_logic
142
  );
143
  end component;
144
 
145
  component mmc_data_pipe
146
  generic (
147
    EXT_CSD_INIT_FILE : string; -- Initial contents of EXT_CSD
148
    FIFO_DEPTH        : integer;
149
    FILL_LEVEL_BITS   : integer; -- Should be at least int(floor(log2(FIFO_DEPTH))+1.0)
150
    RAM_ADR_WIDTH     : integer
151
  );
152
  port (
153
 
154
    -- Asynchronous reset
155
    sys_rst_n     : in  std_logic;
156
    sys_clk       : in  std_logic;
157
 
158
    -- Bus interface
159
    adr_i         : in  unsigned(3 downto 0);
160
    sel_i         : in  std_logic;
161
    we_i          : in  std_logic;
162
    dat_i         : in  unsigned(31 downto 0);
163
    dat_o         : out unsigned(31 downto 0);
164
    ack_o         : out std_logic;
165
 
166
    -- SD/MMC card signals
167
    mmc_clk_i     : in  std_logic;
168
    mmc_cmd_i     : in  std_logic;
169
    mmc_cmd_o     : out std_logic;
170
    mmc_cmd_oe_o  : out std_logic;
171
    mmc_od_mode_o : out std_logic; -- Open drain mode
172
    mmc_dat_i     : in  unsigned(7 downto 0);
173
    mmc_dat_o     : out unsigned(7 downto 0);
174
    mmc_dat_oe_o  : out std_logic;
175
    mmc_dat_siz_o : out unsigned(1 downto 0);
176
 
177
    -- Data Pipe FIFOs
178
    wr_clk_i      : in  std_logic;
179
    wr_clk_en_i   : in  std_logic;
180
    wr_reset_i    : in  std_logic;  -- Synchronous
181
    wr_en_i       : in  std_logic;
182
    wr_dat_i      : in  unsigned(7 downto 0);
183
    wr_fifo_level : out unsigned(FILL_LEVEL_BITS-1 downto 0);
184
    wr_fifo_full  : out std_logic;
185
    wr_fifo_empty : out std_logic;
186
 
187
    rd_clk_i      : in  std_logic;
188
    rd_clk_en_i   : in  std_logic;
189
    rd_reset_i    : in  std_logic;  -- Synchronous
190
    rd_en_i       : in  std_logic;
191
    rd_dat_o      : out unsigned(7 downto 0);
192
    rd_fifo_level : out unsigned(FILL_LEVEL_BITS-1 downto 0);
193
    rd_fifo_full  : out std_logic;
194
    rd_fifo_empty : out std_logic;
195
 
196
    -- Data Pipe RAM
197
    ram_clk_i     : in  std_logic;
198
    ram_clk_en_i  : in  std_logic;
199
    ram_adr_i     : in  unsigned(RAM_ADR_WIDTH-1 downto 0);
200
    ram_we_i      : in  std_logic;
201
    ram_dat_i     : in  unsigned(7 downto 0);
202
    ram_dat_o     : out unsigned(7 downto 0)
203
 
204
  );
205
  end component;
206
 
207
end sd_card_pack;
208
 
209
package body sd_card_pack is
210
end sd_card_pack;
211
 
212
 
213
-------------------------------------------------------------------------------
214
-- SD/MMC Card Command Receiver
215
-------------------------------------------------------------------------------
216
--
217
-- Author: John Clayton
218
-- Update: Mar. 24, 2016 Wrote description and initial code
219
--
220
-- Description
221
-------------------------------------------------------------------------------
222
-- This module is meant to be part of a system that emulates an SD/MMC card.
223
--
224
-- This module clocks incoming serial command bits into a 48 bit shift
225
-- register.  It starts when a '0' (start) bit is found, and then shifts in
226
-- 47 additional bits.  The expected format of the command is:
227
--
228
-- 0 1 [index] [arg] [crc] 1
229
--
230
-- Where:
231
--   index = 6 bits
232
--   arg   = 32 bits
233
--   crc   = 7  bits
234
--
235
-- It checks that the second bit is a '1' indicating that
236
-- the command is from the host to the card.  It also checks that the last bit
237
-- is a '1' (stop) bit.  The seven bits immediately prior to the stop bit are
238
-- checked using a CRC-7 code.
239
--
240
-- If any of the checks does not pass, the associated error bits are set, and
241
-- the cmd_ outputs remain unchanged.  If all checks pass, then the newly
242
-- received command contents are stored into the cmd_ outputs, and the
243
-- cmd_rx_done_o output is pulsed high for one sd_clk_i cycle.
244
--
245
-- Note that this receiver runs entirely within the sd_clk_i clock domain.
246
-- Therefore, care must be taken when using the outputs.  A FIFO can form
247
-- a natural "clock domain boundary crossing" or the user may need to
248
-- implement other special handshaking to safely transfer signals into a
249
-- different clock domain.
250
--
251
 
252
library IEEE;
253
use IEEE.STD_LOGIC_1164.ALL;
254
use IEEE.NUMERIC_STD.ALL;
255
 
256
library work;
257
use work.ucrc_pack.all;
258
 
259
entity sd_card_cmd_rx is
260
  port (
261
    -- Asynchronous reset
262
    sys_rst_n   : in  std_logic;
263
    -- SD/MMC card command signals
264
    sd_clk_i    : in  std_logic;
265
    sd_cmd_i    : in  std_logic;
266
    -- Command outputs
267
    cmd_raw_o   : out unsigned(47 downto 0);
268
    cmd_index_o : out unsigned(5 downto 0);
269
    cmd_arg_o   : out unsigned(31 downto 0);
270
    -- Status and done indicator
271
    cmd_done_o  : out std_logic;
272
    crc_err_o   : out std_logic;
273
    dir_err_o   : out std_logic;
274
    stop_err_o  : out std_logic
275
  );
276
end sd_card_cmd_rx;
277
 
278
architecture beh of sd_card_cmd_rx is
279
 
280
  -- CRC related
281
signal crc_clk    : std_logic;
282
signal crc_clr    : std_logic;
283
signal crc_match  : std_logic;
284
signal crc_val    : unsigned(6 downto 0);
285
  -- Related to the incoming command
286
signal counter    : unsigned(5 downto 0);
287
signal rx_sr      : unsigned(45 downto 0);
288
signal cmd_raw_l  : unsigned(47 downto 0);
289
 
290
begin
291
 
292
--------------------------------------------
293
-- CRC generator
294
 
295
  crc0 : ucrc_ser
296
  generic map (
297
    POLYNOMIAL => "0001001",
298
    INIT_VALUE => "0000000"
299
  )
300
  port map (
301
    -- System clock and asynchronous reset
302
    sys_clk    => crc_clk,
303
    sys_rst_n  => sys_rst_n,
304
    sys_clk_en => '1',
305
 
306
    -- Input and Control
307
    clear_i    => crc_clr,
308
    data_i     => sd_cmd_i,
309
    flush_i    => '0',
310
 
311
    -- Output
312
    match_o    => crc_match,
313
    crc_o      => crc_val
314
  );
315
  -- Falling edge was used for a while...
316
  -- Rising edge is now being used, per the specification.
317
  --crc_clk <= not sd_clk_i;
318
  crc_clk <= sd_clk_i;
319
  crc_clr <= '1' when counter=0 else '0';
320
 
321
 
322
--------------------------------------------
323
-- Command receiver
324
 
325
command_rx_proc : process(sys_rst_n, sd_clk_i)
326
begin
327
  if (sys_rst_n='0') then
328
    counter <= (others=>'0');
329
    rx_sr <= (others=>'0');
330
    cmd_raw_l <= (others=>'1');
331
--  elsif (sd_clk_i'event and sd_clk_i='0') then -- falling edge is used, when data is stable.
332
  elsif (sd_clk_i'event and sd_clk_i='1') then -- rising edge is used, per the specification.
333
    -- The shift register bit is the only synchronization flip flop
334
    rx_sr(0)  <= sd_cmd_i;
335
    rx_sr(45 downto 1) <= rx_sr(44 downto 0);
336
    -- Decrement the counter when it is non-zero
337
    if (counter>0) then
338
      counter <= counter-1;
339
    end if;
340
    -- Load the counter when a start bit is seen
341
    if (counter=0 and sd_cmd_i='0') then
342
      counter <= to_unsigned(47,counter'length);
343
    end if;
344
    -- Store the output when the counter is expiring
345
    if (counter=2) then
346
      cmd_raw_l <= rx_sr & sd_cmd_i & '1';
347
    end if;
348
  end if;
349
end process;
350
 
351
-- Provide output signals
352
cmd_done_o <= '1' when (counter=1 and crc_match='1' and cmd_raw_l(46)='1' and sd_cmd_i='1') else '0';
353
stop_err_o <= '1' when (counter=1 and sd_cmd_i='0') else '0';
354
dir_err_o  <= '1' when (counter=1 and cmd_raw_l(46)='0') else '0';
355
crc_err_o  <= '1' when (counter=1 and crc_match='0') else '0';
356
 
357
-- Split out fields of the command
358
cmd_raw_o   <= cmd_raw_l;
359
cmd_index_o <= cmd_raw_l(45 downto 40);
360
cmd_arg_o   <= cmd_raw_l(39 downto 8);
361
 
362
end beh;
363
 
364
 
365
-------------------------------------------------------------------------------
366
-- SD/MMC Card Command Responder
367
-------------------------------------------------------------------------------
368
--
369
-- Author: John Clayton
370
-- Update: Mar. 30, 2016 Wrote description and initial code
371
--         Apr. 28, 2016 Added N_CR generic
372
--
373
-- Description
374
-------------------------------------------------------------------------------
375
-- This module is meant to be part of a system that emulates an SD/MMC card.
376
--
377
-- This module latches parallel data into a shift register, and then shifts
378
-- the data out as a command response, using the sd_clk_i input as the clock.
379
--
380
-- The process of sending a response begins when the respond_i input is driven
381
-- high.  The generic N_CR determines how many clocks occur between the
382
-- respond_i pulse, and the actual start bit of the response.  However,
383
-- per the SD/MMC card specifications, there are supposed to be two clock
384
-- periods of tri-state bus "turnaround time" following the stop bit of the
385
-- command.  After the bus turnaround time, then the card pulls up the
386
-- command line, until the full N_CR time has expired.  The value of N_CR
387
-- must not be set below 5 to comply with the minimum values in the SD/MMC
388
-- specification.  If the respond_i input occurs in the clock following the
389
-- command stop bit, then a value of 4 is the lowest permitted N_CR...
390
--
391
-- According to the SD/MMC card specification, there are several different
392
-- types of card responses:
393
--
394
--  type   length  structure
395
--  ----   ------  ----------------------------------------------------------
396
--   R1 =  48 bit, ("00" & 6 bit index & 32 bit card status & 7 bit CRC & '1')
397
--   R2 = 136 bit, ("00111111" & 120 bit CID or CSD & 7 bit CRC & '1')
398
--   R3 =  48 bit, ("00111111" & 32 bit OCR & "11111111")
399
--   R4 =  48 bit, ("00100111" & 16 bit RCA & 1 bit status & 7 bit reg addr & 8 bit reg read data & 7 bit CRC & '1')
400
--   R5 =  48 bit, ("00101000" & 16 bit RCA & 16 bit undefined & 7 bit CRC & '1')
401
--
402
-- The length of the shift register is determined by a generic parameter,
403
-- RESP_PYLD_LEN, according to the following formula:
404
--
405
--   shift_reg_length = 8 + RESP_PYLD_LEN + 8 = 16 + RESP_PYLD_LEN
406
--
407
-- Thus, for the 48 bit response, RESP_PYLD_LEN is set to 32, and for the
408
-- 136 bit response, RESP_PYLD_LEN is set to 120.
409
--
410
-- This module automatically populates the first two bits of the reply with
411
-- "00", Because all of the replies begin with "00."  The next 6 bits are
412
-- supplied by the "resp_index_i" signal.  After that, the next set of bits
413
-- is supplied by the signal "resp_pyld_i" which stands for 
414
-- "response payload."  Since the payload could be either 32 bits or 120 bits
415
-- long, the payload length is set by the RESP_PYLD_LEN generic.
416
--
417
-- This module contains a CRC unit which calculates the 7 CRC bits to place at
418
-- the end, and the final stop bit is also sent out automatically.
419
--
420
-- Whenever the responder is idle, asserting the respond_i input causes the
421
-- response to be latched immediately, and the first bit of the newly
422
-- requested reponse is sent out on the following clock cycle.  If a response
423
-- is already being actively sent out, the respond_i input is simply ignored.
424
--
425
-- It is envisioned that several instances of the responder may be used in
426
-- parallel, each being connected to a different SD/MMC card register.
427
-- Therefore, coordination of the response signals needs to be done in a
428
-- higher level module, possibly through some sort of data selector or "mux"
429
-- to determine which sd_cmd_o and sd_cmd_oe_o outputs get used, and also
430
-- perhaps through logic to ensure that only the desired respond_i input is
431
-- asserted.  This may seem somewhat "messy," but it was done with the idea
432
-- in mind that certain types of responses could be easily left out of the
433
-- design completely, by eliminating the associated responder instance.
434
-- The higher level module is also responsible for determining the timing
435
-- between receipt of a command, and the start of the response.
436
-- For instance, the SD/MMC card standard specifies that for identification
437
-- responses, exactly 5 clock cycles should exist between the stop bit of
438
-- the command, and the start bit of the response.
439
-- 
440
-- Note that this responder runs entirely within the sd_clk_i clock domain.
441
-- Therefore, care must be taken when supplying the inputs.  A FIFO can form
442
-- a natural "clock domain boundary crossing" or the user may need to
443
-- implement other special handshaking to safely receive signals from a
444
-- different clock domain.
445
--
446
 
447
library IEEE;
448
use IEEE.STD_LOGIC_1164.ALL;
449
use IEEE.NUMERIC_STD.ALL;
450
 
451
library work;
452
use work.ucrc_pack.all;
453
use work.convert_pack.all;
454
 
455
entity sd_card_responder is
456
  generic (
457
    N_CR          : integer :=  5; -- Number of clocks between respond_i and response start
458
    RESP_PYLD_LEN : integer := 32;
459
    CRC_OFFSET    : integer := 0; -- Start CRC calculation after this many bits
460
    CRC_SEND_ONES : integer := 0
461
  );
462
  port (
463
    -- Asynchronous reset
464
    sys_rst_n     : in  std_logic;
465
    -- SD/MMC card command signals
466
    sd_clk_i      : in  std_logic;
467
    sd_cmd_o      : out std_logic;
468
    sd_cmd_oe_o   : out std_logic;
469
    -- Response inputs
470
    resp_index_i  : in  unsigned(5 downto 0);
471
    resp_pyld_i   : in  unsigned(RESP_PYLD_LEN-1 downto 0);
472
    respond_i     : in  std_logic;
473
    -- Status and done indicator
474
    done_o        : out std_logic; -- A one clock long pulse
475
    busy_o        : out std_logic
476
  );
477
end sd_card_responder;
478
 
479
architecture beh of sd_card_responder is
480
 
481
  -- CRC related
482
signal crc_clk_en : std_logic;
483
signal crc_clr    : std_logic;
484
signal crc_val    : unsigned(6 downto 0);
485
signal crc_enable : std_logic;
486
  -- Related to the response
487
signal n_cr_count : unsigned(timer_width(N_CR)-1 downto 0);
488
signal counter    : unsigned(7 downto 0);
489
signal tx_sr      : unsigned(RESP_PYLD_LEN+7 downto 0);
490
signal tail_end   : unsigned(8 downto 0);
491
 
492
begin
493
 
494
--------------------------------------------
495
-- CRC generator
496
 
497
  crc0 : ucrc_ser
498
  generic map (
499
    POLYNOMIAL => "0001001",
500
    INIT_VALUE => "0000000"
501
  )
502
  port map (
503
    -- System clock and asynchronous reset
504
    sys_clk    => sd_clk_i,
505
    sys_rst_n  => sys_rst_n,
506
    sys_clk_en => crc_clk_en,
507
 
508
    -- Input and Control
509
    clear_i    => crc_clr,
510
    data_i     => tx_sr(tx_sr'length-1),
511
    flush_i    => '0',
512
 
513
    -- Output
514
    match_o    => open,
515
    crc_o      => crc_val
516
  );
517
  crc_clk_en <= '1' when counter=0 or counter>8 else '0';
518
  crc_clr <= '1' when counter=0 or counter>(RESP_PYLD_LEN+16-CRC_OFFSET) else '0';
519
  -- Attach a stop bit to the CRC code, and another for easy indexing by the main counter
520
  tail_end <= crc_val & "11" when CRC_SEND_ONES=0 else "111111111";
521
 
522
--------------------------------------------
523
-- Response Transmitter
524
 
525
response_tx_proc : process(sys_rst_n, sd_clk_i)
526
begin
527
  if (sys_rst_n='0') then
528
    n_cr_count  <= (others=>'0');
529
    counter     <= (others=>'0');
530
    tx_sr       <= (others=>'1');
531
  elsif (sd_clk_i'event and sd_clk_i='1') then
532
    -- Default values
533
 
534
    -- Left shifting shift register
535
    tx_sr <= tx_sr(tx_sr'length-2 downto 0) & '0';
536
    -- Handle the N_CR counter, an incrementing counter
537
    if (n_cr_count>0) then
538
      n_cr_count <= n_cr_count+1;
539
    end if;
540
    -- Decrement the main counter when it is non-zero
541
    if (counter>0) then
542
      counter <= counter-1;
543
    end if;
544
    -- Load the N_CR counter when a start signal is seen
545
    if (counter=0 and n_cr_count=0 and respond_i='1') then
546
      n_cr_count <= to_unsigned(1,n_cr_count'length);
547
    end if;
548
    -- Load the main counter when the N_CR counter matures
549
    if (counter=0 and n_cr_count=N_CR) then
550
      n_cr_count <= (others=>'0');
551
      counter <= to_unsigned(RESP_PYLD_LEN+16,counter'length);
552
      tx_sr <= "00" & resp_index_i & resp_pyld_i;
553
    end if;
554
  end if;
555
end process;
556
 
557
done_o <= '1' when counter=1 else '0';
558
busy_o <= '1' when counter>0 else '0';
559
-- In our current implementation, there are already two cycles of
560
-- tri-state bus turnaround time, therefore and additional two cycles
561
-- is not required.  Uncomment this line for implementations where
562
-- the bus turnaround needs to be explicitly created.
563
--sd_cmd_oe_o <= '1' when n_cr_count>2 or counter>0 else '0';
564
sd_cmd_oe_o <= '1' when n_cr_count>0 or counter>0 else '0';
565
sd_cmd_o <= tx_sr(tx_sr'length-1) when counter>8 else
566
            tail_end(to_integer(counter));
567
 
568
end beh;
569
 
570
 
571
-------------------------------------------------------------------------------
572
-- SD/MMC Card Data Unit
573
-------------------------------------------------------------------------------
574
--
575
-- Author: John Clayton
576
-- Update: Apr. 14, 2016 Wrote description and initial code
577
--         June  9, 2016 Added bustest_w_i and bustest_r_i signals
578
--
579
-- Description
580
-------------------------------------------------------------------------------
581
-- This module is meant to emulate the data handler of an SD/MMC card.
582
--
583
-- This module accepts SD/MMC card data transfers, and generates return
584
-- transfers for sending data to the cardbus host.
585
--
586
-- Most of this unit runs entirely within the sd_clk_i clock domain.  The FIFO
587
-- data storage buffers form a natural place at which to interface between
588
-- clock domains, and the registers are also read and written from a separate
589
-- clock domain (sys_clk).
590
--
591
--
592
 
593
library IEEE;
594
use IEEE.STD_LOGIC_1164.ALL;
595
use IEEE.NUMERIC_STD.ALL;
596
 
597
library work;
598
use work.ucrc_pack.all;
599
use work.sd_host_pack.all;
600
use work.convert_pack.all;
601
 
602
entity sd_card_data_unit is
603
  generic (
604
    BLK_PRG_TIME  : integer := 400; -- Number of clocks to program a sector in FLASH (emulated)
605
    BLKSIZE_W     : integer := 12;
606
    BLKCNT_W      : integer := 16
607
  );
608
  port(
609
    sd_clk_i      : in  std_logic;
610
    sys_rst_n     : in  std_logic;
611
    --Tx Fifo
612
    tx_dat_i      : in  unsigned(7 downto 0);
613
    tx_dat_rd_o   : out std_logic;
614
    --Rx Fifo
615
    rx_dat_o      : out unsigned(7 downto 0);
616
    rx_dat_we_o   : out std_logic;
617
    --SD data
618
    sd_dat_i      : in  unsigned(7 downto 0);
619
    sd_dat_o      : out unsigned(7 downto 0);
620
    sd_dat_oe_o   : out  std_logic;
621
    --Control signals
622
    blksize_i     : in  unsigned(BLKSIZE_W-1 downto 0);
623
    bus_size_i    : in  unsigned(1 downto 0);
624
    blkcnt_i      : in  unsigned(BLKCNT_W-1 downto 0);
625
    continuous_i  : in  std_logic;
626
    d_stop_i      : in  std_logic;
627
    d_read_i      : in  std_logic;
628
    d_write_i     : in  std_logic;
629
    bustest_w_i   : in  std_logic;
630
    bustest_r_i   : in  std_logic;
631
    sd_dat_busy_o : out std_logic;
632
    fsm_busy_o    : out std_logic;
633
    crc_ok_o      : out std_logic
634
  );
635
end sd_card_data_unit;
636
 
637
architecture beh of sd_card_data_unit is
638
 
639
-- Internal constants
640
constant BLK_PRG_CBITS : natural := timer_width(BLK_PRG_TIME);
641
 
642
-- Internal signals
643
signal dat_reg      : unsigned(7 downto 0);
644
signal data_cycles  : unsigned(BLKSIZE_W+2 downto 0);
645
signal bus_size_reg : unsigned(1 downto 0);
646
--CRC16
647
signal crc_in     : unsigned(7 downto 0);
648
signal crc_enable : std_logic;
649
signal crc_rst_n  : std_logic;
650
type crc_out_type is
651
  array (integer range 0 to 7) of unsigned(15 downto 0);
652
signal crc_out    : crc_out_type;
653
signal crc_ok_l   : std_logic;
654
signal transf_cnt : unsigned(15 downto 0);
655
  --State Machine
656
type FSM_STATE_TYPE is (IDLE, SEND_DAT, READ_WAIT, READ_DAT, SEND_BUSTEST,
657
                        READ_BUSTEST_WAIT, READ_BUSTEST, CRC_ACK, CRC_NACK,
658
                        RECV_BUSY);
659
signal state : FSM_STATE_TYPE;
660
 
661
signal busy_int    : std_logic;
662
signal blkcnt_reg  : unsigned(BLKCNT_W-1 downto 0);
663
signal start_bit   : std_logic;
664
signal crc_c       : unsigned(4 downto 0);
665
signal crc_status  : unsigned(3 downto 0);
666
signal data_index  : unsigned(2 downto 0);
667
signal last_din    : unsigned(7 downto 0);
668
 
669
signal bustest_0   : unsigned(7 downto 0);
670
signal bustest_1   : unsigned(7 downto 0);
671
signal blk_prg_tmr : unsigned(BLK_PRG_CBITS-1 downto 0);
672
signal d_stop_pending : std_logic;
673
 
674
begin
675
 
676
--sd data input pad register
677
process(sys_rst_n,sd_clk_i)
678
begin
679
  if (sys_rst_n='0') then
680
    dat_reg <= (others=>'0');
681
  elsif (sd_clk_i'event and sd_clk_i='1') then
682
    dat_reg <= sd_dat_i;
683
  end if;
684
end process;
685
 
686
-- There are eight different CRC generators
687
sd_crc_gen : for nvar in 0 to 7 generate
688
begin
689
  crc_unit : ucrc_ser
690
  generic map (
691
    POLYNOMIAL => "0001000000100001",
692
    INIT_VALUE => "0000000000000000"
693
  )
694
  port map (
695
    -- System clock and asynchronous reset
696
    sys_clk    => sd_clk_i,
697
    sys_rst_n  => crc_rst_n,
698
    sys_clk_en => crc_enable,
699
 
700
    -- Input and Control
701
    clear_i    => '0',
702
    data_i     => crc_in(nvar),
703
    flush_i    => '0',
704
 
705
    -- Output
706
    match_o    => open,
707
    crc_o      => crc_out(nvar)
708
  );
709
end generate;
710
 
711
crc_ok_o   <= crc_ok_l;
712
fsm_busy_o <= '1' when (state/=IDLE) else '0';
713
start_bit  <= '1' when (dat_reg(0)='0') else '0';
714
sd_dat_busy_o <= '1' when (state=RECV_BUSY) else '0';
715
 
716
fsm_proc : process(sys_rst_n,sd_clk_i)
717
begin
718
  if (sys_rst_n='0') then
719
    state <= IDLE;
720
    sd_dat_oe_o <= '0';
721
    crc_enable <= '0';
722
    crc_rst_n <= '0';
723
    transf_cnt <= (others=>'0');
724
    tx_dat_rd_o <= '0';
725
    last_din <= (others=>'0');
726
    crc_c <= (others=>'0');
727
    crc_status <= (others=>'0');
728
    crc_in <= (others=>'0');
729
    sd_dat_o <= (others=>'0');
730
    rx_dat_we_o <= '0';
731
    rx_dat_o <= (others=>'0');
732
    crc_ok_l <= '0';
733
    busy_int <= '0';
734
    data_index <= (others=>'0');
735
    blkcnt_reg <= (others=>'0');
736
    data_cycles <= (others=>'0');
737
    bus_size_reg <= (others=>'0');
738
    bustest_0 <= (others=>'0');
739
    bustest_1 <= (others=>'0');
740
    blk_prg_tmr <= (others=>'0');
741
    d_stop_pending <= '0';
742
  elsif (sd_clk_i'event and sd_clk_i='1') then
743
    -- Handle block programming timer
744
    if (blk_prg_tmr>0) then
745
      blk_prg_tmr <= blk_prg_tmr-1;
746
    end if;
747
    -- Implement finite state machine
748
    case(state) is
749
      when IDLE =>
750
        d_stop_pending <= '0';
751
        sd_dat_oe_o <= '0';
752
        sd_dat_o <= "11111111";
753
        crc_enable <= '0';
754
        crc_rst_n <= '0';
755
        transf_cnt <= (others=>'0');
756
        crc_c <= to_unsigned(16,crc_c'length);
757
        rx_dat_we_o <= '0';
758
        tx_dat_rd_o <= '0';
759
        data_index <= (others=>'0');
760
        blkcnt_reg <= blkcnt_i;
761
        if (bus_size_i=2) then
762
          data_cycles <= "000" & blksize_i; -- (<<0) operation
763
        elsif (bus_size_i=1) then
764
          data_cycles <= "00" & blksize_i & '0'; -- (<<1) operation
765
        else
766
          data_cycles <= blksize_i & "000"; -- (<<3) operation
767
        end if;
768
        bus_size_reg <= bus_size_i;
769
        -- state transition
770
        -- Currently stream based reads and writes are not supported, so if "continuous_i" is
771
        -- asserted, remain in IDLE state
772
        if (continuous_i='0' and d_stop_i='0' and d_read_i='0' and d_write_i='1' and bustest_w_i='0' and bustest_r_i='0') then
773
          state <= SEND_DAT;
774
          -- For the case of 8-bit bus width, provide early read signal, so that
775
          -- synchronous RAMs, such as BRAMs and BRAM based FIFOs have a chance to
776
          -- respond in time.
777
          if (bus_size_reg=2) then
778
            tx_dat_rd_o <= '1';
779
          end if;
780
        elsif  (continuous_i='0' and d_stop_i='0' and d_read_i='1' and d_write_i='0' and bustest_w_i='0' and bustest_r_i='0') then
781
          state <= READ_WAIT;
782
        elsif  (continuous_i='0' and d_stop_i='0' and d_read_i='0' and d_write_i='0' and bustest_w_i='0' and bustest_r_i='1') then
783
          data_cycles <= to_unsigned(24,data_cycles'length);
784
          state <= SEND_BUSTEST;
785
        elsif  (continuous_i='0' and d_stop_i='0' and d_read_i='0' and d_write_i='0' and bustest_w_i='1' and bustest_r_i='0') then
786
          state <= READ_BUSTEST_WAIT;
787
        end if;
788
 
789
      when SEND_BUSTEST =>
790
        transf_cnt <= transf_cnt+1;
791
        tx_dat_rd_o <= '0';
792
        -- Send out start bits
793
        if (transf_cnt = 1) then
794
          sd_dat_oe_o <= '1';
795
          sd_dat_o <= "00000000";
796
        end if;
797
        if (transf_cnt = 2) then
798
          sd_dat_o <= bustest_0;
799
        end if;
800
        if (transf_cnt = 3) then
801
          sd_dat_o <= bustest_1;
802
        end if;
803
        if ((transf_cnt >= 4) and (transf_cnt < data_cycles)) then
804
          sd_dat_o <= "00000000";
805
        end if;
806
        if (transf_cnt = data_cycles) then -- stop bits
807
          sd_dat_o <= "11111111";
808
        end if;
809
        if (transf_cnt = data_cycles+1) then
810
          sd_dat_oe_o <= '0';
811
        end if;
812
        -- state transition
813
        if ((d_stop_i='1') or (transf_cnt >= data_cycles+1)) then
814
          state <= IDLE;
815
        end if;
816
 
817
      when READ_BUSTEST_WAIT =>
818
        sd_dat_oe_o <= '0';
819
        transf_cnt <= (others=>'0');
820
        -- state transition
821
        if (d_stop_i='1') then -- signal for stopping
822
          state <= IDLE;
823
        elsif (start_bit='1') then
824
          state <= READ_BUSTEST;
825
        end if;
826
 
827
      when READ_BUSTEST =>
828
        transf_cnt <= transf_cnt+1;
829
        if (transf_cnt = 0) then
830
          bustest_0 <= not dat_reg;
831
        end if;
832
        if (transf_cnt = 1) then
833
          bustest_1 <= not dat_reg;
834
        end if;
835
        -- state transition
836
        -- No CRC status response is needed
837
        -- Look for stop bits
838
        if ((d_stop_i='1') or (dat_reg = "11111111")) then
839
          state <= IDLE;
840
        end if;
841
 
842
      when SEND_DAT =>
843
        crc_ok_l <= '0';
844
        transf_cnt <= transf_cnt+1;
845
        tx_dat_rd_o <= '0';
846
        -- Load values for last_din and crc_in
847
        if (bus_size_reg=2) then
848
          last_din <= tx_dat_i;
849
          crc_in <= tx_dat_i;
850
          if (transf_cnt<data_cycles-1) then
851
            tx_dat_rd_o <= '1';
852
          else
853
            tx_dat_rd_o <= '0';
854
          end if;
855
        elsif (bus_size_reg=1) then
856
          last_din <=
857
            "1111" &
858
            tx_dat_i(7-(4*to_integer(data_index(0 downto 0)))) &
859
            tx_dat_i(6-(4*to_integer(data_index(0 downto 0)))) &
860
            tx_dat_i(5-(4*to_integer(data_index(0 downto 0)))) &
861
            tx_dat_i(4-(4*to_integer(data_index(0 downto 0))));
862
          crc_in <=
863
            "1111" &
864
            tx_dat_i(7-(4*to_integer(data_index(0 downto 0)))) &
865
            tx_dat_i(6-(4*to_integer(data_index(0 downto 0)))) &
866
            tx_dat_i(5-(4*to_integer(data_index(0 downto 0)))) &
867
            tx_dat_i(4-(4*to_integer(data_index(0 downto 0))));
868
          if (transf_cnt<data_cycles) and (data_index(0 downto 0)=1) then
869
            tx_dat_rd_o <= '1';
870
          end if;
871
          if (data_index(0 downto 0)=1) then
872
            data_index <= (others=>'0');
873
          else
874
            data_index <= data_index+1;
875
          end if;
876
        else
877
          last_din <= "1111111" & tx_dat_i(7-to_integer(data_index));
878
          crc_in <= "1111111" & tx_dat_i(7-to_integer(data_index));
879
          if (transf_cnt<data_cycles) and (data_index = 5) then
880
            tx_dat_rd_o <= '1';
881
          end if;
882
          if (data_index = 7) then
883
            data_index <= (others=>'0');
884
          else
885
            data_index <= data_index+1;
886
          end if;
887
        end if;
888
        -- Treat first transfer differently
889
        if (transf_cnt = 1) then
890
          crc_rst_n <= '1';
891
          crc_enable <= '1';
892
          if (bus_size_reg=2) then
893
            last_din <= tx_dat_i;
894
            crc_in <= tx_dat_i;
895
          elsif (bus_size_reg=1) then
896
            last_din <= "1111" & tx_dat_i(7 downto 4);
897
            crc_in <= "1111" & tx_dat_i(7 downto 4);
898
          else
899
            last_din <= "1111111" & tx_dat_i(7);
900
            crc_in <= "1111111" & tx_dat_i(7);
901
          end if;
902
          sd_dat_oe_o <= '1';
903
          sd_dat_o <= "00000000";
904
          -- Previous code took care to provide start bit on only
905
          -- the active lines.  However, the bus_size_reg logic
906
          -- takes care of masking the unused lines anyway, at
907
          -- the top level.
908
          --if (bus_size_reg=2) then -- start bits
909
          --  sd_dat_o <= "00000000";
910
          --elsif (bus_size_reg=1) then
911
          --  sd_dat_o <= "11110000";
912
          --else
913
          --  sd_dat_o <= "11111110";
914
          --end if;
915
          data_index <= to_unsigned(1,data_index'length);
916
        end if;
917
        if ((transf_cnt >= 2) and (transf_cnt <= data_cycles+1)) then
918
          sd_dat_o <= last_din;
919
          if (transf_cnt = data_cycles+1) then
920
            crc_enable <= '0';
921
          end if;
922
        elsif (transf_cnt > data_cycles+1) and (crc_c/=0) then
923
          crc_enable <= '0';
924
          crc_c <= crc_c-1;
925
          sd_dat_o(0) <= crc_out(0)(to_integer(crc_c)-1);
926
          if (bus_size_reg=2) then
927
            sd_dat_o(7 downto 1) <= crc_out(7)(to_integer(crc_c)-1) & crc_out(6)(to_integer(crc_c)-1) &
928
                                    crc_out(5)(to_integer(crc_c)-1) & crc_out(4)(to_integer(crc_c)-1) &
929
                                    crc_out(3)(to_integer(crc_c)-1) & crc_out(2)(to_integer(crc_c)-1) &
930
                                    crc_out(1)(to_integer(crc_c)-1);
931
          elsif (bus_size_reg=1) then
932
            sd_dat_o(3 downto 1) <= crc_out(3)(to_integer(crc_c)-1) & crc_out(2)(to_integer(crc_c)-1) &
933
                                    crc_out(1)(to_integer(crc_c)-1);
934
            sd_dat_o(7 downto 4) <= (others=>'1');
935
          else
936
            sd_dat_o(7 downto 1) <= (others=>'1');
937
          end if;
938
        end if;
939
        if (transf_cnt = data_cycles+18) then -- stop bits
940
          sd_dat_o <= "11111111";
941
        end if;
942
        if (transf_cnt = data_cycles+19) then
943
          sd_dat_oe_o <= '0';
944
        end if;
945
        -- state transition
946
        if (d_stop_i='1') then -- signal for stopping
947
          state <= IDLE;
948
        elsif (transf_cnt >= data_cycles+19) then
949
          transf_cnt <= (others=>'0');
950
          if (blkcnt_reg>0) then
951
            blkcnt_reg <= blkcnt_reg-1;
952
          end if;
953
          crc_rst_n <= '0';
954
          crc_c <= to_unsigned(16,crc_c'length);
955
          -- State transition
956
          if (blkcnt_reg=1) then
957
            state <= IDLE;
958
          else
959
            state <= SEND_DAT;
960
          end if;
961
        end if;
962
 
963
      when READ_WAIT =>
964
        sd_dat_oe_o   <= '0';
965
        crc_rst_n  <= '1';
966
        crc_enable <= '1';
967
        crc_in     <= (others=>'0');
968
        crc_c      <= to_unsigned(15,crc_c'length);-- end
969
        transf_cnt <= (others=>'0');
970
        -- state transition
971
        if (d_stop_i='1') then -- signal for stopping
972
          state <= IDLE;
973
        elsif (start_bit='1') then
974
          state <= READ_DAT;
975
        end if;
976
 
977
      when READ_DAT =>
978
        transf_cnt <= transf_cnt+1;
979
        if (transf_cnt < data_cycles) then
980
          if (bus_size_reg=2) then
981
            rx_dat_we_o <= '1';
982
            rx_dat_o <= dat_reg;
983
          elsif (bus_size_reg=1) then
984
            if (transf_cnt(0 downto 0)=1) then
985
              rx_dat_we_o <= '1';
986
            else
987
              rx_dat_we_o <= '0';
988
            end if;
989
            rx_dat_o(7-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(3);
990
            rx_dat_o(6-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(2);
991
            rx_dat_o(5-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(1);
992
            rx_dat_o(4-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(0);
993
          else
994
            if (transf_cnt(2 downto 0)=7) then
995
              rx_dat_we_o <= '1';
996
            else
997
              rx_dat_we_o <= '0';
998
            end if;
999
            rx_dat_o(7-to_integer(transf_cnt(2 downto 0))) <= dat_reg(0);
1000
          end if;
1001
          crc_in <= dat_reg;
1002
          crc_ok_l <= '1';
1003
        elsif (transf_cnt <= data_cycles+16) then
1004
          crc_enable <= '0';
1005
          last_din <= dat_reg;
1006
          rx_dat_we_o <= '0';
1007
          if (transf_cnt > data_cycles) then
1008
            crc_c <= crc_c-1;
1009
            if  (crc_out(0)(to_integer(crc_c)) /= last_din(0)) then
1010
              crc_ok_l <= '0';
1011
            end if;
1012
            if  (crc_out(1)(to_integer(crc_c)) /= last_din(1) and bus_size_reg>0) then
1013
              crc_ok_l <= '0';
1014
            end if;
1015
            if  (crc_out(2)(to_integer(crc_c)) /= last_din(2) and bus_size_reg>0) then
1016
              crc_ok_l <= '0';
1017
            end if;
1018
            if  (crc_out(3)(to_integer(crc_c)) /= last_din(3) and bus_size_reg>0) then
1019
              crc_ok_l <= '0';
1020
            end if;
1021
            if  (crc_out(4)(to_integer(crc_c)) /= last_din(4) and bus_size_reg>1) then
1022
              crc_ok_l <= '0';
1023
            end if;
1024
            if  (crc_out(5)(to_integer(crc_c)) /= last_din(5) and bus_size_reg>1) then
1025
              crc_ok_l <= '0';
1026
            end if;
1027
            if  (crc_out(6)(to_integer(crc_c)) /= last_din(6) and bus_size_reg>1) then
1028
              crc_ok_l <= '0';
1029
            end if;
1030
            if  (crc_out(7)(to_integer(crc_c)) /= last_din(7) and bus_size_reg>1) then
1031
              crc_ok_l <= '0';
1032
            end if;
1033
            if (crc_c=0) then
1034
              crc_rst_n <= '0';
1035
            end if;
1036
          end if;
1037
        end if;
1038
        -- state transition, provide CRC status reponse
1039
        -- The bus turnaround time (tri-state condition) occurs
1040
        -- on transf_cnt=(data_cycles+16) and (data_cycles+17)
1041
        -- Then, CRC status response begins
1042
        if (d_stop_i='1') then -- signal for stopping
1043
          if (transf_cnt <= data_cycles+16) then
1044
            state <= IDLE;
1045
          else
1046
            d_stop_pending <= '1'; -- Continue with CRC status token response, then stop.
1047
          end if;
1048
        end if;
1049
        if (transf_cnt=(data_cycles+17)) then
1050
          sd_dat_oe_o <= '1';
1051
          sd_dat_o <= "00000000"; -- start bit for CRC status response
1052
          if (crc_ok_l='1') then
1053
            crc_status <= "0101"; -- status for good CRC, with stop bit
1054
            state <= CRC_ACK;
1055
          else
1056
            crc_status <= "1011"; -- status for bad CRC, with stop bit
1057
            state <= CRC_NACK;
1058
          end if;
1059
        end if;
1060
 
1061
      when CRC_ACK =>
1062
        transf_cnt <= transf_cnt+1;
1063
        sd_dat_o(0) <= crc_status(3);
1064
        sd_dat_o(7 downto 1) <= (others=>'1');
1065
        crc_status <= crc_status(2 downto 0) & '1';
1066
        if (d_stop_i='1') then
1067
          d_stop_pending <= '1';
1068
        end if;
1069
        if (transf_cnt=(data_cycles+22)) then
1070
          blk_prg_tmr <= to_unsigned(BLK_PRG_TIME,blk_prg_tmr'length);
1071
          state <= RECV_BUSY;
1072
        end if;
1073
 
1074
      when CRC_NACK =>
1075
        transf_cnt <= transf_cnt+1;
1076
        sd_dat_o(0) <= crc_status(3);
1077
        sd_dat_o(7 downto 1) <= (others=>'1');
1078
        crc_status <= crc_status(2 downto 0) & '1';
1079
        if (d_stop_i='1') then
1080
          d_stop_pending <= '1';
1081
        end if;
1082
        if (transf_cnt=(data_cycles+22)) then
1083
          blkcnt_reg <= to_unsigned(1,blkcnt_reg'length); -- Cancel remainder, due to error
1084
          blk_prg_tmr <= to_unsigned(BLK_PRG_TIME,blk_prg_tmr'length);
1085
          state <= RECV_BUSY;
1086
        end if;
1087
 
1088
      when RECV_BUSY =>
1089
        if (d_stop_i='1') then
1090
          d_stop_pending <= '1';
1091
        end if;
1092
        -- Wait for busy period, even if stop is pending
1093
        if (blk_prg_tmr>0) then
1094
          null; -- Wait here
1095
        else
1096
          if (blkcnt_reg>0) then
1097
            blkcnt_reg <= blkcnt_reg-1;
1098
          end if;
1099
          -- State transition
1100
          if ((d_stop_pending='1') or (blkcnt_reg=1)) then
1101
            state <= IDLE;
1102
          else
1103
            state <= READ_WAIT;
1104
          end if;
1105
        end if;
1106
 
1107
 
1108
    when others =>
1109
      null;
1110
 
1111
    end case;
1112
  end if;
1113
end process;
1114
 
1115
 
1116
end beh;
1117
 
1118
 
1119
 
1120
-------------------------------------------------------------------------------
1121
-- SD/MMC Card Emulator
1122
-------------------------------------------------------------------------------
1123
--
1124
-- Author: John Clayton
1125
-- Update: Apr.  6, 2016 Wrote description and initial code
1126
--         Apr. 28, 2016 Added sd_card_data_unit, and various signals to
1127
--                       support it.
1128
--
1129
-- Description
1130
-------------------------------------------------------------------------------
1131
-- This module is meant to emulate an SD/MMC card.
1132
--
1133
-- This module does what an SD/MMC card would do, within reason.
1134
-- That is to say, it responds to commands, and accepts and provides data,
1135
-- but it does not contain the actual data storage cells.  Instead of storing
1136
-- data directly into a non-volatile memory array, this module provides an
1137
-- addressed parallel bus interface, so that FIFOs or other RAM buffers can
1138
-- be attached and used as storage.  The idea is that entire ranges of
1139
-- addresses can be mapped into the same storage buffer, or a set of buffers
1140
-- which are set up on certain address boundaries.  With these buffers in
1141
-- place, the sd_card_controller can act as a "bridge" between a host system
1142
-- and the FIFOs.  Data can be transferred to and from the FIFO buffers, all
1143
-- the while the host system believes it is communicating with an SD or MMC
1144
-- card.
1145
--
1146
-- Obviously, this scheme does not include the concept of a file system, which
1147
-- may necessitate one of the following:
1148
--
1149
--   1. A host command which can read and write to specific SD/MMC sectors,
1150
--      which are most likely 512 bytes each in length.
1151
--   2. The creation of a file system within the storage area of the FIFO
1152
--      buffers.
1153
--
1154
-- Option 2 requires significant amounts of cleverness, and has not yet been
1155
-- worked out in the mind of this code author.  However, for option 1, there
1156
-- exists a command in Linux, namely the "dd" command, which can serve to 
1157
-- access individual sectors, in the fashion shown below.  A software log
1158
-- file output is shown after each command, helping to illustrate the sequence
1159
-- of SD/MMC commands and responses which occur as part of the transfer, from
1160
-- the perspective of the host:
1161
--
1162
--   WRITE BLOCK 0 : dd if=/dev/zero of=/dev/mmcblk1 bs=4k count=1
1163
--   
1164
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.042114] omap_hsmmc 481d8000.mmc: enabled
1165
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.042235] omap_hsmmc 481d8000.mmc: mmc1: CMD25, argument 0x00000000
1166
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.042274] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1167
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.096448] omap_hsmmc 481d8000.mmc: IRQ Status is 2
1168
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.096478] omap_hsmmc 481d8000.mmc: mmc1: CMD12, argument 0x00000000
1169
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.096511] omap_hsmmc 481d8000.mmc: IRQ Status is 3
1170
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.096617] omap_hsmmc 481d8000.mmc: mmc1: CMD13, argument 0x00010000
1171
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.096640] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1172
--   Jan 15 11:45:31 bbb user.debug kernel: [  253.195201] omap_hsmmc 481d8000.mmc: disabled
1173
--   
1174
--   
1175
--   WRITE BLOCK 1 : dd if=/dev/zero of=/dev/mmcblk1 bs=4k count=1 seek=1
1176
--   
1177
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.840549] omap_hsmmc 481d8000.mmc: enabled
1178
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.840671] omap_hsmmc 481d8000.mmc: mmc1: CMD25, argument 0x00000008
1179
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.840706] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1180
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.840858] omap_hsmmc 481d8000.mmc: IRQ Status is 2
1181
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.840871] omap_hsmmc 481d8000.mmc: mmc1: CMD12, argument 0x00000000
1182
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.840888] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1183
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.841414] omap_hsmmc 481d8000.mmc: IRQ Status is 2
1184
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.841484] omap_hsmmc 481d8000.mmc: mmc1: CMD13, argument 0x00010000
1185
--   Jan 15 11:50:19 bbb user.debug kernel: [  541.841506] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1186
--   Jan 15 11:50:20 bbb user.debug kernel: [  541.934036] omap_hsmmc 481d8000.mmc: disabled
1187
--   
1188
--   
1189
--   WRITE BLOCK 2 : dd if=/dev/zero of=/dev/mmcblk1 bs=4k count=1 seek=2
1190
--   
1191
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.057453] omap_hsmmc 481d8000.mmc: enabled
1192
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.057573] omap_hsmmc 481d8000.mmc: mmc1: CMD25, argument 0x00000010
1193
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.057610] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1194
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.057761] omap_hsmmc 481d8000.mmc: IRQ Status is 2
1195
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.057775] omap_hsmmc 481d8000.mmc: mmc1: CMD12, argument 0x00000000
1196
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.057792] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1197
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.058283] omap_hsmmc 481d8000.mmc: IRQ Status is 2
1198
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.058357] omap_hsmmc 481d8000.mmc: mmc1: CMD13, argument 0x00010000
1199
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.058379] omap_hsmmc 481d8000.mmc: IRQ Status is 1
1200
--   Jan 15 11:49:31 bbb user.debug kernel: [  493.150608] omap_hsmmc 481d8000.mmc: disabled
1201
--   
1202
--
1203
-- This SD/MMC card emulator can implement the following response types:
1204
--
1205
--  type   length  structure
1206
--  ----   ------  ----------------------------------------------------------
1207
--   R1 =  48 bit, ("00" & 6 bit index & 32 bit card status & 7 bit CRC & '1')
1208
--   R2 = 136 bit, ("00111111" & 120 bit CID or CSD & 7 bit CRC & '1')
1209
--   R3 =  48 bit, ("00111111" & 32 bit OCR & "11111111")
1210
--   R4 =  48 bit, ("00100111" & 16 bit RCA & 1 bit status & 7 bit reg addr & 8 bit reg read data & 7 bit CRC & '1')
1211
--   R5 =  48 bit, ("00101000" & 16 bit RCA & 16 bit undefined & 7 bit CRC & '1')
1212
--
1213
-- There is a "modified" R1 response, called R1b, in which the D0 line can
1214
-- be held low to signify that the card needs more time.  Currently, the
1215
-- R1b option to delay is not used by this card.
1216
--
1217
-- The R4 and R5 response types are optional, and can be de-selected by
1218
-- setting generics to zero.
1219
-- 
1220
-- Boot modes are not supported, although they probably could be with a
1221
-- modicum of additional work.  The states are already provided in the card
1222
-- state machine, along with some comments detailing what needs to be done.
1223
--
1224
-- This module assumes it is the only card on the host SD/MMC card bus.
1225
-- Therefore, it does not "bitwise monitor its outgoing bitstream" when
1226
-- responding to ALL_SEND_CID (CMD2).  See eMMC Specification JESD84-A44,
1227
-- section 7.4.5 "Card Identification Process" for further details.
1228
-- Of course, feel free to upgrade this module to operate on multi-card
1229
-- buses, if that is what interests you.
1230
--
1231
-- Most of this unit runs entirely within the sd_clk_i clock domain.  The FIFO
1232
-- data storage buffers form a natural place at which to interface between
1233
-- clock domains, and the registers are also read and written from a separate
1234
-- clock domain (sys_clk).
1235
--
1236
-- Register default values are set by generics.
1237
--
1238
-- The module registers are summarized as follows:
1239
--
1240
-- Address   Structure   Function
1241
-- -------   ---------   -----------------------------------------------------
1242
--   0x0       (31:0)    Card status, some bits read only
1243
--   0x1       (31:0)    (31:16)=Relative Card Address (RCA) (READ ONLY)
1244
--                       (15: 0)=Driver Stage Register (DSR) (READ ONLY)
1245
--   0x2        (8:0)    EXT_CSD address
1246
--   0x3        (7:0)    EXT_CSD data
1247
--   0x4       (31:0)    Card Specific Data (CSD) bytes [ 3: 0]
1248
--                       Byte [0] is a "scratchpad" only, since the card R2
1249
--                       responder populates those bits with CRC-7 and stop bit.
1250
--   0x5       (31:0)    Card Specific Data (CSD) bytes [ 7: 4]
1251
--   0x6       (31:0)    Card Specific Data (CSD) bytes [11: 8]
1252
--   0x7       (31:0)    Card Specific Data (CSD) bytes [15:12]
1253
--   0xF       (15:0)    Command receive CRC error count
1254
--
1255
-- Notes on SD/MMC card registers:
1256
--
1257
-- The module registers are not equivalent to the card registers.
1258
-- The card has the following registers:
1259
--                      Module
1260
--   Name    Size       Access  Notes
1261
--   ------  ---------  ------  ---------------------------
1262
--   status    4 bytes    RW    module R_0
1263
--   CID      16 bytes    RO    Wholly set by generics
1264
--   CSD      16 bytes    RW    Defaults set by generics
1265
--   EXT_CSD 512 bytes    RW    contained in initialized Block RAM
1266
--   RCA       2 bytes    RO    module R_1, bits (31:16)
1267
--   DSR       2 bytes    RO    module R_1, bits (15: 0), use is optional
1268
--   OCR       4 bytes    RW    Set by constant plus generics
1269
--
1270
--   CSD is readable in the 2 lower bytes by the card, the upper 14 bytes are
1271
--   read only on the card side.
1272
--
1273
-- Notes on module registers:
1274
--
1275
--   Refer to MMC specifications for descriptions of the card registers.
1276
--
1277
--   0xF : SD/MMC command receive CRC error count
1278
--
1279
--     bits (15:0) contain a count of the number of SD/MMC commands
1280
--                 which have been received with CRC errors.  This
1281
--                 count is reset to zero whenever it is written to,
1282
--                 regardless of what value is written.
1283
--
1284
 
1285
 
1286
library IEEE;
1287
use IEEE.STD_LOGIC_1164.ALL;
1288
use IEEE.NUMERIC_STD.ALL;
1289
 
1290
library work;
1291
use work.convert_pack.all;
1292
use work.sd_card_pack.all;
1293
use work.block_ram_pack.all;
1294
 
1295
entity sd_card_emulator is
1296
  generic (
1297
    USE_R4_RESPONSE      : integer := 1; -- Fast I/O read/write (app specific)
1298
    USE_R5_RESPONSE      : integer := 0; -- Interrupt Request Mode
1299
    EXT_CSD_INIT_FILE    : string  := "ext_csd_init.txt"; -- Initial contents of EXT_CSD
1300
    OCR_USE_DUAL_VOLTAGE : integer := 1;
1301
    OCR_USE_SECTOR_MODE  : integer := 0;
1302
    CID_MID    : unsigned( 7 downto 0) := str2u("45",8); -- Manufacturer ID
1303
    CID_OID    : unsigned( 7 downto 0) := str2u("77",8); -- OEM ID
1304
    CID_CBX    : unsigned( 1 downto 0) := "00"; -- 0=Card, 1=BGA, 2=Package On Package
1305
    CID_PNM    : unsigned(47 downto 0) := str2u("434152444C59",48); -- Product Name, 6 ASCII chars
1306
    CID_PRV    : unsigned( 7 downto 0) := str2u("01",8); -- Product Rev (2 BCD digits, e.g. 6.2=0x62)
1307
    CID_PSN    : unsigned(31 downto 0) := str2u("00000012",32); -- Product serial number
1308
    CID_MDT    : unsigned( 7 downto 0) := str2u("43",8); -- Manufacture Date (Jan=1, 1997=0, e.g. Apr. 2000=0x43)
1309
    DEF_STAT   : unsigned(31 downto 0) := str2u("00000000",32); -- Read Write, R_0
1310
    CSD_WORD_3 : unsigned(31 downto 0) := str2u("905E002A",32); -- See MMC spec
1311
    CSD_WORD_2 : unsigned(31 downto 0) := str2u("1F5983D3",32); -- See MMC spec
1312
    CSD_WORD_1 : unsigned(31 downto 0) := str2u("EDB707FF",32); -- See MMC spec
1313
    CSD_WORD_0 : unsigned(31 downto 0) := str2u("96400000",32); -- See MMC spec, bits (7:0) not used.
1314
    DEF_R_Z    : unsigned(31 downto 0) := str2u("33333333",32)  -- Value returned for nonexistent registers
1315
  );
1316
  port (
1317
 
1318
    -- Asynchronous reset
1319
    sys_rst_n     : in  std_logic;
1320
    sys_clk       : in  std_logic;
1321
 
1322
    -- Bus interface
1323
    adr_i         : in  unsigned(3 downto 0);
1324
    sel_i         : in  std_logic;
1325
    we_i          : in  std_logic;
1326
    dat_i         : in  unsigned(31 downto 0);
1327
    dat_o         : out unsigned(31 downto 0);
1328
    ack_o         : out std_logic;
1329
 
1330
    -- SD/MMC card command signals
1331
    sd_clk_i      : in  std_logic;
1332
    sd_cmd_i      : in  std_logic;
1333
    sd_cmd_o      : out std_logic;
1334
    sd_cmd_oe_o   : out std_logic;
1335
    sd_od_mode_o  : out std_logic; -- Open drain mode
1336
    sd_dat_i      : in  unsigned(7 downto 0);
1337
    sd_dat_o      : out unsigned(7 downto 0);
1338
    sd_dat_oe_o   : out std_logic;
1339
    sd_dat_siz_o  : out unsigned(1 downto 0);
1340
 
1341
    -- Data FIFO interface
1342
    buf_adr_o     : out unsigned(31 downto 0);
1343
    buf_dat_o     : out unsigned(7 downto 0);
1344
    buf_dat_we_o  : out std_logic;
1345
    buf_dat_i     : in  unsigned(7 downto 0);
1346
    buf_dat_rd_o  : out std_logic
1347
  );
1348
end sd_card_emulator;
1349
 
1350
architecture beh of sd_card_emulator is
1351
 
1352
-- constants
1353
constant N_ID   : integer := 5; -- Number of clocks before response in card ID mode
1354
constant N_CR   : integer := 5; -- Number of clocks before response in data transfer mode
1355
 
1356
constant N_CD   : integer := 45; -- Number of clocks before return data in data transfer mode
1357
constant D_BITS : integer := 6; -- Number of bits used in N_CD countdown timers (Command to Data)
1358
 
1359
constant IMPLEMENT_WRITE_PROT : integer := 0; -- Can be added as a generic if desired...
1360
 
1361
-- signals
1362
  -- related to card registers
1363
signal status             : unsigned(31 downto 0);
1364
signal reported_status    : unsigned(31 downto 0); -- contains some read only fields
1365
signal cid                : unsigned(127 downto 0);
1366
signal cid_resp           : unsigned(119 downto 0);
1367
signal csd                : unsigned(127 downto 0);
1368
signal csd_resp           : unsigned(119 downto 0);
1369
signal rca                : unsigned(15 downto 0);
1370
signal dsr                : unsigned(15 downto 0);
1371
signal ocr                : unsigned(31 downto 0);
1372
signal ocr_mode           : unsigned(1 downto 0);
1373
signal ocr_vbit           : std_logic;
1374
signal ocr_pwrup_done     : std_logic;
1375
    -- Pertaining to the EXT_CSD register, which is 512 bytes long
1376
      -- System side
1377
signal ext_csd_reg_adr    : unsigned(8 downto 0);
1378
signal ext_csd_reg_dat_rd : unsigned(7 downto 0);
1379
signal ext_csd_reg_we     : std_logic;
1380
      -- SD/MMC card side
1381
signal ext_csd_sd_adr     : unsigned(8 downto 0);
1382
signal ext_csd_sd_we      : std_logic;
1383
signal ext_csd_sd_dat_wr  : unsigned(7 downto 0);
1384
signal ext_csd_sd_dat_rd  : unsigned(7 downto 0);
1385
signal cmd6_op            : unsigned(1 downto 0); -- Orchestrates operations on ext_csd mode space
1386
signal ext_csd_rd_adr     : unsigned(8 downto 0);
1387
signal dreply_start_count : unsigned(D_BITS-1 downto 0); -- Determines when data reply begins
1388
signal prg_dly_count      : unsigned(23 downto 0); -- Determines emulated programming delay
1389
 
1390
  -- Related to receiving commands
1391
signal sd_cmd_index       : unsigned(5 downto 0);
1392
signal sd_cmd_arg         : unsigned(31 downto 0);
1393
signal sd_cmd_done        : std_logic;
1394
signal sd_cmd_crc_err     : std_logic;
1395
signal sd_cmd_rx          : std_logic;
1396
signal sd_cmd_oe_l        : std_logic;
1397
 
1398
  -- Related to Card Finite State Machine
1399
type CARD_STATE_TYPE is (CARD_IDLE, CARD_READY, CARD_IDENT, CARD_STBY,
1400
                         CARD_TRAN, CARD_DATA, CARD_RCV,
1401
                         CARD_PRG,  CARD_DIS,  CARD_BTST, CARD_SLP, CARD_INA,
1402
                         CARD_IRQ,  CARD_PRE_IDLE, CARD_PRE_BOOT, CARD_BOOT);
1403
signal card_state       : CARD_STATE_TYPE;
1404
signal card_state_reply : CARD_STATE_TYPE; -- For reporting in status
1405
 
1406
signal r_delay                : unsigned(5 downto 0); -- Used to count reply delay clock cycles
1407
signal sd_adr_match           : std_logic;
1408
signal card_cmd_set           : unsigned(2 downto 0);
1409
signal card_adr               : unsigned(31 downto 0);
1410
signal card_blocklen          : unsigned(11 downto 0);
1411
signal card_block_count       : unsigned(15 downto 0);
1412
signal card_dbus_size         : unsigned(1 downto 0);
1413
signal card_rel_wr_req        : std_logic;
1414
signal card_erase_group_start : unsigned(31 downto 0);
1415
signal card_erase_group_end   : unsigned(31 downto 0);
1416
signal card_d_crc_ok          : std_logic;
1417
signal card_d_continuous      : std_logic;
1418
signal card_d_stop            : std_logic;
1419
signal card_d_recv            : std_logic;
1420
signal card_d_send            : std_logic;
1421
signal card_bustest_r         : std_logic;
1422
signal card_bustest_w         : std_logic;
1423
signal card_sd_dat_busy       : std_logic;
1424
signal sd_dat_unbusy          : unsigned(7 downto 0); -- Data lines before adding CARD_PRG busy indication
1425
signal sd_dat_oe_unbusy       : std_logic; -- Output enable before adding CARD_PRG busy indication
1426
signal card_d_busy            : std_logic;
1427
signal card_d_busy_r1         : std_logic;
1428
signal card_tx_dat            : unsigned(7 downto 0);
1429
signal card_tx_dat_rd         : std_logic;
1430
signal adr_offset             : unsigned(31 downto 0);
1431
signal buf_dat_we_l           : std_logic;
1432
 
1433
  -- Related to command responses
1434
    -- R1 response type
1435
signal r1_cmd             : std_logic;
1436
signal r1_cmd_oe          : std_logic;
1437
signal r1_start           : std_logic;
1438
signal r1_done            : std_logic;
1439
signal crc_err_reply      : std_logic; -- Cleared at end of R1 reponse
1440
signal cmd_err_reply      : std_logic; -- Cleared at end of R1 reponse
1441
signal sd_cmd_neverbad    : std_logic;
1442
 
1443
    -- R2 CID response type
1444
signal r2_cid_cmd         : std_logic;
1445
signal r2_cid_cmd_oe      : std_logic;
1446
signal r2_cid_start       : std_logic;
1447
signal r2_cid_done        : std_logic;
1448
 
1449
    -- R2 CSD response type
1450
signal r2_csd_cmd         : std_logic;
1451
signal r2_csd_cmd_oe      : std_logic;
1452
signal r2_csd_start       : std_logic;
1453
signal r2_csd_done        : std_logic;
1454
 
1455
    -- R3 response type
1456
signal r3_cmd             : std_logic;
1457
signal r3_cmd_oe          : std_logic;
1458
signal r3_start           : std_logic;
1459
signal r3_done            : std_logic;
1460
 
1461
    -- R4 response type
1462
signal r4_cmd             : std_logic;
1463
signal r4_cmd_oe          : std_logic;
1464
signal r4_start           : std_logic;
1465
signal r4_done            : std_logic;
1466
signal fast_io_reg_rd     : unsigned(31 downto 0);
1467
signal r4_status          : std_logic;
1468
signal r4_reg_adr         : unsigned(6 downto 0);
1469
signal r4_reg_dat_rd      : unsigned(7 downto 0);
1470
signal r4_reg_dat_wr      : unsigned(7 downto 0);
1471
signal r4_reg_we          : std_logic;
1472
      -- Special purpose registers used with CMD39
1473
      -- (Currently only a pair of registers is provided,
1474
      --  because we do not have a purpose for them.)
1475
signal r4_reg_0           : unsigned(7 downto 0);
1476
signal r4_reg_1           : unsigned(7 downto 0);
1477
 
1478
    -- R5 response type
1479
signal r5_cmd             : std_logic;
1480
signal r5_cmd_oe          : std_logic;
1481
signal r5_start           : std_logic;
1482
signal r5_done            : std_logic;
1483
signal irq_response       : unsigned(31 downto 0);
1484
 
1485
begin
1486
 
1487
  -- module register read mux
1488
  with to_integer(adr_i) select
1489
  dat_o <=
1490
    reported_status                        when 0,
1491
    rca & dsr                              when 1,
1492
    u_resize(ext_csd_reg_adr,32)           when 2,
1493
    u_resize(ext_csd_reg_dat_rd,32)        when 3,
1494
    csd(31 downto 0)                       when 4,
1495
    csd(63 downto 32)                      when 5,
1496
    csd(95 downto 64)                      when 6,
1497
    csd(127 downto 96)                     when 7,
1498
    DEF_R_Z                                when others;
1499
 
1500
  -- Create acknowledge signal
1501
  ack_o <= sel_i;
1502
 
1503
  -- module register writes are handled here.
1504
  module_reg_proc: process(sys_clk, sys_rst_n)
1505
  begin
1506
    if (sys_rst_n='0') then
1507
      status <= DEF_STAT;
1508
      csd    <= CSD_WORD_3 & CSD_WORD_2 & CSD_WORD_1 & CSD_WORD_0;
1509
      ext_csd_reg_adr  <= (others=>'0');
1510
    elsif (sys_clk'event and sys_clk='1') then
1511
      -- Handle bus writes to registers
1512
      -- These are allowed even when sys_clk_en is low
1513
      if (sel_i='1' and we_i='1') then
1514
        case to_integer(adr_i) is
1515
          when 0 =>
1516
            -- Except for the reported card_state, and the self-clearing
1517
            -- error bits, the other status can be set here.
1518
            status <= dat_i;
1519
          when 2 =>
1520
            ext_csd_reg_adr <= dat_i(8 downto 0);
1521
          when 3 =>
1522
            null; -- EXT_CSD write handled at the BRAM
1523
          when 4 =>
1524
            csd( 31 downto  0) <= dat_i;
1525
          when 5 =>
1526
            csd( 63 downto 32) <= dat_i;
1527
          when 6 =>
1528
            csd( 95 downto 64) <= dat_i;
1529
          when 7 =>
1530
            csd(127 downto 96) <= dat_i;
1531
          when others => null;
1532
        end case;
1533
      end if; -- sel_i
1534
    end if; -- sys_clk
1535
  end process;
1536
 
1537
  -- formulate any card registers which may depend on generics
1538
    -- Per the eMMC Specification, JESD84-A44, the following fields are defined:
1539
    -- (From table 41, page 112, Section 8.1)
1540
    --
1541
    -- Bits (127:120) = MID (Manufacturer ID)
1542
    -- Bits (119:114) = 000000b (Reserved)
1543
    -- Bits (113:112) = CBX (00=Card, 01=BGA, 10=POP, 11=reserved) (POP=Package On Package)
1544
    -- Bits (111:104) = OID (OEM ID)
1545
    -- Bits (103: 56) = PNM (Product Name, 6 ASCII characters)
1546
    -- Bits ( 55: 48) = PRV (Product Revision)
1547
    -- Bits ( 47: 16) = PSN (Product Serial Number)
1548
    -- Bits ( 15:  8) = MDT (Manufacturing Date)
1549
    -- Bits (  7:  1) = CRC-7 checksum (Added by responder in this case)
1550
    -- Bit  (      0) = '1' (Stop Bit, added by responder in this case)
1551
cid <= CID_MID & "000000" & CID_CBX & CID_OID & CID_PNM &
1552
       CID_PRV & CID_PSN & CID_MDT & "00000001";
1553
cid_resp <= cid(127 downto 8); -- Responder adds the CRC-7 and stop bit fields
1554
csd_resp <= csd(127 downto 8); -- Responder adds the CRC-7 and stop bit fields
1555
 
1556
    -- OCR:
1557
    -- Per the eMMC Specification, JESD84-A44, the following fields are defined:
1558
    -- (From table 40, page 111, Section 8.1)
1559
    --
1560
    -- Bits (6:0) = "0000000" (Reserved)
1561
    -- Bit  (7) = '0' (1.7 to 1.95 Volts; set for "dual voltage" cards)
1562
    -- Bits (14:8) = "0000000" (2.0 to 2.6 Volts)
1563
    -- Bits (23:15) = "111111111" (2.7 to 3.6 Volts)
1564
    -- Bits (28:24) = "00000" (Reserved)
1565
    -- Bits (30:29) = "00" (Byte mode) or "01" (Sector mode)
1566
    -- Bit  (31) = '1' (Card power up status, set to '0' for "busy")
1567
ocr_vbit <= '1' when OCR_USE_DUAL_VOLTAGE>0 else '0';
1568
ocr_mode <= "01" when OCR_USE_SECTOR_MODE>0 else "00";
1569
ocr <= ocr_pwrup_done & ocr_mode & "00000" & "111111111" & "0000000" & ocr_vbit & "0000000";
1570
    -- Note: OCR response to CMD1 for eMMC can be a fixed pattern, because the
1571
    -- host's indication of requested voltage ranges is no longer meaningful for
1572
    -- eMMC devices.
1573
    -- Fixed pattern can be:
1574
    --   0x00FF8080 for <=2GB eMMC
1575
    --   0x40FF8080 for >2GB eMMC
1576
    -- These fixed patterns are exactly what is produced by the ocr setting above, assuming
1577
    -- that OCR_USE_DUAL_VOLTAGE is non-zero.
1578
 
1579
--------------------------------------------
1580
-- BRAM for 512 byte long EXT_CSD register
1581
  ext_csd_ram: swiss_army_ram
1582
    generic map(
1583
      USE_BRAM  => 1, -- Set to nonzero value for BRAM, zero for distributed RAM
1584
      WRITETHRU => 1, -- Set to nonzero value for writethrough mode
1585
      USE_FILE  => 1, -- Set to nonzero value to use INIT_FILE
1586
      INIT_VAL  => 0, -- Value used when INIT_FILE is not used
1587
      INIT_SEL  => 0, -- Selects which segment of (larger) INIT_FILE to use
1588
      INIT_FILE => EXT_CSD_INIT_FILE, -- ASCII hexadecimal initialization file name
1589
      FIL_WIDTH => 8, -- Bit width of init file lines
1590
      ADR_WIDTH => 9,
1591
      DAT_WIDTH => 8
1592
    )
1593
    port map(
1594
       clk_a    => sys_clk,
1595
       adr_a_i  => ext_csd_reg_adr,
1596
       we_a_i   => ext_csd_reg_we,
1597
       en_a_i   => '1',
1598
       dat_a_i  => dat_i(7 downto 0),
1599
       dat_a_o  => ext_csd_reg_dat_rd,
1600
 
1601
       clk_b    => sd_clk_i,
1602
       adr_b_i  => ext_csd_sd_adr,
1603
       we_b_i   => ext_csd_sd_we,
1604
       en_b_i   => '1',
1605
       dat_b_i  => ext_csd_sd_dat_wr,
1606
       dat_b_o  => ext_csd_sd_dat_rd
1607
    );
1608
 
1609
  -- create system side write enable
1610
  ext_csd_reg_we <= '1' when sel_i='1' and we_i='1' and adr_i=3 else '0';
1611
 
1612
  -- process for the SD/MMC card side
1613
  -- CMD39 "FAST_IO" register writes are handled here
1614
  sd_reg_proc: process(sd_clk_i, sys_rst_n)
1615
  begin
1616
    if (sys_rst_n='0') then
1617
      ext_csd_rd_adr <= (others=>'0');
1618
    elsif (sd_clk_i'event and sd_clk_i='1') then
1619
      if (dreply_start_count=0 and card_tx_dat_rd='1') then
1620
        ext_csd_rd_adr <= ext_csd_rd_adr+1;
1621
      end if;
1622
      -- Perform one full readout
1623
      if (ext_csd_rd_adr/=0 and card_tx_dat_rd='1') then
1624
        ext_csd_rd_adr <= ext_csd_rd_adr+1;
1625
      end if;
1626
    end if; -- sd_clk_i
1627
  end process;
1628
 
1629
  ext_csd_sd_adr <= '0' & sd_cmd_arg(23 downto 16) when card_state=CARD_TRAN and sd_cmd_index=6 and sd_cmd_arg(25 downto 24)/=0 and sd_cmd_arg(23 downto 16)<192 else ext_csd_rd_adr;
1630
  ext_csd_sd_we <= '1' when cmd6_op>0 else '0';
1631
  ext_csd_sd_dat_wr <=  sd_cmd_arg(15 downto 8) OR ext_csd_sd_dat_rd when cmd6_op=1 else -- set bits
1632
                        (NOT sd_cmd_arg(15 downto 8)) AND ext_csd_sd_dat_rd when cmd6_op=2 else -- clear bits
1633
                        sd_cmd_arg(15 downto 8); -- write byte
1634
 
1635
--------------------------------------------
1636
-- SD/MMC Command Receiver
1637
 
1638
  -- Make sure the receiver only listens when there are no
1639
  -- outgoing transmissions...
1640
  sd_cmd_rx <= sd_cmd_i when sd_cmd_oe_l='0' else '1';
1641
 
1642
  cmd_receiver: sd_card_cmd_rx
1643
  port map(
1644
    -- Asynchronous reset
1645
    sys_rst_n   => sys_rst_n,
1646
    -- SD/MMC card command signals
1647
    sd_clk_i    => sd_clk_i,
1648
    sd_cmd_i    => sd_cmd_rx,
1649
    -- Command outputs
1650
    cmd_raw_o   => open,
1651
    cmd_index_o => sd_cmd_index,
1652
    cmd_arg_o   => sd_cmd_arg,
1653
    -- Status and done indicator
1654
    cmd_done_o  => sd_cmd_done,
1655
    crc_err_o   => sd_cmd_crc_err,
1656
    dir_err_o   => open,
1657
    stop_err_o  => open
1658
  );
1659
  -- Create an indication that this card is being addressed
1660
  sd_adr_match <= '1' when sd_cmd_arg(31 downto 16)=rca else '0';
1661
  -- Create a signal that indicates when a "neverbad" command is received
1662
  sd_cmd_neverbad <= '1' when sd_cmd_index=0 or sd_cmd_index=15 or sd_cmd_index=55 else '0';
1663
 
1664
--------------------------------------------
1665
-- Card State Machine
1666
 
1667
card_fsm_proc: process(sd_clk_i, sys_rst_n)
1668
begin
1669
  if (sys_rst_n='0') then
1670
    card_state <= CARD_PRE_IDLE;
1671
    card_state_reply <= CARD_PRE_IDLE;
1672
    r_delay    <= (others=>'0');
1673
    rca        <= str2u("0001",16);
1674
    dsr        <= str2u("0404",16);
1675
    card_cmd_set       <= (others=>'0');
1676
    card_adr           <= (others=>'0');
1677
    card_blocklen      <= to_unsigned(512,card_blocklen'length);
1678
    card_block_count   <= to_unsigned(0,card_block_count'length);
1679
    card_dbus_size     <= "00";
1680
    card_rel_wr_req    <= '0';
1681
    card_erase_group_start <= (others=>'0');
1682
    card_erase_group_end   <= (others=>'0');
1683
    cmd6_op            <= (others=>'0');
1684
    prg_dly_count      <= (others=>'0');
1685
    dreply_start_count <= (others=>'0');
1686
    crc_err_reply      <= '0';
1687
    cmd_err_reply      <= '0';
1688
    r1_start           <= '0';
1689
    r2_cid_start       <= '0';
1690
    r2_csd_start       <= '0';
1691
    r3_start           <= '0';
1692
    r4_start           <= '0';
1693
    r5_start           <= '0';
1694
    r4_reg_dat_wr      <= (others=>'0');
1695
    r4_reg_adr         <= (others=>'0');
1696
    r4_status          <= '0';
1697
    r4_reg_we          <= '0';
1698
    card_d_stop        <= '0';
1699
    card_d_recv        <= '0';
1700
    card_bustest_w     <= '0';
1701
    card_d_busy_r1     <= '0';
1702
    ocr_pwrup_done     <= '0'; -- low means busy
1703
 
1704
  elsif (sd_clk_i'event and sd_clk_i='1') then
1705
    -- For edge detection
1706
    card_d_busy_r1 <= card_d_busy;
1707
 
1708
    -- Default values
1709
    r1_start     <= '0';
1710
    r2_cid_start <= '0';
1711
    r2_csd_start <= '0';
1712
    r3_start     <= '0';
1713
    r4_start     <= '0';
1714
    r5_start     <= '0';
1715
 
1716
    r4_reg_we <= '0';
1717
    cmd6_op   <= (others=>'0');
1718
    card_bustest_w <= '0';
1719
    card_d_stop  <= '0';
1720
    card_d_recv  <= '0';
1721
 
1722
    -- Decrement the EXT_CSD readout delay timer
1723
    if (dreply_start_count>0) then
1724
      dreply_start_count <= dreply_start_count-1;
1725
    end if;
1726
 
1727
    -- Decrement the programming delay timer
1728
    if (prg_dly_count>0) then
1729
      prg_dly_count <= prg_dly_count-1;
1730
    end if;
1731
 
1732
    -- Clear certain status bits when reply is done
1733
    if (r1_done='1') then
1734
      crc_err_reply <= '0';
1735
      cmd_err_reply <= '0';
1736
    end if;
1737
    -- Set CRC error status bit when errors are found
1738
    if (sd_cmd_done='1' and sd_cmd_crc_err='1') then
1739
      crc_err_reply <= '1';
1740
    end if;
1741
 
1742
    -- Handle state transitions
1743
    case (card_state) is
1744
      when CARD_PRE_IDLE =>
1745
        -- BOOT_PARTITION_ENABLE bit in EXT_CSD would be checked here
1746
        -- but it is not currently supported, so just move on...
1747
        card_state <= CARD_IDLE;
1748
        -- IF BOOT_PARTITION_ENABLE were set, state would move to
1749
        -- CARD_PRE_BOOT.
1750
 
1751
      when CARD_PRE_BOOT =>
1752
        -- If sd_cmd_i is held low for >74 clocks, or if CMD0 is
1753
        -- received with arg = 0xFFFFFFFA, then move to CARD_BOOT.
1754
        -- NOTE: CMD0 is implemented outside of the FSM case statement,
1755
        --       so be aware of that when implementing this special
1756
        --       CMD0 with arg = 0xFFFFFFFA.
1757
        -- but it is not currently supported, so just move on...
1758
        card_state <= CARD_IDLE;
1759
        -- If sd_cmd_i goes high in less than 74 clocks, or if
1760
        -- "normal" CMD0 or CMD1 is received, move to CARD_IDLE
1761
 
1762
      when CARD_BOOT =>
1763
        -- sd_cmd_i going high, or CMD0 with arg = 0x00000000
1764
        -- moves to CARD_IDLE.  Meanwhile, all the while this
1765
        -- state persists, continue sending boot data
1766
        card_state <= CARD_IDLE;
1767
 
1768
      when CARD_IDLE =>
1769
        if (sd_cmd_done='1' and sd_cmd_crc_err='0') then
1770
          -- Look for SEND_OP_COND (CMD1)
1771
          if (sd_cmd_index=1) then
1772
            -- Here, check the sd_cmd_arg to see if the voltage range is
1773
            -- compatible.  If it is not, then go to CARD_INA (inactive)
1774
            -- state.  For this module, it is "inconceivable" that the
1775
            -- requested voltage range would not be acceptable, so the
1776
            -- check is not being performed.  But the reply is being given.
1777
            r3_start <= '1';
1778
            if (ocr_pwrup_done='1') then
1779
              card_state <= CARD_READY;
1780
            end if;
1781
          elsif (sd_cmd_neverbad='0') then
1782
            cmd_err_reply <= '1';
1783
          end if;
1784
        end if;
1785
        -- When the R3 reply is finished, set the ocr_pwrup_done bit, to show
1786
        -- that the card powerup status is no longer busy...  this is
1787
        -- sketchy in the eMMC spec.  The card can stay in CARD_IDLE when
1788
        -- it is busy.  We do this once, to match behavior of real cards.
1789
        if (r3_done='1') then
1790
          ocr_pwrup_done <= '1';
1791
        end if;
1792
 
1793
      when CARD_READY =>
1794
        if (sd_cmd_done='1' and sd_cmd_crc_err='0') then
1795
          -- Look for ALL_SEND_CID (CMD2)
1796
          if (sd_cmd_index=2) then
1797
            -- Here, the idea in multi-card systems, is that each card sends
1798
            -- out its 136 bit reply containing the CID.  All cards reply
1799
            -- simultaneously, using open-drain drivers.  They are supposed
1800
            -- to monitor the state of the sd_cmd_i line while sending their
1801
            -- replies, to see if what they are seeing does not match what
1802
            -- they are sending out.  If there is a mismatch, then the card
1803
            -- "loses" the bus, and goes to the CARD_INA (inactive) state.
1804
            --
1805
            -- This implementation does not do the check, since in this case
1806
            -- the assumption is made that there are no other cards present.
1807
            --
1808
            -- It's a pretty safe assumption, n'est-ce pas?
1809
            -- So, this module expects to always "win" the bus!
1810
            --
1811
            r2_cid_start <= '1';
1812
            card_state <= CARD_IDENT;
1813
          elsif (sd_cmd_neverbad='0') then
1814
            cmd_err_reply <= '1';
1815
          end if;
1816
        end if;
1817
 
1818
      when CARD_IDENT =>
1819
        if (sd_cmd_done='1' and sd_cmd_crc_err='0') then
1820
          -- Look for SET_RELATIVE_ADDR (CMD3)
1821
          if (sd_cmd_index=3) then
1822
            rca <= sd_cmd_arg(31 downto 16);
1823
            r1_start <= '1';
1824
            card_state_reply <= card_state;
1825
            card_state <= CARD_STBY;
1826
          elsif (sd_cmd_neverbad='0') then
1827
            cmd_err_reply <= '1';
1828
          end if;
1829
        end if;
1830
 
1831
      when CARD_STBY =>
1832
        -- Look for SET_DSR (CMD4), which has no reply
1833
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=4) then
1834
          dsr <= sd_cmd_arg(31 downto 16);
1835
        end if;
1836
        -- Look for SLEEP_AWAKE (CMD5), which toggles between CARD_STBY and CARD_SLP
1837
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=5) then
1838
          if sd_cmd_arg(15)='1' then
1839
            r1_start <= '1';
1840
            card_state_reply <= card_state;
1841
            card_state <= CARD_SLP;
1842
          end if;
1843
        end if;
1844
        -- Look for SELECT_CARD (CMD7)
1845
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=7) then
1846
          r1_start <= '1';
1847
          card_state_reply <= card_state;
1848
          card_state <= CARD_TRAN;
1849
        end if;
1850
        -- Look for SEND_CSD (CMD9)
1851
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=9) then
1852
          r2_csd_start <= '1';
1853
        end if;
1854
        -- Look for SEND_CID (CMD10)
1855
        -- This is similar to CMD2, except only the addressed card responds...
1856
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=10) then
1857
          r2_cid_start <= '1';
1858
        end if;
1859
        -- Look for SEND_STATUS (CMD13)
1860
        -- Per the spec, if a CRC error occurs, the card does not
1861
        -- respond.  Therefore, the CRC error status bit is not
1862
        -- reported until an R1 response is given to a later command
1863
        -- that is received correctly.
1864
        -- Hence, CMD13 is only executed if there is no CRC error.
1865
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
1866
          r1_start <= '1';
1867
          card_state_reply <= card_state;
1868
        end if;
1869
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
1870
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
1871
          card_state <= CARD_INA;
1872
        end if;
1873
        -- Look for FAST_IO (CMD39)
1874
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=39) then
1875
          if (USE_R4_RESPONSE>0) then
1876
            r4_start       <= '1';
1877
            r4_status      <= sd_cmd_arg(15);
1878
            r4_reg_adr     <= sd_cmd_arg(14 downto 8);
1879
            r4_reg_dat_wr  <= sd_cmd_arg(7 downto 0);
1880
            if (sd_cmd_arg(15)='1') then
1881
              r4_reg_we <= '1';
1882
            end if;
1883
          end if;
1884
        end if;
1885
        -- Look for GO_IRQ_STATE (CMD40)
1886
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=40) then
1887
          if (USE_R5_RESPONSE>0) then
1888
            r5_start <= '1';
1889
            card_state <= CARD_IRQ;
1890
          end if;
1891
        end if;
1892
        -- Flag illegal commands
1893
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
1894
          if (sd_cmd_index/= 4 and sd_cmd_index/= 5 and sd_cmd_index/= 7 and
1895
              sd_cmd_index/= 9 and sd_cmd_index/=10 and sd_cmd_index/=13 and
1896
              sd_cmd_index/=39 and sd_cmd_index/=40) then
1897
            cmd_err_reply <= '1';
1898
          end if;
1899
        end if;
1900
 
1901
      when CARD_TRAN =>
1902
        -- Look for SWITCH (CMD6)
1903
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=6) then
1904
          r1_start <= '1';
1905
          card_state_reply <= card_state;
1906
          if (sd_cmd_arg(25 downto 24)=0) then
1907
            card_cmd_set <= sd_cmd_arg(2 downto 0);
1908
          else
1909
            -- Setting the cmd6_op non-zero performs a write to ext_csd
1910
            -- Different masking is done, based on the value
1911
            cmd6_op <= sd_cmd_arg(25 downto 24);
1912
          end if;
1913
        end if;
1914
        -- For CMD6, change states only when ext_csd processing is complete
1915
        -- In reality, only a few clock cycles are needed for the processing,
1916
        -- but it is convenient to simply wait for the response to finish.
1917
        if (sd_cmd_index=6) then
1918
          if (r1_done='1') then
1919
            --prg_dly_count <= to_unsigned(4000,prg_dly_count'length); -- Programming delay (card is busy!)
1920
            prg_dly_count <= to_unsigned(40,prg_dly_count'length); -- Programming delay (card is busy!)
1921
            card_state <= CARD_PRG;
1922
            -- "Operation Complete" returns from CARD_PRG back to CARD_TRAN.
1923
            -- prg_dly_count determines the length of the programming time.
1924
            -- The parts of the EXT_CSD that we really care about,
1925
            -- are being "shadowed" as registers here, so capture them
1926
            card_dbus_size <= ext_csd_sd_dat_rd(1 downto 0);
1927
          end if;
1928
        end if;
1929
        -- Look for DESELECT_CARD (CMD7 without address match), which has no reply
1930
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='0' and sd_cmd_index=7) then
1931
          card_state <= CARD_STBY;
1932
        end if;
1933
        -- Look for SEND_EXT_CSD (CMD8)
1934
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=8) then
1935
          r1_start <= '1';
1936
          card_state_reply <= card_state;
1937
          dreply_start_count <= to_unsigned(N_CD,D_BITS); -- Set the data readout delay counter
1938
          card_block_count <= to_unsigned(1,card_block_count'length);
1939
          card_state <= CARD_DATA;
1940
        end if;
1941
        -- Look for READ_DAT_UNTIL_STOP (CMD11)
1942
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=11) then
1943
          r1_start <= '1';
1944
          card_state_reply <= card_state;
1945
          card_adr <= sd_cmd_arg;
1946
          dreply_start_count <= to_unsigned(N_CD,D_BITS); -- Set the data readout delay counter
1947
          card_state <= CARD_DATA;
1948
        end if;
1949
        -- Look for SEND_STATUS (CMD13)
1950
        -- Per the spec, if a CRC error occurs, the card does not
1951
        -- respond.  Therefore, the CRC error status bit is not
1952
        -- reported until an R1 response is given to a later command
1953
        -- that is received correctly.
1954
        -- Hence, CMD13 is only executed if there is no CRC error.
1955
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
1956
          r1_start <= '1';
1957
          card_state_reply <= card_state;
1958
        end if;
1959
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
1960
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
1961
          card_state <= CARD_INA;
1962
        end if;
1963
        -- Look for SET_BLOCKLEN (CMD16)
1964
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=16) then
1965
          r1_start <= '1';
1966
          card_state_reply <= card_state;
1967
          card_blocklen <= sd_cmd_arg(card_blocklen'length-1 downto 0);
1968
        end if;
1969
        -- Look for READ_SINGLE_BLOCK (CMD17)
1970
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=17) then
1971
          r1_start <= '1';
1972
          card_state_reply <= card_state;
1973
          card_adr <= sd_cmd_arg;
1974
          card_block_count   <= to_unsigned(1,card_block_count'length);
1975
          dreply_start_count <= to_unsigned(N_CD,D_BITS); -- Set the data readout delay counter
1976
          card_state <= CARD_DATA;
1977
        end if;
1978
        -- Look for READ_MULTIPLE_BLOCK (CMD18)
1979
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=18) then
1980
          r1_start <= '1';
1981
          card_state_reply <= card_state;
1982
          card_adr <= sd_cmd_arg;
1983
          dreply_start_count <= to_unsigned(N_CD,D_BITS); -- Set the data readout delay counter
1984
          card_state <= CARD_DATA;
1985
        end if;
1986
        -- Look for BUSTEST_W (CMD19)
1987
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=19) then
1988
          r1_start <= '1';
1989
          card_state_reply <= card_state;
1990
          card_bustest_w <= '1';
1991
          card_state <= CARD_BTST;
1992
        end if;
1993
        -- Look for WRITE_DAT_UNTIL_STOP (CMD20)
1994
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=20) then
1995
          r1_start <= '1';
1996
          card_state_reply <= card_state;
1997
          card_adr <= sd_cmd_arg;
1998
          card_d_recv <= '1';
1999
          card_state <= CARD_RCV;
2000
        end if;
2001
        -- Look for SET_BLOCK_COUNT (CMD23)
2002
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=23) then
2003
          r1_start <= '1';
2004
          card_state_reply <= card_state;
2005
          card_block_count <= sd_cmd_arg(15 downto 0);
2006
          card_rel_wr_req <= sd_cmd_arg(31);
2007
        end if;
2008
        -- Look for WRITE_BLOCK (CMD24)
2009
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=24) then
2010
          r1_start <= '1';
2011
          card_state_reply <= card_state;
2012
          card_adr <= sd_cmd_arg;
2013
          card_block_count <= to_unsigned(1,card_block_count'length);
2014
          card_d_recv <= '1';
2015
          card_state <= CARD_RCV;
2016
        end if;
2017
        -- Look for WRITE_MULTIPLE_BLOCK (CMD25)
2018
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=25) then
2019
          r1_start <= '1';
2020
          card_state_reply <= card_state;
2021
          card_adr <= sd_cmd_arg;
2022
          card_d_recv <= '1';
2023
          card_state <= CARD_RCV;
2024
        end if;
2025
        -- Look for PROGRAM_CSD (CMD27)
2026
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=27) then
2027
          r1_start <= '1';
2028
          card_state_reply <= card_state;
2029
          card_state <= CARD_RCV;
2030
        end if;
2031
        -- Look for SET_WRITE_PROT (CMD28)
2032
        -- Currently there is no write protection in this emulated card.
2033
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=28 and IMPLEMENT_WRITE_PROT>0) then
2034
          r1_start <= '1';
2035
          card_state_reply <= card_state;
2036
          prg_dly_count <= to_unsigned(4000,prg_dly_count'length); -- Programming delay (card is busy!)
2037
          card_state <= CARD_PRG;
2038
        end if;
2039
        -- Look for CLR_WRITE_PROT (CMD29)
2040
        -- Currently there is no write protection in this emulated card.
2041
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=29 and IMPLEMENT_WRITE_PROT>0) then
2042
          r1_start <= '1';
2043
          card_state_reply <= card_state;
2044
          prg_dly_count <= to_unsigned(4000,prg_dly_count'length); -- Programming delay (card is busy!)
2045
          card_state <= CARD_PRG;
2046
        end if;
2047
        -- Look for SEND_WRITE_PROT (CMD30)
2048
        -- Currently there is no write protection in this emulated card.
2049
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=30 and IMPLEMENT_WRITE_PROT>0) then
2050
          r1_start <= '1';
2051
          card_state_reply <= card_state;
2052
          card_state <= CARD_DATA;
2053
        end if;
2054
        -- Look for SEND_WRITE_PROT_TYPE (CMD31)
2055
        -- Currently there is no write protection in this emulated card.
2056
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=31 and IMPLEMENT_WRITE_PROT>0) then
2057
          r1_start <= '1';
2058
          card_state_reply <= card_state;
2059
          card_state <= CARD_DATA;
2060
        end if;
2061
        -- Look for ERASE_GROUP_START (CMD35)
2062
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=35) then
2063
          r1_start <= '1';
2064
          card_state_reply <= card_state;
2065
          card_erase_group_start <= sd_cmd_arg;
2066
        end if;
2067
        -- Look for ERASE_GROUP_END (CMD36)
2068
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=36) then
2069
          r1_start <= '1';
2070
          card_state_reply <= card_state;
2071
          card_erase_group_end <= sd_cmd_arg;
2072
        end if;
2073
        -- Look for ERASE (CMD38)
2074
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=38) then
2075
          r1_start <= '1';
2076
          card_state_reply <= card_state;
2077
          prg_dly_count <= to_unsigned(4000,prg_dly_count'length); -- Programming delay for erase (card is busy!)
2078
          card_state <= CARD_PRG;
2079
        end if;
2080
        -- Look for LOCK_UNLOCK (CMD42)
2081
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=42) then
2082
          r1_start <= '1';
2083
          card_state_reply <= card_state;
2084
          card_state <= CARD_RCV;
2085
        end if;
2086
        -- Flag illegal commands
2087
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
2088
          if (sd_cmd_index< 6 or sd_cmd_index= 9 or sd_cmd_index=10 or
2089
              sd_cmd_index=12 or sd_cmd_index=14 or sd_cmd_index=21 or
2090
              sd_cmd_index=22 or sd_cmd_index=26 or sd_cmd_index=32 or
2091
              sd_cmd_index=33 or sd_cmd_index=34 or sd_cmd_index=37 or
2092
              sd_cmd_index=39 or sd_cmd_index=40 or sd_cmd_index=41 or
2093
              sd_cmd_index>42) then
2094
            cmd_err_reply <= '1';
2095
          end if;
2096
        end if;
2097
 
2098
      when CARD_DATA =>
2099
        -- Look for DESELECT_CARD (CMD7 without address match), which has no reply
2100
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='0' and sd_cmd_index=7) then
2101
          card_state <= CARD_STBY;
2102
        end if;
2103
        -- Look for STOP_TRANSMISSION (CMD12)
2104
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=12) then
2105
          r1_start <= '1';
2106
          card_state_reply <= card_state;
2107
          card_d_stop <= '1';
2108
          card_block_count <= to_unsigned(0,card_block_count'length);
2109
          card_state <= CARD_TRAN;
2110
        end if;
2111
        -- Look for SEND_STATUS (CMD13)
2112
        -- Per the spec, if a CRC error occurs, the card does not
2113
        -- respond.  Therefore, the CRC error status bit is not
2114
        -- reported until an R1 response is given to a later command
2115
        -- that is received correctly.
2116
        -- Hence, CMD13 is only executed if there is no CRC error.
2117
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
2118
          r1_start <= '1';
2119
          card_state_reply <= card_state;
2120
        end if;
2121
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
2122
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
2123
          card_state <= CARD_INA;
2124
        end if;
2125
        -- Flag illegal commands
2126
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
2127
          if (sd_cmd_index/= 7 and sd_cmd_index/=12 and sd_cmd_index/=13) then
2128
            cmd_err_reply <= '1';
2129
          end if;
2130
        end if;
2131
        -- check for various "operation complete" conditions
2132
        if (card_d_busy='0' and card_d_busy_r1='1') then -- falling edge
2133
          -- For completion of CMD8 (SEND_EXT_CSD), change states
2134
          -- Change states for CMD8, CMD11, CMD17, CMD18, CMD30 or CMD56(read).
2135
          -- To make this easy, do not filter on the command index, just
2136
          -- make the transition, assuming that the data transaction which
2137
          -- just ended resulted from one of the listed commands.
2138
          --if (sd_cmd_index=8) then
2139
          card_block_count <= to_unsigned(0,card_block_count'length);
2140
          card_state <= CARD_TRAN;
2141
          --end if;
2142
        end if;
2143
 
2144
      when CARD_BTST =>
2145
        -- Look for SEND_STATUS (CMD13)
2146
        -- Per the spec, if a CRC error occurs, the card does not
2147
        -- respond.  Therefore, the CRC error status bit is not
2148
        -- reported until an R1 response is given to a later command
2149
        -- that is received correctly.
2150
        -- Hence, CMD13 is only executed if there is no CRC error.
2151
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
2152
          r1_start <= '1';
2153
          card_state_reply <= card_state;
2154
        end if;
2155
        -- Look for BUSTEST_R (CMD14)
2156
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=14) then
2157
          r1_start <= '1';
2158
          card_state_reply <= card_state;
2159
          dreply_start_count <= to_unsigned(N_CD,D_BITS); -- Set the data readout delay counter
2160
          card_state <= CARD_TRAN;
2161
        end if;
2162
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
2163
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
2164
          card_state <= CARD_INA;
2165
        end if;
2166
        -- Flag illegal commands
2167
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
2168
          if (sd_cmd_index/=13 and sd_cmd_index/=14) then
2169
            cmd_err_reply <= '1';
2170
          end if;
2171
        end if;
2172
 
2173
      when CARD_RCV =>
2174
        -- Look for STOP_TRANSMISSION (CMD12)
2175
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=12) then
2176
          r1_start <= '1';
2177
          card_state_reply <= card_state;
2178
          card_d_stop <= '1';
2179
          card_block_count <= to_unsigned(0,card_block_count'length);
2180
          card_state <= CARD_PRG; -- sd_card_data_unit handles busy indication for this case.
2181
        end if;
2182
        -- Check for various "operation complete" conditions
2183
        if (card_d_busy='0' and card_d_busy_r1='1') then -- falling edge
2184
          -- Change states for CMD24, CMD25, CMD26, CMD27, CMD42 or CMD56(write).
2185
          -- To make this easy, do not filter on the command index, just
2186
          -- make the transition, assuming that the data transaction which
2187
          -- just ended resulted from one of the listed commands.
2188
          card_block_count <= to_unsigned(0,card_block_count'length);
2189
          card_state <= CARD_PRG; -- sd_card_data_unit handles busy indication for this case.
2190
        end if;
2191
        -- Look for SEND_STATUS (CMD13)
2192
        -- Per the spec, if a CRC error occurs, the card does not
2193
        -- respond.  Therefore, the CRC error status bit is not
2194
        -- reported until an R1 response is given to a later command
2195
        -- that is received correctly.
2196
        -- Hence, CMD13 is only executed if there is no CRC error.
2197
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
2198
          r1_start <= '1';
2199
          card_state_reply <= card_state;
2200
        end if;
2201
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
2202
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
2203
          card_state <= CARD_INA;
2204
        end if;
2205
        -- Flag illegal commands
2206
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
2207
          if (sd_cmd_index/=12 and sd_cmd_index/=13) then
2208
            cmd_err_reply <= '1';
2209
          end if;
2210
        end if;
2211
 
2212
      -- Within this state, use card_cmd_arg directly for command input, including the address.
2213
      when CARD_PRG =>
2214
        -- Look for DESELECT_CARD (CMD7 without address match), which has no reply
2215
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='0' and sd_cmd_index=7) then
2216
          card_state <= CARD_DIS;
2217
        end if;
2218
        -- Look for SEND_STATUS (CMD13)
2219
        -- Per the spec, if a CRC error occurs, the card does not
2220
        -- respond.  Therefore, the CRC error status bit is not
2221
        -- reported until an R1 response is given to a later command
2222
        -- that is received correctly.
2223
        -- Hence, CMD13 is only executed if there is no CRC error.
2224
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
2225
          r1_start <= '1';
2226
          card_state_reply <= card_state;
2227
        end if;
2228
        -- If the programming timer expires, then consider that the programming
2229
        -- operation is complete.  Return to CARD_TRAN state.  In a real card, this
2230
        -- CARD_PRG state would be actual flash programming time, but in our emulated
2231
        -- card, the programming time is merely emulated.
2232
        -- Delay, with accompanying card busy indication, is implemented explicitly
2233
        -- here, by prg_dly_count, for:
2234
        --   CMD6, CMD28, CMD29 and CMD38.
2235
        -- A signal from sd_card_data_unit, controls the assertion of the card
2236
        -- busy indicator during CARD_RCV state.
2237
        if (prg_dly_count=0) then
2238
          card_state <= CARD_TRAN;
2239
        end if;
2240
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
2241
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
2242
          prg_dly_count <= (others=>'0');
2243
          card_state <= CARD_INA;
2244
        end if;
2245
        -- A host should not issue this while busy is active and card is in this state
2246
        -- Look for WRITE_BLOCK (CMD24)
2247
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=24) then
2248
          r1_start <= '1';
2249
          card_state_reply <= card_state;
2250
          prg_dly_count <= (others=>'0');
2251
          card_state <= CARD_RCV;
2252
          card_adr <= sd_cmd_arg;
2253
        end if;
2254
        -- A host should not issue this while busy is active and card is in this state
2255
        -- Look for WRITE_MULTIPLE_BLOCK (CMD25)
2256
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=25) then
2257
          r1_start <= '1';
2258
          card_state_reply <= card_state;
2259
          prg_dly_count <= (others=>'0');
2260
          card_state <= CARD_RCV;
2261
          card_adr <= sd_cmd_arg;
2262
        end if;
2263
        -- Flag illegal commands
2264
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
2265
          if (sd_cmd_index/= 7 and sd_cmd_index/=13 and
2266
              sd_cmd_index/=24 and sd_cmd_index/=25) then
2267
            cmd_err_reply <= '1';
2268
          end if;
2269
        end if;
2270
 
2271
      when CARD_DIS =>
2272
        -- Look for SELECT_CARD (CMD7)
2273
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=7) then
2274
          r1_start <= '1';
2275
          card_state_reply <= card_state;
2276
          prg_dly_count <= to_unsigned(4000,prg_dly_count'length);
2277
          card_state <= CARD_PRG;
2278
        end if;
2279
        -- Look for SEND_STATUS (CMD13)
2280
        -- Per the spec, if a CRC error occurs, the card does not
2281
        -- respond.  Therefore, the CRC error status bit is not
2282
        -- reported until an R1 response is given to a later command
2283
        -- that is received correctly.
2284
        -- Hence, CMD13 is only executed if there is no CRC error.
2285
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=13) then
2286
          r1_start <= '1';
2287
          card_state_reply <= card_state;
2288
        end if;
2289
        -- Look for GO_INACTIVE_STATE (CMD15), which has no reply
2290
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=15) then
2291
          card_state <= CARD_INA;
2292
        end if;
2293
        -- Flag illegal commands
2294
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_neverbad='0') then
2295
          if (sd_cmd_index/= 7 and sd_cmd_index/=13) then
2296
            cmd_err_reply <= '1';
2297
          end if;
2298
        end if;
2299
 
2300
      when CARD_INA =>
2301
        -- This state is pretty much a "dead end"
2302
        -- Only reset can get out of it.
2303
        null;
2304
 
2305
      when CARD_SLP =>
2306
        -- Look for SLEEP_AWAKE (CMD5), which toggles between CARD_STBY and CARD_SLP
2307
        if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_adr_match='1' and sd_cmd_index=5) then
2308
          if sd_cmd_arg(15)='0' then
2309
            r1_start <= '1';
2310
            card_state_reply <= card_state;
2311
            card_state <= CARD_STBY;
2312
          end if;
2313
        end if;
2314
        -- Flag illegal commands
2315
        if (sd_cmd_done='1' and sd_cmd_crc_err='0') then
2316
          if (sd_cmd_index/= 0 and sd_cmd_index/= 5) then
2317
            cmd_err_reply <= '1';
2318
          end if;
2319
        end if;
2320
 
2321
      when CARD_IRQ =>
2322
        -- In reality, there is supposed to be some IRQ event which occurs
2323
        -- before the R5 response is given.  No such events are implemented
2324
        -- in this card, but if they were, it would be cool to just reset a
2325
        -- timer or counter, in the manner of kicking a watchdog timer, until
2326
        -- the event trigger occurs, then issue the r5_start pulse.
2327
        -- The host has a mechanism for generating a "pseudo reply" at RCA
2328
        -- of 0x0000, which is supposed to cause this state to exit.
2329
        -- 
2330
        -- "Any start bit on the bus" exits this state
2331
        -- The method used here is to wait for the command to finish
2332
        -- being received, instead of actually looking for the start
2333
        -- bit...
2334
        if (sd_cmd_done='1' and sd_cmd_crc_err='0') then
2335
          if (sd_cmd_index/=55) then
2336
            card_state <= CARD_STBY;
2337
          end if;
2338
        end if;
2339
 
2340
      when others =>
2341
        card_state <= CARD_IDLE;
2342
    end case;
2343
    -- CMD0 affects nearly all states the same way, so it is coded here.
2344
    if (sd_cmd_done='1' and sd_cmd_crc_err='0' and sd_cmd_index=0) then
2345
      if (card_state/=CARD_INA and card_state/=CARD_IRQ) then
2346
        ocr_pwrup_done <= '0';
2347
        card_dbus_size <= "00"; -- Revert back to single data bit
2348
        rca <= str2u("0001",16);
2349
        dsr <= str2u("0404",16);
2350
        card_cmd_set <= (others=>'0');
2351
        if (sd_cmd_arg=0) then
2352
          card_state <= CARD_IDLE;
2353
        else
2354
          card_state <= CARD_PRE_IDLE;
2355
        end if;
2356
      end if;
2357
    end if;
2358
  end if; -- sd_clk_i
2359
end process;
2360
 
2361
--------------------------------------------
2362
-- Response Transmitters
2363
 
2364
  R1_responder: sd_card_responder
2365
  generic map(
2366
    N_CR          => N_CR,
2367
    RESP_PYLD_LEN => 32,
2368
    CRC_OFFSET    =>  0, -- Start CRC calculation after this many bits
2369
    CRC_SEND_ONES =>  0
2370
  )
2371
  port map(
2372
    -- Asynchronous reset
2373
    sys_rst_n     => sys_rst_n,
2374
    -- SD/MMC card command signals
2375
    sd_clk_i      => sd_clk_i,
2376
    sd_cmd_o      => r1_cmd,
2377
    sd_cmd_oe_o   => r1_cmd_oe,
2378
    -- Response inputs
2379
    resp_index_i  => sd_cmd_index,
2380
    resp_pyld_i   => reported_status,
2381
    respond_i     => r1_start,
2382
    -- Status and done indicator
2383
    done_o        => r1_done, -- A one clock long pulse
2384
    busy_o        => open
2385
  );
2386
  -- Create a status report.  Some fields are read only.
2387
  reported_status(31 downto 24) <= status(31 downto 24);
2388
  reported_status(23) <= crc_err_reply;
2389
  reported_status(22) <= cmd_err_reply;
2390
  reported_status(21 downto 13) <= status(21 downto 13);
2391
  with card_state_reply select
2392
  reported_status(12 downto 9) <=
2393
    "0000" when CARD_IDLE,
2394
    "0001" when CARD_READY,
2395
    "0010" when CARD_IDENT,
2396
    "0011" when CARD_STBY,
2397
    "0100" when CARD_TRAN,
2398
    "0101" when CARD_DATA,
2399
    "0110" when CARD_RCV,
2400
    "0111" when CARD_PRG,
2401
    "1000" when CARD_DIS,
2402
    "1001" when CARD_BTST,
2403
    "1010" when CARD_SLP,
2404
    "1011" when others;
2405
  reported_status(8) <= '1'; -- Buffers are empty!
2406
  reported_status(7 downto 0) <= status(7 downto 0);
2407
 
2408
  R2_CID_responder: sd_card_responder
2409
  generic map(
2410
    N_CR          => N_CR,
2411
    RESP_PYLD_LEN => 120,
2412
    CRC_OFFSET    =>   8, -- Start CRC calculation after this many bits
2413
    CRC_SEND_ONES =>   0
2414
  )
2415
  port map(
2416
    -- Asynchronous reset
2417
    sys_rst_n     => sys_rst_n,
2418
    -- SD/MMC card command signals
2419
    sd_clk_i      => sd_clk_i,
2420
    sd_cmd_o      => r2_cid_cmd,
2421
    sd_cmd_oe_o   => r2_cid_cmd_oe,
2422
    -- Response inputs
2423
    resp_index_i  => "111111",
2424
    resp_pyld_i   => cid_resp,
2425
    respond_i     => r2_cid_start,
2426
    -- Status and done indicator
2427
    done_o        => r2_cid_done, -- A one clock long pulse
2428
    busy_o        => open
2429
  );
2430
 
2431
  R2_CSD_responder: sd_card_responder
2432
  generic map(
2433
    N_CR          => N_CR,
2434
    RESP_PYLD_LEN => 120,
2435
    CRC_OFFSET    =>   8, -- Start CRC calculation after this many bits
2436
    CRC_SEND_ONES =>   0
2437
  )
2438
  port map(
2439
    -- Asynchronous reset
2440
    sys_rst_n     => sys_rst_n,
2441
    -- SD/MMC card command signals
2442
    sd_clk_i      => sd_clk_i,
2443
    sd_cmd_o      => r2_csd_cmd,
2444
    sd_cmd_oe_o   => r2_csd_cmd_oe,
2445
    -- Response inputs
2446
    resp_index_i  => "111111",
2447
    resp_pyld_i   => csd_resp,
2448
    respond_i     => r2_csd_start,
2449
    -- Status and done indicator
2450
    done_o        => r2_csd_done, -- A one clock long pulse
2451
    busy_o        => open
2452
  );
2453
 
2454
  R3_responder: sd_card_responder
2455
  generic map(
2456
    N_CR          => N_ID,
2457
    RESP_PYLD_LEN => 32,
2458
    CRC_OFFSET    =>  0, -- Start CRC calculation after this many bits
2459
    CRC_SEND_ONES =>  1
2460
  )
2461
  port map(
2462
    -- Asynchronous reset
2463
    sys_rst_n     => sys_rst_n,
2464
    -- SD/MMC card command signals
2465
    sd_clk_i      => sd_clk_i,
2466
    sd_cmd_o      => r3_cmd,
2467
    sd_cmd_oe_o   => r3_cmd_oe,
2468
    -- Response inputs
2469
    resp_index_i  => "111111",
2470
    resp_pyld_i   => ocr,
2471
    respond_i     => r3_start,
2472
    -- Status and done indicator
2473
    done_o        => r3_done, -- A one clock long pulse
2474
    busy_o        => open
2475
  );
2476
 
2477
  R4_responder: sd_card_responder
2478
  generic map(
2479
    N_CR          => N_CR,
2480
    RESP_PYLD_LEN => 32,
2481
    CRC_OFFSET    =>  0, -- Start CRC calculation after this many bits
2482
    CRC_SEND_ONES =>  0
2483
  )
2484
  port map(
2485
    -- Asynchronous reset
2486
    sys_rst_n     => sys_rst_n,
2487
    -- SD/MMC card command signals
2488
    sd_clk_i      => sd_clk_i,
2489
    sd_cmd_o      => r4_cmd,
2490
    sd_cmd_oe_o   => r4_cmd_oe,
2491
    -- Response inputs
2492
    resp_index_i  => "100111",
2493
    resp_pyld_i   => fast_io_reg_rd,
2494
    respond_i     => r4_start,
2495
    -- Status and done indicator
2496
    done_o        => r4_done, -- A one clock long pulse
2497
    busy_o        => open
2498
  );
2499
  -- Assign response for R4
2500
  fast_io_reg_rd <= rca & r4_status & r4_reg_adr & r4_reg_dat_rd;
2501
 
2502
  -- An interesting idea : Add a CMD39 "R4" register read/write bus port
2503
  -- to the top level of this module, if this function of reading and
2504
  -- writing special registers becomes important and/or useful enough
2505
  -- to justify the effort.
2506
 
2507
  -- CMD39 "FAST_IO" register read mux
2508
  with to_integer(r4_reg_adr) select
2509
  r4_reg_dat_rd <=
2510
    r4_reg_0         when 0,
2511
    r4_reg_1         when 1,
2512
    "10100101"       when others;
2513
 
2514
  -- CMD39 "FAST_IO" register writes are handled here
2515
  reg_proc: process(sd_clk_i, sys_rst_n)
2516
  begin
2517
    if (sys_rst_n='0') then
2518
      r4_reg_0 <= (others=>'0');
2519
      r4_reg_1 <= (others=>'0');
2520
    elsif (sd_clk_i'event and sd_clk_i='1') then
2521
      if (r4_reg_we='1') then
2522
        case to_integer(adr_i) is
2523
          when 0 =>
2524
            r4_reg_0 <= r4_reg_dat_wr;
2525
          when 1 =>
2526
            r4_reg_1 <= r4_reg_dat_wr;
2527
          when others => null;
2528
        end case;
2529
      end if;
2530
    end if; -- sd_clk_i
2531
  end process;
2532
 
2533
 
2534
  R5_responder: sd_card_responder
2535
  generic map(
2536
    N_CR          => N_CR,
2537
    RESP_PYLD_LEN => 32,
2538
    CRC_OFFSET    =>  0, -- Start CRC calculation after this many bits
2539
    CRC_SEND_ONES =>  0
2540
  )
2541
  port map(
2542
    -- Asynchronous reset
2543
    sys_rst_n     => sys_rst_n,
2544
    -- SD/MMC card command signals
2545
    sd_clk_i      => sd_clk_i,
2546
    sd_cmd_o      => r5_cmd,
2547
    sd_cmd_oe_o   => r5_cmd_oe,
2548
    -- Response inputs
2549
    resp_index_i  => "101000",
2550
    resp_pyld_i   => irq_response,
2551
    respond_i     => r5_start,
2552
    -- Status and done indicator
2553
    done_o        => r5_done, -- A one clock long pulse
2554
    busy_o        => open
2555
  );
2556
  -- Assign response for R5
2557
  irq_response <= rca & to_unsigned(0,16);
2558
 
2559
-- Select which response gets driven out
2560
sd_cmd_o <= r1_cmd     when r1_cmd_oe='1' else
2561
            r2_cid_cmd when r2_cid_cmd_oe='1' else
2562
            r2_csd_cmd when r2_csd_cmd_oe='1' else
2563
            r3_cmd     when r3_cmd_oe='1' else
2564
            r4_cmd     when r4_cmd_oe='1' and USE_R4_RESPONSE>0 else
2565
            r5_cmd     when r5_cmd_oe='1' and USE_R5_RESPONSE>0 else
2566
            '1';
2567
 
2568
  -- NOTE: If multiple responders are active at the same time, then
2569
  --       the drive signal may not be correct, so don't do that!
2570
sd_cmd_oe_l <= '1' when r1_cmd_oe='1' or r2_cid_cmd_oe='1' or
2571
                        r2_csd_cmd_oe='1' or r3_cmd_oe='1' or
2572
                        (r4_cmd_oe='1' and USE_R4_RESPONSE>0) or
2573
                        (r5_cmd_oe='1' and USE_R5_RESPONSE>0) else
2574
                '0';
2575
sd_cmd_oe_o <= sd_cmd_oe_l;
2576
 
2577
sd_od_mode_o <= '1' when card_state=CARD_INA      or card_state=CARD_PRE_IDLE or
2578
                         card_state=CARD_PRE_BOOT or card_state=CARD_IDLE     or
2579
                         card_state=CARD_READY    or card_state=CARD_IDENT    or
2580
                         card_state=CARD_IRQ else '0';
2581
 
2582
-- SD/MMC data handling unit
2583
  sd_card_d_handler : sd_card_data_unit
2584
  generic map(
2585
    BLK_PRG_TIME => 200, -- Number of clocks to program a sector in FLASH (emulated)
2586
    BLKSIZE_W    => card_blocklen'length,
2587
    BLKCNT_W     => card_block_count'length
2588
  )
2589
  port map(
2590
    sd_clk_i      => sd_clk_i,
2591
    sys_rst_n     => sys_rst_n,
2592
    --Tx Fifo
2593
    tx_dat_i      => card_tx_dat,
2594
    tx_dat_rd_o   => card_tx_dat_rd,
2595
    --Rx Fifo
2596
    rx_dat_o      => buf_dat_o,
2597
    rx_dat_we_o   => buf_dat_we_l,
2598
    --SD data
2599
    sd_dat_i      => sd_dat_i,
2600
    sd_dat_o      => sd_dat_unbusy,
2601
    sd_dat_oe_o   => sd_dat_oe_unbusy,
2602
    --Control signals
2603
    blksize_i     => card_blocklen,
2604
    bus_size_i    => card_dbus_size,
2605
    blkcnt_i      => card_block_count,
2606
    continuous_i  => card_d_continuous,
2607
    d_stop_i      => card_d_stop,
2608
    d_read_i      => card_d_recv,
2609
    d_write_i     => card_d_send,
2610
    bustest_w_i   => card_bustest_w, -- Receives bustest pattern
2611
    bustest_r_i   => card_bustest_r, -- Sends bustest pattern out
2612
    sd_dat_busy_o => card_sd_dat_busy,
2613
    fsm_busy_o    => card_d_busy,
2614
    crc_ok_o      => card_d_crc_ok
2615
  );
2616
 
2617
-- Indicate when the card is busy, based on state of this state machine,
2618
-- and the state in sd_card_data_unit.  The output enable signal is also
2619
-- controlled, to ensure that the busy indicator is asserted.
2620
sd_dat_o <= (sd_dat_unbusy and "11111110") when card_sd_dat_busy='1' or (card_state=CARD_PRG and prg_dly_count>0) else sd_dat_unbusy;
2621
sd_dat_oe_o <= '1' when card_state=CARD_PRG and prg_dly_count>0 else sd_dat_oe_unbusy;
2622
 
2623
-- Provide special override for bus size, when BUSTEST is being used.
2624
sd_dat_siz_o <= "10" when sd_cmd_index=14 else card_dbus_size; -- Drive entire bus for BUSTEST_R response
2625
 
2626
-- Provide write enable output for data buffers
2627
buf_dat_we_o <= buf_dat_we_l;
2628
 
2629
-- Provide address to be used for steering data to and from
2630
-- the correct FIFO buffers.
2631
  adr_offset_proc: process(sd_clk_i, sys_rst_n)
2632
  begin
2633
    if (sys_rst_n='0') then
2634
      adr_offset <= (others=>'0');
2635
    elsif (sd_clk_i'event and sd_clk_i='1') then
2636
      if (buf_dat_we_l='1' or (card_tx_dat_rd='1' and sd_cmd_index/=8)) then -- Do not increment for CMD8 (EXT_CSD_READ)
2637
        adr_offset <= adr_offset+1;
2638
      end if;
2639
      if (card_d_recv='1' or card_d_send='1') then
2640
        adr_offset <= (others=>'0');
2641
      end if;
2642
    end if; -- sd_clk_i
2643
  end process;
2644
 
2645
buf_adr_o <= card_adr(buf_adr_o'length-1 downto 0) + adr_offset;
2646
 
2647
-- Select which data source is transmitted.
2648
card_tx_dat <= ext_csd_sd_dat_rd when sd_cmd_index=8 else buf_dat_i; -- CMD8 returns EXT_CSD as data...
2649
buf_dat_rd_o <= '0' when sd_cmd_index=8 else card_tx_dat_rd;
2650
 
2651
-- Indicate to sd_card_data_unit when continous data transfer is needed
2652
card_d_continuous <= '1' when (sd_cmd_index=11 or sd_cmd_index=20) else '0';
2653
-- Start a transmit operation, when needed
2654
card_d_send <= '1' when (sd_cmd_index=8 or sd_cmd_index=11 or sd_cmd_index=17 or sd_cmd_index=18) and dreply_start_count=1 else '0';
2655
-- Start sending bustest response data, when needed
2656
card_bustest_r <= '1' when sd_cmd_index=14 and dreply_start_count=1 else '0';
2657
 
2658
end beh;
2659
 
2660
-------------------------------------------------------------------------------
2661
-- MMC Data Pipe
2662
-------------------------------------------------------------------------------
2663
--
2664
-- Author: John Clayton
2665
-- Update: May  12, 2016 Wrote description and assembled the needed modules
2666
--
2667
-- Description
2668
-------------------------------------------------------------------------------
2669
-- This module is meant to emulate an SD/MMC card, but instead of having a
2670
-- vast FLASH based storage array, it provides a simple set of data FIFOs
2671
-- and a small RAM.
2672
--
2673
-- MMC sectors of 512 bytes are mapped into the RAM.  These are the lowest
2674
-- addresses of the card address space.  All addresses higher than the end
2675
-- of the RAM block are mapped to the FIFOs.  Data written to the card ends
2676
-- up in one FIFO, while data read from the card is taken from the other FIFO.
2677
--
2678
-- The unit is currently set up to use MMC byte addressing mode.  Sector
2679
-- addressing could also be enabled if desired.  Just set the EXT_CSD correctly,
2680
-- and then change the generic OCR_USE_SECTOR_MODE to a nonzero value on the
2681
-- sd_card_emulator unit.
2682
--
2683
-- Note that the FIFOs are "clock domain crossing" type.  The thing to remember
2684
-- with them is that the fifo empty, full and fill_level status is not
2685
-- synchronized to the local clock domain.  The user is free to perform that
2686
-- synchronization or not, as desired, and incur the additional delay that it
2687
-- brings, or not.
2688
--
2689
 
2690
library IEEE;
2691
use IEEE.STD_LOGIC_1164.ALL;
2692
use IEEE.NUMERIC_STD.ALL;
2693
 
2694
library work;
2695
use work.fifo_pack.all;
2696
use work.convert_pack.all;
2697
use work.sd_card_pack.all;
2698
use work.block_ram_pack.all;
2699
 
2700
  entity mmc_data_pipe is
2701
  generic (
2702
    EXT_CSD_INIT_FILE : string  := "ext_csd_init.txt"; -- Initial contents of EXT_CSD
2703
    FIFO_DEPTH        : integer := 2048;
2704
    FILL_LEVEL_BITS   : integer :=   12; -- Should be at least int(floor(log2(FIFO_DEPTH))+1.0)
2705
    RAM_ADR_WIDTH     : integer :=   14  -- 16 Kilobytes
2706
  );
2707
  port (
2708
 
2709
    -- Asynchronous reset
2710
    sys_rst_n     : in  std_logic;
2711
    sys_clk       : in  std_logic;
2712
 
2713
    -- Bus interface
2714
    adr_i         : in  unsigned(3 downto 0);
2715
    sel_i         : in  std_logic;
2716
    we_i          : in  std_logic;
2717
    dat_i         : in  unsigned(31 downto 0);
2718
    dat_o         : out unsigned(31 downto 0);
2719
    ack_o         : out std_logic;
2720
 
2721
    -- SD/MMC card signals
2722
    mmc_clk_i     : in  std_logic;
2723
    mmc_cmd_i     : in  std_logic;
2724
    mmc_cmd_o     : out std_logic;
2725
    mmc_cmd_oe_o  : out std_logic;
2726
    mmc_od_mode_o : out std_logic; -- Open drain mode
2727
    mmc_dat_i     : in  unsigned(7 downto 0);
2728
    mmc_dat_o     : out unsigned(7 downto 0);
2729
    mmc_dat_oe_o  : out std_logic;
2730
    mmc_dat_siz_o : out unsigned(1 downto 0);
2731
 
2732
    -- Data Pipe FIFOs
2733
    wr_clk_i      : in  std_logic;
2734
    wr_clk_en_i   : in  std_logic;
2735
    wr_reset_i    : in  std_logic;  -- Synchronous
2736
    wr_en_i       : in  std_logic;
2737
    wr_dat_i      : in  unsigned(7 downto 0);
2738
    wr_fifo_level : out unsigned(FILL_LEVEL_BITS-1 downto 0);
2739
    wr_fifo_full  : out std_logic;
2740
    wr_fifo_empty : out std_logic;
2741
 
2742
    rd_clk_i      : in  std_logic;
2743
    rd_clk_en_i   : in  std_logic;
2744
    rd_reset_i    : in  std_logic;  -- Synchronous
2745
    rd_en_i       : in  std_logic;
2746
    rd_dat_o      : out unsigned(7 downto 0);
2747
    rd_fifo_level : out unsigned(FILL_LEVEL_BITS-1 downto 0);
2748
    rd_fifo_full  : out std_logic;
2749
    rd_fifo_empty : out std_logic;
2750
 
2751
    -- Data Pipe RAM
2752
    ram_clk_i     : in  std_logic;
2753
    ram_clk_en_i  : in  std_logic;
2754
    ram_adr_i     : in  unsigned(RAM_ADR_WIDTH-1 downto 0);
2755
    ram_we_i      : in  std_logic;
2756
    ram_dat_i     : in  unsigned(7 downto 0);
2757
    ram_dat_o     : out unsigned(7 downto 0)
2758
  );
2759
  end mmc_data_pipe;
2760
 
2761
architecture beh of mmc_data_pipe is
2762
 
2763
-- constants
2764
constant N_ID   : integer := 5; -- Number of clocks before response in card ID mode
2765
 
2766
-- signals
2767
signal buf_adr             : unsigned(31 downto 0);
2768
signal buf_dat_from_host   : unsigned(7 downto 0);
2769
signal buf_dat_we          : std_logic;
2770
signal buf_dat_to_host     : unsigned(7 downto 0);
2771
signal buf_dat_rd          : std_logic;
2772
signal fifo_dat_rd         : std_logic;
2773
signal fifo_dat_to_host    : unsigned(7 downto 0);
2774
signal ram_dat_to_host     : unsigned(7 downto 0);
2775
signal fifo_we             : std_logic;
2776
signal ram_we              : std_logic;
2777
 
2778
begin
2779
 
2780
-- Instantiate the MMC
2781
mmc_1 : sd_card_emulator
2782
  generic map(
2783
    USE_R4_RESPONSE      => 0, -- Fast I/O read/write (app specific)
2784
    USE_R5_RESPONSE      => 0, -- Interrupt Request Mode
2785
    EXT_CSD_INIT_FILE    => EXT_CSD_INIT_FILE, -- Initial contents of EXT_CSD
2786
    OCR_USE_DUAL_VOLTAGE => 0,
2787
    OCR_USE_SECTOR_MODE  => 0,
2788
    CID_MID    => str2u("45",8), -- Manufacturer ID
2789
    CID_OID    => str2u("77",8), -- OEM ID
2790
    CID_CBX    => "00", -- 0=Card, 1=BGA, 2=Package On Package
2791
    CID_PNM    => str2u("434152444953",48), -- Product Name, 6 ASCII chars
2792
    CID_PRV    => str2u("01",8), -- Product Rev (2 BCD digits, e.g. 6.2=0x62)
2793
    CID_PSN    => str2u("00000012",32), -- Product serial number
2794
    CID_MDT    => str2u("43",8), -- Manufacture Date (Jan=1, 1997=0, e.g. Apr. 2000=0x43)
2795
    DEF_STAT   => str2u("00000000",32), -- Read Write, R_0
2796
    CSD_WORD_3 => str2u("905E002A",32), -- See MMC spec
2797
    CSD_WORD_2 => str2u("1F5903D3",32), -- See MMC spec
2798
    CSD_WORD_1 => str2u("EDB707FF",32), -- See MMC spec
2799
    CSD_WORD_0 => str2u("96400000",32), -- See MMC spec, bits (7:0) not used
2800
    DEF_R_Z    => str2u("33333333",32)  -- Value returned for nonexistent registers
2801
  )
2802
  port map(
2803
 
2804
    -- Asynchronous reset
2805
    sys_rst_n     => sys_rst_n,
2806
    sys_clk       => sys_clk,
2807
 
2808
    -- Bus interface
2809
    adr_i         => adr_i,
2810
    sel_i         => sel_i,
2811
    we_i          => we_i,
2812
    dat_i         => dat_i,
2813
    dat_o         => dat_o,
2814
    ack_o         => ack_o,
2815
 
2816
    -- SD/MMC card command signals
2817
    sd_clk_i      => mmc_clk_i,
2818
    sd_cmd_i      => mmc_cmd_i,
2819
    sd_cmd_o      => mmc_cmd_o,
2820
    sd_cmd_oe_o   => mmc_cmd_oe_o,
2821
    sd_od_mode_o  => mmc_od_mode_o, -- Open drain mode
2822
    sd_dat_i      => mmc_dat_i,
2823
    sd_dat_o      => mmc_dat_o,
2824
    sd_dat_oe_o   => mmc_dat_oe_o,
2825
    sd_dat_siz_o  => mmc_dat_siz_o,
2826
 
2827
    -- Data FIFO interface
2828
    buf_adr_o     => buf_adr,
2829
    buf_dat_o     => buf_dat_from_host,
2830
    buf_dat_we_o  => buf_dat_we,
2831
    buf_dat_i     => buf_dat_to_host,
2832
    buf_dat_rd_o  => buf_dat_rd  -- Used by FIFOs only.
2833
  );
2834
  -- Select the appropriate data to return to the host
2835
  buf_dat_to_host <= ram_dat_to_host when buf_adr(buf_adr'length-1 downto RAM_ADR_WIDTH)=0 else fifo_dat_to_host;
2836
 
2837
-- Instantiate the FIFOs
2838
  fifo_from_mmc : swiss_army_fifo_cdc
2839
    generic map(
2840
      USE_BRAM         => 1, -- Set to nonzero value for BRAM, zero for distributed RAM
2841
      WIDTH            => 8,
2842
      DEPTH            => FIFO_DEPTH,
2843
      FILL_LEVEL_BITS  => FILL_LEVEL_BITS, -- Should be at least int(floor(log2(DEPTH))+1.0)
2844
      PF_FULL_POINT    => FIFO_DEPTH-1,
2845
      PF_FLAG_POINT    => 512,
2846
      PF_EMPTY_POINT   => 1
2847
    )
2848
    port map(
2849
      sys_rst_n        => sys_rst_n, -- Asynchronous
2850
 
2851
      wr_clk_i         => mmc_clk_i,
2852
      wr_clk_en_i      => '1',
2853
      wr_reset_i       => '0',  -- Synchronous
2854
      wr_en_i          => fifo_we,
2855
      wr_dat_i         => buf_dat_from_host,
2856
      wr_fifo_level    => open,
2857
      wr_fifo_full     => open,
2858
      wr_fifo_empty    => open,
2859
      wr_fifo_pf_full  => open,
2860
      wr_fifo_pf_flag  => open,
2861
      wr_fifo_pf_empty => open,
2862
 
2863
      rd_clk_i         => rd_clk_i,
2864
      rd_clk_en_i      => rd_clk_en_i,
2865
      rd_reset_i       => rd_reset_i,  -- Synchronous
2866
      rd_en_i          => rd_en_i,
2867
      rd_dat_o         => rd_dat_o,
2868
      rd_fifo_level    => rd_fifo_level,
2869
      rd_fifo_full     => rd_fifo_full,
2870
      rd_fifo_empty    => rd_fifo_empty,
2871
      rd_fifo_pf_full  => open,
2872
      rd_fifo_pf_flag  => open,
2873
      rd_fifo_pf_empty => open
2874
 
2875
    );
2876
    fifo_we <= '1' when buf_dat_we='1' and buf_adr(buf_adr'length-1 downto RAM_ADR_WIDTH)>0 else '0';
2877
 
2878
  fifo_to_mmc : swiss_army_fifo_cdc
2879
    generic map(
2880
      USE_BRAM         => 1, -- Set to nonzero value for BRAM, zero for distributed RAM
2881
      WIDTH            => 8,
2882
      DEPTH            => FIFO_DEPTH,
2883
      FILL_LEVEL_BITS  => FILL_LEVEL_BITS, -- Should be at least int(floor(log2(DEPTH))+1.0)
2884
      PF_FULL_POINT    => FIFO_DEPTH-1,
2885
      PF_FLAG_POINT    => 512,
2886
      PF_EMPTY_POINT   => 1
2887
    )
2888
    port map(
2889
      sys_rst_n        => sys_rst_n, -- Asynchronous
2890
 
2891
      wr_clk_i         => wr_clk_i,
2892
      wr_clk_en_i      => wr_clk_en_i,
2893
      wr_reset_i       => wr_reset_i,  -- Synchronous
2894
      wr_en_i          => wr_en_i,
2895
      wr_dat_i         => wr_dat_i,
2896
      wr_fifo_level    => wr_fifo_level,
2897
      wr_fifo_full     => wr_fifo_full,
2898
      wr_fifo_empty    => wr_fifo_empty,
2899
      wr_fifo_pf_full  => open,
2900
      wr_fifo_pf_flag  => open,
2901
      wr_fifo_pf_empty => open,
2902
 
2903
      rd_clk_i         => mmc_clk_i,
2904
      rd_clk_en_i      => '1',
2905
      rd_reset_i       => '0',  -- Synchronous
2906
      rd_en_i          => fifo_dat_rd,
2907
      rd_dat_o         => fifo_dat_to_host,
2908
      rd_fifo_level    => open,
2909
      rd_fifo_full     => open,
2910
      rd_fifo_empty    => open,
2911
      rd_fifo_pf_full  => open,
2912
      rd_fifo_pf_flag  => open,
2913
      rd_fifo_pf_empty => open
2914
    );
2915
    fifo_dat_rd <= '1' when buf_dat_rd='1' and buf_adr(buf_adr'length-1 downto RAM_ADR_WIDTH)>0 else '0';
2916
 
2917
-- Instantiate the RAM block
2918
-- Port A is the MMC side.
2919
-- Port B is the user side.
2920
  pipe_ram : swiss_army_ram
2921
    generic map(
2922
      USE_BRAM  => 1,
2923
      WRITETHRU => 0, -- Set to nonzero value for writethrough mode
2924
      USE_FILE  => 0, -- Set to nonzero value to use INIT_FILE
2925
      INIT_VAL  => 18,
2926
      INIT_SEL  => 0, -- No generate loop here
2927
      INIT_FILE => ".\foo.txt", -- ASCII hexadecimal initialization file name
2928
      FIL_WIDTH => 32, -- Bit width of init file lines
2929
      ADR_WIDTH => RAM_ADR_WIDTH,
2930
      DAT_WIDTH => 8
2931
    )
2932
    port map (
2933
       clk_a    => mmc_clk_i,
2934
       clk_b    => ram_clk_i,
2935
 
2936
       adr_a_i  => buf_adr(RAM_ADR_WIDTH-1 downto 0),
2937
       adr_b_i  => ram_adr_i,
2938
 
2939
       we_a_i   => ram_we,
2940
       en_a_i   => '1',
2941
       dat_a_i  => buf_dat_from_host,
2942
       dat_a_o  => ram_dat_to_host,
2943
 
2944
       we_b_i   => ram_we_i,
2945
       en_b_i   => ram_clk_en_i,
2946
       dat_b_i  => ram_dat_i,
2947
       dat_b_o  => ram_dat_o
2948
    );
2949
    ram_we <= '1' when buf_dat_we='1' and buf_adr(buf_adr'length-1 downto RAM_ADR_WIDTH)=0 else '0';
2950
 
2951
end beh;
2952
 

powered by: WebSVN 2.1.0

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