Subversion Repositories pentevo

Rev

Rev 582 | Rev 669 | 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. // most of pentevo ports are here
  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. `include "../include/tune.v"
  25.  
  26. module zports(
  27.  
  28.         input  wire        zclk,   // z80 clock
  29.         input  wire        fclk,  // global FPGA clock
  30.         input  wire        rst_n, // system reset
  31.  
  32.         input  wire        zpos,
  33.         input  wire        zneg,
  34.  
  35.  
  36.         input  wire [ 7:0] din,
  37.         output reg  [ 7:0] dout,
  38.         output wire        dataout,
  39.         input  wire [15:0] a,
  40.  
  41.         input  wire        iorq_n,
  42.         input  wire        mreq_n,
  43.         input  wire        m1_n,
  44.         input  wire        rd_n,
  45.         input  wire        wr_n,
  46.  
  47.         output reg         porthit, // when internal port hit occurs, this is 1, else 0; used for iorq1_n iorq2_n on zxbus
  48.         output reg         external_port, // asserts for AY and VG93 accesses
  49.  
  50.         output wire [15:0] ideout,
  51.         input  wire [15:0] idein,
  52.         output wire        idedataout, // IDE must IN data from IDE device when idedataout=0, else it OUTs
  53.         output wire [ 2:0] ide_a,
  54.         output wire        ide_cs0_n,
  55.         output wire        ide_cs1_n,
  56.         output wire        ide_rd_n,
  57.         output wire        ide_wr_n,
  58.  
  59.  
  60.         input  wire [ 4:0] keys_in, // keys (port FE)
  61.         input  wire [ 7:0] mus_in,  // mouse (xxDF)
  62.         input  wire [ 4:0] kj_in,
  63.  
  64.         output reg  [ 3:0] border,
  65.  
  66.  
  67.         input  wire        dos,
  68.  
  69.  
  70.         output wire        ay_bdir,
  71.         output wire        ay_bc1,
  72.  
  73.         output wire [ 7:0] p7ffd,
  74.         output wire [ 7:0] peff7,
  75.  
  76.         input  wire        tape_read,
  77.  
  78.         output wire        vg_cs_n,
  79.         input  wire        vg_intrq,
  80.         input  wire        vg_drq, // from vg93 module - drq + irq read
  81.         output wire        vg_wrFF,        // write strobe of #FF port
  82.  
  83.         output wire        sd_cs_n_val,
  84.         output wire        sd_cs_n_stb,
  85.         output wire        sd_start,
  86.         output wire [ 7:0] sd_datain,
  87.         input  wire [ 7:0] sd_dataout,
  88.  
  89.         // WAIT-ports related
  90.         //
  91.         output reg  [ 7:0] gluclock_addr,
  92.         //
  93.         output reg  [ 2:0] comport_addr,
  94.         //
  95.         output wire        wait_start_gluclock, // begin wait from some ports
  96.         output wire        wait_start_comport,  //
  97.         //
  98.         output reg         wait_rnw,   // whether it was read(=1) or write(=0)
  99.         output reg  [ 7:0] wait_write,
  100.         input  wire [ 7:0] wait_read,
  101.  
  102.  
  103.         output wire        atmF7_wr_fclk, // used in atm_pager.v
  104.  
  105.  
  106.         output reg  [ 2:0] atm_scr_mode, // RG0..RG2 in docs
  107.         output reg         atm_turbo,    // turbo mode ON
  108.         output reg         atm_pen,      // pager_off in atm_pager.v, NOT inverted!!!
  109.         output reg         atm_cpm_n,    // permanent dos on
  110.         output reg         atm_pen2,     // PEN2 - fucking palette mode, NOT inverted!!!
  111.  
  112.         output wire        romrw_en, // from port BF
  113.  
  114.  
  115.         output wire        pent1m_ram0_0, // d3.eff7
  116.         output wire        pent1m_1m_on,  // d2.eff7
  117.         output wire [ 5:0] pent1m_page,   // full 1 meg page number
  118.         output wire        pent1m_ROM,     // d4.7ffd
  119.  
  120.  
  121.         output wire        atm_palwr,   // palette write strobe
  122.         output wire [ 5:0] atm_paldata, // palette write data
  123.  
  124.         output wire        covox_wr,
  125.         output wire        beeper_wr,
  126.  
  127.         output wire        clr_nmi,
  128.  
  129.         output wire        fnt_wr,              // write to font_ram enabled
  130.  
  131.         // inputs from atm_pagers, to read back its config
  132.         input  wire [63:0] pages,
  133.         input  wire [ 7:0] ramnroms,
  134.         input  wire [ 7:0] dos7ffds,
  135.  
  136.         input  wire [ 5:0] palcolor,
  137.         input  wire [ 7:0] fontrom_readback,
  138.  
  139.         // ulaplus
  140.         output reg         up_ena,
  141.         output reg  [ 5:0] up_paladdr,
  142.         output wire [ 7:0] up_paldata,
  143.         output wire        up_palwr,
  144.  
  145.  
  146.  
  147.         // NMI generation
  148.         output reg         set_nmi,
  149.  
  150.         // break enable & address
  151.         output reg         brk_ena,
  152.         output reg  [15:0] brk_addr
  153. );
  154.  
  155.  
  156. `define IS_NIDE_REGS(x) ( (x[2:0]==3'b000) && (x[3]!=x[4]) )
  157. `define IS_NIDE_HIGH(x) ( x[7:0]==8'h11 )
  158. `define IS_PORT_NIDE(x) ( `IS_NIDE_REGS(x) || `IS_NIDE_HIGH(x) )
  159. `define NIDE_REGS 8'h10,8'h30,8'h50,8'h70,8'h90,8'hB0,8'hD0,8'hF0, \
  160.                   8'h08,8'h28,8'h48,8'h68,8'h88,8'hA8,8'hC8,8'hE8
  161.  
  162.         localparam PORTFE = 8'hFE;
  163.         localparam PORTF6 = 8'hF6;
  164.         localparam PORTF7 = 8'hF7;
  165.  
  166.         localparam NIDE10 = 8'h10;
  167.         localparam NIDE11 = 8'h11;
  168.         localparam NIDE30 = 8'h30;
  169.         localparam NIDE50 = 8'h50;
  170.         localparam NIDE70 = 8'h70;
  171.         localparam NIDE90 = 8'h90;
  172.         localparam NIDEB0 = 8'hB0;
  173.         localparam NIDED0 = 8'hD0;
  174.         localparam NIDEF0 = 8'hF0;
  175.         localparam NIDEC8 = 8'hC8;
  176.  
  177.         localparam PORTFD = 8'hFD;
  178.  
  179.         localparam VGCOM  = 8'h1F;
  180.         localparam VGTRK  = 8'h3F;
  181.         localparam VGSEC  = 8'h5F;
  182.         localparam VGDAT  = 8'h7F;
  183.         localparam VGSYS  = 8'hFF;
  184.  
  185.         localparam SAVPORT1 = 8'h2F;
  186.         localparam SAVPORT2 = 8'h4F;
  187.         localparam SAVPORT3 = 8'h6F;
  188.         localparam SAVPORT4 = 8'h8F;
  189.  
  190.         localparam KJOY   = 8'h1F;
  191.         localparam KMOUSE = 8'hDF;
  192.  
  193.         localparam SDCFG  = 8'h77;
  194.         localparam SDDAT  = 8'h57;
  195.  
  196.         localparam ATMF7  = 8'hF7;
  197.         localparam ATM77  = 8'h77;
  198.  
  199.         localparam ZXEVBE = 8'hBE; // xxBE config-read and nmi-end port
  200.         localparam ZXEVBF = 8'hBF; // xxBF config port
  201.         localparam ZXEVBRK = 8'hBD; // xxBD breakpoint address port    
  202.  
  203.         localparam COMPORT = 8'hEF; // F8EF..FFEF - rs232 ports
  204.  
  205.  
  206.         localparam COVOX   = 8'hFB;
  207.  
  208.        
  209.         localparam ULAPLUS = 8'h3B;
  210.  
  211.  
  212.  
  213.         reg port_wr;
  214.         reg port_rd;
  215.  
  216.         reg iowr_reg;
  217.         reg iord_reg;
  218.  
  219.  
  220.         reg port_wr_fclk,
  221.             port_rd_fclk,
  222.             mem_wr_fclk;
  223.  
  224.         reg [1:0] iowr_reg_fclk,
  225.                   iord_reg_fclk;
  226.  
  227.         reg [1:0] memwr_reg_fclk;
  228.  
  229.  
  230.         wire [7:0] loa;
  231.  
  232.         wire portfe_wr;
  233.  
  234.  
  235.  
  236.         wire ideout_hi_wr;
  237.         wire idein_lo_rd;
  238.         reg [7:0] idehiin; // IDE high part read register: low part is read directly to Z80 bus,
  239.                            // while high part is remembered here
  240.         reg ide_ports; // ide ports selected
  241.  
  242.         reg ide_rd_trig; // nemo-divide read trigger
  243.         reg ide_rd_latch; // to save state of trigger during read cycle
  244.  
  245.         reg ide_wrlo_trig,  ide_wrhi_trig;  // nemo-divide write triggers
  246.         reg ide_wrlo_latch, ide_wrhi_latch; // save state during write cycles
  247.  
  248.  
  249.  
  250.         reg  [15:0] idewrreg; // write register, either low or high part is pre-written here,
  251.                               // while other part is out directly from Z80 bus
  252.  
  253.         wire [ 7:0] iderdeven; // to control read data from "even" ide ports (all except #11)
  254.         wire [ 7:0] iderdodd;  // read data from "odd" port (#11)
  255.  
  256.  
  257.  
  258.         reg pre_bc1,pre_bdir;
  259.  
  260.         wire gluclock_on;
  261.  
  262.  
  263.  
  264.         reg  shadow_en_reg; //bit0.xxBF
  265.         reg   romrw_en_reg; //bit1.xxBF
  266.         reg  fntw_en_reg;       //bit2.xxBF
  267.  
  268.         wire shadow;
  269.  
  270.  
  271.  
  272.         reg [7:0] portbemux;
  273.  
  274.  
  275.  
  276.         reg [7:0] savport [3:0];
  277.  
  278.  
  279.  
  280.  
  281.  
  282.         assign shadow = dos || shadow_en_reg;
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.         assign loa=a[7:0];
  290.  
  291.         always @*
  292.         begin
  293.                 if( (loa==PORTFE) || (loa==PORTF6) ||
  294.                     (loa==PORTFD) ||
  295.  
  296.                     `IS_PORT_NIDE(loa) ||
  297. //                  (loa==NIDE10) || (loa==NIDE11) || (loa==NIDE30) || (loa==NIDE50) || (loa==NIDE70) ||
  298. //                  (loa==NIDE90) || (loa==NIDEB0) || (loa==NIDED0) || (loa==NIDEF0) || (loa==NIDEC8) ||
  299.  
  300.                     (loa==KMOUSE) ||
  301.  
  302.                     ( (loa==VGCOM)&&shadow ) || ( (loa==VGTRK)&&shadow ) || ( (loa==VGSEC)&&shadow ) || ( (loa==VGDAT)&&shadow ) ||
  303.                     ( (loa==VGSYS)&&shadow ) || ( (loa==KJOY)&&(!shadow) ) ||
  304.  
  305.                     ( (loa==SAVPORT1)&&shadow ) || ( (loa==SAVPORT2)&&shadow ) ||
  306.                     ( (loa==SAVPORT3)&&shadow ) || ( (loa==SAVPORT4)&&shadow ) ||
  307.  
  308.  
  309.                     ( (loa==PORTF7)&&(!shadow) ) || ( (loa==SDCFG)&&(!shadow) ) || ( (loa==SDDAT) ) ||
  310.  
  311.                     ( (loa==ATMF7)&&shadow ) || ( (loa==ATM77)&&shadow ) ||
  312.  
  313.                     ( loa==ZXEVBF ) || ( loa==ZXEVBE) || ( loa==ZXEVBRK) || ( loa==COMPORT ) ||
  314.  
  315.                     ( loa==ULAPLUS)
  316.                   )
  317.  
  318.  
  319.  
  320.                         porthit = 1'b1;
  321.                 else
  322.                         porthit = 1'b0;
  323.         end
  324.  
  325.         always @*
  326.         begin
  327.                 if( ((loa==PORTFD) && a[15]) || // 0xBFFD/0xFFFD ports
  328.                     (( (loa==VGCOM)&&shadow ) || ( (loa==VGTRK)&&shadow ) || ( (loa==VGSEC)&&shadow ) || ( (loa==VGDAT)&&shadow )) ) // vg93 ports
  329.                         external_port = 1'b1;
  330.                 else
  331.                         external_port = 1'b0;
  332.         end
  333.  
  334.         assign dataout = porthit & (~iorq_n) & (~rd_n) & (~external_port);
  335.  
  336.  
  337.  
  338.         // this is zclk-synchronous strobes
  339.         always @(posedge zclk)
  340.         begin
  341.                 iowr_reg <= ~(iorq_n | wr_n);
  342.                 iord_reg <= ~(iorq_n | rd_n);
  343.  
  344.                 if( (!iowr_reg) && (!iorq_n) && (!wr_n) )
  345.                         port_wr <= 1'b1;
  346.                 else
  347.                         port_wr <= 1'b0;
  348.  
  349.  
  350.                 if( (!iord_reg) && (!iorq_n) && (!rd_n) )
  351.                         port_rd <= 1'b1;
  352.                 else
  353.                         port_rd <= 1'b0;
  354.         end
  355.  
  356.  
  357.  
  358.  
  359.         // fclk-synchronous stobes
  360.         //
  361.         always @(posedge fclk) if( zpos )
  362.         begin
  363.                 iowr_reg_fclk[0] <= ~(iorq_n | wr_n);
  364.                 iord_reg_fclk[0] <= ~(iorq_n | rd_n);
  365.         end
  366.  
  367.         always @(posedge fclk)
  368.         begin
  369.                 iowr_reg_fclk[1] <= iowr_reg_fclk[0];
  370.                 iord_reg_fclk[1] <= iord_reg_fclk[0];
  371.         end
  372.  
  373.         always @(posedge fclk)
  374.         begin
  375.                 port_wr_fclk <= iowr_reg_fclk[0] && (!iowr_reg_fclk[1]);
  376.                 port_rd_fclk <= iord_reg_fclk[0] && (!iord_reg_fclk[1]);
  377.         end
  378.  
  379.         always @(posedge fclk)
  380.                 memwr_reg_fclk[1:0] <= { memwr_reg_fclk[0], ~(mreq_n | wr_n) };
  381.  
  382.         always @(posedge fclk)
  383.                 mem_wr_fclk <= memwr_reg_fclk[0] && (!memwr_reg_fclk[1]);
  384.  
  385.  
  386.  
  387.         // dout data
  388.         always @*
  389.         begin
  390.                 case( loa )
  391.                 PORTFE:
  392.                         dout = { 1'b1, tape_read, 1'b0, keys_in };
  393.                 PORTF6:
  394.                         dout = { 1'b1, tape_read, 1'b0, keys_in };
  395.  
  396.  
  397.                 `NIDE_REGS:
  398.                         dout = iderdeven;
  399.                 NIDE11:
  400.                         dout = iderdodd;
  401.  
  402.  
  403.                 //PORTFD:
  404.  
  405.                 VGSYS:
  406.                         dout = { vg_intrq, vg_drq, 6'b111111 };
  407.  
  408.                 SAVPORT1, SAVPORT2, SAVPORT3, SAVPORT4:
  409.                         dout = savport[ loa[6:5] ];
  410.  
  411.  
  412.                 KJOY:
  413.                         dout = {3'b000, kj_in};
  414.                 KMOUSE:
  415.                         dout = mus_in;
  416.  
  417.                 SDCFG:
  418.                         dout = 8'h00; // always SD inserted, SD is in R/W mode
  419.                 SDDAT:
  420.                         dout = sd_dataout;
  421.  
  422.  
  423.                 PORTF7: begin
  424.                         if( !a[14] && (a[8]^shadow) && gluclock_on ) // $BFF7 - data i/o
  425.                                 dout = wait_read;
  426.                         else // any other $xxF7 port
  427.                                 dout = 8'hFF;
  428.                 end
  429.  
  430.                 COMPORT: begin
  431.                         dout = wait_read; // $F8EF..$FFEF
  432.                 end
  433.  
  434.                 ZXEVBF: begin
  435.                         dout = { 3'b000, brk_ena, set_nmi, fntw_en_reg, romrw_en_reg, shadow_en_reg };
  436.                 end
  437.  
  438.                 ZXEVBE: begin
  439.                         dout = portbemux;
  440.                 end
  441.  
  442.  
  443.                 default:
  444.                         dout = 8'hFF;
  445.                 endcase
  446.         end
  447.  
  448.  
  449.  
  450.         assign portfe_wr    = (((loa==PORTFE) || (loa==PORTF6)) && port_wr);
  451.         assign portfd_wr    = ( (loa==PORTFD) && port_wr);
  452.  
  453.         // F7 ports (like EFF7) are accessible in shadow mode but at addresses like EEF7, DEF7, BEF7 so that
  454.         // there are no conflicts in shadow mode with ATM xFF7 and x7F7 ports
  455.         assign portf7_wr    = ( (loa==PORTF7) && (a[8]==1'b1) && port_wr && (!shadow) ) ||
  456.                               ( (loa==PORTF7) && (a[8]==1'b0) && port_wr &&   shadow  ) ;
  457.  
  458.         assign portf7_rd    = ( (loa==PORTF7) && (a[8]==1'b1) && port_rd && (!shadow) ) ||
  459.                               ( (loa==PORTF7) && (a[8]==1'b0) && port_rd &&   shadow  ) ;
  460.  
  461.         assign vg_wrFF = ( ( (loa==VGSYS)&&shadow ) && port_wr);
  462.  
  463.         assign comport_wr   = ( (loa==COMPORT) && port_wr);
  464.         assign comport_rd   = ( (loa==COMPORT) && port_rd);
  465.  
  466.        
  467.         assign zxevbrk_wr_fclk = ( (loa==ZXEVBRK) && port_wr_fclk);
  468.  
  469.  
  470.  
  471.  
  472.  
  473.         // break address write
  474.         always @(posedge fclk)
  475.         if( zxevbrk_wr_fclk)
  476.         begin
  477.                 if( !a[8] )
  478.                         brk_addr[ 7:0] <= din;
  479.                 else // a[8]==1
  480.                         brk_addr[15:8] <= din;
  481.         end
  482.  
  483.  
  484.  
  485.  
  486.  
  487.         //border port FE
  488.         wire portwe_wr_fclk;
  489.  
  490.         assign portfe_wr_fclk = (((loa==PORTFE) || (loa==PORTF6)) && port_wr_fclk);
  491.  
  492.         always @(posedge fclk)
  493.         if( portfe_wr_fclk )
  494.                 border <= { ~a[3], din[2:0] };
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.         // IDE ports
  502.  
  503.         // IDE physical ports (that go to IDE device)
  504.         always @(loa)
  505.         if( `IS_NIDE_REGS(loa) )
  506.                 ide_ports = 1'b1;
  507.         else
  508.                 ide_ports = 1'b0;
  509.  
  510.  
  511.         assign idein_lo_rd  = port_rd && (loa==NIDE10) && (!ide_rd_trig);
  512.  
  513.         // control read & write triggers, which allow nemo-divide mod to work.
  514.         //
  515.         // read trigger:
  516.         always @(posedge zclk)
  517.         begin
  518.                 if( (loa==NIDE10) && port_rd && !ide_rd_trig )
  519.                         ide_rd_trig <= 1'b1;
  520.                 else if( ( ide_ports || (loa==NIDE11) ) && ( port_rd || port_wr ) )
  521.                         ide_rd_trig <= 1'b0;
  522.         end
  523.         //
  524.         // two triggers for write sequence...
  525.         always @(posedge zclk)
  526.         if( ( ide_ports || (loa==NIDE11) ) && ( port_rd || port_wr ) )
  527.         begin
  528.                 if( (loa==NIDE11) && port_wr )
  529.                         ide_wrhi_trig <= 1'b1;
  530.                 else
  531.                         ide_wrhi_trig <= 1'b0;
  532.                 //
  533.                 if( (loa==NIDE10) && port_wr && !ide_wrhi_trig && !ide_wrlo_trig )
  534.                         ide_wrlo_trig <= 1'b1;
  535.                 else
  536.                         ide_wrlo_trig <= 1'b0;
  537.         end
  538.  
  539.         // normal read: #10(low), #11(high)
  540.         // divide read: #10(low), #10(high)
  541.         //
  542.         // normal write: #11(high), #10(low)
  543.         // divide write: #10(low),  #10(high)
  544.  
  545.  
  546.         always @(posedge zclk)
  547.         begin
  548.                 if( port_wr && (loa==NIDE11) )
  549.                         idewrreg[15:8] <= din;
  550.  
  551.                 if( port_wr && (loa==NIDE10) && !ide_wrlo_trig )
  552.                         idewrreg[ 7:0] <= din;
  553.         end
  554.  
  555.  
  556.  
  557.  
  558.         always @(posedge zclk)
  559.         if( idein_lo_rd )
  560.                         idehiin <= idein[15:8];
  561.  
  562.  
  563.         assign ide_a = a[7:5];
  564.  
  565.  
  566.         // This is unknown shit... Probably need more testing with old WD
  567.         // drives WITHOUT this commented fix.
  568.         //
  569.         // trying to fix old WD drives...
  570.         //assign ide_cs0_n = iorq_n | (rd_n&wr_n) | (~ide_ports) | (~(loa!=NIDEC8));
  571.         //assign ide_cs1_n = iorq_n | (rd_n&wr_n) | (~ide_ports) | (~(loa==NIDEC8));
  572.         // fix ends...
  573.  
  574.  
  575.         assign ide_cs0_n = (~ide_ports) | (~(loa!=NIDEC8));
  576.         assign ide_cs1_n = (~ide_ports) | (~(loa==NIDEC8));
  577.  
  578.  
  579.         // generate read cycles for IDE as usual, except for reading #10
  580.         // instead of #11 for high byte (nemo-divide). I use additional latch
  581.         // since 'ide_rd_trig' clears during second Z80 IO read cycle to #10
  582.         always @* if( rd_n ) ide_rd_latch <= ide_rd_trig;
  583.         //
  584.         assign ide_rd_n = iorq_n | rd_n | (~ide_ports) | (ide_rd_latch && (loa==NIDE10));
  585.  
  586.         always @* if( wr_n ) ide_wrlo_latch <= ide_wrlo_trig; // same for write triggers
  587.         always @* if( wr_n ) ide_wrhi_latch <= ide_wrhi_trig; //
  588.         //
  589.         assign ide_wr_n = iorq_n | wr_n | (~ide_ports) | ( (loa==NIDE10) && !ide_wrlo_latch && !ide_wrhi_latch );
  590.                                                   // do NOT generate IDE write, if neither of ide_wrhi|lo latches
  591.                                                   // set and writing to NIDE10
  592.  
  593.  
  594.  
  595. //      assign idedataout = ide_rd_n;
  596.         assign idedataout = ~ide_wr_n; // shit-fix in try to fix IDE errors
  597.         // warning: this fix kinda blind-picking, good way is to
  598.         // have idedataout lead wr or rd strobes. also good point to disable data ringing
  599.         // on ide data bus while not accessing IDE
  600.  
  601.  
  602.         // data read by Z80 from IDE
  603.         //
  604.         assign iderdodd[ 7:0] = idehiin[ 7:0];
  605.         //
  606.         assign iderdeven[ 7:0] = (ide_rd_latch && (loa==NIDE10)) ? idehiin[ 7:0] : idein[ 7:0];
  607.  
  608.         // data written to IDE from Z80
  609.         //
  610.         assign ideout[15:8] = ide_wrhi_latch ? idewrreg[15:8] : din[ 7:0];
  611.         assign ideout[ 7:0] = ide_wrlo_latch ? idewrreg[ 7:0] : din[ 7:0];
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.         // AY control
  620.         always @*
  621.         begin
  622.                 pre_bc1 = 1'b0;
  623.                 pre_bdir = 1'b0;
  624.  
  625.                 if( loa==PORTFD )
  626.                 begin
  627.                         if( a[15:14]==2'b11 )
  628.                         begin
  629.                                 pre_bc1=1'b1;
  630.                                 pre_bdir=1'b1;
  631.                         end
  632.                         else if( a[15:14]==2'b10 )
  633.                         begin
  634.                                 pre_bc1=1'b0;
  635.                                 pre_bdir=1'b1;
  636.                         end
  637.                 end
  638.         end
  639.  
  640.         assign ay_bc1  = pre_bc1  & (~iorq_n) & ((~rd_n)|(~wr_n));
  641.         assign ay_bdir = pre_bdir & (~iorq_n) & (~wr_n);
  642.  
  643.  
  644.  
  645.         // 7FFD port
  646.         reg [7:0] p7ffd_int,peff7_int;
  647.         reg p7ffd_rom_int;
  648.         wire block7ffd;
  649.         wire block1m;
  650.  
  651.         always @(posedge zclk, negedge rst_n)
  652.         begin
  653.                 if( !rst_n )
  654.                         p7ffd_int <= 7'h00;
  655.                 else if( (a[15]==1'b0) && portfd_wr && (!block7ffd) )
  656.                         p7ffd_int <= din; // 2..0 - page, 3 - screen, 4 - rom, 5 - block48k, 6..7 -
  657.         end
  658.  
  659.         always @(posedge zclk, negedge rst_n)
  660.         if( !rst_n )
  661.                         p7ffd_rom_int <= 1'b0;
  662.         else
  663.                 if( (a[15]==1'b0) && portfd_wr && (!block7ffd) )
  664.                         p7ffd_rom_int <= din[4];
  665.  
  666.  
  667.         assign block7ffd=p7ffd_int[5] & block1m;
  668.  
  669.  
  670.         // EFF7 port
  671.         always @(posedge zclk, negedge rst_n)
  672.         begin
  673.                 if( !rst_n )
  674.                         peff7_int <= 8'h00;
  675.                 else if( !a[12] && portf7_wr && (!shadow) ) // EEF7 in shadow mode is abandoned!
  676.                         peff7_int <= din; // 4 - turbooff, 0 - p16c on, 2 - block1meg
  677.         end
  678.         assign block1m = peff7_int[2];
  679.  
  680.         assign p7ffd = { (block1m ? 3'b0 : p7ffd_int[7:5]),p7ffd_rom_int,p7ffd_int[3:0]};
  681.  
  682.         assign peff7 = block1m ? { peff7_int[7], 1'b0, peff7_int[5], peff7_int[4], 3'b000, peff7_int[0] } : peff7_int;
  683.  
  684.  
  685.         assign pent1m_ROM       = p7ffd_int[4];
  686.         assign pent1m_page[5:0] = { p7ffd_int[7:5], p7ffd_int[2:0] };
  687.         assign pent1m_1m_on     = ~peff7_int[2];
  688.         assign pent1m_ram0_0    = peff7_int[3];
  689.  
  690.  
  691.  
  692.  
  693.         // gluclock ports (bit7:eff7 is above)
  694.  
  695.         assign gluclock_on = peff7_int[7] || shadow; // in shadow mode EEF7 is abandoned: instead, gluclock access
  696.                                                      // is ON forever in shadow mode.
  697.  
  698.         always @(posedge zclk)
  699.         begin
  700.                 if( gluclock_on && portf7_wr ) // gluclocks on
  701.                 begin
  702.                         if( !a[13] ) // $DFF7 - addr reg
  703.                                 gluclock_addr <= din;
  704.  
  705.                         // write to waiting register is not here - in separate section managing wait_write
  706.                 end
  707.         end
  708.  
  709.  
  710.         // comports
  711.  
  712.         always @(posedge zclk)
  713.         begin
  714.                 if( comport_wr || comport_rd )
  715.                         comport_addr <= a[10:8 ];
  716.         end
  717.  
  718.  
  719.  
  720.         // write to wait registers
  721.         always @(posedge zclk)
  722.         begin
  723.                 // gluclocks
  724.                 if( gluclock_on && portf7_wr && !a[14] ) // $BFF7 - data reg
  725.                         wait_write <= din;
  726.                 // com ports
  727.                 else if( comport_wr ) // $F8EF..$FFEF - comports
  728.                         wait_write <= din;
  729.         end
  730.  
  731.         // wait from wait registers
  732.         //
  733.         // ACHTUNG!!!! here portxx_wr are ON Z80 CLOCK! logic must change when moving to fclk strobes
  734.         //
  735.         assign wait_start_gluclock = ( gluclock_on && !a[14] && (portf7_rd || portf7_wr) ); // $BFF7 - gluclock r/w
  736.         //
  737.         assign wait_start_comport = ( comport_rd || comport_wr );
  738.         //
  739.         //
  740.         always @(posedge zclk) // wait rnw - only meanful during wait
  741.         begin
  742.                 if( port_wr )
  743.                         wait_rnw <= 1'b0;
  744.  
  745.                 if( port_rd )
  746.                         wait_rnw <= 1'b1;
  747.         end
  748.  
  749.  
  750.  
  751.  
  752.  
  753.         // VG93 control
  754.         assign vg_cs_n =  (~shadow) | iorq_n | (rd_n & wr_n) | ( ~((loa==VGCOM)|(loa==VGTRK)|(loa==VGSEC)|(loa==VGDAT)) );
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763. // SD card (z-controlâ••r compatible)
  764.  
  765.         wire sdcfg_wr,sddat_wr,sddat_rd;
  766.  
  767.         assign sdcfg_wr = ( (loa==SDCFG) && port_wr_fclk && (!shadow) )                  ||
  768.                           ( (loa==SDDAT) && port_wr_fclk &&   shadow  && (a[15]==1'b1) ) ;
  769.  
  770.         assign sddat_wr = ( (loa==SDDAT) && port_wr_fclk && (!shadow) )                  ||
  771.                           ( (loa==SDDAT) && port_wr_fclk &&   shadow  && (a[15]==1'b0) ) ;
  772.  
  773.         assign sddat_rd = ( (loa==SDDAT) && port_rd_fclk              );
  774.  
  775.         // SDCFG write - sdcs_n control
  776.         assign sd_cs_n_stb = sdcfg_wr;
  777.         assign sd_cs_n_val = din[1];
  778.  
  779.  
  780.         // start signal for SPI module with resyncing to fclk
  781.  
  782.         assign sd_start = sddat_wr || sddat_rd;
  783.  
  784.         // data for SPI module
  785.         assign sd_datain = sddat_rd ? 8'hFF : din;
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793. /////////////////////////////////////////////////////////////////////////////////////////////////
  794.  
  795.         ///////////////
  796.         // ATM ports //
  797.         ///////////////
  798.  
  799.         wire atm77_wr_fclk;
  800.         wire zxevbf_wr_fclk;
  801.  
  802.         assign atmF7_wr_fclk = ( (loa==ATMF7) && (a[8]==1'b1) && shadow && port_wr_fclk ); // xFF7 and x7F7 ports, NOT xEF7!
  803.         assign atm77_wr_fclk = ( (loa==ATM77) && shadow && port_wr_fclk );
  804.  
  805.         assign zxevbf_wr_fclk = ( (loa==ZXEVBF) && port_wr_fclk );
  806.  
  807.  
  808.         // port BF write
  809.         //
  810.         always @(posedge fclk, negedge rst_n)
  811.         if( !rst_n )
  812.         begin
  813.                 shadow_en_reg <= 1'b0;
  814.                 romrw_en_reg  <= 1'b0;
  815.                 fntw_en_reg   <= 1'b0;
  816.                 set_nmi       <= 1'b0;
  817.                 brk_ena       <= 1'b0;
  818.         end
  819.         else if( zxevbf_wr_fclk )
  820.         begin
  821.                 shadow_en_reg <= din[0];
  822.                 romrw_en_reg  <= din[1];
  823.                 fntw_en_reg   <= din[2];
  824.                 set_nmi       <= din[3];
  825.                 brk_ena       <= din[4];
  826.         end
  827.  
  828.         assign romrw_en = romrw_en_reg;
  829.  
  830.  
  831.  
  832.         // port xx77 write
  833.         always @(posedge fclk, negedge rst_n)
  834.         if( !rst_n )
  835.         begin
  836.                 atm_scr_mode = 3'b011;
  837.                 atm_turbo    = 1'b0;
  838.  
  839.                 atm_pen =   1'b1; // no manager,
  840.                 atm_cpm_n = 1'b0; // permanent dosen (shadow ports on)
  841.  
  842.  
  843.                 atm_pen2     = 1'b0;
  844.         end
  845.         else if( atm77_wr_fclk )
  846.         begin
  847.                 atm_scr_mode <= din[2:0];
  848.                 atm_turbo    <= din[3];
  849.                 atm_pen      <= ~a[8];
  850.                 atm_cpm_n    <=  a[9];
  851.                 atm_pen2     <= ~a[14];
  852.         end
  853.  
  854.  
  855.         // atm palette strobe and data
  856.         wire vg_wrFF_fclk;
  857.  
  858.         assign vg_wrFF_fclk = ( ( (loa==VGSYS)&&shadow ) && port_wr_fclk);
  859.  
  860.  
  861.         assign atm_palwr = vg_wrFF_fclk & atm_pen2;
  862.  
  863.         assign atm_paldata = { ~din[4], ~din[7], ~din[1], ~din[6], ~din[0], ~din[5] };
  864.  
  865.  
  866.  
  867.         // port BE write
  868.         assign clr_nmi = ( (loa==ZXEVBE) && port_wr_fclk );
  869.  
  870.  
  871.  
  872.  
  873.         // covox/beeper writes
  874.  
  875.         assign beeper_wr = (loa==PORTFE) && portfe_wr_fclk;
  876.         assign covox_wr  = (loa==COVOX) && port_wr_fclk;
  877.  
  878.  
  879.  
  880.         // font write enable
  881.         assign fnt_wr = fntw_en_reg && mem_wr_fclk;
  882.  
  883.  
  884.  
  885.         // port BE read
  886.  
  887.         always @*
  888.         case( a[12:8] )
  889.  
  890.         5'h0: portbemux = pages[ 7:0 ];
  891.         5'h1: portbemux = pages[15:8 ];
  892.         5'h2: portbemux = pages[23:16];
  893.         5'h3: portbemux = pages[31:24];
  894.         5'h4: portbemux = pages[39:32];
  895.         5'h5: portbemux = pages[47:40];
  896.         5'h6: portbemux = pages[55:48];
  897.         5'h7: portbemux = pages[63:56];
  898.  
  899.         5'h8: portbemux = ramnroms;
  900.         5'h9: portbemux = dos7ffds;
  901.  
  902.         5'hA: portbemux = p7ffd_int;
  903.         5'hB: portbemux = peff7_int;
  904.  
  905.         5'hC: portbemux = { ~atm_pen2, atm_cpm_n, ~atm_pen, dos, atm_turbo, atm_scr_mode };
  906.  
  907.         5'hD: portbemux = { ~palcolor[4], ~palcolor[2], ~palcolor[0], ~palcolor[5], 2'b11, ~palcolor[3], ~palcolor[1] };
  908. //      assign atm_paldata = { ~din[4], ~din[7], ~din[1], ~din[6], ~din[0], ~din[5] };
  909. //  {GgRrBb} -> {grbG11RB}
  910. // was: 76543210 -> 471605
  911. // now:             543210 -> 4205xx31
  912.  
  913.         5'hE: portbemux = fontrom_readback;
  914.  
  915.         5'h10: portbemux = brk_addr[7:0];
  916.         5'h11: portbemux = brk_addr[15:8];
  917.  
  918.         default: portbemux = 8'bXXXXXXXX;
  919.  
  920.         endcase
  921.  
  922.  
  923.  
  924.  
  925.  
  926.         // savelij ports write
  927.         //
  928.         always @(posedge fclk)
  929.         if( port_wr_fclk && shadow )
  930.         begin
  931.                 if( (loa==SAVPORT1) ||
  932.                     (loa==SAVPORT2) ||
  933.                     (loa==SAVPORT3) ||
  934.                     (loa==SAVPORT4) )
  935.                         savport[ loa[6:5] ] <= din;
  936.         end
  937.  
  938.  
  939.  
  940.  
  941.         // ULAPLUS ports
  942.         reg up_select; // 0 -- ena/dis, 1 -- palette write
  943.         //
  944.         wire up_wr = port_wr_fclk && (loa==ULAPLUS);
  945.         //
  946.         always @(posedge fclk)
  947.         if( up_wr && !a[14] )
  948.         begin
  949.                 if( !din[7] &&  din[6] )
  950.                 begin
  951.                         up_select <= 1'b1;
  952.                 end
  953.  
  954.                 if( !din[7] && !din[6] )
  955.                 begin
  956.                         up_select <= 1'b0;
  957.                         up_paladdr[5:0] <= din[5:0];
  958.                 end
  959.         end
  960.         //
  961.         assign up_palwr = up_wr && a[14] && !up_select;
  962.         //
  963.         always @(posedge fclk, negedge rst_n)
  964.         if( !rst_n )
  965.                 up_ena <= 1'b0;
  966.         else if( up_wr && a[14] && up_select )
  967.                 up_ena <= din[0];
  968.         //
  969.         assign up_paldata = {din[4:2],din[7:5],din[1:0]}; // G3R3B2 to R3G3B2
  970.  
  971. endmodule
  972.  
  973.