Subversion Repositories pentevo

Rev

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

  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.  
  81.         input  wire        romrw_en,
  82.  
  83.  
  84.         output reg  [ 4:0] rompg, // output for ROM paging
  85.         output wire        romoe_n,
  86.         output wire        romwe_n,
  87.         output wire        csrom,
  88.  
  89.  
  90.         output wire        cpu_req,
  91.         output wire        cpu_rnw,
  92.         output wire [20:0] cpu_addr,
  93.         output wire [ 7:0] cpu_wrdata,
  94.         output wire        cpu_wrbsel,
  95.  
  96.         input  wire [15:0] cpu_rddata,
  97.  
  98.         input  wire        cpu_next,
  99.         input  wire        cpu_strobe,
  100.  
  101.  
  102.         output wire        cpu_stall // for zclock
  103.  
  104. );
  105.  
  106.  
  107.         wire [1:0] win;
  108.         reg [7:0] page;
  109.         reg romnram;
  110.  
  111.  
  112.  
  113.  
  114.         reg [15:0] rd_buf;
  115.  
  116.         reg [15:1] cached_addr;
  117.         reg        cached_addr_valid;
  118.  
  119.         wire cache_hit;
  120.  
  121.  
  122.         wire dram_beg;
  123.         wire opfetch, memrd, memwr;
  124.         wire stall14, stall7_35;
  125.  
  126.         wire stall14_ini;
  127.         wire stall14_cyc;
  128.         reg  stall14_cycrd;
  129.         reg  stall14_fin;
  130.  
  131.         reg r_mreq_n;
  132.  
  133.  
  134.         reg pending_cpu_req;
  135.  
  136.         reg cpu_rnw_r;
  137.  
  138.  
  139.  
  140.         // this is for 7/3.5mhz  
  141.         wire ramreq;
  142.         wire ramwr,ramrd;
  143.         wire cpureq_357;
  144.         reg ramrd_reg,ramwr_reg;
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.         // make paging
  152.         assign win[1:0] = za[15:14];
  153.  
  154.         always @*
  155.         case( win )
  156.                 2'b00: begin
  157.                         page    = win0_page;
  158.                         romnram = win0_romnram;
  159.                 end
  160.  
  161.                 2'b01: begin
  162.                         page    = win1_page;
  163.                         romnram = win1_romnram;
  164.                 end
  165.  
  166.                 2'b10: begin
  167.                         page    = win2_page;
  168.                         romnram = win2_romnram;
  169.                 end
  170.  
  171.                 2'b11: begin
  172.                         page    = win3_page;
  173.                         romnram = win3_romnram;
  174.                 end
  175.         endcase
  176.  
  177.  
  178.         // rom paging - only half a megabyte addressing.
  179.         always @*
  180.         begin
  181.                 rompg[4:0] = page[4:0];
  182.         end
  183.  
  184.  
  185.  
  186.  
  187.         assign romwe_n = wr_n | mreq_n | (~romrw_en);
  188.         assign romoe_n = rd_n | mreq_n;
  189.  
  190.         assign csrom = romnram; // positive polarity!
  191.  
  192.  
  193.  
  194.         // 7/3.5mhz support
  195.  
  196.         assign ramreq = (~mreq_n) && (~romnram) && rfsh_n;
  197.         assign ramrd = ramreq & (~rd_n);
  198.         assign ramwr = ramreq & (~wr_n);
  199.  
  200.         always @(posedge fclk)
  201.         if( cend && (!cpu_stall) )
  202.         begin
  203.                 ramrd_reg <= ramrd;
  204.                 ramwr_reg <= ramwr;
  205.         end
  206.  
  207.         assign cpureq_357 = ( ramrd & (~ramrd_reg) ) | ( ramwr & (~ramwr_reg) );
  208.        
  209.  
  210.  
  211.  
  212.         assign zd_ena = (~mreq_n) & (~rd_n) & (~romnram);
  213.  
  214.  
  215.  
  216.         assign cache_hit = ( (za[15:1] == cached_addr[15:1]) && cached_addr_valid );
  217.  
  218.  
  219.  
  220.         // strobe the beginnings of DRAM cycles
  221.  
  222.         always @(posedge fclk)
  223.         if( zneg )
  224.                 r_mreq_n <= mreq_n | (~rfsh_n);
  225.         //
  226.         assign dram_beg = ( (!cache_hit) || memwr ) && zneg && r_mreq_n && (!romnram) && (!mreq_n) && rfsh_n;
  227.  
  228.         // access type
  229.         assign opfetch = (~mreq_n) && (~m1_n);
  230.         assign memrd   = (~mreq_n) && (~rd_n);
  231.         assign memwr   = (~mreq_n) &&   rd_n && rfsh_n;
  232.  
  233.  
  234.         // wait tables:
  235.         //
  236.         // M1 opcode fetch, dram_beg coincides with:
  237.         // cend:      +3
  238.         // pre_cend:  +4
  239.         // post_cbeg: +5
  240.         // cbeg:      +6
  241.         //
  242.         // memory read, dram_beg coincides with:
  243.         // cend:      +2
  244.         // pre_cend:  +3
  245.         // post_cbeg: +4
  246.         // cbeg:      +5
  247.         //
  248.         // memory write: no wait
  249.         //
  250.         // special case: if dram_beg pulses 1 when cpu_next is 0,
  251.         // unconditional wait has to be performed until cpu_next is 1, and
  252.         // then wait as if dram_beg would coincide with cbeg
  253.  
  254.         assign stall14_ini = dram_beg && ( (!cpu_next) || opfetch || memrd ); // no wait at all in write cycles, if next dram cycle is available
  255.  
  256.  
  257.         // memrd, opfetch - wait till cend & cpu_next,
  258.         // memwr - wait till cpu_next
  259.         assign stall14_cyc = memwr ? (!cpu_next) : stall14_cycrd;
  260.         //
  261.         always @(posedge fclk, negedge rst_n)
  262.         if( !rst_n )
  263.                 stall14_cycrd <= 1'b0;
  264.         else // posedge fclk
  265.         begin
  266.                 if( cpu_next && cend )
  267.                         stall14_cycrd <= 1'b0;
  268.                 else if( dram_beg && ( (!cend) || (!cpu_next) ) && (opfetch || memrd) )
  269.                         stall14_cycrd <= 1'b1;
  270.         end
  271.         //
  272.         always @(posedge fclk, negedge rst_n)
  273.         if( !rst_n )
  274.                 stall14_fin <= 1'b0;
  275.         else // posedge fclk
  276.         begin
  277.                 if( stall14_fin && ( (opfetch&pre_cend) || (memrd&post_cbeg) ) )
  278.                         stall14_fin <= 1'b0;
  279.                 else if( cpu_next && cend && cpu_req && (opfetch || memrd) )
  280.                         stall14_fin <= 1'b1;
  281.         end
  282.  
  283.  
  284.         //
  285.         assign cpu_stall = int_turbo[1] ? (stall14_ini | stall14_cyc | stall14_fin) : (cpureq_357 && (!cpu_next));
  286.  
  287.         // cpu request
  288.         assign cpu_req = int_turbo[1] ? (pending_cpu_req | dram_beg) : cpureq_357;
  289.         //
  290.         assign cpu_rnw = int_turbo[1] ? (dram_beg ? (!memwr) : cpu_rnw_r) : ramrd;
  291.         //
  292.         //
  293.         always @(posedge fclk, negedge rst_n)
  294.         if( !rst_n )
  295.                 pending_cpu_req <= 1'b0;
  296.         else if( cpu_next && cend )
  297.                 pending_cpu_req <= 1'b0;
  298.         else if( dram_beg )
  299.                 pending_cpu_req <= 1'b1;
  300.         //
  301.         always @(posedge fclk)
  302.         if( dram_beg )
  303.                 cpu_rnw_r <= !memwr;
  304.  
  305.  
  306.  
  307.         // address, data in and data out
  308.         //
  309.         assign cpu_wrbsel = za[0];
  310.         assign cpu_addr[20:0] = { page[7:0], za[13:1] };
  311.         assign cpu_wrdata = zd_in;
  312.         //
  313.         always @* if( cpu_strobe ) // WARNING! ACHTUNG! LATCH!!!
  314.                 rd_buf <= cpu_rddata;
  315.         //
  316.         assign zd_out = cpu_wrbsel ? rd_buf[7:0] : rd_buf[15:8];
  317.  
  318.  
  319.  
  320.  
  321.  
  322.         wire io;
  323.         reg  io_r;
  324.         //
  325.         assign io = (~iorq_n);
  326.         //
  327.         always @(posedge fclk)
  328.         if( zpos )
  329.                 io_r <= io;
  330.         //
  331.         always @(posedge fclk, negedge rst_n)
  332.         if( !rst_n )
  333.         begin
  334.                 cached_addr_valid <= 1'b0;
  335.         end
  336.         else
  337.         begin
  338.                 if( (zneg && r_mreq_n && (!mreq_n) && rfsh_n && romnram) ||
  339.                     (zneg && r_mreq_n && memwr                         ) ||
  340.                     (io && (!io_r) && zpos                             ) )
  341.                         cached_addr_valid <= 1'b0;
  342.                 else if( cpu_strobe )
  343.                         cached_addr_valid <= 1'b1;
  344.         end
  345.         //
  346.         always @(posedge fclk)
  347.         if( !rst_n )
  348.         begin
  349.                 cached_addr <= 15'd0;
  350.         end
  351.         else if( cpu_strobe )
  352.         begin
  353.                 cached_addr[15:1] <= za[15:1];
  354.         end
  355.  
  356.  
  357.  
  358.  
  359. endmodule
  360.  
  361.