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

Subversion Repositories ao486

[/] [ao486/] [trunk/] [sim/] [verilator/] [soc/] [hdd/] [main_plugin.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
#include <cstdio>
2
#include <cstdlib>
3
 
4
#include <dlfcn.h>
5
 
6
#include <sys/mman.h>
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11
 
12
#include "Vhdd.h"
13
#include "verilated.h"
14
#include "verilated_vcd_c.h"
15
 
16
#include "shared_mem.h"
17
 
18
//------------------------------------------------------------------------------
19
//------------------------------------------------------------------------------
20
//------------------------------------------------------------------------------
21
 
22
volatile shared_mem_t *shared_ptr = NULL;
23
 
24
//------------------------------------------------------------------------------
25
//------------------------------------------------------------------------------
26
//------------------------------------------------------------------------------
27
 
28
uint32 hd_cylinders = 1024;
29
uint32 hd_heads = 16;
30
uint32 hd_spt = 63;
31
uint32 hd_total_sectors = hd_cylinders * hd_heads * hd_spt;
32
 
33
unsigned int identify[256] = {
34
    0x0040,                                         //word 0
35
    (hd_cylinders > 16383)? 16383 : hd_cylinders,   //word 1
36
    0x0000,                                         //word 2 reserved
37
    hd_heads,                                       //word 3
38
    (unsigned short)(512 * hd_spt),                 //word 4
39
    512,                                            //word 5
40
    hd_spt,                                         //word 6
41
    0x0000,                                         //word 7 vendor specific
42
    0x0000,                                         //word 8 vendor specific
43
    0x0000,                                         //word 9 vendor specific
44
    ('B' << 8) | 'X',                               //word 10
45
    ('H' << 8) | 'D',                               //word 11
46
    ('0' << 8) | '0',                               //word 12
47
    ('0' << 8) | '1',                               //word 13
48
    ('1' << 8) | ' ',                               //word 14
49
    (' ' << 8) | ' ',                               //word 15
50
    (' ' << 8) | ' ',                               //word 16
51
    (' ' << 8) | ' ',                               //word 17
52
    (' ' << 8) | ' ',                               //word 18
53
    (' ' << 8) | ' ',                               //word 19
54
    3,                                              //word 20 buffer type
55
    512,                                            //word 21 cache size
56
    4,                                              //word 22 number of ecc bytes
57
    0,0,0,0,                                        //words 23..26 firmware revision
58
    ('H' << 8) | 'D',                               //words 27..46 model number
59
    ('m' << 8) | 'o',
60
    ('d' << 8) | 'e',
61
    ('l' << 8) | ' ',
62
    (' ' << 8) | ' ',
63
    (' ' << 8) | ' ',
64
    (' ' << 8) | ' ',
65
    (' ' << 8) | ' ',
66
    (' ' << 8) | ' ',
67
    (' ' << 8) | ' ',
68
    (' ' << 8) | ' ',
69
    (' ' << 8) | ' ',
70
    (' ' << 8) | ' ',
71
    (' ' << 8) | ' ',
72
    (' ' << 8) | ' ',
73
    (' ' << 8) | ' ',
74
    (' ' << 8) | ' ',
75
    (' ' << 8) | ' ',
76
    (' ' << 8) | ' ',
77
    (' ' << 8) | ' ',
78
    16,                                             //word 47 max multiple sectors
79
    1,                                              //word 48 dword io
80
    1<<9,                                           //word 49 lba supported
81
    0x0000,                                         //word 50 reserved
82
    0x0200,                                         //word 51 pio timing
83
    0x0200,                                         //word 52 pio timing
84
    0x0007,                                         //word 53 valid fields
85
    (hd_cylinders > 16383)? 16383 : hd_cylinders,   //word 54
86
    hd_heads,                                       //word 55
87
    hd_spt,                                         //word 56
88
    hd_total_sectors & 0xFFFF,                      //word 57
89
    hd_total_sectors >> 16,                         //word 58
90
    0x0000,                                         //word 59 multiple sectors
91
    hd_total_sectors & 0xFFFF,                      //word 60
92
    hd_total_sectors >> 16,                         //word 61
93
    0x0000,                                         //word 62 single word dma modes
94
    0x0000,                                         //word 63 multiple word dma modes
95
    0x0000,                                         //word 64 pio modes
96
    120,120,120,120,                                //word 65..68
97
    0,0,0,0,0,0,0,0,0,0,0,                          //word 69..79
98
    0x007E,                                         //word 80 ata modes
99
    0x0000,                                         //word 81 minor version number
100
    1<<14,                                          //word 82 supported commands
101
    (1<<14) | (1<<13) | (1<<12) | (1<<10),          //word 83
102
    1<<14,                                          //word 84
103
    1<<14,                                          //word 85
104
    (1<<14) | (1<<13) | (1<<12) | (1<<10),          //word 86
105
    1<<14,                                          //word 87
106
    0x0000,                                         //word 88
107
    0,0,0,0,                                        //word 89..92
108
    1 | (1<<14) | 0x2000,                           //word 93
109
    0,0,0,0,0,0,                                    //word 94..99
110
    hd_total_sectors & 0xFFFF,                      //word 100
111
    hd_total_sectors >> 16,                         //word 101
112
    0,                                              //word 102
113
    0,                                              //word 103
114
 
115
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//word 104..127
116
 
117
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                //word 128..255
118
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
119
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
120
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
121
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
122
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
123
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
124
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
125
};
126
 
127
//------------------------------------------------------------------------------
128
 
129
uint32 sd_address;
130
uint32 sd_sector_count;
131
uint32 sd_command;
132
 
133
bool sd_mutex = false;
134
uint32 sd_waiting = 0;
135
 
136
int main(int argc, char **argv) {
137
    //map shared memory
138
    int fd = open("./../../../sim_pc/shared_mem.dat", O_RDWR, S_IRUSR | S_IWUSR);
139
 
140
    if(fd == -1) {
141
        perror("open() failed for shared_mem.dat");
142
        return -1;
143
    }
144
 
145
    shared_ptr = (shared_mem_t *)mmap(NULL, sizeof(shared_mem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
146
 
147
    if(shared_ptr == MAP_FAILED) {
148
        perror("mmap() failed");
149
        close(fd);
150
        return -2;
151
    }
152
 
153
    Verilated::commandArgs(argc, argv);
154
 
155
    Verilated::traceEverOn(true);
156
    VerilatedVcdC* tracer = new VerilatedVcdC;
157
 
158
    Vhdd *top = new Vhdd();
159
    top->trace (tracer, 99);
160
    //tracer->rolloverMB(1000000);
161
    tracer->open("hdd.vcd");
162
 
163
    bool dump = false;
164
 
165
    //reset
166
    top->clk = 0; top->rst_n = 1; top->eval();
167
    top->clk = 1; top->rst_n = 1; top->eval();
168
    top->clk = 1; top->rst_n = 0; top->eval();
169
    top->clk = 0; top->rst_n = 0; top->eval();
170
    top->clk = 0; top->rst_n = 1; top->eval();
171
 
172
    uint64 cycle = 0;
173
    bool read_cycle = false;
174
 
175
    /*
176
    0x00.[31:0]:    identify write
177
    0x01.[16:0]:    media cylinders
178
    0x02.[4:0]:     media heads
179
    0x03.[8:0]:     media spt
180
    0x04.[13:0]:    media sectors per cylinder = spt * heads
181
    0x05.[31:0]:    media sectors total
182
    0x06.[31:0]:    media sd base
183
    */
184
 
185
    for(uint32 i=0; i<128; i++) {
186
 
187
        top->mgmt_address = 0;
188
        top->mgmt_write = 1;
189
        top->mgmt_writedata = ((unsigned int)identify[2*i+1] << 16) | (unsigned int)identify[2*i+0];
190
 
191
        top->clk = 0;
192
        top->eval();
193
        if(dump) tracer->dump(cycle++);
194
 
195
        top->clk = 1;
196
        top->eval();
197
        if(dump) tracer->dump(cycle++);
198
 
199
        tracer->flush();
200
    }
201
 
202
    for(uint32 i=0; i<6; i++) {
203
        top->mgmt_address = i+1;
204
        top->mgmt_write   = 1;
205
        top->mgmt_writedata = (i==0)? hd_cylinders : (i==1)? hd_heads : (i==2)? hd_spt : (i==3)? hd_heads * hd_spt : (i==4)? hd_total_sectors : 0;
206
 
207
        top->clk = 0;
208
        top->eval();
209
        if(dump) tracer->dump(cycle++);
210
 
211
        top->clk = 1;
212
        top->eval();
213
        if(dump) tracer->dump(cycle++);
214
 
215
        tracer->flush();
216
    }
217
    top->mgmt_write = 0;
218
 
219
    printf("hdd main_plugin.cpp\n");
220
    while(!Verilated::gotFinish()) {
221
 
222
        //----------------------------------------------------------------------
223
 
224
        /*
225
        uint32 combined.io_address;
226
        uint32 combined.io_data;
227
        uint32 combined.io_byteenable;
228
        uint32 combined.io_is_write;
229
        step_t combined.io_step;
230
 
231
        //avalon slave
232
        input               io_address,
233
        input       [3:0]   io_byteenable,
234
        input               io_read,
235
        output reg  [31:0]  io_readdata,
236
        input               io_write,
237
        input       [31:0]  io_writedata,
238
 
239
        //ide shared port 0x3F6
240
        input               ide_3f6_read,
241
        output reg  [7:0]   ide_3f6_readdata,
242
        input               ide_3f6_write,
243
        input       [7:0]   ide_3f6_writedata,
244
        */
245
 
246
        top->io_read      = 0;
247
        top->io_write     = 0;
248
 
249
        top->ide_3f6_read = 0;
250
        top->ide_3f6_write= 0;
251
 
252
        if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write && (shared_ptr->combined.io_address == 0x01F0 || shared_ptr->combined.io_address == 0x01F4)) {
253
            top->io_address    = (shared_ptr->combined.io_address == 0x01F0)? 0 : 1;
254
            top->io_byteenable = shared_ptr->combined.io_byteenable;
255
            top->io_writedata  = shared_ptr->combined.io_data;
256
 
257
            top->io_read  = 0;
258
            top->io_write = 1;
259
 
260
            shared_ptr->combined.io_step = STEP_ACK;
261
        }
262
 
263
        if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write && shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 0x4) {
264
            top->ide_3f6_writedata = (shared_ptr->combined.io_data >> 16) & 0xFF;
265
 
266
            top->ide_3f6_read = 0;
267
            top->ide_3f6_write= 1;
268
 
269
            shared_ptr->combined.io_step = STEP_ACK;
270
        }
271
 
272
        if(read_cycle == false) {
273
            if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write == 0 && (shared_ptr->combined.io_address == 0x01F0 || shared_ptr->combined.io_address == 0x01F4)) {
274
                top->io_address    = (shared_ptr->combined.io_address == 0x01F0)? 0 : 1;
275
                top->io_byteenable = shared_ptr->combined.io_byteenable;
276
                top->io_writedata  = 0;
277
 
278
                top->io_read  = 1;
279
                top->io_write = 0;
280
 
281
                read_cycle = true;
282
            }
283
 
284
            if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write == 0 && shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 0x4) {
285
                top->ide_3f6_writedata = 0;
286
 
287
                top->ide_3f6_read = 1;
288
                top->ide_3f6_write= 0;
289
 
290
                read_cycle = true;
291
            }
292
        }
293
        else {
294
            if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write == 0 && (shared_ptr->combined.io_address == 0x01F0 || shared_ptr->combined.io_address == 0x01F4)) {
295
                uint32 val = top->io_readdata;
296
 
297
                if((shared_ptr->combined.io_byteenable & 1) == 0) val &= 0xFFFFFF00;
298
                if((shared_ptr->combined.io_byteenable & 2) == 0) val &= 0xFFFF00FF;
299
                if((shared_ptr->combined.io_byteenable & 4) == 0) val &= 0xFF00FFFF;
300
                if((shared_ptr->combined.io_byteenable & 8) == 0) val &= 0x00FFFFFF;
301
 
302
                shared_ptr->combined.io_data = val;
303
 
304
                read_cycle = false;
305
                shared_ptr->combined.io_step = STEP_ACK;
306
            }
307
 
308
            if(shared_ptr->combined.io_step == STEP_REQ && shared_ptr->combined.io_is_write == 0 && shared_ptr->combined.io_address == 0x03F4 && shared_ptr->combined.io_byteenable == 0x4) {
309
                uint32 val = (top->ide_3f6_readdata & 0xFF) << 16;
310
 
311
                shared_ptr->combined.io_data = val;
312
 
313
                read_cycle = false;
314
                shared_ptr->combined.io_step = STEP_ACK;
315
            }
316
        }
317
 
318
        //----------------------------------------------------------------------
319
 
320
        if(top->irq) {
321
            printf("ERROR: hdd irq set.\n");
322
            exit(-1);
323
        }
324
 
325
        //----------------------------------------------------------------------
326
 
327
        top->sd_master_waitrequest = 0;
328
        top->sd_master_readdatavalid = 0;
329
 
330
        if(top->sd_master_read) {
331
            sd_mutex = true;
332
            top->sd_master_readdata = 2;
333
        }
334
        else if(sd_mutex) {
335
            top->sd_master_readdatavalid = 1;
336
            sd_mutex = false;
337
        }
338
 
339
        if(top->sd_master_write) {
340
printf("sd_master %08x %08x\n", top->sd_master_address, top->sd_master_writedata);
341
 
342
            if(top->sd_master_address == 0x00000004) sd_address      = top->sd_master_writedata;
343
            if(top->sd_master_address == 0x00000008) sd_sector_count = top->sd_master_writedata;
344
            if(top->sd_master_address == 0x0000000C) sd_command      = top->sd_master_writedata;
345
 
346
            sd_waiting = 1000; //10000;
347
        }
348
        else if(sd_command != 0) {
349
 
350
            if(sd_waiting > 0) {
351
                sd_waiting--;
352
            }
353
            else if(sd_command == 2) { //read
354
 
355
                FILE *fp = fopen("/home/alek/temp/bochs-run/hd_copy_for_sim.img", "rb");
356
 
357
                //usleep(500*1000);
358
 
359
                for(uint32 i=0; i<sd_sector_count*128; i++) {
360
 
361
                    fseek(fp, (sd_address*512)+(i*4), SEEK_SET);
362
 
363
                    uint32 val;
364
                    fread(&val, 4, 1, fp);
365
 
366
                    top->sd_slave_write = 1;
367
                    top->sd_slave_writedata = val;
368
 
369
                    top->clk = 0;
370
                    top->eval();
371
                    if(dump) tracer->dump(cycle++);
372
 
373
                    top->clk = 1;
374
                    top->eval();
375
                    if(dump) tracer->dump(cycle++);
376
 
377
                    tracer->flush();
378
                }
379
 
380
                top->sd_slave_write = 0;
381
                sd_command = 0;
382
 
383
                for(uint32 i=0; i<30; i++) {
384
                    top->clk = 0;
385
                    top->eval();
386
                    if(dump) tracer->dump(cycle++);
387
 
388
                    top->clk = 1;
389
                    top->eval();
390
                    if(dump) tracer->dump(cycle++);
391
 
392
                    tracer->flush();
393
                }
394
 
395
                fclose(fp);
396
            }
397
            else if(sd_command == 3) { //write
398
 
399
                FILE *fp = fopen("/home/alek/temp/bochs-run/hd_copy_for_sim.img", "r+b");
400
 
401
                //usleep(500*1000);
402
 
403
                bool wait = false;
404
                for(uint32 i=0; i<sd_sector_count*128*2; i++) {
405
 
406
                    top->sd_slave_read = (wait == false)? 1 : 0;
407
 
408
                    top->clk = 0;
409
                    top->eval();
410
                    if(dump) tracer->dump(cycle++);
411
 
412
                    top->clk = 1;
413
                    top->eval();
414
                    if(dump) tracer->dump(cycle++);
415
 
416
                    tracer->flush();
417
 
418
                    if(wait) {
419
                        wait = (wait == false)? true : false;
420
                        continue;
421
                    }
422
 
423
                    //--- write
424
 
425
                    fseek(fp, (sd_address*512)+(i*4/2), SEEK_SET);
426
 
427
                    uint32 val = top->sd_slave_readdata;
428
                    fwrite(&val, 4, 1, fp);
429
 
430
                    wait = (wait == false)? true : false;
431
                }
432
 
433
                top->sd_slave_read = 0;
434
                sd_command = 0;
435
 
436
                for(uint32 i=0; i<30; i++) {
437
                    top->clk = 0;
438
                    top->eval();
439
                    if(dump) tracer->dump(cycle++);
440
 
441
                    top->clk = 1;
442
                    top->eval();
443
                    if(dump) tracer->dump(cycle++);
444
 
445
                    tracer->flush();
446
                }
447
 
448
                fclose(fp);
449
            }
450
        }
451
 
452
        //----------------------------------------------------------------------
453
 
454
        top->clk = 0;
455
        top->eval();
456
        if(dump) tracer->dump(cycle++);
457
 
458
        top->clk = 1;
459
        top->eval();
460
        if(dump) tracer->dump(cycle++);
461
 
462
        tracer->flush();
463
 
464
        usleep(10);
465
    }
466
    tracer->close();
467
    delete tracer;
468
    delete top;
469
 
470
    return 0;
471
}
472
 
473
//------------------------------------------------------------------------------
474
 
475
/*
476
    input               clk,
477
    input               rst_n,
478
 
479
    //irq
480
    output reg          irq,
481
 
482
    //avalon slave
483
    input               io_address,
484
    input       [3:0]   io_byteenable,
485
    input               io_read,
486
    output reg  [31:0]  io_readdata,
487
    input               io_write,
488
    input       [31:0]  io_writedata,
489
 
490
    //ide shared port 0x3F6
491
    input               ide_3f6_read,
492
    output reg  [7:0]   ide_3f6_readdata,
493
    input               ide_3f6_write,
494
    input       [7:0]   ide_3f6_writedata,
495
 
496
    //master to control sd
497
    output      [31:0]  sd_master_address,
498
    input               sd_master_waitrequest,
499
    output              sd_master_read,
500
    input               sd_master_readdatavalid,
501
    input       [31:0]  sd_master_readdata,
502
    output              sd_master_write,
503
    output      [31:0]  sd_master_writedata,
504
 
505
    //slave with data from/to sd
506
    input       [8:0]   sd_slave_address,
507
    input               sd_slave_read,
508
    output reg  [31:0]  sd_slave_readdata,
509
    input               sd_slave_write,
510
    input       [31:0]  sd_slave_writedata,
511
 
512
    //management slave
513
    //0x00.[31:0]:    identify write
514
    //0x01.[16:0]:    media cylinders
515
    //0x02.[4:0]:     media heads
516
    //0x03.[8:0]:     media spt
517
    //0x04.[13:0]:    media sectors per cylinder = spt * heads
518
    //0x05.[31:0]:    media sectors total
519
    //0x06.[31:0]:    media sd base
520
 
521
    input       [2:0]   mgmt_address,
522
    input               mgmt_write,
523
    input       [31:0]  mgmt_writedata
524
*/

powered by: WebSVN 2.1.0

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