diff --git a/MCUME_teensy/teensy5200/emuapi.cpp b/MCUME_teensy/teensy5200/emuapi.cpp index 1b702d8..576f2d5 100644 --- a/MCUME_teensy/teensy5200/emuapi.cpp +++ b/MCUME_teensy/teensy5200/emuapi.cpp @@ -9,38 +9,83 @@ extern "C" { #include "logo.h" #include "bmpjoy.h" #include "bmpvbar.h" +#ifdef OLD_LAYOUT #include "bmpvga.h" #include "bmptft.h" - -#ifndef SD_CS -#define USE_SDFAT 1 #endif + #ifdef HAS_I2CKBD #include #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -561,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -583,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -802,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -839,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1043,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Atari5200 Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " Atari5200 Emulator" #define ROMSDIR "5200" #define emu_Init(ROM) {at5_Init(); at5_Start(ROM);} @@ -119,6 +121,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensy5200/iopins.h b/MCUME_teensy/teensy5200/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensy5200/iopins.h +++ b/MCUME_teensy/teensy5200/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensy5200/platform_config.h b/MCUME_teensy/teensy5200/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensy5200/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensy5200/teensy5200.ino b/MCUME_teensy/teensy5200/teensy5200.ino index b40becf..7fa82ca 100644 --- a/MCUME_teensy/teensy5200/teensy5200.ino +++ b/MCUME_teensy/teensy5200/teensy5200.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensy5200/tft_t_dma.cpp b/MCUME_teensy/teensy5200/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensy5200/tft_t_dma.cpp +++ b/MCUME_teensy/teensy5200/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - -void emu_resetSD(void) -{ -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } -} - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -570,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -592,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -811,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -848,6 +415,45 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } return (retval); } @@ -1052,3 +658,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " NES Emulator " +// Title: < > +#define TITLE " NES Emulator " #define ROMSDIR "c64" #define emu_Init(ROM) {c64_Start(ROM); c64_Init(); } @@ -94,6 +95,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensy64/iopins.h b/MCUME_teensy/teensy64/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensy64/iopins.h +++ b/MCUME_teensy/teensy64/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensy64/patches.cpp b/MCUME_teensy/teensy64/patches.cpp index 22d62df..46abd17 100644 --- a/MCUME_teensy/teensy64/patches.cpp +++ b/MCUME_teensy/teensy64/patches.cpp @@ -191,13 +191,13 @@ uint16_t addr,size; Serial.println("loading"); //printf("%s,%d,%d:", filename, device, secondaryAddress); - //tft.stopDMA(); + tft.stopDMA(); //emu_resetSD(); - //tft.fillScreenNoDma( RGBVAL16(0x00,0x00,0x00) ); + tft.fillScreenNoDma( RGBVAL16(0x00,0x00,0x00) ); if (emu_FileOpen(filename) == 0) { //Serial.println("not found"); cpu.pc = 0xf530; //Jump to $F530 - //tft.startDMA(); + tft.startDMA(); return; } @@ -214,7 +214,7 @@ uint16_t addr,size; cpu.y = 0x49; //Offset for "LOADING" cpu.pc = 0xF12B; //Print and return emu_printf("loaded"); - //tft.startDMA(); + tft.startDMA(); return; } diff --git a/MCUME_teensy/teensy64/platform_config.h b/MCUME_teensy/teensy64/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensy64/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensy64/teensy64.ino b/MCUME_teensy/teensy64/teensy64.ino index 269725c..eb7a72e 100644 --- a/MCUME_teensy/teensy64/teensy64.ino +++ b/MCUME_teensy/teensy64/teensy64.ino @@ -180,19 +180,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensy64/tft_t_dma.cpp b/MCUME_teensy/teensy64/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensy64/tft_t_dma.cpp +++ b/MCUME_teensy/teensy64/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -561,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -583,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -802,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -839,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1043,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Atari 800 Emulator " +// Title: < > +#define TITLE " Atari 800 Emulator" #define ROMSDIR "800" #define emu_Init(ROM) {at8_Init(); at8_Start(ROM);} @@ -109,6 +110,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensy800/iopins.h b/MCUME_teensy/teensy800/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensy800/iopins.h +++ b/MCUME_teensy/teensy800/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensy800/platform_config.h b/MCUME_teensy/teensy800/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensy800/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensy800/teensy800.ino b/MCUME_teensy/teensy800/teensy800.ino index 641e578..12f1a21 100644 --- a/MCUME_teensy/teensy800/teensy800.ino +++ b/MCUME_teensy/teensy800/teensy800.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensy800/tft_t_dma.cpp b/MCUME_teensy/teensy800/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensy800/tft_t_dma.cpp +++ b/MCUME_teensy/teensy800/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} void emu_printf(char * text) @@ -563,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -585,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -804,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -841,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1045,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " 8086 PC Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > > +#define TITLE " 8086 PC Emulator" #define ROMSDIR "/pc" #define emu_Init(ROM) {apc_Init(); apc_Start(ROM);} @@ -112,6 +114,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensy8086/iopins.h b/MCUME_teensy/teensy8086/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensy8086/iopins.h +++ b/MCUME_teensy/teensy8086/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensy8086/platform_config.h b/MCUME_teensy/teensy8086/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensy8086/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensy8086/teensy8086.ino b/MCUME_teensy/teensy8086/teensy8086.ino index 3ac31e9..b4f2b2d 100644 --- a/MCUME_teensy/teensy8086/teensy8086.ino +++ b/MCUME_teensy/teensy8086/teensy8086.ino @@ -174,19 +174,6 @@ static unsigned char col=0; // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensy8086/tft_t_dma.cpp b/MCUME_teensy/teensy8086/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensy8086/tft_t_dma.cpp +++ b/MCUME_teensy/teensy8086/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; -static const uint16_t * logo = logozx80kbd; -static const unsigned short * keysw = keyswzx80; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -61,21 +105,24 @@ static const unsigned short * keysw = keyswzx80; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; +static const uint16_t * logo = logozx80kbd; +static const unsigned short * keysw = keyswzx80; const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -97,53 +144,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } -/* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); -*/ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -564,12 +251,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -586,151 +289,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -805,18 +363,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -842,6 +418,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1050,3 +666,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " ZX81/ZX80 Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " ZX81/ZX80 Emulator" #define ROMSDIR "z81" #define emu_Init(ROM) {z81_Start(ROM); z81_Init(); } @@ -109,6 +111,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensy81/iopins.h b/MCUME_teensy/teensy81/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensy81/iopins.h +++ b/MCUME_teensy/teensy81/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensy81/platform_config.h b/MCUME_teensy/teensy81/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensy81/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensy81/teensy81.ino b/MCUME_teensy/teensy81/teensy81.ino index 8366e95..7c928ce 100644 --- a/MCUME_teensy/teensy81/teensy81.ino +++ b/MCUME_teensy/teensy81/teensy81.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensy81/tft_t_dma.cpp b/MCUME_teensy/teensy81/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensy81/tft_t_dma.cpp +++ b/MCUME_teensy/teensy81/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif #ifdef USE_SDFS -#define SDFSDEV "2:" - #include "uSDFS.h" static FATFS fatfs; static FIL file; #else + +#ifdef USE_SDFAT +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; +#else #include +#endif static File file; #endif -extern TFT_T_DMA tft; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -const uint16_t deflogo[] = { - 0x0000,0x0000 -}; -static const uint16_t * logo = deflogo; -static unsigned short * keys; -static int keyMap; - -#ifdef USE_SDFS -#define CALIBRATION_FILE SDFSDEV "/cal.cfg" -#else #define CALIBRATION_FILE "/cal.cfg" -#endif -#define MAX_FILES 128 -#define MAX_FILENAME_SIZE 28 -#define MAX_MENULINES (MKEY_L9) -#define TEXT_HEIGHT 16 -#define TEXT_WIDTH 8 -#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) -#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) -#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) -#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) -#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) -#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) -#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) -#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) - -#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) -//#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -//#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) - #define MKEY_L1 1 #define MKEY_L2 2 @@ -82,6 +84,45 @@ static int keyMap; #define MKEY_TFT 23 #define MKEY_VGA 24 +#define MAX_FILES 32 +#define MAX_FILENAME_SIZE 24 +#define MAX_MENULINES (MKEY_L9) +#define TEXT_HEIGHT 16 +#define TEXT_WIDTH 8 +#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) +#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) +#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) +#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) +#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) +#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) +#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) +#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) + +#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) +#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) + +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +const uint16_t deflogo[] = { + 0x0000,0x0000 +}; +static const uint16_t * logo = deflogo; +static unsigned short * keys; +static int keyMap; + + const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, TAREA_WH,MENU_FILE_W, TEXT_HEIGHT, @@ -102,73 +143,18 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - -#ifdef USE_SDFS - DIR dir; - FILINFO entry; - f_opendir(&dir, romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - emu_printf(newpath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(newpath, &entry); - if ( (entry.fattrib & AM_DIR) ) { -#else - File file = SD.open(newpath); - if (file.isDirectory()) { -#endif - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - //tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFS - strcpy(romspath,SDFSDEV); - strcat(romspath,ROMSDIR); - FRESULT rc; - if((rc = f_mount (&fatfs, romspath, 1))) { - emu_printf("Fail mounting SDFS"); - } - else { - rc = f_chdrive(ROMSDIR); - } -#else - if (!SD.begin(SD_CS)) { - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); -#endif - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif - - keys = key_map3; - keyMap = 2; -} - void emu_printf(char * text) { @@ -673,216 +288,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { -#else - if ((file = SD.open(filepath, O_READ))) { -#endif - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - int retval=0; - while (remaining>0) { -#ifdef USE_SDFS - if( !(f_read (&file, buffer, 256, &retval)) ) - //f_read (&file, buffer, 256, &retval); -#else - retval = file.read(buffer, 256); -#endif - if (retval>0) { - //emu_printi(retval); - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - else { - break; - } - } - - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; -#ifdef USE_SDFS - int retval=0; - if( !(f_read (&file, &c, 1, &retval)) ) -#else - int retval = file.read(&c, 1); -#endif - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ -#ifdef USE_SDFS - f_close(&file); -#else - file.close(); -#endif -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(filepath, &entry); - filesize = entry.fsize; -#else - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } -#endif - - return(filesize); -} - -int emu_FileSeek(int seek) -{ -#ifdef USE_SDFS - f_lseek(&file, seek); -#else - file.seek(seek); -#endif - return (seek); -} - -int emu_FileTell(void) -{ -#ifdef USE_SDFS - return (f_tell(&file)); -#else - return (50); -#endif -} - - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - filesize = f_size(&file); - emu_printf(filesize); - if (size >= filesize) - { - int retval=0; - if( (f_read (&file, buf, filesize, &retval)) ) { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } -#endif - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - f_lseek(&file, seek); - emu_printf(size); - if (size >= filesize) - { - int retval=0; - if( (!f_read (&file, buf, size, &retval)) ) - if (retval != size) - { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } -#endif - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -957,18 +362,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -992,8 +415,48 @@ int emu_ReadKeys(void) if ( digitalRead(PIN_KEY_USER4) == LOW ) retval |= MASK_KEY_USER4; #endif - //Serial.println(j1,HEX); + //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1034,7 +497,7 @@ int emu_ReadI2CKeyboard(void) { //Serial.println(match,HEX); for (i=0; i= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i #include #define SPI SPI2 //#define SPICLOCK 104000000 -#define SPICLOCK 60000000 +#define SPICLOCK 70000000 //#define SPICLOCK 144e6 //Just a number..max speed #define SPI_MODE SPI_MODE0 @@ -20,10 +28,6 @@ uint8_t PSRAM_T::_cs, PSRAM_T::_miso, PSRAM_T::_mosi, PSRAM_T::_sclk; -Page PSRAM_T::pages[MAX_PAGES]; -uint8_t PSRAM_T::nbPages=0; -int8_t PSRAM_T::top=0; -int8_t PSRAM_T::last=0; PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) @@ -43,19 +47,20 @@ void PSRAM_T::begin(void) SPI.setMISO(_miso); SPI.setSCK(_sclk); SPI.begin(); - //SPI.setClockDivider(SPI_CLOCK_DIV2); + delay(1); - SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); + SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); SPI.transfer(0x66); digitalWrite(_cs, 1); SPI.endTransaction(); SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); SPI.transfer(0x99); - digitalWrite(_cs, 1); SPI.endTransaction(); + digitalWrite(_cs, 1); + delayMicroseconds(20); } @@ -64,8 +69,8 @@ uint8_t PSRAM_T::psram_read(uint32_t addr) { uint8_t val=0; + digitalWrite(_cs, 0); SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); - digitalWrite(_cs, 0); SPI.transfer(RAM_READ); SPI.transfer((addr>>16)&0xff); SPI.transfer((addr>>8)&0xff); @@ -73,17 +78,17 @@ uint8_t PSRAM_T::psram_read(uint32_t addr) #if RAM_READ == 0xB SPI.transfer(0xFF); #endif - val = SPI.transfer(0xFF); - digitalWrite(_cs, 1); + val = SPI.transfer(0xFF); SPI.endTransaction(); + digitalWrite(_cs, 1); return val; } void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) { - SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); + SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); SPI.transfer(RAM_READ); SPI.transfer((addr>>16)&0xff); SPI.transfer((addr>>8)&0xff); @@ -98,22 +103,22 @@ void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) } */ SPI.transfer(val,n); - digitalWrite(_cs, 1); SPI.endTransaction(); + digitalWrite(_cs, 1); } void PSRAM_T::psram_write(uint32_t addr, uint8_t val) { - SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); + SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); SPI.transfer(RAM_WRITE); SPI.transfer((addr>>16)&0xff); SPI.transfer((addr>>8)&0xff); SPI.transfer(addr&0xff); SPI.transfer(val); - digitalWrite(_cs, 1); SPI.endTransaction(); + digitalWrite(_cs, 1); } static uint8_t resp[PAGE_SIZE]; @@ -121,8 +126,8 @@ static uint8_t resp[PAGE_SIZE]; void PSRAM_T::psram_write_n(uint32_t addr, uint8_t * val, int n) { - SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); + SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); SPI.transfer(RAM_WRITE); SPI.transfer((addr>>16)&0xff); SPI.transfer((addr>>8)&0xff); @@ -134,8 +139,8 @@ void PSRAM_T::psram_write_n(uint32_t addr, uint8_t * val, int n) } */ SPI.transfer(val,&resp[0],n); - digitalWrite(_cs, 1); - SPI.endTransaction(); + SPI.endTransaction(); + digitalWrite(_cs, 1); } @@ -230,6 +235,35 @@ Serial.printf("D") ; Serial.flush(); +#else + + +#include "emuapi.h" + + +PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) +{ +} + +void PSRAM_T::begin(void) +{ + emu_FileTempInit(); +} + +void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) +{ + emu_FileTempRead(addr,val,n); +} + + +void PSRAM_T::psram_write(uint32_t addr, uint8_t val) +{ + emu_FileTempWrite(addr,val); +} + +#endif + + void PSRAM_T::pswrite(uint32_t addr, uint8_t val) { @@ -244,13 +278,15 @@ void PSRAM_T::pswrite(uint32_t addr, uint8_t val) } } -#include "emuapi.h" + + uint8_t PSRAM_T::psread(uint32_t addr) { - //return psram_read(addr); - + //uint8_t val = psram_read(addr); + //return val; uint32_t curPage=addr&(~(PAGE_SIZE-1)); + uint32_t offs = addr&(PAGE_SIZE-1); for (int i=0; i +#include "platform_config.h" -#define PAGE_SIZE 16 //16 //32 //2048 //1024 +#ifdef HAS_PSRAM +#define PAGE_SIZE 16 //32 //2048 //1024 #define MAX_PAGES 8 +#else +#define PAGE_SIZE 256 +#define MAX_PAGES 8 +#endif struct Page { uint8_t page[PAGE_SIZE]; @@ -25,7 +31,8 @@ class PSRAM_T void begin(void); void pswrite(uint32_t addr, uint8_t val); uint8_t psread(uint32_t addr); - + uint16_t psread_w(uint32_t addr); + private: static uint8_t psram_read(uint32_t addr); static void psram_read_n(uint32_t addr, uint8_t * val, int n); diff --git a/MCUME_teensy/teensycastaway/teensycastaway.ino b/MCUME_teensy/teensycastaway/teensycastaway.ino index 763892a..139e5d3 100644 --- a/MCUME_teensy/teensycastaway/teensycastaway.ino +++ b/MCUME_teensy/teensycastaway/teensycastaway.ino @@ -174,19 +174,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensycastaway/tft_t_dma.cpp b/MCUME_teensy/teensycastaway/tft_t_dma.cpp index 669f8ca..5013b73 100644 --- a/MCUME_teensy/teensycastaway/tft_t_dma.cpp +++ b/MCUME_teensy/teensycastaway/tft_t_dma.cpp @@ -6,8 +6,7 @@ #include "font8x8.h" -//#define SPICLOCK 144e6 //Just a number..max speed -#define SPICLOCK 60000000 +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -219,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -326,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -352,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -362,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -943,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -561,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -583,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -802,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -839,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1043,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Coleco Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " Coleco Emulator" #define ROMSDIR "coleco" #define emu_Init(ROM) {coc_Init();coc_Start(ROM);} @@ -99,6 +101,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensycolem/iopins.h b/MCUME_teensy/teensycolem/iopins.h index f6e4816..eead4a9 100644 --- a/MCUME_teensy/teensycolem/iopins.h +++ b/MCUME_teensy/teensycolem/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 -#define SD_MOSI 11 -#define SD_MISO 12 +#define SD_MOSI 12 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensycolem/platform_config.h b/MCUME_teensy/teensycolem/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensycolem/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensycolem/teensycolem.ino b/MCUME_teensy/teensycolem/teensycolem.ino index 70cf600..ae88ffa 100644 --- a/MCUME_teensy/teensycolem/teensycolem.ino +++ b/MCUME_teensy/teensycolem/teensycolem.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensycolem/tft_t_dma.cpp b/MCUME_teensy/teensycolem/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensycolem/tft_t_dma.cpp +++ b/MCUME_teensy/teensycolem/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif #ifdef USE_SDFS -#define SDFSDEV "2:" - #include "uSDFS.h" static FATFS fatfs; static FIL file; #else + +#ifdef USE_SDFAT +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; +#else #include +#endif static File file; #endif -extern TFT_T_DMA tft; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -const uint16_t deflogo[] = { - 0x0000,0x0000 -}; -static const uint16_t * logo = deflogo; - -#ifdef USE_SDFS -#define CALIBRATION_FILE SDFSDEV "/cal.cfg" -#else #define CALIBRATION_FILE "/cal.cfg" -#endif -#define MAX_FILES 128 -#define MAX_FILENAME_SIZE 28 -#define MAX_MENULINES (MKEY_L9) -#define TEXT_HEIGHT 16 -#define TEXT_WIDTH 8 -#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) -#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) -#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) -#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) -#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) -#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) -#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) -#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) - -#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) -//#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -//#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) - #define MKEY_L1 1 #define MKEY_L2 2 @@ -80,6 +84,43 @@ static const uint16_t * logo = deflogo; #define MKEY_TFT 23 #define MKEY_VGA 24 +#define MAX_FILES 32 +#define MAX_FILENAME_SIZE 24 +#define MAX_MENULINES (MKEY_L9) +#define TEXT_HEIGHT 16 +#define TEXT_WIDTH 8 +#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) +#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) +#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) +#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) +#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) +#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) +#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) +#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) + +#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) +#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) + +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; + + const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, TAREA_WH,MENU_FILE_W, TEXT_HEIGHT, @@ -100,73 +141,18 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - -#ifdef USE_SDFS - DIR dir; - FILINFO entry; - f_opendir(&dir, romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - emu_printf(newpath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(newpath, &entry); - if ( (entry.fattrib & AM_DIR) ) { -#else - File file = SD.open(newpath); - if (file.isDirectory()) { -#endif - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - //tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFS - strcpy(romspath,SDFSDEV); - strcat(romspath,ROMSDIR); - FRESULT rc; - if((rc = f_mount (&fatfs, romspath, 1))) { - emu_printf("Fail mounting SDFS"); - } - else { - rc = f_chdrive(ROMSDIR); - } -#else - while (!SD.begin(SD_CS)) { - emu_printf("SdFat.begin() failed"); - delay(1000); - } - strcpy(romspath,ROMSDIR); -#endif - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -669,216 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { -#else - if ((file = SD.open(filepath, O_READ))) { -#endif - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - int retval=0; - while (remaining>0) { -#ifdef USE_SDFS - if( !(f_read (&file, buffer, 256, &retval)) ) - //f_read (&file, buffer, 256, &retval); -#else - retval = file.read(buffer, 256); -#endif - if (retval>0) { - //emu_printi(retval); - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - else { - break; - } - } - - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; -#ifdef USE_SDFS - int retval=0; - if( !(f_read (&file, &c, 1, &retval)) ) -#else - int retval = file.read(&c, 1); -#endif - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ -#ifdef USE_SDFS - f_close(&file); -#else - file.close(); -#endif -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(filepath, &entry); - filesize = entry.fsize; -#else - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } -#endif - - return(filesize); -} - -int emu_FileSeek(int seek) -{ -#ifdef USE_SDFS - f_lseek(&file, seek); -#else - file.seek(seek); -#endif - return (seek); -} - -int emu_FileTell(void) -{ -#ifdef USE_SDFS - return (f_tell(&file)); -#else - return (50); -#endif -} - - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - filesize = f_size(&file); - emu_printf(filesize); - if (size >= filesize) - { - int retval=0; - if( (f_read (&file, buf, filesize, &retval)) ) { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } -#endif - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - f_lseek(&file, seek); - emu_printf(size); - if (size >= filesize) - { - int retval=0; - if( (!f_read (&file, buf, size, &retval)) ) - if (retval != size) - { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } -#endif - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -953,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -988,8 +413,48 @@ int emu_ReadKeys(void) if ( digitalRead(PIN_KEY_USER4) == LOW ) retval |= MASK_KEY_USER4; #endif - //Serial.println(j1,HEX); + //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1194,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i #include @@ -20,10 +28,6 @@ uint8_t PSRAM_T::_cs, PSRAM_T::_miso, PSRAM_T::_mosi, PSRAM_T::_sclk; -Page PSRAM_T::pages[MAX_PAGES]; -uint8_t PSRAM_T::nbPages=0; -int8_t PSRAM_T::top=0; -int8_t PSRAM_T::last=0; PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) @@ -231,6 +235,35 @@ Serial.printf("D") ; Serial.flush(); +#else + + +#include "emuapi.h" + + +PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) +{ +} + +void PSRAM_T::begin(void) +{ + emu_FileTempInit(); +} + +void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) +{ + emu_FileTempRead(addr,val,n); +} + + +void PSRAM_T::psram_write(uint32_t addr, uint8_t val) +{ + emu_FileTempWrite(addr,val); +} + +#endif + + void PSRAM_T::pswrite(uint32_t addr, uint8_t val) { diff --git a/MCUME_teensy/teensygen/psram_t.h b/MCUME_teensy/teensygen/psram_t.h index c6161db..2a8b644 100644 --- a/MCUME_teensy/teensygen/psram_t.h +++ b/MCUME_teensy/teensygen/psram_t.h @@ -7,9 +7,15 @@ #ifdef __cplusplus #include +#include "platform_config.h" -#define PAGE_SIZE 16 //16 //32 //2048 //1024 +#ifdef HAS_PSRAM +#define PAGE_SIZE 16 //32 //2048 //1024 #define MAX_PAGES 8 +#else +#define PAGE_SIZE 256 +#define MAX_PAGES 8 +#endif struct Page { uint8_t page[PAGE_SIZE]; diff --git a/MCUME_teensy/teensygen/render.c b/MCUME_teensy/teensygen/render.c index 09b7dd8..dfbf59d 100644 --- a/MCUME_teensy/teensygen/render.c +++ b/MCUME_teensy/teensygen/render.c @@ -196,7 +196,7 @@ static uint8 nta_buf[0x400]; /* Plane A / Window line buffer * static uint8 ntb_buf[0x400]; /* Plane B line buffer */ static uint8 obj_buf[0x400]; /* Object layer line buffer */ -//static uint16 line_buf[0x400]; +static uint16 line_buf[0x400]; @@ -366,9 +366,9 @@ void render_line(int line) - //remap_16(lb+0x20, line_buf, pixel_16, width); - //emu_DrawLine16(line_buf, width ,256, line); - remap_16(lb+0x20, emu_LineBuffer(line), pixel_16, width); + remap_16(lb+0x20, line_buf, pixel_16, width); + emu_DrawLine16(line_buf, width ,256, line); + //remap_16(lb+0x20, emu_LineBuffer(line), pixel_16, width); } /*--------------------------------------------------------------------------*/ /* Window rendering */ diff --git a/MCUME_teensy/teensygen/teensygen.ino b/MCUME_teensy/teensygen/teensygen.ino index b23fdd6..7726e45 100644 --- a/MCUME_teensy/teensygen/teensygen.ino +++ b/MCUME_teensy/teensygen/teensygen.ino @@ -175,19 +175,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensygen/tft_t_dma.cpp b/MCUME_teensy/teensygen/tft_t_dma.cpp index 33c4d32..e1aab00 100644 --- a/MCUME_teensy/teensygen/tft_t_dma.cpp +++ b/MCUME_teensy/teensygen/tft_t_dma.cpp @@ -5,8 +5,8 @@ #include "TFT_T_DMA.h" #include "font8x8.h" -//#define SPICLOCK 144e6 //Just a number..max speed -#define SPICLOCK 60000000 + +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -33,7 +33,7 @@ static uint16_t fb0[LINES_PER_BLOCK*TFT_WIDTH]; static uint16_t fb1[LINES_PER_BLOCK*TFT_WIDTH]; static uint16_t fb2[LINES_PER_BLOCK*TFT_WIDTH]; -static uint16_t fb3[LINES_PER_BLOCK*TFT_WIDTH]; +static uint16_t fb3[(TFT_HEIGHT-3*LINES_PER_BLOCK)*TFT_WIDTH]; static uint16_t * blocks[NR_OF_BLOCK]={fb0,fb1,fb2,fb3}; #else static uint16_t * blocks[NR_OF_BLOCK]; @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -861,7 +882,7 @@ void TFT_T_DMA::writeScreen(int width, int height, int stride, uint8_t *buf, uin src=buffer; for (i=0; i>6]; - uint16_t * dst=&block[(y&0x3F)*TFT_WIDTH]; + uint16_t * dst=&block[(y&0x3F)*TFT_WIDTH]; if (width > TFT_WIDTH) { #ifdef TFT_LINEARINT int delta = (width/(width-TFT_WIDTH))-1; int pos = delta; for (int i=0; i> 8]&PAL_COLOR_MASK]; + *dst++=palette16[buf[pos >> 8]]; pos +=step; } #endif @@ -945,8 +966,8 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * else if ((width*2) == TFT_WIDTH) { for (int i=0; i #endif #ifdef USE_SDFS -#define SDFSDEV "2:" - #include "uSDFS.h" static FATFS fatfs; static FIL file; #else + +#ifdef USE_SDFAT +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; +#else #include +#endif static File file; #endif -extern TFT_T_DMA tft; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -const uint16_t deflogo[] = { - 0x0000,0x0000 -}; -static const uint16_t * logo = deflogo; - -#ifdef USE_SDFS -#define CALIBRATION_FILE SDFSDEV "/cal.cfg" -#else #define CALIBRATION_FILE "/cal.cfg" -#endif -#define MAX_FILES 128 -#define MAX_FILENAME_SIZE 28 -#define MAX_MENULINES (MKEY_L9) -#define TEXT_HEIGHT 16 -#define TEXT_WIDTH 8 -#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) -#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) -#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) -#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) -#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) -#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) -#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) -#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) - -#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) -//#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -//#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) - #define MKEY_L1 1 #define MKEY_L2 2 @@ -80,6 +84,43 @@ static const uint16_t * logo = deflogo; #define MKEY_TFT 23 #define MKEY_VGA 24 +#define MAX_FILES 32 +#define MAX_FILENAME_SIZE 24 +#define MAX_MENULINES (MKEY_L9) +#define TEXT_HEIGHT 16 +#define TEXT_WIDTH 8 +#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) +#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) +#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) +#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) +#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) +#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) +#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) +#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) + +#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) +#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) + +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; + + const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, TAREA_WH,MENU_FILE_W, TEXT_HEIGHT, @@ -100,73 +141,18 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - -#ifdef USE_SDFS - DIR dir; - FILINFO entry; - f_opendir(&dir, romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - emu_printf(newpath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(newpath, &entry); - if ( (entry.fattrib & AM_DIR) ) { -#else - File file = SD.open(newpath); - if (file.isDirectory()) { -#endif - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - //tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFS - strcpy(romspath,SDFSDEV); - strcat(romspath,ROMSDIR); - FRESULT rc; - if((rc = f_mount (&fatfs, romspath, 1))) { - emu_printf("Fail mounting SDFS"); - } - else { - rc = f_chdrive(ROMSDIR); - } -#else - while (!SD.begin(SD_CS)) { - emu_printf("SdFat.begin() failed"); - delay(1000); - } - strcpy(romspath,ROMSDIR); -#endif - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -669,216 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { -#else - if ((file = SD.open(filepath, O_READ))) { -#endif - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - int retval=0; - while (remaining>0) { -#ifdef USE_SDFS - if( !(f_read (&file, buffer, 256, &retval)) ) - //f_read (&file, buffer, 256, &retval); -#else - retval = file.read(buffer, 256); -#endif - if (retval>0) { - //emu_printi(retval); - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - else { - break; - } - } - - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; -#ifdef USE_SDFS - int retval=0; - if( !(f_read (&file, &c, 1, &retval)) ) -#else - int retval = file.read(&c, 1); -#endif - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ -#ifdef USE_SDFS - f_close(&file); -#else - file.close(); -#endif -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(filepath, &entry); - filesize = entry.fsize; -#else - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } -#endif - - return(filesize); -} - -int emu_FileSeek(int seek) -{ -#ifdef USE_SDFS - f_lseek(&file, seek); -#else - file.seek(seek); -#endif - return (seek); -} - -int emu_FileTell(void) -{ -#ifdef USE_SDFS - return (f_tell(&file)); -#else - return (50); -#endif -} - - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - filesize = f_size(&file); - emu_printf(filesize); - if (size >= filesize) - { - int retval=0; - if( (f_read (&file, buf, filesize, &retval)) ) { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } -#endif - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - f_lseek(&file, seek); - emu_printf(size); - if (size >= filesize) - { - int retval=0; - if( (!f_read (&file, buf, size, &retval)) ) - if (retval != size) - { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } -#endif - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -953,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -988,8 +413,48 @@ int emu_ReadKeys(void) if ( digitalRead(PIN_KEY_USER4) == LOW ) retval |= MASK_KEY_USER4; #endif - //Serial.println(j1,HEX); + //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1194,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Gameboy Emulator " +// Title: < > +#define TITLE " Gameboy Emulator" #define ROMSDIR "/gameboy" #define emu_Init(ROM) {gbe_Init(); gbe_Start(ROM);} @@ -117,6 +117,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensygnuboy/iopins.h b/MCUME_teensy/teensygnuboy/iopins.h index f57086c..eead4a9 100644 --- a/MCUME_teensy/teensygnuboy/iopins.h +++ b/MCUME_teensy/teensygnuboy/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -37,10 +36,13 @@ // SD #define SD_SCLK 13 -#define SD_MOSI 11 -#define SD_MISO 12 -//#define SD_CS BUILTIN_SDCARD +#define SD_MOSI 12 +#define SD_MISO 11 +#ifdef TEENSYBOY +#define SD_CS BUILTIN_SDCARD +#else #define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -55,8 +57,17 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 diff --git a/MCUME_teensy/teensygnuboy/platform_config.h b/MCUME_teensy/teensygnuboy/platform_config.h new file mode 100644 index 0000000..89bbe11 --- /dev/null +++ b/MCUME_teensy/teensygnuboy/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensygnuboy/psram_t.cpp b/MCUME_teensy/teensygnuboy/psram_t.cpp index 3a47eb8..c5661bc 100644 --- a/MCUME_teensy/teensygnuboy/psram_t.cpp +++ b/MCUME_teensy/teensygnuboy/psram_t.cpp @@ -4,6 +4,14 @@ #include "psram_t.h" +Page PSRAM_T::pages[MAX_PAGES]; +uint8_t PSRAM_T::nbPages=0; +int8_t PSRAM_T::top=0; +int8_t PSRAM_T::last=0; + + +#ifdef HAS_PSRAM + #include #include @@ -20,10 +28,6 @@ uint8_t PSRAM_T::_cs, PSRAM_T::_miso, PSRAM_T::_mosi, PSRAM_T::_sclk; -Page PSRAM_T::pages[MAX_PAGES]; -uint8_t PSRAM_T::nbPages=0; -int8_t PSRAM_T::top=0; -int8_t PSRAM_T::last=0; PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) @@ -231,6 +235,35 @@ Serial.printf("D") ; Serial.flush(); +#else + + +#include "emuapi.h" + + +PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) +{ +} + +void PSRAM_T::begin(void) +{ + emu_FileTempInit(); +} + +void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) +{ + emu_FileTempRead(addr,val,n); +} + + +void PSRAM_T::psram_write(uint32_t addr, uint8_t val) +{ + emu_FileTempWrite(addr,val); +} + +#endif + + void PSRAM_T::pswrite(uint32_t addr, uint8_t val) { diff --git a/MCUME_teensy/teensygnuboy/psram_t.h b/MCUME_teensy/teensygnuboy/psram_t.h index c6161db..2a8b644 100644 --- a/MCUME_teensy/teensygnuboy/psram_t.h +++ b/MCUME_teensy/teensygnuboy/psram_t.h @@ -7,9 +7,15 @@ #ifdef __cplusplus #include +#include "platform_config.h" -#define PAGE_SIZE 16 //16 //32 //2048 //1024 +#ifdef HAS_PSRAM +#define PAGE_SIZE 16 //32 //2048 //1024 #define MAX_PAGES 8 +#else +#define PAGE_SIZE 256 +#define MAX_PAGES 8 +#endif struct Page { uint8_t page[PAGE_SIZE]; diff --git a/MCUME_teensy/teensygnuboy/teensygnuboy.ino b/MCUME_teensy/teensygnuboy/teensygnuboy.ino index fad38d6..2208433 100644 --- a/MCUME_teensy/teensygnuboy/teensygnuboy.ino +++ b/MCUME_teensy/teensygnuboy/teensygnuboy.ino @@ -175,19 +175,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensygnuboy/tft_t_dma.cpp b/MCUME_teensy/teensygnuboy/tft_t_dma.cpp index 33c4d32..5013b73 100644 --- a/MCUME_teensy/teensygnuboy/tft_t_dma.cpp +++ b/MCUME_teensy/teensygnuboy/tft_t_dma.cpp @@ -5,8 +5,8 @@ #include "TFT_T_DMA.h" #include "font8x8.h" -//#define SPICLOCK 144e6 //Just a number..max speed -#define SPICLOCK 60000000 + +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -33,7 +33,7 @@ static uint16_t fb0[LINES_PER_BLOCK*TFT_WIDTH]; static uint16_t fb1[LINES_PER_BLOCK*TFT_WIDTH]; static uint16_t fb2[LINES_PER_BLOCK*TFT_WIDTH]; -static uint16_t fb3[LINES_PER_BLOCK*TFT_WIDTH]; +static uint16_t fb3[(TFT_HEIGHT-3*LINES_PER_BLOCK)*TFT_WIDTH]; static uint16_t * blocks[NR_OF_BLOCK]={fb0,fb1,fb2,fb3}; #else static uint16_t * blocks[NR_OF_BLOCK]; @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -861,7 +882,7 @@ void TFT_T_DMA::writeScreen(int width, int height, int stride, uint8_t *buf, uin src=buffer; for (i=0; i> 8]&PAL_COLOR_MASK]; + *dst++=palette16[buf[pos >> 8]]; pos +=step; } #endif @@ -945,8 +966,8 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * else if ((width*2) == TFT_WIDTH) { for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -59,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -95,7 +141,7 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + static int keypadval=0; static boolean joySwapped = false; static uint16_t bLastState; @@ -106,45 +152,7 @@ static uint8_t prev_zt=0; static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); -#ifdef TEENSYBOY - joySwapped = true; -#else - joySwapped = false; -#endif - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -573,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -595,145 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} int emu_ReadAnalogJoyX(int min, int max) @@ -863,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1067,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " File Selector " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " File Selector" #define ROMSDIR "/" #define emu_Init(ROM) { } @@ -100,6 +102,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensylogo/iopins.h b/MCUME_teensy/teensylogo/iopins.h index df6a178..eead4a9 100644 --- a/MCUME_teensy/teensylogo/iopins.h +++ b/MCUME_teensy/teensylogo/iopins.h @@ -28,6 +28,12 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 + // SD #define SD_SCLK 13 #define SD_MOSI 12 diff --git a/MCUME_teensy/teensylogo/platform_config.h b/MCUME_teensy/teensylogo/platform_config.h index 995b49f..4186a21 100644 --- a/MCUME_teensy/teensylogo/platform_config.h +++ b/MCUME_teensy/teensylogo/platform_config.h @@ -3,6 +3,12 @@ //#define OLD_LAYOUT 1 //#define TEENSYBOY 1 + //#define HAS_PSRAM 1 +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + #endif diff --git a/MCUME_teensy/teensylogo/teensylogo.ino b/MCUME_teensy/teensylogo/teensylogo.ino index 25ce77c..b8c1339 100644 --- a/MCUME_teensy/teensylogo/teensylogo.ino +++ b/MCUME_teensy/teensylogo/teensylogo.ino @@ -26,21 +26,6 @@ static uint8_t col=0x00; void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else -// if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) -// || (emu_ReadKeys() & MASK_KEY_USER4 ) ) -// { -// emu_printf((char*)"esc"); -// *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; -// while (true) { -// ; -// } -// } -#endif - - - uint16_t bClick = emu_DebounceLocalKeys(); if (menuActive()) { int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensylogo/tft_t_dma.cpp b/MCUME_teensy/teensylogo/tft_t_dma.cpp index e59787f..5013b73 100644 --- a/MCUME_teensy/teensylogo/tft_t_dma.cpp +++ b/MCUME_teensy/teensylogo/tft_t_dma.cpp @@ -963,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -59,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -95,7 +141,7 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + static int keypadval=0; static boolean joySwapped = false; static uint16_t bLastState; @@ -106,45 +152,7 @@ static uint8_t prev_zt=0; static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); -#ifdef TEENSYBOY - joySwapped = true; -#else - joySwapped = false; -#endif - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -573,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -595,145 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} int emu_ReadAnalogJoyX(int min, int max) @@ -863,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1067,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " NES Emulator " +// Title: < > +#define TITLE " NES Emulator" #define ROMSDIR "nes" #define emu_Init(ROM) {nes_Start(ROM); nes_Init(); } @@ -92,6 +93,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensynofrendo/iopins.h b/MCUME_teensy/teensynofrendo/iopins.h index df6a178..eead4a9 100644 --- a/MCUME_teensy/teensynofrendo/iopins.h +++ b/MCUME_teensy/teensynofrendo/iopins.h @@ -28,6 +28,12 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 + // SD #define SD_SCLK 13 #define SD_MOSI 12 diff --git a/MCUME_teensy/teensynofrendo/platform_config.h b/MCUME_teensy/teensynofrendo/platform_config.h index 995b49f..4186a21 100644 --- a/MCUME_teensy/teensynofrendo/platform_config.h +++ b/MCUME_teensy/teensynofrendo/platform_config.h @@ -3,6 +3,12 @@ //#define OLD_LAYOUT 1 //#define TEENSYBOY 1 + //#define HAS_PSRAM 1 +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + #endif diff --git a/MCUME_teensy/teensynofrendo/teensynofrendo.ino b/MCUME_teensy/teensynofrendo/teensynofrendo.ino index 99b995d..6fd6032 100644 --- a/MCUME_teensy/teensynofrendo/teensynofrendo.ino +++ b/MCUME_teensy/teensynofrendo/teensynofrendo.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensynofrendo/tft_t_dma.cpp b/MCUME_teensy/teensynofrendo/tft_t_dma.cpp index e59787f..5013b73 100644 --- a/MCUME_teensy/teensynofrendo/tft_t_dma.cpp +++ b/MCUME_teensy/teensynofrendo/tft_t_dma.cpp @@ -963,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -561,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -583,150 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -801,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -838,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1042,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Oddysey Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " Oddysey Emulator" #define ROMSDIR "o2em" @@ -119,6 +121,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensyo2em/iopins.h b/MCUME_teensy/teensyo2em/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensyo2em/iopins.h +++ b/MCUME_teensy/teensyo2em/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensyo2em/platform_config.h b/MCUME_teensy/teensyo2em/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensyo2em/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensyo2em/teensyo2em.ino b/MCUME_teensy/teensyo2em/teensyo2em.ino index 8ef6c9e..1dfd620 100644 --- a/MCUME_teensy/teensyo2em/teensyo2em.ino +++ b/MCUME_teensy/teensyo2em/teensyo2em.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensyo2em/tft_t_dma.cpp b/MCUME_teensy/teensyo2em/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensyo2em/tft_t_dma.cpp +++ b/MCUME_teensy/teensyo2em/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif #ifdef USE_SDFS -#define SDFSDEV "2:" - #include "uSDFS.h" static FATFS fatfs; static FIL file; #else + +#ifdef USE_SDFAT +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; +#else #include +#endif static File file; #endif -extern TFT_T_DMA tft; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -const uint16_t deflogo[] = { - 0x0000,0x0000 -}; -static const uint16_t * logo = deflogo; - -#ifdef USE_SDFS -#define CALIBRATION_FILE SDFSDEV "/cal.cfg" -#else #define CALIBRATION_FILE "/cal.cfg" -#endif -#define MAX_FILES 128 -#define MAX_FILENAME_SIZE 28 -#define MAX_MENULINES (MKEY_L9) -#define TEXT_HEIGHT 16 -#define TEXT_WIDTH 8 -#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) -#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) -#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) -#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) -#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) -#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) -#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) -#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) - -#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) -//#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -//#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) - #define MKEY_L1 1 #define MKEY_L2 2 @@ -80,6 +84,43 @@ static const uint16_t * logo = deflogo; #define MKEY_TFT 23 #define MKEY_VGA 24 +#define MAX_FILES 32 +#define MAX_FILENAME_SIZE 24 +#define MAX_MENULINES (MKEY_L9) +#define TEXT_HEIGHT 16 +#define TEXT_WIDTH 8 +#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) +#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) +#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) +#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) +#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) +#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) +#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) +#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) + +#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) +#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) + +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; + + const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, TAREA_WH,MENU_FILE_W, TEXT_HEIGHT, @@ -100,73 +141,18 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - -#ifdef USE_SDFS - DIR dir; - FILINFO entry; - f_opendir(&dir, romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - emu_printf(newpath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(newpath, &entry); - if ( (entry.fattrib & AM_DIR) ) { -#else - File file = SD.open(newpath); - if (file.isDirectory()) { -#endif - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - //tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFS - strcpy(romspath,SDFSDEV); - strcat(romspath,ROMSDIR); - FRESULT rc; - if((rc = f_mount (&fatfs, romspath, 1))) { - emu_printf("Fail mounting SDFS"); - } - else { - rc = f_chdrive(ROMSDIR); - } -#else - while (!SD.begin(SD_CS)) { - emu_printf("SdFat.begin() failed"); - delay(1000); - } - strcpy(romspath,ROMSDIR); -#endif - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -669,216 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { -#else - if ((file = SD.open(filepath, O_READ))) { -#endif - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - int retval=0; - while (remaining>0) { -#ifdef USE_SDFS - if( !(f_read (&file, buffer, 256, &retval)) ) - //f_read (&file, buffer, 256, &retval); -#else - retval = file.read(buffer, 256); -#endif - if (retval>0) { - //emu_printi(retval); - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - else { - break; - } - } - - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; -#ifdef USE_SDFS - int retval=0; - if( !(f_read (&file, &c, 1, &retval)) ) -#else - int retval = file.read(&c, 1); -#endif - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ -#ifdef USE_SDFS - f_close(&file); -#else - file.close(); -#endif -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(filepath, &entry); - filesize = entry.fsize; -#else - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } -#endif - - return(filesize); -} - -int emu_FileSeek(int seek) -{ -#ifdef USE_SDFS - f_lseek(&file, seek); -#else - file.seek(seek); -#endif - return (seek); -} - -int emu_FileTell(void) -{ -#ifdef USE_SDFS - return (f_tell(&file)); -#else - return (50); -#endif -} - - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - filesize = f_size(&file); - emu_printf(filesize); - if (size >= filesize) - { - int retval=0; - if( (f_read (&file, buf, filesize, &retval)) ) { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } -#endif - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - f_lseek(&file, seek); - emu_printf(size); - if (size >= filesize) - { - int retval=0; - if( (!f_read (&file, buf, size, &retval)) ) - if (retval != size) - { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } -#endif - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -953,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -988,8 +413,48 @@ int emu_ReadKeys(void) if ( digitalRead(PIN_KEY_USER4) == LOW ) retval |= MASK_KEY_USER4; #endif - //Serial.println(j1,HEX); + //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1194,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " PC Engine Emulator " +// Title: < > +#define TITLE " PC Engine Emulator" #define ROMSDIR "/pce" #define emu_Init(ROM) {pce_Init(); pce_Start(ROM);} @@ -33,6 +33,7 @@ #define ACTION_RUNTFT 129 #define ACTION_RUNVGA 130 + #ifdef KEYMAP_PRESENT #define TAREA_W_DEF 32 @@ -117,6 +118,11 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensypce/iopins.h b/MCUME_teensy/teensypce/iopins.h index f57086c..eead4a9 100644 --- a/MCUME_teensy/teensypce/iopins.h +++ b/MCUME_teensy/teensypce/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -37,10 +36,13 @@ // SD #define SD_SCLK 13 -#define SD_MOSI 11 -#define SD_MISO 12 -//#define SD_CS BUILTIN_SDCARD +#define SD_MOSI 12 +#define SD_MISO 11 +#ifdef TEENSYBOY +#define SD_CS BUILTIN_SDCARD +#else #define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -55,8 +57,17 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 diff --git a/MCUME_teensy/teensypce/platform_config.h b/MCUME_teensy/teensypce/platform_config.h new file mode 100644 index 0000000..89bbe11 --- /dev/null +++ b/MCUME_teensy/teensypce/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensypce/psram_t.cpp b/MCUME_teensy/teensypce/psram_t.cpp index 3a47eb8..c5661bc 100644 --- a/MCUME_teensy/teensypce/psram_t.cpp +++ b/MCUME_teensy/teensypce/psram_t.cpp @@ -4,6 +4,14 @@ #include "psram_t.h" +Page PSRAM_T::pages[MAX_PAGES]; +uint8_t PSRAM_T::nbPages=0; +int8_t PSRAM_T::top=0; +int8_t PSRAM_T::last=0; + + +#ifdef HAS_PSRAM + #include #include @@ -20,10 +28,6 @@ uint8_t PSRAM_T::_cs, PSRAM_T::_miso, PSRAM_T::_mosi, PSRAM_T::_sclk; -Page PSRAM_T::pages[MAX_PAGES]; -uint8_t PSRAM_T::nbPages=0; -int8_t PSRAM_T::top=0; -int8_t PSRAM_T::last=0; PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) @@ -231,6 +235,35 @@ Serial.printf("D") ; Serial.flush(); +#else + + +#include "emuapi.h" + + +PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) +{ +} + +void PSRAM_T::begin(void) +{ + emu_FileTempInit(); +} + +void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) +{ + emu_FileTempRead(addr,val,n); +} + + +void PSRAM_T::psram_write(uint32_t addr, uint8_t val) +{ + emu_FileTempWrite(addr,val); +} + +#endif + + void PSRAM_T::pswrite(uint32_t addr, uint8_t val) { diff --git a/MCUME_teensy/teensypce/psram_t.h b/MCUME_teensy/teensypce/psram_t.h index c6161db..2a8b644 100644 --- a/MCUME_teensy/teensypce/psram_t.h +++ b/MCUME_teensy/teensypce/psram_t.h @@ -7,9 +7,15 @@ #ifdef __cplusplus #include +#include "platform_config.h" -#define PAGE_SIZE 16 //16 //32 //2048 //1024 +#ifdef HAS_PSRAM +#define PAGE_SIZE 16 //32 //2048 //1024 #define MAX_PAGES 8 +#else +#define PAGE_SIZE 256 +#define MAX_PAGES 8 +#endif struct Page { uint8_t page[PAGE_SIZE]; diff --git a/MCUME_teensy/teensypce/teensypce.ino b/MCUME_teensy/teensypce/teensypce.ino index d9a97c7..cc3fb22 100644 --- a/MCUME_teensy/teensypce/teensypce.ino +++ b/MCUME_teensy/teensypce/teensypce.ino @@ -44,6 +44,8 @@ volatile boolean vbl=true; static int skip=0; static elapsedMicros tius; + + static void vblCount() { if (vbl) { vbl = false; @@ -158,14 +160,17 @@ void * emu_LineBuffer(int line) #endif } + // **************************************************** // the setup() method runs once, when the sketch starts // **************************************************** void setup() { + tft.begin(); emu_init(); + myTimer.begin(vblCount, 20000); //to run every 20ms } @@ -174,19 +179,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensypce/tft_t_dma.cpp b/MCUME_teensy/teensypce/tft_t_dma.cpp index 33c4d32..5013b73 100644 --- a/MCUME_teensy/teensypce/tft_t_dma.cpp +++ b/MCUME_teensy/teensypce/tft_t_dma.cpp @@ -5,8 +5,8 @@ #include "TFT_T_DMA.h" #include "font8x8.h" -//#define SPICLOCK 144e6 //Just a number..max speed -#define SPICLOCK 60000000 + +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -33,7 +33,7 @@ static uint16_t fb0[LINES_PER_BLOCK*TFT_WIDTH]; static uint16_t fb1[LINES_PER_BLOCK*TFT_WIDTH]; static uint16_t fb2[LINES_PER_BLOCK*TFT_WIDTH]; -static uint16_t fb3[LINES_PER_BLOCK*TFT_WIDTH]; +static uint16_t fb3[(TFT_HEIGHT-3*LINES_PER_BLOCK)*TFT_WIDTH]; static uint16_t * blocks[NR_OF_BLOCK]={fb0,fb1,fb2,fb3}; #else static uint16_t * blocks[NR_OF_BLOCK]; @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -861,7 +882,7 @@ void TFT_T_DMA::writeScreen(int width, int height, int stride, uint8_t *buf, uin src=buffer; for (i=0; i> 8]&PAL_COLOR_MASK]; + *dst++=palette16[buf[pos >> 8]]; pos +=step; } #endif @@ -945,8 +966,8 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * else if ((width*2) == TFT_WIDTH) { for (int i=0; i #endif #ifdef USE_SDFS -#define SDFSDEV "2:" - #include "uSDFS.h" static FATFS fatfs; static FIL file; #else + +#ifdef USE_SDFAT +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; +#else #include +#endif static File file; #endif -extern TFT_T_DMA tft; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -const uint16_t deflogo[] = { - 0x0000,0x0000 -}; -static const uint16_t * logo = deflogo; - -#ifdef USE_SDFS -#define CALIBRATION_FILE SDFSDEV "/cal.cfg" -#else #define CALIBRATION_FILE "/cal.cfg" -#endif -#define MAX_FILES 128 -#define MAX_FILENAME_SIZE 28 -#define MAX_MENULINES (MKEY_L9) -#define TEXT_HEIGHT 16 -#define TEXT_WIDTH 8 -#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) -#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) -#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) -#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) -#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) -#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) -#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) -#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) - -#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) -//#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) -//#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) - #define MKEY_L1 1 #define MKEY_L2 2 @@ -80,6 +84,43 @@ static const uint16_t * logo = deflogo; #define MKEY_TFT 23 #define MKEY_VGA 24 +#define MAX_FILES 32 +#define MAX_FILENAME_SIZE 24 +#define MAX_MENULINES (MKEY_L9) +#define TEXT_HEIGHT 16 +#define TEXT_WIDTH 8 +#define MENU_FILE_XOFFSET (6*TEXT_WIDTH) +#define MENU_FILE_YOFFSET (2*TEXT_HEIGHT) +#define MENU_FILE_W (MAX_FILENAME_SIZE*TEXT_WIDTH) +#define MENU_FILE_H (MAX_MENULINES*TEXT_HEIGHT) +#define MENU_FILE_BGCOLOR RGBVAL16(0x00,0x00,0x20) +#define MENU_JOYS_YOFFSET (12*TEXT_HEIGHT) +#define MENU_VBAR_XOFFSET (0*TEXT_WIDTH) +#define MENU_VBAR_YOFFSET (MENU_FILE_YOFFSET) + +#define MENU_TFT_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_TFT_YOFFSET (MENU_VBAR_YOFFSET+32) +#define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) +#define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) + +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; + + const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, TAREA_WH,MENU_FILE_W, TEXT_HEIGHT, @@ -100,73 +141,18 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - -#ifdef USE_SDFS - DIR dir; - FILINFO entry; - f_opendir(&dir, romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - emu_printf(newpath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(newpath, &entry); - if ( (entry.fattrib & AM_DIR) ) { -#else - File file = SD.open(newpath); - if (file.isDirectory()) { -#endif - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - //tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFS - strcpy(romspath,SDFSDEV); - strcat(romspath,ROMSDIR); - FRESULT rc; - if((rc = f_mount (&fatfs, romspath, 1))) { - emu_printf("Fail mounting SDFS"); - } - else { - rc = f_chdrive(ROMSDIR); - } -#else - while (!SD.begin(SD_CS)) { - emu_printf("SdFat.begin() failed"); - delay(1000); - } - strcpy(romspath,ROMSDIR); -#endif - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -669,216 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { -#else - if ((file = SD.open(filepath, O_READ))) { -#endif - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - int retval=0; - while (remaining>0) { -#ifdef USE_SDFS - if( !(f_read (&file, buffer, 256, &retval)) ) - //f_read (&file, buffer, 256, &retval); -#else - retval = file.read(buffer, 256); -#endif - if (retval>0) { - //emu_printi(retval); - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - else { - break; - } - } - - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; -#ifdef USE_SDFS - int retval=0; - if( !(f_read (&file, &c, 1, &retval)) ) -#else - int retval = file.read(&c, 1); -#endif - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ -#ifdef USE_SDFS - f_close(&file); -#else - file.close(); -#endif -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); -#ifdef USE_SDFS - FILINFO entry; - f_stat(filepath, &entry); - filesize = entry.fsize; -#else - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } -#endif - - return(filesize); -} - -int emu_FileSeek(int seek) -{ -#ifdef USE_SDFS - f_lseek(&file, seek); -#else - file.seek(seek); -#endif - return (seek); -} - -int emu_FileTell(void) -{ -#ifdef USE_SDFS - return (f_tell(&file)); -#else - return (50); -#endif -} - - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - filesize = f_size(&file); - emu_printf(filesize); - if (size >= filesize) - { - int retval=0; - if( (f_read (&file, buf, filesize, &retval)) ) { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } -#endif - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); -#ifdef USE_SDFS - if( !(f_open(&file, filepath, FA_READ)) ) { - f_lseek(&file, seek); - emu_printf(size); - if (size >= filesize) - { - int retval=0; - if( (!f_read (&file, buf, size, &retval)) ) - if (retval != size) - { - emu_printf("File read failed"); - } - } - f_close(&file); - } -#else - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } -#endif - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -953,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -988,8 +413,48 @@ int emu_ReadKeys(void) if ( digitalRead(PIN_KEY_USER4) == LOW ) retval |= MASK_KEY_USER4; #endif - //Serial.println(j1,HEX); + //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1194,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i #include @@ -20,10 +28,6 @@ uint8_t PSRAM_T::_cs, PSRAM_T::_miso, PSRAM_T::_mosi, PSRAM_T::_sclk; -Page PSRAM_T::pages[MAX_PAGES]; -uint8_t PSRAM_T::nbPages=0; -int8_t PSRAM_T::top=0; -int8_t PSRAM_T::last=0; PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) @@ -231,6 +235,35 @@ Serial.printf("D") ; Serial.flush(); +#else + + +#include "emuapi.h" + + +PSRAM_T::PSRAM_T(uint8_t cs, uint8_t mosi, uint8_t sclk, uint8_t miso) +{ +} + +void PSRAM_T::begin(void) +{ + emu_FileTempInit(); +} + +void PSRAM_T::psram_read_n(uint32_t addr, uint8_t * val, int n) +{ + emu_FileTempRead(addr,val,n); +} + + +void PSRAM_T::psram_write(uint32_t addr, uint8_t val) +{ + emu_FileTempWrite(addr,val); +} + +#endif + + void PSRAM_T::pswrite(uint32_t addr, uint8_t val) { diff --git a/MCUME_teensy/teensysms/psram_t.h b/MCUME_teensy/teensysms/psram_t.h index c6161db..2a8b644 100644 --- a/MCUME_teensy/teensysms/psram_t.h +++ b/MCUME_teensy/teensysms/psram_t.h @@ -7,9 +7,15 @@ #ifdef __cplusplus #include +#include "platform_config.h" -#define PAGE_SIZE 16 //16 //32 //2048 //1024 +#ifdef HAS_PSRAM +#define PAGE_SIZE 16 //32 //2048 //1024 #define MAX_PAGES 8 +#else +#define PAGE_SIZE 256 +#define MAX_PAGES 8 +#endif struct Page { uint8_t page[PAGE_SIZE]; diff --git a/MCUME_teensy/teensysms/render.c b/MCUME_teensy/teensysms/render.c index 7cd6cea..3a2173e 100644 --- a/MCUME_teensy/teensysms/render.c +++ b/MCUME_teensy/teensysms/render.c @@ -170,7 +170,8 @@ void render_line(int line) memset(linebuf, BACKDROP_COLOR, 8); } } - + for (int i=0; i> 8]&PAL_COLOR_MASK]; + *dst++=palette16[buf[pos >> 8]]; pos +=step; } #endif @@ -945,8 +966,8 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * else if ((width*2) == TFT_WIDTH) { for (int i=0; i>3)&0xfc) #define B16(rgb) ((rgb<<3)&0xf8) -#define PAL_COLOR_MASK 0x1f #define TFT_HEIGHT 192 diff --git a/MCUME_teensy/teensysms/tft_t_dma_config.h b/MCUME_teensy/teensysms/tft_t_dma_config.h index 3006874..7ee0925 100644 --- a/MCUME_teensy/teensysms/tft_t_dma_config.h +++ b/MCUME_teensy/teensysms/tft_t_dma_config.h @@ -1,5 +1,12 @@ -//#define ST7789 1 +#include "platform_config.h" + +#ifdef TEENSYBOY +#define ST7789 1 +#define ROTATE_SCREEN 1 +#else #define ILI9341 1 +#endif + #define TFT_LINEARINT 1 #define LINEARINT_HACK 1 diff --git a/MCUME_teensy/teensyspeccy/.DS_Store b/MCUME_teensy/teensyspeccy/.DS_Store deleted file mode 100644 index f7a8f99..0000000 Binary files a/MCUME_teensy/teensyspeccy/.DS_Store and /dev/null differ diff --git a/MCUME_teensy/teensyspeccy/emuapi.cpp b/MCUME_teensy/teensyspeccy/emuapi.cpp index 1b702d8..576f2d5 100644 --- a/MCUME_teensy/teensyspeccy/emuapi.cpp +++ b/MCUME_teensy/teensyspeccy/emuapi.cpp @@ -9,38 +9,83 @@ extern "C" { #include "logo.h" #include "bmpjoy.h" #include "bmpvbar.h" +#ifdef OLD_LAYOUT #include "bmpvga.h" #include "bmptft.h" - -#ifndef SD_CS -#define USE_SDFAT 1 #endif + #ifdef HAS_I2CKBD #include #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} - -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} - void emu_printf(char * text) { @@ -561,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -583,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -802,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -839,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1043,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " SPECTRUM Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " SPECTRUM Emulator" #define ROMSDIR "spec" #define emu_Init(ROM) {spec_Init(); spec_Start(ROM);} @@ -111,6 +113,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensyspeccy/iopins.h b/MCUME_teensy/teensyspeccy/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensyspeccy/iopins.h +++ b/MCUME_teensy/teensyspeccy/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensyspeccy/platform_config.h b/MCUME_teensy/teensyspeccy/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensyspeccy/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensyspeccy/teensyspeccy.ino b/MCUME_teensy/teensyspeccy/teensyspeccy.ino index 132973b..3811b4c 100644 --- a/MCUME_teensy/teensyspeccy/teensyspeccy.ino +++ b/MCUME_teensy/teensyspeccy/teensyspeccy.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensyspeccy/tft_t_dma.cpp b/MCUME_teensy/teensyspeccy/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensyspeccy/tft_t_dma.cpp +++ b/MCUME_teensy/teensyspeccy/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} void emu_printf(char * text) @@ -562,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -584,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -803,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -840,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1044,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Atari2600 Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " Atari2600 Emulator" #define ROMSDIR "2600" #define emu_Init(ROM) {vcs_Init();vcs_Start(ROM);} @@ -85,6 +87,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensyvcs/iopins.h b/MCUME_teensy/teensyvcs/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensyvcs/iopins.h +++ b/MCUME_teensy/teensyvcs/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensyvcs/platform_config.h b/MCUME_teensy/teensyvcs/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensyvcs/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensyvcs/teensyvcs.ino b/MCUME_teensy/teensyvcs/teensyvcs.ino index c58a208..ee1fefc 100644 --- a/MCUME_teensy/teensyvcs/teensyvcs.ino +++ b/MCUME_teensy/teensyvcs/teensyvcs.ino @@ -166,19 +166,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensyvcs/tft_t_dma.cpp b/MCUME_teensy/teensyvcs/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensyvcs/tft_t_dma.cpp +++ b/MCUME_teensy/teensyvcs/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i #endif +#ifdef USE_SDFS +#include "uSDFS.h" +static FATFS fatfs; +static FIL file; +#else + #ifdef USE_SDFAT -#include -static SdFatSdio SD; +#include "SdFat.h" +#include "sdios.h" + +#ifndef SDCARD_SS_PIN +const uint8_t SD_CS_PIN = SS; + +#else // SDCARD_SS_PIN +// Assume built-in SD is used. +const uint8_t SD_CS_PIN = SDCARD_SS_PIN; +#endif // SDCARD_SS_PIN + +#define SD_CONFIG SdioConfig(FIFO_SDIO) + +// Try to select the best SD card configuration. +#if HAS_SDIO_CLASS +#define SD_CONFIG SdioConfig(FIFO_SDIO) +#elif ENABLE_DEDICATED_SPI +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI) +#else // HAS_SDIO_CLASS +#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI) +#endif // HAS_SDIO_CLASS +//------------------------------------------------------------------------------ + +#if SD_FAT_TYPE == 0 +#elif SD_FAT_TYPE == 1 +#define SdFat SdFat32 +#define File File32 +#elif SD_FAT_TYPE == 2 +#define SdFat SdExFat +#define File ExFile +#elif SD_FAT_TYPE == 3 +#define SdFat SdFs +#define File FsFile +#endif // SD_FAT_TYPE +static SdFat SD; #else #include #endif - -extern TFT_T_DMA tft; static File file; -static char romspath[64]; -static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; -static bool i2cKeyboardPresent = false; -//const uint16_t deflogo[] = { -// 0x0000,0x0000 -//}; -//static const uint16_t * logo = deflogo; +#endif #define CALIBRATION_FILE "/cal.cfg" +#define MKEY_L1 1 +#define MKEY_L2 2 +#define MKEY_L3 3 +#define MKEY_L4 4 +#define MKEY_L5 5 +#define MKEY_L6 6 +#define MKEY_L7 7 +#define MKEY_L8 8 +#define MKEY_L9 9 +#define MKEY_UP 20 +#define MKEY_DOWN 21 +#define MKEY_JOY 22 +#define MKEY_TFT 23 +#define MKEY_VGA 24 + #define MAX_FILES 32 -#define MAX_FILENAME_SIZE 28 +#define MAX_FILENAME_SIZE 24 #define MAX_MENULINES (MKEY_L9) #define TEXT_HEIGHT 16 #define TEXT_WIDTH 8 @@ -58,21 +103,23 @@ static bool i2cKeyboardPresent = false; #define MENU_VGA_XOFFSET (MENU_FILE_XOFFSET+MENU_FILE_W+8) #define MENU_VGA_YOFFSET (MENU_VBAR_YOFFSET+MENU_FILE_H-32-37) +extern TFT_T_DMA tft; + +static char romspath[64]; +static int nbFiles=0; +static int curFile=0; +static int topFile=0; +static char selection[MAX_FILENAME_SIZE+1]=""; +static char files[MAX_FILES][MAX_FILENAME_SIZE]; +static bool menuRedraw=true; + +static int16_t calMinX=-1,calMinY=-1,calMaxX=-1,calMaxY=-1; +static bool i2cKeyboardPresent = false; +//const uint16_t deflogo[] = { +// 0x0000,0x0000 +//}; +//static const uint16_t * logo = deflogo; -#define MKEY_L1 1 -#define MKEY_L2 2 -#define MKEY_L3 3 -#define MKEY_L4 4 -#define MKEY_L5 5 -#define MKEY_L6 6 -#define MKEY_L7 7 -#define MKEY_L8 8 -#define MKEY_L9 9 -#define MKEY_UP 20 -#define MKEY_DOWN 21 -#define MKEY_JOY 22 -#define MKEY_TFT 23 -#define MKEY_VGA 24 const unsigned short menutouchareas[] = { TAREA_XY,MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, @@ -94,53 +141,19 @@ const unsigned short menutouchactions[] = { MKEY_UP,MKEY_DOWN,ACTION_NONE,MKEY_JOY, MKEY_TFT,MKEY_VGA}; - + +static int keypadval=0; +static boolean joySwapped = false; +static uint16_t bLastState; +static int xRef; +static int yRef; +static uint8_t prev_zt=0; + static bool menuOn=true; static bool callibrationOn=false; static int callibrationStep=0; -static bool menuRedraw=true; -static int nbFiles=0; -static int curFile=0; -static int topFile=0; -static char selection[MAX_FILENAME_SIZE+1]=""; -static uint8_t prev_zt=0; -static char files[MAX_FILES][MAX_FILENAME_SIZE]; - -static int readNbFiles(void) { - int totalFiles = 0; - File entry; - file = SD.open(romspath); - while ( (true) && (totalFiles (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) - && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { - calMinX /= 2; - calMinY /= 2; - calMaxX /= 2; - calMaxY /= 2; - writeCallibration(); - toggleMenu(true); - } - else { - callibrationInit(); - } - break; - - } - delay(100); - } - } - else { - prev_zt = 0; - } -} -bool menuActive(void) -{ - return (menuOn); -} -int handleMenu(uint16_t bClick) -{ - int action = ACTION_NONE; - - char newpath[80]; - strcpy(newpath, romspath); - strcat(newpath, "/"); - strcat(newpath, selection); - - int rx=0,ry=0,rw=0,rh=0; - char c = captureTouchZone(menutouchareas, menutouchactions, &rx,&ry,&rw,&rh); - - if ( (bClick & MASK_JOY2_BTN) || (c == MKEY_TFT) ) { - File file = SD.open(newpath); - emu_printf(newpath); - if (file.isDirectory()) { - strcpy(romspath,newpath); - curFile = 0; - nbFiles = readNbFiles(); - } - else { - action = ACTION_RUNTFT; - } - menuRedraw=true; - } - else if ( (bClick & MASK_KEY_USER1) || (c == MKEY_VGA) ) { - menuRedraw=true; - action = ACTION_RUNVGA; - } - else if ( (c >= MKEY_L1) && (c <= MKEY_L9) ) { - if ( (topFile+(int)c-1) <= (nbFiles-1) ) - { - curFile = topFile + (int)c -1; - menuRedraw=true; - //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); - } - } - else if (bClick & MASK_JOY2_UP) { - if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { - if ((curFile-9)>=0) { - menuRedraw=true; - curFile -= 9; - } else if (curFile!=0) { - menuRedraw=true; - curFile--; - } - } - else if (bClick & MASK_JOY2_DOWN) { - if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { - if ((curFile<(nbFiles-9)) && (nbFiles)) { - curFile += 9; - menuRedraw=true; - } - else if ((curFile<(nbFiles-1)) && (nbFiles)) { - curFile++; - menuRedraw=true; - } - } - else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { - emu_SwapJoysticks(0); - menuRedraw=true; - } - - if (menuRedraw && nbFiles) { - int fileIndex = 0; - tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); -// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; -// else topFile=curFile-(MAX_MENULINES/2); - if (curFile <= (MAX_MENULINES-1)) topFile=0; - else topFile=curFile-(MAX_MENULINES/2); - - //Serial.print("curfile: "); - //Serial.println(curFile); - //Serial.print("topFile: "); - //Serial.println(topFile); - - int i=0; - while (i=nbFiles) { - // no more files - break; - } - char * filename = &files[fileIndex][0]; - if (fileIndex >= topFile) { - if ((i+topFile) < nbFiles ) { - if ((i+topFile)==curFile) { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); - strcpy(selection,filename); - } - else { - tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); - } - } - i++; - } - fileIndex++; - } - - tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); - tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); - menuRedraw=false; - } - - return (action); -} - -char * menuSelection(void) -{ - return (selection); -} - - - - -void emu_init(void) -{ - Serial.begin(115200); - //while (!Serial) {} -#ifdef USE_SDFAT - if (!SD.begin()) { -#else - if (!SD.begin(SD_CS)) { -#endif - emu_printf("SdFat.begin() failed"); - } - strcpy(romspath,ROMSDIR); - nbFiles = readNbFiles(); - - Serial.print("SD initialized, files found: "); - Serial.println(nbFiles); - - emu_InitJoysticks(); - readCallibration(); - - if ((tft.isTouching()) || (emu_ReadKeys() & MASK_JOY2_BTN) ) { - callibrationInit(); - } - else - { - toggleMenu(true); - } - -#ifdef HAS_I2CKBD - byte msg[7]={0,0,0,0,0,0,0}; - Wire.begin(); // join i2c bus SDA2/SCL2 - Wire.requestFrom(8, 7); // request 7 bytes from slave device #8 - int i = 0; - while (Wire.available() && (i<7) ) { // slave may send less than requested - byte b = Wire.read(); // receive a byte - msg[i++] = b; - } - /* - Serial.println(msg[0], BIN); - Serial.println(msg[1], BIN); - Serial.println(msg[2], BIN); - Serial.println(msg[3], BIN); - Serial.println(msg[4], BIN); - Serial.println(msg[5], BIN); - Serial.println(msg[6], BIN); - */ - if ( (msg[0] == 0xff) && (msg[1] == 0xff) && - (msg[2] == 0xff) && (msg[3] == 0xff) && - (msg[4] == 0xff) && (msg[5] == 0xff) && (msg[6] == 0xff)) { - i2cKeyboardPresent = true; - Serial.println("i2C keyboard found"); - } -#endif -} void emu_printf(char * text) @@ -562,12 +248,28 @@ void emu_printi(int val) Serial.println(val); } +void emu_printh(int val) +{ + Serial.println(val,HEX); +} + +static int malbufpt = 0; +static char malbuf[EXTRA_HEAP]; + void * emu_Malloc(int size) { void * retval = malloc(size); if (!retval) { - emu_printf("failled to allocate "); + emu_printf("failled to allocate"); emu_printf(size); + emu_printf("fallback"); + if ( (malbufpt+size) < sizeof(malbuf) ) { + retval = (void *)&malbuf[malbufpt]; + malbufpt += size; + } + else { + emu_printf("failure to allocate"); + } } else { emu_printf("could allocate "); @@ -584,151 +286,6 @@ void emu_Free(void * pt) -int emu_FileOpen(char * filename) -{ - int retval = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileOpen..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) { - retval = 1; - } - else { - emu_printf("FileOpen failed"); - } - return (retval); -} - -int emu_FileRead(char * buf, int size) -{ - unsigned char buffer[256]; - - int remaining = size; - int byteread = 0; - while (remaining >= 256) { - int retval = file.read(buffer, 256); - if (retval>0) { - memcpy(buf,buffer,retval); - buf += retval; - byteread += retval; - remaining -= retval; - } - } - if (remaining) { - int retval = file.read(buffer, remaining); - if (retval>0) { - memcpy(buf,buffer,retval); - byteread += retval; - } - } - return byteread; -} - -unsigned char emu_FileGetc(void) { - unsigned char c; - int retval = file.read(&c, 1); - if (retval != 1) { - emu_printf("emu_FileGetc failed"); - } - return c; -} - - -void emu_FileClose(void) -{ - file.close(); -} - -int emu_FileSize(char * filename) -{ - int filesize=0; - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("FileSize..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - emu_printf("filesize is..."); - filesize = file.size(); - emu_printf(filesize); - file.close(); - } - - return(filesize); -} - -int emu_FileSeek(int seek) -{ - file.seek(seek); - return (seek); -} - -int emu_LoadFile(char * filename, char * buf, int size) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFile..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - filesize = file.size(); - emu_printf(filesize); - - if (size >= filesize) - { - if (emu_FileRead(buf, filesize) != filesize) - { - emu_printf("File read failed"); - } - } - file.close(); - } - - return(filesize); -} - -int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) -{ - int filesize = 0; - - char filepath[80]; - strcpy(filepath, romspath); - strcat(filepath, "/"); - strcat(filepath, filename); - emu_printf("LoadFileSeek..."); - emu_printf(filepath); - - if ((file = SD.open(filepath, O_READ))) - { - file.seek(seek); - emu_printf(size); - if (file.read(buf, size) != size) { - emu_printf("File read failed"); - } - file.close(); - } - - return(filesize); -} - -static int keypadval=0; -static boolean joySwapped = false; -static uint16_t bLastState; -static int xRef; -static int yRef; int emu_ReadAnalogJoyX(int min, int max) @@ -803,18 +360,36 @@ int emu_ReadKeys(void) uint16_t j2 = 0; // Second joystick +#if INVY +#ifdef PIN_JOY1_1 + if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_DOWN; +#endif +#ifdef PIN_JOY1_2 + if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_UP; +#endif +#else #ifdef PIN_JOY1_1 if ( digitalRead(PIN_JOY1_1) == LOW ) j2 |= MASK_JOY2_UP; #endif #ifdef PIN_JOY1_2 if ( digitalRead(PIN_JOY1_2) == LOW ) j2 |= MASK_JOY2_DOWN; #endif +#endif +#if INVX +#ifdef PIN_JOY1_3 + if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_LEFT; +#endif +#ifdef PIN_JOY1_4 + if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_RIGHT; +#endif +#else #ifdef PIN_JOY1_3 if ( digitalRead(PIN_JOY1_3) == LOW ) j2 |= MASK_JOY2_RIGHT; #endif #ifdef PIN_JOY1_4 if ( digitalRead(PIN_JOY1_4) == LOW ) j2 |= MASK_JOY2_LEFT; #endif +#endif #ifdef PIN_JOY1_BTN if ( digitalRead(PIN_JOY1_BTN) == LOW ) j2 |= MASK_JOY2_BTN; #endif @@ -840,6 +415,46 @@ int emu_ReadKeys(void) //Serial.println(retval,HEX); + if ( ((retval & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) + || (retval & MASK_KEY_USER4 ) ) + { +// Reset procedure T3.X and T4.0 by Frank Boesing !! +#if defined(__IMXRT1052__) || defined(__IMXRT1062__) + uint32_t tmp = SNVS_LPCR; // save control register + + SNVS_LPSR |= 1; + + // disable alarm + SNVS_LPCR &= ~0x02; + while (SNVS_LPCR & 0x02); + + __disable_irq(); + //get Time: + uint32_t lsb, msb; + do { + msb = SNVS_LPSRTCMR; + lsb = SNVS_LPSRTCLR; + } while ( (SNVS_LPSRTCLR != lsb) | (SNVS_LPSRTCMR != msb) ); + uint32_t secs = (msb << 17) | (lsb >> 15); + + //set alarm + secs += 2; + SNVS_LPTAR = secs; + while (SNVS_LPTAR != secs); + + SNVS_LPCR = tmp | 0x02; // restore control register and set alarm + while (!(SNVS_LPCR & 0x02)); + + SNVS_LPCR |= (1 << 6); // turn off power + while (1) asm("wfi"); +#else + *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; + while (true) { + ; + } +#endif + } + return (retval); } @@ -1044,3 +659,759 @@ int emu_setKeymap(int index) { + +static int readNbFiles(void) { + int totalFiles = 0; + +#ifdef USE_SDFS + DIR dir; + FILINFO entry; + f_opendir(&dir, romspath); + while ( (true) && (totalFiles= MKEY_L1) && (c <= MKEY_L9) ) { + if ( (topFile+(int)c-1) <= (nbFiles-1) ) + { + curFile = topFile + (int)c -1; + menuRedraw=true; + //tft.drawRectNoDma( rx,ry,rw,rh, KEYBOARD_HIT_COLOR ); + } + } + else if (bClick & MASK_JOY2_UP) { + if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if ( (bClick & MASK_JOY2_RIGHT) || (bClick & MASK_JOY1_RIGHT) || (c == MKEY_UP) ) { + if ((curFile-9)>=0) { + menuRedraw=true; + curFile -= 9; + } else if (curFile!=0) { + menuRedraw=true; + curFile--; + } + } + else if (bClick & MASK_JOY2_DOWN) { + if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_JOY2_LEFT) || (bClick & MASK_JOY1_LEFT) || (c == MKEY_DOWN) ) { + if ((curFile<(nbFiles-9)) && (nbFiles)) { + curFile += 9; + menuRedraw=true; + } + else if ((curFile<(nbFiles-1)) && (nbFiles)) { + curFile++; + menuRedraw=true; + } + } + else if ( (bClick & MASK_KEY_USER2) || (c == MKEY_JOY) ) { + emu_SwapJoysticks(0); + menuRedraw=true; + } + + if (menuRedraw && nbFiles) { + int fileIndex = 0; + tft.drawRectNoDma(MENU_FILE_XOFFSET,MENU_FILE_YOFFSET, MENU_FILE_W, MENU_FILE_H, MENU_FILE_BGCOLOR); +// if (curFile <= (MAX_MENULINES/2-1)) topFile=0; +// else topFile=curFile-(MAX_MENULINES/2); + if (curFile <= (MAX_MENULINES-1)) topFile=0; + else topFile=curFile-(MAX_MENULINES/2); + + //Serial.print("curfile: "); + //Serial.println(curFile); + //Serial.print("topFile: "); + //Serial.println(topFile); + + int i=0; + while (i=nbFiles) { + // no more files + break; + } + char * filename = &files[fileIndex][0]; + if (fileIndex >= topFile) { + if ((i+topFile) < nbFiles ) { + if ((i+topFile)==curFile) { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, RGBVAL16(0xff,0xff,0x00), RGBVAL16(0xff,0x00,0x00), true); + strcpy(selection,filename); + } + else { + tft.drawTextNoDma(MENU_FILE_XOFFSET,i*TEXT_HEIGHT+MENU_FILE_YOFFSET, filename, 0xFFFF, 0x0000, true); + } + } + i++; + } + fileIndex++; + } + + tft.drawSpriteNoDma(0,MENU_JOYS_YOFFSET,(uint16_t*)bmpjoy); + tft.drawTextNoDma(48,MENU_JOYS_YOFFSET+8, (emu_SwapJoysticks(1)?(char*)"SWAP=1":(char*)"SWAP=0"), RGBVAL16(0x00,0xff,0xff), RGBVAL16(0xff,0x00,0x00), false); + menuRedraw=false; + } + + return (action); +} + +bool menuActive(void) +{ + return (menuOn); +} + +void toggleMenu(bool on) { + if (on) { + callibrationOn=false; + menuOn=true; + backgroundMenu(); + } else { + menuOn = false; + } +} + +char * menuSelection(void) +{ + return (selection); +} + + + + +static void callibrationInit(void) +{ + callibrationOn=true; + menuOn=false; + callibrationStep = 0; + calMinX=0,calMinY=0,calMaxX=0,calMaxY=0; + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration process:", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " Hit the red cross at each corner", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + prev_zt = 1; +} + + +static void readCallibration(void) +{ + char fileBuffer[64]; + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_READ)) ) { + if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + if (retval == 64) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + } + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_READ); + if (file) { + if ( file.read(fileBuffer, 64) ) { + sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + } + file.close(); + Serial.println("Current callibration params:"); + Serial.println(calMinX); + Serial.println(calMinY); + Serial.println(calMaxX); + Serial.println(calMaxY); + } +#endif + else { + Serial.println("Callibration read error"); + } + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); +} + +static void writeCallibration(void) +{ + tft.callibrateTouch(calMinX,calMinY,calMaxX,calMaxY); + +#ifdef USE_SDFS + FIL file; + int retval; + if( !(f_open(&file, CALIBRATION_FILE, FA_WRITE)) ) { + //if( !(f_read (&file, fileBuffer, 64, &retval)) ) { + // if (retval == 64) { + // sscanf(fileBuffer,"%d %d %d %d", &calMinX,&calMinY,&calMaxX,&calMaxY); + // } + //} + f_close(&file); + } +#else + File file = SD.open(CALIBRATION_FILE, O_WRITE | O_CREAT | O_TRUNC); + if (file) { + file.print(calMinX); + file.print(" "); + file.print(calMinY); + file.print(" "); + file.print(calMaxX); + file.print(" "); + file.println(calMaxY); + file.close(); + } +#endif + else { + Serial.println("Callibration write error"); + } +} + + +bool callibrationActive(void) +{ + return (callibrationOn); +} + + + +int handleCallibration(uint16_t bClick) { + uint16_t xt=0; + uint16_t yt=0; + uint16_t zt=0; + if (tft.isTouching()) { + if (prev_zt == 0) { + prev_zt = 1; + tft.readRaw(&xt,&yt,&zt); + if (zt < 1000) { + return 0; + } + switch (callibrationStep) + { + case 0: + callibrationStep++; + tft.drawTextNoDma(0,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,0, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMinX += xt; + calMinY += yt; + break; + case 1: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,0, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMinY += yt; + break; + case 2: + callibrationStep++; + tft.drawTextNoDma(TFT_REALWIDTH-8,TFT_REALHEIGHT-16, " ", RGBVAL16(0xff,0xff,0xff), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,TFT_REALHEIGHT-16, "+", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + calMaxX += xt; + calMaxY += yt; + break; + case 3: + tft.fillScreenNoDma(RGBVAL16(0xff,0xff,0xff)); + tft.drawTextNoDma(0,100, " Callibration done!", RGBVAL16(0x00,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + tft.drawTextNoDma(0,116, " (Click center to exit)", RGBVAL16(0xff,0x00,0x00), RGBVAL16(0xff,0xff,0xff), true); + callibrationStep++; + calMinX += xt; + calMaxY += yt; + break; + case 4: + //Serial.println(xt); + //Serial.println(yt); + if ( (xt > (TFT_REALWIDTH/4)) && (xt < (TFT_REALWIDTH*3)/4) + && (yt > (TFT_REALHEIGHT/4)) && (yt < (TFT_REALHEIGHT*3)/4) ) { + calMinX /= 2; + calMinY /= 2; + calMaxX /= 2; + calMaxY /= 2; + writeCallibration(); + toggleMenu(true); + } + else { + callibrationInit(); + } + break; + + } + delay(100); + } + } + else { + prev_zt = 0; + } +} + + + + + +int emu_FileOpen(char * filename) +{ + int retval = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileOpen..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { +#else + if ((file = SD.open(filepath, O_READ))) { +#endif + retval = 1; + } + else { + emu_printf("FileOpen failed"); + } + return (retval); +} + +int emu_FileRead(char * buf, int size) +{ + unsigned char buffer[256]; + + int remaining = size; + int byteread = 0; + int retval=0; + if (size < 256) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, size, &retval)) ) +#else + retval = file.read(buffer, size); +#endif + if (retval>0) { + memcpy(buf,buffer,retval); + byteread += retval; + } + } + else { + while (remaining>0) { +#ifdef USE_SDFS + if( !(f_read (&file, buffer, 256, &retval)) ) + //f_read (&file, buffer, 256, &retval); +#else + retval = file.read(buffer, 256); +#endif + if (retval>0) { + //emu_printi(retval); + memcpy(buf,buffer,retval); + buf += retval; + byteread += retval; + remaining -= retval; + } + else { + break; + } + } + } + + return byteread; +} + +unsigned char emu_FileGetc(void) { + unsigned char c; +#ifdef USE_SDFS + int retval=0; + if( !(f_read (&file, &c, 1, &retval)) ) +#else + int retval = file.read(&c, 1); +#endif + if (retval != 1) { + emu_printf("emu_FileGetc failed"); + } + return c; +} + + +void emu_FileClose(void) +{ +#ifdef USE_SDFS + f_close(&file); +#else + file.close(); +#endif +} + +int emu_FileSize(char * filename) +{ + int filesize=0; + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("FileSize..."); + emu_printf(filepath); +#ifdef USE_SDFS + FILINFO entry; + f_stat(filepath, &entry); + filesize = entry.fsize; +#else + if ((file = SD.open(filepath, O_READ))) + { + emu_printf("filesize is..."); + filesize = file.size(); + emu_printf(filesize); + file.close(); + } +#endif + + return(filesize); +} + +int emu_FileSeek(int seek) +{ +#ifdef USE_SDFS + f_lseek(&file, seek); +#else + file.seek(seek); +#endif + return (seek); +} + +int emu_FileTell(void) +{ +#ifdef USE_SDFS + return (f_tell(&file)); +#else + return (50); +#endif +} + + +int emu_LoadFile(char * filename, char * buf, int size) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFile..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + filesize = f_size(&file); + emu_printf(filesize); + if (size >= filesize) + { + int retval=0; + if( (f_read (&file, buf, filesize, &retval)) ) { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + filesize = file.size(); + emu_printf(filesize); + + if (size >= filesize) + { + if (emu_FileRead(buf, filesize) != filesize) + { + emu_printf("File read failed"); + } + } + file.close(); + } +#endif + + return(filesize); +} + +int emu_LoadFileSeek(char * filename, char * buf, int size, int seek) +{ + int filesize = 0; + + char filepath[80]; + strcpy(filepath, romspath); + strcat(filepath, "/"); + strcat(filepath, filename); + emu_printf("LoadFileSeek..."); + emu_printf(filepath); +#ifdef USE_SDFS + if( !(f_open(&file, filepath, FA_READ)) ) { + f_lseek(&file, seek); + emu_printf(size); + if (size >= filesize) + { + int retval=0; + if( (!f_read (&file, buf, size, &retval)) ) + if (retval != size) + { + emu_printf("File read failed"); + } + } + f_close(&file); + } +#else + if ((file = SD.open(filepath, O_READ))) + { + file.seek(seek); + emu_printf(size); + if (file.read(buf, size) != size) { + emu_printf("File read failed"); + } + file.close(); + } +#endif + + return(filesize); +} + +const uint32_t FILE_SIZE_MB = 8; +const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB; +#ifdef USE_SDFS +static FIL tempfile; +#else +static File tempfile; +#endif + +void emu_FileTempInit(void) +{ +#ifdef USE_SDFS + if( (f_open(&tempfile, SDFSDEV "/bench.dat", FA_READ| FA_WRITE )) ) { +#else +#ifdef USE_SDFAT + if (!tempfile.open("/bench.dat", O_RDWR /*| O_CREAT | O_TRUNC*/)) { +#else + if ((tempfile = SD.open("/bench.dat", O_RDWR))) { +#endif +#endif + Serial.println("psram open failed"); + } +/* + else { + uint8_t buf[256]; + Serial.println("psram creating"); + file = SD.open("/bench.dat", O_RDWR | O_CREAT | O_TRUNC); + if (file) { + Serial.println("psram opened"); + file.truncate(0); + + if (!file.preAllocate(FILE_SIZE)) { + Serial.println("allocate failed"); + } + for(int i=0;i -#define TITLE " Vectrex Emulator " +#define EXTRA_HEAP 0x10 + +// Title: < > +#define TITLE " Vectrex Emulator" #define ROMSDIR "/vectrex" #define emu_Init(ROM) {vec_Init(); vec_Start(ROM);} @@ -109,6 +111,10 @@ extern void emu_FileClose(void); extern int emu_FileSize(char * filename); extern int emu_LoadFile(char * filename, char * buf, int size); extern int emu_LoadFileSeek(char * filename, char * buf, int size, int seek); +extern void emu_FileTempInit(void); +extern void emu_FileTempRead(int addr, unsigned char * val, int n); +extern void emu_FileTempWrite(int addr, unsigned char val); + extern void emu_SetPaletteEntry(unsigned char r, unsigned char g, unsigned char b, int index); extern void emu_DrawScreen(unsigned char * VBuf, int width, int height, int stride); extern void emu_DrawLine(unsigned char * VBuf, int width, int height, int line); diff --git a/MCUME_teensy/teensyvectrex/iopins.h b/MCUME_teensy/teensyvectrex/iopins.h index 4bf4905..eead4a9 100644 --- a/MCUME_teensy/teensyvectrex/iopins.h +++ b/MCUME_teensy/teensyvectrex/iopins.h @@ -1,8 +1,7 @@ #ifndef IOPINS_H #define IOPINS_H -//#define OLD_LAYOUT 1 - +#include "platform_config.h" #include "tft_t_dma_config.h" #ifndef OLD_LAYOUT @@ -10,8 +9,8 @@ #ifdef ST7789 // ST7789 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 #define TFT_TOUCH_INT 255 #define TFT_DC 9 @@ -20,8 +19,8 @@ #else // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 255 //38 #define TFT_TOUCH_INT 255 //37 #define TFT_DC 9 @@ -29,12 +28,21 @@ #define TFT_RST 255 #endif +// PSRAM +#define PSRAM_CS 36 +#define PSRAM_MOSI 35 +#define PSRAM_MISO 34 +#define PSRAM_SCLK 37 // SD #define SD_SCLK 13 #define SD_MOSI 12 -#define SD_MISO 11 +#define SD_MISO 11 +#ifdef TEENSYBOY #define SD_CS BUILTIN_SDCARD +#else +#define SD_CS 5 +#endif // I2C keyboard #define I2C_SCL_IO 19 @@ -49,15 +57,24 @@ //#define PIN_KEY_USER3 255 //#define PIN_KEY_USER4 255 +#ifdef TEENSYBOY +// Second joystick +#define PIN_JOY1_BTN 2 +#define PIN_JOY1_1 14 // UP +#define PIN_JOY1_2 7 // DOWN +#define PIN_JOY1_3 6 // RIGHT +#define PIN_JOY1_4 5 // LEFT +#endif -#else // OLD LAYOUT!!!! +#else +// OLD LAYOUT!!!! #define HAS_VGA 1 // ILI9341 #define TFT_SCLK 13 -#define TFT_MOSI 12 -#define TFT_MISO 11 +#define TFT_MOSI 11 +#define TFT_MISO 12 #define TFT_TOUCH_CS 38 #define TFT_TOUCH_INT 37 #define TFT_DC 9 diff --git a/MCUME_teensy/teensyvectrex/platform_config.h b/MCUME_teensy/teensyvectrex/platform_config.h new file mode 100644 index 0000000..4186a21 --- /dev/null +++ b/MCUME_teensy/teensyvectrex/platform_config.h @@ -0,0 +1,14 @@ +#ifndef _PLATFORM_CONFIG_H_ +#define _PLATFORM_CONFIG_H_ + +//#define OLD_LAYOUT 1 +//#define TEENSYBOY 1 + +//#define HAS_PSRAM 1 + +//#define USE_SDFAT 1 +//#define SD_FAT_TYPE 1 +//#define USE_SDFS 1 +//#define SDFSDEV "1:" + +#endif diff --git a/MCUME_teensy/teensyvectrex/teensyvectrex.ino b/MCUME_teensy/teensyvectrex/teensyvectrex.ino index 07c7bec..829f043 100644 --- a/MCUME_teensy/teensyvectrex/teensyvectrex.ino +++ b/MCUME_teensy/teensyvectrex/teensyvectrex.ino @@ -174,19 +174,6 @@ void setup() { // **************************************************** void loop(void) { -#if defined(__IMXRT1052__) || defined(__IMXRT1062__) -#else - // if ( ((emu_ReadKeys() & (MASK_KEY_USER1+MASK_KEY_USER2)) == (MASK_KEY_USER1+MASK_KEY_USER2)) - // || (emu_ReadKeys() & MASK_KEY_USER4 ) ) - // { - // emu_printf((char*)"esc"); - // *(volatile uint32_t *)0xE000ED0C = 0x5FA0004; - // while (true) { - // ; - // } - // } -#endif - if (menuActive()) { uint16_t bClick = emu_DebounceLocalKeys(); int action = handleMenu(bClick); diff --git a/MCUME_teensy/teensyvectrex/tft_t_dma.cpp b/MCUME_teensy/teensyvectrex/tft_t_dma.cpp index 79b3e15..5013b73 100644 --- a/MCUME_teensy/teensyvectrex/tft_t_dma.cpp +++ b/MCUME_teensy/teensyvectrex/tft_t_dma.cpp @@ -6,7 +6,7 @@ #include "font8x8.h" -#define SPICLOCK 144e6 //Just a number..max speed +#define SPICLOCK 60000000 //144e6 //Just a number..max speed #ifdef ILI9341 #define SPI_MODE SPI_MODE0 #endif @@ -218,20 +218,35 @@ TFT_T_DMA::TFT_T_DMA(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t void TFT_T_DMA::setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2) { + int dx=0; + int dy=0; + +#ifdef ST7789 +#ifdef ROTATE_SCREEN + if (!flipped) { + dy += 80; + } +#else + if (flipped) { + dx += 80; + } +#endif +#endif SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE)); digitalWrite(_cs, 0); digitalWrite(_dc, 0); SPI.transfer(TFT_CASET); digitalWrite(_dc, 1); - SPI.transfer16(x1); - SPI.transfer16(x2); + + SPI.transfer16(x1+dx); + SPI.transfer16(x2+dx); digitalWrite(_dc, 0); SPI.transfer(TFT_PASET); digitalWrite(_dc, 1); - SPI.transfer16(y1); - SPI.transfer16(y2); + SPI.transfer16(y1+dy); + SPI.transfer16(y2+dy); digitalWrite(_dc, 0); SPI.transfer(TFT_RAMWR); @@ -325,7 +340,6 @@ void TFT_T_DMA::begin(void) { } } #endif - setArea(0, 0, TFT_REALWIDTH-1, TFT_REALHEIGHT-1); cancelled = false; @@ -351,8 +365,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 - //SPI.transfer(ST77XX_MADCTL_RGB); - SPI.transfer(ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_RGB); +#else + SPI.transfer(ST77XX_MADCTL_MY | ST77XX_MADCTL_MV |ST77XX_MADCTL_RGB); +#endif #endif } else { @@ -361,7 +378,11 @@ void TFT_T_DMA::flipscreen(bool flip) SPI.transfer(ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR); #endif #ifdef ST7789 +#ifdef ROTATE_SCREEN + SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB); +#else SPI.transfer(ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB); +#endif #endif } digitalWrite(_cs, 1); @@ -942,11 +963,21 @@ void TFT_T_DMA::writeLine(int width, int height, int y, uint8_t *buf, uint16_t * } #endif } + else if ((width*2) == TFT_WIDTH) { + for (int i=0; i