From eae3a62c7cc114f01bf3665c07b664824a0792f3 Mon Sep 17 00:00:00 2001 From: Chris Greening Date: Fri, 22 Nov 2024 23:06:18 +0000 Subject: [PATCH] Fix CYD compilation and a few other bugs --- desktop/Makefile | 3 +- desktop/src/input.h | 42 +++++++ desktop/src/loadgame.cpp | 64 +++++++++++ desktop/src/loadgame.h | 7 ++ desktop/src/main.mm | 104 +----------------- firmware/platformio.ini | 2 - firmware/src/AudioOutput/BuzzerOuput.cpp | 7 +- firmware/src/AudioOutput/BuzzerOutput.h | 4 +- .../src/Screens/EmulatorScreen/Machine.cpp | 3 - firmware/src/Screens/GameFilePickerScreen.h | 2 +- firmware/src/TZX/tzx_cas.cpp | 3 + firmware/src/main.cpp | 2 +- 12 files changed, 132 insertions(+), 111 deletions(-) create mode 100644 desktop/src/input.h create mode 100644 desktop/src/loadgame.cpp create mode 100644 desktop/src/loadgame.h diff --git a/desktop/Makefile b/desktop/Makefile index 3758ec5..6bed9aa 100644 --- a/desktop/Makefile +++ b/desktop/Makefile @@ -30,7 +30,8 @@ SRCS = \ ../firmware/src/Emulator/snaps.cpp \ ../firmware/src/AySound/AySound.cpp \ ../firmware/src/Serial.cpp \ - ../firmware/src/TZX/tzx_cas.cpp + ../firmware/src/TZX/tzx_cas.cpp \ + src/loadgame.cpp OBJC_SRCS = \ src/main.mm diff --git a/desktop/src/input.h b/desktop/src/input.h new file mode 100644 index 0000000..7eed12e --- /dev/null +++ b/desktop/src/input.h @@ -0,0 +1,42 @@ +static const std::unordered_map sdl_to_spec = { + {SDLK_a, SpecKeys::SPECKEY_A}, + {SDLK_b, SpecKeys::SPECKEY_B}, + {SDLK_c, SpecKeys::SPECKEY_C}, + {SDLK_d, SpecKeys::SPECKEY_D}, + {SDLK_e, SpecKeys::SPECKEY_E}, + {SDLK_f, SpecKeys::SPECKEY_F}, + {SDLK_g, SpecKeys::SPECKEY_G}, + {SDLK_h, SpecKeys::SPECKEY_H}, + {SDLK_i, SpecKeys::SPECKEY_I}, + {SDLK_j, SpecKeys::SPECKEY_J}, + {SDLK_k, SpecKeys::SPECKEY_K}, + {SDLK_l, SpecKeys::SPECKEY_L}, + {SDLK_m, SpecKeys::SPECKEY_M}, + {SDLK_n, SpecKeys::SPECKEY_N}, + {SDLK_o, SpecKeys::SPECKEY_O}, + {SDLK_p, SpecKeys::SPECKEY_P}, + {SDLK_q, SpecKeys::SPECKEY_Q}, + {SDLK_r, SpecKeys::SPECKEY_R}, + {SDLK_s, SpecKeys::SPECKEY_S}, + {SDLK_t, SpecKeys::SPECKEY_T}, + {SDLK_u, SpecKeys::SPECKEY_U}, + {SDLK_v, SpecKeys::SPECKEY_V}, + {SDLK_w, SpecKeys::SPECKEY_W}, + {SDLK_x, SpecKeys::SPECKEY_X}, + {SDLK_y, SpecKeys::SPECKEY_Y}, + {SDLK_z, SpecKeys::SPECKEY_Z}, + {SDLK_0, SpecKeys::SPECKEY_0}, + {SDLK_1, SpecKeys::SPECKEY_1}, + {SDLK_2, SpecKeys::SPECKEY_2}, + {SDLK_3, SpecKeys::SPECKEY_3}, + {SDLK_4, SpecKeys::SPECKEY_4}, + {SDLK_5, SpecKeys::SPECKEY_5}, + {SDLK_6, SpecKeys::SPECKEY_6}, + {SDLK_7, SpecKeys::SPECKEY_7}, + {SDLK_8, SpecKeys::SPECKEY_8}, + {SDLK_9, SpecKeys::SPECKEY_9}, + {SDLK_RETURN, SpecKeys::SPECKEY_ENTER}, + {SDLK_SPACE, SpecKeys::SPECKEY_SPACE}, + {SDLK_LSHIFT, SpecKeys::SPECKEY_SHIFT}, + {SDLK_RSHIFT, SpecKeys::SPECKEY_SYMB}, +}; \ No newline at end of file diff --git a/desktop/src/loadgame.cpp b/desktop/src/loadgame.cpp new file mode 100644 index 0000000..e884a69 --- /dev/null +++ b/desktop/src/loadgame.cpp @@ -0,0 +1,64 @@ +#include +#ifdef __EMSCRIPTEN__ +#include +#endif +#include "loadgame.h" +#include +#include +#include +#include "spectrum.h" +#include "tzx_cas.h" +#include "snaps.h" +#include "RawAudioListener.h" +#include "DummyListener.h" +#include "ZXSpectrumTapeListener.h" + +void loadGame(std::string filename, ZXSpectrum *machine) { + // check the extension for z80 or sna + if (filename.find(".z80") != std::string::npos || filename.find(".Z80") != std::string::npos + || filename.find(".sna") != std::string::npos || filename.find(".SNA") != std::string::npos) { + Load(machine, filename.c_str()); + return; + } + // time how long it takes to load the tape + int start = SDL_GetTicks(); + FILE *fp = fopen(filename.c_str(), "rb"); + if (fp == NULL) + { + std::cout << "Error: Could not open file." << std::endl; + return; + } + fseek(fp, 0, SEEK_END); + long file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + uint8_t *tzx_data = (uint8_t*)malloc(file_size); + fread(tzx_data, 1, file_size, fp); + fclose(fp); + // load the tape + TzxCas tzxCas; + DummyListener *dummyListener = new DummyListener(); + dummyListener->start(); + tzxCas.load_tzx(dummyListener, tzx_data, file_size); + dummyListener->finish(); + uint64_t totalTicks = dummyListener->getTotalTicks(); + + ZXSpectrumTapeListener *listener = new ZXSpectrumTapeListener(machine, [&](uint64_t progress) + { + // printf("Total execution time: %fs\n", (float) listener->getTotalExecutionTime() / 1000000.0f); + // printf("Total machine time: %f\n", (float) listener->getTotalTicks() / 3500000.0f); + // printf("Progress: %lld\n", progress * 100 / totalTicks); + }); + listener->start(); + if (filename.find(".tap") != std::string::npos || filename.find(".TAP") != std::string::npos) { + tzxCas.load_tap(listener, tzx_data, file_size); + } else { + tzxCas.load_tzx(listener, tzx_data, file_size); + } + listener->finish(); + std::cout << "Total ticks: " << listener->getTotalTicks() << std::endl; + std::cout << "Total time: " << listener->getTotalTicks() / 3500000.0 << " seconds" << std::endl; + + std::cout << "Loaded tape." << std::endl; + int end = SDL_GetTicks(); + std::cout << "Time to load tape: " << end - start << "ms" << std::endl; +} \ No newline at end of file diff --git a/desktop/src/loadgame.h b/desktop/src/loadgame.h new file mode 100644 index 0000000..2f4a3f4 --- /dev/null +++ b/desktop/src/loadgame.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +class ZXSpectrum; + +void loadGame(std::string filename, ZXSpectrum *machine); \ No newline at end of file diff --git a/desktop/src/main.mm b/desktop/src/main.mm index fcc27ee..6ae2a9c 100644 --- a/desktop/src/main.mm +++ b/desktop/src/main.mm @@ -15,52 +15,11 @@ #include "DummyListener.h" #include "ZXSpectrumTapeListener.h" #include "SDLAudioOutput.h" +#include "loadgame.h" +#include "input.h" #include #include -std::unordered_map sdl_to_spec = { - {SDLK_a, SpecKeys::SPECKEY_A}, - {SDLK_b, SpecKeys::SPECKEY_B}, - {SDLK_c, SpecKeys::SPECKEY_C}, - {SDLK_d, SpecKeys::SPECKEY_D}, - {SDLK_e, SpecKeys::SPECKEY_E}, - {SDLK_f, SpecKeys::SPECKEY_F}, - {SDLK_g, SpecKeys::SPECKEY_G}, - {SDLK_h, SpecKeys::SPECKEY_H}, - {SDLK_i, SpecKeys::SPECKEY_I}, - {SDLK_j, SpecKeys::SPECKEY_J}, - {SDLK_k, SpecKeys::SPECKEY_K}, - {SDLK_l, SpecKeys::SPECKEY_L}, - {SDLK_m, SpecKeys::SPECKEY_M}, - {SDLK_n, SpecKeys::SPECKEY_N}, - {SDLK_o, SpecKeys::SPECKEY_O}, - {SDLK_p, SpecKeys::SPECKEY_P}, - {SDLK_q, SpecKeys::SPECKEY_Q}, - {SDLK_r, SpecKeys::SPECKEY_R}, - {SDLK_s, SpecKeys::SPECKEY_S}, - {SDLK_t, SpecKeys::SPECKEY_T}, - {SDLK_u, SpecKeys::SPECKEY_U}, - {SDLK_v, SpecKeys::SPECKEY_V}, - {SDLK_w, SpecKeys::SPECKEY_W}, - {SDLK_x, SpecKeys::SPECKEY_X}, - {SDLK_y, SpecKeys::SPECKEY_Y}, - {SDLK_z, SpecKeys::SPECKEY_Z}, - {SDLK_0, SpecKeys::SPECKEY_0}, - {SDLK_1, SpecKeys::SPECKEY_1}, - {SDLK_2, SpecKeys::SPECKEY_2}, - {SDLK_3, SpecKeys::SPECKEY_3}, - {SDLK_4, SpecKeys::SPECKEY_4}, - {SDLK_5, SpecKeys::SPECKEY_5}, - {SDLK_6, SpecKeys::SPECKEY_6}, - {SDLK_7, SpecKeys::SPECKEY_7}, - {SDLK_8, SpecKeys::SPECKEY_8}, - {SDLK_9, SpecKeys::SPECKEY_9}, - {SDLK_RETURN, SpecKeys::SPECKEY_ENTER}, - {SDLK_SPACE, SpecKeys::SPECKEY_SPACE}, - {SDLK_LSHIFT, SpecKeys::SPECKEY_SHIFT}, - {SDLK_RSHIFT, SpecKeys::SPECKEY_SYMB}, -}; - bool isLoading = false; bool isRunning = false; uint16_t flashTimer = 0; @@ -158,58 +117,6 @@ void updateAndRender(SDL_Renderer *renderer, SDL_Texture *texture, uint16_t *fra SDL_RenderPresent(renderer); } -void loadGame(std::string filename) { - // check the extension for z80 or sna - if (filename.find(".z80") != std::string::npos || filename.find(".Z80") != std::string::npos - || filename.find(".sna") != std::string::npos || filename.find(".SNA") != std::string::npos) { - Load(machine, filename.c_str()); - return; - } - // time how long it takes to load the tape - int start = SDL_GetTicks(); - isLoading = true; - FILE *fp = fopen(filename.c_str(), "rb"); - if (fp == NULL) - { - std::cout << "Error: Could not open file." << std::endl; - return; - } - fseek(fp, 0, SEEK_END); - long file_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - uint8_t *tzx_data = (uint8_t*)malloc(file_size); - fread(tzx_data, 1, file_size, fp); - fclose(fp); - // load the tape - TzxCas tzxCas; - DummyListener *dummyListener = new DummyListener(); - dummyListener->start(); - tzxCas.load_tzx(dummyListener, tzx_data, file_size); - dummyListener->finish(); - uint64_t totalTicks = dummyListener->getTotalTicks(); - - ZXSpectrumTapeListener *listener = new ZXSpectrumTapeListener(machine, [&](uint64_t progress) - { - // printf("Total execution time: %fs\n", (float) listener->getTotalExecutionTime() / 1000000.0f); - // printf("Total machine time: %f\n", (float) listener->getTotalTicks() / 3500000.0f); - // printf("Progress: %lld\n", progress * 100 / totalTicks); - }); - listener->start(); - if (filename.find(".tap") != std::string::npos || filename.find(".TAP") != std::string::npos) { - tzxCas.load_tap(listener, tzx_data, file_size); - } else { - tzxCas.load_tzx(listener, tzx_data, file_size); - } - listener->finish(); - std::cout << "Total ticks: " << listener->getTotalTicks() << std::endl; - std::cout << "Total time: " << listener->getTotalTicks() / 3500000.0 << " seconds" << std::endl; - - std::cout << "Loaded tape." << std::endl; - isLoading = false; - int end = SDL_GetTicks(); - std::cout << "Time to load tape: " << end - start << "ms" << std::endl; -} - // Handles SDL events, including quit and keyboard input void handleEvents(bool &isRunning) { @@ -233,7 +140,7 @@ void handleEvents(bool &isRunning) #ifndef __EMSCRIPTEN__ if(!isLoading) { std::string filename = OpenFileDialog(); - loadGame(filename); + loadGame(filename, machine); return; } #endif @@ -347,7 +254,6 @@ void fillFrameBuffer(uint16_t *pixelBuffer, uint8_t *currentScreenBuffer, uint8_ void main_loop() { handleEvents(isRunning); - // machine->runForFrame(audioOutput, nullptr); count++; // fill out the framebuffer fillFrameBuffer(frameBuffer, machine->mem.currentScreen, machine->hwopt.BorderColor); @@ -403,7 +309,9 @@ int main() #else std::string filename = OpenFileDialog(); #endif - loadGame(filename); + isLoading = true; + loadGame(filename, machine); + isLoading = false; audioOutput = new SDLAudioOutput(machine); audioOutput->start(15600); isRunning = true; diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 2c2d4f7..592d437 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -134,8 +134,6 @@ build_flags = [env:cheap-yellow-display] extends = esp32_common board = esp-wrover-kit -platform_packages = - platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.13 build_flags = ${common.build_flags} ; TFT setup diff --git a/firmware/src/AudioOutput/BuzzerOuput.cpp b/firmware/src/AudioOutput/BuzzerOuput.cpp index 2a3a166..6ade152 100644 --- a/firmware/src/AudioOutput/BuzzerOuput.cpp +++ b/firmware/src/AudioOutput/BuzzerOuput.cpp @@ -76,9 +76,10 @@ bool IRAM_ATTR BuzzerOutput::onTimer() // output a sample from the buffer if we have one if (mCurrentIndex < mBufferLength) { - uint16_t micSample = adc1_get_raw(ADC1_CHANNEL_4); - micAve = (micAve * 99 + micSample) / 100; - uint8_t micValue = micSample > (micAve * 1001)/1000 ? 1 : 0; + // uint16_t micSample = adc1_get_raw(ADC1_CHANNEL_4); + // micAve = (micAve * 99 + micSample) / 100; + // uint8_t micValue = micSample > (micAve * 1001)/1000 ? 1 : 0; + uint8_t micValue = 0; xQueueSendFromISR(micValueQueue, &micValue, &xHigherPriorityTaskWoken); mCount++; // get the first sample from the buffer - shift it up to 9 bits for max resolution diff --git a/firmware/src/AudioOutput/BuzzerOutput.h b/firmware/src/AudioOutput/BuzzerOutput.h index 66228f9..d00829a 100644 --- a/firmware/src/AudioOutput/BuzzerOutput.h +++ b/firmware/src/AudioOutput/BuzzerOutput.h @@ -29,8 +29,8 @@ class BuzzerOutput : public AudioOutput BuzzerOutput(gpio_num_t buzzerPin) : AudioOutput() { // set up the ADC - adc1_config_width(ADC_WIDTH_BIT_12); - adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_12); + // adc1_config_width(ADC_WIDTH_BIT_12); + // adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_11); mBuzzerPin = buzzerPin; mBufferSemaphore = xSemaphoreCreateBinary(); xSemaphoreGive(mBufferSemaphore); diff --git a/firmware/src/Screens/EmulatorScreen/Machine.cpp b/firmware/src/Screens/EmulatorScreen/Machine.cpp index d55b8cd..a0e0446 100644 --- a/firmware/src/Screens/EmulatorScreen/Machine.cpp +++ b/firmware/src/Screens/EmulatorScreen/Machine.cpp @@ -95,10 +95,7 @@ void Machine::startLoading() machine->updatekey(SPECKEY_SYMB, 1); tapKey(SPECKEY_P); tapKey(SPECKEY_P); - tapKey(SPECKEY_SHIFT); - tapKey(SPECKEY_K); machine->updatekey(SPECKEY_SYMB, 0); - machine->updatekey(SPECKEY_SHIFT, 0); tapKey(SPECKEY_ENTER); } else diff --git a/firmware/src/Screens/GameFilePickerScreen.h b/firmware/src/Screens/GameFilePickerScreen.h index aaaa4b6..ca9d4ad 100644 --- a/firmware/src/Screens/GameFilePickerScreen.h +++ b/firmware/src/Screens/GameFilePickerScreen.h @@ -23,7 +23,7 @@ class GameFilePickerScreen : public PickerScreen } // if we found the emulator screen and if we're loading a tape file then we've been triggered due to the user starting the // load routine from the emulator screen. We just need to tell the emulator screen to load the tape file and pop back to it - if (emulatorScreen != nullptr && item->getExtension() == ".tap" || item->getExtension() == ".tzx") { + if (emulatorScreen != nullptr && (item->getExtension() == ".tap" || item->getExtension() == ".tzx")) { Serial.println("Loading tape file into existing emulator screen"); // pop to the emaulator screen - get a local copy of the stack as it will be set to null when we pop NavigationStack *navStack = m_navigationStack; diff --git a/firmware/src/TZX/tzx_cas.cpp b/firmware/src/TZX/tzx_cas.cpp index 3719b73..2b2286d 100644 --- a/firmware/src/TZX/tzx_cas.cpp +++ b/firmware/src/TZX/tzx_cas.cpp @@ -121,6 +121,9 @@ void TzxCas::TzxCas::tzx_cas_get_blocks(const uint8_t *casdata, int caslen) pos += 1 + datasize; break; case 0x33: + // TODO - hardware type block + // haven't seen this in the wild yet - but it could let us + // work out the correct machine to emulate datasize = casdata[pos]; pos += 1 + 3 * datasize; break; diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 72232d8..6dac0ae 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -53,7 +53,7 @@ void setup(void) SDCard *fileSystem = new SDCard(MOUNT_POINT, SD_CARD_MISO, SD_CARD_MOSI, SD_CARD_CLK, SD_CARD_CS); IFiles *files = new FilesImplementation(fileSystem); #endif - // Serial.begin(115200); + Serial.begin(115200); // for(int i = 0; i < 5; i++) { // BusyLight bl; // vTaskDelay(pdMS_TO_TICKS(1000));