summaryrefslogtreecommitdiffstats
path: root/microbe/pic14.h
blob: fc97cb40ac2e13f78569faabd501915536fa40d3 (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
/***************************************************************************
 *   Copyright (C) 2004-2005 by Daniel Clarke                              *
 *   daniel.jc@gmail.com                                                   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#ifndef PIC14_H
#define PIC14_H

#include "expression.h"
#include "microbe.h"

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>

class Code;
class Microbe;
class Parser;

/**
@author David Saxton
 */
class PortPin
{
	public:
		PortPin( const TQString & port, int pin );
		/**
		 * Creates an invalid PortPin ( pin() will return -1).
		 */
		PortPin();
		/**
		 * Returns port (uppercase).
		 */
		TQString port() const { return m_port; }
		/**
		 * Returns the port position (e.g. "PORTA" is 0, "PORTB" is 1, etc).
		 */
		int portPosition() const;
		/**
		 * Returns the pin (-1 == invalid PortPin).
		 */
		int pin() const { return m_pin; }
		
	protected:
		TQString m_port;
		int m_pin;
};
typedef TQValueList<PortPin> PortPinList;


/**
@author Daniel Clarke
@author David Saxton
*/
class PIC14
{
	public:
		enum Type
		{
			P16C84,
			P16F84,
			P16F627,
			P16F628,
			unknown,
		};
		enum LocationType
		{
			num = 1,
			work = 2,
			var = 3,
		};
		/**
		 * Used in determining which delay subroutine should be created.
		 */
		enum DelaySubroutine
		{
			Delay_None	= 0,
			Delay_3uS	= 1,
			Delay_768uS	= 2,
			Delay_200mS	= 3,
			Delay_50S	= 4,
		};
		
		PIC14( Microbe * master, Type type );
		~PIC14();
		
		/**
		 * Tries to convert the string to a PIC type, returning unknown if
		 * unsuccessful.
		 */
		static Type toType( const TQString & text );
		/**
		 * @return the PIC type.
		 */
		Type type() const { return m_type; }
		/**
		 * @return the Type as a string without the P at the front.
		 */
		TQString minimalTypeString() const;
		/**
		 * Translates the portPinString (e.g. "PORTA.2") into a PortPin if the port
		 * and pin combination is valid (otherwise returns an invalid PortPin with
		 * a pin of -1.
		 */
		PortPin toPortPin( const TQString & portPinString );
		/**
		 * Returns the address that the General Purpose Registers starts at.
		 */
		uchar gprStart() const;
		
		void setParser(Parser *parser) { m_parser = parser; }
		void setCode( Code * code ) { m_pCode = code; }
		void mergeCode( Code * code );
	
		void setConditionalCode( Code * ifCode, Code * elseCode );
		Code * ifCode();
		Code * elseCode();
	
		Code * m_ifCode;
		Code * m_elseCode;

		void postCompileConstruct( const TQStringList &interrupts );

		/**
		 * @returns whether or not the port is valid.
		 * @see isValidPortPin
		 */
		bool isValidPort( const TQString & portName ) const;
		/**
		 * @returns whether or not the port and pin is a valid combination.
		 * @see isValidPort
		 */
		bool isValidPortPin( const PortPin & portPin ) const;
		bool isValidTris( const TQString & trisName ) const;
		bool isValidInterrupt( const TQString & interruptName ) const;

		void Sgoto(const TQString &label);
		void Slabel(const TQString &label);
		void Send();
		void Ssubroutine(const TQString &procName, Code * compiledProcCode);
		void Sinterrupt(const TQString & procName, Code * compiledProcCode);
		void Scall(const TQString &name);
	
		void Ssetlh( const PortPin & portPin, bool high);
	
		void add( TQString val1, TQString val2, LocationType val1Type, LocationType val2Type );
		void subtract( const TQString & val1, const TQString & val2, LocationType val1Type, LocationType val2Type );
		void mul( TQString val1, TQString val2, LocationType val1Type, LocationType val2Type);
		void div( const TQString & val1, const TQString & val2, LocationType val1Type, LocationType val2Type);
	
		void assignNum(const TQString & val);
		void assignVar(const TQString & val);
	
		void saveToReg(const TQString &dest);
		/**
		 * Move the contents of the working register to the register with the given
		 * name.
		 */
		void saveResultToVar( const TQString & var );
		/** Code for "==" */
		void equal( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );
		/** Code for "!=" */
		void notEqual( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );
		/** Code for ">" */
		void greaterThan( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );
		/** Code for "<" */
		void lessThan( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );
		/** Code for ">=" */
		void greaterOrEqual( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );
		/** Code for "<=" */
		void lessOrEqual( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );

		void bitwise( Expression::Operation op, const TQString &val1, const TQString &val2, bool val1IsNum, bool val2IsNum );
	
		void Swhile( Code * whileCode, const TQString &expression);
		void Srepeat( Code * repeatCode, const TQString &expression);
		void Sif( Code * ifCode, Code * elseCode, const TQString &expression);
		void Sfor( Code * forCode, Code * initCode, const TQString &expression, const TQString &variable, const TQString &step, bool stepPositive);
	
		void Spin( const PortPin & portPin, bool NOT);
		void addCommonFunctions( DelaySubroutine delay );
	
		//BEGIN "Special Functionality" functions
		/**
		 * Delay the program execution, for the given period of length_us (unit:
		 * microseconds).
		 * @param pos the position to add the code for calling the delay subroutine.
		 */
		void Sdelay( unsigned length_us, Code::InstructionPosition pos = Code::Middle );
		/**
		 * Output the working register to the given seven segment.
		 * @param name The variable giving the pin configuration of the seven
		 * segment.
		 */
		void SsevenSegment( const Variable & pinMap );
		/**
		 * Read the value of the keypad to the working register.
		 * @param name The variable giving the pin configuration of the keypad.
		 */
		void Skeypad( const Variable & pinMap );
		//END "Special Functionality" functions
	
		void SincVar( const TQString &var );
		void SdecVar( const TQString &var );
		void SrotlVar( const TQString &var );
		void SrotrVar( const TQString &var );
	
		void Stristate( const TQString &port );
	
		void Sasm(const TQString &raw);
	
	protected:
		void multiply();
		void divide();
	
		/** @see Microbe::m_picType */
		Type m_type;
	
		Parser * m_parser;
		Microbe * mb;
		Code * m_pCode;
	
		void ifInitCode( const TQString &val1, const TQString &val2, LocationType val1Type, LocationType val2Type );
	
		/**
		 * The function makes sure that val1 always contains a working register
		 * variable, if one has been passed, this is done by swapping val1 and val2 when
		 * neccessary
		 */
		void rearrangeOpArguments( TQString * val1, TQString * val2, LocationType * val1Type, LocationType * val2Type);
	
		/**
		 * @param flag True means give flag bit, false means give enable bit instead
		 */
		int interruptNameToBit(const TQString &name, bool flag);
};

#endif