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

Subversion Repositories altor32

[/] [altor32/] [trunk/] [rtl/] [sim/] [top.cpp] - Blame information for rev 27

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 ultra_embe
//-----------------------------------------------------------------
2
//                           AltOR32 
3
//                Alternative Lightweight OpenRisc 
4
//                            V2.0
5
//                     Ultra-Embedded.com
6
//                   Copyright 2011 - 2013
7
//
8
//               Email: admin@ultra-embedded.com
9
//
10
//                       License: LGPL
11
//-----------------------------------------------------------------
12
//
13
// Copyright (C) 2011 - 2013 Ultra-Embedded.com
14
//
15
// This source file may be used and distributed without         
16
// restriction provided that this copyright statement is not    
17
// removed from the file and that any derivative work contains  
18
// the original copyright notice and the associated disclaimer. 
19
//
20
// This source file is free software; you can redistribute it   
21
// and/or modify it under the terms of the GNU Lesser General   
22
// Public License as published by the Free Software Foundation; 
23
// either version 2.1 of the License, or (at your option) any   
24
// later version.
25
//
26
// This source is distributed in the hope that it will be       
27
// useful, but WITHOUT ANY WARRANTY; without even the implied   
28
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
29
// PURPOSE.  See the GNU Lesser General Public License for more 
30
// details.
31
//
32
// You should have received a copy of the GNU Lesser General    
33
// Public License along with this source; if not, write to the 
34
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
35
// Boston, MA  02111-1307  USA
36
//-----------------------------------------------------------------
37
#include <stdio.h>
38
#include <unistd.h>
39
#include <math.h>
40
#include <time.h>
41
 
42
#include "top_if.h"
43
 
44
#include "Vtop__Syms.h"
45
#include "verilated.h"
46
 
47
#if VM_TRACE
48
#include <verilated_vcd_c.h>
49
#endif
50
 
51
//-----------------------------------------------------------------
52
// Defines
53
//-----------------------------------------------------------------
54
#define MEMORY_START        0x10000000
55
#define MEMORY_SIZE         (1024 * 1024)
56
 
57
#define OR32_BUBBLE_OPCODE  0xFC000000
58
 
59
#define CPU_INSTANCE        top->v->u_cpu->u1_cpu
60
 
61
//-----------------------------------------------------------------
62
// Locals
63
//-----------------------------------------------------------------
64
static Vtop *top;
65
static unsigned int         _stop_pc = 0xFFFFFFFF;
66
 
67
#if VM_TRACE
68
static unsigned int        main_time = 0;
69
static VerilatedVcdC*      tfp;
70
#endif
71
 
72
//-----------------------------------------------------------------
73
// top_init
74
//-----------------------------------------------------------------
75
int top_init(void)
76
{
77
    top = new Vtop();
78
 
79
#if VM_TRACE                  
80
    // If verilator was invoked with --trace
81
    Verilated::traceEverOn(true);
82
    VL_PRINTF("Enabling GTKWave Trace Output...\n");
83
    tfp = new VerilatedVcdC;
84
    top->trace (tfp, 99);
85
    tfp->open ("wave_dump.vcd");
86
#endif
87
 
88
    // Initial
89
    top->clk_i = 0;
90
    top->rst_i = 1;
91
    top->intr_i = 0;
92
    top->eval();
93
 
94
    // Reset
95
    top->clk_i = 1;
96
    top->rst_i = 1;
97
    top->eval();
98
 
99
    top->clk_i = 0;
100
    top->rst_i = 0;
101
    top->eval();
102
 
103
    return 0;
104
}
105
 
106
//-----------------------------------------------------------------
107
// top_load
108
//-----------------------------------------------------------------
109
int top_load(unsigned int addr, unsigned char val)
110
{
111
    if (addr >= (MEMORY_SIZE - MEMORY_START))
112
        return -1;
113
 
114
    addr -= MEMORY_START;
115
 
116
    switch (addr & 0x3)
117
    {
118
    case 0:
119
        top->v->u_ram->u3->ram[addr >> 2] = val;
120
        break;
121
    case 1:
122
        top->v->u_ram->u2->ram[addr >> 2] = val;
123
        break;
124
    case 2:
125
        top->v->u_ram->u1->ram[addr >> 2] = val;
126
        break;
127
    case 3:
128
        top->v->u_ram->u0->ram[addr >> 2] = val;
129
        break;
130
    }
131
 
132
    return 0;
133
}
134
//-----------------------------------------------------------------
135
// top_setbreakpoint
136
//-----------------------------------------------------------------
137
int top_setbreakpoint(int bp, unsigned int pc)
138
{
139
    if (bp != 0)
140
        return -1;
141
    else
142
    {
143
        _stop_pc = pc;
144
        return 0;
145
    }
146
}
147
//-----------------------------------------------------------------
148
// top_run
149
//-----------------------------------------------------------------
150
int top_run(unsigned int pc, int cycles, int intr_after_cycles)
151
{
152
    int current_cycle = 0;
153
 
154
    // Run until fault or number of cycles completed
155
    while (!Verilated::gotFinish() && !top->fault_o && (current_cycle < cycles || cycles == -1))
156
    {
157
        // CLK->L
158
        top->clk_i = 0;
159
        top->eval();
160
 
161
#if VM_TRACE
162
        if (tfp) tfp->dump (main_time++);
163
#endif
164
 
165
        // CLK->H
166
        top->clk_i = 1;
167
        top->eval();
168
 
169
#if VM_TRACE
170
        if (tfp) tfp->dump (main_time++);
171
#endif
172
 
173
        // Get current executing instruction PC
174
        unsigned int pc = CPU_INSTANCE->u_exec->get_pc_ex();
175
        unsigned int opcode = CPU_INSTANCE->u_exec->get_opcode_ex();
176
 
177
#ifdef INST_TRACE
178
        // Instruction trace - decode instruction opcode to text
179
        if (opcode != OR32_BUBBLE_OPCODE)
180
            printf("%08x:   %02x %02x %02x %02x\n", pc, (opcode >> 24) & 0xFF, (opcode >> 16) & 0xFF, (opcode >> 8) & 0xFF, (opcode >> 0) & 0xFF);
181
#endif
182
 
183
        // Generate interrupt after a certain number of cycles?
184
        if (intr_after_cycles >= 0 && intr_after_cycles == current_cycle)
185
        {
186
            top->intr_i = 1;
187
            intr_after_cycles = -1;
188
        }
189
        else
190
            top->intr_i = 0;
191
 
192
        current_cycle++;
193
 
194
        // Breakpoint hit?
195
        if (_stop_pc == pc || top->break_o)
196
        {
197
            if (_stop_pc == pc)
198
                printf("Stopped at breakpoint 0x%x\n", _stop_pc);
199
            printf("Cycles = %d\n", current_cycle);
200
            return TOP_RES_BREAKPOINT;
201
        }
202
    }
203
 
204
    printf("Cycles = %d\n", current_cycle);
205
 
206
    // Fault
207
    if (top->fault_o)
208
    {
209
        printf("FAULT PC %x!\n", CPU_INSTANCE->u_exec->get_pc_ex());
210
        return TOP_RES_FAULT;
211
    }
212
    // Number of cycles reached
213
    else if (current_cycle >= cycles)
214
        return TOP_RES_MAX_CYCLES;
215
    // No error
216
    else
217
        return TOP_RES_OK;
218
}
219
//-----------------------------------------------------------------
220
// top_done
221
//-----------------------------------------------------------------
222
void top_done(void)
223
{
224
    top->final();
225
#if VM_TRACE
226
    if (tfp)
227
    {
228
        tfp->close();
229
        tfp = NULL;
230
    }
231
#endif
232
}

powered by: WebSVN 2.1.0

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