Subversion Repositories pentevo

Rev

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