corundum_verilator.cpp 33.4 KB
Newer Older
1
#include <iostream>
2
#include <deque>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
3
4
#include <set>
#include <signal.h>
5
6
7
8

extern "C" {
    #include <nicsim.h>
}
9
10
11

#include "Vinterface.h"
#include "verilated.h"
12
#ifdef TRACE_ENABLED
13
#include "verilated_vcd_c.h"
14
#endif
15

Antoine Kaufmann's avatar
Antoine Kaufmann committed
16
#include "debug.h"
Antoine Kaufmann's avatar
Antoine Kaufmann committed
17
#include "corundum.h"
Antoine Kaufmann's avatar
Antoine Kaufmann committed
18
#include "coord.h"
Antoine Kaufmann's avatar
Antoine Kaufmann committed
19
20
21
#include "dma.h"
#include "mem.h"

22
23
24
#define CLOCK_PERIOD (100 * 1000ULL) // 100ns -> 10MHz
#define SYNC_PERIOD (500 * 1000ULL) // 100ns
#define PCI_LATENCY (1 * 1000 * 1000ULL) // 1us
25
#define ETH_LATENCY (1 * 1000 * 1000ULL) // 1us
26

Antoine Kaufmann's avatar
Antoine Kaufmann committed
27
28
29
struct DMAOp;

static volatile int exiting = 0;
30
static uint64_t main_time = 0;
31
static struct nicsim_params nsparams;
32
33
34
#ifdef TRACE_ENABLED
static VerilatedVcdC* trace;
#endif
35

36
static volatile union cosim_pcie_proto_d2h *d2h_alloc(void);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
37
38
39
40
41
42
43


static void sigint_handler(int dummy)
{
    exiting = 1;
}

44
45
46
47
48
double sc_time_stamp()
{
    return main_time;
}

Antoine Kaufmann's avatar
Antoine Kaufmann 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
static void reset_inputs(Vinterface *top)
{
    top->clk = 0;
    top->rst = 0;
    top->m_axis_ctrl_dma_read_desc_ready = 0;
    top->s_axis_ctrl_dma_read_desc_status_tag = 0;
    top->s_axis_ctrl_dma_read_desc_status_valid = 0;
    top->m_axis_ctrl_dma_write_desc_ready = 0;
    top->s_axis_ctrl_dma_write_desc_status_tag = 0;
    top->s_axis_ctrl_dma_write_desc_status_valid = 0;
    top->m_axis_data_dma_read_desc_ready = 0;
    top->s_axis_data_dma_read_desc_status_tag = 0;
    top->s_axis_data_dma_read_desc_status_valid = 0;
    top->m_axis_data_dma_write_desc_ready = 0;
    top->s_axis_data_dma_write_desc_status_tag = 0;
    top->s_axis_data_dma_write_desc_status_valid = 0;
    top->s_axil_awaddr = 0;
    top->s_axil_awprot = 0;
    top->s_axil_awvalid = 0;
    top->s_axil_wdata = 0;
    top->s_axil_wstrb = 0;
    top->s_axil_wvalid = 0;
    top->s_axil_bready = 0;
    top->s_axil_araddr = 0;
    top->s_axil_arprot = 0;
    top->s_axil_arvalid = 0;
    top->s_axil_rready = 0;
    top->m_axil_csr_awready = 0;
    top->m_axil_csr_wready = 0;
    top->m_axil_csr_bresp = 0;
    top->m_axil_csr_bvalid = 0;
    top->m_axil_csr_arready = 0;
    top->m_axil_csr_rdata = 0;
    top->m_axil_csr_rresp = 0;
    top->m_axil_csr_rvalid = 0;
    top->ctrl_dma_ram_wr_cmd_sel = 0;
    //top->ctrl_dma_ram_wr_cmd_be = 0;
    //top->ctrl_dma_ram_wr_cmd_addr = 0;
    top->ctrl_dma_ram_wr_cmd_valid = 0;
    top->ctrl_dma_ram_rd_cmd_sel = 0;
    //top->ctrl_dma_ram_rd_cmd_addr = 0;
    top->ctrl_dma_ram_rd_cmd_valid = 0;
    top->ctrl_dma_ram_rd_resp_ready = 0;
    top->data_dma_ram_wr_cmd_sel = 0;
    //top->data_dma_ram_wr_cmd_be = 0;
    //top->data_dma_ram_wr_cmd_addr = 0;
    top->data_dma_ram_wr_cmd_valid = 0;
    top->data_dma_ram_rd_cmd_sel = 0;
    //top->data_dma_ram_rd_cmd_addr = 0;
    top->data_dma_ram_rd_cmd_valid = 0;
    top->data_dma_ram_rd_resp_ready = 0;
    top->tx_axis_tready = 0;
    top->s_axis_tx_ptp_ts_valid = 0;
    top->rx_axis_tkeep = 0;
    top->rx_axis_tvalid = 0;
    top->rx_axis_tlast = 0;
    top->rx_axis_tuser = 0;
    top->s_axis_rx_ptp_ts_valid = 0;
    top->ptp_ts_step = 0;
}

static void report_output(const char *label, uint64_t val)
{
    if (val == 0)
        return;

    std::cout << "    " << label << " = " << val << std::endl;
}

static void report_outputs(Vinterface *top)
{
    report_output("m_axis_ctrl_dma_read_desc_dma_addr", top->m_axis_ctrl_dma_read_desc_dma_addr);
    report_output("m_axis_ctrl_dma_read_desc_ram_sel", top->m_axis_ctrl_dma_read_desc_ram_sel);
    report_output("m_axis_ctrl_dma_read_desc_ram_addr", top->m_axis_ctrl_dma_read_desc_ram_addr);
    report_output("m_axis_ctrl_dma_read_desc_len", top->m_axis_ctrl_dma_read_desc_len);
    report_output("m_axis_ctrl_dma_read_desc_tag", top->m_axis_ctrl_dma_read_desc_tag);
    report_output("m_axis_ctrl_dma_read_desc_valid", top->m_axis_ctrl_dma_read_desc_valid);
    report_output("m_axis_ctrl_dma_write_desc_dma_addr", top->m_axis_ctrl_dma_write_desc_dma_addr);
    report_output("m_axis_ctrl_dma_write_desc_ram_sel", top->m_axis_ctrl_dma_write_desc_ram_sel);
    report_output("m_axis_ctrl_dma_write_desc_ram_addr", top->m_axis_ctrl_dma_write_desc_ram_addr);
    report_output("m_axis_ctrl_dma_write_desc_len", top->m_axis_ctrl_dma_write_desc_len);
    report_output("m_axis_ctrl_dma_write_desc_tag", top->m_axis_ctrl_dma_write_desc_tag);
    report_output("m_axis_ctrl_dma_write_desc_valid", top->m_axis_ctrl_dma_write_desc_valid);
    report_output("m_axis_data_dma_read_desc_dma_addr", top->m_axis_data_dma_read_desc_dma_addr);
    report_output("m_axis_data_dma_read_desc_ram_sel", top->m_axis_data_dma_read_desc_ram_sel);
    report_output("m_axis_data_dma_read_desc_ram_addr", top->m_axis_data_dma_read_desc_ram_addr);
    report_output("m_axis_data_dma_read_desc_len", top->m_axis_data_dma_read_desc_len);
    report_output("m_axis_data_dma_read_desc_tag", top->m_axis_data_dma_read_desc_tag);
    report_output("m_axis_data_dma_read_desc_valid", top->m_axis_data_dma_read_desc_valid);
    report_output("m_axis_data_dma_write_desc_dma_addr", top->m_axis_data_dma_write_desc_dma_addr);
    report_output("m_axis_data_dma_write_desc_ram_sel", top->m_axis_data_dma_write_desc_ram_sel);
    report_output("m_axis_data_dma_write_desc_ram_addr", top->m_axis_data_dma_write_desc_ram_addr);
    report_output("m_axis_data_dma_write_desc_len", top->m_axis_data_dma_write_desc_len);
    report_output("m_axis_data_dma_write_desc_tag", top->m_axis_data_dma_write_desc_tag);
    report_output("m_axis_data_dma_write_desc_valid", top->m_axis_data_dma_write_desc_valid);
    report_output("s_axil_awready", top->s_axil_awready);
    report_output("s_axil_wready", top->s_axil_wready);
    report_output("s_axil_bresp", top->s_axil_bresp);
    report_output("s_axil_bvalid", top->s_axil_bvalid);
    report_output("s_axil_arready", top->s_axil_arready);
    report_output("s_axil_rdata", top->s_axil_rdata);
    report_output("s_axil_rresp", top->s_axil_rresp);
    report_output("s_axil_rvalid", top->s_axil_rvalid);
    report_output("m_axil_csr_awaddr", top->m_axil_csr_awaddr);
    report_output("m_axil_csr_awprot", top->m_axil_csr_awprot);
    report_output("m_axil_csr_awvalid", top->m_axil_csr_awvalid);
    report_output("m_axil_csr_wdata", top->m_axil_csr_wdata);
    report_output("m_axil_csr_wstrb", top->m_axil_csr_wstrb);
    report_output("m_axil_csr_wvalid", top->m_axil_csr_wvalid);
    report_output("m_axil_csr_bready", top->m_axil_csr_bready);
    report_output("m_axil_csr_araddr", top->m_axil_csr_araddr);
    report_output("m_axil_csr_arprot", top->m_axil_csr_arprot);
    report_output("m_axil_csr_arvalid", top->m_axil_csr_arvalid);
    report_output("m_axil_csr_rready", top->m_axil_csr_rready);
    report_output("ctrl_dma_ram_wr_cmd_ready", top->ctrl_dma_ram_wr_cmd_ready);
    report_output("ctrl_dma_ram_rd_cmd_ready", top->ctrl_dma_ram_rd_cmd_ready);
    report_output("ctrl_dma_ram_rd_resp_valid", top->ctrl_dma_ram_rd_resp_valid);
    report_output("data_dma_ram_wr_cmd_ready", top->data_dma_ram_wr_cmd_ready);
    report_output("data_dma_ram_rd_cmd_ready", top->data_dma_ram_rd_cmd_ready);
    report_output("data_dma_ram_rd_resp_valid", top->data_dma_ram_rd_resp_valid);
    report_output("tx_axis_tkeep", top->tx_axis_tkeep);
    report_output("tx_axis_tvalid", top->tx_axis_tvalid);
    report_output("tx_axis_tlast", top->tx_axis_tlast);
    report_output("tx_axis_tuser", top->tx_axis_tuser);
    report_output("s_axis_tx_ptp_ts_ready", top->s_axis_tx_ptp_ts_ready);
    report_output("rx_axis_tready", top->rx_axis_tready);
    report_output("s_axis_rx_ptp_ts_ready", top->s_axis_rx_ptp_ts_ready);
    report_output("msi_irq", top->msi_irq);
}

179
180
181
182
183
184
185
186
struct MMIOOp {
    uint64_t id;
    uint64_t addr;
    uint64_t value;
    size_t len;
    bool isWrite;
};

187
188
189
190
191
192
193
194
195
196
class MMIOInterface {
    protected:

        enum OpState {
            AddrIssued,
            AddrAcked,
            AddrDone,
        };

        Vinterface &top;
197
198
199
        PCICoordinator &coord;
        std::deque<MMIOOp *> queue;
        MMIOOp *rCur, *wCur;
200
201
202
        enum OpState rState, wState;

    public:
203
204
        MMIOInterface(Vinterface &top_,  PCICoordinator &coord_)
            : top(top_), coord(coord_), rCur(0), wCur(0)
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        {
        }

        void step()
        {
            if (rCur) {
                /* work on active read operation */

                if (rState == AddrIssued && top.s_axil_arready) {
                    /* read handshake is complete */
                    top.s_axil_arvalid = 0;
                    rState = AddrAcked;
                }
                if (rState == AddrAcked && top.s_axil_rvalid) {
                    /* read data received */
                    top.s_axil_rready = 0;
                    rCur->value = top.s_axil_rdata;
222
223
224
225
226
                    coord.mmio_comp_enqueue(rCur);
#ifdef MMIO_DEBUG
                    std::cout << "MMIO: completed AXI read op=" << rCur << " val=" <<
                        rCur->value << std::endl;
#endif
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                    rCur = 0;
                }
            } else if (wCur) {
                /* work on active write operation */

                if (wState == AddrIssued && top.s_axil_awready) {
                    /* write addr handshake is complete */
                    top.s_axil_awvalid = 0;
                    wState = AddrAcked;
                }
                if (wState == AddrAcked && top.s_axil_wready) {
                    /* write data handshake is complete */
                    top.s_axil_wvalid = 0;
                    top.s_axil_bready = 1;
                    wState = AddrDone;
                }
                if (wState == AddrDone && top.s_axil_bvalid) {
                    /* write complete */
                    top.s_axil_bready = 0;
246
247
248
249
250
251
                    // TODO: check top.s_axil_bresp
#ifdef MMIO_DEBUG
                    std::cout << "MMIO: completed AXI write op=" << wCur <<
                        std::endl;
#endif
                    coord.mmio_comp_enqueue(wCur);
252
253
254
255
256
                    wCur = 0;
                }
            } else if (/*!top.clk &&*/ !queue.empty()) {
                /* issue new operation */

257
                MMIOOp *op = queue.front();
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
                queue.pop_front();
                if (!op->isWrite) {
                    /* issue new read */
                    rCur = op;

                    rState = AddrIssued;

                    top.s_axil_araddr = rCur->addr;
                    top.s_axil_arprot = 0x0;
                    top.s_axil_arvalid = 1;
                    top.s_axil_rready = 1;
                } else {
                    /* issue new write */
                    wCur = op;

                    wState = AddrIssued;

                    top.s_axil_awaddr = wCur->addr;
                    top.s_axil_awprot = 0x0;
                    top.s_axil_awvalid = 1;

                    top.s_axil_wdata = wCur->value;
                    top.s_axil_wstrb = 0xf;
                    top.s_axil_wvalid = 1;

                }
            }
        }

        void issueRead(uint64_t id, uint64_t addr, size_t len)
        {
289
290
291
292
293
            MMIOOp *op = new MMIOOp;
#ifdef MMIO_DEBUG
            std::cout << "MMIO: read id=" << id << " addr=" << std::hex << addr
                << " len=" << len << " op=" << op << std::endl;
#endif
294
295
296
297
298
299
300
301
302
303
            op->id = id;
            op->addr = addr;
            op->len = len;
            op->isWrite = false;
            queue.push_back(op);
        }


        void issueWrite(uint64_t id, uint64_t addr, size_t len, uint64_t val)
        {
304
305
306
307
308
            MMIOOp *op = new MMIOOp;
#ifdef MMIO_DEBUG
            std::cout << "MMIO: write id=" << id << " addr=" << std::hex << addr
                << " len=" << len << " val=" << val << " op=" << op << std::endl;
#endif
309
310
311
312
313
314
315
316
            op->id = id;
            op->addr = addr;
            op->len = len;
            op->value = val;
            op->isWrite = true;
            queue.push_back(op);
        }

317
};
Antoine Kaufmann's avatar
Antoine Kaufmann committed
318

319
320
void pci_rwcomp_issue(MMIOOp *op)
{
321
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
322
323
    volatile struct cosim_pcie_proto_d2h_readcomp *rc;
    volatile struct cosim_pcie_proto_d2h_writecomp *wc;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
324

325
326
    if (!msg)
        throw "completion alloc failed";
Antoine Kaufmann's avatar
Antoine Kaufmann committed
327

328
329
330
    if (op->isWrite) {
        wc = &msg->writecomp;
        wc->req_id = op->id;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
331

332
333
334
335
336
337
338
        //WMB();
        wc->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITECOMP |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    } else {
        rc = &msg->readcomp;
        memcpy((void *) rc->data, &op->value, op->len);
        rc->req_id = op->id;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
339

340
341
342
343
344
345
346
        //WMB();
        rc->own_type = COSIM_PCIE_PROTO_D2H_MSG_READCOMP |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    }

    delete op;
}
Antoine Kaufmann's avatar
Antoine Kaufmann committed
347

Antoine Kaufmann's avatar
Antoine Kaufmann committed
348
#if 0
Antoine Kaufmann's avatar
Antoine Kaufmann committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
class MemAccessor {
    protected:
        Vinterface &top;

        /* outputs to memory */
        vluint8_t   &p_mem_sel;
        vluint32_t (*p_mem_be)[4]; /* for write only */
        vluint32_t (&p_mem_addr)[3];
        vluint8_t   &p_mem_valid;
        vluint8_t   *p_mem_resp_ready; /* for read only */

        /* direction depends */
        vluint32_t (&p_mem_data)[32];

        /* inputs from memory */
        vluint8_t   &p_mem_ready;
        vluint8_t   *p_mem_resp_valid; /* for read only */

    public:
        MemAccessor(Vinterface &top_, bool read,
                vluint8_t &p_mem_sel_,
                vluint32_t (*p_mem_be_)[4],
                vluint32_t (&p_mem_addr_)[3],
                vluint8_t &p_mem_valid_,
                vluint8_t *p_mem_resp_ready_,
                vluint32_t (&p_mem_data_)[32],
                vluint8_t &p_mem_ready_,
                vluint8_t *p_mem_resp_valid_)
            : top(top_),
            p_mem_sel(p_mem_sel_), p_mem_be(p_mem_be_), p_mem_addr(p_mem_addr_),
            p_mem_valid(p_mem_valid_), p_mem_resp_ready(p_mem_resp_ready_),
            p_mem_data(p_mem_data_), p_mem_ready(p_mem_ready_),
            p_mem_resp_valid(p_mem_resp_valid_)
        {
383
384
        }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
385
386
387
388
        void step()
        {
        }
};
Antoine Kaufmann's avatar
Antoine Kaufmann committed
389
#endif
390

Antoine Kaufmann's avatar
Antoine Kaufmann committed
391
392
std::set<DMAOp *> pci_dma_pending;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
393
void pci_dma_issue(DMAOp *op)
Antoine Kaufmann's avatar
Antoine Kaufmann committed
394
{
395
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
396
397
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
    uint8_t ty;

    if (!msg)
        throw "completion alloc failed";

    if (op->write) {
        volatile struct cosim_pcie_proto_d2h_write *write = &msg->write;
        write->req_id = (uintptr_t) op;
        write->offset = op->dma_addr;
        write->len = op->len;

        // TODO: check DMA length
        memcpy((void *) write->data, op->data, op->len);

        // WMB();
        write->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITE |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    } else {
        volatile struct cosim_pcie_proto_d2h_read *read = &msg->read;
        read->req_id = (uintptr_t) op;
        read->offset = op->dma_addr;
        read->len = op->len;

        // WMB();
        read->own_type = COSIM_PCIE_PROTO_D2H_MSG_READ |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    }

    pci_dma_pending.insert(op);
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
427

Antoine Kaufmann's avatar
Antoine Kaufmann committed
428
429
430
431
432
433
434
435
static void h2d_readcomp(volatile struct cosim_pcie_proto_h2d_readcomp *rc)
{
    DMAOp *op = (DMAOp *) (uintptr_t) rc->req_id;
    if (pci_dma_pending.find(op) == pci_dma_pending.end())
        throw "unexpected completion";
    pci_dma_pending.erase(op);

    memcpy(op->data, (void *) rc->data, op->len);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
436

Antoine Kaufmann's avatar
Antoine Kaufmann committed
437
#if 0
Antoine Kaufmann's avatar
Antoine Kaufmann committed
438
439
440
441
    std::cerr << "dma read comp: ";
    for (size_t i = 0; i < op->len; i++)
        std::cerr << (unsigned) op->data[i] << " ";
    std::cerr << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
442
#endif
Antoine Kaufmann's avatar
Antoine Kaufmann committed
443
444


Antoine Kaufmann's avatar
Antoine Kaufmann committed
445
446
447
448
449
450
451
452
453
454
455
456
457
    op->engine->pci_op_complete(op);
}

static void h2d_writecomp(volatile struct cosim_pcie_proto_h2d_writecomp *wc)
{
    DMAOp *op = (DMAOp *) (uintptr_t) wc->req_id;
    if (pci_dma_pending.find(op) == pci_dma_pending.end())
        throw "unexpected completion";
    pci_dma_pending.erase(op);

    op->engine->pci_op_complete(op);
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
458
459
460
static uint64_t csr_read(uint64_t off)
{
    switch (off) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
461
462
463
464
465
466
467
468
469
470
471
        case   0x00: return 32; /* firmware id */
        case   0x04: return 1; /* firmware version */
        case   0x08: return 0x43215678; /* board id */
        case   0x0c: return 0x1; /* board version */
        case   0x10: return 1; /* phc count */
        case   0x14: return 0x200; /* phc offset */
        case   0x18: return 0x80; /* phc stride */
        case   0x20: return 1; /* if_count */
        case   0x24: return 0x80000; /* if stride */
        case   0x2c: return 0x80000; /* if csr offset */
        case  0x200: return 0x1; /* phc features */
Antoine Kaufmann's avatar
Antoine Kaufmann committed
472
473
474
475
476
477
        default:
            std::cerr << "csr_read(" << off << ") unimplemented" << std::endl;
            return 0;
    }
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
478
479
480
481
static void csr_write(uint64_t off, uint64_t val)
{
}

482
483
484
static void h2d_read(MMIOInterface &mmio,
        volatile struct cosim_pcie_proto_h2d_read *read)
{
Antoine Kaufmann's avatar
Antoine Kaufmann committed
485
    //std::cout << "got read " << read->offset << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
486
    if (read->offset < 0x80000) {
487
        volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
        volatile struct cosim_pcie_proto_d2h_readcomp *rc;

        if (!msg)
            throw "completion alloc failed";

        rc = &msg->readcomp;
        memset((void *) rc->data, 0, read->len);
        uint64_t val = csr_read(read->offset);
        memcpy((void *) rc->data, &val, read->len);
        rc->req_id = read->req_id;

        //WMB();
        rc->own_type = COSIM_PCIE_PROTO_D2H_MSG_READCOMP |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    } else {
        /*printf("read(bar=%u, off=%lu, len=%u) = %lu\n", read->bar, read->offset,
                read->len, val);*/
        mmio.issueRead(read->req_id, read->offset, read->len);
    }
507
508
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
509
510
511
512
513
514
515
516
517
518
static void h2d_write(MMIOInterface &mmio,
        volatile struct cosim_pcie_proto_h2d_write *write)
{
    uint64_t val = 0;

    memcpy(&val, (void *) write->data, write->len);

    //std::cout << "got write " << write->offset << " = " << val << std::endl;

    if (write->offset < 0x80000) {
519
        volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
        volatile struct cosim_pcie_proto_d2h_writecomp *wc;

        if (!msg)
            throw "completion alloc failed";

        csr_write(write->offset, val);

        wc = &msg->writecomp;
        wc->req_id = write->req_id;

        //WMB();
        wc->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITECOMP |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    } else {
        mmio.issueWrite(write->req_id, write->offset, write->len, val);
    }
}

538
539
static void poll_h2d(MMIOInterface &mmio)
{
540
541
    volatile union cosim_pcie_proto_h2d *msg =
        nicif_h2d_poll(&nsparams, main_time);
542
543
544
545
546
547
    uint8_t t;

    if (msg == NULL)
        return;

    t = msg->dummy.own_type & COSIM_PCIE_PROTO_H2D_MSG_MASK;
548

Antoine Kaufmann's avatar
Antoine Kaufmann committed
549
    //std::cerr << "poll_h2d: polled type=" << (int) t << std::endl;
550
551
552
553
554
    switch (t) {
        case COSIM_PCIE_PROTO_H2D_MSG_READ:
            h2d_read(mmio, &msg->read);
            break;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
555
556
557
558
559
560
561
562
563
564
565
        case COSIM_PCIE_PROTO_H2D_MSG_WRITE:
            h2d_write(mmio, &msg->write);
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_READCOMP:
            h2d_readcomp(&msg->readcomp);
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITECOMP:
            h2d_writecomp(&msg->writecomp);
            break;
566

567
568
569
        case COSIM_PCIE_PROTO_H2D_MSG_SYNC:
            break;

570
571
572
573
574
575
576
        default:
            std::cerr << "poll_h2d: unsupported type=" << t << std::endl;
    }

    nicif_h2d_done(msg);
    nicif_h2d_next();

Antoine Kaufmann's avatar
Antoine Kaufmann committed
577
};
578

579
580
static volatile union cosim_pcie_proto_d2h *d2h_alloc(void)
{
581
    return nicsim_d2h_alloc(&nsparams, main_time);
582
583
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
584
585
586
class EthernetTx {
    protected:
        Vinterface &top;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
587
588
        uint8_t packet_buf[2048];
        size_t packet_len;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
589
590
591

    public:
        EthernetTx(Vinterface &top_)
Antoine Kaufmann's avatar
Antoine Kaufmann committed
592
593
594
595
596
            : top(top_), packet_len(0)
        {
        }

        void packet_done()
Antoine Kaufmann's avatar
Antoine Kaufmann committed
597
        {
598
599
            volatile union cosim_eth_proto_d2n *msg =
                nicsim_d2n_alloc(&nsparams, main_time);
600
601
602
603
604
605
606
607
            volatile struct cosim_eth_proto_d2n_send *send;

            if (!msg)
                throw "completion alloc failed";

            send = &msg->send;
            memcpy((void *) send->data, packet_buf, packet_len);
            send->len = packet_len;
608
            send->timestamp = main_time + ETH_LATENCY;
609
610
611
612
613

            //WMB();
            send->own_type = COSIM_ETH_PROTO_D2N_MSG_SEND |
                COSIM_ETH_PROTO_D2N_OWN_NET;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
614
#ifdef ETH_DEBUG
Antoine Kaufmann's avatar
Antoine Kaufmann committed
615
            std::cerr << "EthernetTx: packet len=" << std::hex << packet_len << " ";
Antoine Kaufmann's avatar
Antoine Kaufmann committed
616
617
618
619
            for (size_t i = 0; i < packet_len; i++) {
                std::cerr << (unsigned) packet_buf[i] << " ";
            }
            std::cerr << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
620
#endif
Antoine Kaufmann's avatar
Antoine Kaufmann committed
621
622
623
624
625
626
627
        }

        void step()
        {
            top.tx_axis_tready = 1;

            if (top.tx_axis_tvalid) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
628
629
630
631
632
633
634
635
636
637
638
639
                /* iterate over all 8 bytes */
                for (size_t i = 0; i < 8; i++) {
                    if ((top.tx_axis_tkeep & (1 << i)) != 0) {
                        assert(packet_len < 2048);
                        packet_buf[packet_len++] = (top.tx_axis_tdata >> (i * 8));
                    }
                }

                if (top.tx_axis_tlast) {
                    packet_done();
                    packet_len = 0;
                }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
640
641
            }
        }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
642
643
644
645
646
};

class EthernetRx {
    protected:
        Vinterface &top;
647
648
649
650
651
652
653
654

        static const size_t FIFO_SIZE = 32;
        uint8_t fifo_bufs[FIFO_SIZE][2048];
        size_t fifo_lens[FIFO_SIZE];
        size_t fifo_pos_rd;
        size_t fifo_pos_wr;


Antoine Kaufmann's avatar
Antoine Kaufmann committed
655
656
657
658
        size_t packet_off;

    public:
        EthernetRx(Vinterface &top_)
659
            : top(top_), fifo_pos_rd(0), fifo_pos_wr(0), packet_off(0)
Antoine Kaufmann's avatar
Antoine Kaufmann committed
660
        {
661
662
            for (size_t i = 0; i < FIFO_SIZE; i++)
                fifo_lens[i] = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
663
664
665
666
        }

        void packet_received(const void *data, size_t len)
        {
667
            if (fifo_lens[fifo_pos_wr] != 0) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
668
669
670
671
                std::cerr << "EthernetRx: dropping packet" << std::endl;
                return;
            }

672
673
            memcpy(fifo_bufs[fifo_pos_wr], data, len);
            fifo_lens[fifo_pos_wr] = len;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
674

Antoine Kaufmann's avatar
Antoine Kaufmann committed
675
#ifdef ETH_DEBUG
676
            std::cout << "rx into " << fifo_pos_wr << std::endl;
677
678
            std::cerr << "EthernetRx: packet len=" << std::hex << len << " ";
            for (size_t i = 0; i < len; i++) {
679
                std::cerr << (unsigned) fifo_bufs[fifo_pos_wr][i] << " ";
Antoine Kaufmann's avatar
Antoine Kaufmann committed
680
681
            }
            std::cerr << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
682
#endif
683
684

            fifo_pos_wr = (fifo_pos_wr + 1) % FIFO_SIZE;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
685
686
687
688
        }

        void step()
        {
689
            if (fifo_lens[fifo_pos_rd] != 0) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
690
691
692
                // we have data to send
                if (packet_off != 0 && !top.rx_axis_tready) {
                    // no ready signal, can't advance
Antoine Kaufmann's avatar
Antoine Kaufmann committed
693
                    std::cerr << "eth rx: no ready" << std::endl;
694
                } else if (packet_off == fifo_lens[fifo_pos_rd]) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
695
                    // done with packet
Antoine Kaufmann's avatar
Antoine Kaufmann committed
696
#ifdef ETH_DEBUG
Antoine Kaufmann's avatar
Antoine Kaufmann committed
697
                    std::cerr << "EthernetRx: finished packet" << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
698
#endif
Antoine Kaufmann's avatar
Antoine Kaufmann committed
699
                    top.rx_axis_tvalid = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
700
                    top.rx_axis_tlast = 0;
701
702
703
704

                    packet_off = 0;
                    fifo_lens[fifo_pos_rd] = 0;
                    fifo_pos_rd = (fifo_pos_rd + 1) % FIFO_SIZE;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
705
706
                } else {
                    // put out more packet data
Antoine Kaufmann's avatar
Antoine Kaufmann committed
707
#ifdef ETH_DEBUG
Antoine Kaufmann's avatar
Antoine Kaufmann committed
708
                    std::cerr << "EthernetRx: push flit " << packet_off << std::endl;
709
710
                    if (packet_off == 0)
                        std::cout << "rx from " << fifo_pos_rd << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
711
#endif
Antoine Kaufmann's avatar
Antoine Kaufmann committed
712
713
                    top.rx_axis_tkeep = 0;
                    top.rx_axis_tdata = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
714
                    size_t i;
715
                    for (i = 0; i < 8 && packet_off < fifo_lens[fifo_pos_rd]; i++) {
Antoine Kaufmann's avatar
Antoine Kaufmann committed
716
                        top.rx_axis_tdata |=
717
718
                            ((uint64_t) fifo_bufs[fifo_pos_rd][packet_off]) <<
                            (i * 8);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
719
720
721
                        top.rx_axis_tkeep |= (1 << i);
                        packet_off++;
                    }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
722
                    top.rx_axis_tvalid = 1;
723
                    top.rx_axis_tlast = (packet_off == fifo_lens[fifo_pos_rd]);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
724
                }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
725
                //trace->dump(main_time);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
726
727
728
            } else {
                // no data
                top.rx_axis_tvalid = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
729
                top.rx_axis_tlast = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
730
731
            }
        }
Antoine Kaufmann's avatar
Antoine Kaufmann committed
732

Antoine Kaufmann's avatar
Antoine Kaufmann committed
733
734
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
735
736
737
738
739
740
741
742
static void n2d_recv(EthernetRx &rx,
        volatile struct cosim_eth_proto_n2d_recv *recv)
{
    rx.packet_received((const void *) recv->data, recv->len);
}

static void poll_n2d(EthernetRx &rx)
{
743
744
    volatile union cosim_eth_proto_n2d *msg =
        nicif_n2d_poll(&nsparams, main_time);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
745
746
747
748
749
750
    uint8_t t;

    if (msg == NULL)
        return;

    t = msg->dummy.own_type & COSIM_ETH_PROTO_N2D_MSG_MASK;
751

Antoine Kaufmann's avatar
Antoine Kaufmann committed
752
753
754
755
756
    switch (t) {
        case COSIM_ETH_PROTO_N2D_MSG_RECV:
            n2d_recv(rx, &msg->recv);
            break;

757
758
759
        case COSIM_ETH_PROTO_N2D_MSG_SYNC:
            break;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
760
761
762
763
764
765
766
        default:
            std::cerr << "poll_n2d: unsupported type=" << t << std::endl;
    }

    nicif_n2d_done(msg);
    nicif_n2d_next();
}
Antoine Kaufmann's avatar
Antoine Kaufmann committed
767

Antoine Kaufmann's avatar
Antoine Kaufmann committed
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#if 0
class PCICoordinator {
    protected:
        struct PCIOp {
            union {
                DMAOp *dma_op;
                uint32_t msi_vec;
            };
            bool isDma;
            bool ready;
        };

        Vinterface &top;
        std::deque<PCIOp *> queue;
        std::map<DMAOp *, PCIOp *> dmamap;

        void process()
        {
            PCIOp *op;
            while (queue.empty()) {
                op = queue.front();
                if (!op->ready)
                    break;

                queue.pop_front();
                if (!op->isDma) {
                    pci_msi_issue(op->msi_vec);
                    delete op;
                } else {
                    pci_dma_issue(op->dma_op);
                    dmamap.erase(op->dma_op);
                    delete op;
                }
            }
        }

    public:
        PCICoordinator(Vinterface &top_)
            : top(top_)
        {
        }

        void dma_register(DMAOp *dma_op, bool ready)
        {
            PCIOp *op = new PCIOp;
            op->dma_op = vec;
            op->isDma = true;
            op->ready = ready;

            queue.push_back(op);
            dmamap[op] = dma_op;

            process();
        }

        void dma_mark_ready(DMAOp *op)
        {
            dmamap[op]->ready = true;

            process();
        }

        void msi_enqueue(uint32_t vec)
        {
            PCIOp *op = new PCIOp;
            op->msi_vec = vec;
            op->isDma = false;
            op->ready = true;
            queue.push_back(op);

            process();
        }
};
#endif

void pci_msi_issue(uint8_t vec)
844
{
845
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
846
847
    volatile struct cosim_pcie_proto_d2h_interrupt *intr;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
848
#ifdef MSI_DEBUG
849
    std::cerr << "MSI interrupt vec=" << (int) vec << std::endl;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
850
#endif
851
852
853
854
855
856
857
858
859
860

    intr = &msg->interrupt;
    intr->vector = vec;
    intr->inttype = COSIM_PCIE_PROTO_INT_MSI;

    // WMB();
    intr->own_type = COSIM_PCIE_PROTO_D2H_MSG_INTERRUPT |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
861
862

static void msi_step(Vinterface &top, PCICoordinator &coord)
863
864
865
866
{
    if (!top.msi_irq)
        return;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
867
868
869
#ifdef MSI_DEBUG
    std::cerr << "msi_step: MSI interrupt raw vec=" << (int) top.msi_irq << std::endl;
#endif
870
871
872
    for (size_t i = 0; i < 32; i++) {
        if (!((1ULL << i) & top.msi_irq))
            continue;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
873
        coord.msi_enqueue(i);
874
875
876
    }
}

877
878
int main(int argc, char *argv[])
{
879
880
881
    char *vargs[2] = { argv[0], NULL };
    Verilated::commandArgs(1, vargs);
#ifdef TRACE_ENABLED
882
    Verilated::traceEverOn(true);
883
884
#endif

885
    if (argc != 4 && argc != 5) {
886
        fprintf(stderr, "Usage: corundum_verilator PCI-SOCKET ETH-SOCKET "
887
                "SHM [START-TICK]\n");
888
889
        return EXIT_FAILURE;
    }
890
891
    if (argc == 5)
        main_time = strtoull(argv[4], NULL, 0);
892
893
894
895

    struct cosim_pcie_proto_dev_intro di;
    memset(&di, 0, sizeof(di));

Antoine Kaufmann's avatar
Antoine Kaufmann committed
896
    di.bars[0].len = 1 << 24;
897
898
    di.bars[0].flags = COSIM_PCIE_PROTO_BAR_64;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
899
900
    di.pci_vendor_id = 0x5543;
    di.pci_device_id = 0x1001;
901
902
903
    di.pci_class = 0x02;
    di.pci_subclass = 0x00;
    di.pci_revision = 0x00;
904
    di.pci_msi_nvecs = 32;
905

906
907
908
909
910
911
912
913
914
    nsparams.sync_pci = 1;
    nsparams.sync_eth = 1;
    nsparams.pci_socket_path = argv[1];
    nsparams.eth_socket_path = argv[2];
    nsparams.shm_path = argv[3];
    nsparams.pci_latency = PCI_LATENCY;
    nsparams.eth_latency = ETH_LATENCY;
    nsparams.sync_delay = SYNC_PERIOD;
    if (nicsim_init(&nsparams, &di)) {
915
916
        return EXIT_FAILURE;
    }
917
918
    std::cout << "sync_pci=" << nsparams.sync_pci <<
        "  sync_eth=" << nsparams.sync_eth << std::endl;
919

Antoine Kaufmann's avatar
Antoine Kaufmann committed
920
    signal(SIGINT, sigint_handler);
921
922


923
    Vinterface *top = new Vinterface;
924
925
#ifdef TRACE_ENABLED
    trace = new VerilatedVcdC;
926
    top->trace(trace, 99);
927
928
    trace->open("debug.vcd");
#endif
929

Antoine Kaufmann's avatar
Antoine Kaufmann committed
930
    MemWritePort p_mem_write_ctrl_dma(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
931
            top->ctrl_dma_ram_wr_cmd_sel,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
932
            top->ctrl_dma_ram_wr_cmd_be,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
933
934
            top->ctrl_dma_ram_wr_cmd_addr,
            top->ctrl_dma_ram_wr_cmd_data,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
935
936
937
938
939
940
            top->ctrl_dma_ram_wr_cmd_valid,
            top->ctrl_dma_ram_wr_cmd_ready);
    MemReadPort p_mem_read_ctrl_dma(
            top->ctrl_dma_ram_rd_cmd_sel,
            top->ctrl_dma_ram_rd_cmd_addr,
            top->ctrl_dma_ram_rd_cmd_valid,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
941
            top->ctrl_dma_ram_rd_resp_ready,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
942
            top->ctrl_dma_ram_rd_resp_data,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
943
944
            top->ctrl_dma_ram_rd_cmd_ready,
            top->ctrl_dma_ram_rd_resp_valid);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
945
946
947
948
949
950
951
952
953
954
955
    MemWritePort p_mem_write_data_dma(
            top->data_dma_ram_wr_cmd_sel,
            top->data_dma_ram_wr_cmd_be,
            top->data_dma_ram_wr_cmd_addr,
            top->data_dma_ram_wr_cmd_data,
            top->data_dma_ram_wr_cmd_valid,
            top->data_dma_ram_wr_cmd_ready);
    MemReadPort p_mem_read_data_dma(
            top->data_dma_ram_rd_cmd_sel,
            top->data_dma_ram_rd_cmd_addr,
            top->data_dma_ram_rd_cmd_valid,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
956
            top->data_dma_ram_rd_resp_ready,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
957
            top->data_dma_ram_rd_resp_data,
Antoine Kaufmann's avatar
Antoine Kaufmann committed
958
959
            top->data_dma_ram_rd_cmd_ready,
            top->data_dma_ram_rd_resp_valid);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
960
961

    DMAPorts p_dma_read_ctrl(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
962
963
964
965
966
967
968
969
970
            top->m_axis_ctrl_dma_read_desc_dma_addr,
            top->m_axis_ctrl_dma_read_desc_ram_sel,
            top->m_axis_ctrl_dma_read_desc_ram_addr,
            top->m_axis_ctrl_dma_read_desc_len,
            top->m_axis_ctrl_dma_read_desc_tag,
            top->m_axis_ctrl_dma_read_desc_valid,
            top->m_axis_ctrl_dma_read_desc_ready,
            top->s_axis_ctrl_dma_read_desc_status_tag,
            top->s_axis_ctrl_dma_read_desc_status_valid);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
971
    DMAPorts p_dma_write_ctrl(
Antoine Kaufmann's avatar
Antoine Kaufmann committed
972
973
974
975
976
977
978
979
980
            top->m_axis_ctrl_dma_write_desc_dma_addr,
            top->m_axis_ctrl_dma_write_desc_ram_sel,
            top->m_axis_ctrl_dma_write_desc_ram_addr,
            top->m_axis_ctrl_dma_write_desc_len,
            top->m_axis_ctrl_dma_write_desc_tag,
            top->m_axis_ctrl_dma_write_desc_valid,
            top->m_axis_ctrl_dma_write_desc_ready,
            top->s_axis_ctrl_dma_write_desc_status_tag,
            top->s_axis_ctrl_dma_write_desc_status_valid);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
    DMAPorts p_dma_read_data(
            top->m_axis_data_dma_read_desc_dma_addr,
            top->m_axis_data_dma_read_desc_ram_sel,
            top->m_axis_data_dma_read_desc_ram_addr,
            top->m_axis_data_dma_read_desc_len,
            top->m_axis_data_dma_read_desc_tag,
            top->m_axis_data_dma_read_desc_valid,
            top->m_axis_data_dma_read_desc_ready,
            top->s_axis_data_dma_read_desc_status_tag,
            top->s_axis_data_dma_read_desc_status_valid);
    DMAPorts p_dma_write_data(
            top->m_axis_data_dma_write_desc_dma_addr,
            top->m_axis_data_dma_write_desc_ram_sel,
            top->m_axis_data_dma_write_desc_ram_addr,
            top->m_axis_data_dma_write_desc_len,
            top->m_axis_data_dma_write_desc_tag,
            top->m_axis_data_dma_write_desc_valid,
            top->m_axis_data_dma_write_desc_ready,
            top->s_axis_data_dma_write_desc_status_tag,
            top->s_axis_data_dma_write_desc_status_valid);

1002
1003
    PCICoordinator pci_coord;
    MMIOInterface mmio(*top, pci_coord);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1004

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1005
    MemWriter mem_control_writer(p_mem_write_ctrl_dma);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1006
    MemReader mem_control_reader(p_mem_read_ctrl_dma);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1007
    MemWriter mem_data_writer(p_mem_write_data_dma);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1008
1009
    MemReader mem_data_reader(p_mem_read_data_dma);

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1010
1011
1012
1013
    DMAReader dma_read_ctrl("read ctrl", p_dma_read_ctrl, mem_control_writer, pci_coord);
    DMAWriter dma_write_ctrl("write ctrl", p_dma_write_ctrl, mem_control_reader, pci_coord);
    DMAReader dma_read_data("read data", p_dma_read_data, mem_data_writer, pci_coord);
    DMAWriter dma_write_data("write data", p_dma_write_data, mem_data_reader, pci_coord);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1014
1015

    EthernetTx tx(*top);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1016
    EthernetRx rx(*top);
1017

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1018
1019
1020
1021
    reset_inputs(top);
    top->rst = 1;
    top->eval();

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1022
    /* raising edge */
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1023
1024
1025
1026
1027
    top->clk = !top->clk;
    top->eval();

    top->rst = 0;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1028
    while (!exiting) {
1029
1030
1031
1032
        while (nicsim_sync(&nsparams, main_time)) {
            std::cerr << "warn: nicsim_sync failed (t=" << main_time << ")" <<
                std::endl;
        }
1033
1034
1035
1036

        do {
            poll_h2d(mmio);
            poll_n2d(rx);
1037
1038
        } while ((nsparams.sync_pci || nsparams.sync_eth) &&
            netsim_next_timestamp(&nsparams) <= main_time && !exiting);
1039

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1040
        /* falling edge */
1041
        top->clk = !top->clk;
1042
        main_time += CLOCK_PERIOD / 2;
1043
1044
1045
        top->eval();

        mmio.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1046

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1047
        dma_read_ctrl.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1048
        dma_write_ctrl.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1049
        dma_read_data.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1050
1051
        dma_write_data.step();

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1052
        mem_control_writer.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1053
        mem_control_reader.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1054
        mem_data_writer.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1055
        mem_data_reader.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1056

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1057
        tx.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1058
        rx.step();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1059

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1060
        msi_step(*top, pci_coord);
1061

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1062
        /* raising edge */
1063
        top->clk = !top->clk;
1064
        main_time += CLOCK_PERIOD / 2;
1065

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1066
1067
        //top->s_axis_tx_ptp_ts_96 = main_time;
        top->s_axis_tx_ptp_ts_valid = 1;
1068
        top->s_axis_rx_ptp_ts_valid = 1;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1069

1070
1071
1072
        top->eval();
    }
    report_outputs(top);
1073
    std::cout << std::endl << std::endl << "main_time:" << main_time << std::endl;
1074

1075
1076
1077
1078
#ifdef TRACE_ENABLED
    trace->dump(main_time + 1);
    trace->close();
#endif
1079
1080
1081
1082
    top->final();
    delete top;
    return 0;
}