The PS/2 mouse is seen by the PC as a PS/2 mouse: load a DOS mouse driver that supports PS/2 mice (for example CTMOUSE) to use it in DOS programs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include "freertos/FreeRTOS.h"
38#include "freertos/task.h"
39#include "freertos/semphr.h"
40#include "esp_system.h"
41#include <memory>
42
43extern "C" {
44#include "esp_psram.h"
45}
46#include "esp_sntp.h"
47#include "esp_wifi.h"
48
49
52
53#include "mconf.h"
54#include "machine.h"
55
56
57
58
59#define UART_URX 3
60#define UART_UTX 1
61
62
63
64using std::unique_ptr;
65
66using fabgl::StringList;
67using fabgl::imin;
68using fabgl::imax;
69
70
71
74Machine * machine;
75
76
77
78__NOINIT_ATTR static timeval savedTimeValue;
79
80
82static bool wifiConnected = false;
83static bool downloadOK = false;
84
85
86
87bool tryToConnect()
88{
90 if (!connected) {
91 char SSID[32] = "";
92 char psw[32] = "";
93 if (preferences.
getString(
"SSID", SSID,
sizeof(SSID)) && preferences.
getString(
"WiFiPsw", psw,
sizeof(psw))) {
94 ibox.
progressBox(
"",
"Abort",
true, 200, [&](fabgl::ProgressForm * form) {
97 if (!form->update(i * 100 / 32, "Connecting to %s...", SSID))
98 break;
99 vTaskDelay(pdMS_TO_TICKS(500));
100 }
102 });
103
104 if (!connected) {
106 ibox.
message(
"",
"WiFi Connection failed!");
107 }
108 }
109 }
110 return connected;
111}
112
113
114bool checkWiFi()
115{
116 wifiConnected = tryToConnect();
117 if (!wifiConnected) {
118
119
120 if (ibox.
message(
"WiFi Configuration",
"Configure WiFi?",
"No",
"Yes") == InputResult::Enter) {
121
122
123 do {
124
125
126 int networksCount = 0;
128 ibox.
progressBox(
"",
nullptr,
false, 200, [&](fabgl::ProgressForm * form) {
129 form->update(0, "Scanning WiFi networks...");
130 if (scanner.
scan(16))
131 networksCount = scanner.
count();
132 });
133
134
135 if (networksCount > 0) {
136
137
138 StringList list;
139 for (int i = 0; i < networksCount; ++i)
140 list.appendFmt(
"%s (%d dBm)", (
char const *) scanner.
get(i)->ssid, scanner.
get(i)->rssi);
141 int s = ibox.
menu(
"WiFi Configuration",
"Please select a WiFi network", &list);
142
143
144 if (s > -1) {
145
146 char psw[32] = "";
147 if (ibox.
textInput(
"WiFi Configuration",
"Insert WiFi password", psw, 31,
"Cancel",
"OK",
true) == InputResult::Enter) {
148
149 preferences.putString(
"SSID", (
char const *) scanner.
get(s)->ssid);
150 preferences.putString("WiFiPsw", psw);
151 wifiConnected = tryToConnect();
152
153 if (wifiConnected)
154 ibox.
message(
"",
"Connection succeeded!");
155 } else
156 break;
157 } else
158 break;
159 } else {
160
161 ibox.
message(
"",
"No WiFi network found!");
162 break;
163 }
164
165 } while (!wifiConnected);
166
167 }
168
169 }
170
171 return wifiConnected;
172}
173
174
175
176void shutdownHandler()
177{
178
179 gettimeofday(&savedTimeValue, nullptr);
180}
181
182
183void updateDateTime()
184{
185
186 setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
187 tzset();
188
189
190 if (esp_reset_reason() == ESP_RST_SW) {
191
192 savedTimeValue.tv_usec += (int) esp_timer_get_time();
193 savedTimeValue.tv_sec += savedTimeValue.tv_usec / 1000000;
194 savedTimeValue.tv_usec %= 1000000;
195 settimeofday(&savedTimeValue, nullptr);
196 return;
197 }
198
199 if (checkWiFi()) {
200
201
202 ibox.
progressBox(
"",
nullptr,
true, 200, [&](fabgl::ProgressForm * form) {
203 esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
204 esp_sntp_setservername(0, "pool.ntp.org");
205 esp_sntp_init();
206 for (int i = 0; i < 12 && sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED; ++i) {
207 form->update(i * 100 / 12, "Getting date-time from SNTP...");
208 vTaskDelay(pdMS_TO_TICKS(500));
209 }
210 esp_sntp_stop();
212 ibox.
message(
"",
"Date and Time updated. Restarting...");
213 #ifndef FABGL_EMULATED
214 esp_restart();
215 #endif
216 });
217
218 } else {
219
220
221 struct tm tm = { };
222 tm.tm_hour = 8;
223 tm.tm_mday = 14;
224 tm.tm_mon = 7;
225 tm.tm_year = 84;
226 timeval now = { };
227 now.tv_sec = mktime(&tm);
228 settimeofday(&now, nullptr);
229
230 }
231}
232
233
234
235struct DownloadJob {
236 char const * URL;
237 FILE * file;
238 volatile int64_t received;
239 volatile int64_t total;
240 volatile int retries;
241 volatile bool abort;
242 volatile bool success;
243 SemaphoreHandle_t done;
244};
245
246
247
248
249
250static void downloadTask(void * arg)
251{
252 constexpr int MAXRETRIES = 10;
253
254 auto job = (DownloadJob *) arg;
255 auto buf = (uint8_t*) SOC_EXTRAM_DATA_LOW;
256 bool writeError = false;
257 int failures = 0;
258
259
260
261 wifi_ps_type_t savedPS = WIFI_PS_MIN_MODEM;
262 esp_wifi_get_ps(&savedPS);
263 esp_wifi_set_ps(WIFI_PS_NONE);
264
266 while (!job->abort && !writeError && failures <= MAXRETRIES) {
267 if (failures > 0) {
268 job->retries = job->retries + 1;
269 vTaskDelay(pdMS_TO_TICKS(1000));
270 }
271 int64_t offset = job->received;
272 int status = http.
open(job->URL, offset);
273 if (status != 200 && status != 206)
274 ESP_LOGW("download", "open at %lld: HTTP status %d", (long long) offset, status);
275 if (offset == 0 && status == 200) {
277 } else if (offset > 0 && status == 206) {
278
279 } else if (offset > 0 && status == 200) {
280
281 if (fseek(job->file, 0, SEEK_SET) != 0)
282 break;
283 job->received = 0;
285 } else if (status >= 400 && status < 500 && status != 408 && status != 429) {
286 break;
287 } else {
288 ++failures;
290 continue;
291 }
292
293 while (!job->abort) {
294 int c = http.
read(buf, 16384);
295 if (c <= 0) {
296 if (c < 0 || (job->total >= 0 && job->received < job->total))
297 ESP_LOGW("download", "read returned %d at %lld", c, (long long) job->received);
298 break;
299 }
300 if (fwrite(buf, 1, c, job->file) != c) {
301 ESP_LOGE("download", "SD card write failed at %lld (errno %d)", (long long) job->received, errno);
302 writeError = true;
303 break;
304 }
305 job->received = job->received + c;
306 failures = 0;
307 }
309 if (job->total >= 0 ? job->received >= job->total : (job->received > 0 && !writeError)) {
310 job->success = !job->abort && !writeError;
311 break;
312 }
313 ++failures;
314 }
315
316 esp_wifi_set_ps(savedPS);
317 xSemaphoreGive(job->done);
318 vTaskDelete(nullptr);
319}
320
321
322
323bool downloadURL(char const * URL, FILE * file)
324{
325 downloadOK = false;
326 if (!file)
327 return false;
328
329 char const * filename = strrchr(URL, '/') + 1;
330
331 DownloadJob job = { };
332 job.URL = URL;
333 job.file = file;
334 job.total = -1;
335 job.done = xSemaphoreCreateBinary();
336
337 ibox.
progressBox(
"",
"Abort",
true, 380, [&](fabgl::ProgressForm * form) {
338 form->update(0, "Preparing to download %s", filename);
339
340 if (xTaskCreatePinnedToCore(downloadTask, "download", 8192, &job, 5, nullptr, CoreUsage::quietCore() < 0 ? 0 : CoreUsage::quietCore()) != pdPASS)
341 return;
342 while (xSemaphoreTake(job.done, pdMS_TO_TICKS(250)) == pdFALSE) {
343 int64_t received = job.received;
344 int64_t total = job.total;
345 bool cont;
346 if (job.retries > 0)
347 cont = form->update(total > 0 ? received * 100 / total : 0, "Downloading %s (%.2f / %.2f MB) retry %d", filename, received / 1048576.0, total / 1048576.0, job.retries);
348 else
349 cont = form->update(total > 0 ? received * 100 / total : 0, "Downloading %s (%.2f / %.2f MB)", filename, received / 1048576.0, total / 1048576.0);
350 if (!cont)
351 job.abort = true;
352 }
353 downloadOK = job.success;
354 });
355
356 vSemaphoreDelete(job.done);
357 return downloadOK;
358}
359
360
361
362char const * getDisk(char const * url)
363{
365
366 char const * filename = nullptr;
367 if (url) {
368 if (strncmp("http://", url, 7) == 0 || strncmp("https://", url, 7) == 0) {
369
370 filename = strrchr(url, '/') + 1;
371 if (filename && !fb.exists(filename, false)) {
372
373 if (!checkWiFi())
374 return nullptr;
375 auto file = fb.openFile(filename, "wb");
376 bool success = downloadURL(url, file);
377 fclose(file);
378 if (!success) {
379 fb.remove(filename);
380 return nullptr;
381 }
382 }
383 } else {
384
385 if (fb.filePathExists(url))
386 filename = url;
387 }
388 }
389 return filename;
390}
391
392
393
394void sysReqCallback()
395{
396 machine->graphicsAdapter()->enableVideo(false);
398
399 int s = ibox.
menu(
"",
"Select a command",
"Restart Emulator;Restart Machine;Mount Disk;Continue");
400 switch (s) {
401
402
403 case 0:
404 esp_restart();
405 break;
406
407
408 case 1:
409 machine->trigReset();
410 break;
411
412
413 case 2:
414 {
415 int s = ibox.
menu(
"",
"Select Drive",
"Floppy A (fd0);Floppy B (fd1)");
416 if (s > -1) {
417 constexpr int MAXNAMELEN = 256;
418 unique_ptr<char[]> dir(new char[MAXNAMELEN + 1] { '/', 'S', 'D', 0 } );
419 unique_ptr<char[]> filename(new char[MAXNAMELEN + 1] { 0 } );
420 if (machine->diskFilename(s))
421 strcpy(filename.get(), machine->diskFilename(s));
422 if (ibox.
fileSelector(
"Select Disk Image",
"Image Filename", dir.get(), MAXNAMELEN, filename.get(), MAXNAMELEN) == InputResult::Enter) {
423 machine->setDriveImage(s, filename.get());
424 }
425 }
426 break;
427 }
428
429
430 default:
431 break;
432 }
433
435 PS2Controller::keyboard()->enableVirtualKeys(false, false);
436 machine->graphicsAdapter()->enableVideo(true);
437}
438
439
440void setup()
441{
442 vTaskDelay(pdMS_TO_TICKS(500)); printf("\n\n\nReset\n\n");
443
444
445
446 preferences.
begin(
"PCEmulator",
false);
447
448
449
450
451
453
456
458
459
460
461
462 if (!esp_psram_is_initialized())
463 ibox.
message(
"Error!",
"This app requires a board with PSRAM!",
nullptr,
nullptr);
464
466 ibox.
message(
"Error!",
"This app requires a SD-CARD!",
nullptr,
nullptr);
467
468
469
470
471 esp_register_shutdown_handler(shutdownHandler);
472
473 updateDateTime();
474
475
476
477 MachineConf mconf;
478
479
480
482 int idx = preferences.getInt("dconf", 0);
483
484 for (bool showDialog = true; showDialog; ) {
485
486 loadMachineConfiguration(&mconf);
487
488 StringList dconfs;
489 for (auto conf = mconf.getFirstItem(); conf; conf = conf->next)
490 dconfs.append(conf->desc);
491 dconfs.select(idx, true);
492
494 ibox.
setupButton(1,
"Machine",
"Edit;New;Remove", 52);
495 auto r = ibox.
select(
"Machine Configurations",
"Please select a machine configuration", &dconfs,
nullptr,
"Run");
496
497 idx = dconfs.getFirstSelected();
498
499 switch (r) {
500 case InputResult::ButtonExt0:
501
503 break;
504 case InputResult::ButtonExt1:
505
507
508 case 0:
509 editConfigDialog(&ibox, &mconf, idx);
510 break;
511
512 case 1:
513 newConfigDialog(&ibox, &mconf, idx);
514 break;
515
516 case 2:
517 delConfigDialog(&ibox, &mconf, idx);
518 break;
519 };
520 break;
521 case InputResult::Enter:
522
523 showDialog = false;
524 break;
525 default:
526 break;
527 }
528
529
531 }
532
533 idx = imax(idx, 0);
534 preferences.putInt("dconf", idx);
535
536
537 auto conf = mconf.getItem(idx);
538
539 char const * diskFilename[DISKCOUNT];
540 downloadOK = true;
541 for (int i = 0; i < DISKCOUNT && downloadOK; ++i)
542 diskFilename[i] = getDisk(conf->disk[i]);
543
544 if (!downloadOK || (!diskFilename[0] && !diskFilename[2])) {
545
546 ibox.
message(
"Error!",
"Unable to get system disks!");
547 esp_restart();
548 }
549
550 if (wifiConnected) {
551
553 ibox.
message(
"",
"Disks downloaded. Restarting...");
554 #ifndef FABGL_EMULATED
555 esp_restart();
556 #endif
557 }
558
560
561
562 FileBrowser::setSDCardMaxFreqKHz(SDMMC_FREQ_DEFAULT);
564
565 machine = new Machine;
566
567 machine->setBaseDirectory(SD_MOUNT_PATH);
568 for (int i = 0; i < DISKCOUNT; ++i)
569 machine->setDriveImage(i, diskFilename[i], conf->cylinders[i], conf->heads[i], conf->sectors[i]);
570
571 machine->setBootDrive(conf->bootDrive);
572
573 auto serial1 = new SerialPort;
574 serial1->setSignals(UART_URX, UART_UTX);
575 machine->setCOM1(serial1);
576
577
578
579
580
581
582
583
584
585 machine->setSysReqCallback(sysReqCallback);
586
587 machine->run();
588}
589
590
591
592#if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
593namespace fabgl {
594 extern volatile uint64_t s_vgapalctrlcycles;
595}
596using fabgl::s_vgapalctrlcycles;
597#endif
598
599
600
601void loop()
602{
603
604#if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK
605 static uint32_t tcpu = 0, s1 = 0, count = 0;
606 tcpu = machine->ticksCounter();
607 s_vgapalctrlcycles = 0;
608 s1 = fabgl::getCycleCount();
609 vTaskDelay(pdMS_TO_TICKS(1000));
610 printf("%d\tCPU: %d", count, machine->ticksCounter() - tcpu);
611 printf(" Graph: %lld / %d (%d%%)\n", s_vgapalctrlcycles, fabgl::getCycleCount() - s1, (int)((double)s_vgapalctrlcycles/240000000*100));
612 ++count;
613#else
614
615 vTaskDelete(NULL);
616
617#endif
618}
619
620
621extern "C" void app_main()
622{
623 setup();
624 while (true)
625 loop();
626}
Provides a set of drawing methods.
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.
static bool remountSDCard()
Mounts the SD Card filesystem again, using the same parameters.
FileBrowser provides basic file system operations (directory listing, mkdir, remove and rename).
size_t getString(char const *key, char *value, size_t maxLen)
Gets a string.
bool begin(char const *name, bool readOnly=false)
Opens the specified namespace.
Simple key/value storage in non-volatile flash (NVS).
static int queueSize
Size of the display controller primitives queue.
int open(char const *URL, int64_t rangeStart=0)
Opens a streaming GET request (follows redirects).
int contentLength()
Gets the content length.
void close()
Closes the HTTP connection.
int read(void *buffer, int length)
Reads content after a successful open().
HTTP connection and request helper.
WiFiConnectionState connect(char const *ssid, char const *password, int waitConnectionTimeOutMS=10000)
Tries to connect to a WiFi network.
WiFiConnectionState state()
Returns the connection state.
void disconnect()
Disconnects from the WiFi network.
WiFi connection helper class.
int count()
Returns the number of networks found (limited to the requested maximum).
bool scan(int maxItems=8, bool justCount=false)
Performs a WiFi scan.
wifi_ap_record_t * get(int index)
Returns information about the specified network.
WiFi scanner helper class.
Single include file of the FabGL library.
This file contains the fabgl::WiFiScanner, fabgl::WiFiConnection, fabgl::HTTPRequest and fabgl::TCPCl...
Represents a 24-bit RGB color.