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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [bench/] [cpp/] [zippy_tb.cpp] - Blame information for rev 4

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

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    zippy_tb.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     A bench simulator for the CPU.  Eventually, you should be
8
//              able to give this program the name of a piece of compiled
9
//              code to load into memory.  For now, we hand assemble with the
10
//              computers help.
11
//
12
//
13
// Creator:     Dan Gisselquist, Ph.D.
14
//              Gisselquist Tecnology, LLC
15
//
16
///////////////////////////////////////////////////////////////////////////////
17
//
18
// Copyright (C) 2015, Gisselquist Technology, LLC
19
//
20
// This program is free software (firmware): you can redistribute it and/or
21
// modify it under the terms of  the GNU General Public License as published
22
// by the Free Software Foundation, either version 3 of the License, or (at
23
// your option) any later version.
24
//
25
// This program is distributed in the hope that it will be useful, but WITHOUT
26
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
27
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
28
// for more details.
29
//
30
// License:     GPL, v3, as defined and found on www.gnu.org,
31
//              http://www.gnu.org/licenses/gpl.html
32
//
33
//
34
///////////////////////////////////////////////////////////////////////////////
35
//
36
//
37
#include <signal.h>
38
#include <time.h>
39
 
40
#include <ctype.h>
41
#include <ncurses.h>
42
 
43
#include "verilated.h"
44
#include "Vzipsystem.h"
45
 
46
#include "testb.h"
47
// #include "twoc.h"
48
// #include "qspiflashsim.h"
49
#include "memsim.h"
50
#include "zopcodes.h"
51
#include "zparser.h"
52
 
53
#define CMD_REG         0
54
#define CMD_DATA        1
55
#define CMD_HALT        (1<<10)
56
#define CMD_STALL       (1<<9)
57
#define CMD_STEP        (1<<8)
58
#define CMD_INT         (1<<7)
59
#define CMD_RESET       (1<<6)
60
 
61
 
62
// No particular "parameters" need definition or redefinition here.
63
class   ZIPPY_TB : public TESTB<Vzipsystem> {
64
public:
65
        unsigned long   m_tx_busy_count;
66
        MEMSIM          m_mem;
67
        // QSPIFLASHSIM m_flash;
68
        FILE            *dbg_fp;
69
        bool            dbg_flag, bomb;
70
 
71
        ZIPPY_TB(void) : m_mem(1<<20) {
72
                //dbg_fp = fopen("dbg.txt", "w");
73
                dbg_fp = NULL;
74
                dbg_flag = false;
75
                bomb = false;
76
        }
77
 
78
        void    reset(void) {
79
                // m_flash.debug(false);
80
                TESTB<Vzipsystem>::reset();
81
        }
82
 
83
        bool    on_tick(void) {
84
                tick();
85
                return true;
86
        }
87
 
88
        void    showval(int y, int x, const char *lbl, unsigned int v) {
89
                mvprintw(y,x, "%s: 0x%08x", lbl, v);
90
        }
91
 
92
        void    dispreg(int y, int x, const char *n, unsigned int v) {
93
                // 4,4,8,1 = 17 of 20, +3 = 19
94
                mvprintw(y, x, "%s: 0x%08x", n, v);
95
        }
96
 
97
        void    showreg(int y, int x, const char *n, int r) {
98
                // 4,4,8,1 = 17 of 20, +3 = 19
99
                mvprintw(y, x, "%s: 0x%08x", n, m_core->v__DOT__thecpu__DOT__regset[r]);
100
                addch( ((r == m_core->v__DOT__thecpu__DOT__dcdA)
101
                                &&(m_core->v__DOT__thecpu__DOT__dcdvalid)
102
                                &&(m_core->v__DOT__thecpu__DOT__dcdA_rd))
103
                        ?'a':' ');
104
                addch( ((r == m_core->v__DOT__thecpu__DOT__dcdB)
105
                                &&(m_core->v__DOT__thecpu__DOT__dcdvalid)
106
                                &&(m_core->v__DOT__thecpu__DOT__dcdB_rd))
107
                        ?'b':' ');
108
                addch( ((r == m_core->v__DOT__thecpu__DOT__wr_reg_id)
109
                                &&(m_core->v__DOT__thecpu__DOT__wr_reg_ce))
110
                        ?'W':' ');
111
        }
112
 
113
        void    showins(int y, const char *lbl, const int ce, const int valid,
114
                        const int gie, const int stall, const unsigned int pc) {
115
                char    line[80];
116
 
117
                if (ce)
118
                        mvprintw(y, 0, "Ck ");
119
                else
120
                        mvprintw(y, 0, "   ");
121
                if (stall)
122
                        printw("Stl ");
123
                else
124
                        printw("    ");
125
                printw("%s: 0x%08x", lbl, pc);
126
 
127
                if (valid) {
128
                        if (gie) attroff(A_BOLD);
129
                        else    attron(A_BOLD);
130
                        zipi_to_string(m_mem[pc], line);
131
                        printw("  %-20s", &line[1]);
132
                } else {
133
                        attroff(A_BOLD);
134
                        printw("  (0x%08x)%28s", m_mem[pc],"");
135
                }
136
                attroff(A_BOLD);
137
        }
138
 
139
        void    dbgins(const char *lbl, const int ce, const int valid,
140
                        const int gie, const int stall, const unsigned int pc) {
141
                char    line[80];
142
 
143
                if (!dbg_fp)
144
                        return;
145
 
146
                if (ce)
147
                        fprintf(dbg_fp, "%s Ck ", lbl);
148
                else
149
                        fprintf(dbg_fp, "%s    ", lbl);
150
                if (stall)
151
                        fprintf(dbg_fp, "Stl ");
152
                else
153
                        fprintf(dbg_fp, "    ");
154
                fprintf(dbg_fp, "0x%08x:  ", pc);
155
 
156
                if (valid) {
157
                        zipi_to_string(m_mem[pc], line);
158
                        fprintf(dbg_fp, "  %-20s\n", &line[1]);
159
                } else {
160
                        fprintf(dbg_fp, "  (0x%08x)\n", m_mem[pc]);
161
                }
162
        }
163
 
164
        void    show_state(void) {
165
                int     ln= 0;
166
 
167
                mvprintw(ln,0, "Peripherals-SS"); ln++;
168
                /*
169
                showval(ln, 1, "TRAP", m_core->v__DOT__trap_data);
170
                        mvprintw(ln, 17, "%s%s",
171
                                ((m_core->v__DOT__sys_cyc)
172
                                &&(m_core->v__DOT__sys_we)
173
                                &&(m_core->v__DOT__sys_addr == 0))?"W":" ",
174
                                (m_core->v__DOT__trap_int)?"I":" ");
175
                */
176
                showval(ln, 1, "PIC ", m_core->v__DOT__pic_data);
177
                showval(ln,21, "WDT ", m_core->v__DOT__watchdog__DOT__r_value);
178
                showval(ln,41, "CACH", m_core->v__DOT__manualcache__DOT__cache_base);
179
                showval(ln,61, "PIC2", m_core->v__DOT__ctri__DOT__r_int_state);
180
 
181
                ln++;
182
                showval(ln, 1, "TMRA", m_core->v__DOT__timer_a__DOT__r_value);
183
                showval(ln,21, "TMRB", m_core->v__DOT__timer_b__DOT__r_value);
184
                showval(ln,41, "TMRB", m_core->v__DOT__timer_c__DOT__r_value);
185
                showval(ln,61, "JIF ", m_core->v__DOT__jiffies__DOT__r_counter);
186
 
187
                ln++;
188
                showval(ln, 1, "UTSK", m_core->v__DOT__utc_data);
189
                showval(ln,21, "UMST", m_core->v__DOT__umc_data);
190
                showval(ln,41, "UPST", m_core->v__DOT__upc_data);
191
                showval(ln,61, "UAST", m_core->v__DOT__uac_data);
192
 
193
                ln++;
194
                mvprintw(ln, 40, "%s %s",
195
                        (m_core->v__DOT__cpu_halt)? "CPU-HALT": "        ",
196
                        (m_core->v__DOT__cpu_reset)?"CPU-RESET":"         "); ln++;
197
                mvprintw(ln, 40, "%s %s %s 0x%02x",
198
                        (m_core->v__DOT__cmd_halt)? "HALT": "    ",
199
                        (m_core->v__DOT__cmd_reset)?"RESET":"     ",
200
                        (m_core->v__DOT__cmd_step)? "STEP" :"    ",
201
                        (m_core->v__DOT__cmd_addr)&0x3f);
202
                if (m_core->v__DOT__thecpu__DOT__gie)
203
                        attroff(A_BOLD);
204
                else
205
                        attron(A_BOLD);
206
                mvprintw(ln, 0, "Supervisor Registers");
207
                ln++;
208
 
209
                showreg(ln, 1, "sR0 ", 0);
210
                showreg(ln,21, "sR1 ", 1);
211
                showreg(ln,41, "sR2 ", 2);
212
                showreg(ln,61, "sR3 ", 3); ln++;
213
 
214
                showreg(ln, 1, "sR4 ", 4);
215
                showreg(ln,21, "sR5 ", 5);
216
                showreg(ln,41, "sR6 ", 6);
217
                showreg(ln,61, "sR7 ", 7); ln++;
218
 
219
                showreg(ln, 1, "sR8 ",  8);
220
                showreg(ln,21, "sR9 ",  9);
221
                showreg(ln,41, "sR10", 10);
222
                showreg(ln,61, "sR11", 11); ln++;
223
 
224
                showreg(ln, 1, "sR12", 12);
225
                showreg(ln,21, "sSP ", 13);
226
                mvprintw(ln,41, "sCC :%s%s%s%s%s%s%s",
227
                        (m_core->v__DOT__thecpu__DOT__step)?"STP":"   ",
228
                        (m_core->v__DOT__thecpu__DOT__sleep)?"SLP":"   ",
229
                        (m_core->v__DOT__thecpu__DOT__gie)?"GIE":"   ",
230
                        (m_core->v__DOT__thecpu__DOT__iflags&8)?"V":" ",
231
                        (m_core->v__DOT__thecpu__DOT__iflags&4)?"N":" ",
232
                        (m_core->v__DOT__thecpu__DOT__iflags&2)?"C":" ",
233
                        (m_core->v__DOT__thecpu__DOT__iflags&1)?"Z":" ");
234
                mvprintw(ln,61, "sPC : 0x%08x", m_core->v__DOT__thecpu__DOT__ipc);
235
                ln++;
236
 
237
                if (m_core->v__DOT__thecpu__DOT__gie)
238
                        attron(A_BOLD);
239
                else
240
                        attroff(A_BOLD);
241
                mvprintw(ln, 0, "User Registers"); ln++;
242
                showreg(ln, 1, "uR0 ", 16);
243
                showreg(ln,21, "uR1 ", 17);
244
                showreg(ln,41, "uR2 ", 18);
245
                showreg(ln,61, "uR3 ", 19); ln++;
246
 
247
                showreg(ln, 1, "uR4 ", 20);
248
                showreg(ln,21, "uR5 ", 21);
249
                showreg(ln,41, "uR6 ", 22);
250
                showreg(ln,61, "uR7 ", 23); ln++;
251
 
252
                showreg(ln, 1, "uR8 ", 24);
253
                showreg(ln,21, "uR9 ", 25);
254
                showreg(ln,41, "uR10", 26);
255
                showreg(ln,61, "uR11", 27); ln++;
256
 
257
                showreg(ln, 1, "uR12", 28);
258
                showreg(ln,21, "uSP ", 29);
259
                mvprintw(ln,41, "uCC :%s%s%s%s%s%s%s",
260
                        (m_core->v__DOT__thecpu__DOT__step)?"STP":"   ",
261
                        (m_core->v__DOT__thecpu__DOT__sleep)?"SLP":"   ",
262
                        (m_core->v__DOT__thecpu__DOT__gie)?"GIE":"   ",
263
                        (m_core->v__DOT__thecpu__DOT__flags&8)?"V":" ",
264
                        (m_core->v__DOT__thecpu__DOT__flags&4)?"N":" ",
265
                        (m_core->v__DOT__thecpu__DOT__flags&2)?"C":" ",
266
                        (m_core->v__DOT__thecpu__DOT__flags&1)?"Z":" ");
267
                mvprintw(ln,61, "uPC : 0x%08x", m_core->v__DOT__thecpu__DOT__upc);
268
 
269
                attroff(A_BOLD);
270
                ln+=1;
271
 
272 4 dgisselq
                mvprintw(ln, 0, "PFPIPE: rda=%08x/%d, bas=%08x, off=%08x, nv=%03x, ackw=%d",
273 2 dgisselq
                        m_core->v__DOT__thecpu__DOT__pf__DOT__r_addr,
274
                        m_core->v__DOT__thecpu__DOT__pf__DOT__r_cv,
275
                        m_core->v__DOT__thecpu__DOT__pf__DOT__r_cache_base,
276
                        m_core->v__DOT__thecpu__DOT__pf__DOT__r_cache_offset,
277 4 dgisselq
                        m_core->v__DOT__thecpu__DOT__pf__DOT__r_nvalid,
278
                        m_core->v__DOT__thecpu__DOT__pf__DOT__r_acks_waiting);
279 2 dgisselq
                ln++;
280
                mvprintw(ln, 0, "PF BUS: %3s %3s %s @0x%08x[0x%08x] -> %s %s %08x",
281
                        (m_core->v__DOT__thecpu__DOT__pf_cyc)?"CYC":"   ",
282
                        (m_core->v__DOT__thecpu__DOT__pf_stb)?"STB":"   ",
283
                        "  ", // (m_core->v__DOT__thecpu__DOT__pf_we )?"WE":"  ",
284
                        (m_core->v__DOT__thecpu__DOT__pf_addr),
285
                        0, // (m_core->v__DOT__thecpu__DOT__pf_data),
286
                        (m_core->v__DOT__thecpu__DOT__pf_ack)?"ACK":"   ",
287
                        (m_core->v__DOT__cpu_stall)?"STL":"   ",
288
                        (m_core->v__DOT__wb_data)); ln++;
289
 
290
                mvprintw(ln, 0, "MEMBUS: %3s %3s %s @0x%08x[0x%08x] -> %s %s %08x",
291
                        (m_core->v__DOT__thecpu__DOT__mem_cyc)?"CYC":"   ",
292
                        (m_core->v__DOT__thecpu__DOT__mem_stb)?"STB":"   ",
293
                        (m_core->v__DOT__thecpu__DOT__mem_we )?"WE":"  ",
294
                        (m_core->v__DOT__thecpu__DOT__mem_addr),
295
                        (m_core->v__DOT__thecpu__DOT__mem_data),
296
                        (m_core->v__DOT__thecpu__DOT__mem_ack)?"ACK":"   ",
297
                        (m_core->v__DOT__cpu_stall)?"STL":"   ",
298
                        (m_core->v__DOT__thecpu__DOT__mem_result)); ln++;
299
 
300
                mvprintw(ln, 0, "SYSBUS: %3s %3s %s @0x%08x[0x%08x] -> %s %s %08x",
301
                        (m_core->o_wb_cyc)?"CYC":"   ",
302
                        (m_core->o_wb_stb)?"STB":"   ",
303
                        (m_core->o_wb_we )?"WE":"  ",
304
                        (m_core->o_wb_addr),
305
                        (m_core->o_wb_data),
306
                        (m_core->i_wb_ack)?"ACK":"   ",
307
                        (m_core->i_wb_stall)?"STL":"   ",
308
                        (m_core->i_wb_data)); ln+=2;
309
 
310
                showins(ln, "I ",
311
                        !m_core->v__DOT__thecpu__DOT__dcd_stalled,
312
                        m_core->v__DOT__thecpu__DOT__pf_valid,
313
                        //m_core->v__DOT__thecpu__DOT__instruction_gie,
314
                        m_core->v__DOT__thecpu__DOT__gie,
315
                        0,
316
                        // m_core->v__DOT__thecpu__DOT__instruction_pc); ln++;
317
                        m_core->v__DOT__thecpu__DOT__pf_pc); ln++;
318
 
319
                showins(ln, "Dc",
320
                        m_core->v__DOT__thecpu__DOT__dcd_ce,
321
                        m_core->v__DOT__thecpu__DOT__dcdvalid,
322
                        m_core->v__DOT__thecpu__DOT__dcd_gie,
323
                        m_core->v__DOT__thecpu__DOT__dcd_stalled,
324
                        m_core->v__DOT__thecpu__DOT__dcd_pc-1); ln++;
325
 
326
                showins(ln, "Op",
327
                        m_core->v__DOT__thecpu__DOT__op_ce,
328
                        m_core->v__DOT__thecpu__DOT__opvalid,
329
                        m_core->v__DOT__thecpu__DOT__op_gie,
330
                        m_core->v__DOT__thecpu__DOT__op_stall,
331
                        m_core->v__DOT__thecpu__DOT__op_pc-1); ln++;
332
 
333
                showins(ln, "Al",
334
                        m_core->v__DOT__thecpu__DOT__alu_ce,
335
                        m_core->v__DOT__thecpu__DOT__alu_pc_valid,
336
                        m_core->v__DOT__thecpu__DOT__alu_gie,
337
                        m_core->v__DOT__thecpu__DOT__alu_stall,
338
                        m_core->v__DOT__thecpu__DOT__alu_pc-1); ln++;
339
 
340
                mvprintw(ln-4, 48,
341
                        (m_core->v__DOT__thecpu__DOT__new_pc)?"new-pc":"      ");
342
                printw("(%s:%02x,%x)",
343
                        (m_core->v__DOT__thecpu__DOT__set_cond)?"SET":"   ",
344
                        (m_core->v__DOT__thecpu__DOT__opF&0x0ff),
345
                        (m_core->v__DOT__thecpu__DOT__op_gie)
346
                                ?  (m_core->v__DOT__thecpu__DOT__w_uflags)
347
                                : (m_core->v__DOT__thecpu__DOT__w_iflags));
348
 
349
                printw("(%s%s%s:%02x)",
350
                        (m_core->v__DOT__thecpu__DOT__opF_wr)?"OF":"  ",
351
                        (m_core->v__DOT__thecpu__DOT__alF_wr)?"FL":"  ",
352
                        (m_core->v__DOT__thecpu__DOT__wr_flags_ce)?"W":" ",
353
                        (m_core->v__DOT__thecpu__DOT__alu_flags));
354
                /*
355
                mvprintw(ln-3, 48, "dcdI : 0x%08x",
356
                        m_core->v__DOT__thecpu__DOT__dcdI);
357
                mvprintw(ln-2, 48, "r_opB: 0x%08x",
358
                        m_core->v__DOT__thecpu__DOT__opB);
359
                */
360
                mvprintw(ln-3, 48, "Op(%x)%8x %8x->%08x",
361
                        m_core->v__DOT__thecpu__DOT__opn,
362
                        m_core->v__DOT__thecpu__DOT__opA,
363
                        m_core->v__DOT__thecpu__DOT__opB,
364
                        m_core->v__DOT__thecpu__DOT__alu_result);
365
                mvprintw(ln-1, 48, "MEM: %s%s %s%s %s %-5s",
366
                        (m_core->v__DOT__thecpu__DOT__opM)?"M":" ",
367
                        (m_core->v__DOT__thecpu__DOT__mem_ce)?"CE":"  ",
368
                        (m_core->v__DOT__thecpu__DOT__mem_we)?"Wr ":"Rd ",
369
                        (m_core->v__DOT__thecpu__DOT__mem_stalled)?"PIPE":"    ",
370
                        (m_core->v__DOT__thecpu__DOT__mem_valid)?"MEMV":"    ",
371
                        zop_regstr[(m_core->v__DOT__thecpu__DOT__mem_wreg&0x1f)^0x10]);
372
        }
373
 
374
        unsigned int    cmd_read(unsigned int a) {
375
                if (dbg_fp) {
376
                        dbg_flag= true;
377
                        fprintf(dbg_fp, "CMD-READ(%d)\n", a);
378
                }
379
                wb_write(CMD_REG, CMD_HALT|(a&0x3f));
380
                while((wb_read(CMD_REG) & CMD_STALL) == 0)
381
                        ;
382
                unsigned int v = wb_read(CMD_DATA);
383
 
384
                if (dbg_flag)
385
                        fprintf(dbg_fp, "CMD-READ(%d) = 0x%08x\n", a,
386
                                v);
387
                dbg_flag = false;
388
                return v;
389
        }
390
 
391
        void    read_state(void) {
392
                int     ln= 0;
393
 
394
                mvprintw(ln,0, "Peripherals-RS"); ln++;
395
                showval(ln, 1, "PIC ", cmd_read(32+ 0));
396
                showval(ln,21, "WDT ", cmd_read(32+ 1));
397
                showval(ln,41, "CACH", cmd_read(32+ 2));
398
                showval(ln,61, "PIC2", cmd_read(32+ 3));
399
                ln++;
400
                showval(ln, 1, "TMRA", cmd_read(32+ 4));
401
                showval(ln,21, "TMRB", cmd_read(32+ 5));
402
                showval(ln,41, "TMRC", cmd_read(32+ 6));
403
                showval(ln,61, "JIF ", cmd_read(32+ 7));
404
 
405
                ln++;
406
                showval(ln, 1, "UTSK", cmd_read(32+12));
407
                showval(ln,21, "UMST", cmd_read(32+13));
408
                showval(ln,41, "UPST", cmd_read(32+14));
409
                showval(ln,61, "UAST", cmd_read(32+15));
410
 
411
                ln++;
412
                ln++;
413
                unsigned int cc = cmd_read(14);
414
                if (dbg_fp) fprintf(dbg_fp, "CC = %08x, gie = %d\n", cc,
415
                        m_core->v__DOT__thecpu__DOT__gie);
416
                if (cc & 0x020)
417
                        attroff(A_BOLD);
418
                else
419
                        attron(A_BOLD);
420
                mvprintw(ln, 0, "Supervisor Registers");
421
                ln++;
422
 
423
                dispreg(ln, 1, "sR0 ", cmd_read(0));
424
                dispreg(ln,21, "sR1 ", cmd_read(1));
425
                dispreg(ln,41, "sR2 ", cmd_read(2));
426
                dispreg(ln,61, "sR3 ", cmd_read(3)); ln++;
427
 
428
                dispreg(ln, 1, "sR4 ", cmd_read(4));
429
                dispreg(ln,21, "sR5 ", cmd_read(5));
430
                dispreg(ln,41, "sR6 ", cmd_read(6));
431
                dispreg(ln,61, "sR7 ", cmd_read(7)); ln++;
432
 
433
                dispreg(ln, 1, "sR8 ", cmd_read( 8));
434
                dispreg(ln,21, "sR9 ", cmd_read( 9));
435
                dispreg(ln,41, "sR10", cmd_read(10));
436
                dispreg(ln,61, "sR11", cmd_read(11)); ln++;
437
 
438
                dispreg(ln, 1, "sR12", cmd_read(12));
439
                dispreg(ln,21, "sSP ", cmd_read(13));
440
 
441
                mvprintw(ln,41, "sCC :%s%s%s%s%s%s%s",
442
                        (cc & 0x040)?"STP":"   ",
443
                        (cc & 0x020)?"GIE":"   ",
444
                        (cc & 0x010)?"SLP":"   ",
445
                        (cc&8)?"V":" ",
446
                        (cc&4)?"N":" ",
447
                        (cc&2)?"C":" ",
448
                        (cc&1)?"Z":" ");
449
                mvprintw(ln,61, "sPC : 0x%08x", cmd_read(15));
450
                ln++;
451
 
452
                if (cc & 0x020)
453
                        attron(A_BOLD);
454
                else
455
                        attroff(A_BOLD);
456
                mvprintw(ln, 0, "User Registers"); ln++;
457
                dispreg(ln, 1, "uR0 ", cmd_read(16));
458
                dispreg(ln,21, "uR1 ", cmd_read(17));
459
                dispreg(ln,41, "uR2 ", cmd_read(18));
460
                dispreg(ln,61, "uR3 ", cmd_read(19)); ln++;
461
 
462
                dispreg(ln, 1, "uR4 ", cmd_read(20));
463
                dispreg(ln,21, "uR5 ", cmd_read(21));
464
                dispreg(ln,41, "uR6 ", cmd_read(22));
465
                dispreg(ln,61, "uR7 ", cmd_read(23)); ln++;
466
 
467
                dispreg(ln, 1, "uR8 ", cmd_read(24));
468
                dispreg(ln,21, "uR9 ", cmd_read(25));
469
                dispreg(ln,41, "uR10", cmd_read(26));
470
                dispreg(ln,61, "uR11", cmd_read(27)); ln++;
471
 
472
                dispreg(ln, 1, "uR12", cmd_read(28));
473
                dispreg(ln,21, "uSP ", cmd_read(29));
474
                cc = cmd_read(30);
475
                mvprintw(ln,41, "uCC :%s%s%s%s%s%s%s",
476
                        (cc&0x040)?"STP":"   ",
477
                        (cc&0x020)?"GIE":"   ",
478
                        (cc&0x010)?"SLP":"   ",
479
                        (cc&8)?"V":" ",
480
                        (cc&4)?"N":" ",
481
                        (cc&2)?"C":" ",
482
                        (cc&1)?"Z":" ");
483
                mvprintw(ln,61, "uPC : 0x%08x", cmd_read(31));
484
 
485
                attroff(A_BOLD);
486
                ln+=2;
487
 
488
                ln+=3;
489
 
490
                showins(ln, "I ",
491
                        !m_core->v__DOT__thecpu__DOT__dcd_stalled,
492
                        m_core->v__DOT__thecpu__DOT__pf_valid,
493
                        m_core->v__DOT__thecpu__DOT__gie,
494
                        0,
495
                        // m_core->v__DOT__thecpu__DOT__instruction_pc); ln++;
496
                        m_core->v__DOT__thecpu__DOT__pf_pc); ln++;
497
 
498
                showins(ln, "Dc",
499
                        m_core->v__DOT__thecpu__DOT__dcd_ce,
500
                        m_core->v__DOT__thecpu__DOT__dcdvalid,
501
                        m_core->v__DOT__thecpu__DOT__dcd_gie,
502
                        m_core->v__DOT__thecpu__DOT__dcd_stalled,
503
                        m_core->v__DOT__thecpu__DOT__dcd_pc-1); ln++;
504
 
505
                showins(ln, "Op",
506
                        m_core->v__DOT__thecpu__DOT__op_ce,
507
                        m_core->v__DOT__thecpu__DOT__opvalid,
508
                        m_core->v__DOT__thecpu__DOT__op_gie,
509
                        m_core->v__DOT__thecpu__DOT__op_stall,
510
                        m_core->v__DOT__thecpu__DOT__op_pc-1); ln++;
511
 
512
                showins(ln, "Al",
513
                        m_core->v__DOT__thecpu__DOT__alu_ce,
514
                        m_core->v__DOT__thecpu__DOT__alu_pc_valid,
515
                        m_core->v__DOT__thecpu__DOT__alu_gie,
516
                        m_core->v__DOT__thecpu__DOT__alu_stall,
517
                        m_core->v__DOT__thecpu__DOT__alu_pc-1); ln++;
518
        }
519
        void    tick(void) {
520
                int gie = m_core->v__DOT__thecpu__DOT__gie;
521
                /*
522
                m_core->i_qspi_dat = m_flash(m_core->o_qspi_cs_n,
523
                                                m_core->o_qspi_sck,
524
                                                m_core->o_qspi_dat);
525
                */
526
 
527
                m_mem(m_core->o_wb_cyc, m_core->o_wb_stb, m_core->o_wb_we,
528
                        m_core->o_wb_addr & ((1<<20)-1), m_core->o_wb_data,
529
                        m_core->i_wb_ack, m_core->i_wb_stall,m_core->i_wb_data);
530
 
531
                if ((dbg_flag)&&(dbg_fp)) {
532
                        fprintf(dbg_fp, "DBG  %s %s %s @0x%08x/%d[0x%08x] %s %s [0x%08x] %s %s %s%s%s%s%s%s%s%s\n",
533
                                (m_core->i_dbg_cyc)?"CYC":"   ",
534
                                (m_core->i_dbg_stb)?"STB":
535
                                        ((m_core->v__DOT__dbg_stb)?"DBG":"   "),
536
                                ((m_core->i_dbg_we)?"WE":"  "),
537
                                (m_core->i_dbg_addr),0,
538
                                m_core->i_dbg_data,
539
                                (m_core->o_dbg_ack)?"ACK":"   ",
540
                                (m_core->o_dbg_stall)?"STALL":"     ",
541
                                (m_core->o_dbg_data),
542
                                (m_core->v__DOT__cpu_halt)?"CPU-HALT ":"",
543
                                (m_core->v__DOT__cpu_dbg_stall)?"CPU-DBG_STALL":"",
544
                                (m_core->v__DOT__thecpu__DOT__dcdvalid)?"DCDV ":"",
545
                                (m_core->v__DOT__thecpu__DOT__opvalid)?"OPV ":"",
546
                                (m_core->v__DOT__thecpu__DOT__pf_cyc)?"PCYC ":"",
547
                                (m_core->v__DOT__thecpu__DOT__mem_cyc)?"MCYC ":"",
548
                                (m_core->v__DOT__thecpu__DOT__alu_wr)?"ALUW ":"",
549
                                (m_core->v__DOT__thecpu__DOT__alu_ce)?"ALCE ":"",
550
                                (m_core->v__DOT__thecpu__DOT__alu_valid)?"ALUV ":"",
551
                                (m_core->v__DOT__thecpu__DOT__mem_valid)?"MEMV ":"");
552
                        fprintf(dbg_fp, " SYS %s %s %s @0x%08x/%d[0x%08x] %s [0x%08x]\n",
553
                                (m_core->v__DOT__sys_cyc)?"CYC":"   ",
554
                                (m_core->v__DOT__sys_stb)?"STB":"   ",
555
                                (m_core->v__DOT__sys_we)?"WE":"  ",
556
                                (m_core->v__DOT__sys_addr),
557
                                (m_core->v__DOT__dbg_addr),
558
                                (m_core->v__DOT__sys_data),
559
                                (m_core->v__DOT__dbg_ack)?"ACK":"   ",
560
                                (m_core->v__DOT__wb_data));
561
                }
562
 
563
                if (dbg_fp)
564
                        fprintf(dbg_fp, "CEs %d/0x%08x,%d/0x%08x DCD: ->%02x, OP: ->%02x, ALU: halt=%d,%d ce=%d, valid=%d, wr=%d  Reg=%02x, IPC=%08x, UPC=%08x\n",
565
                                m_core->v__DOT__thecpu__DOT__dcd_ce,
566
                                m_core->v__DOT__thecpu__DOT__dcd_pc,
567
                                m_core->v__DOT__thecpu__DOT__op_ce,
568
                                m_core->v__DOT__thecpu__DOT__op_pc,
569
                                m_core->v__DOT__thecpu__DOT__dcdA,
570
                                m_core->v__DOT__thecpu__DOT__opR,
571
                                m_core->v__DOT__cmd_halt,
572
                                m_core->v__DOT__cpu_halt,
573
                                m_core->v__DOT__thecpu__DOT__alu_ce,
574
                                m_core->v__DOT__thecpu__DOT__alu_valid,
575
                                m_core->v__DOT__thecpu__DOT__alu_wr,
576
                                m_core->v__DOT__thecpu__DOT__alu_reg,
577
                                m_core->v__DOT__thecpu__DOT__ipc,
578
                                m_core->v__DOT__thecpu__DOT__upc);
579
 
580
                if ((dbg_fp)&&(!gie)&&(m_core->v__DOT__thecpu__DOT__w_release_from_interrupt)) {
581
                        fprintf(dbg_fp, "RELEASE: int=%d, %d/%02x[%08x] ?/%02x[0x%08x], ce=%d %d,%d,%d\n",
582
                                m_core->v__DOT__pic_interrupt,
583
                                m_core->v__DOT__thecpu__DOT__wr_reg_ce,
584
                                m_core->v__DOT__thecpu__DOT__wr_reg_id,
585
                                m_core->v__DOT__thecpu__DOT__wr_reg_vl,
586
                                m_core->v__DOT__cmd_addr,
587
                                m_core->v__DOT__dbg_idata,
588
                                m_core->v__DOT__thecpu__DOT__master_ce,
589
                                m_core->v__DOT__thecpu__DOT__alu_wr,
590
                                m_core->v__DOT__thecpu__DOT__alu_valid,
591
                                m_core->v__DOT__thecpu__DOT__mem_valid);
592
                } else if ((dbg_fp)&&(gie)&&(m_core->v__DOT__thecpu__DOT__w_switch_to_interrupt)) {
593
                        fprintf(dbg_fp, "SWITCH: %d/%02x[%08x] ?/%02x[0x%08x], ce=%d %d,%d,%d, F%02x,%02x\n",
594
                                m_core->v__DOT__thecpu__DOT__wr_reg_ce,
595
                                m_core->v__DOT__thecpu__DOT__wr_reg_id,
596
                                m_core->v__DOT__thecpu__DOT__wr_reg_vl,
597
                                m_core->v__DOT__cmd_addr,
598
                                m_core->v__DOT__dbg_idata,
599
                                m_core->v__DOT__thecpu__DOT__master_ce,
600
                                m_core->v__DOT__thecpu__DOT__alu_wr,
601
                                m_core->v__DOT__thecpu__DOT__alu_valid,
602
                                m_core->v__DOT__thecpu__DOT__mem_valid,
603
                                m_core->v__DOT__thecpu__DOT__w_iflags,
604
                                m_core->v__DOT__thecpu__DOT__w_uflags);
605
                        fprintf(dbg_fp, "\tbrk=%d,%d\n",
606
                                m_core->v__DOT__thecpu__DOT__break_en,
607
                                m_core->v__DOT__thecpu__DOT__op_break);
608
                }
609
 
610
                TESTB<Vzipsystem>::tick();
611
                if ((dbg_fp)&&(gie != m_core->v__DOT__thecpu__DOT__gie)) {
612
                        fprintf(dbg_fp, "SWITCH FROM %s to %s: sPC = 0x%08x uPC = 0x%08x pf_pc = 0x%08x\n",
613
                                (gie)?"User":"Supervisor",
614
                                (gie)?"Supervisor":"User",
615
                                m_core->v__DOT__thecpu__DOT__ipc,
616
                                m_core->v__DOT__thecpu__DOT__upc,
617
                                m_core->v__DOT__thecpu__DOT__pf_pc);
618
                } if (dbg_fp) {
619
                        dbgins("Op - ", m_core->v__DOT__thecpu__DOT__op_ce,
620
                                m_core->v__DOT__thecpu__DOT__opvalid,
621
                                m_core->v__DOT__thecpu__DOT__op_gie,
622
                                m_core->v__DOT__thecpu__DOT__op_stall,
623
                                m_core->v__DOT__thecpu__DOT__op_pc-1);
624
                        dbgins("Al - ",
625
                                m_core->v__DOT__thecpu__DOT__alu_ce,
626
                                m_core->v__DOT__thecpu__DOT__alu_pc_valid,
627
                                m_core->v__DOT__thecpu__DOT__alu_gie,
628
                                m_core->v__DOT__thecpu__DOT__alu_stall,
629
                                m_core->v__DOT__thecpu__DOT__alu_pc-1);
630
 
631
                }
632
 
633
                if (m_core->v__DOT__cpu_dbg_we) {
634
                        printf("WRITE-ENABLE!!\n");
635
                        bomb = true;
636
                }
637
        }
638
 
639
        bool    test_success(void) {
640
                return ((!m_core->v__DOT__thecpu__DOT__gie)
641
                        &&(m_core->v__DOT__thecpu__DOT__sleep));
642
        }
643
 
644
        bool    test_failure(void) {
645
                return ((m_core->v__DOT__thecpu__DOT__alu_pc_valid)
646
                        &&(!m_core->v__DOT__thecpu__DOT__alu_gie)
647
                        &&(m_mem[m_core->v__DOT__thecpu__DOT__alu_pc-1]
648
                                == 0x2f0f7fff));
649
        }
650
 
651
        void    wb_write(unsigned a, unsigned int v) {
652
                mvprintw(0,35, "%40s", "");
653
                mvprintw(0,40, "wb_write(%d,%x)", a, v);
654
                m_core->i_dbg_cyc = 1;
655
                m_core->i_dbg_stb = 1;
656
                m_core->i_dbg_we  = 1;
657
                m_core->i_dbg_addr = a & 1;
658
                m_core->i_dbg_data = v;
659
 
660
                tick();
661
                while(m_core->o_dbg_stall)
662
                        tick();
663
 
664
                m_core->i_dbg_stb = 0;
665
                while(!m_core->o_dbg_ack)
666
                        tick();
667
 
668
                // Release the bus
669
                m_core->i_dbg_cyc = 0;
670
                m_core->i_dbg_stb = 0;
671
                tick();
672
                mvprintw(0,35, "%40s", "");
673
                mvprintw(0,40, "wb_write -- complete");
674
        }
675
 
676
        unsigned long   wb_read(unsigned a) {
677
                unsigned int    v;
678
                mvprintw(0,35, "%40s", "");
679
                mvprintw(0,40, "wb_read(0x%08x)", a);
680
                m_core->i_dbg_cyc = 1;
681
                m_core->i_dbg_stb = 1;
682
                m_core->i_dbg_we  = 0;
683
                m_core->i_dbg_addr = a & 1;
684
 
685
                tick();
686
                while(m_core->o_dbg_stall)
687
                        tick();
688
 
689
                m_core->i_dbg_stb = 0;
690
                while(!m_core->o_dbg_ack)
691
                        tick();
692
                v = m_core->o_dbg_data;
693
 
694
                // Release the bus
695
                m_core->i_dbg_cyc = 0;
696
                m_core->i_dbg_stb = 0;
697
                tick();
698
 
699
                mvprintw(0,35, "%40s", "");
700
                mvprintw(0,40, "wb_read = 0x%08x", v);
701
 
702
                return v;
703
        }
704
 
705
};
706
 
707
 
708
int     main(int argc, char **argv) {
709
        Verilated::commandArgs(argc, argv);
710
        ZIPPY_TB        *tb = new ZIPPY_TB();
711
        ZPARSER         zp;
712
 
713
        printf("uCC = %d\n", (int)zp.ZIP_uCC);
714
        printf("MOV CC,R0 = 0x%08x\n", zp.op_mov(0,zp.ZIP_uCC, zp.ZIP_R0));
715
                // = 0x200e8000
716
                // Op = 0x2
717
                // Result = 0x0, R0 (Supervisor/default)
718
                // Cond   = 0x0
719
                // BReg   = 0xe (CC)
720
                // BMap   = 1, BReg = uCC
721
                //
722
 
723
        initscr();
724
        raw();
725
        noecho();
726
        keypad(stdscr, true);
727
 
728
        // mem[0x00000] = 0xbe000010; // Halt instruction
729
        unsigned int mptr = 0;
730
        /*
731
        tb->m_mem[mptr++] = 0x30000000; //  0: CLR R0
732
        tb->m_mem[mptr++] = 0x21000000; //  1: MOV R0,R1
733
        tb->m_mem[mptr++] = 0x22000001; //  2: MOV $1+R0,R2
734
        tb->m_mem[mptr++] = 0x23000002; //  3: MOV $2+R0,R3
735
        tb->m_mem[mptr++] = 0x24000022; //  4: MOV $22h+R0,R4
736
        tb->m_mem[mptr++] = 0x25100377; //  5: MOV $377h+R0,uR5
737
        tb->m_mem[mptr++] = 0x4e000000; //  6: NOOP
738
        tb->m_mem[mptr++] = 0xa0120000; //  7: ADD R2,R0
739
        tb->m_mem[mptr++] = 0xa0000020; //  8: ADD $32,R0
740
        tb->m_mem[mptr++] = 0xa00fffdf; //  9: ADD -$33,R0
741
        tb->m_mem[mptr++] = 0xc02fffff; //  A: NOT.Z R0
742
        tb->m_mem[mptr++] = 0xc0100000; //  B: CLRF R0
743
        tb->m_mem[mptr++] = 0x31000005; //  C: LDI $5,R1
744
        tb->m_mem[mptr++] = 0x00110000; //  D: CMP R0,R1
745
        tb->m_mem[mptr++] = 0xc0afffff; //  E: NOT.LT R0
746
        tb->m_mem[mptr++] = 0xc1cfffff; //  F: NOT.GE R1
747
        tb->m_mem[mptr++] = 0x621ffff9; // 10: LOD $-7(PC),R2
748
        tb->m_mem[mptr++] = 0x4f13dead; // 11: LODIHI $deadh,R3
749
        tb->m_mem[mptr++] = 0x4f03beef; // 12: LODILO $beefh,R3
750
        tb->m_mem[mptr++] = 0x731f0002; // 13: STO R3,$2(PC)
751
        */
752
 
753
        /*
754
        tb->m_mem[mptr++] = zp.op_clr(zp::ZIP_R12);//  0: CLR R12
755
        tb->m_mem[mptr++] = 0x4f1cc000; //  1: LODIHI $c000h,R12
756
        tb->m_mem[mptr++] = 0x2c1c0000; //  2: MOV R12,uR12
757
        tb->m_mem[mptr++] = 0x2f1f000a; //  3: MOV $12+PC,uPC
758
        tb->m_mem[mptr++] = 0x4f108001; //  4: LODIHI $8001,R0 // Turn on trap
759
        tb->m_mem[mptr++] = 0x4f00ffff; //  5: LODILO $ffff,R0 // interrupts
760
        tb->m_mem[mptr++] = 0x701c0001; //  6: STO R0,$1(R12)
761
        tb->m_mem[mptr++] = 0xbe000020; //  7: RTU      // Switch to user mode
762
        tb->m_mem[mptr++] = 0x601c0000; //  8: LOD (R12),R0 // Check the result
763
        tb->m_mem[mptr++] = 0x00000000; //  A: CMP $0,R0
764
        tb->m_mem[mptr++] = 0x2f4f0001; //  B: BNZ $1+PC
765
        tb->m_mem[mptr++] = 0xbe000010; //  C: HALT     // On SUCCESS
766
        tb->m_mem[mptr++] = 0x2f0f7fff; //  D: BRA PC-1 // On FAILURE
767
        */
768
 
769
 
770
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R0); //  0: CLR R0
771
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIP_R0,zp.ZIP_R1); //  1: MOV R0,R1
772
        tb->m_mem[mptr++] = zp.op_mov(1,zp.ZIP_R0,zp.ZIP_R2); //  2: MOV $1+R0,R2
773
        tb->m_mem[mptr++] = zp.op_mov(2,zp.ZIP_R0,zp.ZIP_R3); //  3: MOV $2+R0,R3
774
        tb->m_mem[mptr++] = zp.op_mov(0x022, zp.ZIP_R0, zp.ZIP_R4); //  4: MOV $22h+R0,R4
775
        tb->m_mem[mptr++] = zp.op_mov(0x377, zp.ZIP_R0, zp.ZIP_uR5); //  5: MOV $377h+R0,uR5
776
        tb->m_mem[mptr++] = zp.op_noop(); //  6: NOOP
777
        tb->m_mem[mptr++] = zp.op_add(0,zp.ZIP_R2,zp.ZIP_R0); //  7: ADD R2,R0
778
        tb->m_mem[mptr++] = zp.op_add(32,zp.ZIP_R0); //  8: ADD $32,R0
779
        tb->m_mem[mptr++] = zp.op_add(-33,zp.ZIP_R0); //  9: ADD -$33,R0
780
        tb->m_mem[mptr++] = zp.op_not(zp.ZIPC_Z, zp.ZIP_R0); //  A: NOT.Z R0
781
        tb->m_mem[mptr++] = zp.op_clrf(zp.ZIP_R0); //  B: CLRF R0
782
        tb->m_mem[mptr++] = zp.op_ldi(5,zp.ZIP_R1); //  C: LDI $5,R1
783
        tb->m_mem[mptr++] = zp.op_cmp(0,zp.ZIP_R0,zp.ZIP_R1); //  D: CMP R0,R1
784
        tb->m_mem[mptr++] = zp.op_not(zp.ZIPC_LT, zp.ZIP_R0); //  E: NOT.LT R0
785
        tb->m_mem[mptr++] = zp.op_not(zp.ZIPC_GE, zp.ZIP_R1); //  F: NOT.GE R1
786
        tb->m_mem[mptr++] = zp.op_lod(-7,zp.ZIP_PC, zp.ZIP_R2); // 10: LOD $-7(PC),R2
787
        tb->m_mem[mptr++] = zp.op_ldihi(0xdead, zp.ZIP_R3); // 11: LODIHI $deadh,R3
788
        tb->m_mem[mptr++] = zp.op_ldilo(0xbeef, zp.ZIP_R3); // 12: LODILO $beefh,R3
789
 
790
        // Let's build a software test bench.
791
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R12);//  0: CLR R12
792
        tb->m_mem[mptr++] = zp.op_ldihi(0xc000,zp.ZIP_R12);
793
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIP_R12,zp.ZIP_uR12);
794
        tb->m_mem[mptr++] = zp.op_mov(10,zp.ZIP_PC,zp.ZIP_uPC);
795
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R0); // Clear R0, and disable ints
796
        tb->m_mem[mptr++] = zp.op_sto(zp.ZIP_R0,0,zp.ZIP_R12);
797
        tb->m_mem[mptr++] = zp.op_rtu(); //  7: RTU     // Switch to user mode
798
        tb->m_mem[mptr++] = zp.op_mov(0,zp.ZIP_uCC, zp.ZIP_R0); // Check result
799
        tb->m_mem[mptr++] = zp.op_tst(-256,zp.ZIP_R0);
800
        tb->m_mem[mptr++] = zp.op_bnz(1);
801
        tb->m_mem[mptr++] = zp.op_halt();// On SUCCESS
802
        tb->m_mem[mptr++] = zp.op_busy(); // On FAILURE
803
 
804
 
805
        // Now for a series of tests.  If the test fails, call the trap
806
        // interrupt with the test number that failed.  Upon completion,
807
        // call the trap with #0.
808
 
809
        // Now for a series of tests.  If the test fails, call the trap
810
        // interrupt with the test number that failed.  Upon completion,
811
        // call the trap with #0.
812
 
813
        // Test LDI to PC
814
        // Some data registers
815
        tb->m_mem[mptr] = mptr + 5 + 0x0100000; mptr++;
816
        tb->m_mem[mptr++] = zp.op_ldi(0x020,zp.ZIP_CC); //  LDI $GIE,CC
817
        tb->m_mem[mptr++] = zp.op_ldi(0x0200,zp.ZIP_R11); //  LDI $200h,R11
818
        tb->m_mem[mptr++] = zp.op_lod(-4,zp.ZIP_PC,zp.ZIP_PC); //  1: LOD $-3(PC),PC
819
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R11); //  2: CLR R11
820
        tb->m_mem[mptr++] = zp.op_noop(); //  3: NOOP
821
        tb->m_mem[mptr++] = zp.op_cmp(0,zp.ZIP_R11); //  4: CMP $0,R11
822
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIPC_Z, 0, zp.ZIP_R11,zp.ZIP_R10); //  5: STO.Z R11,(R12)
823
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIPC_Z, 0, zp.ZIP_R11,zp.ZIP_CC); //  5: STO.Z R11,(R12)
824
        tb->m_mem[mptr++] = zp.op_add(1,zp.ZIP_R0); //  6: ADD $1,R0
825
        tb->m_mem[mptr++] = zp.op_add(1,zp.ZIP_R0); //  7: ADD $1,R0
826
 
827
        // Let's test whether overflow works
828
        tb->m_mem[mptr++] = zp.op_ldi(0x0300,zp.ZIP_R11); //  0: LDI $3,R11
829
        tb->m_mem[mptr++] = zp.op_ldi(-1,zp.ZIP_R0); //  1: LDI $-1,R0
830
        tb->m_mem[mptr++] = zp.op_lsr(1,zp.ZIP_R0); //  R0 // R0 = max int
831
        tb->m_mem[mptr++] = zp.op_add(1,zp.ZIP_R0); //  Should set ovfl
832
        tb->m_mem[mptr++] = zp.op_bv(1); //  4: BV $1+PC
833
        tb->m_mem[mptr++] = zp.op_mov(0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
834
        // Overflow set from subtraction
835
        tb->m_mem[mptr++] = zp.op_ldi(0x0400,zp.ZIP_R11); //  6: LDI $4,R11
836
        tb->m_mem[mptr++] = zp.op_ldi(1,zp.ZIP_R0); //  7: LDI $1,R0
837
        tb->m_mem[mptr++] = 0x5000001f; //  8: ROL $31,R0
838
        tb->m_mem[mptr++] = zp.op_sub(1,zp.ZIP_R0); // Should set ovfl
839
        tb->m_mem[mptr++] = zp.op_bv(1); //  A: BV $1+PC
840
        tb->m_mem[mptr++] = zp.op_mov(0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
841
        // Overflow set from LSR
842
        tb->m_mem[mptr++] = zp.op_ldi(0x0500,zp.ZIP_R11); //  C: LDI $5,R11
843
        tb->m_mem[mptr++] = zp.op_ldi(1,zp.ZIP_R0); //  D: LDI $1,R0
844
        tb->m_mem[mptr++] = 0x5000001f; //  E: ROL $31,R0
845
        tb->m_mem[mptr++] = zp.op_lsr(1,zp.ZIP_R0); //  F: LSR $1,R0
846
        tb->m_mem[mptr++] = zp.op_bv(1); //  A: BV $1+PC
847
        tb->m_mem[mptr++] = zp.op_mov(0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
848
        // Overflow set from LSL
849
        tb->m_mem[mptr++] = zp.op_ldi(0x0600,zp.ZIP_R11); //  C: LDI $6,R11
850
        tb->m_mem[mptr++] = zp.op_ldi(1,zp.ZIP_R0); //  D: LDI $1,R0
851
        tb->m_mem[mptr++] = 0x5000001e; //  E: ROL $30,R0
852
        tb->m_mem[mptr++] = zp.op_lsl(1,zp.ZIP_R0); //  F: LSR $1,R0
853
        tb->m_mem[mptr++] = zp.op_bv(1); //  A: BV $1+PC
854
        tb->m_mem[mptr++] = zp.op_mov(0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
855
        // Overflow set from LSL, negative to positive
856
        tb->m_mem[mptr++] = zp.op_ldi(0x0700,zp.ZIP_R11); //  C: LDI $7,R11
857
        tb->m_mem[mptr++] = zp.op_ldi(1,zp.ZIP_R0); //  D: LDI $1,R0
858
        tb->m_mem[mptr++] = 0x5000001f; //  E: ROL $30,R0
859
        tb->m_mem[mptr++] = zp.op_lsl(1,zp.ZIP_R0); //  F: LSR $1,R0
860
        tb->m_mem[mptr++] = zp.op_bv(1); //  A: BV $1+PC
861
        tb->m_mem[mptr++] = zp.op_mov(0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
862
 
863
 
864
        // Test carry
865
        tb->m_mem[mptr++] = zp.op_ldi(0x01000, zp.ZIP_R11); //  0: LDI $16,R11
866
        tb->m_mem[mptr++] = zp.op_ldi(-1, zp.ZIP_R0); //  1: LDI $-1,R0
867
        tb->m_mem[mptr++] = zp.op_add(1, zp.ZIP_R0); //  2: ADD $1,R0
868
        tb->m_mem[mptr++] = zp.op_tst(2, zp.ZIP_CC); //  3: TST $2,CC // Is the carry set?
869
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIPC_Z,0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
870
        // and carry from subtraction
871
        tb->m_mem[mptr++] = zp.op_ldi(0x01100, zp.ZIP_R11); //  0: LDI $17,R11
872
        tb->m_mem[mptr++] = zp.op_sub(1, zp.ZIP_R0); //  1: SUB $1,R0
873
        tb->m_mem[mptr++] = zp.op_tst(2, zp.ZIP_CC); //  2: TST $2,CC // Is the carry set?
874
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIPC_Z,0,zp.ZIP_R11, zp.ZIP_CC); // FAIL! if here
875
 
876
 
877
 
878
        // Let's try a loop: for i=0; i<5; i++)
879
        //      We'll use R0=i, Immediates for 5
880
        tb->m_mem[mptr++] = zp.op_ldi(0x01200, zp.ZIP_R11); //  0: LDI $18,R11
881
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R0); //  0: CLR R0
882
        tb->m_mem[mptr++] = zp.op_noop();
883
        tb->m_mem[mptr++] = zp.op_add(1, zp.ZIP_R0); //  2: R0 = R0 + 1
884
        tb->m_mem[mptr++] = zp.op_cmp(5, zp.ZIP_R0); //  3: CMP $5,R0
885
        tb->m_mem[mptr++] = zp.op_blt(-4); //  4: BLT PC-4
886
        //
887
        // Let's try a reverse loop.  Such loops are usually cheaper to
888
        // implement, and this one is no different: 2 loop instructions 
889
        // (minus setup instructions) vs 3 from before.
890
        // R0 = 5; (from before)
891
        // do {
892
        // } while (R0 > 0);
893
        tb->m_mem[mptr++] = zp.op_ldi(0x01300, zp.ZIP_R11); //  0: LDI $18,R11
894
        tb->m_mem[mptr++] = zp.op_noop(); //  5: NOOP
895
        tb->m_mem[mptr++] = zp.op_sub( 1, zp.ZIP_R0); //  6: R0 = R0 - 1
896
        tb->m_mem[mptr++] = zp.op_bgt(-3); //  7: BGT PC-3
897
        // How about the same thing with a >= comparison?
898
        // R1 = 5; // Need to do this explicitly
899
        // do {
900
        // } while(R1 >= 0);
901
        tb->m_mem[mptr++] = zp.op_ldi(0x01400, zp.ZIP_R11); //  0: LDI $18,R11
902
        tb->m_mem[mptr++] = zp.op_ldi(5, zp.ZIP_R1);
903
        tb->m_mem[mptr++] = zp.op_noop();
904
        tb->m_mem[mptr++] = zp.op_sub(1, zp.ZIP_R1);
905
        tb->m_mem[mptr++] = zp.op_bge(-3);
906
 
907
        // Let's try the reverse loop again, only this time we'll store our
908
        // loop variable in memory.
909
        // R0 = 5; (from before)
910
        // do {
911
        // } while (R0 > 0);
912
        tb->m_mem[mptr++] = zp.op_ldi(0x01500, zp.ZIP_R11); //  0: LDI $18,R11
913
        tb->m_mem[mptr++] = zp.op_bra(1); // Give us a memory location
914
        tb->m_mem[mptr++] = 5; // Loop five times
915
        tb->m_mem[mptr++] = zp.op_mov(-2, zp.ZIP_PC, zp.ZIP_R1); // Get var adr
916
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R2);
917
        tb->m_mem[mptr++] = zp.op_ldi(5, zp.ZIP_R0);
918
        tb->m_mem[mptr++] = zp.op_sto(zp.ZIP_R0,0,zp.ZIP_R1);
919
        tb->m_mem[mptr++] = zp.op_add(1,zp.ZIP_R2);
920
        tb->m_mem[mptr++] = zp.op_add(14,zp.ZIP_R0);
921
        tb->m_mem[mptr++] = zp.op_lod(0,zp.ZIP_R1,zp.ZIP_R0);
922
        tb->m_mem[mptr++] = zp.op_sub( 1, zp.ZIP_R0);
923
        tb->m_mem[mptr++] = zp.op_bgt(-6);
924
        tb->m_mem[mptr++] = zp.op_cmp( 5, zp.ZIP_R2);
925
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIPC_NZ, 0, zp.ZIP_R11, zp.ZIP_CC);
926
 
927
        // Return success / Test the trap interrupt
928
        tb->m_mem[mptr++] = zp.op_clr(zp.ZIP_R11); //  0: CLR R11
929
        tb->m_mem[mptr++] = zp.op_mov(zp.ZIP_R11, zp.ZIP_CC);
930
        tb->m_mem[mptr++] = zp.op_noop(); //  2: NOOP // Give it a chance to take
931
        tb->m_mem[mptr++] = zp.op_noop(); //  3: NOOP // effect
932
 
933
        // Go into an infinite loop if the trap fails
934
        // Permanent loop instruction -- a busy halt if you will
935
        tb->m_mem[mptr++] = zp.op_busy(); //  4: BRA PC-1
936
 
937
        // And, in case we miss a halt ...
938
        tb->m_mem[mptr++] = zp.op_halt(); // HALT
939
 
940
        tb->reset();
941
        int     chv = 'q';
942
        const   bool    live_debug_mode = true;
943
 
944
        if (live_debug_mode) {
945
                bool    done = false, halted = true, manual = true;
946
 
947
                halfdelay(1);
948
                tb->wb_write(CMD_REG, CMD_HALT | CMD_RESET);
949
                // while((tb->wb_read(CMD_REG) & (CMD_HALT|CMD_STALL))==(CMD_HALT|CMD_STALL))
950
                        // tb->show_state();
951
 
952
                while(!done) {
953
                        chv = getch();
954
                        switch(chv) {
955
                        case 'h': case 'H':
956
                                tb->wb_write(CMD_REG, CMD_HALT);
957
                                if (!halted)
958
                                        erase();
959
                                halted = true;
960
                                break;
961
                        case 'g': case 'G':
962
                                tb->wb_write(CMD_REG, 0);
963
                                if (halted)
964
                                        erase();
965
                                halted = false;
966
                                manual = false;
967
                                break;
968
                        case 'q': case 'Q':
969
                                done = true;
970
                                break;
971
                        case 'r': case 'R':
972
                                tb->wb_write(CMD_REG, CMD_RESET|CMD_HALT);
973
                                halted = true;
974
                                erase();
975
                                break;
976
                        case 's': case 'S':
977
                                tb->wb_write(CMD_REG, CMD_STEP);
978
                                manual = false;
979
                                break;
980
                        case 't': case 'T':
981
                                manual = true;
982
                                tb->tick();
983
                                break;
984
                        case ERR:
985
                        default:
986
                                if (!manual)
987
                                        tb->tick();
988
                        }
989
 
990
                        if (manual) {
991
                                tb->show_state();
992
                        } else if (halted) {
993
                                if (tb->dbg_fp)
994
                                        fprintf(tb->dbg_fp, "\n\nREAD-STATE ******\n");
995
                                tb->read_state();
996
                        } else
997
                                tb->show_state();
998
 
999
                        if (tb->m_core->i_rst)
1000
                                done =true;
1001
                        if (tb->bomb)
1002
                                done = true;
1003
                }
1004
 
1005
        } else { // Manual stepping mode
1006
                tb->show_state();
1007
 
1008
                while('q' != tolower(chv = getch())) {
1009
                        tb->tick();
1010
                        tb->show_state();
1011
 
1012
                        if (tb->test_success())
1013
                                break;
1014
                        else if (tb->test_failure())
1015
                                break;
1016
                }
1017
        }
1018
 
1019
        endwin();
1020
 
1021
        if (tb->test_success())
1022
                printf("SUCCESS!\n");
1023
        else if (tb->test_failure())
1024
                printf("TEST FAILED!\n");
1025
        else if (chv == 'q')
1026
                printf("chv = %c\n", chv);
1027
        exit(0);
1028
}
1029
 

powered by: WebSVN 2.1.0

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