summaryrefslogtreecommitdiffstats
path: root/src/electronics/gpsimprocessor.h
blob: aae26058fd0c0ee9393efc74a3aebad617b5e29f (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program 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 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "config.h"
#ifndef NO_GPSIM

#ifndef GPSIMPROCESSOR_H
#define GPSIMPROCESSOR_H

#include "sourceline.h"

#include <tqmap.h>
#include <tqvaluevector.h>
#include <tqobject.h>
#include <tqvaluelist.h>

class DebugLine;
class GpsimProcessor;
class MicroInfo;
class pic_processor; // from gpsim
class Register;
class RegisterMemoryAccess;

typedef TQMap<SourceLine, SourceLine> SourceLineMap;
typedef TQMap<int, TQString> TQStringMap;
typedef TQValueList<int> IntList;


class DebugLine : public SourceLine
{
	public:
		DebugLine();
		DebugLine( const TQString & fileName, int line );
		/**
		 * Whether or not to break when we reach this line.
		 */
		bool isBreakpoint() const { return m_bIsBreakpoint; }
		/**
		 * Set whether or not to break when we reach this line.
		 */
		void setBreakpoint( bool breakpoint ) { m_bIsBreakpoint = breakpoint; }
		/**
		 * Used for efficiency purposes by GpsimProcessor. Sets a flag.
		 */
		void markAsDeleted() { m_bMarkedAsDeleted = true; }
		/**
		 * Used for efficiency purposes by GpsimProcessor.
		 */
		bool markedAsDeleted() const { return m_bMarkedAsDeleted; }
		
	protected:
		bool m_bIsBreakpoint;
		bool m_bMarkedAsDeleted;
		
	private:
		DebugLine( const DebugLine & dl );
		DebugLine & operator = ( const DebugLine & dl );
};


/**
@short Stores info from gpsim register, used to hide gpsim interface
@author David Saxton
*/
class RegisterInfo : public TQObject
{
	Q_OBJECT
  
	public:
		RegisterInfo( Register * reg );
	
		enum RegisterType
		{
			Invalid,
			Generic,
			File,
			SFR,
			Breakpoint
		};
		
		RegisterType type() const { return m_type; }
		TQString name() const { return m_name; }
		unsigned value() const;
		static TQString toString( RegisterType type );
		
		/**
		 * Checks to see if the value has changed; if so, emit new value.
		 */
		void update();
		
	signals:
		void valueChanged( unsigned newValue );
		
	protected:
		TQString m_name;
		RegisterType m_type;
		Register * m_pRegister;
		unsigned m_prevEmitValue;
};


/**
@short Stores information about a set of registers, used to hide gpsim interface.
@author David Saxton
*/
class RegisterSet
{
	public:
		RegisterSet( pic_processor * picProcessor );
		~RegisterSet();
		
		/**
		 * Calls update for each RegisterInfo in this set.
		 */
		void update();
		/**
		 * Returns the number of registers.
		 */
		unsigned size() const { return m_registers.size(); }
		
		RegisterInfo * fromAddress( unsigned address );
		RegisterInfo * fromName( const TQString & name );
		
	protected:
		typedef TQMap< TQString, RegisterInfo * > RegisterInfoMap;
		RegisterInfoMap m_nameToRegisterMap;
		TQValueVector< RegisterInfo * > m_registers;
};


/**
@author David Saxton
*/
class GpsimDebugger : public TQObject
{
	friend class GpsimProcessor;
	Q_OBJECT
  
	
	public:
		enum Type
		{
			AsmDebugger = 0,
			HLLDebugger = 1
		};
		
		GpsimDebugger( Type type, GpsimProcessor * gpsim );
		~GpsimDebugger();
		
		GpsimProcessor * gpsim() const { return m_pGpsim; }
		
		/**
		 * When an assembly file was generated by a high level language compiler
		 * like SDCC, it will insert markers like ";#CSRC" that show which line
		 * of source-code generated the given set of assembly instructions. This
		 * matches up the assembly file lines with the associated source file
		 * lines.
		 */
		void associateLine( const TQString & sourceFile, int sourceLine, const TQString & assemblyFile, int assemblyLine );
		/**
		 * Check to see if we've hit a breakpoint or similar; if so, this
		 * function will stop the execution of the PIC program.
		 */
		void checkForBreak();
		/**
		 * Sets the breakpoints used for the given file to exactly those that
		 * are contained in this list. Breakpoints for other files are not
		 * affected.
		 * @param path the location of the file (which gpsim must recognise).
		 */
		void setBreakpoints( const TQString & path, const IntList & lines );
		/**
		 * Sets / removes the breakpoint at the given line
		 */
		void setBreakpoint( const TQString & path, int line, bool isBreakpoint );
		/**
		 * Returns the current source line that gpsim is at. By default, this
		 * will be the corresponding assembly line. That can be overwritten
		 * using mapAddressBlockToLine.
		 */
		SourceLine currentLine();
		/**
		 * Returns a pointer to the debug info for the current line.
		 */
		DebugLine * currentDebugLine();
		/**
		 * @return the program address for the given line (or -1 if no such
		 * line).
		 */
		int programAddress( const TQString & path, int line );
		/**
		 * Step into the next program line.
		 */
		void stepInto();
		/**
		 * Step over the next program instruction. If we are currently running,
		 * this function will do nothing. Otherwise, it will record the current
		 * stack level, step, and if the new stack level is <= the initial level
		 * then return - otherwise, this processor will set a breakpoint for
		 * stack levels <= initial, and go to running mode.
		 */
		void stepOver();
		/**
		 * Similar to stepOver, except we break when the stack level becomes <
		 * the initial stack level (instead of <= initial).
		 */
		void stepOut();
		
	signals:
		/**
		 * Emitted when a line is reached. By default, this is the line of the
		 * input assembly file; however, the line associated with an address in
		 * the PIC memory can be changed with mapAddressBlockToLine.
		 */
		void lineReached( const SourceLine & sourceLine );
		
	protected slots:
		void gpsimRunningStatusChanged( bool isRunning );
		
	protected:
		void initAddressToLineMap();
		void stackStep( int dl );
		void emitLineReached();
		
		int m_stackLevelLowerBreak; // Set by step-over, for when the stack level decreases to the one given
		SourceLine m_previousAtLineEmit; // Used for working out whether we should emit a new line reached signal
		DebugLine ** m_addressToLineMap;
		DebugLine * m_pBreakFromOldLine;
		GpsimProcessor * m_pGpsim;
		Type m_type;
		unsigned m_addressSize;
		SourceLineMap m_sourceLineMap; // assembly <--> High level language
};


/**
@author David Saxton
*/
class GpsimProcessor : public TQObject
{
	friend class GpsimDebugger;
	Q_OBJECT
  
	
	public:
		/**
		 * Create a new gpsim processor. After calling this constructor, you
		 * should always call codLoadStatus() to ensure that the cod file was
		 * loaded successfully.
		 */
		GpsimProcessor( TQString symbolFile, TQObject *parent = 0 );
		~GpsimProcessor();
		
		void setDebugMode( GpsimDebugger::Type mode ) { m_debugMode = mode; }
		GpsimDebugger * currentDebugger() const { return m_pDebugger[m_debugMode]; }
		
		enum CodLoadStatus
		{
			CodSuccess,
			CodFileNotFound,
			CodUnrecognizedProcessor,
			CodFileNameTooLong,
			CodLstNotFound,
			CodBadFile,
			CodFileUnreadable,
			CodFailure,
			CodUnknown // Should never be this, but just in case load_symbol_file returns something funny
		};
		
		enum InstructionType
		{
			LiteralOp,
			BitOp,
			RegisterOp,
			UnknownOp,
		};
				
		/**
		 * @return status of opening the COD file
		 * @see displayCodLoadStatus
		 */
		CodLoadStatus codLoadStatus() const { return m_codLoadStatus; }
		/**
		 * Popups a messagebox to the user according to the CodLoadStatus. Will
		 * only popup a messagebox if the CodLoadStatus wasn't CodSuccess.
		 */
		void displayCodLoadStatus();
		/**
		 * Returns a list of source files for the currently running program.
		 */
		TQStringList sourceFileList();
		/**
		 * Set whether or not to run gpsim. (i.e. whether or not the step
		 * function should do anything when called with force=false).
		 */
		void setRunning( bool run );
		/**
		 * Returns true if running (currently simulating), else gpsim is paused.
		 */
		bool isRunning() const { return m_bIsRunning; }
		/**
		 * Execute the next program instruction. If we are not in a running
		 * mode, then this function will do nothing.
		 */
		void executeNext();
		/**
		 * Reset all parts of the simulation. Gpsim will not run until
		 * setRunning(true) is called. Breakpoints are not affected.
		 */
		void reset();
		/**
		 * Returns the microinfo describing this processor.
		 */
		MicroInfo * microInfo() const;
		
		pic_processor * picProcessor() const { return m_pPicProcessor; }
		unsigned programMemorySize() const;
		RegisterSet * registerMemory() const { return m_pRegisterMemory; }
		/**
		 * @return the instruction type at the given address.
		 */
		InstructionType instructionType( unsigned address );
		/**
		 * @return the address of the operand's register at address if the
		 * instruction at address is a register operation, and -1 otherwise.
		 */
		int operandRegister( unsigned address );
		/**
		 * @return the literal if the instruction at address is a literal
		 * operation, and -1 otherwise.
		 */
		int operandLiteral( unsigned address );
		
		//BEGIN Convenience functions for PIC files
		enum ProgramFileValidity { DoesntExist, IncorrectType, Valid };
		/**
		 * @return information on the validity of the given program file (either
		 * DoesntExist, IncorrectType, or Valid).
		 * @see static TQString generateSymbolFile
		 */
		static ProgramFileValidity isValidProgramFile( const TQString & programFile );
		/**
		 * Converts the file at programFile to a Symbol file for emulation,
		 * and returns that symbol file's path
		 * @param programFile The full url to the file
		 * @param assembled The slot to connect the assembled signal to
		 * @see static bool isValidProgramFile( const TQString &programFile )
		 */
		static TQString generateSymbolFile( const TQString &fileName, TQObject *receiver, const char *successMember, const char * failMember = 0l );
		/**
		 *Compile microbe to output to the given filename
		 */
		static void compileMicrobe( const TQString &filename, TQObject *receiver, const char * successMember, const char * failMember = 0l );
		//END convenience functions for PIC files
		
	signals:
		/**
		 * Emitted when the running status of gpsim changes.
		 */
		void runningStatusChanged( bool isRunning );
		
	protected:
		/**
		 * Calls emitLineReached for each debugger.
		 */
		void emitLineReached();
		
		pic_processor * m_pPicProcessor;
		CodLoadStatus m_codLoadStatus;
		const TQString m_symbolFile;
		RegisterSet * m_pRegisterMemory;
		GpsimDebugger::Type m_debugMode;
		GpsimDebugger * m_pDebugger[2]; // Asm, HLL
		
		/**
		 * We are called effectively for each cycle of the cycle of the
		 * processor. This value is used as some instructions (e.g. goto) take
		 * two cycles to execute, and so we must ignore one cycle to ensure
		 * realtime simulation.
		 */
		bool m_bCanExecuteNextCycle;
		
	private:
		bool m_bIsRunning;
};

#endif

#endif // !NO_GPSIM