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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [bench/] [cpp/] [zip_sim.cpp] - Blame information for rev 10

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

Line No. Rev Author Line
1 10 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3 10 dgisselq
// Filename:    zip_sim.cpp
4 2 dgisselq
//
5 10 dgisselq
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6 2 dgisselq
//
7 10 dgisselq
// Purpose:     This provides a simulation capability for the CMod S6 SoC.
8 2 dgisselq
//
9 10 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11 2 dgisselq
//
12 10 dgisselq
////////////////////////////////////////////////////////////////////////////////
13 2 dgisselq
//
14 10 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
15 2 dgisselq
//
16 10 dgisselq
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20 2 dgisselq
//
21 10 dgisselq
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38 2 dgisselq
#include <stdio.h>
39
#include <sys/types.h>
40
#include <sys/stat.h>
41
#include <fcntl.h>
42
#include <signal.h>
43
#include <time.h>
44
#include <unistd.h>
45 10 dgisselq
#include <stdint.h>
46 2 dgisselq
 
47
#include "verilated.h"
48
#include "Vbusmaster.h"
49
 
50 10 dgisselq
#include "regdefs.h"
51 2 dgisselq
#include "testb.h"
52
// #include "twoc.h"
53
#include "qspiflashsim.h"
54
#include "uartsim.h"
55
 
56 10 dgisselq
typedef uint32_t        BUSW;
57
 
58 2 dgisselq
class   GPIOSIM {
59
public:
60
        unsigned operator()(const unsigned o_gpio) { return 0; }
61
};
62
 
63
class   KEYPADSIM {
64
public:
65
        unsigned operator()(const unsigned o_kpd) { return 0; }
66
};
67
 
68
// Add a reset line, since Vbusmaster doesn't have one
69
class   Vbusmasterr : public Vbusmaster {
70
public:
71
        int     i_rst;
72
};
73
 
74
// No particular "parameters" need definition or redefinition here.
75
class   ZIPSIM_TB : public TESTB<Vbusmasterr> {
76
public:
77
        QSPIFLASHSIM    m_flash;
78
        UARTSIM         m_uart;
79
        GPIOSIM         m_gpio;
80
        KEYPADSIM       m_keypad;
81
        unsigned        m_last_led;
82
        time_t          m_start_time;
83
 
84
        ZIPSIM_TB(void) : m_uart(0x2b6) {
85
                m_start_time = time(NULL);
86
        }
87
 
88
        void    reset(void) {
89
                m_flash.debug(false);
90
        }
91
 
92
        void    tick(void) {
93
                if ((m_tickcount & ((1<<28)-1))==0) {
94
                        double  ticks_per_second = m_tickcount;
95
                        time_t  nsecs = (time(NULL)-m_start_time);
96
                        if (nsecs > 0) {
97
                                ticks_per_second /= (double)nsecs;
98
                                printf(" ********   %.6f TICKS PER SECOND\n",
99
                                        ticks_per_second);
100
                        }
101
                }
102
 
103
                // Set up the bus before any clock tick
104
 
105
                // We've got the flash to deal with ...
106
                m_core->i_qspi_dat = m_flash(m_core->o_qspi_cs_n,
107
                                                m_core->o_qspi_sck,
108
                                                m_core->o_qspi_dat);
109
 
110
                // And the GPIO lines
111
                m_core->i_gpio = m_gpio(m_core->o_gpio);
112
 
113
                m_core->i_btn = 0; // 2'b0
114
                // o_led, o_pwm, o_pwm_aux
115
 
116
                // And the keypad
117
                m_core->i_kp_row = m_keypad(m_core->o_kp_col);
118
 
119
                // And the UART
120
                m_core->i_rx_stb  = m_uart.rx(m_core->i_rx_data);
121
                m_core->i_tx_busy = m_uart.tx(m_core->o_tx_stb, m_core->o_tx_data);
122
 
123
                TESTB<Vbusmasterr>::tick();
124
 
125
                if (m_core->o_led != m_last_led) {
126
                        printf("LED: %08x\n", m_core->o_led);
127 10 dgisselq
                        m_last_led = m_core->o_led;
128 2 dgisselq
                }
129
 
130
                /*
131 10 dgisselq
                printf("PC: %08x:%08x [%08x:%08x:%08x:%08x:%08x],%08x,%08x,%d,%08x,%08x (%x,%x)\n",
132 2 dgisselq
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__ipc,
133
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__upc,
134
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[0],
135
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[1],
136
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[2],
137
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[3],
138
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[15],
139
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_decoder__DOT__r_I,
140
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__r_opB,
141
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_decoder__DOT__w_dcdR_pc,
142
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__r_opA,
143 10 dgisselq
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__wr_reg_vl,
144
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__w_iflags,
145
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__w_iflags,
146
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__pf_pc
147
                        );
148
                if (m_core->v__DOT__wb_cyc) {
149
                printf("WB: %s/%s/%s[@0x%08x] %08x ->%s/%s %08x\n",
150
                        (m_core->v__DOT__wb_cyc)?"CYC":"   ",
151
                        (m_core->v__DOT__wb_stb)?"STB":"   ",
152
                        (m_core->v__DOT__wb_we )?"WE ":"   ",
153
                        (m_core->v__DOT__w_zip_addr),
154
                        (m_core->v__DOT__wb_data),
155
                        (m_core->v__DOT__wb_ack)?"ACK":"   ",
156
                        (m_core->v__DOT__wb_stall)?"STL":"   ",
157
                        (m_core->v__DOT__wb_idata)
158
                        );
159
                }
160 2 dgisselq
 
161
                if (m_core->v__DOT__thecpu__DOT__thecpu__DOT__pf_valid)
162
                        printf("PC: %08x - %08x, uart=%d,%d, pic = %d,%04x,%0d,%04x\n",
163
                                m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_pc,
164
                                m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction,
165
                                m_core->i_rx_stb, m_core->i_tx_busy,
166
                                m_core->v__DOT__pic__DOT__r_gie,
167
                                m_core->v__DOT__pic__DOT__r_int_enable,
168
                                m_core->v__DOT__pic__DOT__r_any,
169
                                m_core->v__DOT__pic__DOT__r_int_state);
170
                */
171 10 dgisselq
 
172
                printf("%08x\n", m_core->v__DOT__zipt_a__DOT__r_value);
173 2 dgisselq
        }
174
};
175
 
176
ZIPSIM_TB       *tb;
177
 
178
bool    iself(const char *fname) {
179
        FILE    *fp;
180
        bool    ret = true;
181
 
182 10 dgisselq
        if ((!fname)||(!fname[0]))
183 2 dgisselq
                return false;
184
 
185 10 dgisselq
        fp = fopen(fname, "rb");
186
 
187
        if (!fp)        return false;
188
        if (0x7f != fgetc(fp))  ret = false;
189
        if ('E'  != fgetc(fp))  ret = false;
190
        if ('L'  != fgetc(fp))  ret = false;
191
        if ('F'  != fgetc(fp))  ret = false;
192 2 dgisselq
        fclose(fp);
193 10 dgisselq
        return  ret;
194 2 dgisselq
}
195
 
196 10 dgisselq
long    fgetwords(FILE *fp) {
197
        // Return the number of words in the current file, and return the 
198
        // file as though it had never been adjusted
199
        long    fpos, flen;
200
        fpos = ftell(fp);
201
        if (0 != fseek(fp, 0l, SEEK_END)) {
202
                fprintf(stderr, "ERR: Could not determine file size\n");
203
                perror("O/S Err:");
204
                exit(-2);
205
        } flen = ftell(fp);
206
        if (0 != fseek(fp, fpos, SEEK_SET)) {
207
                fprintf(stderr, "ERR: Could not seek on file\n");
208
                perror("O/S Err:");
209
                exit(-2);
210
        } flen /= sizeof(BUSW);
211
        return flen;
212
}
213
 
214
class   SECTION {
215
public:
216
        unsigned        m_start, m_len;
217
        BUSW    m_data[1];
218
};
219
 
220
SECTION **singlesection(int nwords) {
221
        fprintf(stderr, "NWORDS = %d\n", nwords);
222
        size_t  sz = (2*(sizeof(SECTION)+sizeof(SECTION *))
223
                +(nwords-1)*(sizeof(BUSW)));
224
        char    *d = (char *)malloc(sz);
225
        SECTION **r = (SECTION **)d;
226
        memset(r, 0, sz);
227
        r[0] = (SECTION *)(&d[2*sizeof(SECTION *)]);
228
        r[0]->m_len   = nwords;
229
        r[1] = (SECTION *)(&r[0]->m_data[r[0]->m_len]);
230
        r[0]->m_start = 0;
231
        r[1]->m_start = 0;
232
        r[1]->m_len   = 0;
233
 
234
        return r;
235
}
236
 
237
SECTION **rawsection(const char *fname) {
238
        SECTION         **secpp, *secp;
239
        unsigned        num_words;
240
        FILE            *fp;
241
        int             nr;
242
 
243
        fp = fopen(fname, "r");
244
        if (fp == NULL) {
245
                fprintf(stderr, "Could not open: %s\n", fname);
246
                exit(-1);
247
        }
248
 
249
        if ((num_words=fgetwords(fp)) > FLASHWORDS) {
250
                fprintf(stderr, "File overruns flash memory\n");
251
                exit(-1);
252
        }
253
        secpp = singlesection(num_words);
254
        secp = secpp[0];
255
        secp->m_start = RESET_ADDRESS;
256
        secp->m_len = num_words;
257
        nr= fread(secp->m_data, sizeof(BUSW), num_words, fp);
258
        if (nr != (int)num_words) {
259
                fprintf(stderr, "Could not read entire file\n");
260
                perror("O/S Err:");
261
                exit(-2);
262
        } assert(secpp[1]->m_len == 0);
263
 
264
        return secpp;
265
}
266
 
267
unsigned        byteswap(unsigned n) {
268
        unsigned        r;
269
 
270
        r = (n&0x0ff); n>>= 8;
271
        r = (r<<8) | (n&0x0ff); n>>= 8;
272
        r = (r<<8) | (n&0x0ff); n>>= 8;
273
        r = (r<<8) | (n&0x0ff); n>>= 8;
274
 
275
        return r;
276
}
277
 
278
#include <libelf.h>
279
#include <gelf.h>
280
 
281
void    elfread(const char *fname, unsigned &entry, SECTION **&sections) {
282
        Elf     *e;
283
        int     fd, i;
284
        size_t  n;
285
        char    *id;
286
        Elf_Kind        ek;
287
        GElf_Ehdr       ehdr;
288
        GElf_Phdr       phdr;
289
        const   bool    dbg = false;
290
 
291
        if (elf_version(EV_CURRENT) == EV_NONE) {
292
                fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
293
                perror("O/S Err:");
294
                exit(EXIT_FAILURE);
295
        } if ((fd = open(fname, O_RDONLY, 0)) < 0) {
296
                fprintf(stderr, "Could not open %s\n", fname);
297
                perror("O/S Err:");
298
                exit(EXIT_FAILURE);
299
        } if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
300
                fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
301
                exit(EXIT_FAILURE);
302
        }
303
 
304
        ek = elf_kind(e);
305
        if (ek == ELF_K_ELF) {
306
                ; // This is the kind of file we should expect
307
        } else if (ek == ELF_K_AR) {
308
                fprintf(stderr, "Cannot run an archive!\n");
309
                exit(EXIT_FAILURE);
310
        } else if (ek == ELF_K_NONE) {
311
                ;
312
        } else {
313
                fprintf(stderr, "Unexpected ELF file kind!\n");
314
                exit(EXIT_FAILURE);
315
        }
316
 
317
        if (gelf_getehdr(e, &ehdr) == NULL) {
318
                fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
319
                exit(EXIT_FAILURE);
320
        } if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
321
                fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
322
                exit(EXIT_FAILURE);
323
        } if ((id = elf_getident(e, NULL)) == NULL) {
324
                fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
325
                exit(EXIT_FAILURE);
326
        } if (i != ELFCLASS32) {
327
                fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
328
                exit(EXIT_FAILURE);
329
        }
330
 
331
        if (dbg) {
332
        printf("    %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
333
        printf("    %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
334
        printf("    %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
335
        printf("    %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
336
        printf("    %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
337
        printf("    %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
338
        printf("    %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
339
        printf("    %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
340
        printf("    %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
341
        printf("    %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
342
        printf("\n");
343
        }
344
 
345
 
346
        // Check whether or not this is an ELF file for the ZipCPU ...
347
        if (ehdr.e_machine != 0x0dadd) {
348
                fprintf(stderr, "This is not a ZipCPU ELF file\n");
349
                exit(EXIT_FAILURE);
350
        }
351
 
352
        // Get our entry address
353
        entry = ehdr.e_entry;
354
 
355
 
356
        // Now, let's go look at the program header
357
        if (elf_getphdrnum(e, &n) != 0) {
358
                fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
359
                exit(EXIT_FAILURE);
360
        }
361
 
362
        unsigned total_octets = 0, current_offset=0, current_section=0;
363
        for(i=0; i<(int)n; i++) {
364
                total_octets += sizeof(SECTION *)+sizeof(SECTION);
365
 
366
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
367
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
368
                        exit(EXIT_FAILURE);
369
                }
370
 
371
                if (dbg) {
372
                printf("    %-20s 0x%x\n", "p_type",   phdr.p_type);
373
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
374
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
375
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
376
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
377
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
378
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
379
 
380
                if (phdr.p_flags & PF_X)        printf(" Execute");
381
                if (phdr.p_flags & PF_R)        printf(" Read");
382
                if (phdr.p_flags & PF_W)        printf(" Write");
383
                printf("]\n");
384
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
385
                }
386
 
387
                total_octets += phdr.p_memsz;
388
        }
389
 
390
        char    *d = (char *)malloc(total_octets + sizeof(SECTION)+sizeof(SECTION *));
391
        memset(d, 0, total_octets);
392
 
393
        SECTION **r = sections = (SECTION **)d;
394
        current_offset = (n+1)*sizeof(SECTION *);
395
        current_section = 0;
396
 
397
        for(i=0; i<(int)n; i++) {
398
                r[i] = (SECTION *)(&d[current_offset]);
399
 
400
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
401
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
402
                        exit(EXIT_FAILURE);
403
                }
404
 
405
                if (dbg) {
406
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
407
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
408
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
409
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
410
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
411
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
412
 
413
                if (phdr.p_flags & PF_X)        printf(" Execute");
414
                if (phdr.p_flags & PF_R)        printf(" Read");
415
                if (phdr.p_flags & PF_W)        printf(" Write");
416
                printf("]\n");
417
 
418
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
419
                }
420
 
421
                current_section++;
422
 
423
                r[i]->m_start = phdr.p_vaddr;
424
                r[i]->m_len   = phdr.p_filesz/ sizeof(BUSW);
425
 
426
                current_offset += phdr.p_memsz + sizeof(SECTION);
427
 
428
                // Now, let's read in our section ...
429
                if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
430
                        fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
431
                        perror("O/S Err:");
432
                        exit(EXIT_FAILURE);
433
                } if (phdr.p_filesz > phdr.p_memsz)
434
                        phdr.p_filesz = 0;
435
                if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
436
                        fprintf(stderr, "Didnt read entire section\n");
437
                        perror("O/S Err:");
438
                        exit(EXIT_FAILURE);
439
                }
440
 
441
                // Next, we need to byte swap it from big to little endian
442
                for(unsigned j=0; j<r[i]->m_len; j++)
443
                        r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
444
 
445
                if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
446
                        fprintf(stderr, "ADR[%04x] = %08x\n", r[i]->m_start+j,
447
                        r[i]->m_data[j]);
448
        }
449
 
450
        r[i] = (SECTION *)(&d[current_offset]);
451
        r[current_section]->m_start = 0;
452
        r[current_section]->m_len   = 0;
453
 
454
        elf_end(e);
455
        close(fd);
456
}
457
 
458
 
459 2 dgisselq
void    usage(void) {
460
        fprintf(stderr, "Usage: zip_sim flash_program\n");
461
}
462
 
463
int     main(int argc, char **argv) {
464
        Verilated::commandArgs(argc, argv);
465
        tb = new ZIPSIM_TB;
466
        const char      *codef = NULL;
467
 
468
        for(int argn=1; argn<argc; argn++) {
469
                if (argv[argn][0] == '-') {
470
                        usage();
471
                        exit(-1);
472
                } else
473
                        codef = argv[argn];
474
        }
475
 
476
        if ((!codef)||(!codef[0]))
477
                fprintf(stderr, "No executable code filename found!\n");
478
 
479
        if (access(codef, R_OK)!=0)
480
                fprintf(stderr, "Cannot read code filename, %s\n", codef);
481
 
482
        if (iself(codef)) {
483 10 dgisselq
                SECTION **secpp, *secp;
484
                BUSW    entry;
485
                elfread(codef, entry, secpp);
486 2 dgisselq
 
487 10 dgisselq
                for(int i=0; secpp[i]->m_len; i++) {
488
                        secp = secpp[i];
489
                        tb->m_flash.write(secp->m_start, secp->m_len, secp->m_data);
490 2 dgisselq
                }
491
        } else {
492 10 dgisselq
                tb->m_flash.load(RESET_ADDRESS, codef);
493 2 dgisselq
        }
494
 
495
        tb->reset();
496
 
497
        while(1)
498
                tb->tick();
499
 
500
        printf("SUCCESS!\n");
501
        exit(0);
502
}

powered by: WebSVN 2.1.0

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