File: cmd_x.cc

package info (click to toggle)
gpsim 0.32.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,644 kB
  • sloc: cpp: 121,258; asm: 54,223; ansic: 13,576; python: 9,708; sh: 4,695; makefile: 1,575; lex: 1,139; yacc: 854; pascal: 511; perl: 93; awk: 44; xml: 41
file content (285 lines) | stat: -rw-r--r-- 8,093 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
   Copyright (C) 1999 T. Scott Dattalo

This file is part of gpsim.

gpsim is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

gpsim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with gpsim; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include <glib.h>

#include <iostream>
#include <string>

#include "cmd_x.h"
#include "cmd_dump.h"
#include "misc.h"

#include "../src/expr.h"
#include "../src/processor.h"
#include "../src/registers.h"
#include "../src/ui.h"
#include "../src/value.h"

cmd_x c_x;

cmd_reg c_reg;

static cmd_options cmd_x_options[] =
{
    {nullptr, 0, 0}
};
static cmd_options cmd_reg_options[] =
{
    {nullptr, 0, 0}
};


cmd_x::cmd_x()
    : command("x", nullptr)
{
    brief_doc = "[deprecated] examine and/or modify memory";
    long_doc = "\nx examine command -- deprecated\n"
               "\tInstead of the using a special command to examine and modify\n"
               "\tvariables, it's possible to directly access them using gpsim's\n"
               "\texpression parsing. For example, to examine a variable:\n"
               "gpsim> my_variable\n"
               "my_variable [0x27] = 0x00 = 0b00000000\n"
               "\tTo modify a variable\n"
               "gpsim> my_variable = 10\n"
               "\tIt's also possible to assign the value of register to another\n"
               "gpsim> my_variable = porta\n"
               "\tOr to assign the results of an expression:\n"
               "gpsim> my_variable = (porta ^ portc) & 0x0f\n";
    /*
    long_doc = string ("\nx [file_register] [new_value]\n\
    \toptions:\n\
    \t\tfile_register - ram location to be examined or modified.\n\
    \t\tnew_value - the new value written to the file_register.\n\
    \t\tif no options are specified, then the entire contents\n\
    \t\tof the file registers will be displayed (dump).\n\
    ");
    */
    op = cmd_x_options;
}

cmd_reg::cmd_reg()
    : command("reg", nullptr)
{
    brief_doc = "examine and/or modify register memory";
    long_doc = "\nreg examine/modify register memory\n"
		"    reg(SRC)[= EXPRESSION]\n\n"
		"      SRC is an expression which evaluates to the target\n"
		"        register address. The expression can be or contain\n"
		"	 integers, register names (address of register is\n"
		"        used), REGnnn's(nnn, a HEX memory address, is used),\n"
		"	 and arithmetic operators such as + - & ^.\n\n"
		"      EXPRESSION is an expression which evaluates to the\n"
		"	 integer which is placed in SRC memory location.\n"
		"	 EXPRESSION uses the same elements as SRC except \n"
		"	 register names (including REGnnn) return their\n"
		"        contents.\n\n" 
		"    Some examples:\n"
		"        **gpsim> reg(0xf92)\n"
		"        trisa[0xf92] = 0x7f\n\n"
		"        **gpsim> reg(REGf92)\n"
		"        trisa[0xf92] = 0x7f\n\n"
		"        reg(trisa) = trisa & $f0\n"
		"        trisa[0xf92] = 0x70 was 0x7f\n\n"
		"        **gpsim> reg(0x20) = trisb\n"
		"        REG020[0x20] = 0xff was 0x0\n"
		"        **gpsim> reg(0x20+1) = REG020\n"
		"        REG021[0x21] = 0xff was 0x0\n\n"

		;
    op = cmd_reg_options;
}

void cmd_x::x()
{
    dump.dump(cmd_dump::DUMP_RAM);

    if (GetActiveCPU())
    {
        GetActiveCPU()->dump_registers();
    }
}

/*
 * This function produces the output.
 * if pExpr == nullptr it outputs the value of the specified register(reg)
 * otherwise it evaluates pExpr, updates the specified register and
 * outputs the result
 */
void cmd_x::x(int reg, Expression *pExpr)
{
    Register *pReg = nullptr;

    if (!GetActiveCPU())
    {
        return;
    }

    if (reg < 0 || (reg >= (int)GetActiveCPU()->register_memory_size()) ||
	(pReg = GetActiveCPU()->registers[reg]) == nullptr)
    {
        GetUserInterface().DisplayMessage("bad file register 0x%x\n", reg);
        return;
    }

    RegisterValue rvCurrent(pReg->getRVN());

    if (pExpr == nullptr)
    {
        GetUserInterface().DisplayMessage("%s[0x%x] = 0x%x\n",
                                          pReg->name().c_str(), reg, rvCurrent.data);
    }
    else
    {
	unsigned int val;
        if (int_from_expression(pExpr, val))
        {
	    unsigned int new_value = val & GetActiveCPU()->register_mask();
            RegisterValue RV(new_value, 0);
            pReg->putRV(RV);
            // Notify listeners
            pReg->update();
            // Display new value
            GetUserInterface().DisplayMessage("%s[0x%x] = 0x%x ",
                                              pReg->name().c_str(), reg, new_value);
            // Display old value
            GetUserInterface().DisplayMessage("was 0x%x\n",
                                              ((gint64)rvCurrent.get() &
                                               GetActiveCPU()->register_mask()));
        }
        else
        {
            GetUserInterface().DisplayMessage("Error evaluating the expression\n");
        }

        delete pExpr;
    }
}


void cmd_x::x(char *reg_name)
{
    std::cout << "this command is deprecated. "
              << "Type '" << reg_name << "' at the command line to display the contents of a register.\n";
    // get_symbol_table().dump_one(reg_name);
}


void cmd_x::x(char * /* reg_name */, int /* val */)
{
    std::cout << "this command is deprecated. use: \n  symbol_name = value\n\ninstead\n";
    //  update_symbol_value(reg_name,val);
}


/*
 * this function is called from the parser.
 * it evaluates the first expression and passes the result
 * to another function to evaluate the second expression and
 * output the result
 */
void cmd_x::x(Expression *expr, Expression *pExpr)
{
    unsigned int val;

    expr->get_address = true;
    try {
        if(int_from_expression(expr, val))
        {
	    x(val, pExpr);
        }
    }

    catch (const Error &err)
    {
        std::cout << "ERROR:" << err.what() << std::endl;
    }

    delete expr;
}
/*
 * Evaluate an expression to an integer value.
 * if the expression is a register label, return the register address
 * if get_address == true, otherwise return register value.
 * return true on success
 */
bool cmd_x::int_from_expression(Expression *pExpr, unsigned int &val)
{
    Value *pValue = nullptr;
    Integer *pInt = nullptr;



    if ( (typeid(LiteralString) == typeid(*pExpr) &&
        !((LiteralString*)pExpr)->toString().empty() ))
    {
            std::string sName(((LiteralString*)pExpr)->toString());
            if (sscanf(sName.c_str(), "REG%x", &val) > 00)
            {
		if (pExpr->get_address)
		    return true;
		else
		{
    		    Register *pReg = GetActiveCPU()->registers[val];
		    if (pReg)
		    {
		        val = (unsigned int)pReg->get_value();
			return true;
		    }

                    GetUserInterface().DisplayMessage(
                        "Error: %s is not REGnnn\n", sName.c_str());
	            return false;
		}
		    
            }
	    else
	    {
                GetUserInterface().DisplayMessage(
                    "Error: %s is not REGnnn\n", sName.c_str());
	        return false;
	    }
    }
/*
    else if (typeid(LiteralInteger) == typeid(*pExpr) &&
		(pValue = toValue(pExpr)) && 
		(pInt = dynamic_cast<Integer*>(pValue)))
    {
	    val = pInt->getVal();

	    delete pValue;
	    return true;
    }
*/
    else if ((pValue = pExpr->evaluate()) &&
             (pInt = dynamic_cast<Integer*>(pValue))
            )
    {
        val = (unsigned int)pInt->getVal();
        delete pValue;
        return true;
    }
    else
    {
        GetUserInterface().DisplayMessage(
            "Error: the expression did not evaluate to on integer\n");
    }
    return false;
}