Subversion Repositories ngs

Rev

Rev 20 | Rev 28 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // Part of NeoGS project
  2. //
  3. // FPGA early and on-the-fly configuration, Z80 clock switch,
  4. // 3.3v RAM buffer
  5. //
  6. // (c) 2008-2010 NedoPC
  7.  
  8. module GS_cpld(
  9. output reg disbl,
  10.         output reg         config_n,  // ACEX1K config pins
  11.         input  wire        status_n,  //
  12.         input  wire        conf_done, //
  13.         output wire        cs,        //
  14.         input  wire        init_done, //
  15.  
  16.  
  17.         input  wire        clk24in, // 24mhz in
  18.         input  wire        clk20in, // 20mhz in
  19.         input  wire        clksel0, // clock select 0 (1=divide by 2, 0=no divide)
  20.         input  wire        clksel1, // clock select 1 (1=clk20in, 0=clk24in)
  21.         output wire        clkout,  // clock out
  22.  
  23.         input  wire        clkin, // input of clkout signal, buffered, same as for Z80
  24.  
  25.  
  26.         input  wire        coldres_n, // resets
  27.         output reg         warmres_n,
  28.  
  29.  
  30.         input  wire        iorq_n, // Z80 control signals
  31.         input  wire        mreq_n,
  32.         input  wire        rd_n,
  33.         input  wire        wr_n,
  34.  
  35.         inout  wire [ 7:0] d, // Z80 data bus
  36.  
  37.         input  wire        a6,  // some Z80 addresses
  38.         input  wire        a7,
  39.         input  wire        a10,
  40.         input  wire        a11,
  41.         input  wire        a12,
  42.         input  wire        a13,
  43.         input  wire        a14,
  44.         input  wire        a15,
  45.  
  46.  
  47.         output wire        mema14,
  48.         output wire        mema15,
  49.         output wire        mema19,
  50.  
  51.         inout  wire        romcs_n,
  52.         inout  wire        memoe_n,
  53.         inout  wire        memwe_n,
  54.  
  55.         input  wire        in_ramcs0_n,
  56.         input  wire        in_ramcs1_n,
  57.         input  wire        in_ramcs2_n,
  58.         input  wire        in_ramcs3_n,
  59.  
  60.         output wire        out_ramcs0_n,
  61.         output wire        out_ramcs1_n,
  62.  
  63.  
  64.         output wire        ra6,  // some buffered memory addresses
  65.         output wire        ra7,
  66.         output wire        ra10,
  67.         output wire        ra11,
  68.         output wire        ra12,
  69.         output wire        ra13,
  70.  
  71.         inout  wire [ 7:0] rd // memory data bus
  72.  
  73. ,output reg int_romcs_n
  74. );
  75.  
  76.  
  77.  
  78.  
  79.         reg int_mema14,int_mema15;
  80.         reg /*int_romcs_n,*/int_ramcs_n;
  81.         wire int_memoe_n,int_memwe_n;
  82.         wire int_cs;
  83.  
  84.         wire ext_romcs_n,
  85.              ext_memoe_n,
  86.              ext_memwe_n;
  87.  
  88.  
  89.         reg [1:0] memcfg; // memcfg[1]: 1 ram, 0 roms
  90.                           // memcfg[0]: 0 page0, 1 page1 -> in 8000-ffff region
  91.  
  92. //      reg disbl; // =1 - cpld disabled, =0 - enabled
  93.  
  94.         reg was_cold_reset_n; // 1 - no cold reset, 0 - was cold reset
  95.  
  96.  
  97.         reg  [1:0] dbout;
  98.         wire [1:0] dbin;
  99.  
  100.         wire memcfg_write_n;
  101.         wire rescfg_write_n;
  102.  
  103.         wire coldrstf_read_n;
  104.         wire fpgastat_read_n;
  105.  
  106.  
  107.  
  108.  
  109.         assign dbin[1] = d[7];
  110.         assign dbin[0] = d[0];
  111.  
  112.  
  113.  
  114.         reg [3:0] rstcount; // counter for warm reset period
  115.  
  116.         reg [2:0] disbl_sync;
  117.  
  118.         // PORTS:
  119.         // {a7,a6}
  120.         // 00 - fpga ports
  121.         // 01,WR - write memcfg: d7=RAM(1)/ROM(0), d0=32k page(0/1)
  122.         // 01,RD - read cold_reset_n flag: d7=(0:was cold reset,1:no cold reset)
  123.         // 10,WR - set cold_reset_n flag & write FPGA nCONFIG: d7=1: set cold_reset_n flag, d0: nCONFIG
  124.         // 10,RD - read FPGA status: d7=nSTATUS, d0=CONF_DONE
  125.         // 11,WR - write to FPGA
  126.         // 11,RD - read from FPGA
  127.  
  128.  
  129.         // clock selector
  130.         clocker clk( .clk1(clk24in),
  131.                      .clk2(clk20in),
  132.                      .clksel(clksel1),
  133.                      .divsel(clksel0),
  134.                      .clkout(clkout)
  135.                    );
  136.  
  137.  
  138.         // disable control
  139.  
  140.         always @(negedge config_n,posedge init_done)
  141.         begin
  142.                 if( !config_n ) // asynchronous reset
  143.                         disbl <= 0;
  144.                 else // posedge of init_done, synchronous set
  145.                         disbl <= 1;
  146.         end
  147.  
  148.  
  149.  
  150.         // memory control pins when running without configured FPGA
  151.         assign mema14  = disbl ? 1'bZ : int_mema14;
  152.         assign mema15  = disbl ? 1'bZ : int_mema15;
  153.         assign romcs_n = disbl ? 1'bZ : int_romcs_n;
  154.         assign memoe_n = disbl ? 1'bZ : int_memoe_n;
  155.         assign memwe_n = disbl ? 1'bZ : int_memwe_n;
  156.         assign cs      = disbl ? 1'bZ : int_cs;
  157.  
  158.         assign ext_romcs_n = 1'b1;//romcs_n;
  159.         assign ext_memoe_n = memoe_n;
  160.         assign ext_memwe_n = memwe_n;
  161.  
  162.  
  163.         // controlling memory paging
  164.         always @*
  165.         begin
  166.                 casex( {a15,a14,memcfg[1]} )
  167.                 3'b00x:
  168.                         {int_mema15,int_mema14,int_romcs_n,int_ramcs_n} <= 4'b0001;
  169.                 3'b01x:
  170.                         {int_mema15,int_mema14,int_romcs_n,int_ramcs_n} <= 4'b0010;
  171.                 3'b1x0:
  172.                         {int_mema15,int_mema14,int_romcs_n,int_ramcs_n} <= {memcfg[0],a14,2'b01};
  173.                 3'b1x1:
  174.                         {int_mema15,int_mema14,int_romcs_n,int_ramcs_n} <= {memcfg[0],a14,2'b10};
  175.                 endcase
  176.         end
  177.  
  178.         // controlling memory /OE, /WE
  179.         assign int_memoe_n = mreq_n | rd_n;
  180.         assign int_memwe_n = mreq_n | wr_n;
  181.  
  182.  
  183.         // writing paging register [1:0] memcfg
  184.         assign memcfg_write_n = iorq_n | wr_n | a7 | ~a6; // {a7,a6}==01
  185.  
  186.         always @(negedge coldres_n, posedge memcfg_write_n)
  187.         begin
  188.                 if( !coldres_n ) // reset on coldres_n
  189.                         memcfg <= 2'b00;
  190.                 else // write on memcfg_write_n
  191.                         memcfg <= dbin;
  192.         end
  193.  
  194.  
  195.         // writing nCONFIG and cold reset "register"
  196.         assign rescfg_write_n = iorq_n | wr_n | ~a7 | a6; // {a7,a6}==10
  197.  
  198.         always @(negedge coldres_n, posedge rescfg_write_n)
  199.         begin
  200.                 if( coldres_n==0 ) // async reset
  201.                 begin
  202.                         was_cold_reset_n <= 0; // there was!
  203.                         config_n <= 0; // start FPGA config
  204.                 end
  205.                 else // sync set/load
  206.                 begin
  207.                         config_n <= dbin[0];
  208.                         was_cold_reset_n <= dbin[1] | was_cold_reset_n;
  209.                 end
  210.         end
  211.  
  212.  
  213.         // controlling positive CS pin to FPGA
  214.         assign int_cs = a7 & a6; // {a7,a6}==11
  215.  
  216.  
  217.  
  218.         // reading control
  219.         assign coldrstf_read_n = iorq_n | rd_n | a7 | ~a6; // {a7,a6}=01
  220.         assign fpgastat_read_n = iorq_n | rd_n | ~a7 | a6; // {a7,a6}=10
  221.  
  222.  
  223.  
  224.         always @*
  225.         begin
  226.                 case( {coldrstf_read_n,fpgastat_read_n} )
  227.                         2'b01:
  228.                                 dbout = { was_cold_reset_n, 1'bX };
  229.                         2'b10:
  230.                                 dbout = { status_n, conf_done };
  231.                         default:
  232.                                 dbout = 2'bXX;
  233.                 endcase
  234.         end
  235.  
  236.  
  237.  
  238.         // warm resetter control
  239.  
  240.         always @(posedge clkin)
  241.         begin
  242.                 disbl_sync[2:0]={disbl_sync[1:0],disbl};
  243.         end
  244.  
  245.         always @(negedge coldres_n,posedge clkin)
  246.         begin
  247.                 if( coldres_n==0 ) // async reset
  248.                 begin
  249.                         rstcount <= (-1);
  250.                         warmres_n <= 0;
  251.                 end
  252.                 else // posedge clkin
  253.                 begin
  254.                         if( disbl_sync[2]==0 && disbl_sync[1]==1 ) // positive pulse
  255.                         begin
  256.                                 warmres_n <= 0;
  257.                                 rstcount <= (-1);
  258.                         end
  259.                         else // no disbl_sync positive pulse
  260.                         begin
  261.                                 rstcount <= rstcount - 1;
  262.                                 if( |rstcount == 0 )
  263.                                         warmres_n <= 1'bZ;
  264.                         end
  265.                 end
  266.  
  267.         end
  268.  
  269.  
  270.  
  271.         // Z80 data bus control
  272.  
  273.         assign d = ( (!coldrstf_read_n)||(!fpgastat_read_n) )   ?
  274.                    { dbout[1], 6'bXXXXXX, dbout[0] }            :
  275.                    ( (ext_romcs_n&&(!ext_memoe_n)) ? rd : 8'bZZZZZZZZ ) ;
  276.  
  277.         // memory data bus control
  278.  
  279.         assign rd = (ext_romcs_n&&(!ext_memwe_n)) ? d : 8'bZZZZZZZZ;
  280.  
  281.         // memory addresses buffering
  282.  
  283.         assign ra6  = a6;
  284.         assign ra7  = a7;
  285.         assign ra10 = a10;
  286.         assign ra11 = a11;
  287.         assign ra12 = a12;
  288.         assign ra13 = a13;
  289.  
  290.  
  291.         // memory CS'ing
  292.  
  293.         assign out_ramcs0_n = disbl ? ( in_ramcs0_n & in_ramcs1_n ) : int_ramcs_n;
  294.         assign out_ramcs1_n = disbl ? ( in_ramcs2_n & in_ramcs3_n ) : 1'b1;
  295.  
  296.         assign mema19 = disbl ? ( in_ramcs0_n & in_ramcs2_n ) : 1'b0;
  297.  
  298.  
  299.  
  300. endmodule
  301.  
  302.