Guest User

DC EMU New SD Driver (OzOnE)

a guest
Jul 22nd, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     case (state)
  2.             // *** INIT SD Card / Driver ***
  3.             0: begin
  4.                 sd_rst_i <= 1'b0;                   // Bring the SD Card driver out of reset (active HIGH)!
  5.  
  6.                 sd_address_i <= `TRANS_TYPE_REG;    // Select the SD Transmission Type reg.
  7.                 sd_data_i <= `INIT_SD;              // Initialize the SD Card.
  8.                 sd_we_i <= 1'b1;                    // WRITE (1).
  9.                 sd_strobe_i <= 1'b1;                // Select the SD Driver.
  10.  
  11.                 state <= 10'd1;
  12.             end
  13.  
  14.             1: if (sd_ack_o) begin                  // Wait for SD Driver ack (for previous WRITE).
  15.                 sd_address_i <= `TRANS_CTRL_REG;    // Select the SD Transmission Control reg.
  16.                 sd_data_i <= `TRANS_START;          // Initialize the SD Card.
  17.                 sd_we_i <= 1'b1;                    // WRITE (1).
  18. //              sd_strobe_i <= 1'b1;                // Select the SD Driver. (still set from previous state!)
  19.                
  20.                 state <= 10'd2;
  21.             end
  22.  
  23.             2: if (sd_ack_o) begin                  // Wait for SD Driver ack (for previous WRITE).
  24.                 sd_address_i <= `TRANS_STS_REG;     // Read the SD Transmission Status reg.
  25.                 sd_we_i <= 1'b0;                    // READ (0).
  26. //              sd_strobe_i <= 1'b1;                // Select the SD Driver. (still set from previous state!)
  27.            
  28.                 state <= 10'd3;
  29.             end
  30.  
  31.             3: if (sd_ack_o && !(sd_data_o == `TRANS_BUSY)) begin   // Wait for SD Driver ack, and check if NOT busy...
  32.                     sd_address_i <= `TRANS_ERROR_REG;   // Read the SD Transmission Error reg.
  33.                     sd_we_i <= 1'b0;                    // READ (0).
  34. //                  sd_strobe_i <= 1'b1;                // Select the SD Driver. (still set from previous state!)
  35.                     state <= 10'd4;
  36.                 end
  37.  
  38.             4: if (sd_ack_o) begin                      // Wait for SD Driver ack (for previous READ).
  39.                 if (sd_data_o[1:0] == `INIT_NO_ERROR) begin
  40.                     error <= 1'b0;
  41.                     sd_strobe_i <= 1'b0;                // De-select the SD Driver.
  42.                     state <= 5;
  43.                 end
  44.                 else
  45.                     error <= 1'b1;
  46.             end
  47.  
  48.             // *** IDLE State (wait for "trig_block_read")... ***
  49.             5: begin
  50.                 block_read_done <= 1'b0;
  51.                
  52.                 if (trig_block_read) begin
  53.                 gd_bytecount <= gd_sector_count * 2048; // 2048 Bytes per GD sector.
  54.  
  55. //              gd_bytecount <= 7056000;                // TESTING audio streaming !! (20 seconds worth at 44100Hz).
  56.  
  57.                 sd_bytecount <= 32'd0;
  58.                 dc_bytecount <= 32'd0;
  59.  
  60. //              Host_ADDR <= 24'h000000;        // Set SDRAM start Address.
  61.    
  62.                 SRAM_ADDR_REG <= 19'h000000;    // Set SRAM start Address.
  63.                 SRAM_OE_n <= 1'b1;              // De-assert SRAM Output Enable (allow writing)!
  64.  
  65. //              if (CCS)    // This should be HIGH if SD Card is SDHC.
  66. //                  sd_sector_addr <= (gd_start_sector - 45150) * 4;    // SDHC cards use 512-byte SECTOR addressing - Need to multiply the GD sector offset by 4.
  67. //              else
  68.                     sd_sector_addr <= (gd_start_sector - 45150) * 2048; // Low-density SD Cards use BYTE addressing - Need to multiply the GD sector offset by 2048.
  69.  
  70.                 sd_address_i <= `SD_ADDR_31_24_REG;     // Select the SD Sector Address reg (bits 31:24).
  71.                 sd_data_i <= sd_sector_addr[31:24];     // Set the address bits.
  72.                 sd_we_i <= 1'b1;                        // WRITE (1).
  73.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  74.  
  75.                 state <= 10'd6;
  76.                
  77.                 end
  78.             end
  79.  
  80.             6: if (sd_ack_o) begin                      // Wait for SD Driver ack (for previous WRITE).
  81.                 sd_address_i <= `SD_ADDR_23_16_REG;     // Select the SD Sector Address reg (bits 23:16).
  82.                 sd_data_i <= sd_sector_addr[23:16];     // Set the address bits.
  83.                 sd_we_i <= 1'b1;                        // WRITE (1).
  84.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  85.  
  86.                 state <= 10'd7;
  87. //              state <= 10'd12;                // TESTING audio streaming !!
  88.             end
  89.  
  90.             7: if (sd_ack_o) begin                      // Wait for SD Driver ack (for previous WRITE).
  91.                 sd_address_i <= `SD_ADDR_15_8_REG;      // Select the SD Sector Address reg (bits 15:8).
  92.                 sd_data_i <= sd_sector_addr[15:8];      // Set the address bits.
  93.                 sd_we_i <= 1'b1;                        // WRITE (1).
  94.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  95.  
  96.                 state <= 10'd8;
  97.             end
  98.            
  99.             8: if (sd_ack_o) begin                      // Wait for SD Driver ack (for previous WRITE).
  100.                 sd_address_i <= `SD_ADDR_7_0_REG;       // Select the SD Sector Address reg (bits 7:0).
  101.                 sd_data_i <= sd_sector_addr[7:0];       // Set the address bits.
  102.                 sd_we_i <= 1'b81;                       // WRITE (1).
  103.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  104.  
  105.                 state <= 10'd9;
  106.             end
  107.  
  108.             9: if (sd_ack_o) begin                      // Wait for SD Driver ack (for previous WRITE).
  109.                 sd_address_i <= `SD_ADDR_7_0_REG;       // Select the SD Sector Address reg (bits 7:0).
  110.                 sd_data_i <= sd_sector_addr[7:0];       // Set the address bits.
  111.                 sd_we_i <= 1'b1;                        // WRITE (1).
  112.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  113.  
  114.                 state <= 10'd10;
  115.             end
  116.  
  117.             10: if (sd_ack_o) begin                     // Wait for SD Driver ack (for previous WRITE).
  118.                 sd_address_i <= `TRANS_TYPE_REG;        // Select the SD Transmission Type reg.
  119.                 sd_data_i <= `RW_READ_SD_BLOCK;         // "Block Read" command.
  120.                 sd_we_i <= 1'b1;                        // WRITE (1).
  121.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  122.  
  123.                 state <= 10'd11;
  124.             end
  125.  
  126.             11: if (sd_ack_o) begin                     // Wait for SD Driver ack (for previous WRITE).
  127.                 sd_address_i <= `TRANS_CTRL_REG;        // Select the SD Transmission Control reg.
  128.                 sd_data_i <= `TRANS_START;              // Transmit the command.
  129.                 sd_we_i <= 1'b1;                        // WRITE (1).
  130.                 sd_strobe_i <= 1'b1;                    // Select the SD Driver.
  131.  
  132.                 state <= 10'd12;
  133.             end
  134.  
  135.             12: if (sd_ack_o && sd_data_o !== `TRANS_BUSY) begin    // Wait for SD Driver ack, and check if NOT busy...
  136.                     sd_address_i <= `TRANS_ERROR_REG;   // Read the SD Transmission Error reg.
  137.                     sd_we_i <= 1'b0;                    // READ (0).
  138. //                  sd_strobe_i <= 1'b1;                // Select the SD Driver. (still set from previous state!)
  139.                     state <= 10'd13;
  140.                 end
  141.  
  142.             13: if (sd_ack_o) begin                 // Wait for SD Driver ack (for previous READ), and check if NOT busy...
  143.  
  144.                 if (sd_data_o[3:2] == `READ_NO_ERROR) begin
  145.                     error <= 1'b0;
  146.                     sd_strobe_i <= 1'b0;                // De-select the SD Driver.
  147.                     state <= 14;
  148.                 end
  149.                 else
  150.                     error <= 1'b1;
  151.             end
  152.  
  153.             14: begin
  154.                
  155.             end
  156.  
  157. // Grab the MSB Byte from SD Card...
Advertisement
Add Comment
Please, Sign In to add comment