dma_adapter.v 17.2 KB
Newer Older
Alexey Grebenkin's avatar
Alexey Grebenkin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*******************************************************************************
 * Module: dma_adapter
 * Date: 2015-07-11  
 * Author: Alexey     
 * Description: temporary interconnect to membridge testing purposes only
 *
 * Copyright (c) 2015 Elphel, Inc.
 * dma_adapter.v is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dma_adapter.v file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> .
 *******************************************************************************/
/*
 * The module is temporary
 * It could make transactions from DMA data buffer to membridge and vice versa.
 * Processes 1 transaction of 16 x 64bit-words at a time. 
 * Waits until 1 read or 1 write is completely done.
 * After that it deasserts busy and is ready to process a new transaction.
 *
 * The whole purpose of a module as a system block is to be a buffer between 
 * a big dma data storage and axi interface. So it shall recieve data and control
 * for 1 burst and pass it to axi.
 */
Alexey Grebenkin's avatar
Alexey Grebenkin committed
32
module dma_adapter(
Alexey Grebenkin's avatar
Alexey Grebenkin committed
33 34
    input   wire                                clk,
    input   wire                                rst,
Alexey Grebenkin's avatar
Alexey Grebenkin committed
35

Alexey Grebenkin's avatar
Alexey Grebenkin committed
36 37 38 39 40 41 42 43
    // cmd iface
    input   wire                                cmd_type, // 1 = wr, 0 = rd
    input   wire                                cmd_val, // issue a cmd
    input   wire    [31:7]                      cmd_addr, // [2:0] - 64-bit (8-bytes) word offset, [6:3] - 16-words transfer offset
    output  wire                                cmd_busy, // no-pipelined cmd execution, 1 cmd at a time

    // data iface
    input   wire    [63:0]                      wr_data_in,
Alexey Grebenkin's avatar
Alexey Grebenkin committed
44 45
    input   wire                                wr_val_in,
    output  wire                                wr_ack_out,
Alexey Grebenkin's avatar
Alexey Grebenkin committed
46
    output  wire    [63:0]                      rd_data_out,
Alexey Grebenkin's avatar
Alexey Grebenkin committed
47 48
    output  wire                                rd_val_out,
    input   wire                                rd_ack_in,
Alexey Grebenkin's avatar
Alexey Grebenkin committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    // membridge iface
    output  wire    [7:0]                       cmd_ad,
    output  wire                                cmd_stb,
    input   wire    [7:0]                       status_ad,
    input   wire                                status_rq,
    output  wire                                status_start,
    input   wire                                frame_start_chn,
    input   wire                                next_page_chn,
    output  wire                                cmd_wrmem,
    output  wire                                page_ready_chn,
    output  wire                                frame_done_chn,
    output  wire    [15:0]                      line_unfinished_chn1,
    input   wire                                suspend_chn1,
    output  wire                                xfer_reset_page_rd,
    output  wire                                buf_wpage_nxt,
    output  wire                                buf_wr,
    output  wire    [63:0]                      buf_wdata,
    output  wire                                xfer_reset_page_wr,
    output  wire                                buf_rpage_nxt,
    output  wire                                buf_rd,
    input   wire    [63:0]                      buf_rdata,
    // additinal wire to indicate if membridge recieved a packet
    input   wire                                rdata_done // = membridge.is_last_in_page & membridge.afi_rready;
);
// cmd handling
// if not busy and got cmd with val => cmd recieved, assert busy, start a respective algorithm
wire            wr_start;
wire            rd_start;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
78
wire            dma_start;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
79 80 81 82
reg             wr_done;
reg             rd_done;

reg             cmd_type_r;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
83
reg     [31:7]  cmd_addr_r;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
84 85 86 87 88
reg             cmd_busy_r;
wire            set_busy;
wire            clr_busy;

assign  set_busy = ~cmd_busy_r & cmd_val;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
89 90 91 92 93
assign  clr_busy = cmd_busy_r & (wr_done | rd_done);

assign  cmd_busy = cmd_busy_r;
assign  wr_start = set_busy & cmd_type;
assign  rd_start = set_busy & ~cmd_type;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
94 95 96

always @ (posedge clk)
begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
97 98 99
    cmd_type_r  <= rst ? 1'b0 : set_busy ? cmd_type : cmd_type_r;
    cmd_addr_r  <= rst ? 1'b0 : set_busy ? cmd_addr : cmd_addr_r;
    cmd_busy_r  <= (cmd_busy_r | set_busy) & ~rst & ~clr_busy;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
100 101 102
end

/*
Alexey Grebenkin's avatar
Alexey Grebenkin committed
103
 * Read/write data state machine
Alexey Grebenkin's avatar
Alexey Grebenkin committed
104 105 106 107
 * For better readability the state machine is splitted to two pieces:
 * the first one is responsible only for the CMD WRITE case handling, 
 * the second one, respectively, for CMD READ
 *
Alexey Grebenkin's avatar
Alexey Grebenkin committed
108 109
 * Simultaniously with each fsm starts a membridge fsm, which, if being 1st time launched, 
 * sets up membridge's registers, or, if have been launched before, just programs read/write 
Alexey Grebenkin's avatar
Alexey Grebenkin committed
110 111 112 113 114 115
 * address.
 *
 * Current implementation is extremely slow, but simple and reliable
 * After all other parts are implemented and this place occurs to be a bottleneck
 * then replace it (and may be membridge too) with something more ... pipelined
 */
Alexey Grebenkin's avatar
Alexey Grebenkin committed
116 117 118 119 120 121 122
// check if memberidge was already set up
reg             membr_is_set;
always @ (posedge clk)
    membr_is_set <= (membr_is_set | dma_start) & ~rst;

// common state register
reg     [3:0]   rdwr_state;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
123 124

// Get data from buffer
Alexey Grebenkin's avatar
Alexey Grebenkin committed
125 126 127
localparam READ_IDLE        = 0;
localparam READ_WAIT_ADDR   = 3;
localparam READ_DATA        = 4;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
128 129
reg             rd_reset_page;
reg             rd_next_page;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
130 131 132 133 134 135 136 137 138 139
reg             rd_data;
reg     [6:0]   rd_data_count;

wire            rd_stop;
wire            rd_cnt_to_pull;

assign  rd_cnt_to_pull == 7'hf;
assign  rd_stop = rd_ack_in & rd_data_count == rd_cnt_to_pull;

assign  rd_data_out = rd_data;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
140 141 142 143

always @ (posedge clk)
    if (rst)
    begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
144 145 146 147 148
        rdwr_state      <= READ_IDLE;
        rd_done         <= 1'b0;
        rd_data_count   <= 7'h0;
        rd_next_page    <= 1'b0;
        rd_en           <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
149 150 151 152 153
    end
    else
        case (rst)
        READ_IDLE:
        begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
154 155 156 157 158
            rdwr_state      <= rd_start ? READ_WAIT_ADDR : READ_IDLE;
            rd_done         <= 1'b0;
            rd_data_count   <= 7'h0;
            rd_next_page    <= 1'b0;
            rd_en           <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
159
        end
Alexey Grebenkin's avatar
Alexey Grebenkin committed
160
        READ_WAIT_ADDR: // wait until address information is sent to the bus and input buffer got data
Alexey Grebenkin's avatar
Alexey Grebenkin committed
161
        begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
            rdwr_state      <= membr_state == IDLE & rdata_done ? READ_DATA : READ_WAIT_ADDR;
            rd_done         <= 1'b0;
            rd_data_count   <= 7'h0;
            rd_next_page    <= 1'b0;
            rd_en           <= 1'b0;
        end
        READ_DATA: 
        begin
            rdwr_state      <= rd_stop ? READ_IDLE : READ_DATA;
            rd_done         <= rd_stop ? 1'b1 : 1'b0;
            rd_data_count   <= rd_ack_in ? rd_data_count + 1'b1 : rd_data_count;
            rd_next_page    <= rd_stop ? 1'b1 : 1'b0;
            rd_en           <= rd_ack_in ? 1'b1 : 1'b0;
        end
        default: // write is processing
        begin
            rdwr_state      <= READ_IDLE;
            rd_done         <= 1'b0;
            rd_data_count   <= 7'h0;
            rd_next_page    <= 1'b0;
            rd_en           <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
183 184 185 186
        end


// Put data into buffer
Alexey Grebenkin's avatar
Alexey Grebenkin committed
187 188 189
localparam WRITE_IDLE       = 0;
localparam WRITE_DATA       = 1;
localparam WRITE_WAIT_ADDR  = 2;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
190 191 192 193
reg             wr_en;
reg             wr_reset_page;
reg             wr_next_page;
reg     [63:0]  wr_data;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
194
reg     [6:0]   wr_data_count;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
195 196 197 198 199 200 201
reg             wr_page_ready;
reg             wr_val;

wire    [6:0]   wr_cnt_to_push;
wire            wr_stop;

assign  wr_cnt_to_push  = 7'hf;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
202
assign  wr_stop         = wr_val_in & wr_data_count == wr_cnt_to_push;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
203

Alexey Grebenkin's avatar
Alexey Grebenkin committed
204
assign  wr_ack_out   = wr_val_in & rdwr_state == WRITE_DATA;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
205 206 207 208 209 210 211 212
assign  wr_data_in   = wr_data;


// assuming for now we write only pre-defined 16 64-bit words
always @ (posedge clk)
    if (rst)
    begin
        wr_done         <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
213
        wr_data_count   <= 7'd0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
214 215 216 217 218 219 220 221 222 223 224 225
        wr_val          <= 1'b0;
        wr_data         <= 64'h0;
        wr_next_page    <= 1'b0;
        wr_reset_page   <= 1'b0;
        wr_en           <= 1'b0;
        wr_page_ready   <= 1'b0;
        rdwr_state      <= WRITE_IDLE;
    end
    else
        case (wr_state)
            WRITE_IDLE:
            begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
226
                wr_data_count   <= 7'd0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
227 228 229 230 231 232
                wr_done         <= 1'b0;
                wr_data         <= 64'h0;
                wr_next_page    <= 1'b0;
                wr_reset_page   <= wr_start ? 1'b1 : 1'b0;
                wr_en           <= 1'b0;
                wr_page_ready   <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
233
                rdwr_state      <= wr_start ? WRITE_DATA : WRITE_IDLE;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
234
            end
Alexey Grebenkin's avatar
Alexey Grebenkin committed
235
            WRITE_DATA:
Alexey Grebenkin's avatar
Alexey Grebenkin committed
236
            begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
237 238 239
                wr_done         <= wr_stop & membr_state == IDLE ? 1'b1 : 1'b0;
                wr_data_count   <= wr_val_in ? wr_data_count + 1'b1 : wr_data_count;
                wr_data         <= in_data : 
Alexey Grebenkin's avatar
Alexey Grebenkin committed
240 241
                wr_next_page    <= wr_stop ? 1'b1 : 1'b0;
                wr_reset_page   <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
242
                wr_en           <= wr_val_in;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
243
                wr_page_ready   <= wr_stop ? 1'b1 : 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
244 245 246 247 248 249 250 251 252 253 254 255 256
                rdwr_state      <= wr_stop & membr_state == IDLE ? WRITE_IDLE : 
                                   wr_stop                       ? WRITE_WAIT_ADDR : WRITE_DATA;
            end
            WRITE_WAIT_ADDR: // in case all data is written into a buffer, but address is still being issued on axi bus
            begin
                wr_done         <= membr_state == IDLE ? 1'b1 : 1'b0;
                wr_data_count   <= 7'd0;
                wr_data         <= 64'h0;
                wr_next_page    <= 1'b0;
                wr_reset_page   <= 1'b0;
                wr_en           <= 1'b0;
                wr_page_ready   <= 1'b0;
                rdwr_state      <= membr_state == IDLE ? WRITE_IDLE : WRITE_WAIT_ADDR;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
257 258 259 260
            end
            default: // read is executed
            begin
                wr_done         <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
261
                wr_data_count   <= 7'd0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
262 263 264 265 266 267 268 269 270
                wr_data         <= 64'h0;
                wr_next_page    <= 1'b0;
                wr_reset_page   <= 1'b0;
                wr_en           <= 1'b0;
                wr_page_ready   <= 1'b0;
                rdwr_state      <= rdwr_state;
            end
        endcase

Alexey Grebenkin's avatar
Alexey Grebenkin committed
271 272 273 274 275 276
// membridge interface assigments
assign  status_start        = 1'b0; // no need until status is used
assign  cmd_wrmem           = ~cmd_type_r;
assign  xfer_reset_page_wr  = rd_reset_page;
assign  buf_rpage_nxt       = rd_next_page;
assign  buf_rd              = rd_en;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
277 278 279 280 281 282 283
assign  buf_wdata           = wr_data;
assign  buf_wr              = wr_en;
assign  buf_wpage_nxt       = wr_next_page;
assign  xfer_reset_page_rd  = wr_reset_page;
assign  page_ready_chn      = cmd_wrmem ? 1'b0 : wr_page_ready;
assign  frame_done_chn      = 1'b1;

Alexey Grebenkin's avatar
Alexey Grebenkin committed
284 285 286
/*
 * Transfer address and membridge set-ups state machine
 */
Alexey Grebenkin's avatar
Alexey Grebenkin committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
localparam MEMBR_IDLE   = 0;
localparam MEMBR_MODE   = 1;
localparam MEMBR_WIDTH  = 2;
localparam MEMBR_LEN    = 3;
localparam MEMBR_START  = 4;
localparam MEMBR_SIZE   = 5;
localparam MEMBR_LOADDR = 6;
localparam MEMBR_CTRL   = 7;

reg     [32:0]  membr_data;
reg     [15:0]  membr_addr;
reg             membr_start;
reg             membr_done;
reg     [2:0]   membr_state;
reg             membr_setup; // indicates the first tick of the state
wire            membr_inprocess;

Alexey Grebenkin's avatar
Alexey Grebenkin committed
304
assign  dma_start = wr_start | rd_start;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
305 306 307 308 309 310 311 312

always @ (posedge clk)
    if (rst)
    begin
        membr_data  <= 32'h0;
        membr_addr  <= 16'h0;
        membr_start <= 1'b0;
        membr_setup <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
313
        membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
314 315 316 317 318 319 320 321 322 323
        membr_state <= MEMBR_IDLE;
    end
    else
        case (membr_state)
            MEMBR_IDLE:
            begin
                membr_data  <= 32'h0;
                membr_addr  <= 16'h200;
                membr_start <= dma_start ? 1'b1 : 1'b0;
                membr_setup <= dma_start ? 1'b1 : 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
324 325 326
                membr_done  <= 1'b0;
                membr_state <= dma_start &  membr_is_set ? MEMBR_LOADDDR : 
                               dma_start                 ? MEMBR_MODE : MEMBR_IDLE;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
327 328 329 330 331 332 333
            end
            MEMBR_MODE:
            begin
                membr_data  <= 32'h3;
                membr_addr  <= 16'h207;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
334
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
335 336 337 338 339 340 341 342
                membr_state <= membr_inprocess | membr_setup ? MEMBR_MODE : MEMBR_WIDTH;
            end
            MEMBR_WIDTH:
            begin
                membr_data  <= 32'h10;
                membr_addr  <= 16'h206;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
343
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
344 345 346 347 348 349 350 351
                membr_state <= membr_inprocess | membr_setup ? MEMBR_WIDTH : MEMBR_LEN;
            end
            MEMBR_LEN:
            begin
                membr_data  <= 32'h10;
                membr_addr  <= 16'h205;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
352
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
353 354 355 356 357 358 359 360
                membr_state <= membr_inprocess | membr_setup ? MEMBR_LEN : MEMBR_START;
            end
            MEMBR_START:
            begin
                membr_data  <= 32'h0;
                membr_addr  <= 16'h204;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
361
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
362 363 364 365 366 367 368 369
                membr_state <= membr_inprocess | membr_setup ? MEMBR_START : MEMBR_SIZE;
            end
            MEMBR_SIZE:
            begin
                membr_data  <= 32'h10;
                membr_addr  <= 16'h203;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
370
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
371 372 373 374
                membr_state <= membr_inprocess | membr_setup ? MEMBR_SIZE : MEMBR_LOADDR;
            end
            MEMBR_LOADDR:
            begin
Alexey Grebenkin's avatar
Alexey Grebenkin committed
375
                membr_data  <= cmd_addr_r;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
376 377 378
                membr_addr  <= 16'h202;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
379
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
380 381 382 383 384 385 386 387
                membr_state <= membr_inprocess | membr_setup ? MEMBR_LOADDR : MEMBR_CTRL;
            end
            MEMBR_CTRL:
            begin
                membr_data  <= {28'h0000000, 4'b0011};
                membr_addr  <= 16'h200;
                membr_start <= membr_inprocess ? 1'b0 : 1'b1;
                membr_setup <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
388
                membr_done  <= membr_inprocess | membr_setup ? 1'b0 : 1'b1;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
389 390 391 392 393 394 395 396
                membr_state <= membr_inprocess | membr_setup ? MEMBR_CTRL : MEMBR_IDLE;
            end
            default:
            begin
                membr_data  <= 32'h0;
                membr_addr  <= 16'h0;
                membr_start <= 1'b0;
                membr_setup <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
397
                membr_done  <= 1'b0;
Alexey Grebenkin's avatar
Alexey Grebenkin committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
                membr_state <= MEMBR_IDLE;
            end
        endcase

// write to memridge registers fsm
localparam STATE_IDLE   = 3'h0;
localparam STATE_CMD_0  = 3'h1;
localparam STATE_CMD_1  = 3'h2;
localparam STATE_DATA_0 = 3'h3;
localparam STATE_DATA_1 = 3'h4;
localparam STATE_DATA_2 = 3'h5;
localparam STATE_DATA_3 = 3'h6;

reg     [2:0]   state;
reg     [7:0]   out_ad;
reg             out_stb;

assign  membr_inprocess = state != STATE_IDLE;
assign  cmd_ad          = out_ad;
assign  cmd_stb         = out_stb;

always @ (posedge clk)
    if (rst)
    begin
        state   <= STATE_IDLE;
        out_ad  <= 8'h0;
        out_stb <= 1'b0;
    end
    else
        case (state)
            STATE_IDLE: 
            begin
                out_ad  <= 8'h0;
                out_stb <= 1'b0;
                state   <= membr_setup ? STATE_CMD_0 : STATE_IDLE;
            end
            STATE_CMD_0:
            begin
                out_ad  <= membr_addr[7:0];
                out_stb <= 1'b1;
                state   <= STATE_CMD_1;
            end
            STATE_CMD_1:
            begin
                out_ad  <= membr_addr[15:8];
                out_stb <= 1'b0;
                state   <= STATE_DATA_0;
            end
            STATE_DATA_0:
            begin
                out_ad  <= membr_data[7:0];
                out_stb <= 1'b0;
                state   <= STATE_DATA_1;
            end
            STATE_DATA_1:
            begin
                out_ad  <= membr_data[15:8];
                out_stb <= 1'b0;
                state   <= STATE_DATA_2;
            end
            STATE_DATA_2:
            begin
                out_ad  <= membr_data[23:16];
                out_stb <= 1'b0;
                state   <= STATE_DATA_3;
            end
            STATE_DATA_3:
            begin
                out_ad  <= membr_data[31:24];
                out_stb <= 1'b0;
                state   <= STATE_IDLE;
            end
            default:
            begin
                out_ad  <= 8'hff;
                out_stb <= 1'b0;
                state   <= STATE_IDLE;
            end
        endcase

endmodule