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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [sim/] [sim_pc/] [main.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
#include <cstdio>
2
#include <cstring>
3
 
4
#include <sys/mman.h>
5
#include <sys/types.h>
6
#include <sys/stat.h>
7
#include <fcntl.h>
8
#include <unistd.h>
9
 
10
#include "shared_mem.h"
11
 
12
volatile shared_mem_t *shared_ptr = NULL;
13
 
14
int load_file(const char *name, int byte_location) {
15
    FILE *fp = fopen(name, "rb");
16
    if(fp == NULL) {
17
        return -1;
18
    }
19
 
20
    int int_ret = fseek(fp, 0, SEEK_END);
21
    if(int_ret != 0) {
22
        fclose(fp);
23
        return -2;
24
    }
25
 
26
    long size = ftell(fp);
27
    rewind(fp);
28
 
29
    int_ret = fread((void *)&shared_ptr->mem.bytes[byte_location], size, 1, fp);
30
    if(int_ret != 1) {
31
        fclose(fp);
32
        return -3;
33
    }
34
    fclose(fp);
35
 
36
    return 0;
37
}
38
 
39
int main(int argc, char **argv) {
40
 
41
    int int_ret;
42
 
43
    //open file with truncate
44
    FILE *fp = fopen("shared_mem.dat", "wb");
45
    if(fp == NULL) {
46
        perror("Can not truncate file shared_mem.dat");
47
        return -1;
48
    }
49
    uint8 *buf = new uint8[sizeof(shared_mem_t)];
50
    memset(buf, 0, sizeof(shared_mem_t));
51
 
52
    int_ret = fwrite(buf, sizeof(shared_mem_t), 1, fp);
53
    delete buf;
54
    if(int_ret != 1) {
55
        perror("Can not zero-fill file shared_mem.dat");
56
        fclose(fp);
57
        return -2;
58
    }
59
    fclose(fp);
60
 
61
    int fd = open("shared_mem.dat", O_RDWR, S_IRUSR | S_IWUSR);
62
 
63
    if(fd == -1) {
64
        perror("open() failed for shared_mem.dat");
65
        return -1;
66
    }
67
 
68
    shared_ptr = (shared_mem_t *)mmap(NULL, sizeof(shared_mem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
69
 
70
    if(shared_ptr == MAP_FAILED) {
71
        perror("mmap() failed");
72
        close(fd);
73
        return -2;
74
    }
75
 
76
    //load bios
77
    int_ret = load_file("./../../sd/bios/bochs_legacy", 0xF0000);
78
    if(int_ret != 0) {
79
        perror("Can not load bios file");
80
        return -3;
81
    }
82
 
83
    //load vgabios
84
    int_ret = load_file("./../../sd/vgabios/vgabios_lgpl", 0xC0000);
85
    if(int_ret != 0) {
86
        perror("Can not load bios file");
87
        return -3;
88
    }
89
 
90
    //--------------------------------------------------------------------------
91
 
92
/*
93
    while(true) {
94
        if(shared_ptr->bochs486_pc.starting == STEP_REQ) {
95
            printf("Starting bochs486_pc.\n");
96
            shared_ptr->bochs486_pc.starting = STEP_ACK;
97
            break;
98
        }
99
    }
100
*/
101
    while(true) {
102
        if(shared_ptr->ao486.starting == STEP_REQ) {
103
            printf("Starting ao486.\n");
104
            shared_ptr->ao486.starting = STEP_ACK;
105
            break;
106
        }
107
    }
108
 
109
/*
110
    while(true) {
111
        if(shared_ptr->bochsDevs_starting == STEP_REQ) {
112
            printf("Starting bochsDevs.\n");
113
            shared_ptr->bochsDevs_starting = STEP_ACK;
114
            break;
115
        }
116
    }
117
*/
118
    //--------------------------------------------------------------------------
119
 
120
    uint32 ctrl_io_read  = 0;
121
    uint32 ctrl_io_write = 0;
122
 
123
    uint32 ctrl_mem_read  = 0;
124
    uint32 ctrl_mem_write = 0;
125
 
126
    uint32 bochs486_pc_only = 0;
127
    uint32 ao486_only = 1;
128
 
129
    FILE *fp_stop = NULL;
130
    uint32 bochs486_stopped = 0;
131
 
132
    FILE *debug_fp = fopen("output.txt", "w");
133
 
134
    while(true) {
135
 
136
        //---------------------------------------------------------------------- stop control
137
 
138
        if(bochs486_stopped == 0) {
139
            if(fp_stop == NULL) fp_stop = fopen("ctrl_stop.do", "rb");
140
            if(fp_stop != NULL) {
141
 
142
                if(shared_ptr->bochs486_pc.stop == STEP_IDLE) {
143
                    shared_ptr->bochs486_pc.stop = STEP_REQ;
144
                }
145
 
146
                if(shared_ptr->bochs486_pc.stop == STEP_ACK) {
147
                    if(shared_ptr->bochs486_pc.instr_counter <= shared_ptr->interrupt_at_counter + 10) {
148
                        printf("Stop failed ..\n");
149
                        shared_ptr->bochs486_pc.stop = STEP_IDLE;
150
                        usleep(100000);
151
                    }
152
                    else {
153
                        printf("Stop ok.\n");
154
                        fclose(fp_stop);
155
                        fp_stop = NULL;
156
                        bochs486_stopped = 1;
157
                    }
158
                }
159
            }
160
        }
161
        if(bochs486_stopped == 1) {
162
            if(fp_stop == NULL) fp_stop = fopen("ctrl_stop.do", "rb");
163
            if(fp_stop != NULL) {
164
                fclose(fp_stop);
165
                fp_stop = NULL;
166
            }
167
            else {
168
                bochs486_stopped = 0;
169
 
170
                //wait for ack from ao486
171
                shared_ptr->ao486.instr_counter = shared_ptr->bochs486_pc.instr_counter;
172
 
173
                while(true) {
174
                    if(shared_ptr->ao486.starting == STEP_REQ) {
175
                        printf("Starting ao486.\n");
176
                        shared_ptr->ao486.starting = STEP_ACK;
177
                        break;
178
                    }
179
                }
180
 
181
                shared_ptr->bochs486_pc.stop = STEP_IDLE;
182
 
183
                bochs486_pc_only = 0;
184
            }
185
 
186
        }
187
 
188
        //---------------------------------------------------------------------- control io read
189
 
190
        if(ctrl_io_read == 0) {
191
            if( (bochs486_pc_only && shared_ptr->bochs486_pc.io_step == STEP_REQ && shared_ptr->bochs486_pc.io_is_write == 0) ||
192
                (ao486_only       && shared_ptr->ao486.io_step == STEP_REQ       && shared_ptr->ao486.io_is_write == 0) || (
193
                shared_ptr->ao486.io_step == STEP_REQ && shared_ptr->bochs486_pc.io_step == STEP_REQ && shared_ptr->ao486.io_is_write == 0 &&
194
                    shared_ptr->ao486.io_address    == shared_ptr->bochs486_pc.io_address &&
195
                    shared_ptr->ao486.io_byteenable == shared_ptr->bochs486_pc.io_byteenable &&
196
                    shared_ptr->ao486.io_is_write   == shared_ptr->bochs486_pc.io_is_write &&
197
                    shared_ptr->ao486.io_step       == shared_ptr->bochs486_pc.io_step ) )
198
            {
199
                ctrl_io_read = 1;
200
 
201
                shared_ptr->combined.io_address    = (ao486_only)? shared_ptr->ao486.io_address : shared_ptr->bochs486_pc.io_address;
202
                shared_ptr->combined.io_byteenable = (ao486_only)? shared_ptr->ao486.io_byteenable : shared_ptr->bochs486_pc.io_byteenable;
203
                shared_ptr->combined.io_is_write   = 0;
204
                shared_ptr->combined.io_step       = STEP_REQ;
205
            }
206
        }
207
 
208
        if(ctrl_io_read == 1) {
209
            if(shared_ptr->combined.io_address == 0x888C) {
210
                ctrl_io_read = 0;
211
 
212
                if(bochs486_pc_only == 0) {
213
                    shared_ptr->ao486.io_data = 0xFFFFFFFF;
214
                    shared_ptr->ao486.io_step = STEP_ACK;
215
                }
216
                if(ao486_only == 0) {
217
                    shared_ptr->bochs486_pc.io_data = 0xFFFFFFFF;
218
                    shared_ptr->bochs486_pc.io_step = STEP_ACK;
219
                }
220
            }
221
            else if(shared_ptr->combined.io_step == STEP_ACK && shared_ptr->combined.io_is_write == 0) {
222
                ctrl_io_read = 0;
223
 
224
                if(bochs486_pc_only == 0) {
225
                    shared_ptr->ao486.io_data = shared_ptr->combined.io_data;
226
                    shared_ptr->ao486.io_step = STEP_ACK;
227
                }
228
                if(ao486_only == 0) {
229
                    shared_ptr->bochs486_pc.io_data = shared_ptr->combined.io_data;
230
                    shared_ptr->bochs486_pc.io_step = STEP_ACK;
231
                }
232
            }
233
        }
234
 
235
        //---------------------------------------------------------------------- control io write
236
 
237
        if(ctrl_io_write == 0) {
238
            if( (bochs486_pc_only && shared_ptr->bochs486_pc.io_step == STEP_REQ && shared_ptr->bochs486_pc.io_is_write == 1) ||
239
                (ao486_only       && shared_ptr->ao486.io_step == STEP_REQ       && shared_ptr->ao486.io_is_write == 1) || (
240
                shared_ptr->ao486.io_step == STEP_REQ && shared_ptr->bochs486_pc.io_step == STEP_REQ && shared_ptr->ao486.io_is_write == 1 &&
241
                    shared_ptr->ao486.io_address    == shared_ptr->bochs486_pc.io_address &&
242
                    shared_ptr->ao486.io_data       == shared_ptr->bochs486_pc.io_data &&
243
                    shared_ptr->ao486.io_byteenable == shared_ptr->bochs486_pc.io_byteenable &&
244
                    shared_ptr->ao486.io_is_write   == shared_ptr->bochs486_pc.io_is_write &&
245
                    shared_ptr->ao486.io_step       == shared_ptr->bochs486_pc.io_step ) )
246
            {
247
                ctrl_io_write = 1;
248
 
249
                shared_ptr->combined.io_address    = (ao486_only)? shared_ptr->ao486.io_address : shared_ptr->bochs486_pc.io_address;
250
                shared_ptr->combined.io_data       = (ao486_only)? shared_ptr->ao486.io_data : shared_ptr->bochs486_pc.io_data;
251
                shared_ptr->combined.io_byteenable = (ao486_only)? shared_ptr->ao486.io_byteenable : shared_ptr->bochs486_pc.io_byteenable;
252
                shared_ptr->combined.io_is_write   = 1;
253
                shared_ptr->combined.io_step       = STEP_REQ;
254
            }
255
        }
256
 
257
        if(ctrl_io_write == 1) {
258
            if(shared_ptr->combined.io_address == 0x8888) {
259
                fprintf(debug_fp, "%c", shared_ptr->combined.io_data & 0xFF);
260
                fflush(debug_fp);
261
 
262
                ctrl_io_write = 0;
263
 
264
                if(bochs486_pc_only == 0) shared_ptr->ao486.io_step = STEP_ACK;
265
                if(ao486_only == 0)       shared_ptr->bochs486_pc.io_step = STEP_ACK;
266
            }
267
            else if(shared_ptr->combined.io_step == STEP_ACK && shared_ptr->combined.io_is_write == 1) {
268
                ctrl_io_write = 0;
269
 
270
                if(bochs486_pc_only == 0) shared_ptr->ao486.io_step = STEP_ACK;
271
                if(ao486_only == 0)       shared_ptr->bochs486_pc.io_step = STEP_ACK;
272
            }
273
        }
274
 
275
        //---------------------------------------------------------------------- control mem read
276
 
277
        if(ctrl_mem_read == 0) {
278
            if( (bochs486_pc_only && shared_ptr->bochs486_pc.mem_step == STEP_REQ && shared_ptr->bochs486_pc.mem_is_write == 0) ||
279
                (ao486_only       && shared_ptr->ao486.mem_step == STEP_REQ       && shared_ptr->ao486.mem_is_write == 0) || (
280
                shared_ptr->ao486.mem_step == STEP_REQ && shared_ptr->bochs486_pc.mem_step == STEP_REQ && shared_ptr->ao486.mem_is_write == 0 &&
281
                    shared_ptr->ao486.mem_address    == shared_ptr->bochs486_pc.mem_address &&
282
                    shared_ptr->ao486.mem_byteenable == shared_ptr->bochs486_pc.mem_byteenable &&
283
                    shared_ptr->ao486.mem_is_write   == shared_ptr->bochs486_pc.mem_is_write &&
284
                    shared_ptr->ao486.mem_step       == shared_ptr->bochs486_pc.mem_step ) )
285
            {
286
                ctrl_mem_read = 1;
287
 
288
                shared_ptr->combined.mem_address    = (ao486_only)? shared_ptr->ao486.mem_address : shared_ptr->bochs486_pc.mem_address;
289
                shared_ptr->combined.mem_byteenable = (ao486_only)? shared_ptr->ao486.mem_byteenable : shared_ptr->bochs486_pc.mem_byteenable;
290
                shared_ptr->combined.mem_is_write   = 0;
291
                shared_ptr->combined.mem_step       = STEP_REQ;
292
            }
293
        }
294
 
295
        if(ctrl_mem_read == 1) {
296
            if(shared_ptr->combined.mem_step == STEP_ACK && shared_ptr->combined.mem_is_write == 0) {
297
                ctrl_mem_read = 0;
298
 
299
                if(bochs486_pc_only == 0) {
300
                    shared_ptr->ao486.mem_data = shared_ptr->combined.mem_data;
301
                    shared_ptr->ao486.mem_step = STEP_ACK;
302
                }
303
                if(ao486_only == 0) {
304
                    shared_ptr->bochs486_pc.mem_data = shared_ptr->combined.mem_data;
305
                    shared_ptr->bochs486_pc.mem_step = STEP_ACK;
306
                }
307
            }
308
        }
309
 
310
        //---------------------------------------------------------------------- control mem write
311
 
312
        if(ctrl_mem_write == 0) {
313
            if( (bochs486_pc_only && shared_ptr->bochs486_pc.mem_step == STEP_REQ && shared_ptr->bochs486_pc.mem_is_write == 1) ||
314
                (ao486_only       && shared_ptr->ao486.mem_step == STEP_REQ       && shared_ptr->ao486.mem_is_write == 1) || (
315
                shared_ptr->ao486.mem_step == STEP_REQ && shared_ptr->bochs486_pc.mem_step == STEP_REQ && shared_ptr->ao486.mem_is_write == 1 &&
316
                    shared_ptr->ao486.mem_address    == shared_ptr->bochs486_pc.mem_address &&
317
                    shared_ptr->ao486.mem_data       == shared_ptr->bochs486_pc.mem_data &&
318
                    shared_ptr->ao486.mem_byteenable == shared_ptr->bochs486_pc.mem_byteenable &&
319
                    shared_ptr->ao486.mem_is_write   == shared_ptr->bochs486_pc.mem_is_write &&
320
                    shared_ptr->ao486.mem_step       == shared_ptr->bochs486_pc.mem_step ) )
321
            {
322
                ctrl_mem_write = 1;
323
 
324
                shared_ptr->combined.mem_address    = (ao486_only)? shared_ptr->ao486.mem_address : shared_ptr->bochs486_pc.mem_address;
325
                shared_ptr->combined.mem_data       = (ao486_only)? shared_ptr->ao486.mem_data : shared_ptr->bochs486_pc.mem_data;
326
                shared_ptr->combined.mem_byteenable = (ao486_only)? shared_ptr->ao486.mem_byteenable : shared_ptr->bochs486_pc.mem_byteenable;
327
                shared_ptr->combined.mem_is_write   = 1;
328
                shared_ptr->combined.mem_step       = STEP_REQ;
329
            }
330
        }
331
 
332
        if(ctrl_mem_write == 1) {
333
            if(shared_ptr->combined.mem_step == STEP_ACK && shared_ptr->combined.mem_is_write == 1) {
334
                ctrl_mem_write = 0;
335
 
336
                if(bochs486_pc_only == 0) shared_ptr->ao486.mem_step = STEP_ACK;
337
                if(ao486_only == 0)       shared_ptr->bochs486_pc.mem_step = STEP_ACK;
338
            }
339
        }
340
 
341
        //---------------------------------------------------------------------- combined mem write
342
 
343
        if(shared_ptr->combined.mem_step == STEP_REQ && shared_ptr->combined.mem_is_write) {
344
            uint32 address = shared_ptr->combined.mem_address;
345
            if(address < 0xA0000 || address >= 0xC0000) {
346
                for(uint32 i=0; i<4; i++) {
347
                    if(shared_ptr->combined.mem_byteenable & 1) {
348
                        shared_ptr->mem.bytes[address + i] = shared_ptr->combined.mem_data & 0xFF;
349
//printf("[%08x] = %02x\n", address + i, shared_ptr->combined.mem_data & 0xFF);
350
                    }
351
                    shared_ptr->combined.mem_byteenable >>= 1;
352
                    shared_ptr->combined.mem_data >>= 8;
353
                }
354
                shared_ptr->combined.mem_step = STEP_ACK;
355
            }
356
        }
357
 
358
        //----------------------------------------------------------------------
359
 
360
        usleep(10);
361
    }
362
 
363
    munmap((void *)shared_ptr, sizeof(shared_mem_t));
364
    close(fd);
365
 
366
    return 0;
367
}

powered by: WebSVN 2.1.0

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