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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zipdbg/] [zipdbg.cpp] - Blame information for rev 106

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

Line No. Rev Author Line
1 19 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    zipdbg.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     Provide a simple debugger for the Zip CPU.  This allows you
8
//              to halt the CPU, examine the registers, and even single step
9
//      the CPU.  It's not fully functional yet, as I would like to implement
10
//      breakpoints and the ability to modify registers, but it's a good
11
//      start.
12
//
13
//      Commands while in the debugger are:
14
//      'r'     - RESET the CPU
15
//      'g'     - Go.  Release the CPU and exit the debugger.
16
//      'q'     - Quit.  Leave the debugger, while leaving the CPU halted.
17
//      's'     - Single Step.  Allows the CPU to advance by one instruction.
18
//
19
//
20
//
21
// Creator:     Dan Gisselquist, Ph.D.
22 69 dgisselq
//              Gisselquist Tecnhology, LLC
23 19 dgisselq
//
24
///////////////////////////////////////////////////////////////////////////////
25
//
26
// Copyright (C) 2015, Gisselquist Technology, LLC
27
//
28
// This program is free software (firmware): you can redistribute it and/or
29
// modify it under the terms of  the GNU General Public License as published
30
// by the Free Software Foundation, either version 3 of the License, or (at
31
// your option) any later version.
32
//
33
// This program is distributed in the hope that it will be useful, but WITHOUT
34
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
35
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36
// for more details.
37
//
38
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
///////////////////////////////////////////////////////////////////////////////
43
//
44
//
45
#include <stdlib.h>
46
#include <signal.h>
47
#include <time.h>
48
#include <unistd.h>
49
 
50
#include <ctype.h>
51
#include <ncurses.h>
52
 
53
// #include "twoc.h"
54
// #include "qspiflashsim.h"
55
#include "zopcodes.h"
56
#include "zparser.h"
57
#include "devbus.h"
58
#include "regdefs.h"
59
// #include "port.h"
60
 
61
// No particular "parameters" need definition or redefinition here.
62
class   ZIPPY : public DEVBUS {
63
        typedef DEVBUS::BUSW    BUSW;
64
        DEVBUS  *m_fpga;
65
public:
66
        ZIPPY(DEVBUS *fpga) : m_fpga(fpga) {}
67
 
68
        void    kill(void) { m_fpga->kill(); }
69
        void    close(void) { m_fpga->close(); }
70
        void    writeio(const BUSW a, const BUSW v) { m_fpga->writeio(a, v); }
71
        BUSW    readio(const BUSW a) { return m_fpga->readio(a); }
72
        void    readi(const BUSW a, const int len, BUSW *buf) {
73
                return m_fpga->readi(a, len, buf); }
74
        void    readz(const BUSW a, const int len, BUSW *buf) {
75
                return m_fpga->readz(a, len, buf); }
76
        void    writei(const BUSW a, const int len, const BUSW *buf) {
77
                return m_fpga->writei(a, len, buf); }
78
        void    writez(const BUSW a, const int len, const BUSW *buf) {
79
                return m_fpga->writez(a, len, buf); }
80
        bool    poll(void) { return m_fpga->poll(); }
81
        void    usleep(unsigned ms) { m_fpga->usleep(ms); }
82
        void    wait(void) { m_fpga->wait(); }
83
        bool    bus_err(void) const { return m_fpga->bus_err(); }
84
        void    reset_err(void) { m_fpga->reset_err(); }
85
        void    clear(void) { m_fpga->clear(); }
86
 
87
        void    reset(void) { writeio(R_ZIPCTRL, CPU_RESET|CPU_HALT); }
88
        void    step(void) { writeio(R_ZIPCTRL, CPU_STEP); }
89
        void    go(void) { writeio(R_ZIPCTRL, CPU_GO); }
90
        void    halt(void) {    writeio(R_ZIPCTRL, CPU_HALT); }
91
        bool    stalled(void) { return ((readio(R_ZIPCTRL)&CPU_STALL)==0); }
92
 
93
        void    showval(int y, int x, const char *lbl, unsigned int v) {
94
                mvprintw(y,x, "%s: 0x%08x", lbl, v);
95
        }
96
 
97
        void    dispreg(int y, int x, const char *n, unsigned int v) {
98
                // 4,4,8,1 = 17 of 20, +3 = 19
99
                mvprintw(y, x, "%s: 0x%08x", n, v);
100
        }
101
 
102
        void    showins(int y, const char *lbl,
103
                        const int gie, const unsigned int pc) {
104
                char    line[80];
105
                unsigned int    v;
106
 
107
                mvprintw(y, 0, "%s: 0x%08x", lbl, pc);
108
 
109
                if (gie) attroff(A_BOLD);
110
                else    attron(A_BOLD);
111
 
112
                line[0] = '\0';
113
                try {
114
                        v= readio(pc);
115
                        zipi_to_string(v, line);
116
                        printw(" 0x%08x", v);
117
                        printw("  %-24s", &line[1]);
118
                } catch(BUSERR b) {
119
                        printw(" 0x%08x  %-24s", b.addr, "(Bus Error)");
120
                }
121
                attroff(A_BOLD);
122
        }
123
 
124
        unsigned int    cmd_read(unsigned int a) {
125
                writeio(R_ZIPCTRL, CPU_HALT|(a&0x3f));
126
                while((readio(R_ZIPCTRL) & CPU_STALL) == 0)
127
                        ;
128
                return readio(R_ZIPDATA);
129
        }
130
 
131
        void    read_state(void) {
132
                int     ln= 0;
133
                bool    gie;
134
 
135
                mvprintw(ln,0, "Peripherals");
136
                mvprintw(ln,40, "CPU State: ");
137
                {
138
                        unsigned int v = readio(R_ZIPDATA);
139
                        if (v & 0x010000)
140
                                printw("EXT-INT ");
141
                        if (v & 0x002000)
142
                                printw("Supervisor Mod ");
143
                        if (v & 0x001000)
144
                                printw("Sleeping ");
145
                        if (v & 0x008000)
146
                                printw("Break-Enabled ");
147
                }
148
                ln++;
149
                showval(ln, 1, "PIC ", cmd_read(32+ 0));
150
                showval(ln,21, "WDT ", cmd_read(32+ 1));
151
                showval(ln,41, "CACH", cmd_read(32+ 2));
152
                showval(ln,61, "PIC2", cmd_read(32+ 3));
153
                ln++;
154
                showval(ln, 1, "TMRA", cmd_read(32+ 4));
155
                showval(ln,21, "TMRB", cmd_read(32+ 5));
156
                showval(ln,41, "TMRC", cmd_read(32+ 6));
157
                showval(ln,61, "JIF ", cmd_read(32+ 7));
158
 
159
                ln++;
160
                showval(ln, 1, "UTSK", cmd_read(32+12));
161
                showval(ln,21, "UMST", cmd_read(32+13));
162
                showval(ln,41, "UPST", cmd_read(32+14));
163
                showval(ln,61, "UAST", cmd_read(32+15));
164
 
165
                ln++;
166
                ln++;
167
                unsigned int cc = cmd_read(14);
168
                gie = (cc & 0x020);
169
                if (gie)
170
                        attroff(A_BOLD);
171
                else
172
                        attron(A_BOLD);
173
                mvprintw(ln, 0, "Supervisor Registers");
174
                ln++;
175
 
176
                dispreg(ln, 1, "sR0 ", cmd_read(0));
177
                dispreg(ln,21, "sR1 ", cmd_read(1));
178
                dispreg(ln,41, "sR2 ", cmd_read(2));
179
                dispreg(ln,61, "sR3 ", cmd_read(3)); ln++;
180
 
181
                dispreg(ln, 1, "sR4 ", cmd_read(4));
182
                dispreg(ln,21, "sR5 ", cmd_read(5));
183
                dispreg(ln,41, "sR6 ", cmd_read(6));
184
                dispreg(ln,61, "sR7 ", cmd_read(7)); ln++;
185
 
186
                dispreg(ln, 1, "sR8 ", cmd_read( 8));
187
                dispreg(ln,21, "sR9 ", cmd_read( 9));
188
                dispreg(ln,41, "sR10", cmd_read(10));
189
                dispreg(ln,61, "sR11", cmd_read(11)); ln++;
190
 
191
                dispreg(ln, 1, "sR12", cmd_read(12));
192
                dispreg(ln,21, "sSP ", cmd_read(13));
193
 
194
                mvprintw(ln,41, "sCC :%s%s%s%s%s%s%s",
195
                        (cc & 0x040)?"STP":"   ",
196
                        (cc & 0x020)?"GIE":"   ",
197
                        (cc & 0x010)?"SLP":"   ",
198
                        (cc&8)?"V":" ",
199
                        (cc&4)?"N":" ",
200
                        (cc&2)?"C":" ",
201
                        (cc&1)?"Z":" ");
202
                mvprintw(ln,61, "sPC : 0x%08x", cmd_read(15));
203
                ln++;
204
 
205
                if (gie)
206
                        attron(A_BOLD);
207
                else
208
                        attroff(A_BOLD);
209
                mvprintw(ln, 0, "User Registers"); ln++;
210
                dispreg(ln, 1, "uR0 ", cmd_read(16));
211
                dispreg(ln,21, "uR1 ", cmd_read(17));
212
                dispreg(ln,41, "uR2 ", cmd_read(18));
213
                dispreg(ln,61, "uR3 ", cmd_read(19)); ln++;
214
 
215
                dispreg(ln, 1, "uR4 ", cmd_read(20));
216
                dispreg(ln,21, "uR5 ", cmd_read(21));
217
                dispreg(ln,41, "uR6 ", cmd_read(22));
218
                dispreg(ln,61, "uR7 ", cmd_read(23)); ln++;
219
 
220
                dispreg(ln, 1, "uR8 ", cmd_read(24));
221
                dispreg(ln,21, "uR9 ", cmd_read(25));
222
                dispreg(ln,41, "uR10", cmd_read(26));
223
                dispreg(ln,61, "uR11", cmd_read(27)); ln++;
224
 
225
                dispreg(ln, 1, "uR12", cmd_read(28));
226
                dispreg(ln,21, "uSP ", cmd_read(29));
227
                cc = cmd_read(30);
228
                mvprintw(ln,41, "uCC :%s%s%s%s%s%s%s",
229
                        (cc&0x040)?"STP":"   ",
230
                        (cc&0x020)?"GIE":"   ",
231
                        (cc&0x010)?"SLP":"   ",
232
                        (cc&8)?"V":" ",
233
                        (cc&4)?"N":" ",
234
                        (cc&2)?"C":" ",
235
                        (cc&1)?"Z":" ");
236
                mvprintw(ln,61, "uPC : 0x%08x", cmd_read(31));
237
 
238
                attroff(A_BOLD);
239
                ln+=2;
240
 
241
                ln+=3;
242
                BUSW pc = cmd_read((gie)?31:15);
243
                showins(ln, "I ", gie, pc+2); ln++;
244
                showins(ln, "Dc", gie, pc+1); ln++;
245
                showins(ln, "Op", gie, pc  ); ln++;
246
                showins(ln, "Al", gie, pc-1); ln++;
247
        }
248
};
249
 
250
DEVBUS  *m_fpga;
251
 
252
int     main(int argc, char **argv) {
253
//      FPGAOPEN(m_fpga);
254
        ZIPPY   *zip = new ZIPPY(m_fpga);
255
 
256
        initscr();
257
        raw();
258
        noecho();
259
        keypad(stdscr, true);
260
 
261
        int     chv;
262
        bool    done = false;
263
 
264
        zip->halt();
265
        for(int i=0; (i<5)&&(zip->stalled()); i++)
266
                ;
267
        if (!zip->stalled())
268
                zip->read_state();
269
        while(!done) {
270
                chv = getch();
271
                switch(chv) {
272
                case 'g': case 'G':
273
                        m_fpga->writeio(R_ZIPCTRL, CPU_GO);
274
                        // We just released the CPU, so we're now done.
275
                        done = true;
276
                        break;
277
                case 'q': case 'Q':
278
                        done = true;
279
                        break;
280
                case 'r': case 'R':
281
                        zip->reset();
282
                        erase();
283
                        break;
284
                case 's': case 'S':
285
                        zip->step();
286
                        break;
287
                case ERR:
288
                default:
289
                        ;
290
                }
291
 
292
                if (zip->stalled())
293
                        erase();
294
                else
295
                        zip->read_state();
296
        }
297
 
298
        endwin();
299
}
300
 

powered by: WebSVN 2.1.0

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