Skip to content

Commit

Permalink
Fix CYD compilation and a few other bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cgreening committed Nov 22, 2024
1 parent aac70b2 commit eae3a62
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 111 deletions.
3 changes: 2 additions & 1 deletion desktop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions desktop/src/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
static const std::unordered_map<SDL_Keycode, SpecKeys> 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},
};
64 changes: 64 additions & 0 deletions desktop/src/loadgame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#include "loadgame.h"
#include <iostream>
#include <cstdint>
#include <unordered_map>
#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;
}
7 changes: 7 additions & 0 deletions desktop/src/loadgame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <string>

class ZXSpectrum;

void loadGame(std::string filename, ZXSpectrum *machine);
104 changes: 6 additions & 98 deletions desktop/src/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,11 @@
#include "DummyListener.h"
#include "ZXSpectrumTapeListener.h"
#include "SDLAudioOutput.h"
#include "loadgame.h"
#include "input.h"
#include <thread>
#include <chrono>

std::unordered_map<SDL_Keycode, SpecKeys> 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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -233,7 +140,7 @@ void handleEvents(bool &isRunning)
#ifndef __EMSCRIPTEN__
if(!isLoading) {
std::string filename = OpenFileDialog();
loadGame(filename);
loadGame(filename, machine);
return;
}
#endif
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions firmware/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions firmware/src/AudioOutput/BuzzerOuput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/AudioOutput/BuzzerOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions firmware/src/Screens/EmulatorScreen/Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/Screens/GameFilePickerScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GameFilePickerScreen : public PickerScreen<FileInfoPtr>
}
// 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;
Expand Down
3 changes: 3 additions & 0 deletions firmware/src/TZX/tzx_cas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SDCard>(fileSystem);
#endif
// Serial.begin(115200);
Serial.begin(115200);
// for(int i = 0; i < 5; i++) {
// BusyLight bl;
// vTaskDelay(pdMS_TO_TICKS(1000));
Expand Down

0 comments on commit eae3a62

Please sign in to comment.