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

Subversion Repositories s6soc

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

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 17 dgisselq
        FILE            *m_dbg;
84 2 dgisselq
 
85
        ZIPSIM_TB(void) : m_uart(0x2b6) {
86
                m_start_time = time(NULL);
87 17 dgisselq
                m_dbg = fopen("dbg.txt","w");
88 2 dgisselq
        }
89
 
90
        void    reset(void) {
91
                m_flash.debug(false);
92
        }
93
 
94
        void    tick(void) {
95
                if ((m_tickcount & ((1<<28)-1))==0) {
96
                        double  ticks_per_second = m_tickcount;
97
                        time_t  nsecs = (time(NULL)-m_start_time);
98
                        if (nsecs > 0) {
99
                                ticks_per_second /= (double)nsecs;
100
                                printf(" ********   %.6f TICKS PER SECOND\n",
101
                                        ticks_per_second);
102
                        }
103
                }
104
 
105
                // Set up the bus before any clock tick
106
 
107
                // We've got the flash to deal with ...
108
                m_core->i_qspi_dat = m_flash(m_core->o_qspi_cs_n,
109
                                                m_core->o_qspi_sck,
110
                                                m_core->o_qspi_dat);
111
 
112
                // And the GPIO lines
113
                m_core->i_gpio = m_gpio(m_core->o_gpio);
114
 
115
                m_core->i_btn = 0; // 2'b0
116
                // o_led, o_pwm, o_pwm_aux
117
 
118
                // And the keypad
119
                m_core->i_kp_row = m_keypad(m_core->o_kp_col);
120
 
121
                // And the UART
122
                m_core->i_rx_stb  = m_uart.rx(m_core->i_rx_data);
123
                m_core->i_tx_busy = m_uart.tx(m_core->o_tx_stb, m_core->o_tx_data);
124
 
125
                TESTB<Vbusmasterr>::tick();
126
 
127
                if (m_core->o_led != m_last_led) {
128
                        printf("LED: %08x\n", m_core->o_led);
129 10 dgisselq
                        m_last_led = m_core->o_led;
130 2 dgisselq
                }
131
 
132 17 dgisselq
 
133
                if (m_dbg) fprintf(m_dbg, "PC: %08x:%08x [%08x:%08x:%08x:%08x:%08x],%08x,%08x,%d,%08x,%08x (%x,%x/0x%08x)\n",
134 2 dgisselq
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__ipc,
135
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__upc,
136
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[0],
137
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[1],
138
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[2],
139
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[3],
140
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__regset[15],
141
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_decoder__DOT__r_I,
142
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__r_opB,
143
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_decoder__DOT__w_dcdR_pc,
144
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__r_opA,
145 10 dgisselq
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__wr_reg_vl,
146
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__w_iflags,
147 17 dgisselq
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__w_uflags,
148 10 dgisselq
                        m_core->v__DOT__thecpu__DOT__thecpu__DOT__pf_pc
149
                        );
150 17 dgisselq
                if ((m_core->v__DOT__wb_cyc)&&(m_dbg))
151
                fprintf(m_dbg, "WB: %s/%s/%s[@0x%08x] %08x ->%s/%s %08x\n",
152 10 dgisselq
                        (m_core->v__DOT__wb_cyc)?"CYC":"   ",
153
                        (m_core->v__DOT__wb_stb)?"STB":"   ",
154
                        (m_core->v__DOT__wb_we )?"WE ":"   ",
155
                        (m_core->v__DOT__w_zip_addr),
156
                        (m_core->v__DOT__wb_data),
157
                        (m_core->v__DOT__wb_ack)?"ACK":"   ",
158
                        (m_core->v__DOT__wb_stall)?"STL":"   ",
159
                        (m_core->v__DOT__wb_idata)
160
                        );
161 17 dgisselq
                if (m_dbg)
162
                        fprintf(m_dbg, "PIC: %3s(%4x) %3s(%4x)%s\n",
163
                                (m_core->v__DOT__pic__DOT__r_gie)?"GIE":"",
164
                                (m_core->v__DOT__pic__DOT__r_int_enable),
165
                                (m_core->v__DOT__pic__DOT__r_any)?"ANY":"",
166
                                (m_core->v__DOT__pic__DOT__r_int_state),
167
                                (m_core->v__DOT__pic__DOT__r_interrupt)?" ---> INT!":"");
168 2 dgisselq
 
169 17 dgisselq
                if ((m_core->v__DOT__thecpu__DOT__thecpu__DOT__pf_valid)&&(m_dbg))
170
                        fprintf(m_dbg, "PC: %08x - %08x, uart=%d,%d, pic = %d,%04x,%0d,%04x\n",
171 2 dgisselq
                                m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction_pc,
172
                                m_core->v__DOT__thecpu__DOT__thecpu__DOT__instruction,
173
                                m_core->i_rx_stb, m_core->i_tx_busy,
174
                                m_core->v__DOT__pic__DOT__r_gie,
175
                                m_core->v__DOT__pic__DOT__r_int_enable,
176
                                m_core->v__DOT__pic__DOT__r_any,
177
                                m_core->v__DOT__pic__DOT__r_int_state);
178
        }
179
};
180
 
181
ZIPSIM_TB       *tb;
182
 
183
bool    iself(const char *fname) {
184
        FILE    *fp;
185
        bool    ret = true;
186
 
187 10 dgisselq
        if ((!fname)||(!fname[0]))
188 2 dgisselq
                return false;
189
 
190 10 dgisselq
        fp = fopen(fname, "rb");
191
 
192
        if (!fp)        return false;
193
        if (0x7f != fgetc(fp))  ret = false;
194
        if ('E'  != fgetc(fp))  ret = false;
195
        if ('L'  != fgetc(fp))  ret = false;
196
        if ('F'  != fgetc(fp))  ret = false;
197 2 dgisselq
        fclose(fp);
198 10 dgisselq
        return  ret;
199 2 dgisselq
}
200
 
201 10 dgisselq
long    fgetwords(FILE *fp) {
202
        // Return the number of words in the current file, and return the 
203
        // file as though it had never been adjusted
204
        long    fpos, flen;
205
        fpos = ftell(fp);
206
        if (0 != fseek(fp, 0l, SEEK_END)) {
207
                fprintf(stderr, "ERR: Could not determine file size\n");
208
                perror("O/S Err:");
209
                exit(-2);
210
        } flen = ftell(fp);
211
        if (0 != fseek(fp, fpos, SEEK_SET)) {
212
                fprintf(stderr, "ERR: Could not seek on file\n");
213
                perror("O/S Err:");
214
                exit(-2);
215
        } flen /= sizeof(BUSW);
216
        return flen;
217
}
218
 
219
class   SECTION {
220
public:
221
        unsigned        m_start, m_len;
222
        BUSW    m_data[1];
223
};
224
 
225
SECTION **singlesection(int nwords) {
226
        fprintf(stderr, "NWORDS = %d\n", nwords);
227
        size_t  sz = (2*(sizeof(SECTION)+sizeof(SECTION *))
228
                +(nwords-1)*(sizeof(BUSW)));
229
        char    *d = (char *)malloc(sz);
230
        SECTION **r = (SECTION **)d;
231
        memset(r, 0, sz);
232
        r[0] = (SECTION *)(&d[2*sizeof(SECTION *)]);
233
        r[0]->m_len   = nwords;
234
        r[1] = (SECTION *)(&r[0]->m_data[r[0]->m_len]);
235
        r[0]->m_start = 0;
236
        r[1]->m_start = 0;
237
        r[1]->m_len   = 0;
238
 
239
        return r;
240
}
241
 
242
SECTION **rawsection(const char *fname) {
243
        SECTION         **secpp, *secp;
244
        unsigned        num_words;
245
        FILE            *fp;
246
        int             nr;
247
 
248
        fp = fopen(fname, "r");
249
        if (fp == NULL) {
250
                fprintf(stderr, "Could not open: %s\n", fname);
251
                exit(-1);
252
        }
253
 
254
        if ((num_words=fgetwords(fp)) > FLASHWORDS) {
255
                fprintf(stderr, "File overruns flash memory\n");
256
                exit(-1);
257
        }
258
        secpp = singlesection(num_words);
259
        secp = secpp[0];
260
        secp->m_start = RESET_ADDRESS;
261
        secp->m_len = num_words;
262
        nr= fread(secp->m_data, sizeof(BUSW), num_words, fp);
263
        if (nr != (int)num_words) {
264
                fprintf(stderr, "Could not read entire file\n");
265
                perror("O/S Err:");
266
                exit(-2);
267
        } assert(secpp[1]->m_len == 0);
268
 
269
        return secpp;
270
}
271
 
272
unsigned        byteswap(unsigned n) {
273
        unsigned        r;
274
 
275
        r = (n&0x0ff); n>>= 8;
276
        r = (r<<8) | (n&0x0ff); n>>= 8;
277
        r = (r<<8) | (n&0x0ff); n>>= 8;
278
        r = (r<<8) | (n&0x0ff); n>>= 8;
279
 
280
        return r;
281
}
282
 
283
#include <libelf.h>
284
#include <gelf.h>
285
 
286
void    elfread(const char *fname, unsigned &entry, SECTION **&sections) {
287
        Elf     *e;
288
        int     fd, i;
289
        size_t  n;
290
        char    *id;
291
        Elf_Kind        ek;
292
        GElf_Ehdr       ehdr;
293
        GElf_Phdr       phdr;
294
        const   bool    dbg = false;
295
 
296
        if (elf_version(EV_CURRENT) == EV_NONE) {
297
                fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
298
                perror("O/S Err:");
299
                exit(EXIT_FAILURE);
300
        } if ((fd = open(fname, O_RDONLY, 0)) < 0) {
301
                fprintf(stderr, "Could not open %s\n", fname);
302
                perror("O/S Err:");
303
                exit(EXIT_FAILURE);
304
        } if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
305
                fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
306
                exit(EXIT_FAILURE);
307
        }
308
 
309
        ek = elf_kind(e);
310
        if (ek == ELF_K_ELF) {
311
                ; // This is the kind of file we should expect
312
        } else if (ek == ELF_K_AR) {
313
                fprintf(stderr, "Cannot run an archive!\n");
314
                exit(EXIT_FAILURE);
315
        } else if (ek == ELF_K_NONE) {
316
                ;
317
        } else {
318
                fprintf(stderr, "Unexpected ELF file kind!\n");
319
                exit(EXIT_FAILURE);
320
        }
321
 
322
        if (gelf_getehdr(e, &ehdr) == NULL) {
323
                fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
324
                exit(EXIT_FAILURE);
325
        } if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
326
                fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
327
                exit(EXIT_FAILURE);
328
        } if ((id = elf_getident(e, NULL)) == NULL) {
329
                fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
330
                exit(EXIT_FAILURE);
331
        } if (i != ELFCLASS32) {
332
                fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
333
                exit(EXIT_FAILURE);
334
        }
335
 
336
        if (dbg) {
337
        printf("    %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
338
        printf("    %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
339
        printf("    %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
340
        printf("    %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
341
        printf("    %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
342
        printf("    %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
343
        printf("    %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
344
        printf("    %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
345
        printf("    %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
346
        printf("    %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
347
        printf("\n");
348
        }
349
 
350
 
351
        // Check whether or not this is an ELF file for the ZipCPU ...
352
        if (ehdr.e_machine != 0x0dadd) {
353
                fprintf(stderr, "This is not a ZipCPU ELF file\n");
354
                exit(EXIT_FAILURE);
355
        }
356
 
357
        // Get our entry address
358
        entry = ehdr.e_entry;
359
 
360
 
361
        // Now, let's go look at the program header
362
        if (elf_getphdrnum(e, &n) != 0) {
363
                fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
364
                exit(EXIT_FAILURE);
365
        }
366
 
367
        unsigned total_octets = 0, current_offset=0, current_section=0;
368
        for(i=0; i<(int)n; i++) {
369
                total_octets += sizeof(SECTION *)+sizeof(SECTION);
370
 
371
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
372
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
373
                        exit(EXIT_FAILURE);
374
                }
375
 
376
                if (dbg) {
377
                printf("    %-20s 0x%x\n", "p_type",   phdr.p_type);
378
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
379
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
380
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
381
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
382
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
383
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
384
 
385
                if (phdr.p_flags & PF_X)        printf(" Execute");
386
                if (phdr.p_flags & PF_R)        printf(" Read");
387
                if (phdr.p_flags & PF_W)        printf(" Write");
388
                printf("]\n");
389
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
390
                }
391
 
392
                total_octets += phdr.p_memsz;
393
        }
394
 
395
        char    *d = (char *)malloc(total_octets + sizeof(SECTION)+sizeof(SECTION *));
396
        memset(d, 0, total_octets);
397
 
398
        SECTION **r = sections = (SECTION **)d;
399
        current_offset = (n+1)*sizeof(SECTION *);
400
        current_section = 0;
401
 
402
        for(i=0; i<(int)n; i++) {
403
                r[i] = (SECTION *)(&d[current_offset]);
404
 
405
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
406
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
407
                        exit(EXIT_FAILURE);
408
                }
409
 
410
                if (dbg) {
411
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
412
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
413
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
414
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
415
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
416
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
417
 
418
                if (phdr.p_flags & PF_X)        printf(" Execute");
419
                if (phdr.p_flags & PF_R)        printf(" Read");
420
                if (phdr.p_flags & PF_W)        printf(" Write");
421
                printf("]\n");
422
 
423
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
424
                }
425
 
426
                current_section++;
427
 
428
                r[i]->m_start = phdr.p_vaddr;
429
                r[i]->m_len   = phdr.p_filesz/ sizeof(BUSW);
430
 
431
                current_offset += phdr.p_memsz + sizeof(SECTION);
432
 
433
                // Now, let's read in our section ...
434
                if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
435
                        fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
436
                        perror("O/S Err:");
437
                        exit(EXIT_FAILURE);
438
                } if (phdr.p_filesz > phdr.p_memsz)
439
                        phdr.p_filesz = 0;
440
                if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
441
                        fprintf(stderr, "Didnt read entire section\n");
442
                        perror("O/S Err:");
443
                        exit(EXIT_FAILURE);
444
                }
445
 
446
                // Next, we need to byte swap it from big to little endian
447
                for(unsigned j=0; j<r[i]->m_len; j++)
448
                        r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
449
 
450
                if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
451
                        fprintf(stderr, "ADR[%04x] = %08x\n", r[i]->m_start+j,
452
                        r[i]->m_data[j]);
453
        }
454
 
455
        r[i] = (SECTION *)(&d[current_offset]);
456
        r[current_section]->m_start = 0;
457
        r[current_section]->m_len   = 0;
458
 
459
        elf_end(e);
460
        close(fd);
461
}
462
 
463
 
464 2 dgisselq
void    usage(void) {
465
        fprintf(stderr, "Usage: zip_sim flash_program\n");
466
}
467
 
468
int     main(int argc, char **argv) {
469
        Verilated::commandArgs(argc, argv);
470
        tb = new ZIPSIM_TB;
471
        const char      *codef = NULL;
472
 
473
        for(int argn=1; argn<argc; argn++) {
474
                if (argv[argn][0] == '-') {
475
                        usage();
476
                        exit(-1);
477
                } else
478
                        codef = argv[argn];
479
        }
480
 
481
        if ((!codef)||(!codef[0]))
482
                fprintf(stderr, "No executable code filename found!\n");
483
 
484
        if (access(codef, R_OK)!=0)
485
                fprintf(stderr, "Cannot read code filename, %s\n", codef);
486
 
487
        if (iself(codef)) {
488 10 dgisselq
                SECTION **secpp, *secp;
489
                BUSW    entry;
490
                elfread(codef, entry, secpp);
491 2 dgisselq
 
492 10 dgisselq
                for(int i=0; secpp[i]->m_len; i++) {
493
                        secp = secpp[i];
494
                        tb->m_flash.write(secp->m_start, secp->m_len, secp->m_data);
495 2 dgisselq
                }
496
        } else {
497 10 dgisselq
                tb->m_flash.load(RESET_ADDRESS, codef);
498 2 dgisselq
        }
499
 
500
        tb->reset();
501
 
502
        while(1)
503
                tb->tick();
504
 
505
        printf("SUCCESS!\n");
506
        exit(0);
507
}

powered by: WebSVN 2.1.0

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