Subversion Repositories pentevo

Rev

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

  1. module main(
  2.  
  3.     // clocks
  4.     input fclk,
  5.     output clkz_out,
  6.     input clkz_in,
  7.  
  8.     // z80
  9.     input iorq_n,
  10.     input mreq_n,
  11.     input rd_n,
  12.     input wr_n,
  13.     input m1_n,
  14.     input rfsh_n,
  15.     input int_n,
  16.     input nmi_n,
  17.     input wait_n,
  18.     output res,
  19.  
  20.     inout [7:0] d,
  21.     output [15:0] a,
  22.  
  23.     // zxbus and related
  24.     output csrom,
  25.     output romoe_n,
  26.     output romwe_n,
  27.  
  28.     output rompg0_n,
  29.     output dos_n, // aka rompg1
  30.     output rompg2,
  31.     output rompg3,
  32.     output rompg4,
  33.  
  34.     input iorqge1,
  35.     input iorqge2,
  36.     output iorq1_n,
  37.     output iorq2_n,
  38.  
  39.     // DRAM
  40.     inout [15:0] rd,
  41.     output [9:0] ra,
  42.     output rwe_n,
  43.     output rucas_n,
  44.     output rlcas_n,
  45.     output rras0_n,
  46.     output rras1_n,
  47.  
  48.     // video
  49.     output reg [1:0] vred,
  50.     output reg [1:0] vgrn,
  51.     output reg [1:0] vblu,
  52.  
  53.     output vhsync,
  54.     output vvsync,
  55.     output vcsync,
  56.  
  57.     // AY control and audio/tape
  58.     input ay_clk,
  59.     output ay_bdir,
  60.     output ay_bc1,
  61.  
  62. //    output beep,
  63.     output reg beep,
  64.  
  65.     // IDE
  66.     input [2:0] ide_a,
  67.     input [15:0] ide_d,
  68.  
  69.     output ide_dir,
  70.  
  71.     input ide_rdy,
  72.  
  73.     output ide_cs0_n,
  74.     output ide_cs1_n,
  75.     output ide_rs_n,
  76.     output ide_rd_n,
  77.     output ide_wr_n,
  78.  
  79.     // VG93 and diskdrive
  80.     output reg vg_clk,
  81.  
  82.     output vg_cs_n,
  83.     output vg_res_n,
  84.  
  85.     input vg_hrdy,
  86.     input vg_rclk,
  87.     input vg_rawr,
  88.     input [1:0] vg_a, // disk drive selection
  89.     input vg_wrd,
  90.     input vg_side,
  91.  
  92.     input step,
  93.     input vg_sl,
  94.     input vg_sr,
  95.     input vg_tr43,
  96.     input rdat_b_n,
  97.     input vg_wf_de,
  98.     input vg_drq,
  99.     input vg_irq,
  100.     input vg_wd,
  101.  
  102.     // serial links (atmega-fpga, sdcard)
  103.     output sdcs_n,
  104.     output sddo,
  105.     output sdclk,
  106.     input sddi,
  107.  
  108.     input spics_n,
  109.     input spick,
  110.     input spido,
  111.     output spidi,
  112.     output reg spiint_n
  113. );
  114.  
  115. //--Dummy----------------------------------------------------------------------
  116.  
  117.     assign iorq1_n = 1'b1;
  118.     assign iorq2_n = 1'b1;
  119.  
  120.     assign res= 1'b1;
  121.  
  122.     assign ay_bdir = 1'b0;
  123.     assign ay_bc1  = 1'b0;
  124.  
  125.     assign vg_cs_n  = 1'b1;
  126.     assign vg_res_n = 1'b0;
  127.  
  128.     assign ide_dir=1'b1;
  129.     assign ide_rs_n = 1'b0;
  130.     assign ide_cs0_n = 1'b1;
  131.     assign ide_cs1_n = 1'b1;
  132.     assign ide_rd_n = 1'b1;
  133.     assign ide_wr_n = 1'b1;
  134.  
  135.     assign a[15:14] = 2'b00;
  136.  
  137. //--INT---------------------------------------------------------------------------
  138.  
  139.  reg enable_covox_int;  initial enable_covox_int = 1'b0;
  140.  reg enable_frame_int;  initial enable_frame_int = 1'b0;
  141.  
  142.  always @(posedge fclk)
  143.   begin
  144.    if ( enable_covox_int )
  145.     spiint_n <= ( main_osc[9:2]==8'h00 ) ? 1'b0 : 1'b1;
  146.    else if ( enable_frame_int )
  147.     spiint_n <= ~vblank;
  148.    else
  149.     spiint_n <= 1'b1;
  150.   end
  151.  
  152. //--clocks--Z80_clk--VG_clk--COVOX---------------------------------------------
  153.  
  154.  reg [9:0] main_osc;
  155.  reg [7:0] covox;       initial covox = 8'h7f;
  156.  reg [7:0] covox_work;
  157.  reg [2:0] vgclk_div7;
  158.  
  159.  always @(posedge fclk)
  160.   begin
  161.    //
  162.    main_osc <= main_osc + 10'h01;
  163.    //
  164.    if ( main_osc[7:0] < covox_work )
  165.     beep <= 1'b1;
  166.    else
  167.     beep <= 1'b0;
  168.    //
  169.    if ( main_osc[7:0]==8'h00 )
  170.     covox_work <= covox;
  171.    //
  172.    if ( main_osc[1:0]==2'b00 )
  173.    begin
  174.     if ( vgclk_div7[2:1] == 2'b11 )
  175.      begin
  176.       vgclk_div7 <= 3'd0;
  177.       vg_clk <= ~vg_clk;
  178.      end
  179.     else
  180.      vgclk_div7 <= vgclk_div7 + 3'd1;
  181.    end
  182.    //
  183.   end
  184.  
  185.  assign clkz_out = main_osc[2]; // 3.5 MHz
  186.  
  187. //--Video----------------------------------------------------------------------
  188. // character image - 6x8
  189. // character size  - 6x10
  190. // col x row       - 53x25 (318x250)
  191.  
  192.  localparam HBLNK_BEG  = 9'd416;
  193.  localparam CSYNC_CUT  = 9'd415;
  194.  localparam HSYNC_BEG  = 9'd0;
  195.  localparam HSYNC_END  = 9'd33;
  196.  localparam HSYNC_END2 = 9'd53;
  197.  localparam HBLNK_END  = 9'd98;
  198.  localparam HMAX       = 9'd447;
  199.  
  200.  localparam VBLNK_BEG  = 9'd294;
  201.  localparam VSYNC_BEG  = 9'd0;
  202.  localparam VSYNC_END  = 9'd2;
  203.  localparam VBLNK_END  = 9'd44;
  204.  localparam VMAX       = 9'd319;
  205.  
  206.  localparam HMARK_A    = 9'd77;
  207.  localparam HMARK_B    = 9'd437;
  208.  localparam VMARK_A    = 9'd25;
  209.  localparam VMARK_B    = 9'd313;
  210.  
  211.  reg [8:0] hcount;      initial hcount = 9'd0;
  212.  reg [2:0] pixptr;
  213.  reg [8:0] vcount;      initial vcount = 9'd0;
  214.  reg [5:0] hcharcount;
  215.  wire [2:0] vcharline;
  216.  reg [10:0] voffset;
  217.  reg [3:0] vcharlinecount;
  218.  reg hsync;             initial hsync = 1'b1;
  219.  reg hblank;            initial hblank = 1'b1;
  220.  reg vsync;             initial vsync = 1'b1;
  221.  reg vblank;            initial vblank = 1'b1;
  222.  reg csync;             initial csync = 1'b1;
  223.  wire [10:0] video_addr;
  224.  wire [7:0] charcode0, charcode1, charcode2;
  225.  wire [7:0] charcode;
  226.  wire [7:0] attrcode0, attrcode1, attrcode2;
  227.  wire [7:0] attrcode;
  228.  wire [5:0] charpix;
  229.  wire pixel;
  230.  wire [5:0] fcolor, bcolor, color, image_color;
  231.  wire fontenable;
  232.  reg [8:0] hmouse, vmouse;
  233.  wire mouse_here, mouse_i, mouse_image, mouse_m, mouse_mask;
  234.  reg vgaff, nextline;
  235.  reg hmark;             initial hmark = 1'b0;
  236.  reg vmark;             initial vmark = 1'b0;
  237.  
  238.  always @(posedge fclk)
  239.   begin
  240.    //
  241.    if ( {(main_osc[1]&scr_tv_mode),main_osc[0]}==2'h0 )
  242.     begin
  243.  
  244.      if ( hblank )
  245.       begin
  246.        hcharcount <= 6'h00;
  247.        pixptr <= 3'd0;
  248.       end
  249.      else
  250.       begin
  251.        if ( pixptr==3'd5 )
  252.         begin
  253.          pixptr <= 3'd0;
  254.          hcharcount <= hcharcount + 6'h01;
  255.         end
  256.        else
  257.         pixptr <= pixptr + 3'd1;
  258.       end
  259.  
  260.      if ( hcount==HMAX )
  261.       hcount <= 9'd0;
  262.      else
  263.       hcount <= hcount + 9'd1;
  264.  
  265.      if ( hcount==HBLNK_BEG )
  266.       hblank <= 1'b1;
  267.      else if ( hcount==HBLNK_END )
  268.       hblank <= 1'b0;
  269.  
  270.      if ( hcount==HSYNC_BEG )
  271.       begin
  272.        hsync <= 1'b1;
  273.        vgaff <= scr_tv_mode | ~vgaff;
  274.        if ( vgaff )
  275.         begin
  276.          csync <= 1'b1;
  277.          nextline <= 1'b1;
  278.         end
  279.       end
  280.  
  281.      if ( (~scr_tv_mode) && (hcount==HSYNC_END2) )
  282.       begin
  283.        hsync <= 1'b0;
  284.        if ( !vsync )
  285.         csync <= 1'b0;
  286.       end
  287.  
  288.      if ( scr_tv_mode && (hcount==HSYNC_END) )
  289.       begin
  290.        hsync <= 1'b0;
  291.        if ( !vsync )
  292.         csync <= 1'b0;
  293.       end
  294.  
  295.      if ( (vgaff) && (hcount==HBLNK_BEG) ) // localparam CSYNC_CUT2 = 9'd382;
  296.       csync <= 1'b0;
  297.  
  298.      if ( scr_tv_mode && (hcount==CSYNC_CUT) )
  299.       csync <= 1'b0;
  300.  
  301.      if ( hcount==HMARK_A )
  302.       hmark <= 1'b1;
  303.      else if ( hcount==HMARK_B )
  304.       hmark <= 1'b0;
  305.  
  306.      vgrn[1] <= color[5];
  307.      vgrn[0] <= color[4];
  308.      vred[1] <= color[3];
  309.      vred[0] <= color[2];
  310.      vblu[1] <= color[1];
  311.      vblu[0] <= color[0];
  312.  
  313.      hmouse <= hcount - scr_mouse_x;
  314.  
  315.     end
  316.    //
  317.    if ( nextline )
  318.     begin
  319.  
  320.      nextline <= 1'b0;
  321.  
  322.      if ( vblank )
  323.       begin
  324.        voffset <= 11'd0;
  325.        vcharlinecount <= 4'd15;
  326.       end
  327.      else
  328.       begin
  329.        if ( vcharlinecount==4'd8 )
  330.         begin
  331.          voffset <= voffset + 11'd53;
  332.          vcharlinecount <= 4'd15;
  333.         end
  334.        else
  335.         vcharlinecount <= vcharlinecount + 4'd1;
  336.       end
  337.  
  338.      if ( vcount==VMAX )
  339.       begin
  340.        vcount <= 9'd0;
  341. //       cursorflash <= cursorflash + 4'd1;
  342.       end
  343.      else
  344.       vcount <= vcount + 9'd1;
  345.  
  346.      if ( vcount==VBLNK_BEG )
  347.       vblank <= 1'b1;
  348.      else if ( vcount==VBLNK_END )
  349.       vblank <= 1'b0;
  350.  
  351.      if ( vcount==VSYNC_BEG )
  352.       vsync <= 1'b1;
  353.      else if ( vcount==VSYNC_END )
  354.       vsync <= 1'b0;
  355.  
  356.      if ( vcount==VMARK_A )
  357.       vmark <= 1'b1;
  358.      else if ( vcount==VMARK_B )
  359.       vmark <= 1'b0;
  360.  
  361.      vmouse <= vcount - scr_mouse_y;
  362.  
  363.     end
  364.    //
  365.   end
  366.  
  367.  lpm_rom_7x2 mouse_cursor ( .address({ vmouse[3:0], hmouse[2:0] }), .q({ mouse_i, mouse_m }) );
  368.  assign mouse_here = (hmouse[8:3] == 6'd0) && (vmouse[8:4] == 5'd0);
  369.  assign mouse_mask = mouse_here & mouse_m;
  370.  assign mouse_image = mouse_here & mouse_i;
  371.  
  372.  assign video_addr = voffset + { 4'h0, hcharcount[5:0] };
  373.  lpm_ram_dp_9x8 scrmem0  ( .data(indata), .wraddress(scr_addr[8:0]), .wren((scr_wren_c)&&(scr_addr[10:9]==2'h0)),
  374.                            .rdaddress(video_addr[8:0]), .q(charcode0) );
  375.  lpm_ram_dp_9x8 scrmem1  ( .data(indata), .wraddress(scr_addr[8:0]), .wren((scr_wren_c)&&(scr_addr[10:9]==2'h1)),
  376.                            .rdaddress(video_addr[8:0]), .q(charcode1) );
  377.  lpm_ram_dp_9x8 scrmem2  ( .data(indata), .wraddress(scr_addr[8:0]), .wren((scr_wren_c)&&(scr_addr[10:9]==2'h2)),
  378.                            .rdaddress(video_addr[8:0]), .q(charcode2) );
  379.  assign charcode = (video_addr[10:9]==2'h0) ? charcode0 :
  380.                    (video_addr[10:9]==2'h1) ? charcode1 : charcode2 ;
  381.  lpm_ram_dp_9x8 attrmem0 ( .data(scr_attr), .wraddress(scr_addr[8:0]), .wren((scr_wren_a)&&(scr_addr[10:9]==2'h0)),
  382.                            .rdaddress(video_addr[8:0]), .q(attrcode0) );
  383.  lpm_ram_dp_9x8 attrmem1 ( .data(scr_attr), .wraddress(scr_addr[8:0]), .wren((scr_wren_a)&&(scr_addr[10:9]==2'h1)),
  384.                            .rdaddress(video_addr[8:0]), .q(attrcode1) );
  385.  lpm_ram_dp_9x8 attrmem2 ( .data(scr_attr), .wraddress(scr_addr[8:0]), .wren((scr_wren_a)&&(scr_addr[10:9]==2'h2)),
  386.                            .rdaddress(video_addr[8:0]), .q(attrcode2) );
  387.  assign attrcode = (video_addr[10:9]==2'h0) ? attrcode0 :
  388.                    (video_addr[10:9]==2'h1) ? attrcode1 : attrcode2 ;
  389.  assign vcharline = (vcharlinecount[3]) ? ~vcharlinecount[2:0] : vcharlinecount[2:0];
  390.  lpm_rom_11x6 chargen ( .address({ charcode, vcharline }), .q(charpix) );
  391.  
  392.  assign fcolor = { attrcode[2], (attrcode[2]&attrcode[3]),
  393.                    attrcode[1], (attrcode[1]&attrcode[3]),
  394.                    attrcode[0], (attrcode[0]&attrcode[3]) };
  395.  assign bcolor = { (attrcode[6]&attrcode[7]), (attrcode[6]&(~attrcode[7])),
  396.                    (attrcode[5]&attrcode[7]), (attrcode[5]&(~attrcode[7])),
  397.                    (attrcode[4]&attrcode[7]), (attrcode[4]&(~attrcode[7])) };
  398.  assign fontenable = (charcode[7:4]==4'hb)||
  399.                      (charcode[7:4]==4'hc)||
  400.                      (charcode[7:4]==4'hd)||
  401.                      (~vcharlinecount[3]);
  402.  assign pixel = ( charcode==8'hb0 ) ? ( (vcount[0]^hcount[1])&~hcount[0] ) :
  403.                 ( charcode==8'hb1 ) ? (  vcount[0]^hcount[0]             ) :
  404.                 ( charcode==8'hb2 ) ? ( (vcount[0]^hcount[1])| hcount[0] ) :
  405.                 ( fontenable ) ? charpix[3'd5-pixptr] : 1'b0;
  406.  
  407.  assign mark = vmark&hmark&(vcount[0]^hcount[0]);
  408.  
  409.  assign image_color = (mouse_mask) ? 6'h00 : ( pixel ? fcolor : bcolor ) ;
  410.  assign color = (scr_mark) ? ((mark) ? 6'h3f : 6'h00) :
  411.                 (hblank | vblank) ? 6'h00 :
  412.                 (mouse_image) ? ~image_color : image_color;
  413.  
  414.  assign vhsync = hsync;
  415.  assign vvsync = vsync;
  416.  assign vcsync = ~csync;
  417.  
  418. //--AVRSPI--FlashROM-----------------------------------------------------------
  419.  
  420.  localparam TEMP_REG      = 8'ha0;
  421.  
  422.  localparam SD_CS0        = 8'ha1;
  423.  localparam SD_CS1        = 8'ha2;
  424.  localparam FLASH_LOADDR  = 8'ha3;
  425.  localparam FLASH_MIDADDR = 8'ha4;
  426.  localparam FLASH_HIADDR  = 8'ha5;
  427.  localparam FLASH_DATA    = 8'ha6;
  428.  localparam FLASH_CTRL    = 8'ha7;
  429.  localparam SCR_LOADDR    = 8'ha8;
  430.  localparam SCR_HIADDR    = 8'ha9;
  431.  localparam SCR_SET_ATTR  = 8'haa; // чряшё№ т ATTR
  432.  localparam SCR_FILL      = 8'hab; // яЁхфшэъЁхьхэЄ рфЁхёр ш чряшё№ т ATTR ш т ярь Є№
  433.                                    // (хёыш Єюы№ъю фхЁурЄ№ spics_n, Єю т ярь Є№ сєфхЄ яшёрЄ№ё  яЁхф√фє∙хх чэрўхэшх)
  434.  localparam SCR_CHAR      = 8'hac; // яЁхфшэъЁхьхэЄ рфЁхёр ш чряшё№ т ярь Є№ ёшьтюыют ш ATTR т ярь Є№ рЄЁшсєЄют
  435.                                    // (хёыш Єюы№ъю фхЁурЄ№ spics_n, Єю т ярь Є№ сєфхЄ яшёрЄ№ё  яЁхф√фє∙шх чэрўхэш )
  436.  localparam SCR_MOUSE_X   = 8'had;
  437.  localparam SCR_MOUSE_Y   = 8'hae;
  438.  localparam SCR_MODE      = 8'haf; // .0 - 0=VGAmode, 1=TVmode; .1 - 0=ёхЄър 720x576
  439.  
  440.  localparam MTST_CONTROL  = 8'h50; // .0 - ЄхёЄ ярь Єш (0 - ёсЁюё, 1 - ЁрсюЄр)
  441.  localparam MTST_PASS_CNT0= 8'h51;
  442.  localparam MTST_PASS_CNT1= TEMP_REG;
  443.  localparam MTST_FAIL_CNT0= 8'h52;
  444.  localparam MTST_FAIL_CNT1= TEMP_REG;
  445.  
  446.  localparam COVOX         = 8'h53;
  447.  
  448.  localparam INT_CONTROL   = 8'h54; // .0 - ЁрчЁх°хэшх яЁхЁ√трэшщ юЄ covox-р (27343.75 Hz)
  449.                                    // .1 - ЁрчЁх°хэшх ърфЁют√ї яЁхЁ√трэшщ (~50 Hz)
  450.  
  451.  reg [7:0] number;          initial number = 8'hff;
  452.  reg [7:0] indata;          initial indata = 8'hff;
  453.  reg [7:0] outdata;
  454.  reg [2:0] bitptr;
  455.  reg prev_spics_n;
  456.  reg [18:0] flash_addr;
  457.  reg flash_cs;              initial flash_cs = 1'b0;
  458.  reg flash_oe;              initial flash_oe = 1'b0;
  459.  reg flash_we;              initial flash_we = 1'b0;
  460.  reg flash_postinc;         initial flash_postinc = 1'b0;
  461.  reg [7:0] flash_data_out;
  462.  reg [10:0] scr_addr;       initial scr_addr = 11'h000;
  463.  reg [7:0] scr_attr;        initial scr_attr = 8'h0f;
  464.  reg scr_wren_c;            initial scr_wren_c = 1'b0;
  465.  reg scr_wren_a;            initial scr_wren_a = 1'b0;
  466.  reg [8:0] scr_mouse_x;     initial scr_mouse_x = 9'd0;
  467.  reg [8:0] scr_mouse_y;     initial scr_mouse_y = 9'd0;
  468.  reg scr_tv_mode;           initial scr_tv_mode = 1'b1;
  469.  reg scr_mark;              initial scr_mark = 1'b0;
  470.  wire spicsn_rising;
  471.  wire spicsn_falling;
  472.  wire sd_selected;
  473.  reg cs_trg;
  474.  reg [7:0] temp_reg;
  475.  
  476.  always @(posedge spick)
  477.   begin
  478.    if ( spics_n )
  479.     number <= { number[6:0], spido };
  480.    else
  481.     indata <= { indata[6:0], spido };
  482.   end
  483.  
  484.  always @(negedge spick or posedge spics_n)
  485.   begin
  486.    if ( spics_n )
  487.     bitptr <= 3'b111;
  488.    else
  489.     bitptr <= bitptr - 3'b001;
  490.   end
  491.  
  492.  always @(posedge fclk)
  493.   begin
  494.    //
  495.    if ( spicsn_rising )
  496.     begin
  497.      //
  498.      cs_trg <= 1'b1;
  499.      //
  500.      case ( number )
  501.       FLASH_LOADDR:  flash_addr[7:0] <= indata;
  502.       FLASH_MIDADDR: flash_addr[15:8] <= indata;
  503.       FLASH_HIADDR:  flash_addr[18:16] <= indata[2:0];
  504.       FLASH_DATA:    begin
  505.                       flash_data_out <= indata;
  506.                       if (flash_postinc) flash_addr[13:0] <= flash_addr[13:0] + 14'd1;
  507.                      end
  508.       FLASH_CTRL:    begin
  509.                       flash_cs <= indata[0];
  510.                       flash_oe <= indata[1];
  511.                       flash_we <= indata[2];
  512.                       flash_postinc <= indata[3];
  513.                      end
  514.       SCR_LOADDR:    scr_addr[7:0] <= indata;
  515.       SCR_HIADDR:    scr_addr[10:8] <= indata[2:0];
  516.       SCR_SET_ATTR:  scr_attr <= indata;
  517.       SCR_FILL:      begin
  518.                       scr_attr <= indata;
  519.                       scr_wren_a <= 1'b1;
  520.                      end
  521.       SCR_CHAR:      begin
  522.                       scr_wren_c <= 1'b1;
  523.                       scr_wren_a <= 1'b1;
  524.                      end
  525.       TEMP_REG:      temp_reg <= indata;
  526.       SCR_MOUSE_X:   scr_mouse_x <= { temp_reg[0], indata };
  527.       SCR_MOUSE_Y:   scr_mouse_y <= { temp_reg[0], indata };
  528.       SCR_MODE:      begin
  529.                       scr_tv_mode <= indata[0];
  530.                       scr_mark <= ~indata[1];
  531.                      end
  532.       MTST_CONTROL:  mtst_run <= indata[0];
  533.       COVOX:         covox <= indata;
  534.       INT_CONTROL:   begin
  535.                       enable_covox_int <= indata[0];
  536.                       enable_frame_int <= indata[1];
  537.                      end
  538.      endcase
  539.      //
  540.     end
  541.    else
  542.    begin
  543.     //
  544.     scr_wren_c <= 1'b0;
  545.     scr_wren_a <= 1'b0;
  546.     //
  547.     if ( spicsn_falling )
  548.      begin
  549.       //
  550.       cs_trg <= 1'b0;
  551.       //
  552.       case ( number )
  553.        SCR_FILL:      begin
  554.                        outdata <= 8'hff;
  555.                        scr_addr <= scr_addr + 11'd1;
  556.                       end
  557.        SCR_CHAR:      begin
  558.                        outdata <= 8'hff;
  559.                        scr_addr <= scr_addr + 11'd1;
  560.                       end
  561.        FLASH_DATA:    outdata <= d;
  562.        COVOX:         outdata <= ~covox; // for SPI testing
  563.        MTST_PASS_CNT0:begin
  564.                        outdata <= mtst_pass_counter[7:0];
  565.                        temp_reg <= mtst_pass_counter[15:8];
  566.                       end
  567.        MTST_FAIL_CNT0:begin
  568.                        outdata <= mtst_fail_counter[7:0];
  569.                        temp_reg <= mtst_fail_counter[15:8];
  570.                       end
  571.        TEMP_REG:      outdata <= temp_reg; // read after MTST_PASS_CNT0, MTST_FAIL_CNT0
  572.        default:       outdata <= 8'hff;
  573.       endcase
  574.       //
  575.      end
  576.     //
  577.    end
  578.    //
  579.    prev_spics_n <= spics_n;
  580.    //
  581.   end
  582.  
  583.  assign spicsn_rising  = ( { cs_trg, prev_spics_n, spics_n } == 3'b011 );
  584.  assign spicsn_falling = ( { cs_trg, prev_spics_n, spics_n } == 3'b100 );
  585.  
  586.  assign sd_selected = ( ( (number==SD_CS0) || (number==SD_CS1) ) && (~spics_n) );
  587.  assign spidi = sd_selected ? sddi : outdata[bitptr];
  588.  assign sddo  = sd_selected ? spido : 1'b1;
  589.  assign sdclk = sd_selected ? spick : 1'b0;
  590.  assign sdcs_n = !( (number==SD_CS0) && (~spics_n) );
  591.  
  592.  assign a[13:0]  =  flash_addr[13:0];
  593.  assign rompg0_n = ~flash_addr[14];
  594.  assign { rompg4, rompg3, rompg2, dos_n } = flash_addr[18:15];
  595.  assign csrom   =  flash_cs;
  596.  assign romoe_n = ~flash_oe;
  597.  assign romwe_n = ~flash_we;
  598.  assign d = flash_oe ? 8'bZZZZZZZZ : flash_data_out;
  599.  
  600. //-----------------------------------------------------------------------------
  601.  
  602.  reg mtst_run;      initial mtst_run = 1'b0;
  603.  wire [15:0] mtst_pass_counter;
  604.  wire [15:0] mtst_fail_counter;
  605.  
  606.  mem_tester mtst( .clk(fclk), .rst_n(mtst_run),
  607.                   .pass_counter(mtst_pass_counter),
  608.                   .fail_counter(mtst_fail_counter),
  609.                   .DRAM_DQ(rd), .DRAM_MA(ra), .DRAM_RAS0_N(rras0_n), .DRAM_RAS1_N(rras1_n),
  610.                   .DRAM_LCAS_N(rlcas_n), .DRAM_UCAS_N(rucas_n), .DRAM_WE_N(rwe_n) );
  611.  
  612. //-----------------------------------------------------------------------------
  613.  
  614. endmodule
  615.