Download firmware/dosvga32/machine.cpp from HelloSun/esp32emu: direct link, hf CLI and curl.
- Browser
- Download file 30.9 kB
-
https://huggingface.co/spaces/HelloSun/esp32emu/resolve/main/firmware/dosvga32/machine.cpp
- Command line
-
hf download hf://spaces/HelloSun/esp32emu/firmware/dosvga32/machine.cpp
-
curl -L -o machine.cpp https://huggingface.co/spaces/HelloSun/esp32emu/resolve/main/firmware/dosvga32/machine.cpp
30.9 kB
/*
Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
Copyright (c) 2019-2022 Fabrizio Di Vittorio.
All rights reserved.
* Please contact fdivitto2013@gmail.com if you need a commercial license.
* This library and related software is available under GPL v3.
FabGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FabGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FabGL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "machine.h"
#include "diskpart.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <math.h>
#pragma GCC optimize ("O3")
// CGA Craphics Card Ports Bits
#define CGA_MODECONTROLREG_TEXT80 0x01 // 0 = 40x25, 1 = 80x25
#define CGA_MODECONTROLREG_GRAPHICS 0x02 // 0 = text, 1 = graphics
#define CGA_MODECONTROLREG_COLOR 0x04 // 0 = color, 1 = monochrome
#define CGA_MODECONTROLREG_ENABLED 0x08 // 0 = video off, 1 = video on
#define CGA_MODECONTROLREG_GRAPH640 0x10 // 0 = 320x200 graphics, 1 = 640x200 graphics
#define CGA_MODECONTROLREG_BIT7BLINK 0x20 // 0 = text mode bit 7 controls background, 1 = text mode bit 7 controls blinking
#define CGA_COLORCONTROLREG_BACKCOLR_MASK 0x0f // mask for 320x200 background color index (on 640x200 is the foreground)
#define CGA_COLORCONTROLREG_HIGHINTENSITY 0x10 // select high intensity colors
#define CGA_COLORCONTROLREG_PALETTESEL 0x20 // 0 is Green, red and brown, 1 is Cyan, magenta and white
// Hercules (HGC) Ports Bits
#define HGC_MODECONTROLREG_GRAPHICS 0x02 // 0 = text mode, 1 = graphics mode
#define HGC_MODECONTROLREG_ENABLED 0x08 // 0 = video off, 1 = video on
#define HGC_MODECONTROLREG_BIT7BLINK 0x20 // 0 = text mode bit 7 controls background, 1 = text mode bit 7 controls blinking
#define HGC_MODECONTROLREG_GRAPHICSPAGE 0x80 // 0 = graphics mapped on page 0 (0xB0000), 1 = graphics mapped on page 1 (0xB8000)
#define HGC_CONFSWITCH_ALLOWGRAPHICSMODE 0x01 // 0 = prevents graphics mode, 1 = allows graphics mode
#define HGC_CONFSWITCH_ALLOWPAGE1 0x02 // 0 = prevents access to page 1, 1 = allows access to page 1
// I/O expander (based on MCP23S17) ports
#define EXTIO_CONFIG 0x00e0 // configuration port (see EXTIO_CONFIG_.... flags)
// whole 8 bit ports handling
#define EXTIO_DIRA 0x00e1 // port A direction (0 = input, 1 = output)
#define EXTIO_DIRB 0x00e2 // port B direction (0 = input, 1 = output)
#define EXTIO_PULLUPA 0x00e3 // port A pullup enable (0 = disabled, 1 = enabled)
#define EXTIO_PULLUPB 0x00e4 // port B pullup enable (0 = disabled, 1 = enabled)
#define EXTIO_PORTA 0x00e5 // port A read/write
#define EXTIO_PORTB 0x00e6 // port B read/write
// single GPIO handling
#define EXTIO_GPIOSEL 0x00e7 // GPIO selection (0..7 = PA0..PA7, 8..15 = PB0..PB8)
#define EXTIO_GPIOCONF 0x00e8 // selected GPIO direction and pullup (0 = input, 1 = output, 2 = input with pullup)
#define EXTIO_GPIO 0x00e9 // selected GPIO read or write (0 = low, 1 = high)
// I/O expander configuration bits
#define EXTIO_CONFIG_AVAILABLE 0x01 // 1 = external IO available, 0 = not available
#define EXTIO_CONFIG_INT_POLARITY 0x02 // 1 = positive polarity, 0 = negative polarity (default)
//////////////////////////////////////////////////////////////////////////////////////
// Machine
using fabgl::i8086;
uint8_t * Machine::s_memory;
uint8_t * Machine::s_videoMemory;
Machine::Machine() :
#ifdef FABGL_EMULATED
m_stepCallback(nullptr),
#endif
m_diskFilename(),
m_disk(),
m_diskChanged(),
m_frameBuffer(nullptr),
m_bootDrive(0),
m_sysReqCallback(nullptr),
m_baseDir(nullptr)
{
}
Machine::~Machine()
{
for (int i = 0; i < DISKCOUNT; ++i) {
free(m_diskFilename[i]);
if (m_disk[i])
fclose(m_disk[i]);
}
vTaskDelete(m_taskHandle);
free(s_videoMemory);
}
void Machine::init()
{
srand((uint32_t)time(NULL));
// to avoid PSRAM bug without -mfix-esp32-psram-cache-issue
// core 0 can only work reliably with the lower 2 MB and core 1 only with the higher 2 MB.
s_memory = (uint8_t*)(SOC_EXTRAM_DATA_LOW + (xPortGetCoreID() == 1 ? 2 * 1024 * 1024 : 0));
s_videoMemory = (uint8_t*)heap_caps_malloc(VIDEOMEMSIZE, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
memset(s_memory, 0, RAM_SIZE);
m_soundGen.play(true);
m_soundGen.attach(&m_sinWaveGen);
m_i8042.init();
printf("kbd init ok\n");
m_i8042.setCallbacks(this, keyboardInterrupt, mouseInterrupt, resetMachine, sysReq);
m_PIT8253.setCallbacks(this, PITChangeOut);
m_PIT8253.reset();
m_MC146818.init("PCEmulator");
m_MC146818.setCallbacks(this, MC146818Interrupt);
// MCP23S17 I/O expander not used (no SPI probing on VGA32)
m_MCP23S17Sel = 0;
m_BIOS.init(this);
printf("bios loaded (ROM at F000:0100)\n", 0u);
// floppy A: backed by flash partition 'dosdisk' (+ RAM write overlay in unused PSRAM)
setDriveImage(0, "dosdisk", 0, 0, 0);
printf("disk %s, size=%u geometry C=%d H=%d S=%d\n", m_disk[0] ? "ok" : "FAILED", (unsigned)m_diskSize[0], m_diskCylinders[0], m_diskHeads[0], m_diskSectors[0]);
i8086::setCallbacks(this, readPort, writePort, writeVideoMemory8, writeVideoMemory16, readVideoMemory8, readVideoMemory16, interrupt);
i8086::setMemory(s_memory);
m_reset = true;
printf("cpu running on core %d\n", xPortGetCoreID());
}
void Machine::setDriveImage(int drive, char const * filename, int cylinders, int heads, int sectors)
{
if (m_disk[drive]) {
fclose(m_disk[drive]);
m_disk[drive] = nullptr;
}
if (m_diskFilename[drive]) {
free(m_diskFilename[drive]);
m_diskFilename[drive] = nullptr;
}
m_BIOS.setDriveMediaType(drive, mediaUnknown);
m_diskCylinders[drive] = cylinders;
m_diskHeads[drive] = heads;
m_diskSectors[drive] = sectors;
m_diskChanged[drive] = true;
if (filename) {
m_diskFilename[drive] = strdup(filename);
m_disk[drive] = openPartitionDisk(s_memory + RAM_SIZE, 1048576, &m_diskSizeTmp);
if (m_disk[drive]) {
// get image file size
m_diskSize[drive] = m_diskSizeTmp;
// need to detect geometry?
if (cylinders == 0 || heads == 0 || sectors == 0)
autoDetectDriveGeometry(drive);
}
}
}
void Machine::autoDetectDriveGeometry(int drive)
{
// well known floppy formats
static const struct {
uint16_t tracks;
uint8_t sectors;
uint8_t heads;
} FLOPPYFORMATS[] = {
{ 40, 8, 1 }, // 163840 bytes (160K, 5.25 inch)
{ 40, 9, 1 }, // 184320 bytes (180K, 5.25 inch)
{ 40, 8, 2 }, // 327680 bytes (320K, 5.25 inch)
{ 40, 9, 2 }, // 368640 bytes (360K, 5.25 inch)
{ 80, 9, 2 }, // 737280 bytes (720K, 3.5 inch)
{ 80, 15, 2 }, // 1228800 bytes (1200K, 5.25 inch)
{ 80, 18, 2 }, // 1474560 bytes (1440K, 3.5 inch)
{ 80, 36, 2 }, // 2949120 bytes (2880K, 3.5 inch)
};
// look for well known floppy formats
for (auto const & ff : FLOPPYFORMATS) {
if (512 * ff.tracks * ff.sectors * ff.heads == m_diskSize[drive]) {
m_diskCylinders[drive] = ff.tracks;
m_diskHeads[drive] = ff.heads;
m_diskSectors[drive] = ff.sectors;
//printf("autoDetectDriveGeometry, found floppy: t=%d s=%d h=%d\n", ff.tracks, ff.sectors, ff.heads);
return;
}
}
// maybe an hard disk, try to calculate geometry (max 528MB, common lower end for BIOS and MSDOS: https://tldp.org/HOWTO/Large-Disk-HOWTO-4.html)
constexpr int MAXCYLINDERS = 1024; // Cylinders : 1...1024
constexpr int MAXHEADS = 16; // Heads : 1...16 (actual limit is 256)
constexpr int MAXSECTORS = 63; // Sectors : 1...63
int c = 1, h = 1;
int s = (int)(m_diskSize[drive] / 512);
if (s > MAXSECTORS) {
h = s / MAXSECTORS;
s = MAXSECTORS;
}
if (h > MAXHEADS) {
c = h / MAXHEADS;
h = MAXHEADS;
}
if (c > MAXCYLINDERS)
c = MAXCYLINDERS;
m_diskCylinders[drive] = c;
m_diskHeads[drive] = h;
m_diskSectors[drive] = s;
//printf("autoDetectDriveGeometry, found HD: c=%d h=%d s=%d (tot=%d filesz=%d)\n", m_diskCylinders[drive], m_diskHeads[drive], m_diskSectors[drive], 512 * m_diskCylinders[drive] * m_diskHeads[drive] * m_diskSectors[drive], (int)m_diskSize[drive]);
}
void Machine::setCOM1(SerialPort * serialPort)
{
m_COM1.setCallbacks(this, COM1Interrupt);
m_COM1.setSerialPort(serialPort);
}
void Machine::setCOM2(SerialPort * serialPort)
{
m_COM2.setCallbacks(this, COM2Interrupt);
m_COM2.setSerialPort(serialPort);
}
void Machine::reset()
{
m_reset = false;
m_ticksCounter = 0;
m_CGAMemoryOffset = 0;
m_CGAModeReg = 0;
m_CGAColorReg = 0;
m_CGAVSyncQuery = 0;
m_HGCMemoryOffset = 0;
m_HGCModeReg = 0;
m_HGCSwitchReg = 0;
m_HGCVSyncQuery = 0;
m_speakerDataEnable = false;
m_i8042.reset();
m_PIC8259A.reset();
m_PIC8259B.reset();
m_PIT8253.reset();
m_PIT8253.setGate(0, true);
//m_PIT8253.setGate(1, true); // @TODO: timer 1 used for DRAM refresh, required to run?
m_MC146818.reset();
m_COM1.reset();
m_COM2.reset();
memset(m_CGA6845, 0, sizeof(m_CGA6845));
memset(m_HGC6845, 0, sizeof(m_HGC6845));
m_BIOS.reset();
i8086::reset();
// set boot drive (0, 1, 0x80, 0x81)
i8086::setDL((m_bootDrive & 1) | (m_bootDrive > 1 ? 0x80 : 0x00));
}
void Machine::run()
{
xTaskCreatePinnedToCore(&runTask, "", 4000, this, 5, &m_taskHandle, CoreUsage::quietCore());
}
void Machine::runTask(void * pvParameters)
{
auto m = (Machine*)pvParameters;
m->init();
while (true) {
if (m->m_reset)
m->reset();
#ifdef FABGL_EMULATED
taskEmuCheck();
if (m->m_stepCallback)
m->m_stepCallback(m);
#endif
i8086::step();
m->tick();
}
}
void Machine::tick()
{
++m_ticksCounter;
if ((m_ticksCounter & 0x03) == 0x03) {
if (m_COM1.assigned())
m_COM1.tick();
if (m_COM2.assigned())
m_COM2.tick();
if ((m_ticksCounter & 0x7f) == 0x7f) {
m_PIT8253.tick();
// run keyboard controller every PIT tick (just to not overload CPU with continous checks)
m_i8042.tick();
}
}
if (m_PIC8259A.pendingInterrupt() && i8086::IRQ(m_PIC8259A.pendingInterruptNum()))
m_PIC8259A.ackPendingInterrupt();
if (m_PIC8259B.pendingInterrupt() && i8086::IRQ(m_PIC8259B.pendingInterruptNum()))
m_PIC8259B.ackPendingInterrupt();
}
void Machine::setCGA6845Register(uint8_t value)
{
m_CGA6845[m_CGA6845SelectRegister] = value;
switch (m_CGA6845SelectRegister) {
// cursor start: (bits 5,6 = cursor blink and visibility control), bits 0..4 cursor start scanline
case 0x0a:
m_graphicsAdapter.setCursorVisible((m_CGA6845[0xa] >> 5) >= 2);
// no break!
// cursor end: bits 0..4 cursor end scanline
case 0x0b:
m_graphicsAdapter.setCursorShape(2 * (m_CGA6845[0xa] & 0x1f), 2 * (m_CGA6845[0xb] & 0x1f));
break;
// video memory start offset (0x0c = H, 0x0d = L)
case 0x0c:
case 0x0d:
m_CGAMemoryOffset = ((m_CGA6845[0xc] << 8) | m_CGA6845[0xd]) << 1;
setCGAMode();
break;
// cursor position (0x0e = H and 0x0f = L)
case 0x0e:
case 0x0f:
{
int pos = (m_CGA6845[0xe] << 8) | m_CGA6845[0xf];
if (m_graphicsAdapter.getTextColumns() == 0) break;
m_graphicsAdapter.setCursorPos(pos / m_graphicsAdapter.getTextColumns(), pos % m_graphicsAdapter.getTextColumns());
break;
}
}
}
void Machine::setCGAMode()
{
#ifdef DOSVGA_NO_VIDEO
return; // QEMU test build: no VGA/I2S emulation
#endif
if ((m_CGAModeReg & CGA_MODECONTROLREG_ENABLED) == 0) {
// video disabled
//printf("CGA, video disabled\n");
m_graphicsAdapter.enableVideo(false);
} else if ((m_CGAModeReg & CGA_MODECONTROLREG_TEXT80) == 0 && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) == 0) {
// 40 column text mode
//printf("CGA, 40 columns text mode\n");
m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Text_40x25_16Colors);
m_graphicsAdapter.setBit7Blink(m_CGAModeReg & CGA_MODECONTROLREG_BIT7BLINK);
m_graphicsAdapter.enableVideo(true);
} else if ((m_CGAModeReg & CGA_MODECONTROLREG_TEXT80) && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) == 0) {
// 80 column text mode
//printf("CGA, 80 columns text mode\n");
m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Text_80x25_16Colors);
m_graphicsAdapter.setBit7Blink(m_CGAModeReg & CGA_MODECONTROLREG_BIT7BLINK);
m_graphicsAdapter.enableVideo(true);
} else if ((m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPH640) == 0) {
// 320x200 graphics
//printf("CGA, 320x200 graphics mode\n");
m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Graphics_320x200_4Colors);
int paletteIndex = (bool)(m_CGAColorReg & CGA_COLORCONTROLREG_PALETTESEL) * 2 + (bool)(m_CGAColorReg & CGA_COLORCONTROLREG_HIGHINTENSITY);
m_graphicsAdapter.setPCGraphicsPaletteInUse(paletteIndex);
m_graphicsAdapter.setPCGraphicsBackgroundColorIndex(m_CGAColorReg & CGA_COLORCONTROLREG_BACKCOLR_MASK);
m_graphicsAdapter.enableVideo(true);
} else if ((m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPH640)) {
// 640x200 graphics
//printf("CGA, 640x200 graphics mode\n");
m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Graphics_640x200_2Colors);
m_graphicsAdapter.setPCGraphicsForegroundColorIndex(m_CGAColorReg & CGA_COLORCONTROLREG_BACKCOLR_MASK);
m_graphicsAdapter.enableVideo(true);
}
}
void Machine::setHGC6845Register(uint8_t value)
{
m_HGC6845[m_HGC6845SelectRegister] = value;
switch (m_HGC6845SelectRegister) {
// cursor start: (bits 5,6 = cursor blink and visibility control), bits 0..4 cursor start scanline
case 0x0a:
m_graphicsAdapter.setCursorVisible((m_HGC6845[0xa] >> 5) >= 2);
// cursor end: bits 0..4 cursor end scanline
case 0x0b:
m_graphicsAdapter.setCursorShape((m_HGC6845[0xa] & 0x1f), (m_HGC6845[0xb] & 0x1f));
break;
// video memory start offset (0x0c = H, 0x0d = L)
case 0x0c:
case 0x0d:
m_HGCMemoryOffset = ((m_HGC6845[0xc] << 8) | m_HGC6845[0xd]) << 1;
setHGCMode();
break;
// cursor position (0x0e = H and 0x0f = L)
case 0x0e:
case 0x0f:
{
int pos = (m_HGC6845[0xe] << 8) | m_HGC6845[0xf];
m_graphicsAdapter.setCursorPos(pos / m_graphicsAdapter.getTextColumns(), pos % m_graphicsAdapter.getTextColumns());
break;
}
}
}
void Machine::setHGCMode()
{
#ifdef DOSVGA_NO_VIDEO
return; // QEMU test build: no VGA/I2S emulation
#endif
constexpr int HGC_OFFSET_PAGE0 = 0x0000;
constexpr int HGC_OFFSET_PAGE1 = 0x8000;
if ((m_HGCModeReg & HGC_MODECONTROLREG_ENABLED) == 0) {
// video disabled
//printf("Hercules, video disabled\n");
m_graphicsAdapter.enableVideo(false);
} else if ((m_HGCModeReg & HGC_MODECONTROLREG_GRAPHICS) == 0 || (m_HGCSwitchReg & HGC_CONFSWITCH_ALLOWGRAPHICSMODE) == 0) {
// text mode
//printf("Hercules, text mode\n");
m_frameBuffer = s_videoMemory + HGC_OFFSET_PAGE0;
m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Text_80x25_16Colors);
m_graphicsAdapter.setBit7Blink(m_HGCModeReg & HGC_MODECONTROLREG_BIT7BLINK);
m_graphicsAdapter.enableVideo(true);
} else if ((m_HGCModeReg &HGC_MODECONTROLREG_GRAPHICS)) {
// graphics mode
//printf("Hercules, graphics mode\n");
int offset = (m_HGCModeReg & HGC_MODECONTROLREG_GRAPHICSPAGE) && (m_HGCSwitchReg & HGC_CONFSWITCH_ALLOWPAGE1) ? HGC_OFFSET_PAGE1 : HGC_OFFSET_PAGE0;
m_frameBuffer = s_videoMemory + offset;
m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Graphics_HGC_720x348);
m_graphicsAdapter.enableVideo(true);
}
}
void Machine::writePort(void * context, int address, uint8_t value)
{
auto m = (Machine*)context;
switch (address) {
// PIC8259A
case 0x20:
case 0x21:
m->m_PIC8259A.write(address & 1, value);
break;
// PIC8259B
case 0xa0:
case 0xa1:
m->m_PIC8259B.write(address & 1, value);
break;
// PIT8253
case 0x0040:
case 0x0041:
case 0x0042:
case 0x0043:
m->m_PIT8253.write(address & 3, value);
if ((address == 0x43 && (value >> 6) == 2) || address == 0x42)
m->speakerSetFreq();
break;
// 8042 keyboard controller input
case 0x0060:
m->m_i8042.write(0, value);
break;
// PortB
// bit 1 : speaker data enable
// bit 0 : timer 2 gate
case 0x0061:
m->m_speakerDataEnable = value & 0x02;
m->m_PIT8253.setGate(2, value & 0x01);
m->speakerEnableDisable();
break;
// 8042 keyboard controller input
case 0x0064:
m->m_i8042.write(1, value);
break;
// MC146818 RTC & RAM
case 0x0070:
case 0x0071:
m->m_MC146818.write(address & 1, value);
break;
// COM2
case 0x2f8 ... 0x2ff:
if (m->m_COM2.assigned())
m->m_COM2.write(address, value);
break;
// CGA - CRT 6845 - register selection register
case 0x3d4:
m->m_CGA6845SelectRegister = value;
break;
// CGA - CRT 6845 - selected register write
case 0x3d5:
m->setCGA6845Register(value);
break;
// CGA - Mode Control Register
case 0x3d8:
m->m_CGAModeReg = value;
m->setCGAMode();
break;
// CGA - Color Select register
case 0x3d9:
m->m_CGAColorReg = value;
m->setCGAMode();
break;
// Hercules (HGC) - CRT 6845 - register selection register
case 0x3b4:
m->m_HGC6845SelectRegister = value;
break;
// Hercules (HGC) - CRT 6845 - selected register write
case 0x3b5:
m->setHGC6845Register(value);
break;
// Hercules (HGC) - Display Mode Control Port
case 0x3b8:
m->m_HGCModeReg = value;
m->setHGCMode();
break;
// Hercules (HGC) - Configuration Switch
case 0x3bf:
m->m_HGCSwitchReg = value;
m->setHGCMode();
break;
// COM1
case 0x3f8 ... 0x3ff:
if (m->m_COM1.assigned())
m->m_COM1.write(address, value);
break;
// I/O expander - Configuration
case EXTIO_CONFIG:
m->m_MCP23S17.setINTActiveHigh(value & EXTIO_CONFIG_INT_POLARITY);
break;
// I/O expander - Port A/B Direction
case EXTIO_DIRA ... EXTIO_DIRB:
m->m_MCP23S17.setPortDir(address - EXTIO_DIRA + MCP_PORTA, ~value);
printf("dir %d = %02X\n", address - EXTIO_DIRA + MCP_PORTA, ~value);
break;
// I/O expander - Port A/B pullup
case EXTIO_PULLUPA ... EXTIO_PULLUPB:
m->m_MCP23S17.enablePortPullUp(address - EXTIO_PULLUPA + MCP_PORTA, value);
break;
// I/O expander - Port A/B write
case EXTIO_PORTA ... EXTIO_PORTB:
m->m_MCP23S17.writePort(address - EXTIO_PORTA + MCP_PORTA, value);
printf("set %d = %02X\n", address - EXTIO_PORTA + MCP_PORTA, value);
break;
// I/O expander - GPIO selection
case EXTIO_GPIOSEL:
m->m_MCP23S17Sel = value & 0xf;
break;
// I/O expander - GPIO direction and pullup
case EXTIO_GPIOCONF:
m->m_MCP23S17.configureGPIO(m->m_MCP23S17Sel, value & 1 ? fabgl::MCPDir::Output : fabgl::MCPDir::Input, value & 2);
break;
// I/O expander - GPIO write
case EXTIO_GPIO:
m->m_MCP23S17.writeGPIO(m->m_MCP23S17Sel, value);
break;
default:
//printf("OUT %04x=%02x\n", address, value);
break;
}
}
uint8_t Machine::readPort(void * context, int address)
{
auto m = (Machine*)context;
switch (address) {
// PIC8259A
case 0x0020:
case 0x0021:
return m->m_PIC8259A.read(address & 1);
// PIC8259B
case 0x00a0:
case 0x00a1:
return m->m_PIC8259B.read(address & 1);
// PIT8253
case 0x0040:
case 0x0041:
case 0x0042:
case 0x0043:
return m->m_PIT8253.read(address & 3);
// 8042 keyboard controller output
case 0x0060:
return m->m_i8042.read(0);
// Port B
// bit 5 : timer 2 out
// bit 4 : toggles every 15.085us (DMA refresh)
// bit 1 : speaker data enable
// bit 0 : timer 2 gate
case 0x0061:
return ((int)m->m_PIT8253.getOut(2) << 5) | // bit 5
(esp_timer_get_time() & 0x10) | // bit 4 (toggles every 16us)
((int)m->m_speakerDataEnable << 1) | // bit 1
((int)m->m_PIT8253.getGate(2)); // bit 0
// I/O port
case 0x0062:
return 0x20 * m->m_PIT8253.getOut(2); // bit 5 = timer 2 output
// 8042 keyboard controller status register
case 0x0064:
return m->m_i8042.read(1);
// MC146818 RTC & RAM
case 0x0070:
case 0x0071:
return m->m_MC146818.read(address & 1);
// COM2
case 0x2f8 ... 0x2ff:
return m->m_COM2.assigned() ? m->m_COM2.read(address) : 0;
// CGA - CRT 6845 - register selection register
case 0x3d4:
return 0x00; // not readable
// CGA - CRT 6845 - selected register read
case 0x3d5:
return m->m_CGA6845SelectRegister >= 14 && m->m_CGA6845SelectRegister < 16 ? m->m_CGA6845[m->m_CGA6845SelectRegister] : 0x00;
// CGA - Color Select register
// note: this register should be write-only, but some games do not work if it isn't readable
case 0x3d9:
return m->m_CGAColorReg;
// CGA - Status Register
// real vertical sync is too fast for our slowly emulated 8086, so
// here it is just a fake, just to allow programs that check it to keep going anyway.
case 0x3da:
m->m_CGAVSyncQuery += 1;
return (m->m_CGAVSyncQuery & 0x7) != 0 ? 0x09 : 0x00; // "not VSync" (0x00) every 7 queries
// Hercules (HGC) - register selection register
case 0x3b4:
return 0x00; // not readable
// Hercules (HGC) - selected register read
case 0x3b5:
return m->m_HGC6845SelectRegister >= 14 && m->m_HGC6845SelectRegister < 16 ? m->m_HGC6845[m->m_HGC6845SelectRegister] : 0x00;
// Hercules (HGC) - Display Status Port
// real vertical sync is too fast for our slowly emulated 8086, so
// here it is just a fake, just to allow programs that check it to keep going anyway.
case 0x3ba:
m->m_HGCVSyncQuery += 1;
return (m->m_HGCVSyncQuery & 0x7) != 0 ? 0x00 : 0x80; // "not VSync" (0x80) every 7 queries
// COM1
case 0x3f8 ... 0x3ff:
return m->m_COM1.assigned() ? m->m_COM1.read(address) : 0;
// I/O expander - Configuration
case EXTIO_CONFIG:
return (m->m_MCP23S17.available() ? EXTIO_CONFIG_AVAILABLE : 0) |
(m->m_MCP23S17.getINTActiveHigh() ? EXTIO_CONFIG_INT_POLARITY : 0);
// I/O expander - Port A/B Direction
case EXTIO_DIRA ... EXTIO_DIRB:
return m->m_MCP23S17.getPortDir(address - EXTIO_DIRA + MCP_PORTA);
// I/O expander - Port A/B pullup
case EXTIO_PULLUPA ... EXTIO_PULLUPB:
return m->m_MCP23S17.getPortPullUp(address - EXTIO_PULLUPA + MCP_PORTA);
// I/O expander - Port A/B read
case EXTIO_PORTA ... EXTIO_PORTB:
return m->m_MCP23S17.readPort(address - EXTIO_PORTA + MCP_PORTA);
// I/O expander - GPIO selection
case EXTIO_GPIOSEL:
return m->m_MCP23S17Sel;
// I/O expander - GPIO read
case EXTIO_GPIO:
return m->m_MCP23S17.readGPIO(m->m_MCP23S17Sel);
}
//printf("IN %04X\n", address);
return 0xff;
}
void Machine::PITChangeOut(void * context, int timerIndex)
{
auto m = (Machine*)context;
// timer 0 trigged?
if (timerIndex == 0 && m->m_PIT8253.getOut(0) == true) {
// yes, report 8259A-IR0 (IRQ0, INT 08h)
m->m_PIC8259A.signalInterrupt(0);
}
}
// reset from 8042
bool Machine::resetMachine(void * context)
{
auto m = (Machine*)context;
m->trigReset();
return true;
}
// SYSREQ (ALT + PRINTSCREEN)
bool Machine::sysReq(void * context)
{
auto m = (Machine*)context;
if (m->m_sysReqCallback)
m->m_sysReqCallback();
return true;
}
// 8259A-IR1 (IRQ1, INT 09h)
bool Machine::keyboardInterrupt(void * context)
{
auto m = (Machine*)context;
return m->m_PIC8259A.signalInterrupt(1);
}
// 8259B-IR4 (IRQ12, INT 074h)
bool Machine::mouseInterrupt(void * context)
{
auto m = (Machine*)context;
return m->m_PIC8259B.signalInterrupt(4);
}
// interrupt from MC146818, trig 8259B-IR0 (IRQ8, INT 70h)
bool Machine::MC146818Interrupt(void * context)
{
auto m = (Machine*)context;
return m->m_PIC8259B.signalInterrupt(0);
}
// interrupt from COM1, trig 8259A-IR4 (IRQ4, INT 0Ch)
bool Machine::COM1Interrupt(PC8250 * source, void * context)
{
auto m = (Machine*)context;
return source->getOut2() && m->m_PIC8259A.signalInterrupt(4);
}
// interrupt from COM2, trig 8259A-IR3 (IRQ3, INT 0Bh)
bool Machine::COM2Interrupt(PC8250 * source, void * context)
{
auto m = (Machine*)context;
return source->getOut2() && m->m_PIC8259A.signalInterrupt(3);
}
void Machine::writeVideoMemory8(void * context, int address, uint8_t value)
{
//printf("WVMEM %05X <= %02X\n", address, value);
if (address >= 0xb0000)
s_videoMemory[address - 0xb0000] = value;
}
void Machine::writeVideoMemory16(void * context, int address, uint16_t value)
{
//printf("WVMEM %05X <= %04X\n", address, value);
if (address >= 0xb0000)
*(uint16_t*)(s_videoMemory + address - 0xb0000) = value;
}
uint8_t Machine::readVideoMemory8(void * context, int address)
{
if (address >= 0xb0000)
return s_videoMemory[address - 0xb0000];
else
return 0xff;
}
uint16_t Machine::readVideoMemory16(void * context, int address)
{
if (address >= 0xb0000)
return *(uint16_t*)(s_videoMemory + address - 0xb0000);
else
return 0xffff;
}
bool Machine::interrupt(void * context, int num)
{
auto m = (Machine*)context;
// emu interrupts callable only inside the BIOS segment
if (i8086::CS() == BIOS_SEG) {
switch (num) {
// Put Char for debug (AL)
case 0xf4:
printf("%c", i8086::AX() & 0xff);
return true;
// BIOS helpers (AH = select helper function)
case 0xf5:
m->m_BIOS.helpersEntry();
return true;
// set or reset flag CF before IRET, replacing value in stack with current value
case 0xf6:
{
auto savedFlags = (uint16_t*) (s_memory + i8086::SS() * 16 + (uint16_t)(i8086::SP() + 4));
*savedFlags = (*savedFlags & 0xfffe) | i8086::flagCF();
return true;
}
// set or reset flag ZF before IRET, replacing value in stack with current value
case 0xf7:
{
auto savedFlags = (uint16_t*) (s_memory + i8086::SS() * 16 + (uint16_t)(i8086::SP() + 4));
*savedFlags = (*savedFlags & 0xffbf) | (i8086::flagZF() << 6);
return true;
}
// set or reset flag IF before IRET, replacing value in stack with current value
case 0xf8:
{
auto savedFlags = (uint16_t*) (s_memory + i8086::SS() * 16 + (uint16_t)(i8086::SP() + 4));
*savedFlags = (*savedFlags & 0xfdff) | (i8086::flagIF() << 9);
return true;
}
// test point P0
case 0xf9:
printf("P0 AX=%04X BX=%04X CX=%04X DX=%04X DS=%04X\n", i8086::AX(), i8086::BX(), i8086::CX(), i8086::DX(), i8086::DS());
return true;
// test point P1
case 0xfa:
printf("P1 AX=%04X BX=%04X CX=%04X DX=%04X DS=%04X\n", i8086::AX(), i8086::BX(), i8086::CX(), i8086::DX(), i8086::DS());
return true;
// BIOS disk handler (INT 13h)
case 0xfb:
m->m_BIOS.diskHandlerEntry();
return true;
// BIOS video handler (INT 10h)
case 0xfc:
m->m_BIOS.videoHandlerEntry();
return true;
}
}
// not hanlded
return false;
}
void Machine::speakerSetFreq()
{
//printf("speakerSetFreq: count = %d\n", m_PIT8253.timerInfo(2).resetCount);
int timerCount = m_PIT8253.timerInfo(2).resetCount;
if (timerCount == 0)
timerCount = 65536;
int freq = PIT_TICK_FREQ / timerCount;
//printf(" freq = %dHz\n", freq);
m_sinWaveGen.setFrequency(freq);
}
void Machine::speakerEnableDisable()
{
bool genEnabled = m_PIT8253.getGate(2);
if (genEnabled && m_speakerDataEnable) {
// speaker enabled
m_sinWaveGen.enable(true);
} else {
// speaker disabled
m_sinWaveGen.enable(false);
}
}
void Machine::dumpMemory(char const * filename)
{
constexpr int BLOCKLEN = 1024;
FILE * file = nullptr; (void)filename; // no filesystem
if (file) {
for (int i = 0; i < 1048576; i += BLOCKLEN)
fwrite(s_memory + i, 1, BLOCKLEN, file);
fclose(file);
}
}
void Machine::dumpInfo(char const * filename)
{
FILE * file = nullptr; (void)filename; // no filesystem
if (file) {
// CPU state
fprintf(file, " CS DS ES SS\n");
fprintf(file, "%04X %04X %04X %04X\n\n", i8086::CS(), i8086::DS(), i8086::ES(), i8086::SS());
fprintf(file, " IP AX BX CX DX SI DI BP SP\n");
fprintf(file, "%04X %04X %04X %04X %04X %04X %04X %04X %04X\n\n", i8086::IP(), i8086::AX(), i8086::BX(), i8086::CX(), i8086::DX(), i8086::SI(), i8086::DI(), i8086::BP(), i8086::SP());
fprintf(file, "O D I T S Z A P C\n");
fprintf(file, "%d %d %d %d %d %d %d %d %d\n\n", i8086::flagOF(), i8086::flagDF(), i8086::flagIF(), i8086::flagTF(), i8086::flagSF(), i8086::flagZF(), i8086::flagAF(), i8086::flagPF(), i8086::flagCF());
fprintf(file, "CS+IP: %05X\n", i8086::CS() * 16 + i8086::IP());
fprintf(file, "SS+SP: %05X\n\n", i8086::SS() * 16 + i8086::SP());
fclose(file);
}
}
/*
Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
Copyright (c) 2019-2022 Fabrizio Di Vittorio.
All rights reserved.
* Please contact fdivitto2013@gmail.com if you need a commercial license.
* This library and related software is available under GPL v3.
FabGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FabGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FabGL. If not, see <http://www.gnu.org/licenses/>.
*/
// CGA Craphics Card Ports Bits
// Hercules (HGC) Ports Bits
// I/O expander (based on MCP23S17) ports
// whole 8 bit ports handling
// single GPIO handling
// I/O expander configuration bits
//////////////////////////////////////////////////////////////////////////////////////
// Machine
using fabgl::i8086;
uint8_t * Machine::s_memory;
uint8_t * Machine::s_videoMemory;
Machine::Machine() :