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

Subversion Repositories aor3000

[/] [aor3000/] [trunk/] [sim/] [vmips/] [main_linux.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/*
2
 * This file is subject to the terms and conditions of the GPL License. See
3
 * the file "LICENSE" in the main directory of this archive for more details.
4
 *
5
 * Copyright (C) 2014 Aleksander Osman
6
 */
7
 
8
#include <cstdio>
9
#include <cstdlib>
10
#include <cstring>
11
 
12
#include <sys/mman.h>
13
#include <sys/types.h>
14
#include <sys/stat.h>
15
#include <fcntl.h>
16
#include <unistd.h>
17
 
18
#include "shared_mem.h"
19
#include "vmips_emulator.h"
20
 
21
//------------------------------------------------------------------------------
22
 
23
volatile shared_mem_t *shared_ptr = NULL;
24
 
25
//------------------------------------------------------------------------------
26
 
27
void CPZero::initialize() {
28
}
29
 
30
void CPU::initialize() {
31
}
32
 
33
//------------------------------------------------------------------------------
34
 
35
void CPZero::report() {
36
}
37
 
38
void CPU::report() {
39
}
40
 
41
//------------------------------------------------------------------------------
42
 
43
CPU *cpu = NULL;
44
uint32 event_counter = 0;
45
 
46
void usleep_or_finish() {
47
    if(shared_ptr->test_finished) {
48
        printf("Finishing.\n");
49
        exit(0);
50
    }
51
    usleep(1);
52
}
53
 
54
//128MB
55
#define MAX_MEMORY   0x08000000
56
#define RESET_VECTOR 0x1FC00000
57
 
58
uint32 isolated_cache[512];
59
 
60
uint32 ao_interrupts() {
61
    return ((event_counter >= shared_ptr->irq2_at_event)? 1 << 10 : 0) | ((event_counter >= shared_ptr->irq3_at_event)? 2 << 10 : 0);
62
}
63
 
64
uint8 ao_fetch_byte(uint32 addr, bool cacheable, bool isolated) {
65
    //DBE IBE
66
    //cpu->exception((mode == INSTFETCH / DATALOAD ? IBE : DBE), mode);
67
 
68
    if(isolated) return
69
        ((addr %4) == 0)?   ((isolated_cache[(addr >> 2)&0x1FF] >> 0) & 0xFF) :
70
        ((addr %4) == 1)?   ((isolated_cache[(addr >> 2)&0x1FF] >> 8) & 0xFF) :
71
        ((addr %4) == 2)?   ((isolated_cache[(addr >> 2)&0x1FF] >> 16) & 0xFF) :
72
                            ((isolated_cache[(addr >> 2)&0x1FF] >> 24) & 0xFF);
73
 
74
    if(addr < MAX_MEMORY) {
75
        return shared_ptr->mem.bytes[addr];
76
    }
77
 
78
    shared_ptr->proc_vmips.read_address    = addr & 0xFFFFFFFC;
79
    shared_ptr->proc_vmips.read_byteenable = ((addr % 4) == 0)? 0x1 : ((addr % 4) == 1)? 0x2 : ((addr % 4) == 2)? 0x3 : 0x4;
80
    shared_ptr->proc_vmips.read_do         = true;
81
 
82
    while(shared_ptr->proc_vmips.read_do) usleep_or_finish();
83
 
84
    return (shared_ptr->proc_vmips.read_data >> ( ((addr % 4) == 0)? 0 : ((addr % 4) == 1)? 8 : ((addr % 4) == 2)? 16 : 24 )) & 0xFF;
85
}
86
 
87
uint16 ao_fetch_halfword(uint32 addr, bool cacheable, bool isolated) {
88
    //AdE
89
    if (addr % 2 != 0) {
90
        cpu->exception(AdEL,DATALOAD);
91
        return 0xffff;
92
    }
93
 
94
    //DBE IBE
95
    //cpu->exception((mode == INSTFETCH / DATALOAD ? IBE : DBE), mode);
96
 
97
    if(isolated) return
98
        ((addr %4) == 0)?   ((isolated_cache[(addr >> 2)&0x1FF] >> 0) & 0xFFFF) :
99
                            ((isolated_cache[(addr >> 2)&0x1FF] >> 16) & 0xFFFF);
100
 
101
    if(addr < MAX_MEMORY) {
102
        return shared_ptr->mem.shorts[addr/2];
103
    }
104
 
105
    shared_ptr->proc_vmips.read_address    = addr & 0xFFFFFFFC;
106
    shared_ptr->proc_vmips.read_byteenable = ((addr % 4) == 0)? 0x3 : 0xC;
107
    shared_ptr->proc_vmips.read_do         = true;
108
 
109
    while(shared_ptr->proc_vmips.read_do) usleep_or_finish();
110
 
111
    return (shared_ptr->proc_vmips.read_data >> ( ((addr % 4) == 0)? 0 : 16 )) & 0xFFFF;
112
}
113
 
114
uint32 ao_fetch_word(uint32 addr, int32 mode, bool cacheable, bool isolated) {
115
    //AdE
116
    if (addr % 4 != 0) {
117
        cpu->exception(AdEL,mode);
118
        return 0xffffffff;
119
    }
120
 
121
    //DBE IBE
122
    //cpu->exception((mode == INSTFETCH / DATALOAD ? IBE : DBE), mode);
123
 
124
    if(isolated && mode == DATALOAD) return ((isolated_cache[(addr >> 2)&0x1FF] >> 0) & 0xFFFFFFFF);
125
 
126
    if(addr < MAX_MEMORY) {
127
        return shared_ptr->mem.ints[addr/4];
128
    }
129
    else if(addr >= RESET_VECTOR && addr < RESET_VECTOR + sizeof(shared_ptr->reset_vector)) {
130
        return shared_ptr->reset_vector[(addr - RESET_VECTOR)/4];
131
    }
132
 
133
    shared_ptr->proc_vmips.read_address    = addr & 0xFFFFFFFC;
134
    shared_ptr->proc_vmips.read_byteenable = 0xF;
135
    shared_ptr->proc_vmips.read_do         = true;
136
 
137
    while(shared_ptr->proc_vmips.read_do) usleep_or_finish();
138
 
139
    return (shared_ptr->proc_vmips.read_data) & 0xFFFFFFFF;
140
}
141
 
142
void ao_store_byte(uint32 addr, uint8 data, bool cacheable, bool isolated) {
143
    //DBE
144
    //cpu->exception((mode == INSTFETCH / DATALOAD ? IBE : DBE), mode);
145
 
146
    if(isolated) return;
147
 
148
    shared_ptr->proc_vmips.write_address    = addr & 0xFFFFFFFC;
149
    shared_ptr->proc_vmips.write_byteenable = ((addr % 4) == 0)? 0x1 : ((addr % 4) == 1)? 0x2 : ((addr % 4) == 2)? 0x4 : 0x8;
150
    shared_ptr->proc_vmips.write_data       = ((addr % 4) == 0)? data : ((addr % 4) == 1)? data << 8 : ((addr % 4) == 2)? data << 16 : data << 24;
151
    shared_ptr->proc_vmips.write_do         = true;
152
 
153
    while(shared_ptr->proc_vmips.write_do) usleep_or_finish();
154
}
155
 
156
void ao_store_halfword(uint32 addr, uint16 data, bool cacheable, bool isolated) {
157
    //AdE
158
    if (addr % 2 != 0) {
159
        cpu->exception(AdES,DATASTORE);
160
        return;
161
    }
162
 
163
    //DBE
164
    //cpu->exception((mode == INSTFETCH / DATALOAD ? IBE : DBE), mode);
165
 
166
    if(isolated) return;
167
 
168
    shared_ptr->proc_vmips.write_address    = addr & 0xFFFFFFFC;
169
    shared_ptr->proc_vmips.write_byteenable = ((addr % 4) == 0)? 0x3 : 0xC;
170
    shared_ptr->proc_vmips.write_data       = ((addr % 4) == 0)? data : data << 16;
171
    shared_ptr->proc_vmips.write_do         = true;
172
 
173
    while(shared_ptr->proc_vmips.write_do) usleep_or_finish();
174
}
175
 
176
void ao_store_word(uint32 addr, uint32 data, bool cacheable, bool isolated, uint32 byteenable) {
177
    //AdE
178
    if (addr % 4 != 0) {
179
        cpu->exception(AdES,DATASTORE);
180
        return;
181
    }
182
 
183
    //DBE
184
    //cpu->exception((mode == INSTFETCH / DATALOAD ? IBE : DBE), mode);
185
 
186
    if(isolated) {
187
        isolated_cache[(addr >> 2)&0x1FF] = data;
188
        return;
189
    }
190
 
191
    shared_ptr->proc_vmips.write_address    = addr & 0xFFFFFFFC;
192
    shared_ptr->proc_vmips.write_byteenable = byteenable;
193
    shared_ptr->proc_vmips.write_data       = data;
194
    shared_ptr->proc_vmips.write_do         = true;
195
 
196
    while(shared_ptr->proc_vmips.write_do) usleep_or_finish();
197
}
198
 
199
void fatal_error(const char *error, ...) {
200
    printf("[fatal_error]: %s\n", error);
201
    exit(-1);
202
}
203
 
204
//------------------------------------------------------------------------------
205
 
206
 
207
int main() {
208
    //map shared memory
209
    int fd = open("./../tester/shared_mem.dat", O_RDWR, S_IRUSR | S_IWUSR);
210
 
211
    if(fd == -1) {
212
        perror("open() failed for shared_mem.dat");
213
        return -1;
214
    }
215
 
216
    shared_ptr = (shared_mem_t *)mmap(NULL, sizeof(shared_mem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
217
 
218
    if(shared_ptr == MAP_FAILED) {
219
        perror("mmap() failed");
220
        close(fd);
221
        return -2;
222
    }
223
 
224
    cpu = new CPU();
225
    cpu->reset();
226
 
227
    printf("Waiting for initialize..."); fflush(stdout);
228
    while(shared_ptr->proc_vmips.initialize_do == false) usleep_or_finish();
229
 
230
    cpu->initialize();
231
    shared_ptr->proc_vmips.initialize_do = false;
232
    printf("done\n");
233
 
234
    while(true) {
235
        bool do_debug = false;//event_counter > 40565500;
236
 
237
        int exception_pending = cpu->step(do_debug);
238
        fflush(stdout);
239
 
240
        shared_ptr->proc_vmips.report.counter = event_counter;
241
 
242
        if(shared_ptr->check_at_event == event_counter) {
243
            shared_ptr->proc_vmips.check_do = true;
244
 
245
            while(shared_ptr->proc_vmips.check_do) usleep_or_finish();
246
        }
247
 
248
        event_counter++;
249
    }
250
    return 0;
251
}
252
 
253
//------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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