CRT Terminal app

This commit is contained in:
Dejvino
2026-11-11 22:03:40 +01:00
parent 1ef22cc832
commit 951c56168c
10 changed files with 1447 additions and 28 deletions
+33 -13
View File
@@ -1,34 +1,54 @@
# Local SDL2 emulator for MissileCommand (run the game on your PC)
# SDL2 emulator for T-Deck example sketches (run the app on your PC)
#
# make build ./missilecommand-emu
# make run build + launch the window
# make smoke headless test: 240 frames, dump the last frame to a BMP
# make build the MissileCommand emulator (default)
# make APP=CrtTerminal build the CRT terminal emulator
# make run [APP=...] build + launch the window
# make smoke [APP=...] headless test: N frames, dump the last frame
# make clean [APP=...] remove the binary + smoke frame
#
# The sketch is included by src/main.cpp as a single translation unit:
# -DAPP_INO='"<App>.ino"'. Apps with a sound_engine.cpp get it linked
# automatically (wildcard).
APP ?= MissileCommand
APP_DIR := ../examples/$(APP)
ifeq ($(APP),MissileCommand)
APP_TITLE := Missile Command
DEFS += -DEMU_APP_MISSILECOMMAND
endif
ifeq ($(APP),CrtTerminal)
APP_TITLE := CRT Terminal
endif
APP_TITLE ?= $(APP)
CXX ?= g++
CXXFLAGS ?= -std=gnu++17 -O2 -Wall -Wextra
SDLFLAGS := -I/usr/include/SDL2
INC := -Isrc/shim -I../examples/MissileCommand
INC := -Isrc/shim -I$(APP_DIR)
LIBS := -lSDL2 -lSDL2_ttf
GAME_DIR := ../examples/MissileCommand
SHIMS := $(wildcard src/shim/*.h) $(wildcard src/shim/driver/*.h)
EMU := missilecommand-emu
DEFS += -DAPP_INO='"$(APP).ino"' -DAPP_NAME='"$(APP_TITLE)"'
SHIMS := $(wildcard src/shim/*.h) $(wildcard src/shim/driver/*.h)
SOUND_SRC := $(wildcard $(APP_DIR)/sound_engine.cpp)
EMU := $(APP)-emu
.PHONY: all run smoke clean
all: $(EMU)
$(EMU): src/main.cpp $(GAME_DIR)/sound_engine.cpp $(GAME_DIR)/MissileCommand.ino $(SHIMS)
$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(INC) \
src/main.cpp $(GAME_DIR)/sound_engine.cpp -o $@ $(LIBS)
$(EMU): src/main.cpp $(SOUND_SRC) $(APP_DIR)/$(APP).ino $(SHIMS)
$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(INC) $(DEFS) \
src/main.cpp $(SOUND_SRC) -o $@ $(LIBS)
run: $(EMU)
./$(EMU)
# Headless smoke test: dummy video/audio, run N frames, save the last frame.
# Feed some keys: 'a' starts the game, arrows steer, etc. via --script.
# Feed keys via --type "text" (queued a few chars per frame).
smoke: $(EMU)
./$(EMU) --frames 240 --dump smoke_frame.bmp
clean:
rm -f $(EMU) smoke_frame.bmp
rm -f $(EMU) smoke_frame.bmp
+60 -11
View File
@@ -1,15 +1,15 @@
/**
* SDL2 host for the MissileCommand sketch — run the game on your PC.
* SDL2 host for T-Deck example sketches — run the app on your PC.
* The sketch is selected with -DAPP_INO (see the Makefile, APP=...).
*
* Input mapping:
* Arrow keys trackball roll (hold to keep rolling)
* Mouse wheel trackball roll (each notch = a ball pulse burst)
* Enter / right-click trackball press (fire)
* Left-click screen tap (aim + fire)
* A S D fire from left / center / right battery
* Space fire from nearest battery
* U / V volume up / down
* M mute toggle
* Enter / right-click trackball press
* Left-click screen tap
* Letters/digits keyboard device @ I2C 0x55
* Backspace/Delete keyboard device (0x08 / 0x7F)
* --type "text" queue keys programmatically (for smoke tests)
*/
#include <SDL.h>
#include <SDL_ttf.h>
@@ -19,8 +19,15 @@
#include "emu_gfx.h"
#include "emu_input.h"
// pull in the whole game as a single translation unit
#include "MissileCommand.ino"
#ifndef APP_INO
#define APP_INO "MissileCommand.ino"
#endif
#ifndef APP_NAME
#define APP_NAME "T-Deck app"
#endif
// pull in the whole sketch as a single translation unit
#include APP_INO
// ---------------------------------------------------------------------------
// Global instances declared extern in the shims
@@ -63,14 +70,22 @@ int main(int argc, char **argv)
{
long maxFrames = -1;
const char *dumpPath = nullptr;
bool autoplay = false;
const char *typeStr = nullptr;
int typeFromFrame = 30;
int dumpEvery = 0;
#ifdef EMU_APP_MISSILECOMMAND
bool autoplay = false;
#endif
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]);
else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i];
else if (!strcmp(argv[i], "--dump-interval") && i + 1 < argc) dumpEvery = atoi(argv[++i]);
else if (!strcmp(argv[i], "--type") && i + 1 < argc) typeStr = argv[++i];
else if (!strcmp(argv[i], "--type-at") && i + 1 < argc) typeFromFrame = atoi(argv[++i]);
#ifdef EMU_APP_MISSILECOMMAND
else if (!strcmp(argv[i], "--wave") && i + 1 < argc) emuStartWaveRequest = atoi(argv[++i]);
else if (!strcmp(argv[i], "--autoplay")) autoplay = true;
#endif
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
@@ -79,7 +94,7 @@ int main(int argc, char **argv)
}
TTF_Init();
SDL_Window *win = SDL_CreateWindow("Missile Command — T-Deck emulator",
SDL_Window *win = SDL_CreateWindow("T-Deck emulator — " APP_NAME,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_RESIZABLE);
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
@@ -122,6 +137,12 @@ int main(int argc, char **argv)
case SDLK_SPACE:
emu::keyboard.push_back(' ');
break;
case SDLK_BACKSPACE:
emu::keyboard.push_back(0x08);
break;
case SDLK_DELETE:
emu::keyboard.push_back(0x7F);
break;
case SDLK_ESCAPE:
running = false;
break;
@@ -203,7 +224,34 @@ int main(int argc, char **argv)
}
}
// --type: queue the string on the keyboard device like a human typing
// (one char per --type-at start, ~40 ms apart in millis time, so the
// sketch processes each key before the next arrives).
// '\n' emulates Enter: press the trackball pin for one iteration.
static const char *typeNext = nullptr;
static int typePressPin = -1;
static unsigned long lastTypeMs = 0;
if (typeStr && frame >= (uint32_t)typeFromFrame) {
if (typePressPin >= 0) {
emu::pinLevels[typePressPin] = 1; // release the Enter press
typePressPin = -1;
}
if (!typeNext) typeNext = typeStr;
const char *typeEnd = typeStr + strlen(typeStr);
if (typeNext < typeEnd && millis() - lastTypeMs >= 40) {
lastTypeMs = millis();
if (*typeNext == '\n' || *typeNext == '\r') {
typeNext++; // consume the Enter char
emu::pinLevels[BOARD_BOOT_PIN] = 0;
typePressPin = BOARD_BOOT_PIN;
} else {
emu::keyboard.push_back((uint8_t)*typeNext++);
}
}
}
// Headless test mode: start the game, wander the crosshair, keep firing.
#ifdef EMU_APP_MISSILECOMMAND
if (autoplay) {
static bool started = false;
if (!started && frame >= 5) {
@@ -215,6 +263,7 @@ int main(int argc, char **argv)
if (frame % 4 == 0) emu::pinLevels[dirPin[dir]] ^= 1;
if (frame % 75 == 0) emu::keyboard.push_back('s');
}
#endif
// fire any emulated interrupts triggered by the pin changes above
emu::dispatchIsrs();
+2 -2
View File
@@ -13,8 +13,8 @@ class Preferences {
public:
bool begin(const char *ns, bool readOnly = false)
{
(void)ns;
(void)readOnly;
snprintf(path_, sizeof(path_), "%s-prefs.cfg", ns);
map_.clear();
if (FILE *f = fopen(path_, "r")) {
char key[64];
@@ -53,5 +53,5 @@ private:
}
std::unordered_map<std::string, uint32_t> map_;
const char *path_ = "mc-prefs.cfg";
char path_[64] = "mc-prefs.cfg";
};