FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
MultitaskingCPM

A multitasking, multi-session CP/M 3 compatible system. Up to 12 independent Z80 machines run at the same time, each one with its own screen, and you switch between them with the function keys. Every session runs a CP/M 3 compatible BDOS and CCP reimplemented in C++, with files stored as normal files on the SD card or on the internal flash (SPIFFS). Watch it on YouTube.

Getting started

At the first boot the system creates the drives and copies the included programs (this takes a while), then shows the A> prompt. Type HELP for the list of the built-in commands.

A>HELP
A>LS BIN
A>CD DEMOS
A>MBASIC SOUND

At every boot each new session runs PROFILE.SUB, which sets the search path (A:BIN;A:SDK) and shows INFO.

Sessions

Key / command Action
F1 ... F12 Switch to session 1...12; a new session is created the first time its key is pressed
TERM 0-11 Switch to the specified session
TERM AUX Start a new session connected to the serial port (TX = GPIO2, RX = GPIO34, 115200 8N1, XON/XOFF)
TERM USB Start a new session connected to the USB serial port (use any terminal program on the PC)
EXIT Close the current session (EXIT n closes session n)

Each session needs about 20 KB of RAM: the number of sessions you can open depends on the free memory.

Built-in commands

Command Description
DIR Directory view
LS Colored directory view
CD Display or change the current directory (subdirectories are supported, e.g. CD SDK)
ERA (ERASE, DELETE, DEL, RM) Remove one or more files
REN (RENAME) Rename one or more files
TYPE (CAT) Display a text file
PATH Get or set the directories searched for programs
MKDIR (MD) / RMDIR Create / remove a directory
COPY (CP) Copy files
INFO / DINFO System info / debug info (sessions, memory)
EMU n Terminal emulation of the session (EMU alone lists them: ANSI, ADM 3A, ADM 31, Hazeltine 1500, Osborne, Kaypro, VT52, ANSI Legacy)
KEYB xx Keyboard layout: US, UK, DE, IT, ES, FR, BE, NO
WIFISCAN Scan the WiFi networks
WIFI ssid password Connect to a WiFi network. Example: WIFI mynet mypass
PING host Ping a host. Example: PING www.fabgl.com
TELNET host Telnet to a host. Example: TELNET towel.blinkenlights.nl
FORMAT Erase the SPIFFS or SD card and restore the programs at the next boot
REBOOT Restart the system
HELP (?) Show the help

Ctrl+C stops the running program (and PING / TELNET).

Drives and included programs

Drive Location
A: driveA directory on the SD card, or on SPIFFS if no SD card is present
B: driveB directory (empty)

Programs copied to drive A: at the first boot:

Directory Programs
\ PROFILE.SUB (startup script of each session)
BIN CP/M 3 utilities: DATE, GET, PUT, SETDEF, SUBMIT, SHOW, PIP, ERASE, ED, DIR, DEVICE, TYPE, SAVE, RENAME, DUMP
SDK MBASIC, MAC, RMAC, LINK, LIB, HEXCOM, GENCOM, ZSID, VDE (text editor)
GAMES CATCHUM, GORILLA
DEMOS BASIC programs that use the FabGL terminal sequences: BLINK, GPIOREAD, ADC, ADCVOLTS, SOUND, MOUSE, GRAPH, SPRITE (CD DEMOS, then MBASIC SOUND); SAY.COM text-to-speech with the sample texts SPEECH1.TXT...SPEECH4.TXT

SAY speaks English text, phonemes in brackets, or a text file:

A>SAY Hello world
A>CD DEMOS
A>SAY SPEECH1.TXT

More programs (Turbo Pascal 3, SBASIC, LADDER, ZEXALL/ZEXDOC, GREP, DIFF, ...) are available in main/src/programs.h but excluded when WiFi is enabled, to save flash space: comment out #define HAS_WIFI in main/src/defs.h to include them (then run FORMAT so they are copied again).

To add your own programs, convert them with xxd -i and add them to the programs[] table in main/src/programs.h (see the instructions at the top of that file), or copy them to the SD card directly from a PC: the driveA and driveB directories are normal FAT directories.

What it demonstrates

  • The fabgl::Z80 CPU core, running several virtual machines in separate FreeRTOS tasks
  • A CP/M 3 compatible BDOS, BIOS and CCP implemented in C++ on top of the ESP-IDF file system (fabgl::FileBrowser, SD card or SPIFFS)
  • Several fabgl::Terminal instances sharing one display, each with its own emulation, and terminals connected to the UART (fabgl::SerialPort)
  • WiFi, ping and telnet from inside an emulated machine
  • Reducing the default queue and stack sizes of the library to save RAM

Hardware

  • VGA output (default pins)
  • PS/2 keyboard on port 0, optional PS/2 mouse on port 1 (used by MOUSE.BAS)
  • Optional SD card (TTGO VGA32 slot: MISO = GPIO16 (2 on PICO-D4), MOSI = GPIO17 (12 on PICO-D4), CLK = GPIO14, CS = GPIO13)
  • Optional audio on GPIO25 (SOUND.BAS, SAY.COM)

Build

The example ships a partitions.csv (2 MB application + SPIFFS). See Building the examples.

Source code

Main file of the example, main/MultitaskingCPM.cpp. The complete project is in the examples/VGA/MultitaskingCPM folder.

1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2026 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26/*
27 * Optional SD Card connections:
28 * MISO => GPIO 16 (2 for PICO-D4)
29 * MOSI => GPIO 17 (12 for PICO-D4)
30 * CLK => GPIO 14
31 * CS => GPIO 13
32 *
33 * To change above assignment fill other paramaters of FileBrowser::mountSDCard().
34 */
35
36
37
38#include <string>
39
40#include "fabgl.h"
41
42
43// UART0 (console) stream
44fabgl::UARTStream Console;
45#include "src/supervisor.h"
46#include "src/programs.h"
47
48
49
50// Flash and SDCard configuration
51#define FORMAT_ON_FAIL true
52#define SPIFFS_MOUNT_PATH "/flash"
53#define SDCARD_MOUNT_PATH "/SD"
54#define MAXFILES 6 // SDCARD: each file takes about 4K of RAM! On ESP32 2.0.0 this may fail without PSRAM, so set to "4"
55
56
57using std::string;
58
59
60// globals
61
62fabgl::VGA16Controller DisplayController; // unsupported on ESP32 core 2.0.0!!
63//fabgl::VGATextController DisplayController;
64fabgl::PS2Controller PS2Controller;
65
66Supervisor supervisor(&DisplayController);
67
68
69
70// base path (can be SPIFFS_MOUNT_PATH or SDCARD_MOUNT_PATH depending from what was successfully mounted first)
71char const * basepath = nullptr;
72
73
74string driveA_path;
75string driveB_path;
76
77
78
79void setup()
80{
81 Console.begin();
82
83
84
85 // Reduces some defaults to save RAM...
91
92 // because mouse is optional, don't re-try if it is not found (to speed-up boot)
94
95 // keyboard configured on port 0, and optionally mouse on port 1
96 PS2Controller.begin(PS2Preset::KeyboardPort0_MousePort1);
97
98 // setup VGA (default configuration with 64 colors)
99
100 DisplayController.begin();
101 DisplayController.setResolution(VGA_640x480_60Hz); // good when using VGA16Controller
102
103 auto term = new fabgl::Terminal;
104 term->begin(&DisplayController);
105 term->connectLocally(); // to use Terminal.read(), available(), etc..
106 term->clear();
107 term->write("Initializing...\r");
108 term->flush();
109
110 if (FileBrowser::mountSDCard(FORMAT_ON_FAIL, SDCARD_MOUNT_PATH, MAXFILES))
111 basepath = SDCARD_MOUNT_PATH;
112 else if (FileBrowser::mountSPIFFS(FORMAT_ON_FAIL, SPIFFS_MOUNT_PATH, MAXFILES))
113 basepath = SPIFFS_MOUNT_PATH;
114
115 // uncomment to format SPIFFS or SD card
116 //FileBrowser::format(fabgl::DriveType::SPIFFS, 0);
117 //FileBrowser::format(fabgl::DriveType::SDCard, 0);
118
119 FileBrowser fb(basepath);
120
121 //driveA_path = string(basepath) + string("/driveA");
122 //driveB_path = string(basepath) + string("/driveB");
123
124 auto sz = fb.getFullPath("driveA", nullptr, 0);
125 char pathA[sz];
126 fb.getFullPath("driveA", pathA, sz);
127 driveA_path = pathA;
128
129 sz = fb.getFullPath("driveB", nullptr, 0);
130 char pathB[sz];
131 fb.getFullPath("driveB", pathB, sz);
132 driveB_path = pathB;
133
134 if (!fb.exists("driveA", false)) {
135
136 fb.makeDirectory("driveA");
137
138 // create default programs
139 for (int i = 0; i < sizeof(programs) / sizeof(Program); ++i) {
140 term->printf("Creating %s\\%s\r\n", programs[i].path, programs[i].filename);
141 term->flush();
142 // check directory
143 fb.setDirectory(driveA_path.c_str());
144 if (!fb.exists(programs[i].path, false))
145 fb.makeDirectory(programs[i].path);
146 fb.changeDirectory(programs[i].path);
147 // copy file
148 FILE * f = fb.openFile(programs[i].filename, "wb");
149 if (f) {
150 fwrite(programs[i].data, 1, programs[i].size, f);
151 fclose(f);
152 } else {
153 term->write(" FAILED!\r\n");
154 }
155 }
156
157 }
158
159 fb.setDirectory(basepath);
160 if (!fb.exists("driveB", false))
161 fb.makeDirectory("driveB");
162
163 delete term;
164
165 supervisor.onNewSession = [&](HAL * hal) {
166
167 hal->mountDrive(0, driveA_path.c_str());
168 hal->mountDrive(1, driveB_path.c_str());
169
170 // Serial (USB, console UART) -> CP/M AUX1
171 hal->setSerial(0, &Console);
172 };
173
174 // change terminal when pressing F1..F12
175 PS2Controller.keyboard()->onVirtualKey = [&](VirtualKey * vk, bool keyDown) {
176 if (*vk >= VirtualKey::VK_F1 && *vk <= VirtualKey::VK_F12) {
177 if (keyDown) {
178 supervisor.activateSession((int)*vk - VirtualKey::VK_F1);
179 }
180 *vk = VirtualKey::VK_NONE;
181 }
182 };
183
184
185
186}
187
188
189void loop()
190{
191 supervisor.activateSession(0);
192 vTaskDelete(NULL);
193}
194
195
196extern "C" void app_main()
197{
198 setup();
199 while (true)
200 loop();
201}
static bool mountSPIFFS(bool formatOnFail, char const *mountPath, size_t maxFiles=4)
Mounts the SPIFFS (Flash) filesystem.
static bool mountSDCard(bool formatOnFail, char const *mountPath, size_t maxFiles=4, int allocationUnitSize=16 *1024, int MISO=16, int MOSI=17, int CLK=14, int CS=13)
Mounts the filesystem of the SD Card.
FileBrowser provides basic file system operations (directory listing, mkdir, remove and rename).
Definition fabutils.h:698
static int queueSize
Size of the display controller primitives queue.
static int scancodeToVirtualKeyTaskStackSize
Stack size of the task that converts scancodes to virtual keys.
Definition keyboard.h:402
Delegate< VirtualKey *, bool > onVirtualKey
Delegate called whenever a new virtual key is decoded from scancodes.
Definition keyboard.h:389
static void quickCheckHardware()
Disables retries when no mouse is found.
Definition mouse.h:379
static Keyboard * keyboard()
Returns the Keyboard object automatically created by PS2Controller.
static void begin(gpio_num_t port0_clkGPIO, gpio_num_t port0_datGPIO, gpio_num_t port1_clkGPIO=GPIO_UNUSED, gpio_num_t port1_datGPIO=GPIO_UNUSED)
Initializes the PS/2 device controller.
PS/2 device controller.
static int keyboardReaderTaskStackSize
Stack size of the task that reads keys from the keyboard and sends the corresponding ANSI/VT codes to...
Definition terminal.h:1374
static int inputQueueSize
Number of characters the terminal can accept without blocking (increase it if characters are lost on ...
Definition terminal.h:1356
static int inputConsumerTaskStackSize
Stack size of the task that processes the Terminal input stream.
Definition terminal.h:1365
bool begin(BaseDisplayController *displayController, int maxColumns=-1, int maxRows=-1, Keyboard *keyboard=nullptr)
Initializes the terminal.
Definition terminal.cpp:307
An ANSI-VT100 compatible display terminal.
Definition terminal.h:883
bool begin(int port=0, int baud=0, int rxPin=-1, int txPin=-1, int rxBufferSize=1024, int txBufferSize=0)
Initializes the UART.
A Stream implemented on top of the ESP-IDF UART driver.
Definition uartstream.h:68
VGA bitmapped display controller with 16 colors.
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
VirtualKey
Represents each possible real or derived (SHIFT + real) key.
Definition fabutils.h:1374
Single include file of the FabGL library.
#define VGA_640x480_60Hz
Definition fabglconf.h:246