Optimizations

This commit is contained in:
Tomislav Kopić 2024-06-16 11:14:06 +02:00
parent 7b96ba2d19
commit 67a5277d2a

View File

@ -19,24 +19,8 @@
#define ENABLE_SAVE_STATUS #define ENABLE_SAVE_STATUS
#define AUTO_SAVE_MINUTES 10 // Auto save for every 10 minutes #define AUTO_SAVE_MINUTES 10 // Auto save for every 10 minutes
#define ENABLE_LOAD_STATE_FROM_EEPROM #define ENABLE_LOAD_STATE_FROM_EEPROM
//#define ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START
// #define ENABLE_SERIAL_DUMP
//#define ENABLE_SERIAL_DEBUG_INPUT
//#define ENABLE_LOAD_HARCODED_STATE_WHEN_START
/***************************/
/***** Time and clock *****/
// ESP8266 is not a very good time keeping device, each one of these little shits has a different clock oscilator.
// it also seems that the temperature also effects the clock frequency so time keeps on drifting and it get's noticable after some time.
// You can play around with these 2 to try and reduce the drift:
#define EMULATOR_CLOCK_SPEED 1000000 #define EMULATOR_CLOCK_SPEED 1000000
// If game time is going faster than it should, increase this value by 1 to slow it down. /***************************/
// fixed microsecond sleep between each cycle used to fine tune the emulator time drift even more
#define FINE_TUNING_DELAY 1
// If game time is going faster than it should, increase this value
/**************************************************************************/
/***** Set display orientation, U8G2_MIRROR_VERTICAL is not supported *****/ /***** Set display orientation, U8G2_MIRROR_VERTICAL is not supported *****/
//#define U8G2_LAYOUT_NORMAL //#define U8G2_LAYOUT_NORMAL
@ -59,7 +43,6 @@ U8G2_SSD1306_128X64_NONAME_2_HW_I2C display(U8G2_MIRROR);
#define PIN_BTN_L 12 // D6 #define PIN_BTN_L 12 // D6
#define PIN_BTN_M 13 // D7 #define PIN_BTN_M 13 // D7
#define PIN_BTN_R 15 // D8 #define PIN_BTN_R 15 // D8
//define PIN_BTN_SAVE 3
#define PIN_BUZZER 0 // D5 #define PIN_BUZZER 0 // D5
/**** TamaLib Specific Variables ****/ /**** TamaLib Specific Variables ****/
@ -81,11 +64,10 @@ static void hal_log(log_level_t level, char *buff, ...) {
static void hal_sleep_until(timestamp_t ts) { static void hal_sleep_until(timestamp_t ts) {
#ifdef ENABLE_REAL_TIME #ifdef ENABLE_REAL_TIME
int32_t remaining = (int32_t) (ts - hal_get_timestamp()); int32_t remaining = (int32_t)(ts - hal_get_timestamp());
if (remaining > 0) { if (remaining > 0) {
delayMicroseconds(remaining); delayMicroseconds(remaining);
} }
delayMicroseconds(FINE_TUNING_DELAY); // fixed delay to compensate for emulator time drifting
#endif #endif
} }
@ -179,17 +161,15 @@ static int hal_handler(void) {
#ifdef ENABLE_SAVE_STATUS #ifdef ENABLE_SAVE_STATUS
if (digitalRead(PIN_BTN_L) == HIGH && digitalRead(PIN_BTN_M) == HIGH && digitalRead(PIN_BTN_R) == HIGH) { if (digitalRead(PIN_BTN_L) == HIGH && digitalRead(PIN_BTN_M) == HIGH && digitalRead(PIN_BTN_R) == HIGH) {
if (button4state==0) { if (button4state==0) {
saveStateToEEPROM(&cpuState); saveStateToEEPROM(&cpuState);
noTone(PIN_BUZZER); noTone(PIN_BUZZER);
tone(PIN_BUZZER, 700,100); tone(PIN_BUZZER, 700, 100);
delay(120); delay(120);
noTone(PIN_BUZZER); noTone(PIN_BUZZER);
tone(PIN_BUZZER, 880,100); tone(PIN_BUZZER, 880, 100);
delay(120); delay(120);
noTone(PIN_BUZZER); noTone(PIN_BUZZER);
tone(PIN_BUZZER, 1175,100); tone(PIN_BUZZER, 1175, 100);
delay(120); delay(120);
noTone(PIN_BUZZER); noTone(PIN_BUZZER);
} }
@ -198,7 +178,6 @@ static int hal_handler(void) {
button4state = 0; button4state = 0;
} }
#endif #endif
#endif #endif
return 0; return 0;
} }
@ -216,32 +195,22 @@ static hal_t hal = {
.handler = &hal_handler, .handler = &hal_handler,
}; };
/*
void drawTriangle(uint8_t x, uint8_t y) {
//display.drawLine(x,y,x+6,y);
display.drawLine(x+1,y+1,x+5,y+1);
display.drawLine(x+2,y+2,x+4,y+2);
display.drawLine(x+3,y+3,x+3,y+3);
}
*/
void drawTamaRow(uint8_t tamaLCD_y, uint8_t ActualLCD_y, uint8_t thick) { void drawTamaRow(uint8_t tamaLCD_y, uint8_t ActualLCD_y, uint8_t thick) {
uint8_t i; uint8_t i;
for (i = 0; i < LCD_WIDTH; i++) { for (i = 0; i < LCD_WIDTH; i++) {
uint8_t mask = 0b10000000; uint8_t mask = 0b10000000;
mask = mask >> (i % 8); mask = mask >> (i % 8);
if ( (matrix_buffer[tamaLCD_y][i/8] & mask) != 0) { if ((matrix_buffer[tamaLCD_y][i/8] & mask) != 0) {
display.drawBox(i+i+i+16,ActualLCD_y,2,thick); display.drawBox(i+i+i+16, ActualLCD_y, 2, thick);
} }
} }
} }
void drawTamaSelection(uint8_t y) { void drawTamaSelection(uint8_t y) {
uint8_t i; uint8_t i;
for(i=0;i<8;i++) { for (i = 0; i < 8; i++) {
if (icon_buffer[i]) { if (icon_buffer[i]) {
// drawTriangle(i*16+5,y); display.drawXBMP(i * 16 + 4, y + 6, 8, 8, bitmaps + i * 8);
display.drawXBMP(i*16+4,y+6,8,8,bitmaps+i*8);
} }
} }
} }
@ -254,36 +223,36 @@ void displayTama() {
display.nextPage(); display.nextPage();
for (j = 11; j < LCD_HEIGHT; j++) { for (j = 11; j < LCD_HEIGHT; j++) {
drawTamaRow(j,j+j+j,2); drawTamaRow(j, j + j + j, 2);
} }
display.nextPage(); display.nextPage();
for (j = 5; j <= 10; j++) { for (j = 5; j <= 10; j++) {
if (j==5) { if (j == 5) {
drawTamaRow(j,j+j+j+1,1); drawTamaRow(j, j + j + j + 1, 1);
} else { } else {
drawTamaRow(j,j+j+j,2); drawTamaRow(j, j + j + j, 2);
} }
} }
display.nextPage(); display.nextPage();
for (j = 0; j <= 5; j++) { for (j = 0; j <= 5; j++) {
if (j==5) { if (j == 5) {
drawTamaRow(j,j+j+j,1); drawTamaRow(j, j + j + j, 1);
} else { } else {
drawTamaRow(j,j+j+j,2); drawTamaRow(j, j + j + j, 2);
} }
} }
display.nextPage(); display.nextPage();
#else #else
for (j = 0; j < LCD_HEIGHT; j++) { for (j = 0; j < LCD_HEIGHT; j++) {
if (j!=5) drawTamaRow(j,j+j+j,2); if (j != 5) drawTamaRow(j, j + j + j, 2);
if (j==5) { if (j == 5) {
drawTamaRow(j,j+j+j,1); drawTamaRow(j, j + j + j, 1);
display.nextPage(); display.nextPage();
drawTamaRow(j,j+j+j+1,1); drawTamaRow(j, j + j + j + 1, 1);
} }
if (j==10) display.nextPage(); if (j == 10) display.nextPage();
} }
display.nextPage(); display.nextPage();
drawTamaSelection(49); drawTamaSelection(49);
@ -293,7 +262,7 @@ void displayTama() {
#if defined(ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START) || defined(ENABLE_SERIAL_DUMP) #if defined(ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START) || defined(ENABLE_SERIAL_DUMP)
void dumpStateToSerial() { void dumpStateToSerial() {
uint16_t i, count=0; uint16_t i, count = 0;
char tmp[10]; char tmp[10];
cpu_get_state(&cpuState); cpu_get_state(&cpuState);
u4_t *memTemp = cpuState.memory; u4_t *memTemp = cpuState.memory;
@ -301,26 +270,17 @@ void dumpStateToSerial() {
Serial.println(""); Serial.println("");
Serial.println("static const uint8_t hardcodedState[] PROGMEM = {"); Serial.println("static const uint8_t hardcodedState[] PROGMEM = {");
for(i=0;i<sizeof(cpu_state_t);i++,count++) { for (i = 0; i < sizeof(cpu_state_t); i++, count++) {
sprintf(tmp, "0x%02X,", cpuS[i]); sprintf(tmp, "0x%02X,", cpuS[i]);
Serial.print(tmp); Serial.print(tmp);
if ((count % 16)==15) Serial.println(""); if ((count % 16) == 15) Serial.println("");
} }
for (i = 0; i < MEMORY_SIZE; i++,count++) { for (i = 0; i < MEMORY_SIZE; i++, count++) {
sprintf(tmp, "0x%02X,",memTemp[i]); sprintf(tmp, "0x%02X,", memTemp[i]);
Serial.print(tmp); Serial.print(tmp);
if ((count % 16)==15) Serial.println(""); if ((count % 16) == 15) Serial.println("");
} }
Serial.println("};"); Serial.println("};");
/*
Serial.println("");
Serial.println("static const uint8_t bitmaps[] PROGMEM = {");
for(i=0;i<144;i++) {
sprintf(tmp, "0x%02X,", bitmaps_raw[i]);
Serial.print(tmp);
if ((i % 18)==17) Serial.println("");
}
Serial.println("};"); */
} }
#endif #endif
@ -328,7 +288,7 @@ uint8_t reverseBits(uint8_t num) {
uint8_t reverse_num = 0; uint8_t reverse_num = 0;
uint8_t i; uint8_t i;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if((num & (1 << i))) if ((num & (1 << i)))
reverse_num |= 1 << ((8 - 1) - i); reverse_num |= 1 << ((8 - 1) - i);
} }
return reverse_num; return reverse_num;
@ -339,7 +299,6 @@ void setup() {
pinMode(PIN_BTN_L, INPUT); pinMode(PIN_BTN_L, INPUT);
pinMode(PIN_BTN_M, INPUT); pinMode(PIN_BTN_M, INPUT);
pinMode(PIN_BTN_R, INPUT); pinMode(PIN_BTN_R, INPUT);
//pinMode(PIN_BTN_SAVE, INPUT);
pinMode(PIN_BUZZER, OUTPUT); pinMode(PIN_BUZZER, OUTPUT);
display.setI2CAddress(DISPLAY_I2C_ADDRESS * 2); // required if display does not use default address of 0x3C display.setI2CAddress(DISPLAY_I2C_ADDRESS * 2); // required if display does not use default address of 0x3C
@ -355,8 +314,7 @@ void setup() {
#endif #endif
#ifdef ENABLE_LOAD_STATE_FROM_EEPROM #ifdef ENABLE_LOAD_STATE_FROM_EEPROM
if (validEEPROM()) if (validEEPROM()) {
{
loadStateFromEEPROM(&cpuState); loadStateFromEEPROM(&cpuState);
} else { } else {
Serial.println(F("No magic number in state, skipping state restore")); Serial.println(F("No magic number in state, skipping state restore"));
@ -365,18 +323,11 @@ void setup() {
loadHardcodedState(&cpuState); loadHardcodedState(&cpuState);
#endif #endif
/*
int i;
for(i=0;i<(18*8);i++) {
bitmaps_raw[i]= reverseBits(bitmaps_raw[i]);
}
*/
#ifdef ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START #ifdef ENABLE_DUMP_STATE_TO_SERIAL_WHEN_START
dumpStateToSerial(); dumpStateToSerial();
#endif #endif
} }
uint32_t middle_long_press_started = 0; uint32_t middle_long_press_started = 0;
uint32_t right_long_press_started = 0; uint32_t right_long_press_started = 0;
bool is_display_off = false; bool is_display_off = false;
@ -387,12 +338,12 @@ void loop() {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
#ifdef AUTO_SAVE_MINUTES #ifdef AUTO_SAVE_MINUTES
if ((currentMillis - lastSaveTimestamp) > AUTO_SAVE_INTERVAL) { if ((currentMillis - lastSaveTimestamp) > AUTO_SAVE_INTERVAL) {
lastSaveTimestamp = currentMillis; lastSaveTimestamp = currentMillis;
saveStateToEEPROM(&cpuState); saveStateToEEPROM(&cpuState);
} }
#endif #endif
bool middleLeftPressed = (digitalRead(PIN_BTN_M) == HIGH) && (digitalRead(PIN_BTN_L) == HIGH); bool middleLeftPressed = (digitalRead(PIN_BTN_M) == HIGH) && (digitalRead(PIN_BTN_L) == HIGH);
bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH); bool rightPressed = (digitalRead(PIN_BTN_R) == HIGH);
@ -400,9 +351,9 @@ void loop() {
if (middleLeftPressed) { if (middleLeftPressed) {
if ((currentMillis - middle_long_press_started) > 2000) { if ((currentMillis - middle_long_press_started) > 2000) {
eraseStateFromEEPROM(); eraseStateFromEEPROM();
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
ESP.restart(); ESP.restart();
#endif #endif
} }
} else { } else {
middle_long_press_started = currentMillis; middle_long_press_started = currentMillis;