File indexing completed on 2024-04-28 05:43:06

0001 /***************************************************************************
0002  *   Copyright (C) 2004-2005 by Daniel Clarke                              *
0003  *   daniel.jc@gmail.com                                                   *
0004  *                                     *
0005  *   24-04-2007                                                            *
0006  *   Modified to add pic 16f877,16f627 and 16f628              *
0007  *   by george john george@space-kerala.org                    *
0008  *   supported by SPACE www.space-kerala.org                   *
0009  *                                                                          *
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the                         *
0022  *   Free Software Foundation, Inc.,                                       *
0023  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0024  ***************************************************************************/
0025 
0026 #ifndef PIC14_H
0027 #define PIC14_H
0028 
0029 #include "expression.h"
0030 #include "microbe.h"
0031 
0032 #include <QString>
0033 #include <QStringList>
0034 #include <QList>
0035 
0036 class Code;
0037 class MicrobeApp;
0038 class Parser;
0039 
0040 /**
0041 @author David Saxton
0042  */
0043 class PortPin
0044 {
0045     public:
0046         PortPin( const QString & port, int pin );
0047         /**
0048          * Creates an invalid PortPin ( pin() will return -1).
0049          */
0050         PortPin();
0051         /**
0052          * Returns port (uppercase).
0053          */
0054         QString port() const { return m_port; }
0055         /**
0056          * Returns the port position (e.g. "PORTA" is 0, "PORTB" is 1, etc).
0057          */
0058         int portPosition() const;
0059         /**
0060          * Returns the pin (-1 == invalid PortPin).
0061          */
0062         int pin() const { return m_pin; }
0063         
0064     protected:
0065         QString m_port;
0066         int m_pin;
0067 };
0068 typedef QList<PortPin> PortPinList;
0069 
0070 
0071 /**
0072 @author Daniel Clarke
0073 @author David Saxton
0074 */
0075 class PIC14
0076 {
0077     public:
0078         enum Type
0079         {
0080             P16C84,
0081             P16F84,
0082             P16F627,
0083             P16F628,
0084             P16F877,
0085             unknown
0086         };
0087         enum LocationType
0088         {
0089             num = 1,
0090             work = 2,
0091             var = 3
0092         };
0093         /**
0094          * Used in determining which delay subroutine should be created.
0095          */
0096         enum DelaySubroutine
0097         {
0098             Delay_None  = 0,
0099             Delay_3uS   = 1,
0100             Delay_768uS = 2,
0101             Delay_200mS = 3,
0102             Delay_50S   = 4
0103         };
0104         
0105         /*PIC14::*/PIC14( MicrobeApp * master, Type type );
0106         ~PIC14();
0107         
0108         /**
0109          * Tries to convert the string to a PIC type, returning unknown if
0110          * unsuccessful.
0111          */
0112         static Type toType( const QString & text );
0113         /**
0114          * @return the PIC type.
0115          */
0116         Type type() const { return m_type; }
0117         /**
0118          * @return the Type as a string without the P at the front.
0119          */
0120         QString minimalTypeString() const;
0121         /**
0122          * Translates the portPinString (e.g. "PORTA.2") into a PortPin if the port
0123          * and pin combination is valid (otherwise returns an invalid PortPin with
0124          * a pin of -1.
0125          */
0126         PortPin toPortPin( const QString & portPinString );
0127         /**
0128          * Returns the address that the General Purpose Registers starts at.
0129          */
0130         uchar gprStart() const;
0131         
0132         void setParser(Parser *parser) { m_parser = parser; }
0133         void setCode( Code * code ) { m_pCode = code; }
0134         void mergeCode( Code * code );
0135     
0136         void setConditionalCode( Code * ifCode, Code * elseCode );
0137         Code * ifCode();
0138         Code * elseCode();
0139     
0140         Code * m_ifCode;
0141         Code * m_elseCode;
0142 
0143         void postCompileConstruct( const QStringList &interrupts );
0144 
0145         /**
0146          * @returns whether or not the port is valid.
0147          * @see isValidPortPin
0148          */
0149         bool isValidPort( const QString & portName ) const;
0150         /**
0151          * @returns whether or not the port and pin is a valid combination.
0152          * @see isValidPort
0153          */
0154         bool isValidPortPin( const PortPin & portPin ) const;
0155         bool isValidTris( const QString & trisName ) const;
0156         bool isValidInterrupt( const QString & interruptName ) const;
0157 
0158 ///modified new function isValidRegister is added ******************
0159 
0160         bool isValidRegister( const QString & interruptName ) const;
0161 //      bool isValidRegisterBit( const QString & interruptName ) const; 
0162 //TODO GIE=high
0163 //******************************modification ends***********************************
0164         void Sgoto(const QString &label);
0165         void Slabel(const QString &label);
0166         void Send();
0167         void Ssubroutine(const QString &procName, Code * compiledProcCode);
0168         void Sinterrupt(const QString & procName, Code * compiledProcCode);
0169         void Scall(const QString &name);
0170     
0171         void Ssetlh( const PortPin & portPin, bool high);
0172     
0173         void add( QString val1, QString val2, LocationType val1Type, LocationType val2Type );
0174         void subtract( const QString & val1, const QString & val2, LocationType val1Type, LocationType val2Type );
0175         void mul( QString val1, QString val2, LocationType val1Type, LocationType val2Type);
0176         void div( const QString & val1, const QString & val2, LocationType val1Type, LocationType val2Type);
0177     
0178         void assignNum(const QString & val);
0179         void assignVar(const QString & val);
0180     
0181         void saveToReg(const QString &dest);
0182         /**
0183          * Move the contents of the working register to the register with the given
0184          * name.
0185          */
0186         void saveResultToVar( const QString & var );
0187         /** Code for "==" */
0188         void equal( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0189         /** Code for "!=" */
0190         void notEqual( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0191         /** Code for ">" */
0192         void greaterThan( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0193         /** Code for "<" */
0194         void lessThan( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0195         /** Code for ">=" */
0196         void greaterOrEqual( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0197         /** Code for "<=" */
0198         void lessOrEqual( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0199 
0200 ///*****modified the function **************
0201 
0202         //commented for new function since it is not working 
0203 //      void bitwise( Expression::Operation op, const QString &val1, const QString &val2, bool val1IsNum, bool val2IsNum );
0204         //code for AND OR XOR opertions 
0205         void bitwise( Expression::Operation op,const QString & val1, const QString & val2, LocationType val1Type, LocationType val2Type);
0206 
0207 //*******************modification end  ---Result --- new code is working well**************
0208     
0209         void Swhile( Code * whileCode, const QString &expression);
0210         void Srepeat( Code * repeatCode, const QString &expression);
0211         void Sif( Code * ifCode, Code * elseCode, const QString &expression);
0212         void Sfor( Code * forCode, Code * initCode, const QString &expression, const QString &variable, const QString &step, bool stepPositive);
0213     
0214         void Spin( const PortPin & portPin, bool NOT);
0215         void addCommonFunctions( DelaySubroutine delay );
0216     
0217         //BEGIN "Special Functionality" functions
0218         /**
0219          * Delay the program execution, for the given period of length_us (unit:
0220          * microseconds).
0221          * @param pos the position to add the code for calling the delay subroutine.
0222          */
0223         void Sdelay( unsigned length_us, Code::InstructionPosition pos = Code::Middle );
0224         /**
0225          * Output the working register to the given seven segment.
0226          * @param pinMap The variable giving the pin configuration of the seven
0227          * segment.
0228          */
0229         void SsevenSegment( const Variable & pinMap );
0230         /**
0231          * Read the value of the keypad to the working register.
0232          * @param pinMap The variable giving the pin configuration of the keypad.
0233          */
0234         void Skeypad( const Variable & pinMap );
0235         //END "Special Functionality" functions
0236     
0237         void SincVar( const QString &var );
0238         void SdecVar( const QString &var );
0239         void SrotlVar( const QString &var );
0240         void SrotrVar( const QString &var );
0241     
0242         void Stristate( const QString &port );
0243     
0244         void Sasm(const QString &raw);
0245 
0246     
0247     protected:
0248         void multiply();
0249         void divide();
0250     
0251         /** @see MicrobeApp::m_picType */
0252         Type m_type;
0253     
0254         Parser * m_parser;
0255         MicrobeApp * mb;
0256         Code * m_pCode;
0257     
0258         void ifInitCode( const QString &val1, const QString &val2, LocationType val1Type, LocationType val2Type );
0259     
0260         /**
0261          * The function makes sure that val1 always contains a working register
0262          * variable, if one has been passed, this is done by swapping val1 and val2 when
0263          * neccessary
0264          */
0265         void rearrangeOpArguments( QString * val1, QString * val2, LocationType * val1Type, LocationType * val2Type);
0266     
0267         /**
0268          * @param flag True means give flag bit, false means give enable bit instead
0269          */
0270         int interruptNameToBit(const QString &name, bool flag);
0271 };
0272 
0273 #endif