Guest User

gd_emu 15-7-12 OzOnE

a guest
Jul 15th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 46.45 KB | None | 0 0
  1. module gd_emu (
  2.     CLK50M,
  3.     gd_data, gd_addr, gd_rd_n, gd_wr_n, gd_dma_ack_n, gd_dma_rq, gd_int_rq, gd_iordy, gd_cs, gd_rst_n,  // GD-Rom stuff.
  4.     gd_cdlrck, gd_cdda_clk, gd_cdbck, gd_cdda_data, gd_cdda_emph,   // CDDA stuff.
  5.  
  6.     RST_KEY_n, dc_rst_n,
  7.    
  8.     SRAM_ADDR,
  9.     SRAM_DATA,
  10.     SRAM_CS_n,
  11.     SRAM_OE_n,
  12.     SRAM_WE_n,
  13.     SRAM_UB_n,
  14.     SRAM_LB_n,
  15.  
  16.     SDRAM_CLK,//connected to the clk port of SDRAM
  17.     SDRAM_CKE,//connected to the cke port of SDRAM
  18.     SDRAM_CS_n,//connected to the CS_n port of SDRAM
  19.     SDRAM_RAS_n,//connected to the RAS_n port of SDRAM
  20.     SDRAM_CAS_n,//connected to the CAS_n port of SDRAM
  21.     SDRAM_WE_n,//connected to the WE_n port of SDRAM
  22.     SDRAM_DQM,//connected to the LDQM port of SDRAM
  23.     SDRAM_BA,//connected to the BA port of SDRAM
  24.     SDRAM_ADDR,//connected to the ADDR port of SDRAM
  25.     SDRAM_DQ,//connected to the DQ port of SDRAM
  26.  
  27.     SD_CS_n, SD_CLK, SD_DATA_IN, SD_DATA_OUT,
  28.  
  29.     SEG_S, SEG,
  30.     LED0,
  31.  
  32.     AUD_I2C_SCK,
  33.     AUD_I2C_DAT,
  34.     AUD_BCLK,
  35.     AUD_DACLRC,
  36.     AUD_DACDAT,
  37.     AUD_MCLK,
  38.  
  39.     USB_TXE_n, USB_RXF_n, USB_RD_n, USB_WR, USB_DATA,
  40.     gd_state);
  41.  
  42.     input wire CLK50M;      // Master clock input
  43.  
  44.     input RST_KEY_n;
  45.     output dc_rst_n = RST_KEY_n_buf;        // Connect RST_KEY_n button to Dreamcast Reset!
  46.  
  47.     output [3:0] SEG_S;
  48.     output [7:0] SEG;
  49.    
  50.     output wire LED0;
  51.    
  52.     output wire AUD_I2C_SCK;
  53.     inout wire AUD_I2C_DAT;
  54.    
  55.     input wire AUD_BCLK;
  56.     input wire AUD_DACLRC;
  57.     output wire AUD_DACDAT;
  58.  
  59.     output reg AUD_MCLK;
  60.    
  61.     input wire USB_TXE_n;
  62.     input wire USB_RXF_n;
  63.     output wire USB_RD_n;
  64.     output wire USB_WR;
  65.     inout wire [7:0] USB_DATA;
  66.  
  67. // SRAM pins...
  68.     output wire SRAM_CS_n = 1'b0;   // Might as well assert all of these...
  69.     output wire SRAM_UB_n = 1'b0;
  70.     output wire SRAM_LB_n = 1'b0;
  71.  
  72.     output wire [18:0] SRAM_ADDR;
  73.     output wire SRAM_OE_n;
  74.     output wire SRAM_WE_n;
  75.     inout wire [15:0] SRAM_DATA;
  76.  
  77. // SDRAM pins...
  78.     output SDRAM_CLK;
  79.     output SDRAM_CKE;
  80.     output SDRAM_CS_n;
  81.     output SDRAM_RAS_n;
  82.     output SDRAM_CAS_n;
  83.     output SDRAM_WE_n;
  84.     output [1: 0] SDRAM_DQM;
  85.     output [1 :0] SDRAM_BA;
  86.     output [12:0] SDRAM_ADDR;
  87.     inout  [15:0] SDRAM_DQ;
  88.  
  89. // SD interface pins and regs...
  90.     output SD_CS_n;
  91.     output SD_CLK;
  92.     input  SD_DATA_IN;
  93.     output SD_DATA_OUT;
  94.  
  95.     reg trig_block_read;
  96.  
  97. // GD Interface pins and regs...
  98.     reg gd_wr_n_1, gd_wr_n_2;   // Flip-flops for detecting async edges.
  99.     reg gd_rd_n_1, gd_rd_n_2;
  100.  
  101.     wire gd_wr_rising = gd_wr_n_1 & ~gd_wr_n_2;
  102.     wire gd_rd_rising = gd_rd_n_1 & ~gd_rd_n_2;
  103.  
  104.     wire gd_wr_falling = ~gd_wr_n_1 & gd_wr_n_2;
  105.     wire gd_rd_falling = ~gd_rd_n_1 & gd_rd_n_2;
  106.  
  107.     input wire gd_rst_n/* synthesis noprune */;
  108.     input wire [1:0] gd_cs/* synthesis noprune */;
  109.     input wire [2:0] gd_addr/* synthesis noprune */;
  110.     input wire gd_rd_n/* synthesis noprune */;
  111.     input wire gd_wr_n/* synthesis noprune */;
  112.    
  113.     input wire gd_dma_ack_n/* synthesis noprune */; // Need this signal so we can drive the output buffer during DMA transfers.
  114.  
  115.     reg [15:0] gd_data_write/* synthesis noprune */;
  116.  
  117.     reg rom_route;              // ROM route (route reply ROM output directly to GD-ROM data input).
  118.  
  119. // Mux for routing data from either the reply ROM, or from internally generated data.
  120.     wire [15:0] gd_data_out = rom_route ? reply_rom_data : gd_data_write;
  121.  
  122. // Address decoder for register READS...
  123. // The top byte is set to 0xFF for all 8-bit regs (this is what the Dreamcast does).
  124.     wire [15:0] gd_reg_mux =    ({gd_cs, gd_addr} == 5'b11000) ? sector_data :                      // DMA Channel !
  125.                                 ({gd_cs, gd_addr} == 5'b010xx) ? 16'bz :                        // High-Z
  126.                                 ({gd_cs, gd_addr} == 5'b0110x) ? 16'bz :                        // High-Z
  127.                                 ({gd_cs, gd_addr} == 5'b01110) ? {8'hff, gd_status_reg} :       // ALTSTATUS reg (same read as status reg).
  128.                                 ({gd_cs, gd_addr} == 5'b01111) ? 16'bz :                        // Drive Address (not used?)
  129.                                 ({gd_cs, gd_addr} == 5'b10000) ? gd_data_out :                  // Data reg
  130.                                 ({gd_cs, gd_addr} == 5'b10001) ? {8'hff, gd_error_reg} :        // Error reg
  131.                                 ({gd_cs, gd_addr} == 5'b10010) ? {8'hff, gd_intreason_reg} :    // IntReason reg
  132.                                 ({gd_cs, gd_addr} == 5'b10011) ? {8'hff, gd_secnumber_reg} :    // SecNumber reg / LBA0
  133.                                 ({gd_cs, gd_addr} == 5'b10100) ? {8'hff, gd_bytecount_low} :    // BYTCLLO reg / LBA1
  134.                                 ({gd_cs, gd_addr} == 5'b10101) ? {8'hff, gd_bytecount_high} :   // BYTCLHI reg / LBA2
  135.                                 ({gd_cs, gd_addr} == 5'b10110) ? {8'hff, gd_drivesel_reg} :     // Drivesel reg / LBA3
  136.                                 ({gd_cs, gd_addr} == 5'b10111) ? {8'hff, gd_status_reg} :       // Status reg
  137.                                 16'hzz/* synthesis noprune */;                                  // Else, High-Z
  138.  
  139.     // Note: Forcing Output Enable when "gd_dma_ack_n" is low (DC should have setup the DMA path before this happens!).
  140. //  wire gd_oe = ((!gd_cs[1] || !gd_cs[0]) || !gd_dma_ack_n) && !gd_rd_n/* synthesis noprune */;
  141.  
  142.     wire gd_oe = (!gd_cs[1] || !gd_cs[0]) && !gd_rd_n || !gd_dma_ack_n/* synthesis noprune */; // TESTING !! Force OE when !gd_dma_ack_n.
  143.  
  144.     reg gd_int_rq_reg;
  145. //  reg gd_dma_rq_reg;
  146.  
  147.     reg [8:0] cdda_clk_div;     // Divider to generate "cdbck" and "cdlrck" from "cdda_clk_pll".
  148.  
  149. // Note: Anything which is normally an OUTPUT from GD-ROM -> Dreamcast is made an inout (bi-dir), so we
  150. // can change the lines below for either Spy mode (GD-ROM drive is plugged in - high-z for monitoring only),
  151. // or Emu mode (FPGA output regs are routed to the Dreamcast).
  152. //  inout wire gd_cdda_clk = 1'bz/* synthesis noprune */;       // Spy mode !!
  153. //  inout wire gd_cdbck = 1'bz/* synthesis noprune */;          // Spy mode !!
  154. //  inout wire gd_cdlrck = 1'bz/* synthesis noprune */;         // Spy mode !!
  155. //  inout wire gd_cdda_data = 1'bz/* synthesis noprune */;      // Spy mode !!
  156. //  inout wire gd_cdda_emph = 1'bz/* synthesis noprune */;      // Spy mode !!
  157. //  inout wire gd_dma_rq = 1'bz/* synthesis noprune */;         // Spy mode !!
  158. //  inout wire gd_int_rq = 1'bz/* synthesis noprune */;         // Spy mode !!
  159. //  inout wire gd_iordy = 1'bz/* synthesis noprune */;          // Spy mode !!
  160. //  inout wire [15:0] gd_data = 16'bz/* synthesis noprune */;   // Spy mode !!
  161.  
  162. //  AICA needs "cdda_clk" (~33.8688MHz), or DC won't start up!...
  163.     inout wire gd_cdda_clk = cdda_clk_pll;                  // Emu mode (GD-ROM drive MUST be unplugged first!)
  164.     inout wire gd_cdbck = cdda_clk_div[3];                  // Emu mode (GD-ROM drive MUST be unplugged first!)
  165.     inout wire gd_cdlrck = cdda_clk_div[8];             // Emu mode (GD-ROM drive MUST be unplugged first!)
  166.     inout wire gd_cdda_data = 1'b0;                     // Emu mode (GD-ROM drive MUST be unplugged first!)
  167.     inout wire gd_cdda_emph = 1'b0;                     // Emu mode (GD-ROM drive MUST be unplugged first!)
  168.     inout wire gd_dma_rq = cont_dma_rq;                 // Emu mode (GD-ROM drive MUST be unplugged first!)
  169.     inout wire gd_int_rq = gd_int_rq_reg;                   // Emu mode (GD-ROM drive MUST be unplugged first!)
  170.     inout wire gd_iordy = 1'b1;                         // Emu mode (GD-ROM drive MUST be unplugged first!)
  171.     inout wire [15:0] gd_data = gd_oe ? gd_reg_mux : 16'bz; // Emu mode (GD-ROM drive MUST be unplugged first!)
  172.  
  173. //  reg [9:0] gd_pio_state;
  174.     reg [9:0] gd_dma_state;
  175.     reg [9:0] gd_reply_state;
  176.    
  177.     output reg [9:0] gd_state/* synthesis noprune */;
  178.  
  179.     reg [15:0] ata_cmd_count/* synthesis noprune */;// VERY handy for SignalTap debugging!
  180.     reg [15:0] packet_cmd_count/* synthesis noprune */;// VERY handy for SignalTap debugging!
  181.  
  182. // GD-Rom ATA regs...
  183.     reg [7:0] gd_ata_cmd;
  184.    
  185.     reg [7:0] gd_bytecount_low;
  186.     reg [7:0] gd_bytecount_high;
  187.    
  188.     reg [7:0] gd_drivesel_reg;
  189.     reg [7:0] gd_features_reg/* synthesis noprune */;   // LSB indicates next data transfer is via DMA !
  190.     reg [7:0] gd_status_reg;
  191.     reg [7:0] gd_error_reg;
  192.     reg [7:0] gd_seccount_reg;
  193.     reg [7:0] gd_intreason_reg;
  194.     reg [7:0] gd_secnumber_reg;
  195.  
  196. // GD-Rom Packet regs...
  197.     reg [15:0] packet_1/* synthesis noprune */; // TODO - Maybe start WORD count from zero next time. :)
  198.     reg [15:0] packet_2/* synthesis noprune */;
  199.     reg [15:0] packet_3/* synthesis noprune */;
  200.     reg [15:0] packet_4/* synthesis noprune */;
  201.     reg [15:0] packet_5/* synthesis noprune */;
  202.     reg [15:0] packet_6/* synthesis noprune */;
  203.  
  204.     wire [7:0] packet_0b = packet_1[7:0]/* synthesis noprune */;
  205.     wire [7:0] packet_1b = packet_1[15:8]/* synthesis noprune */;
  206.     wire [7:0] packet_2b = packet_2[7:0]/* synthesis noprune */;
  207.     wire [7:0] packet_3b = packet_2[15:8]/* synthesis noprune */;
  208.     wire [7:0] packet_4b = packet_3[7:0]/* synthesis noprune */;
  209.     wire [7:0] packet_5b = packet_3[15:8]/* synthesis noprune */;
  210.     wire [7:0] packet_6b = packet_4[7:0]/* synthesis noprune */;
  211.     wire [7:0] packet_7b = packet_4[15:8]/* synthesis noprune */;
  212.     wire [7:0] packet_8b = packet_5[7:0]/* synthesis noprune */;
  213.     wire [7:0] packet_9b = packet_5[15:8]/* synthesis noprune */;
  214.     wire [7:0] packet_10b = packet_6[7:0]/* synthesis noprune */;
  215.     wire [7:0] packet_11b = packet_6[15:8]/* synthesis noprune */;
  216.    
  217.     reg [11:0] gd_sector_type/* synthesis noprune */;   // No point wasting bits! (OzOnE).
  218.     reg [31:0] gd_start_sector;
  219.     reg [31:0] gd_sector_count/* synthesis noprune */;  // Warning - not to be confused with the ATA "gd_seccount_reg"!
  220.  
  221.     // Note: Using "gd_secnumber_reg" to store DiscFormat and GD_status codes. OzOnE.
  222.     reg [3:0] gd_discformat;    // Only using nibble!
  223.     reg [3:0] cdda_repeats;     // Only using nibble!
  224.    
  225.     reg [23:0] cdda_curraddr;   // Using full three bytes together (need to part select when needed). OzOnE.
  226.  
  227.     reg [7:0] stat [0:9];   // Array of bytes for sending REQ_STAT reply.
  228.     reg [7:0] resp [0:9];   // Array of bytes for sending REQ_ERROR reply.
  229.    
  230.     reg [7:0] mode [0:31];  // Array of bytes for storing REQ_MODE / SET_MODE info.
  231.    
  232.     reg [9:0] reply_rom_addr;
  233.  
  234.     reg toc_density/* synthesis noprune */;
  235.    
  236.     reg [3:0] scd_format;
  237.  
  238.     reg [15:0] alloc_length/* synthesis noprune */;
  239.     reg [15:0] byte_count/* synthesis noprune */;
  240.  
  241. // GD-Rom ATA Commands
  242. parameter ATA_NOP =         8'h00;
  243. parameter ATA_SOFT_RESET =  8'h08;
  244. parameter ATA_EXEC_DIAG =   8'h90;
  245. parameter ATA_SPI_PACKET =  8'hA0;
  246. parameter ATA_IDENTIFY_DEV = 8'hA1;
  247. parameter ATA_SET_FEATURES = 8'hEF;
  248.  
  249. // GD-Rom SPI (Sega Packet Interface) Commands
  250. parameter SPI_TEST_UNIT =   8'h00;
  251. parameter SPI_REQ_STAT =    8'h10;
  252. parameter SPI_REQ_MODE =    8'h11;
  253. parameter SPI_SET_MODE =    8'h12;
  254. parameter SPI_REQ_ERROR =   8'h13;
  255. parameter SPI_GET_TOC =     8'h14;
  256. parameter SPI_REQ_SES =     8'h15;
  257. parameter SPI_CD_OPEN =     8'h16;
  258. parameter SPI_CD_PLAY =     8'h20;
  259. parameter SPI_CD_SEEK =     8'h21;
  260. parameter SPI_CD_SCAN =     8'h22;
  261. parameter SPI_CD_READ =     8'h30;
  262. parameter SPI_CD_READ2 =    8'h31;
  263. parameter SPI_GET_SCD =     8'h40;
  264.  
  265. // Using nibbles for status! OzOnE.
  266. parameter GD_BUSY =         4'h0;   // State transition
  267. parameter GD_PAUSE =        4'h1;   // Pause
  268. parameter GD_STANDBY =      4'h2;   // Standby (drive stop)
  269. parameter GD_PLAY =         4'h3;   // CD playback
  270. parameter GD_SEEK =         4'h4;   // Seeking
  271. parameter GD_SCAN =         4'h5;   // Scanning
  272. parameter GD_OPEN =         4'h6;   // Tray is open
  273. parameter GD_NODISC =       4'h7;   // No disc
  274. parameter GD_RETRY =        4'h8;   // Read retry in progress (option)
  275. parameter GD_ERROR =        4'h9;   // Reading of disc TOC failed (state does not allow access)
  276.  
  277.  
  278. always @(posedge cdda_clk_pll) cdda_clk_div <= cdda_clk_div + 9'd1;
  279.  
  280. reg RST_KEY_n_buf;
  281. always @(posedge CLK50M) RST_KEY_n_buf <= RST_KEY_n;
  282.  
  283. reg gd_rst_n_2;
  284. reg gd_rst_n_1;
  285. wire gd_rst_n_buf = (gd_rst_n_2 || gd_rst_n_1 || gd_rst_n); // "gd_rst_n_buf" LOW when "gd_rst_n" AND both regs are low (smooth out glitches).
  286.                                                             // Dependent on whether "gd_rst_n" goes low for three or more clocks though!!
  287. always @(posedge CLK50M) begin
  288.     gd_rst_n_1 <= gd_rst_n;     // Update edge regs on every clock.
  289.     gd_rst_n_2 <= gd_rst_n_1;
  290. end
  291.  
  292. initial begin
  293. //  gd_oe <= 1'b0;
  294.     rom_route <= 1'b0;
  295.     gd_state <= 10'd0;
  296.     gd_ata_cmd <= 8'h00;
  297. //  gd_dma_rq_reg <= 1'b0;      // De-assert GD DMA Request !
  298.     reply_rom_addr <= 10'h000;
  299.  
  300.     gd_dma_state <= 10'd0;
  301.  
  302.     gd_status_reg <= 8'h00;     // SPI Doc says to set these on reset (gd_status_reg must be 0x00 == RESET!).
  303. //  gd_error_reg <= 8'h01;
  304.     gd_error_reg <= 8'h00;
  305.     gd_seccount_reg <= 8'h01;
  306.     gd_secnumber_reg <= 8'h00;  // Set to zero!!
  307.     gd_bytecount_low <= 8'h14;
  308.     gd_bytecount_high <= 8'hEB;
  309.     gd_drivesel_reg <= 8'h00;
  310.  
  311.     gd_intreason_reg <= 8'h00;
  312.    
  313.     gd_int_rq_reg <= 1'b0;
  314.  
  315.     gd_sector_type <= 12'd0;
  316.     gd_start_sector <= 32'd0;
  317.     gd_sector_count <= 32'd0;
  318.  
  319. //  gd_secnumber_reg <= 8'h86;  // <- Top nibble is DiscFormat, bottom nibble is unit status...
  320.                                 // Set to GD-ROM and leave tray open for now (TESTING).
  321.  
  322. //  gd_secnumber_reg <= 8'h06;  // <- Top nibble is DiscFormat, bottom nibble is unit status...
  323.                                 // Set to CD-DA (default / no disk) and leave tray open for now (TESTING).
  324.  
  325.     ata_cmd_count <= 16'd0;
  326.     packet_cmd_count <= 16'd0;
  327.  
  328.     packet_1 <= 16'h0000;
  329.     packet_2 <= 16'h0000;
  330.     packet_3 <= 16'h0000;
  331.     packet_4 <= 16'h0000;
  332.     packet_5 <= 16'h0000;
  333.     packet_6 <= 16'h0000;
  334.    
  335.     trig_block_read <= 1'b0;    // De-assert block read trigger (for SD Card reading).
  336.     end
  337.    
  338. always @(posedge CLK50M or negedge gd_rst_n)
  339.     if (!gd_rst_n) begin
  340. //      gd_oe <= 1'b0;
  341.         rom_route <= 1'b0;
  342.  
  343.         cdda_repeats <= 4'd0;
  344.        
  345.         gd_dma_state <= 10'd0;
  346.  
  347.         gd_ata_cmd <= 8'h00;
  348. //      gd_dma_rq_reg <= 1'b0;      // De-assert GD DMA Request !
  349.         reply_rom_addr <= 10'h000;
  350.        
  351.         gd_status_reg <= 8'h00;     // SPI Doc says to set these on reset (gd_status_reg must be 0x00 == RESET!).
  352. //      gd_error_reg <= 8'h01;
  353.         gd_error_reg <= 8'h00;
  354.         gd_seccount_reg <= 8'h01;
  355.         gd_secnumber_reg <= 8'h00;  // Set to zero!!
  356.         gd_bytecount_low <= 8'h14;
  357.         gd_bytecount_high <= 8'hEB;
  358.         gd_drivesel_reg <= 8'h00;
  359.         gd_intreason_reg <= 8'h00;
  360.        
  361.         gd_int_rq_reg <= 1'b0;
  362.  
  363.         gd_sector_type <= 12'd0;
  364.         gd_start_sector <= 32'd0;
  365.         gd_sector_count <= 32'd0;
  366.  
  367. //      gd_secnumber_reg <= 8'h86;  // <- Top nibble is DiscFormat, bottom nibble is unit status...
  368.                                     // Set to GD-ROM and leave tray open for now (TESTING).
  369.  
  370. //      gd_secnumber_reg <= 8'h07;  // <- Top nibble is DiscFormat, bottom nibble is unit status...
  371.                                     // Set to CD-DA (default DiscFormat for no disk?) and GD_NODISC state.
  372.  
  373. // "DiscFormat" top nibble...
  374. // 0 == CD-DA.
  375. // 1 == CD-ROM.
  376. // 2 == CD-ROM XA, CD Extra
  377. // 3 == CD-I
  378. // 8 == GD-ROM. <- IMPORTANT - MSB is set, NOT bit 6!!
  379.  
  380. // "Unit Status" bottom nibble...
  381. // GD_BUSY  0x00    // State transition
  382. // GD_PAUSE 0x01    // Pause
  383. // GD_STANDBY 0x02  // Standby (drive stop)
  384. // GD_PLAY  0x03    // CD playback
  385. // GD_SEEK  0x04    // Seeking
  386. // GD_SCAN  0x05    // Scanning
  387. // GD_OPEN  0x06    // Tray is open
  388. // GD_NODISC 0x07   // No disc
  389. // GD_RETRY 0x08    // Read retry in progress (option)
  390. // GD_ERROR 0x09    // Reading of disc TOC failed (state does not allow access)
  391. /*
  392. // nullDC original mode settings...
  393.     {mode[1],mode[0]} <= 16'h0000;
  394.     {mode[3],mode[2]} <= 16'h0000;      // Byte 2 is the "CD-ROM Speed" value. 0=MAX, 1=STANDARD, 2=x2, 3=x4, 4=x6, 5=x8, 6=x10, 7=x12.
  395.     {mode[5],mode[4]} <= 16'hB400;      // Word [4:5] is the DEFAULT Standby timeout value. 0x00B4 == 180 seconds = only 3 minutes!
  396.     {mode[7],mode[6]} <= 16'h0019;
  397.     {mode[9],mode[8]} <= 16'h0800;
  398.     {mode[11],mode[10]} <= 16'h4553;
  399.     {mode[13],mode[12]} <= 16'h2020;
  400.     {mode[15],mode[14]} <= 16'h2020;
  401.     {mode[17],mode[16]} <= 16'h2020;
  402.     {mode[19],mode[18]} <= 16'h6552;
  403.     {mode[21],mode[20]} <= 16'h2076;
  404.     {mode[23],mode[22]} <= 16'h2E36;
  405. //  {mode[25],mode[24]} <= 16'h3334;    // TESTING - Change it to this...
  406.     {mode[25],mode[24]} <= 16'h3234;    // <- To reflect the mode from my DC. OzOnE.
  407.     {mode[27],mode[26]} <= 16'h3939;
  408.     {mode[29],mode[28]} <= 16'h3430;
  409.     {mode[31],mode[30]} <= 16'h3830;
  410. */
  411.  
  412. // My modified mode settings (for Crazy Taxi)...
  413.     {mode[1],mode[0]} <= 16'h0000;
  414.     {mode[3],mode[2]} <= 16'h0000;
  415.     {mode[5],mode[4]} <= 16'h100E;      // <- Only this appears different from the nullDC Mode words.
  416.                                         // Word [4:5] is the Standby timeout value. 0x0E10 == 3600 seconds = 60 minutes!
  417.     {mode[7],mode[6]} <= 16'h0019;
  418.     {mode[9],mode[8]} <= 16'h0800;
  419.     {mode[11],mode[10]} <= 16'h4553;
  420.     {mode[13],mode[12]} <= 16'h2020;
  421.     {mode[15],mode[14]} <= 16'h2020;
  422.     {mode[17],mode[16]} <= 16'h2020;
  423.  
  424.     {mode[19],mode[18]} <= 16'h6552;
  425.     {mode[21],mode[20]} <= 16'h2076;
  426.     {mode[23],mode[22]} <= 16'h2E36;
  427.     {mode[25],mode[24]} <= 16'h3334;    // TESTING - Change it to this...
  428. //  {mode[25],mode[24]} <= 16'h3234;    // <- To reflect the mode from my DC. OzOnE.
  429.     {mode[27],mode[26]} <= 16'h3939;
  430.     {mode[29],mode[28]} <= 16'h3430;
  431.     {mode[31],mode[30]} <= 16'h3830;
  432.  
  433.  
  434.     ata_cmd_count <= 16'd0;
  435.     packet_cmd_count <= 16'd0;
  436.  
  437.     packet_1 <= 16'h0000;
  438.     packet_2 <= 16'h0000;
  439.     packet_3 <= 16'h0000;
  440.     packet_4 <= 16'h0000;
  441.     packet_5 <= 16'h0000;
  442.     packet_6 <= 16'h0000;
  443.    
  444.     trig_block_read <= 1'b0;    // De-assert block read trigger (for SD Card reading).
  445.     end
  446.     else begin
  447.  
  448.     gd_wr_n_1 <= gd_wr_n;       // Update edge regs on every clock.
  449.     gd_wr_n_2 <= gd_wr_n_1;
  450.  
  451.     gd_rd_n_1 <= gd_rd_n;
  452.     gd_rd_n_2 <= gd_rd_n_1;
  453.  
  454.     // Handle reg READS... (now using address decoder above for reg reads, but still need to clear interrupt)...
  455.     // ALTSTATUS also reads "gd_status_reg" (assigned above), but Interrupt is NOT cleared.
  456.         if ({gd_cs, gd_addr} == 5'b10111 && gd_rd_falling) begin    // READ from STATUS reg.
  457.             gd_int_rq_reg <= 1'b0;      // Clear interrupt on status read.
  458.         end
  459.  
  460. //      if ({gd_cs, gd_addr} == 5'b10011 && gd_rd_falling) begin    // READ from SECNUM reg.
  461. //          if (gd_secnumber_reg[3:0] == GD_PLAY) gd_secnumber_reg[3:0] <= GD_PAUSE;    // If PREVIOUS CD_READ set to PLAY, set back to PAUSE!
  462. //      end
  463.  
  464.     // Handle reg WRITES...
  465.         if ({gd_cs, gd_addr} == 5'b10001 && gd_wr_rising) begin // WRITE to "gd_features_reg".
  466.             gd_features_reg <= gd_data[7:0];
  467.         end
  468.            
  469.         if ({gd_cs, gd_addr} == 5'b10010 && gd_wr_rising) begin // WRITE to "gd_seccount_reg".
  470.             gd_seccount_reg <= gd_data[7:0];
  471.         end
  472.  
  473.         // Write to secnumber not possible apparently? OzOnE (nullDC source said).
  474. /*      if (gd_cs == 2'd2 && gd_addr == 3'd3 && gd_wr_rising) begin // WRITE to "gd_secnumber_reg".
  475.             gd_secnumber_reg <= gd_data[7:0];
  476.         end */
  477.  
  478.         if ({gd_cs, gd_addr} == 5'b10100 && gd_wr_rising) begin // WRITE to "gd_bytecount_low" reg.
  479.             gd_bytecount_low <= gd_data[7:0];
  480.         end
  481.        
  482.         if ({gd_cs, gd_addr} == 5'b10101 && gd_wr_rising) begin // WRITE to "gd_bytecount_high" reg.
  483.             gd_bytecount_high <= gd_data[7:0];
  484.         end
  485.  
  486.         if ({gd_cs, gd_addr} == 5'b10110 && gd_wr_rising) begin // WRITE to "gd_drivesel" reg. Is this even needed??
  487.             gd_drivesel_reg <= gd_data[7:0];
  488.         end
  489.  
  490.         if ({gd_cs, gd_addr} == 5'b01110 && gd_wr_rising) begin // WRITE to DRIVECTRL reg.
  491.             if (gd_data[7:0] == ATA_SOFT_RESET) gd_state <= 1;  // If 0x08 is written to DRIVECTRL reg, bring out of reset state.
  492.         end
  493.  
  494.         if ({gd_cs, gd_addr} == 5'b10111 && gd_wr_rising) begin // WRITE to COMMAND reg. (need this here, as soft reset can be written at any time!)
  495.             if (gd_data[7:0] == ATA_SOFT_RESET) gd_state <= 1;  // If 0x08 is written to DRIVECTRL reg, bring out of reset state.
  496.         end
  497.  
  498.     case (gd_state)
  499.         0: begin                        // RESET state (wait for Soft Reset cmd in COMMAND or DRIVECTRL reg, above).
  500.             gd_status_reg <= 8'h00;
  501.         end
  502.  
  503.         1:  begin                       // "gds_waitcmd" (wait for ATA command).
  504.             trig_block_read <= 1'b0;    // Clear SD-to-DC DMA trigger !
  505.        
  506.             gd_status_reg[6] <= 1'b1;   // Set DRDY bit - Can accept an ATA cmd :)
  507.             gd_status_reg[7] <= 1'b0;   // Clear BSY bit - Not currently accessing the command block.
  508.             gd_status_reg[3] <= 1'b0;   // Clear DRQ.
  509.  
  510.             gd_status_reg[4] <= 1'b1;   // Set DSC bit (Seek complete) TESTING !!
  511.  
  512.             if (gd_cs == 2'd2 && gd_addr == 3'd7 && gd_wr_rising) begin // WRITE to COMMAND reg.
  513.                 gd_ata_cmd <= gd_data[7:0];
  514.  
  515.                 // "gds_procata"
  516. //              gd_status_reg[6] <= 1'b0;               // Clear DRDY bit - Can't accept another ATA cmd (while processing).
  517.                 gd_status_reg[7] <= 1'b1;               // Set BSY bit - Accessing the command block.
  518.                 ata_cmd_count <= ata_cmd_count + 16'd1;
  519.                 gd_state <= 10'd2;                      // Parse ATA command!
  520.             end
  521.         end
  522.  
  523.         2: begin
  524.             //Any ata cmd clears these bits , unless aborted/error :p
  525.             gd_error_reg[2] <= 1'b0;        // Clear ABRT bit.
  526.             gd_status_reg[0] <= 1'b0;       // Clear CHECK bit.
  527.  
  528.             case (gd_ata_cmd)
  529.                 ATA_NOP: begin
  530.                     gd_error_reg[2] <= 1'b1;    // SET the ABORT bit.
  531.                     //Error.Sense=0x00; //fixme ?
  532.                     gd_status_reg[7] <= 1'b0;   // Clear the BUSY bit.
  533.                     gd_status_reg[0] <= 1'b1;   // SET the CHECK bit.
  534.  
  535.                     gd_int_rq_reg <= 1'b1;      // Raise an int!
  536.                     gd_state <= 10'd1;
  537.                 end
  538.  
  539.                 ATA_SOFT_RESET: begin           // Do "gd_reset" stuff, just does "gd_setdisc" to grab disc type,
  540.                                                 // it then sets the correct status and updates the secnumber reg with the status.
  541.                     gd_secnumber_reg <= 8'h80;  // Set DiscFormat to GD-ROM, set unit Status to 0 (GD_BUSY!).
  542.                     gd_state <= 10'd1;          // Then, just goes to "gds_waitcmd" (state 1).
  543.                 end
  544.  
  545.                 ATA_EXEC_DIAG: gd_state <= 10'd1;       // (Unimplemented in nullDC). OzOnE
  546.  
  547.                 ATA_SPI_PACKET: begin                   // PACKET command! (Sega type, duh!)
  548.                     // Do "gds_waitpacket" stuff...
  549.                     gd_intreason_reg[0] <= 1'b1;    // Set CoD
  550.                     gd_status_reg[7] <= 1'b0;       // Clear BSY bit.
  551.                     gd_intreason_reg[1] <= 1'b0;    // Clear IO int bit.
  552.                     gd_status_reg[3] <= 1'b1;       // Set DRQ (ready for command packet transfer).
  553.  
  554. //                  gd_int_rq_reg <= 1'b1;          // ATA can optionally raise the interrupt ... (nullDC doesn't raise an int).
  555.  
  556.                     if (gd_addr == 3'd0 && gd_wr_rising) begin  // If in DATA reg on RISING edge of "gd_wr_n"...
  557.                         packet_1 <= gd_data;            // Grab packet WORD 1.
  558.                         packet_cmd_count <= packet_cmd_count + 16'd1;
  559.                         gd_state <= 10'd3;
  560.                     end
  561.                 end
  562.  
  563.                 ATA_IDENTIFY_DEV: begin
  564.                     reply_rom_addr <= packet_2b / 2;            // Start reply ROM address at "reply_a1" offset + requested start offset.
  565.                                                                 // (WORD addressed! - divide by two).
  566.                     byte_count <= 16'd0;                        // Zero the byte count.
  567.                     alloc_length <= {8'h00, packet_4b};         // (Bytes).
  568.                     gd_bytecount_high <= 8'h00;
  569.                     gd_bytecount_low <= packet_4b;
  570.                    
  571.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  572.                     gd_state <= 10'd10;                         // Generate 0xa1 reply from ROM (will go back to idle after transfer).
  573.                 end
  574.  
  575.                 ATA_SET_FEATURES: begin         // Not to be confused with a write to "gd_features_reg"!
  576.                     gd_error_reg[2] <= 1'b0;    // Clear ABRT error bit - Command was not aborted ;) [hopefully ...]
  577.  
  578.                     //status : Clear DRDY , DSC , DF , CHECK    <- Not sure if DRDY is supposed to be set here? OzOnE.
  579.                     //DRDY is set on state change
  580. //                  gd_status_reg[4] <= 1'b0;   // Clear DSC status bit.
  581.                     gd_status_reg[5] <= 1'b0;   // Clear DF status bit.
  582.                     gd_status_reg[0] <= 1'b0;   // Clear CHECK status bit.
  583.                     gd_int_rq_reg <= 1'b1;      //??? Raise int (I think? This is what nullDC source does, OzOnE).
  584.                     gd_state <= 10'd1;          // Back to "gds_waitcmd" (state 1).
  585.                 end
  586.  
  587.             default: gd_state <= 10'd1;     // Unhandled ATA command, back to idle.
  588.             endcase
  589.         end
  590.            
  591.         3: if (gd_addr == 3'd0 && gd_wr_rising) begin
  592.                 packet_2 <= gd_data;                    // Grab packet WORD 2.
  593.                 gd_state <= 10'd4;
  594.         end
  595.  
  596.         4: if (gd_addr == 3'd0 && gd_wr_rising) begin
  597.                 packet_3 <= gd_data;                    // Grab packet WORD 3.
  598.                 gd_state <= 10'd5;
  599.         end
  600.  
  601.         5: if (gd_addr == 3'd0 && gd_wr_rising) begin
  602.                 packet_4 <= gd_data;                    // Grab packet WORD 4.
  603.                 gd_state <= 10'd6;
  604.         end
  605.  
  606.         6: if (gd_addr == 3'd0 && gd_wr_rising) begin
  607.                 packet_5 <= gd_data[7:0];               // Grab packet WORD 5.
  608.                 gd_state <= 10'd7;
  609.         end
  610.  
  611.         7: if (gd_addr == 3'd0 && gd_wr_rising) begin
  612.                 packet_6 <= gd_data[7:0];               // Grab packet WORD 6.
  613.                 gd_state <= 10'd8;
  614.         end
  615.        
  616. // ***** PARSE the SPI (Sega Packet Interface) command packet *****
  617. // *****
  618.         8: begin
  619.                                                 // (Similar stage to "gd_process_spi_cmd()" in nullDC source.)
  620.             gd_status_reg[0] <= 1'b0;           // Clear CHECK bit.
  621.  
  622.             // Do "gds_procpacket" bit settings first!...
  623.             // 6. After last packet word is received - DRQ is cleared, BUSY bit is set.
  624.             gd_status_reg[3] <= 1'b0;           // Clear DRQ (no data req <= shouldn't this be RDY bit, OzOnE?
  625.             gd_status_reg[7] <= 1'b1;           // Set BSY bit - Accessing command block to process command (no ATA commands while processing).
  626.  
  627.             case (packet_0b)                    // <- (byte addressing starts at zero!)
  628.  
  629.                 SPI_TEST_UNIT: begin
  630.                     gd_secnumber_reg[7:4] <= 4'h8;      // Set to GD-ROM DiscFormat type. (OzOnE).
  631.  
  632.                     gd_secnumber_reg[3:0] <= GD_BUSY;   // Drive is ready! (nullDC).
  633.  
  634.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  635.                                             // (No data to send for "TEST_UNIT").
  636.                 end
  637.  
  638.                 SPI_REQ_STAT: begin                 // *** TODO - "gd_status" stuff! ***
  639.                     stat[0] <= {4'h0, gd_secnumber_reg[3:0]};           // Status is low nibble (force top bits to zero).
  640.                     stat[1] <= {gd_secnumber_reg[7:4], cdda_repeats};   // Top nibble is "DiscFormat" - forcing to GD-ROM after 0x71 check. (libGDR normally grabs it. OzOnE)
  641.                     stat[2] <= 8'h04;               // ? This is what nullDC does?
  642.                     stat[3] <= 8'd02;               // TNO
  643.  
  644.                     stat[4] <= 8'd00;               // X
  645.  
  646. //                  stat[5] <= cdda_curraddr[23:16];    // Current FAD (MSB?)
  647. //                  stat[6] <= cdda_curraddr[15:8]; // Current FAD
  648. //                  stat[7] <= cdda_curraddr[7:0];  // Current FAD (LSB?)
  649.  
  650. // NOTE: The CF Card version pauses at the normal CD FAD!! (maybe it pauses at the GD FAD only for GD-Audio ???)...
  651.                     stat[5] <= 8'h00;   // Current FAD (MSB?) Default FAD for a CD - drive pauses at 2 second, 0 frames (75 FPS, so 150 = 0x96).
  652.                     stat[6] <= 8'h00;   // Current FAD
  653.                     stat[7] <= 8'h96;   // Current FAD (LSB?)
  654.  
  655. //                  stat[5] <= 8'h00;   // Current FAD (MSB?) Default FAD for GD! - drive pauses at 10 mins, 2 secs, 0 frames (75 FPS, so 45150 = 0xB05E).
  656. //                  stat[6] <= 8'hB0;   // Current FAD
  657. //                  stat[7] <= 8'h5E;   // Current FAD (LSB?)
  658.  
  659.                     stat[8] <= 8'd00;               // Max Read Error Retry Times.
  660.                     stat[9] <= 8'd00;               // All bits zeros.
  661.  
  662.                     // DiscFormat...
  663.                     // 0 == CD-DA.
  664.                     // 1 == CD-ROM.
  665.                     // 2 == CD-ROM XA, CD Extra
  666.                     // 3 == CD-I
  667.                     // 8 == GD-ROM.
  668.  
  669.                     byte_count <= 16'd0;                        // Zero the byte count.
  670.                     alloc_length <= {8'h00, packet_4b};         // (Bytes).
  671.                     gd_bytecount_high <= 8'h00;
  672.                     gd_bytecount_low <= packet_4b;
  673.                    
  674.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  675.                     gd_state <= 10'd13;                         // Generate REQ_STAT reply.
  676.                 end
  677.  
  678.                 SPI_REQ_MODE: begin
  679.                     reply_rom_addr <= (10'h50 + packet_2b) / 2; // Start reply ROM address at "reply_11" offset + requested start offset.
  680.                                                                 // (WORD addressed! - divide by two).
  681.  
  682.                     byte_count <= 16'd0;                        // Zero the byte count.
  683.                     alloc_length <= {8'h00, packet_4b};         // (Bytes).
  684.                     gd_bytecount_high <= 8'h00;
  685.                     gd_bytecount_low <= packet_4b;
  686.                    
  687.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  688.                     gd_state <= 10'd15;                         // Generate 0x11 reply from ROM (will go back to idle after transfer).
  689.                 end
  690.  
  691.                 SPI_SET_MODE: begin
  692.                     byte_count <= 16'd0;                        // Zero the byte count.
  693.                     alloc_length <= {8'h00, packet_4b};         // (Bytes).
  694.  
  695.                     // When preparations are complete, the following steps are carried out at the device.
  696.                     //(1)   Number of bytes to be read is set in "Byte Count" register.
  697.                     gd_bytecount_high <= 8'h00;
  698.                     gd_bytecount_low <= packet_4b;
  699.                     //(2)   IO bit is set and CoD bit is cleared.
  700.                     gd_intreason_reg[1] <= 1'b0;    // Clear IO bit. <- MUST be cleared for transfer from GD -> DC !!
  701.                     gd_intreason_reg[0] <= 1'b0;    // Clear CoD bit.
  702.                     //(3)   DRQ bit is set, BSY bit is cleared.
  703.                     gd_status_reg[3] <= 1'b1;   // Set DRQ bit.
  704.                     gd_status_reg[7] <= 1'b0;   // Clear BUSY bit.
  705.                     //(4)   INTRQ is set, and a host interrupt is issued.
  706.                     gd_int_rq_reg <= 1'b1;
  707.  
  708.                     gd_status_reg[6] <= 1'b1;   // Set DRDY bit (spy log!)
  709.                     gd_state <= 10'd16;                         // Wait for the data, then write it to the mode info regs...
  710.                     end
  711.  
  712.                 SPI_REQ_ERROR: begin
  713.                     resp[0] <= 8'hF0;
  714.                     resp[1] <= 8'h00;
  715.                     resp[2] <= gd_secnumber_reg[3:0] == GD_BUSY ? 2:0;  //sense (if "secnumber.status" == GD_BUSY, then resp[2] <= 2, else 0). phew!
  716.                     resp[3] <= 8'h00;
  717.                     resp[4] <= 8'h00;   //Command Specific Information
  718.                     resp[5] <= 8'h00;   //Command Specific Information
  719.                     resp[6] <= 8'h00;   //Command Specific Information
  720.                     resp[7] <= 8'h00;   //Command Specific Information
  721.                     resp[8] <= 8'h00;   //Additional Sense Code
  722.                     resp[9] <= 8'h00;   //Additional Sense Code Qualifier
  723.  
  724.                     byte_count <= 16'd0;                        // Zero the byte count.
  725.                     alloc_length <= {8'h00, packet_4b};         // (Bytes).
  726.                     gd_bytecount_high <= 8'h00;
  727.                     gd_bytecount_low <= packet_4b;
  728.  
  729.                     gd_status_reg[0] <= 1'b0;       // TESTING !! Assuming it clears the CHECK bit after checking the error? OzOnE.
  730.  
  731.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  732.                     gd_state <= 10'd12;         // Generate REQ_ERROR reply.
  733.                 end
  734.  
  735.                 SPI_GET_TOC: begin                          // *** TODO - Handle both density TOC types! ***
  736.                     toc_density <= packet_1b[0];            // Request TOC from Single-density area (0) or Double-density area (1), LSB of packet_1b!
  737.                    
  738.                     byte_count <= 16'd0;                        // Zero the byte count.
  739.                     alloc_length <= {packet_3b, packet_4b};     // Allocation length in bytes (from WORD), usually 0x198 (408 bytes) for the TOC.
  740.                     gd_bytecount_high <= packet_3b;
  741.                     gd_bytecount_low <= packet_4b;
  742.  
  743.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  744.                     gd_state <= 10'd9;                      // Generate TOC reply (will go back to idle after transfer).
  745.                 end
  746.  
  747.                 8'h70: begin
  748.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  749.                 end
  750.  
  751.                 8'h71: begin        // Security check?
  752.                     reply_rom_addr <= 10'h70 / 2;   // Set reply ROM start address directly (WORD addressed! - divide by two).
  753.  
  754.                     byte_count <= 16'd0;            // Zero the byte count.
  755.                     alloc_length <= 16'h3f4;        // Set reply 0x71 alloc length directly (1012 bytes).
  756.                     gd_bytecount_high <= 8'h03;
  757.                     gd_bytecount_low <= 8'hf4;
  758.  
  759. //                  if (gd_secnumber_reg[7:4] == 4'h8)  // If "DiscFormat" == GD-ROM type...
  760.                         gd_secnumber_reg[7:4] <= 4'h8;  // TESTING !! - Force to GD-ROM disk type at this point. OzOnE.
  761.                         gd_secnumber_reg[3:0] <= GD_PAUSE;
  762. //                  else
  763. //                      gd_secnumber_reg[3:0] <= GD_STANDBY;
  764.  
  765.                     gds_pio_send_data();            // Int, to start transfer. "When preparations are complete...".
  766.  
  767.                     gd_state <= 10'd10;             // Generate 0x71 reply from ROM (will go back to idle after transfer).
  768.                 end
  769.  
  770.                 SPI_REQ_SES: begin
  771.                     byte_count <= 16'd0;                        // Zero the byte count.
  772.                     alloc_length <= {8'h00, packet_4b};         // (Bytes).
  773.                     gd_bytecount_high <= 8'h00;
  774.                     gd_bytecount_low <= packet_4b;
  775.  
  776.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  777.                     gd_state <= 10'd11;             // Generate "Session" data reply (will go back to idle after transfer).
  778.                 end
  779.  
  780.                 SPI_CD_OPEN: begin                  // (Unimplemented in nullDC, but still sends status back). OzOnE
  781.                     gds_procpacketdone();           // Send status back (goes back to idle afterwards).
  782.                 end
  783.                    
  784.                 SPI_CD_PLAY: begin
  785.                     gds_procpacketdone();           // CD-DA stuff unhandled atm !! OzOnE.
  786.                 end
  787.  
  788.                 SPI_CD_SEEK: begin
  789.                     gds_procpacketdone();           // CD-DA stuff unhandled atm !! OzOnE.
  790.                 end
  791.  
  792.                 SPI_CD_SCAN: begin                  // (Unimplemented in nullDC, but still sends status back). OzOnE
  793.                     gds_procpacketdone();           // CD-DA stuff unhandled atm !! OzOnE.
  794.                 end
  795.  
  796.                 SPI_CD_READ: begin
  797.                     if (packet_1b[7]==1 && packet_1b[6]==1 && packet_1b[5]==1 && packet_1b[3:1]==3 && packet_1b[4]==0) gd_sector_type <= 12'd2340;
  798.                     else gd_sector_type <= 12'd2048;
  799.  
  800.                     if (packet_1b[0]) begin // If "parameter type" == 1 (MSF)...
  801.                         gd_start_sector <= (packet_2b*60*75<<16) | (packet_3b*75<<8) | (packet_4b[2]);
  802.                     end else
  803.                         gd_start_sector <= {packet_2b, packet_3b, packet_4b};
  804.                    
  805.                     gd_sector_count <= {packet_8b, packet_9b, packet_10b};
  806.  
  807.                     gd_status_reg[4] <= 1'b1;   // Set DSC (Seek Complete) status bit. (Spy Log)
  808.  
  809. //                  if (gd_features_reg[0] == 1) gd_state <= 15;    // TODO - Handle PIO as well as DMA from SD to DC! ***
  810. //                  else gd_state <= 10'd16;                        // (It looks like all CD READs are done via DMA anyway.)
  811.  
  812.  
  813. //                  gd_dma_state <= 10'd1;  // Kick-off the DMA transfer!
  814. //                  gd_state <= 10'd1;
  815.  
  816.                     if (block_dma_done) begin
  817.                         trig_block_read <= 1'b0;
  818.                         gd_status_reg[4] <= 1'b1;   // Set DSC bit (Seek complete) TESTING !!
  819.                         gds_procpacketdone();   // If DMA finished, assert an Int, then go back to Idle (gd_state 1).
  820.                     end
  821.                     else trig_block_read <= 1'b1;
  822.                 end            
  823.  
  824.                 SPI_CD_READ2: begin         // (Unimplemented in nullDC, but still sends status back). OzOnE
  825.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  826.                 end
  827.  
  828.                 SPI_GET_SCD: begin
  829.                     scd_format <= packet_1b[3:0];
  830.                
  831.                     byte_count <= 16'd0;                        // Zero the byte count.
  832.                     alloc_length <= {packet_3b, packet_4b};     // Allocation length in bytes (from WORD).
  833.                     gd_bytecount_high <= packet_3b;
  834.                     gd_bytecount_low <= packet_4b;
  835.  
  836.                     gds_pio_send_data();                        // Int, to start transfer. "When preparations are complete...".
  837.                     gd_state <= 10'd14;     // <- *** TODO - No actual subcode data sent back yet! ***
  838.                 end
  839.  
  840.  
  841.             default:  gd_state <= 10'd1;    // Unhandled SPI command, back to idle.
  842.             endcase // endcase for "packet" parse!
  843.         end
  844.  
  845.         // Generate TOC reply.
  846.         9: if (toc_density == 1'b0) begin       // Request was for Single-Density TOC!
  847.                 // Generate Single-Density TOC...
  848.                 if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  849.                     case (byte_count)
  850.                         0: gd_data_write <= 16'h0041;   // Track 1 == (Data track, control bit 4) (Sub Q indicates pos, adr bit 0).
  851.                         2: gd_data_write <= 16'h9600;   // Start FAD of track 1 is 0x0096 (FAD 150, so LBA 0).
  852.  
  853.                         4: gd_data_write <= 16'h0001;   // Track 2 == (Audio track, no control bits) (Sub Q indicates pos, adr bit 0).
  854.                         6: gd_data_write <= 16'hEE02;   // Start FAD of track 2 is 0x02EE (FAD 750, so LBA 600).
  855.  
  856.                                                         // All "0xFFFF's" in between are sent by default case!
  857.                         396: gd_data_write <= 16'h0141; // Start track (1) Data.
  858.                         398: gd_data_write <= 16'h0000;
  859.                         400: gd_data_write <= 16'h0201; // End track (2) Audio.
  860.                         402: gd_data_write <= 16'h0000;
  861.                         404: gd_data_write <= 16'h0001; // Lead out.
  862.                         406: gd_data_write <= 16'h2C1A; // 0x1A2C (FAD 6700, so LBA 6850 decimal)
  863.                
  864.                     default: gd_data_write <= 16'hffff; // All other words are 0xFFFF (important!).
  865.                     endcase
  866.            
  867.                     if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  868.                     if (byte_count >= alloc_length) gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  869.                 end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  870.             end
  871.             else begin      // Else, Request was for Double-Density TOC!...
  872.             // Generate Double-Density TOC...
  873.             if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  874.  
  875.                 case (byte_count)                   // The following TOC is taken from the Crazy Taxi (PAL) original GD...
  876.                     0: gd_data_write <= 16'hFFFF;   // Definitely sends 0xFFFF's first?
  877.                     2: gd_data_write <= 16'hFFFF;
  878.                     4: gd_data_write <= 16'hFFFF;
  879.                     6: gd_data_write <= 16'hFFFF;
  880.                     8: gd_data_write <= 16'h0041;   // Data track (top nibble 4 == DATA track)...
  881.                     10: gd_data_write <= 16'h5EB0;  // FAD 0x00B05E (45150, so LBA 45000).
  882.                                                     // All "0xFFFF's" in between are sent by default case!
  883.                     396: gd_data_write <= 16'h0341; // Start track (3).
  884.                     398: gd_data_write <= 16'h0000;
  885.                     400: gd_data_write <= 16'h0341; // End track (3).
  886.                     402: gd_data_write <= 16'h0000;
  887.                     404: gd_data_write <= 16'h0841; // Lead out.
  888.                     406: gd_data_write <= 16'hB461; // FAD 0x0861B4 (549300, so LBA 549150 decimal)
  889.                
  890.                     default: gd_data_write <= 16'hffff; // All other words are 0xFFFF (important!).
  891.                 endcase
  892.  
  893.                 if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  894.                 if (byte_count >= alloc_length) gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  895.                 end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  896.             end // end for "else begin" (request Double-Density TOC).
  897.  
  898.         10: begin   // Generate "ATA_IDENTIFY_DEV (0xa1)" or "SPI_REQ_MODE (0x11)" or Security 0x71 reply (from ROM).
  899.             if ({gd_cs, gd_addr} == 5'b10000) rom_route <= 1'b1;    // ONLY start routing the Reply ROM --> "gd_data" when DATA reg is first read!
  900.  
  901.             if (rom_route == 1'b1 && gd_rd_rising) begin    // If GD is accessing the data reg...
  902.                 reply_rom_addr <= reply_rom_addr + 10'd1;   // Increment ROM address (ROM is WORD addressed!)
  903.                 byte_count <= byte_count + 16'd2;           // Increment byte count (in WORDS)!
  904.             end
  905.  
  906.             if (byte_count >= alloc_length) begin
  907.                 rom_route <= 1'b0;      // Disable Reply ROM --> "gd_data" route.
  908.                 gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  909.             end
  910.         end
  911.  
  912.         11: begin   // Generate SPI_REQ_SES reply.
  913.             if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  914.                 // "packet_2b" contains the read offset!
  915.                 case (packet_2b + byte_count)
  916.                     0: gd_data_write <= {12'h000, gd_secnumber_reg[3:0]};   // CD Status - Is this good enough as a status? OzOnE
  917.                     2: gd_data_write <= 16'h0803;   // Starting TNO (Track NO?) = 3
  918.                     4: gd_data_write <= 16'hB461;   // Lead out / Starting FAD = 0x0861B4 (549300, so LBA 549150 decimal)
  919.                
  920.                     default: gd_data_write <= 16'h0000; // All other words are 0x0000.
  921.                 endcase
  922.  
  923.                 if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  924.                 if (byte_count >= alloc_length) begin
  925.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  926.                 end
  927.             end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  928.         end
  929.  
  930.         12: begin   // Generate SPI_REQ_ERROR reply.
  931.             if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  932.                 // No read offset apparently? OzOnE.
  933.                 gd_data_write <= {resp[byte_count+1], resp[byte_count]};// Need to build a WORD.
  934.            
  935.                 if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  936.                 if (byte_count >= alloc_length) begin
  937.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  938.                 end
  939.             end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  940.         end
  941.  
  942.         13: begin   // Generate SPI_REQ_STAT reply.
  943.             if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  944.                 // "packet_2b" contains the read offset!
  945.                 gd_data_write <= {stat[packet_2b + byte_count+1], stat[packet_2b + byte_count]};// Need to build a WORD.
  946.  
  947.                 if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  948.                 if (byte_count >= alloc_length) begin
  949.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  950.                 end
  951.             end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  952.         end
  953.  
  954.         14: begin   // Generate SPI_GET_SCD reply.
  955.                     // *** TODO - Generate this properly from RAW sector data ! ***
  956.  
  957.             // TODO - "sec_format" contains the format type (nibble), handle it!
  958.  
  959.             if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  960.                 case (byte_count)
  961.                     0: gd_data_write <= 16'h1500;   // Audio status (0x15 == "No audio status information") / reserved.
  962.                     2: gd_data_write <= 16'h6400;   // Subcode data length. Byte 3 is LSB apparently!
  963.  
  964.                     default: gd_data_write <= 16'h0000; // All other words are 0x0000. ** TODO !! **
  965.                 endcase
  966.  
  967.                 if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  968.                 if (byte_count >= alloc_length) begin
  969.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  970.                 end
  971.             end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  972.         end
  973.  
  974.         15: begin   // Generate SPI_REQ_MODE
  975.             if ({gd_cs, gd_addr} == 5'b10000) begin // If in DATA reg...
  976.                 // "packet_2b" contains the READ offset!
  977.                 gd_data_write <= {mode[packet_2b + byte_count+1], mode[packet_2b + byte_count]};// Need to build a WORD.
  978.  
  979.                 if (gd_rd_rising) byte_count <= byte_count + 16'd2; // Increment in WORDS!
  980.                 if (byte_count >= alloc_length) begin
  981.                     gds_procpacketdone();   // Send status back (goes back to idle afterwards).
  982.                 end
  983.             end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  984.         end
  985.  
  986.         16: begin   // Receive SPI_SET_MODE data and update mode storage regs...
  987.             if (gd_cs == 2'd2 && gd_addr == 3'd0 && gd_wr_rising) begin // If in DATA reg on RISING edge of "gd_wr_n"...
  988.                 // "packet_2b" contains the WRITE offset!
  989.                 //mode[packet_2b + byte_count+1] <= gd_data[15:8]; mode[packet_2b + byte_count] <= gd_data[7:0];
  990.                 byte_count <= byte_count + 16'd2;   // Increment in WORDS!
  991.             end // end for "if in DATA reg" - byte_count incrementing MUST be in here!
  992.  
  993. //              gd_bytecount_high <= byte_count[15:8];
  994. //              gd_bytecount_low <= byte_count[7:0];
  995.                 if (byte_count >= alloc_length) begin
  996. // CF Card version...
  997.                     // "gds_pio_send_data"...
  998.                     //(2)   IO bit is set and CoD bit is cleared.
  999.                     gd_intreason_reg[1] <= 1'b1;    // Set IO bit.
  1000.                     gd_intreason_reg[0] <= 1'b0;    // Clear CoD bit.
  1001.                     //(3)   DRQ bit is set, BSY bit is cleared.
  1002.                     gd_status_reg[3] <= 1'b0;   // Clear DRQ bit. <- special case for SET_MODE ! (All data received, clear DRQ).
  1003.                     gd_status_reg[7] <= 1'b1;   // Set BUSY bit. <- special case for SET_MODE ! (DC needs to see busy bit at least once afterwards).
  1004.                     //(4)   INTRQ is set, and a host interrupt is issued.
  1005.                     gd_int_rq_reg <= 1'b1;
  1006.  
  1007.  
  1008.                     gd_state <= 10'd17;
  1009.                 end
  1010.         end
  1011.  
  1012.         17: if (gd_cs == 2'd2 && gd_addr == 3'd7 && gd_rd_rising) begin // After STAT / CMD reg is read at least once...
  1013.             //if (gd_cs == 2'd1 && gd_addr == 3'd6 && gd_rd_rising) begin   // After ALTSTAT reg is read at least once...
  1014.                 gd_int_rq_reg <= 1'b0;      // Clear INTRQ!
  1015.                 gd_state <= 10'd1;          // Go back to IDLE (will clear the BUSY bit for us).
  1016.             end
  1017.  
  1018.     default: gd_state <= 10'd1; // Shouldn't ever get a wrong state here, but just in case.
  1019.     endcase
  1020.  
  1021.  
  1022.     case (gd_dma_state)
  1023.         0: begin            // IDLE state (do nothing until main "gd_state" sets "gd_dma_state" to 1...
  1024.         end
  1025.        
  1026.         1: begin
  1027.             trig_block_read <= 1'b1;
  1028.             gd_dma_state <= 10'd2;
  1029.         end
  1030.  
  1031.         2: if (block_dma_done) begin
  1032.             trig_block_read <= 1'b0;
  1033.             gd_status_reg[4] <= 1'b1;   // Set DSC bit (Seek complete) TESTING !!
  1034.             gds_procpacketdone();   // If DMA finished, assert an Int, then go back to Idle (gd_state 1).
  1035.             gd_dma_state <= 10'd0;  // Go back to Idle DMA state.
  1036.         end
  1037.  
  1038.         default: gd_dma_state <= 10'd0;
  1039.         endcase
  1040.  
  1041. end // For "end else begin" main process.
  1042.  
  1043.  
  1044. task gds_pio_send_data;
  1045. begin
  1046.     // When preparations are complete, the following steps are carried out at the device.
  1047.     //(1)   Number of bytes to be read is set in "Byte Count" register.
  1048. //  gd_bytecount_high <= alloc_length[15:8];    // <- Doesn't work because alloc_length is usually in the same time slot!
  1049. //  gd_bytecount_low <= alloc_length[7:0];      // (Now setting "gd_bytecount" stuff directly before calling this task). OzOnE.
  1050.     //(2)   IO bit is set and CoD bit is cleared.
  1051.     gd_intreason_reg[1] <= 1'b1;    // Set IO bit (Device -> Host).
  1052.     gd_intreason_reg[0] <= 1'b0;    // Clear CoD bit (we have DATA to transfer).
  1053.     //(3)   DRQ bit is set, BSY bit is cleared.
  1054.     gd_status_reg[3] <= 1'b1;   // Set DRQ bit (preparations for data transfer are complete).
  1055.     gd_status_reg[7] <= 1'b0;   // Clear BUSY bit (command block can be accessed). Should probably clear this at "gd_state" 1? OzOnE.
  1056.     //(4)   INTRQ is set, and a host interrupt is issued.
  1057.     gd_int_rq_reg <= 1'b1;
  1058. end
  1059. endtask
  1060.  
  1061. task gds_procpacketdone;
  1062. begin
  1063.     // "Send status" - Done with transferring REQ data.
  1064.     // "gds_procpacketdone". Also does "gio_pio_end", because it only clears DRQ (done below anyway)..
  1065.     // 7.   When the device is ready to send the status, it writes the
  1066.     // final status (IO, CoD, DRDY set, BSY, DRQ cleared) to the "Status" register before making INTRQ valid.
  1067.     // After checking INTRQ, the host reads the "Status" register to check the completion status.
  1068.     //Set IO, CoD, DRDY
  1069.     gd_intreason_reg[1] <= 1'b1;    // Set IO bit (Device -> Host).
  1070.     gd_intreason_reg[0] <= 1'b1;    // Set CoD bit (int reason was a COMMAND).
  1071.     gd_status_reg[6] <= 1'b1;       // Set DRDY bit (drive is able to respond to ATA command?).
  1072.  
  1073.     //Clear DRQ,BSY
  1074.     gd_status_reg[3] <= 1'b0;   // Clear DRQ bit (don't have any data to transfer).
  1075.     gd_status_reg[7] <= 1'b0;   // Clear BUSY bit (command block can be accessed). Should probably clear this at "gd_state" 1? OzOnE.
  1076.  
  1077.     //Make INTRQ valid
  1078.     gd_int_rq_reg <= 1'b1;
  1079.            
  1080.     gd_state <= 10'd1;      // Back to idle (wait for next ATA command).
  1081. end
  1082. endtask
  1083.  
  1084. /*wire SYSCLK;
  1085. wire SDRAM_CLK_100M;
  1086. wire cdda_clk_pll;
  1087. // Main PLL. "SYSCLK" is 100MHz, "SDRAM _CLK_100M" is 100MHZ + 5ns phase shift, "cdda_clk_pll" is roughly 33.8688MHz (44100 * 768).
  1088. PLL_50M_100M    PLL_50M_100M_inst (
  1089.     .inclk0 (CLK50M),
  1090.     .c0 (SYSCLK),
  1091.     .c1 (SDRAM_CLK_100M),
  1092.     .c2 (cdda_clk_pll)
  1093.     ); */
  1094.  
  1095. wire SYSCLK;
  1096. wire CLK24M;
  1097. wire cdda_clk_pll;
  1098. // Main PLL. "SYSCLK" is 100MHz, "CLK24M" is 24MHz for DAC MCLK, "cdda_clk_pll" is roughly 33.8688MHz (44100 * 768).
  1099. PLL_50M_100M    PLL_50M_100M_inst (
  1100.     .inclk0 (CLK50M),
  1101.     .c0 (SYSCLK),
  1102.     .c1 (CLK24M),
  1103.     .c2 (cdda_clk_pll)
  1104.     );
  1105.  
  1106. wire [15:0] reply_rom_data/* synthesis noprune */;
  1107. reply_rom   reply_rom_inst (
  1108.     .address (reply_rom_addr),
  1109.     .clock (CLK50M),
  1110.     .q (reply_rom_data)
  1111.     );
  1112.  
  1113.  
  1114. wire [15:0] sector_data;
  1115. wire cont_dma_rq;
  1116. wire block_dma_done;
  1117. control control_inst (
  1118.     .SYSCLK (CLK50M),
  1119.     .SDRAM_CLK_100M (CLK50M),   // TESTING!! Using SYSCLK instead of SDRAM_CLK_100M, 'cos I'm using the PLL for CLK24M!
  1120.     .RST_n (gd_rst_n),  
  1121.     .gd_sector_type (gd_sector_type),
  1122.     .gd_start_sector (gd_start_sector),
  1123.     .gd_sector_count (gd_sector_count),
  1124.     .trig_block_read (trig_block_read),
  1125.     .block_dma_done (block_dma_done),
  1126.     .sector_data (sector_data),
  1127.     .cont_dma_rq (cont_dma_rq),
  1128.     .gd_dma_ack_n (gd_dma_ack_n),
  1129.     .gd_rd_n (gd_rd_n),
  1130.     .SRAM_ADDR (SRAM_ADDR),
  1131.     .SRAM_OE_n (SRAM_OE_n),
  1132.     .SRAM_WE_n (SRAM_WE_n),
  1133.     .SRAM_DATA (SRAM_DATA),
  1134.     .SDRAM_CLK (SDRAM_CLK),     //connected to the clk port of SDRAM
  1135.     .SDRAM_CKE (SDRAM_CKE),     //connected to the cke port of SDRAM
  1136.     .SDRAM_CS_n (SDRAM_CS_n),   //connected to the CS_n port of SDRAM
  1137.     .SDRAM_RAS_n (SDRAM_RAS_n), //connected to the RAS_n port of SDRAM
  1138.     .SDRAM_CAS_n (SDRAM_CAS_n), //connected to the CAS_n port of SDRAM
  1139.     .SDRAM_WE_n (SDRAM_WE_n),   //connected to the WE_n port of SDRAM
  1140.     .SDRAM_DQM (SDRAM_DQM),     //connected to the LDQM port of SDRAM
  1141.     .SDRAM_BA (SDRAM_BA),       //connected to the BA port of SDRAM
  1142.     .SDRAM_ADDR (SDRAM_ADDR),   //connected to the ADDR port of SDRAM
  1143.     .SDRAM_DQ (SDRAM_DQ),       //connected to the DQ port of SDRAM
  1144.     .SD_CS_n (SD_CS_n),
  1145.     .SD_CLK (SD_CLK),
  1146.     .SD_DATA_IN (SD_DATA_IN),
  1147.     .SD_DATA_OUT (SD_DATA_OUT),
  1148.     .AUD_BCLK(AUD_BCLK),
  1149.     .AUD_DACLRC(AUD_DACLRC),
  1150.     .AUD_DACDAT(AUD_DACDAT),
  1151.     .SEG_S(SEG_S),
  1152.     .SEG(SEG),
  1153.     .USB_TXE_n(USB_TXE_n),
  1154.     .USB_RXF_n(USB_RXF_n),
  1155.     .USB_RD_n(USB_RD_n),
  1156.     .USB_WR(USB_WR),
  1157.     .USB_DATA(USB_DATA),
  1158.     .error(LED0)
  1159.     );
  1160.  
  1161.  
  1162.  
  1163. WM8731_config WM8731_config_inst (
  1164.   .CLK50M(CLK50M),
  1165.   .RST_n(gd_rst_n),
  1166.  
  1167.   .AUD_I2C_SCK(AUD_I2C_SCK),
  1168.   .AUD_I2C_DAT(AUD_I2C_DAT)
  1169. );
  1170.  
  1171. always @ (posedge CLK24M) AUD_MCLK <= ~AUD_MCLK;
  1172.  
  1173.  
  1174.  
  1175. endmodule
Advertisement
Add Comment
Please, Sign In to add comment