Subversion Repositories pentevo

Rev

Rev 674 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. // ZX-Evo Base Configuration (c) NedoPC 2008,2009,2010,2011,2012,2013,2014
  2. //
  3. // Z80 memory manager: routes ROM/RAM accesses, makes wait-states for 14MHz or stall condition, etc.
  4.  
  5. /*
  6.     This file is part of ZX-Evo Base Configuration firmware.
  7.  
  8.     ZX-Evo Base Configuration firmware is free software:
  9.     you can redistribute it and/or modify it under the terms of
  10.     the GNU General Public License as published by
  11.     the Free Software Foundation, either version 3 of the License, or
  12.     (at your option) any later version.
  13.  
  14.     ZX-Evo Base Configuration firmware is distributed in the hope that
  15.     it will be useful, but WITHOUT ANY WARRANTY; without even
  16.     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17.     See the GNU General Public License for more details.
  18.  
  19.     You should have received a copy of the GNU General Public License
  20.     along with ZX-Evo Base Configuration firmware.
  21.     If not, see <http://www.gnu.org/licenses/>.
  22. */
  23.  
  24. //
  25. // fclk    _/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\_/`\
  26. //          |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
  27. // zclk     /```\___/```\___/```\___/```````\_______/```````\_______/```````````````\_______________/```````````````\_______________/`
  28. //          |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
  29. // zpos     `\___/```\___/```\___/```\___________/```\___________/```\___________________________/```\___________________________/```\
  30. //          |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
  31. // zneg     _/```\___/```\___/```\_______/```\___________/```\___________________/```\___________________________/```\________________
  32.  
  33. `include "../include/tune.v"
  34.  
  35. module zmem(
  36.  
  37.         input  wire fclk,
  38.         input  wire rst_n,
  39.  
  40.         input  wire zpos, //
  41.         input  wire zneg, // strobes which show positive and negative edges of zclk
  42.  
  43.         input  wire cbeg,      // DRAM synchronization
  44.         input  wire post_cbeg, //
  45.         input  wire pre_cend,  //
  46.         input  wire cend,      //
  47.  
  48.  
  49.         input  wire [15:0] za,
  50.  
  51.         input  wire [ 7:0] zd_in, // won't emit anything to Z80 bus, data bus mux is another module
  52.         output wire [ 7:0] zd_out, // output to Z80 bus
  53.  
  54.         output wire zd_ena, // out bus to the Z80
  55.  
  56.         input  wire m1_n,
  57.         input  wire rfsh_n,
  58.         input  wire mreq_n,
  59.         input  wire iorq_n,
  60.         input  wire rd_n,
  61.         input  wire wr_n,
  62.  
  63.  
  64.         input  wire [ 1:0] int_turbo, // 2'b00 - 3.5,
  65.                                       // 2'b01 - 7.0,
  66.                                       // 2'b1x - 14.0
  67.  
  68.  
  69.  
  70.         input  wire        win0_romnram, // four windows, each 16k,
  71.         input  wire        win1_romnram, // ==1 - there is rom,
  72.         input  wire        win2_romnram, // ==0 - there is ram
  73.         input  wire        win3_romnram, //
  74.  
  75.         input  wire [ 7:0] win0_page, // which 16k page is in given window
  76.         input  wire [ 7:0] win1_page, //
  77.         input  wire [ 7:0] win2_page, //
  78.         input  wire [ 7:0] win3_page, //
  79.  
  80.         input  wire        win0_wrdisable, // ==1 - no write is possible to window
  81.         input  wire        win1_wrdisable,
  82.         input  wire        win2_wrdisable,
  83.         input  wire        win3_wrdisable,
  84.  
  85.  
  86.         input  wire        romrw_en,
  87.  
  88.  
  89.         output reg  [ 4:0] rompg, // output for ROM paging
  90.         output wire        romoe_n,
  91.         output wire        romwe_n,
  92.         output wire        csrom,
  93.  
  94.  
  95.         output wire        cpu_req,
  96.         output wire        cpu_rnw,
  97.         output wire [20:0] cpu_addr,
  98.         output wire [ 7:0] cpu_wrdata,
  99.         output wire        cpu_wrbsel,
  100.  
  101.         input  wire [15:0] cpu_rddata,
  102.  
  103.         input  wire        cpu_next,
  104.         input  wire        cpu_strobe,
  105.  
  106.  
  107.         output wire        cpu_stall // for zclock
  108.  
  109. );
  110.  
  111.  
  112.         wire [1:0] win;
  113.         reg [7:0] page;
  114.         reg romnram;
  115.         reg wrdisable;
  116.  
  117.  
  118.  
  119.  
  120.         reg [15:0] rd_buf;
  121.  
  122.         reg [15:1] cached_addr;
  123.         reg        cached_addr_valid;
  124.  
  125.         wire cache_hit;
  126.  
  127.  
  128.         wire dram_beg;
  129.         wire opfetch, memrd, memwr;
  130.         wire stall14, stall7_35;
  131.  
  132.         wire stall14_ini;
  133.         wire stall14_cyc;
  134.         reg  stall14_cycrd;
  135.         reg  stall14_fin;
  136.  
  137.         reg r_mreq_n;
  138.  
  139.  
  140.         reg pending_cpu_req;
  141.  
  142.         reg cpu_rnw_r;
  143.  
  144.  
  145.  
  146.         // this is for 7/3.5mhz  
  147.         wire ramreq;
  148.         wire ramwr,ramrd;
  149.         wire cpureq_357;
  150.         reg ramrd_reg,ramwr_reg;
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.         // make paging
  158.         assign win[1:0] = za[15:14];
  159.  
  160.         always @*
  161.         case( win )
  162.                 2'b00: begin
  163.                         page      = win0_page;
  164.                         romnram   = win0_romnram;
  165.                         wrdisable = win0_wrdisable;
  166.                 end
  167.  
  168.                 2'b01: begin
  169.                         page      = win1_page;
  170.                         romnram   = win1_romnram;
  171.                         wrdisable = win1_wrdisable;
  172.                 end
  173.  
  174.                 2'b10: begin
  175.                         page      = win2_page;
  176.                         romnram   = win2_romnram;
  177.                         wrdisable = win2_wrdisable;
  178.                 end
  179.  
  180.                 2'b11: begin
  181.                         page      = win3_page;
  182.                         romnram   = win3_romnram;
  183.                         wrdisable = win3_wrdisable;
  184.                 end
  185.         endcase
  186.  
  187.  
  188.         // rom paging - only half a megabyte addressing.
  189.         always @*
  190.         begin
  191.                 rompg[4:0] = page[4:0];
  192.         end
  193.  
  194.  
  195.  
  196.  
  197.         assign romwe_n = wr_n | mreq_n | (~romrw_en) | wrdisable;
  198.         assign romoe_n = rd_n | mreq_n;
  199.  
  200.         assign csrom = romnram; // positive polarity!
  201.  
  202.  
  203.  
  204.         // 7/3.5mhz support
  205.  
  206.         assign ramreq = (~mreq_n) && (~romnram) && rfsh_n;
  207.         assign ramrd = ramreq & (~rd_n);
  208.         assign ramwr = (ramreq & (~wr_n)) & (~wrdisable);
  209.  
  210.         always @(posedge fclk)
  211.         if( cend && (!cpu_stall) )
  212.         begin
  213.                 ramrd_reg <= ramrd;
  214.                 ramwr_reg <= ramwr;
  215.         end
  216.  
  217.         assign cpureq_357 = ( ramrd & (~ramrd_reg) ) | ( ramwr & (~ramwr_reg) );
  218.        
  219.  
  220.  
  221.  
  222.         assign zd_ena = (~mreq_n) & (~rd_n) & (~romnram);
  223.  
  224.  
  225.  
  226.         assign cache_hit = ( (za[15:1] == cached_addr[15:1]) && cached_addr_valid );
  227.  
  228.  
  229.  
  230.         // strobe the beginnings of DRAM cycles
  231.  
  232.         always @(posedge fclk)
  233.         if( zneg )
  234.                 r_mreq_n <= mreq_n | (~rfsh_n);
  235.         //
  236.         assign dram_beg = ( (!cache_hit) || memwr ) && zneg && r_mreq_n && (!romnram) && (!mreq_n) && rfsh_n;
  237.  
  238.         // access type
  239.         assign opfetch = (~mreq_n) && (~m1_n);
  240.         assign memrd   = (~mreq_n) && (~rd_n);
  241.         assign memwr   = (~mreq_n) &&   rd_n && rfsh_n && (!wrdisable);
  242.  
  243.  
  244.         // wait tables:
  245.         //
  246.         // M1 opcode fetch, dram_beg coincides with:
  247.         // cend:      +3
  248.         // pre_cend:  +4
  249.         // post_cbeg: +5
  250.         // cbeg:      +6
  251.         //
  252.         // memory read, dram_beg coincides with:
  253.         // cend:      +2
  254.         // pre_cend:  +3
  255.         // post_cbeg: +4
  256.         // cbeg:      +5
  257.         //
  258.         // memory write: no wait
  259.         //
  260.         // special case: if dram_beg pulses 1 when cpu_next is 0,
  261.         // unconditional wait has to be performed until cpu_next is 1, and
  262.         // then wait as if dram_beg would coincide with cbeg
  263.  
  264.         assign stall14_ini = dram_beg && ( (!cpu_next) || opfetch || memrd ); // no wait at all in write cycles, if next dram cycle is available
  265.  
  266.  
  267.         // memrd, opfetch - wait till cend & cpu_next,
  268.         // memwr - wait till cpu_next
  269.         assign stall14_cyc = memwr ? (!cpu_next) : stall14_cycrd;
  270.         //
  271.         always @(posedge fclk, negedge rst_n)
  272.         if( !rst_n )
  273.                 stall14_cycrd <= 1'b0;
  274.         else // posedge fclk
  275.         begin
  276.                 if( cpu_next && cend )
  277.                         stall14_cycrd <= 1'b0;
  278.                 else if( dram_beg && ( (!cend) || (!cpu_next) ) && (opfetch || memrd) )
  279.                         stall14_cycrd <= 1'b1;
  280.         end
  281.         //
  282.         always @(posedge fclk, negedge rst_n)
  283.         if( !rst_n )
  284.                 stall14_fin <= 1'b0;
  285.         else // posedge fclk
  286.         begin
  287.                 if( stall14_fin && ( (opfetch&pre_cend) || (memrd&post_cbeg) ) )
  288.                         stall14_fin <= 1'b0;
  289.                 else if( cpu_next && cend && cpu_req && (opfetch || memrd) )
  290.                         stall14_fin <= 1'b1;
  291.         end
  292.  
  293.  
  294.         //
  295.         assign cpu_stall = int_turbo[1] ? (stall14_ini | stall14_cyc | stall14_fin) : (cpureq_357 && (!cpu_next));
  296.  
  297.         // cpu request
  298.         assign cpu_req = int_turbo[1] ? (pending_cpu_req | dram_beg) : cpureq_357;
  299.         //
  300.         assign cpu_rnw = int_turbo[1] ? (dram_beg ? (!memwr) : cpu_rnw_r) : ramrd;
  301.         //
  302.         //
  303.         always @(posedge fclk, negedge rst_n)
  304.         if( !rst_n )
  305.                 pending_cpu_req <= 1'b0;
  306.         else if( cpu_next && cend )
  307.                 pending_cpu_req <= 1'b0;
  308.         else if( dram_beg )
  309.                 pending_cpu_req <= 1'b1;
  310.         //
  311.         always @(posedge fclk)
  312.         if( dram_beg )
  313.                 cpu_rnw_r <= !memwr;
  314.  
  315.  
  316.  
  317.         // address, data in and data out
  318.         //
  319.         assign cpu_wrbsel = za[0];
  320.         assign cpu_addr[20:0] = { page[7:0], za[13:1] };
  321.         assign cpu_wrdata = zd_in;
  322.         //
  323.         always @* if( cpu_strobe ) // WARNING! ACHTUNG! LATCH!!!
  324.                 rd_buf <= cpu_rddata;
  325.         //
  326.         assign zd_out = cpu_wrbsel ? rd_buf[7:0] : rd_buf[15:8];
  327.  
  328.  
  329.  
  330.  
  331.  
  332.         wire io;
  333.         reg  io_r;
  334.         //
  335.         assign io = (~iorq_n);
  336.         //
  337.         always @(posedge fclk)
  338.         if( zpos )
  339.                 io_r <= io;
  340.         //
  341.         always @(posedge fclk, negedge rst_n)
  342.         if( !rst_n )
  343.         begin
  344.                 cached_addr_valid <= 1'b0;
  345.         end
  346.         else
  347.         begin
  348.                 if( (zneg && r_mreq_n && (!mreq_n) && rfsh_n && romnram) ||
  349.                     (zneg && r_mreq_n && memwr                         ) ||
  350.                     (io && (!io_r) && zpos                             ) )
  351.                         cached_addr_valid <= 1'b0;
  352.                 else if( cpu_strobe )
  353.                         cached_addr_valid <= 1'b1;
  354.         end
  355.         //
  356.         always @(posedge fclk)
  357.         if( !rst_n )
  358.         begin
  359.                 cached_addr <= 15'd0;
  360.         end
  361.         else if( cpu_strobe )
  362.         begin
  363.                 cached_addr[15:1] <= za[15:1];
  364.         end
  365.  
  366.  
  367.  
  368.  
  369. endmodule
  370.  
  371.