OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [cpu_sysc_plugin/] [riverlib/] [core/] [proc.cpp] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2016 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      CPU pipeline implementation.
6
 */
7
 
8
#include "proc.h"
9
#include "api_utils.h"
10
 
11
namespace debugger {
12
 
13
Processor::Processor(sc_module_name name_) : sc_module(name_) {
14
 
15
    SC_METHOD(comb);
16
    sensitive << i_nrst;
17
    sensitive << i_resp_ctrl_valid;
18
    sensitive << w.f.pipeline_hold;
19
    sensitive << w.e.valid;
20
    sensitive << w.e.pipeline_hold;
21
    sensitive << w.m.pipeline_hold;
22
    sensitive << w.f.imem_req_valid;
23
    sensitive << w.f.imem_req_addr;
24
    sensitive << w.f.valid;
25
    sensitive << dbg.clock_cnt;
26
    sensitive << dbg.executed_cnt;
27
    sensitive << dbg.core_addr;
28
    sensitive << dbg.halt;
29
    sensitive << dbg.core_wdata;
30
 
31
    SC_METHOD(negedge_dbg_print);
32
    sensitive << i_clk.neg();
33
 
34
    fetch0 = new InstrFetch("fetch0");
35
    fetch0->i_clk(i_clk);
36
    fetch0->i_nrst(i_nrst);
37
    fetch0->i_pipeline_hold(w_fetch_pipeline_hold);
38
    fetch0->i_mem_req_ready(i_req_ctrl_ready);
39
    fetch0->o_mem_addr_valid(w.f.imem_req_valid);
40
    fetch0->o_mem_addr(w.f.imem_req_addr);
41
    fetch0->i_mem_data_valid(i_resp_ctrl_valid);
42
    fetch0->i_mem_data_addr(i_resp_ctrl_addr);
43
    fetch0->i_mem_data(i_resp_ctrl_data);
44
    fetch0->o_mem_resp_ready(o_resp_ctrl_ready);
45
    fetch0->i_e_npc(w.e.npc);
46
    fetch0->i_predict_npc(wb_npc_predict);
47
    fetch0->o_predict_miss(w.f.predict_miss);
48
    fetch0->o_mem_req_fire(w.f.req_fire);
49
    fetch0->o_valid(w.f.valid);
50
    fetch0->o_pc(w.f.pc);
51
    fetch0->o_instr(w.f.instr);
52
    fetch0->o_hold(w.f.pipeline_hold);
53
    fetch0->i_br_fetch_valid(dbg.br_fetch_valid);
54
    fetch0->i_br_address_fetch(dbg.br_address_fetch);
55
    fetch0->i_br_instr_fetch(dbg.br_instr_fetch);
56
    fetch0->o_instr_buf(w.f.instr_buf);
57
 
58
    dec0 = new InstrDecoder("dec0");
59
    dec0->i_clk(i_clk);
60
    dec0->i_nrst(i_nrst);
61
    dec0->i_any_hold(w_any_pipeline_hold);
62
    dec0->i_f_valid(w.f.valid);
63
    dec0->i_f_pc(w.f.pc);
64
    dec0->i_f_instr(w.f.instr);
65
    dec0->o_valid(w.d.instr_valid);
66
    dec0->o_pc(w.d.pc);
67
    dec0->o_instr(w.d.instr);
68
    dec0->o_memop_store(w.d.memop_store);
69
    dec0->o_memop_load(w.d.memop_load);
70
    dec0->o_memop_sign_ext(w.d.memop_sign_ext);
71
    dec0->o_memop_size(w.d.memop_size);
72
    dec0->o_unsigned_op(w.d.unsigned_op);
73
    dec0->o_rv32(w.d.rv32);
74
    dec0->o_isa_type(w.d.isa_type);
75
    dec0->o_instr_vec(w.d.instr_vec);
76
    dec0->o_exception(w.d.exception);
77
 
78
    exec0 = new InstrExecute("exec0");
79
    exec0->i_clk(i_clk);
80
    exec0->i_nrst(i_nrst);
81
    exec0->i_pipeline_hold(w_exec_pipeline_hold);
82
    exec0->i_d_valid(w.d.instr_valid);
83
    exec0->i_d_pc(w.d.pc);
84
    exec0->i_d_instr(w.d.instr);
85
    exec0->i_wb_done(w.m.valid);
86
    exec0->i_memop_store(w.d.memop_store);
87
    exec0->i_memop_load(w.d.memop_load);
88
    exec0->i_memop_sign_ext(w.d.memop_sign_ext);
89
    exec0->i_memop_size(w.d.memop_size);
90
    exec0->i_unsigned_op(w.d.unsigned_op);
91
    exec0->i_rv32(w.d.rv32);
92
    exec0->i_isa_type(w.d.isa_type);
93
    exec0->i_ivec(w.d.instr_vec);
94
    exec0->i_ie(csr.ie);
95
    exec0->i_mtvec(csr.mtvec);
96
    exec0->i_mode(csr.mode);
97
    exec0->i_break_mode(dbg.break_mode);
98
    exec0->i_unsup_exception(w.d.exception);
99
    exec0->i_ext_irq(i_ext_irq);
100
    exec0->i_dport_npc_write(dbg.npc_write);
101
    exec0->i_dport_npc(wb_exec_dport_npc);
102
    exec0->o_radr1(w.e.radr1);
103
    exec0->i_rdata1(ireg.rdata1);
104
    exec0->o_radr2(w.e.radr2);
105
    exec0->i_rdata2(ireg.rdata2);
106
    exec0->o_res_addr(w.e.res_addr);
107
    exec0->o_res_data(w.e.res_data);
108
    exec0->o_pipeline_hold(w.e.pipeline_hold);
109
    exec0->o_xret(w.e.xret);
110
    exec0->o_csr_addr(w.e.csr_addr);
111
    exec0->o_csr_wena(w.e.csr_wena);
112
    exec0->i_csr_rdata(csr.rdata);
113
    exec0->o_csr_wdata(w.e.csr_wdata);
114
    exec0->o_trap_ena(w.e.trap_ena);
115
    exec0->o_trap_code(w.e.trap_code);
116
    exec0->o_trap_pc(w.e.trap_pc);
117
    exec0->o_memop_sign_ext(w.e.memop_sign_ext);
118
    exec0->o_memop_load(w.e.memop_load);
119
    exec0->o_memop_store(w.e.memop_store);
120
    exec0->o_memop_size(w.e.memop_size);
121
    exec0->o_memop_addr(w.e.memop_addr);
122
    exec0->o_valid(w.e.valid);
123
    exec0->o_pc(w.e.pc);
124
    exec0->o_npc(w.e.npc);
125
    exec0->o_instr(w.e.instr);
126
    exec0->o_breakpoint(w.e.breakpoint);
127
    exec0->o_call(w.e.call);
128
    exec0->o_ret(w.e.ret);
129
 
130
    mem0 = new MemAccess("mem0");
131
    mem0->i_clk(i_clk);
132
    mem0->i_nrst(i_nrst);
133
    mem0->i_e_valid(w.e.valid);
134
    mem0->i_e_pc(w.e.pc);
135
    mem0->i_e_instr(w.e.instr);
136
    mem0->i_res_addr(w.e.res_addr);
137
    mem0->i_res_data(w.e.res_data);
138
    mem0->i_memop_sign_ext(w.e.memop_sign_ext);
139
    mem0->i_memop_load(w.e.memop_load);
140
    mem0->i_memop_store(w.e.memop_store);
141
    mem0->i_memop_size(w.e.memop_size);
142
    mem0->i_memop_addr(w.e.memop_addr);
143
    mem0->o_waddr(w.w.waddr);
144
    mem0->o_wena(w.w.wena);
145
    mem0->o_wdata(w.w.wdata);
146
    mem0->i_mem_req_ready(i_req_data_ready);
147
    mem0->o_mem_valid(o_req_data_valid);
148
    mem0->o_mem_write(o_req_data_write);
149
    mem0->o_mem_sz(o_req_data_size);
150
    mem0->o_mem_addr(o_req_data_addr);
151
    mem0->o_mem_data(o_req_data_data);
152
    mem0->i_mem_data_valid(i_resp_data_valid);
153
    mem0->i_mem_data_addr(i_resp_data_addr);
154
    mem0->i_mem_data(i_resp_data_data);
155
    mem0->o_mem_resp_ready(o_resp_data_ready);
156
    mem0->o_hold(w.m.pipeline_hold);
157
    mem0->o_valid(w.m.valid);
158
    mem0->o_pc(w.m.pc);
159
    mem0->o_instr(w.m.instr);
160
 
161
    predic0 = new BranchPredictor("predic0");
162
    predic0->i_clk(i_clk);
163
    predic0->i_nrst(i_nrst);
164
    predic0->i_req_mem_fire(w.f.req_fire);
165
    predic0->i_resp_mem_valid(i_resp_ctrl_valid);
166
    predic0->i_resp_mem_addr(i_resp_ctrl_addr);
167
    predic0->i_resp_mem_data(i_resp_ctrl_data);
168
    predic0->i_f_predic_miss(w.f.predict_miss);
169
    predic0->i_e_npc(w.e.npc);
170
    predic0->i_ra(ireg.ra);
171
    predic0->o_npc_predict(wb_npc_predict);
172
 
173
 
174
    iregs0 = new RegIntBank("iregs0");
175
    iregs0->i_clk(i_clk);
176
    iregs0->i_nrst(i_nrst);
177
    iregs0->i_radr1(w.e.radr1);
178
    iregs0->o_rdata1(ireg.rdata1);
179
    iregs0->i_radr2(w.e.radr2);
180
    iregs0->o_rdata2(ireg.rdata2);
181
    iregs0->i_waddr(w.w.waddr);
182
    iregs0->i_wena(w.w.wena);
183
    iregs0->i_wdata(w.w.wdata);
184
    iregs0->i_dport_addr(wb_ireg_dport_addr);
185
    iregs0->i_dport_ena(dbg.ireg_ena);
186
    iregs0->i_dport_write(dbg.ireg_write);
187
    iregs0->i_dport_wdata(dbg.core_wdata);
188
    iregs0->o_dport_rdata(ireg.dport_rdata);
189
 
190
    iregs0->o_ra(ireg.ra);   // Return address
191
 
192
    csr0 = new CsrRegs("csr0");
193
    csr0->i_clk(i_clk);
194
    csr0->i_nrst(i_nrst);
195
    csr0->i_xret(w.e.xret);
196
    csr0->i_addr(w.e.csr_addr);
197
    csr0->i_wena(w.e.csr_wena);
198
    csr0->i_wdata(w.e.csr_wdata);
199
    csr0->o_rdata(csr.rdata);
200
    csr0->i_break_mode(dbg.break_mode);
201
    csr0->i_breakpoint(w.e.breakpoint);
202
    csr0->i_trap_ena(w.e.trap_ena);
203
    csr0->i_trap_code(w.e.trap_code);
204
    csr0->i_trap_pc(w.e.trap_pc);
205
    csr0->o_ie(csr.ie);
206
    csr0->o_mode(csr.mode);
207
    csr0->o_mtvec(csr.mtvec);
208
    csr0->i_dport_ena(dbg.csr_ena);
209
    csr0->i_dport_write(dbg.csr_write);
210
    csr0->i_dport_addr(dbg.core_addr);
211
    csr0->i_dport_wdata(dbg.core_wdata);
212
    csr0->o_dport_rdata(csr.dport_rdata);
213
 
214
    dbg0 = new DbgPort("dbg0");
215
    dbg0->i_clk(i_clk);
216
    dbg0->i_nrst(i_nrst);
217
    dbg0->i_dport_valid(i_dport_valid);
218
    dbg0->i_dport_write(i_dport_write);
219
    dbg0->i_dport_region(i_dport_region);
220
    dbg0->i_dport_addr(i_dport_addr);
221
    dbg0->i_dport_wdata(i_dport_wdata);
222
    dbg0->o_dport_ready(o_dport_ready);
223
    dbg0->o_dport_rdata(o_dport_rdata);
224
    dbg0->o_core_addr(dbg.core_addr);
225
    dbg0->o_core_wdata(dbg.core_wdata);
226
    dbg0->o_csr_ena(dbg.csr_ena);
227
    dbg0->o_csr_write(dbg.csr_write);
228
    dbg0->i_csr_rdata(csr.dport_rdata);
229
    dbg0->o_ireg_ena(dbg.ireg_ena);
230
    dbg0->o_ireg_write(dbg.ireg_write);
231
    dbg0->o_npc_write(dbg.npc_write);
232
    dbg0->i_ireg_rdata(ireg.dport_rdata);
233
    dbg0->i_pc(w.e.pc);
234
    dbg0->i_npc(w.e.npc);
235
    dbg0->i_e_call(w.e.call);
236
    dbg0->i_e_ret(w.e.ret);
237
    dbg0->i_e_valid(w.e.valid);
238
    dbg0->i_m_valid(w.m.valid);
239
    dbg0->o_clock_cnt(dbg.clock_cnt);
240
    dbg0->o_executed_cnt(dbg.executed_cnt);
241
    dbg0->o_halt(dbg.halt);
242
    dbg0->i_ebreak(w.e.breakpoint);
243
    dbg0->o_break_mode(dbg.break_mode);
244
    dbg0->o_br_fetch_valid(dbg.br_fetch_valid);
245
    dbg0->o_br_address_fetch(dbg.br_address_fetch);
246
    dbg0->o_br_instr_fetch(dbg.br_instr_fetch);
247
    dbg0->i_istate(i_istate);
248
    dbg0->i_dstate(i_dstate);
249
    dbg0->i_cstate(i_cstate);
250
    dbg0->i_instr_buf(w.f.instr_buf);
251
 
252
    reg_dbg = 0;
253
    mem_dbg = 0;
254
};
255
 
256
Processor::~Processor() {
257
    delete fetch0;
258
    delete dec0;
259
    delete exec0;
260
    delete mem0;
261
    delete predic0;
262
    delete iregs0;
263
    delete csr0;
264
    delete dbg0;
265
    if (reg_dbg) {
266
        reg_dbg->close();
267
        delete reg_dbg;
268
    }
269
    if (mem_dbg) {
270
        mem_dbg->close();
271
        delete mem_dbg;
272
    }
273
}
274
 
275
void Processor::generateVCD(sc_trace_file *i_vcd, sc_trace_file *o_vcd) {
276
    if (o_vcd) {
277
        sc_trace(o_vcd, dbg.clock_cnt, "top/dbg_clock_cnt");
278
        sc_trace(o_vcd, dbg.executed_cnt, "top/dbg_executed_cnt");
279
    }
280
    predic0->generateVCD(i_vcd, o_vcd);
281
    csr0->generateVCD(i_vcd, o_vcd);
282
    dbg0->generateVCD(i_vcd, o_vcd);
283
    dec0->generateVCD(i_vcd, o_vcd);
284
    exec0->generateVCD(i_vcd, o_vcd);
285
    fetch0->generateVCD(i_vcd, o_vcd);
286
    mem0->generateVCD(i_vcd, o_vcd);
287
    iregs0->generateVCD(i_vcd, o_vcd);
288
}
289
 
290
 
291
void Processor::comb() {
292
 
293
    w_fetch_pipeline_hold = w.e.pipeline_hold | w.m.pipeline_hold | dbg.halt;
294
    w_any_pipeline_hold = w.f.pipeline_hold | w.e.pipeline_hold
295
                        | w.m.pipeline_hold | dbg.halt;
296
    w_exec_pipeline_hold = w.f.pipeline_hold | w.m.pipeline_hold | dbg.halt;
297
 
298
    wb_ireg_dport_addr = dbg.core_addr.read()(4, 0);
299
    wb_exec_dport_npc = dbg.core_wdata.read()(BUS_ADDR_WIDTH-1, 0);
300
 
301
    o_req_ctrl_valid = w.f.imem_req_valid;
302
    o_req_ctrl_addr = w.f.imem_req_addr;
303
    if (generate_ref_) {
304
        o_time = dbg.executed_cnt;
305
    } else {
306
        o_time = dbg.clock_cnt;
307
    }
308
}
309
 
310
void Processor::generateRef(bool v) {
311
    generate_ref_ = v;
312
    if (generate_ref_) {
313
        reg_dbg = new ofstream("river_sysc_regs.log");
314
        mem_dbg = new ofstream("river_sysc_mem.log");
315
        mem_dbg_write_flag = false;
316
    }
317
}
318
 
319
void Processor::negedge_dbg_print() {
320
    if (!generate_ref_) {
321
        return;
322
    }
323
    int sz;
324
    if (w.m.valid.read()) {
325
        uint64_t line_cnt = dbg.executed_cnt.read() + 1;
326
        sz = RISCV_sprintf(tstr, sizeof(tstr), "%8" RV_PRI64 "d [%08x] %08x: ",
327
            line_cnt,
328
            w.m.pc.read().to_int(),
329
            w.m.instr.read().to_int());
330
        uint64_t prev_val = iregs0->r.mem[w.w.waddr.read().to_int()].to_int64();
331
        uint64_t cur_val = w.w.wdata.read().to_int64();
332
        if (w.w.waddr.read() == 0 || prev_val == cur_val) {
333
            // not writing
334
            sz += RISCV_sprintf(&tstr[sz], sizeof(tstr) - sz, "%s", "-\n");
335
        } else {
336
            sz += RISCV_sprintf(&tstr[sz], sizeof(tstr) - sz,
337
                   "%3s <= %016" RV_PRI64 "x\n",
338
                   IREGS_NAMES[w.w.waddr.read().to_int()], cur_val);
339
        }
340
 
341
        (*reg_dbg) << tstr;
342
        reg_dbg->flush();
343
    }
344
    // Memory access debug:
345
    if (i_resp_data_valid.read()) {
346
        sz = RISCV_sprintf(tstr, sizeof(tstr), "%08x: [%08x] ",
347
                        w.m.pc.read().to_uint(),
348
                        i_resp_data_addr.read().to_uint());
349
        if (mem_dbg_write_flag) {
350
            sz += RISCV_sprintf(&tstr[sz], sizeof(tstr) - sz,
351
                "<= %016" RV_PRI64 "x\n",
352
                dbg_mem_write_value & dbg_mem_value_mask);
353
        } else {
354
            sz += RISCV_sprintf(&tstr[sz], sizeof(tstr) - sz,
355
                "=> %016" RV_PRI64 "x\n",
356
                i_resp_data_data.read().to_uint64() & dbg_mem_value_mask);
357
        }
358
        (*mem_dbg) << tstr;
359
        mem_dbg->flush();
360
    }
361
    if (w.e.memop_store.read() || w.e.memop_load.read()) {
362
        mem_dbg_write_flag = w.e.memop_store;
363
        if (mem_dbg_write_flag) {
364
            dbg_mem_write_value = w.e.res_data.read();
365
        }
366
        switch (w.e.memop_size.read()) {
367
        case 0: dbg_mem_value_mask = 0xFFull; break;
368
        case 1: dbg_mem_value_mask = 0xFFFFull; break;
369
        case 2: dbg_mem_value_mask = 0xFFFFFFFFull; break;
370
        default: dbg_mem_value_mask = ~0ull;
371
        }
372
    }
373
}
374
 
375
 
376
}  // namespace debugger
377
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.