Subversion Repositories ngs

Rev

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

  1. // part of NeoGS project (c) 2007-2008 NedoPC
  2. //
  3.  
  4. // ports $00-$3f are in FPGA, $40-$ff are in CPLD
  5.  
  6. module ports(
  7.  
  8.         input  wire rst_n,
  9.         input  wire cpu_clock, // Z80 CPU clock (clk_fpga on schematics)
  10.  
  11.  
  12.         input  wire [ 7:0] din,   // NGS z80 cpu DATA BUS inputs
  13.         output reg  [ 7:0] dout,  // NGS z80 cpu DATA BUS outputs
  14.         output reg         busin, // direction of bus: =1 - input, =0 - output
  15.         input  wire [15:0] a,     // NSG z80 cpu ADDRESS BUS
  16.  
  17.         input  wire iorq_n,
  18.         input  wire mreq_n,
  19.         input  wire rd_n,
  20.         input  wire wr_n, // NGS z80 cpu control signals
  21.  
  22.         input  wire [ 7:0] data_port_input, // data_port input from zxbus module (async)
  23.         output reg  [ 7:0] data_port_output, // data_port output to zxbus module (async to zxbus, sync here)
  24.         input  wire [ 7:0] command_port_input, // command_port input from zxbus (async)
  25.  
  26.         input  wire data_bit_input, // data_bit from zxbus module (sync)
  27.         input  wire command_bit_input, // --//-- (sync)
  28.  
  29.         output reg  data_bit_output, // output to zxbus module
  30.         output reg  command_bit_output,
  31.  
  32.         output reg  data_bit_wr, // strobes (positive) to zxbus module, synchronous
  33.         output reg  command_bit_wr,
  34.  
  35.  
  36.         output reg  mode_8chans, // mode outputs for sound_main module
  37.         output reg  mode_pan4ch, //
  38.         output reg  mode_inv7b,  //
  39.  
  40.         output reg  mode_ramro, // mode outputs for memmap module
  41.         output reg  mode_norom,
  42.  
  43.         output reg  [7:0] mode_pg0, // page registers for memmap module
  44.         output reg  [7:0] mode_pg1,
  45.  
  46.  
  47.         output reg  clksel0, // clock select (output from FPGA)
  48.         output reg  clksel1,
  49.  
  50.  
  51.         output reg         snd_wrtoggle, // toggle to write sound data to sound system memory
  52.         output reg         snd_datnvol,  // whether it's for volume (=0) or for samples (=1)
  53.         output reg  [ 2:0] snd_addr,     // address: which channel to be written (0-7)
  54.         output reg  [ 7:0] snd_data,     // actual 8-bit data to be written
  55.  
  56.  
  57.         output wire [ 7:0] md_din, // mp3 data interface
  58.         output wire        md_start,
  59.         input  wire        md_dreq,
  60.         output reg         md_halfspeed,
  61.  
  62.         output reg         mc_ncs, // mp3 control interface
  63.         output reg         mc_xrst,
  64.         input  wire [ 7:0] mc_dout,
  65.         output wire [ 7:0] mc_din,
  66.         output wire        mc_start,
  67.         output reg  [ 1:0] mc_speed,
  68.         input  wire        mc_rdy,
  69.  
  70.         output reg         sd_ncs, // SD card interface
  71.         input  wire [ 7:0] sd_dout,
  72.         output wire [ 7:0] sd_din,
  73.         output wire        sd_start,
  74.         input  wire        sd_det,
  75.         input  wire        sd_wp,
  76.  
  77.         output reg  led, // LED control
  78.         input  wire led_toggle,
  79.  
  80.  
  81.         output reg  [7:0] dma_din_modules, // DMA control
  82.         //
  83.         output reg        dma_select_zx,
  84.         input  wire [7:0] dma_dout_zx,
  85.         //
  86.         output reg        dma_wrstb,
  87.         output reg  [1:0] dma_regsel,
  88.  
  89.  
  90.         // timer freq selector
  91.         output reg  [2:0] timer_rate,
  92.  
  93.         // intena/intreq related
  94.         output wire       intena_wr,
  95.         output wire       intreq_wr,
  96.         input  wire [7:0] intreq_rd
  97.  
  98. );
  99.  
  100.  
  101.         localparam MPAG      = 6'h00;
  102.         localparam MPAGEX    = 6'h10;
  103.  
  104.         localparam ZXCMD     = 6'h01;
  105.         localparam ZXDATRD   = 6'h02;
  106.         localparam ZXDATWR   = 6'h03;
  107.         localparam ZXSTAT    = 6'h04;
  108.         localparam CLRCBIT   = 6'h05;
  109.  
  110.         localparam VOL1      = 6'h06;
  111.         localparam VOL2      = 6'h07;
  112.         localparam VOL3      = 6'h08;
  113.         localparam VOL4      = 6'h09;
  114.         localparam VOL5      = 6'h16;
  115.         localparam VOL6      = 6'h17;
  116.         localparam VOL7      = 6'h18;
  117.         localparam VOL8      = 6'h19;
  118.  
  119.         localparam DAMNPORT1 = 6'h0a;
  120.         localparam DAMNPORT2 = 6'h0b;
  121.  
  122.         localparam LEDCTR    = 6'h01;
  123.  
  124.         localparam GSCFG0    = 6'h0f;
  125.  
  126.         localparam SCTRL     = 6'h11;
  127.         localparam SSTAT     = 6'h12;
  128.  
  129.         localparam SD_SEND   = 6'h13;
  130.         localparam SD_READ   = 6'h13;
  131.         localparam SD_RSTR   = 6'h14;
  132.  
  133.         localparam MD_SEND   = 6'h14; // same as SD_RSTR!!!
  134.  
  135.         localparam MC_SEND   = 6'h15;
  136.         localparam MC_READ   = 6'h15;
  137.  
  138.         localparam DMA_MOD   = 6'h1b; // read/write
  139.         localparam DMA_HAD   = 6'h1c; // LSB bits 1:0 are 00 // read/write all
  140.         localparam DMA_MAD   = 6'h1d; //                  01
  141.         localparam DMA_LAD   = 6'h1e; //                  10
  142.         localparam DMA_CST   = 6'h1f; //                  11
  143.  
  144.         localparam DMA_PORTS = 6'h1c; // mask for _HAD, _MAD, _LAD and _CST ports, two LSBs must be zero
  145.  
  146.         localparam TIM_FREQ  = 6'h0e;
  147.  
  148.         localparam INTENA    = 6'h0c;
  149.         localparam INTREQ    = 6'h0d;
  150.  
  151.  
  152.         // FREE PORT ADDRESSES: /*$0C-$0E,*/ $1A, $20-$3F
  153.  
  154.  
  155.  
  156. /*
  157.         // inputs/outputs description
  158.  
  159.         input      [7:0] din;
  160.         output reg [7:0] dout;
  161.  
  162.         output reg busin; // =1 - dbus ins, =0 - dbus outs
  163.  
  164.         input [15:0] a;
  165.  
  166.         input iorq_n,mreq_n,rd_n,wr_n;
  167.  
  168.         input      [7:0] data_port_input;
  169.         input      [7:0] command_port_input;
  170.         output reg [7:0] data_port_output;
  171.  
  172.         input data_bit_input;
  173.         input command_bit_input;
  174.  
  175.         output reg data_bit_output;
  176.  
  177.         output reg command_bit_output;
  178.  
  179.         output reg data_bit_wr;
  180.  
  181.         output reg command_bit_wr;
  182.  
  183.         output reg mode_inv7b,
  184.                    mode_8chans,
  185.                    mode_pan4ch;
  186.  
  187.         output reg mode_ramro;
  188.  
  189.         output reg mode_norom;
  190.  
  191.         output reg [7:0] mode_pg0;
  192.         output reg [7:0] mode_pg1;
  193.  
  194.         output reg clksel0;
  195.         output reg clksel1;
  196.  
  197.  
  198.         output reg snd_wrtoggle;
  199.         output reg snd_datnvol;
  200.         output reg [2:0] snd_addr;
  201.         output reg [7:0] snd_data;
  202.  
  203.  
  204.         input rst_n;
  205.  
  206.         input cpu_clock;
  207.  
  208.  
  209.  
  210.  
  211.         // SPI interfaces related
  212.  
  213.         // MP3 data interface
  214.         output [7:0] md_din; // data to MP3 data SPI interface
  215.  
  216.         output md_start; // start toggle for mp3 data spi
  217.  
  218.         input md_dreq; // data request from mp3 decoder
  219.  
  220.         output reg md_halfspeed;
  221.  
  222.  
  223.         // MP3 control interface
  224.         output reg mc_ncs; // nCS signal
  225.  
  226.         output reg mc_xrst; // xRESET signal
  227.  
  228.         output mc_start; // start toggle
  229.  
  230.         output reg [1:0] mc_speed;
  231.  
  232.         input mc_rdy;
  233.  
  234.         output [7:0] mc_din; // data to send
  235.  
  236.         input [7:0] mc_dout; // received data
  237.  
  238.  
  239.       // SDcard interface
  240.         output reg sd_ncs;
  241.  
  242.         output sd_start;
  243.  
  244.         output [7:0] sd_din;
  245.  
  246.         input [7:0] sd_dout;
  247.  
  248.         input sd_det;
  249.  
  250.         input sd_wp;
  251.  
  252.  
  253.         // DMA modules control
  254.         //
  255.         output reg [7:0] dma_din_modules;
  256.         //
  257.         input [7:0] dma_dout_zx;
  258.         output reg dma_select_zx;
  259.         //
  260.         output reg dma_wrstb;
  261.         output reg [1:0] dma_regsel;
  262.  
  263.  
  264.         // LED control register
  265.         output reg led;
  266.         input led_toggle;
  267. */
  268.  
  269.  
  270. // internal regs & wires
  271.  
  272.         reg mode_expag; // extended paging mode register
  273.  
  274.         reg port09_bit5;
  275.  
  276.         wire port_enabled; // =1 when port address is in enabled region ($00-$3f)
  277.         wire mem_enabled; // =1 when memory mapped sound regs are addressed ($6000-$7FFF)
  278.         reg volports_enabled; // when volume ports are addressed (6-9 and $16-$19)
  279.  
  280.         reg iowrn_reg; // registered io write signal (all positive edge!)
  281.         reg iordn_reg; // --//--
  282.         reg merdn_reg; // --//--
  283.  
  284.  
  285.         reg port_wr; // synchronous positive write pulse (write from z80 to fpga regs)
  286.         reg port_rd;  // synchronous positive read pulse (read done from fpga regs to z80)
  287.  
  288.         reg memreg_rd; // when memory-mapped sound regs are read
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.         wire port00_wr;   // specific write and read strobes (1 clock cycle long positive)
  296.         wire p_ledctr_wr;
  297.         wire port02_rd;
  298.         wire port03_wr;
  299.         wire port05_wrrd;
  300.         wire port09_wr;
  301.         wire port0a_wrrd;
  302.         wire port0b_wrrd;
  303.         wire port0f_wr;
  304.         wire port10_wr;
  305.  
  306. //      wire p_sstat_rd;
  307. //      wire p_sctrl_rd;
  308.         wire p_sctrl_wr;
  309.         wire p_sdsnd_wr;
  310. //      wire p_sdrd_rd;
  311.         wire p_sdrst_rd;
  312.         wire p_mdsnd_wr;
  313.         wire p_mcsnd_wr;
  314. //      wire p_mcrd_rd;
  315.  
  316.         wire p_dmamod_wr;
  317.         wire p_dmaports_wr;
  318.  
  319.         wire p_timfreq_wr;
  320.  
  321.  
  322.  
  323.  
  324.         reg [2:0] volnum; // volume register number from port address
  325.  
  326.  
  327.         reg [2:0] dma_module_select; // which dma module selected: zero - none selected
  328.         localparam DMA_NONE_SELECTED = 3'd0;
  329.         localparam DMA_MODULE_ZX     = 3'd1;
  330. //      localparam DMA_MODULE_...    = 3'd2;
  331.  
  332.         reg [7:0] dma_dout_modules; // select in data from different modules
  333.  
  334.  
  335. // actual code
  336.  
  337.         //enabled ports
  338.         assign port_enabled = ~(a[7] | a[6]); // $00-$3F
  339.  
  340.         //enabled mem
  341.         assign mem_enabled = (~a[15]) & a[14] & a[13]; // $6000-$7FFF
  342.  
  343.         // volume ports enabled
  344.         always @*
  345.         begin
  346.                 if( a[5:0]==VOL1 ||
  347.                     a[5:0]==VOL2 ||
  348.                     a[5:0]==VOL3 ||
  349.                     a[5:0]==VOL4 ||
  350.                     a[5:0]==VOL5 ||
  351.                     a[5:0]==VOL6 ||
  352.                     a[5:0]==VOL7 ||
  353.                     a[5:0]==VOL8 )
  354.  
  355.                         volports_enabled <= 1'b1;
  356.                 else
  357.                         volports_enabled <= 1'b0;
  358.         end
  359.  
  360.  
  361.  
  362.         //when data bus outputs
  363.         always @*
  364.         begin
  365.                 if( port_enabled && (!iorq_n) && (!rd_n) )
  366.                         busin <= 1'b0; // bus outputs
  367.                 else
  368.                         busin <= 1'b1; // bus inputs
  369.         end
  370.  
  371.  
  372.  
  373.         // rd/wr/iorq syncing in and pulses - old gen
  374.         always @(posedge cpu_clock)
  375.         begin
  376.                 iowrn_reg <= iorq_n | wr_n;
  377.                 iordn_reg <= iorq_n | rd_n;
  378.  
  379.                 if( port_enabled && (!iorq_n) && (!wr_n) && iowrn_reg )
  380.                         port_wr <= 1'b1;
  381.                 else
  382.                         port_wr <= 1'b0;
  383.  
  384.                 if( port_enabled && (!iorq_n) && (!rd_n) && iordn_reg )
  385.                         port_rd <= 1'b1;
  386.                 else
  387.                         port_rd <= 1'b0;
  388.  
  389.         end
  390.  
  391. /*
  392.         // new gen of port_rd and port_wr
  393.         reg [2:0] rd_sync;
  394.         reg [2:0] wr_sync;
  395.         always @(posedge cpu_clock)
  396.         begin
  397.                 rd_sync[2:0] <= { rd_sync[1:0], port_enabled&(~iorq_n)&(~rd_n) };
  398.                 wr_sync[2:0] <= { wr_sync[1:0], port_enabled&(~iorq_n)&(~wr_n) };
  399.  
  400.                 port_wr <= (wr_sync[1:0]==2'b01);
  401.                 port_rd <= (rd_sync[1:0]==2'b01);
  402.  
  403.         end
  404. */
  405.  
  406.         // mreq syncing and mem read pulse
  407.         always @(negedge cpu_clock)
  408.         begin
  409.                 merdn_reg <= mreq_n | rd_n;
  410.  
  411.                 if( mem_enabled && (!mreq_n) && (!rd_n) && merdn_reg )
  412.                         memreg_rd <= 1'b1;
  413.                 else
  414.                         memreg_rd <= 1'b0;
  415.  
  416.         end
  417.  
  418.  
  419.         // specific ports strobes
  420.         assign port00_wr   = ( a[5:0]==MPAG      && port_wr            );
  421.         assign port02_rd   = ( a[5:0]==ZXDATRD   && port_rd            );
  422.         assign port03_wr   = ( a[5:0]==ZXDATWR   && port_wr            );
  423.         assign port05_wrrd = ( a[5:0]==CLRCBIT   && (port_wr||port_rd) );
  424.         assign port09_wr   = ( a[5:0]==VOL4      && port_wr            );
  425.         assign port0a_wrrd = ( a[5:0]==DAMNPORT1 && (port_wr||port_rd) );
  426.         assign port0b_wrrd = ( a[5:0]==DAMNPORT2 && (port_wr||port_rd) );
  427.         assign port0f_wr   = ( a[5:0]==GSCFG0    && port_wr            );
  428.         assign port10_wr   = ( a[5:0]==MPAGEX    && port_wr            );
  429.  
  430.  
  431. //      assign p_sctrl_rd = ( a[5:0]==SCTRL  && port_rd );
  432.         assign p_sctrl_wr = ( a[5:0]==SCTRL  && port_wr );
  433. //      assign p_sstat_rd = ( a[5:0]==SSTAT  && port_rd );
  434.         assign p_sdsnd_wr = ( a[5:0]==SD_SEND && port_wr );
  435. //      assign p_sdrd_rd  = ( a[5:0]==SD_READ && port_rd );
  436.         assign p_sdrst_rd = ( a[5:0]==SD_RSTR && port_rd );
  437.         assign p_mdsnd_wr = ( a[5:0]==MD_SEND && port_wr );
  438.         assign p_mcsnd_wr = ( a[5:0]==MC_SEND && port_wr );
  439. //      assign p_mcrd_rd  = ( a[5:0]==MC_READ && port_rd );
  440.  
  441.         assign p_ledctr_wr = ( a[5:0]==LEDCTR && port_wr );
  442.  
  443.         assign p_dmamod_wr   = ( a[5:0]==DMA_MOD && port_wr );
  444.         assign p_dmaports_wr = ( {a[5:2],2'b00}==DMA_PORTS && port_wr );
  445.  
  446.         assign p_timfreq_wr = ( a[5:0]==TIM_FREQ && port_wr );
  447.  
  448.         assign intena_wr = ( a[5:0]==INTENA && port_wr );
  449.         assign intreq_wr = ( a[5:0]==INTREQ && port_wr );
  450.  
  451.  
  452.  
  453.         // read from fpga to Z80
  454.         always @*
  455.         begin
  456.                 case( a[5:0] )
  457.                 ZXCMD: // command register
  458.                         dout <= command_port_input;
  459.                 ZXDATRD: // data register
  460.                         dout <= data_port_input;
  461.                 ZXSTAT: // status bits
  462.                         dout <= { data_bit_input, 6'bXXXXXX, command_bit_input };
  463.                 GSCFG0: // config register #0F
  464.                         dout <= { mode_inv7b, mode_pan4ch, clksel1, clksel0, mode_expag, mode_8chans, mode_ramro, mode_norom };
  465.  
  466.                 SSTAT:
  467.                         dout <= { 4'd0, mc_rdy, sd_wp, sd_det, md_dreq };
  468.                 SCTRL:
  469.                         dout <= { 2'd0, mc_speed[1], md_halfspeed, mc_speed[0], mc_xrst, mc_ncs, sd_ncs };
  470.                 SD_READ:
  471.                         dout <= sd_dout;
  472.                 SD_RSTR:
  473.                         dout <= sd_dout;
  474.                 MC_READ:
  475.                         dout <= mc_dout;
  476.  
  477.                 INTREQ:
  478.                         dout <= intreq_rd;
  479.  
  480.  
  481.                 DMA_MOD:
  482.                         dout <= { 5'd0, dma_module_select };
  483.                 DMA_HAD:
  484.                         dout <= dma_dout_modules;
  485.                 DMA_MAD:
  486.                         dout <= dma_dout_modules;
  487.                 DMA_LAD:
  488.                         dout <= dma_dout_modules;
  489.                 DMA_CST:
  490.                         dout <= dma_dout_modules;
  491.  
  492.  
  493.                 default:
  494.                         dout <= 8'bXXXXXXXX;
  495.                 endcase
  496.         end
  497.  
  498.  
  499.  
  500.  
  501.  
  502.         // write to $00 and $10 ports ++
  503.         always @(posedge cpu_clock)
  504.         begin
  505.                 if( port00_wr==1'b1 ) // port 00
  506.                 begin
  507.                         if( mode_expag==1'b0 ) // normal paging
  508.                                 mode_pg0[7:0] <= { din[6:0], 1'b0 };
  509.                         else // extended paging
  510.                                 mode_pg0[7:0] <= { din[6:0], din[7] };
  511.                 end
  512.  
  513.                 if( mode_expag==1'b0 && port00_wr==1'b1 ) // port 10 (when in normal mode, part of port 00)
  514.                         mode_pg1[7:0] <= { din[6:0], 1'b1 };
  515.                 else if( mode_expag==1'b1 && port10_wr==1'b1 )
  516.                         mode_pg1[7:0] <= { din[6:0], din[7] };
  517.         end
  518.  
  519.         // port $03 write ++
  520.         always @(posedge cpu_clock)
  521.         begin
  522.                 if( port03_wr==1'b1 )
  523.                         data_port_output <= din;
  524.         end
  525.  
  526.         // port $09 bit tracing
  527.         always @(posedge cpu_clock)
  528.         begin
  529.                 if( port09_wr==1'b1 )
  530.                         port09_bit5 <= din[5];
  531.         end
  532.  
  533.         // write and reset of port $0F ++
  534.         always @(posedge cpu_clock,negedge rst_n)
  535.         begin
  536.                 if( rst_n==1'b0 ) // reset!
  537.                         { mode_inv7b, mode_pan4ch, clksel1, clksel0, mode_expag, mode_8chans, mode_ramro, mode_norom } <= 8'b00110000;
  538.                 else // write to port
  539.                 begin
  540.                         if( port0f_wr == 1'b1 )
  541.                         begin
  542.                                 { mode_inv7b, mode_pan4ch, clksel1, clksel0, mode_expag, mode_8chans, mode_ramro, mode_norom } <= din[7:0];
  543.                         end
  544.                 end
  545.         end
  546.  
  547.         // data bit handling
  548.         always @*
  549.         case( {port02_rd,port03_wr,port0a_wrrd} )
  550.                 3'b100:
  551.                 begin
  552.                         data_bit_output <= 1'b0;
  553.                         data_bit_wr <= 1'b1;
  554.                 end
  555.  
  556.                 3'b010:
  557.                 begin
  558.                         data_bit_output <= 1'b1; // ++
  559.                         data_bit_wr <= 1'b1;
  560.                 end
  561.  
  562.                 3'b001:
  563.                 begin
  564.                         data_bit_output <= ~mode_pg0[0];
  565.                         data_bit_wr <= 1'b1;
  566.                 end
  567.  
  568.                 default:
  569.                 begin
  570.                         data_bit_output <= 1'bX;
  571.                         data_bit_wr <= 1'b0;
  572.                 end
  573.         endcase
  574.  
  575.         // command bit handling
  576.         always @*
  577.         case( {port05_wrrd,port0b_wrrd} )
  578.                 2'b10:
  579.                 begin
  580.                         command_bit_output <= 1'b0;
  581.                         command_bit_wr <= 1'b1;
  582.                 end
  583.  
  584.                 2'b01:
  585.                 begin
  586.                         command_bit_output <= port09_bit5;
  587.                         command_bit_wr <= 1'b1;
  588.                 end
  589.  
  590.                 default:
  591.                 begin
  592.                         command_bit_output <= 1'bX;
  593.                         command_bit_wr <= 1'b0;
  594.                 end
  595.         endcase
  596.  
  597.         // handle data going to sound module (volume and samples values)
  598.         always @*
  599.         begin
  600.                 case( a[5:0] ) // port addresses to volume register numbers
  601.                 VOL1:
  602.                         volnum <= 3'd0;
  603.                 VOL2:
  604.                         volnum <= 3'd1;
  605.                 VOL3:
  606.                         volnum <= 3'd2;
  607.                 VOL4:
  608.                         volnum <= 3'd3;
  609.                 VOL5:
  610.                         volnum <= 3'd4;
  611.                 VOL6:
  612.                         volnum <= 3'd5;
  613.                 VOL7:
  614.                         volnum <= 3'd6;
  615.                 VOL8:
  616.                         volnum <= 3'd7;
  617.                 default:
  618.                         volnum <= 3'bXXX;
  619.                 endcase
  620.         end
  621.  
  622.         // handling itself (sending data to sound module)
  623.         always @(posedge cpu_clock)
  624.         begin
  625.                 if( memreg_rd ) // memory read - sample data write
  626.                 begin
  627.                         snd_wrtoggle <= ~snd_wrtoggle;
  628.                         snd_datnvol  <= 1'b1; // sample data
  629.  
  630.                         if( !mode_8chans ) // 4 channel mode
  631.                                 snd_addr <= { 1'b0, a[9:8] };
  632.                         else // 8 channel mode
  633.                                 snd_addr <= a[10:8];
  634.  
  635.                         snd_data <= din;
  636.                 end
  637.                 else if( volports_enabled && port_wr )
  638.                 begin
  639.                         snd_wrtoggle <= ~snd_wrtoggle;
  640.                         snd_datnvol  <= 1'b0; // volume data
  641.                         snd_addr <= volnum;
  642.                         snd_data <= din;
  643.                 end
  644.         end
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.         //SPI (mp3, SD) interfaces
  652.  
  653.         assign sd_din = (a[5:0]==SD_RSTR) ? 8'hFF : din;
  654.         assign mc_din = din;
  655.         assign md_din = din;
  656.  
  657.  
  658.         assign sd_start = p_sdsnd_wr | p_sdrst_rd;
  659.         assign mc_start = p_mcsnd_wr;
  660.         assign md_start = p_mdsnd_wr;
  661.  
  662.  
  663.       always @(posedge cpu_clock, negedge rst_n)
  664.       begin
  665.                 if( !rst_n ) // async reset
  666.                 begin
  667.                         md_halfspeed <= 1'b0;
  668.                         mc_speed     <= 2'b01;
  669.                         mc_xrst      <= 1'b0;
  670.                         mc_ncs       <= 1'b1;
  671.                         sd_ncs       <= 1'b1;
  672.                 end
  673.                 else // clock
  674.                 begin
  675.                         if( p_sctrl_wr )
  676.                         begin
  677.                                 if( din[0] )
  678.                                         sd_ncs       <= din[7];
  679.  
  680.                                 if( din[1] )
  681.                                         mc_ncs       <= din[7];
  682.  
  683.                                 if( din[2] )
  684.                                         mc_xrst      <= din[7];
  685.  
  686.                                 if( din[3] )
  687.                                         mc_speed[0]  <= din[7];
  688.  
  689.                                 if( din[4] )
  690.                                         md_halfspeed <= din[7];
  691.  
  692.                                 if( din[5] )
  693.                                         mc_speed[1]  <= din[7];
  694.  
  695.                         end
  696.                 end
  697.       end
  698.  
  699.  
  700.         // LED control
  701.         always @(posedge cpu_clock, negedge rst_n)
  702.         begin
  703.                 if( !rst_n )
  704.                         led <= 1'b0;
  705.                 else
  706.                 begin
  707.                         if( p_ledctr_wr )
  708.                                 led <= din[0];
  709.                         else if( led_toggle )
  710.                                 led <= ~led;
  711.                 end
  712.  
  713.         end
  714.  
  715.  
  716.  
  717.  
  718.         // DMA control
  719.         //
  720.         always @(posedge cpu_clock, negedge rst_n) // selection of modules
  721.         begin
  722.                 if( !rst_n )
  723.                         dma_module_select <= DMA_NONE_SELECTED;
  724.                 else if( p_dmamod_wr )
  725.                         dma_module_select <= din[2:0];
  726.         end
  727.         //
  728.         always @* dma_din_modules = din; // translate Z80 bus out to all DMA modules
  729.         //
  730.         always @* // select modules by individual signals
  731.         begin
  732.                 dma_select_zx = 1'b0;
  733.                 //dma_select_... = 1'b0;
  734.  
  735.                 case( dma_module_select )
  736.                 DMA_MODULE_ZX:
  737.                         dma_select_zx = 1'b1;
  738.                 //DMA_MODULE_...:
  739.                 //      dma_select_... = 1'b1;
  740.                 endcase
  741.         end
  742.         //
  743.         always @* dma_wrstb = p_dmaports_wr; // translate dma write strobe
  744.         //
  745.         always @* dma_regsel = a[1:0];
  746.         //
  747.         always @* // route data from modules to the common module bus
  748.         begin
  749.                 case( dma_regsel )
  750.                 DMA_MODULE_ZX:
  751.                         dma_dout_modules <= dma_dout_zx;
  752.                 //DMA_MODULE_...:
  753.                 //      dma_dout_modules <= dma_dout_...;
  754.                 default:
  755.                         dma_dout_modules <= 8'bxxxxxxxx;
  756.                 endcase
  757.         end
  758.  
  759.  
  760.  
  761.         // timer rate
  762.         //
  763.         always @(posedge cpu_clock,negedge rst_n)
  764.         if( !rst_n )
  765.                 timer_rate <= 3'b000;
  766.         else if( p_timfreq_wr )
  767.                 timer_rate <= din[2:0];
  768.  
  769.  
  770. endmodule
  771.  
  772.