📄 gui.c
字号:
/* * GUI interface for Picstart Plus programmer * Copyright (C) 2004 by Jeffery L. Post * theposts<AT>pacbell<DOT>net * * gui.c * * Version 0.0.8 - 08/01/04 * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>#include <ctype.h>#include <unistd.h>#include <fcntl.h>#include <signal.h>#include <sys/stat.h>#include "picdev.h"#include "widgets.h"#include "gui.h"#include "fileselect.h"#include "deviceselect.h"#define MINFWVERSION 0x030028 // PS+ version 3.00.40, minimum for oldFirmware = FALSE#define BLANK_PROGRAM 0x01#define BLANK_CFGBITS 0x02#define BLANK_IDLOC 0x04#define BLANK_DATA 0x08#define DATARECORD 0 // data record#define ENDRECORD 1 // end record#define SEGADDRESS 2 // segment address record (INHX32)#define EXTADDRESS 4 // extended linear address record (INHX32)// picp function typesenum functionTypes { PIC_FUNC_NONE, PIC_FUNC_READ, PIC_FUNC_WRITE, PIC_FUNC_VERIFY, PIC_FUNC_MAX // invalid function type};// picp command typesenum commandTypes { PICP_COMMAND_GET_DEVICES, // 0 get supported device types (not used) PICP_COMMAND_GET_VERSION, // 1 get PS+ firmware version PICP_COMMAND_READ_PGM, // 2 read PIC program from device PICP_COMMAND_WRITE_PGM, // 3 write program to PIC device PICP_COMMAND_READ_ID, // 4 read ID bits PICP_COMMAND_WRITE_ID, // 5 write ID bits PICP_COMMAND_READ_CFG, // 6 read configuration bits PICP_COMMAND_WRITE_CFG, // 7 write configuration bits PICP_COMMAND_READ_DATA, // 8 read data memory PICP_COMMAND_WRITE_DATA, // 9 write data memory PICP_COMMAND_READ_CAL, // 10 read calibration PICP_COMMAND_WRITE_CAL, // 11 write calibration PICP_COMMAND_ERASE_PGM, // 12 erase program space (non-flash) PICP_COMMAND_ERASE_ID, // 13 erase ID bits PICP_COMMAND_ERASE_CFG, // 14 erase configuration bits PICP_COMMAND_ERASE_DATA, // 15 erase data space PICP_COMMAND_ERASE_FLASH, // 16 erase flash device PICP_COMMAND_BLANK_CHECK_ALL, // 17 blank check all regions PICP_COMMAND_VERIFY_PGM, // 18 verify program with buffer data PICP_COMMAND_VERIFY_ID, // 19 verify ID locations with ID PICP_COMMAND_VERIFY_CFG, // 20 verify configuration bits PICP_COMMAND_VERIFY_DATA, // 21 verify data with buffer PICP_COMMAND_GET_PICP_VERSION, // 22 get picp version number PICP_COMMAND_MAX // invalid command if >= this number};// Edit typesenum editingTypes { EDIT_NONE, EDIT_CONFIG, EDIT_ID, EDIT_MAX // invalid type};// For building program segment listtypedef struct programSegment { unsigned int start; unsigned int end; struct programSegment *next;} PGMSEG;extern void removeWList(void *w);extern void addWList(void *w);// Prototypesint waitForAlertResponse(ALERT *alert);void connectFailAlert(void);void saveFile(char *fname);void saveUserFileName(void);void getcode(char *from, byte *loc);void setSelectedDevice(char *name);void CloseHelpMenu(SDL_Event *event, WIDGET *w);void ClearHelpMenus(void);bool parseHexLine(char *line, byte *bfr);int getBlankCheckBit(char *line);void moveVMenu(void *m, int x, int y);void setMemoryDisplay(bool flag);void showProgram(void);bool checkPicDevice(void);// Callback functionsvoid AlertCB(SDL_Event *event, ALERT *ab);void NoticeCB(SDL_Event *event, NOTICE *note);void fileButtonCB(SDL_Event *event);void enableButtonCB(SDL_Event *event);void saveButtonCB(SDL_Event *event, MENU *m);void saveAsButtonCB(SDL_Event *event, MENU *m);void saveConfigButtonCB(SDL_Event *event);void userFileCB(SDL_Event *event);void userFileInputCB(SDL_Event *event, INPUTBOX *ib);void userFileOkCB(SDL_Event *event);void userFileCancelCB(SDL_Event *event);void closeFileMenuCB(SDL_Event *event);void editBoxCB(SDL_Event *event, INPUTBOX *ib);void editBoxOkCB(SDL_Event *event);void editBoxCancelCB(SDL_Event *event);void openButtonCB(SDL_Event *event, MENU *m);void portButtonCB(SDL_Event *event);void port0ButtonCB(SDL_Event *event);void port1ButtonCB(SDL_Event *event);void port2ButtonCB(SDL_Event *event);void port3ButtonCB(SDL_Event *event);void portUSB0ButtonCB(SDL_Event *event);void portUSB1ButtonCB(SDL_Event *event);void portUSB2ButtonCB(SDL_Event *event);void portUSB3ButtonCB(SDL_Event *event);void closePortMenuCB(SDL_Event *event);void selectDeviceCB(SDL_Event *event);void readButtonCB(SDL_Event *event);void programButtonCB(SDL_Event *event);void verifyButtonCB(SDL_Event *event);void blankButtonCB(SDL_Event *event);void helpMenuCB(SDL_Event *event, BUTTON *b);void aboutMenuCB(SDL_Event *event, BUTTON *b);void helpButtonCB(SDL_Event *event);void aboutButtonCB(SDL_Event *event);void exitButtonCB(SDL_Event *event, MENU *m);void configEditCB(SDL_Event *event);void IdEditCB(SDL_Event *event);void disVSliderCB(SDL_Event *event, VSLIDER *vs);void outputWinCB(SDL_Event *event, TEXTBOX *tbox);void displayLabelCB(SDL_Event *event);void disasmCB(SDL_Event *event);// Help menu callbacksvoid FileHelpCB(SDL_Event *event);void SaveHelpCB(SDL_Event *event);void SaveAsHelpCB(SDL_Event *event);void SaveConfigHelpCB(SDL_Event *event);void HelpHelpCB(SDL_Event *event);void AboutHelpCB(SDL_Event *event);void serialPortHelpCB(SDL_Event *event);void enableHelpCB(SDL_Event *event);void deviceHelpCB(SDL_Event *event);void readHelpCB(SDL_Event *event);void programHelpCB(SDL_Event *event);void eraseHelpCB(SDL_Event *event);void verifyHelpCB(SDL_Event *event);void blankHelpCB(SDL_Event *event);void configBitsHelpCB(SDL_Event *event);void IdHelpCB(SDL_Event *event);void DisasmHelpCB(SDL_Event *event);void DisplayModeHelpCB(SDL_Event *event);// Global variablesSDL_Surface *screen = NULL; // main screenchar caption[64] = "GPICP PIC Micro Programmer - Version x.xx ";char versionString[64] = " Version ";char picFWversionString[64];char warpFWversion[64];char *picName = NULL;int picFWversion = 0;bool oldFirmware = FALSE;int blankCheckBits = 0;int picpVersion = 0;int picpMajorRev = 0;int picpMinorRev = 0;char configFileName[] = "gpicp.cfg";char configLine[MAX_CFG_LEN];char serialPort[MAX_CFG_LEN] = " none selected ";char deviceType[MAX_CFG_LEN] = " none selected ";//char IdText[64] = "0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000"; // up to 8 wordschar IdText[64] = "0x0000 0x0000 0x0000 0x0000 "; // default 4 words//char configText[] = "0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000 0x0000"; // up to 8 wordschar configText[64] = "0x0000 "; // default 1 wordchar picpCommandName[16] = "picp";char *serialPortNames[] = { "/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3", "/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", NULL};int PicFunction = PIC_FUNC_NONE;PIC_DEFINITION *selectedPicDevice = NULL;// WidgetsMENU *menu1 = NULL; // main menuMENU *menu2 = NULL; // main menuMENU *filename = NULL; // current file name menuLABEL *filelabel = NULL; // current file name labelVMENU *fileMenu = NULL; // file sub-menuVMENU *regionMenu = NULL;BUTTON *regionDataButton = NULL;BUTTON *eraseRegionDataButton = NULL;char regionTypeText[20]; // will be filled in by callbacksVMENU *eraseRegionMenu = NULL;VMENU *helpMenu = NULL; // help screenVMENU *aboutMenu = NULL; // about screenVMENU *portMenu = NULL;MENU *picstartName = NULL;LABEL *picstartVersion = NULL;BUTTON *saveButton = NULL;BUTTON *saveAsButton = NULL;BUTTON *saveConfigButton = NULL;MENU *statusMenu1 = NULL;MENU *statusMenu2 = NULL;MENU *statusMenu3 = NULL;LABEL *configTextLabel = NULL;LABEL *configDataLabel = NULL;LABEL *IdTextLabel = NULL;LABEL *IdDataLabel = NULL;VMENU *FileHelpMenu = NULL;VMENU *OpenHelpMenu = NULL;VMENU *SaveHelpMenu = NULL;VMENU *SaveAsHelpMenu = NULL;VMENU *SaveConfigHelpMenu = NULL;VMENU *ExitHelpMenu = NULL;VMENU *CloseMenuHelpMenu = NULL;VMENU *HelpHelpMenu = NULL;VMENU *AboutHelpMenu = NULL;VMENU *serialPortHelpMenu = NULL;VMENU *enableHelpMenu = NULL;VMENU *deviceHelpMenu = NULL;VMENU *readHelpMenu = NULL;VMENU *programHelpMenu = NULL;VMENU *eraseHelpMenu = NULL;VMENU *verifyHelpMenu = NULL;VMENU *blankHelpMenu = NULL;VMENU *configBitsHelpMenu = NULL;VMENU *IdHelpMenu = NULL;VMENU *DisasmHelpMenu = NULL;VMENU *DisplayHelpMenu = NULL;ALERT *fileErrorAlert = NULL;VSLIDER *disVSlider = NULL;TEXTBOX *outputWin = NULL; // window for output displayLABEL *pmwLabel = NULL; // program memory window labelLABEL *dmwLabel = NULL; // data memory window labelALERT *errorAlert = NULL;ALERT *confirmAlert = NULL;ALERT *confirmEraseAlert = NULL;ALERT *connectAlert = NULL;WINDOW *userFileNameWin = NULL;INPUTBOX *userFileName = NULL;LABEL *userFileNameLabel = NULL;BUTTON *userFileNameOkButton = NULL;BUTTON *userFileNameCancelButton = NULL;char userFileNameBuffer[USER_FILENAME_SIZE];WINDOW *editBoxWin = NULL;INPUTBOX *editBox = NULL;LABEL *editBoxLabel = NULL;BUTTON *editBoxOkButton = NULL;BUTTON *editBoxCancelButton = NULL;int editWhich = EDIT_NONE;char editBoxBuffer[EDIT_BOX_SIZE];WINDOW *connectWin = NULL;WINDOW *readingWin = NULL;WINDOW *writingWin = NULL;WINDOW *erasingWin = NULL;WINDOW *checkingWin = NULL;NOTICE *blankCheckResultWin = NULL;NOTICE *verifyResultWin = NULL;char nullString[] = "";char alertMessage[128] = "";char noticeMessage[256] = "";char linebuffer[MAX_LINE]; // input line bufferbool portButtonActive = FALSE;bool helpButtonActive = FALSE;bool aboutButtonActive = FALSE;bool fileSelectionActive = FALSE;bool fileButtonActive = FALSE;CHECKBOX *commDbgBox = NULL; // for comm line debuggingbool commdbg = FALSE; // generate picpcomm.log fileCHECKBOX *ISPBox = NULL; // for ISP programming withbool ISPprog = FALSE; // Warp-13 BluePole v 1.5char *loadFileTypes[] = { "*.hex", NULL};time_t file_mtime; // input file modification timechar src[FN_LEN]; // file name bufferchar fileStr[FN_LEN]; // the current file namechar fileName[FN_LEN]; // base file namechar fileExt[FN_LEN / 2]; // input file extensionchar programFileStr[FN_LEN]; // program file namechar dataFileStr[FN_LEN]; // data file namebyte dataWidthHi, dataWidthLo; // width of program data wordbyte *picProgramData = NULL; // buffer for PIC program databyte *picProgramTemp = NULL; // extra buffer for verify programbyte *picDataBuffer = NULL; // buffer for PIC data memorybyte *picDataTemp = NULL; // extra buffer for verify databool programDisplayFlag = TRUE; // false if data memory displayedint picProgramPos = 0; // slider position for program displayint picProgramPcent = 0; // slider percent for program displayint picProgramPC = 0; // display program locationint picDataPos = 0; // slider position for data displayint picDataPcent = 0; // slider percent for data displayint picDataPC = 0; // display data locationint picProgramSize = 0; // program memory size for current deviceint picDataSize = 0; // data memory size for current deviceint recordCount; // line number in Intel hex fileint recordType; // Intel hex file record typeunsigned int extendedAddress; // bits 31-16 are bits 31-16 of the address for Intel hex records, bits 15-0 are 0unsigned int IdAddress; // address of ID locations for current device (in bytes)unsigned int IdSize; // size of ID locations (in bytes)unsigned int CfgAddress; // address of configuration bits for current device (in bytes)unsigned int CfgSize; // size of configuration bits (in bytes)unsigned int EepromAddress; // address of eeprom dataunsigned int EepromSize; // size of eeprom dataPGMSEG *segmentList = NULL; // program segment list// Display widgetsLABEL *progLine0 = NULL;LABEL *progLine1 = NULL;LABEL *progLine2 = NULL;LABEL *progLine3 = NULL;LABEL *progLine4 = NULL;LABEL *progLine5 = NULL;LABEL *progLine6 = NULL;LABEL *progLine7 = NULL;LABEL *progLine8 = NULL;LABEL *progLine9 = NULL;LABEL *progLineA = NULL;LABEL *progLineB = NULL;LABEL *progLineC = NULL;LABEL *progLineD = NULL;LABEL *progLineE = NULL;LABEL *progLineF = NULL;char progData0[outputWidth];char progData1[outputWidth];char progData2[outputWidth];char progData3[outputWidth];char progData4[outputWidth];char progData5[outputWidth];char progData6[outputWidth];char progData7[outputWidth];char progData8[outputWidth];char progData9[outputWidth];char progDataA[outputWidth];char progDataB[outputWidth];char progDataC[outputWidth];char progDataD[outputWidth];char progDataE[outputWidth];char progDataF[outputWidth];char *progData[DISPLAY_LINES] = { progData0, progData1, progData2, progData3, progData4, progData5, progData6, progData7, progData8, progData9, progDataA, progDataB, progDataC, progDataD, progDataE, progDataF,};FILE *commandFile = NULL; // for capturing stdout output of picpFILE *errorFile = NULL; // for capturing stderr output of picpFILE *gpicdasmFile = NULL; // for running gpicdasmchar errorFileName[] = "gpicpErrorFile";char tempFileName[] = "gpicpTempFile.hex";char gpicdasmFileName[] = "gpicdasm";char commandResponse[128];char errorResponse[128];///////////////// Code///////////////// return the size of the program space of the specified device (in words)unsigned int GetPgmSize(const PIC_DEFINITION *picDevice){ return(picDevice->def[PD_PGM_SIZEH] * 256 + picDevice->def[PD_PGM_SIZEL]);}// return the size of the data space of the specified device (in bytes)unsigned int GetDataSize(const PIC_DEFINITION *picDevice){ return(picDevice->def[PD_DATA_SIZEH] * 256 + picDevice->def[PD_DATA_SIZEL]);}// return the start address of the data space of the specified deviceunsigned int GetDataStart(const PIC_DEFINITION *picDevice){ return (picDevice->eeaddr) ? picDevice->eeaddr : (unsigned) (picDevice->def[PD_DATA_ADDRH] * 256 + picDevice->def[PD_DATA_ADDRL]);}// return the ID Locations area size, in words.unsigned int GetIDSize(const PIC_DEFINITION *picDevice){ return picDevice->def[PD_ID_SIZE];}// return the ID Locations area start addr.unsigned int GetIDAddr(const PIC_DEFINITION *picDevice){ return picDevice->idaddr ? picDevice->idaddr / 2: (unsigned) picDevice->def[PD_ID_ADDRH] * 256 + picDevice->def[PD_ID_ADDRL];}// return the size of the oscillator calibration space of the specified device (in words)unsigned short int GetOscCalSize(const PIC_DEFINITION *picDevice){ return(picDevice->def[PD_CLK_SIZEH] * 256 + picDevice->def[PD_CLK_SIZEL]);}// return the start address of the oscillator calibration space of the specified deviceunsigned int GetOscCalStart(const PIC_DEFINITION *picDevice){ return(picDevice->def[PD_CLK_ADDRH] * 256 + picDevice->def[PD_CLK_ADDRL]);}// return the start address of the configuration bits, in words.unsigned int GetConfigStart(const PIC_DEFINITION *picDevice){ return picDevice->cfgmem ? picDevice->cfgmem / 2: (unsigned) (picDevice->def[PD_CFG_ADDRH] * 256 + picDevice->def[PD_CFG_ADDRL]);}// return the size of the configuration bits, in words.unsigned int GetConfigSize(const PIC_DEFINITION *picDevice){ return picDevice->def[PD_CFG_SIZE];}// Get the alignment interval for recording purposes (in words).unsigned int GetWordAlign(const PIC_DEFINITION *picDevice){ return picDevice->wordalign ? picDevice->wordalign : 0; // No align by default}// Get the word width for this deviceunsigned short int GetWordWidth(const PIC_DEFINITION *picDevice){ return (picDevice->def[PD_PGM_WIDTHH]) << 8 | picDevice->def[PD_PGM_WIDTHL];}// Routines for segment list handlingvoid showSegmentList(void) // for test only...{ PGMSEG *seg; seg = segmentList; fprintf(stderr, "Program Segment list:\n"); while (seg) { fprintf(stderr, " start 0x%x, end 0x%x\n", seg->start, seg->end); seg = seg->next; }}void removeSegmentList(void){ PGMSEG *seg, *next; seg = segmentList; while (seg) { next = seg->next; free(seg); seg = next; } segmentList = NULL;}void addToSegmentList(unsigned int start, unsigned int end){ PGMSEG *seg, *next; next = (PGMSEG *) malloc(sizeof(PGMSEG)); if (!next) { fprintf(stderr, "Can't allocate memory for program segment list!\n"); exit(1); } next->start = start; next->end = end; next->next = NULL; seg = segmentList; while (seg && seg->next) seg = seg->next; if (seg) seg->next = next; else segmentList = next;}// Construct a segment list based on data in the buffer.void makeSegmentList(void){ int i, size, word, data, start, stop; bool track; PIC_DEFINITION *pdev; pdev = selectedPicDevice; track = TRUE;// [TODO] incomplete - needs more testing if (pdev) { removeSegmentList(); start = stop = 0; size = pdev->def[PD_PGM_SIZEH] * 256 + pdev->def[PD_PGM_SIZEL]; word = pdev->def[PD_DATA_WIDTHH] * 256 + pdev->def[PD_DATA_WIDTHL];//printf("\nsize 0x%x, word 0x%x\n", size, word); for (i=0; i < size - 1; i += 2) { data = picProgramData[i + 1] * 256 + picProgramData[i]; if (track)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -