diff --git a/src/cpu/808x/CMakeLists.txt b/src/cpu/808x/CMakeLists.txt new file mode 100644 index 000000000..72aced128 --- /dev/null +++ b/src/cpu/808x/CMakeLists.txt @@ -0,0 +1,16 @@ +# +# 86Box A hypervisor and IBM PC system emulator that specializes in +# running old operating systems and software designed for IBM +# PC systems and compatibles from 1981 through fairly recent +# system designs based on the PCI bus. +# +# This file is part of the 86Box distribution. +# +# CMake build script. +# +# Authors: David Hrdlička, +# +# Copyright 2020-2021 David Hrdlička. +# + +add_library(808x.c OBJECT queue.c) diff --git a/src/cpu/808x/queue.c b/src/cpu/808x/queue.c new file mode 100644 index 000000000..2eebde0ce --- /dev/null +++ b/src/cpu/808x/queue.c @@ -0,0 +1,190 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * 808x CPU emulation, mostly ported from reenigne's XTCE, which + * is cycle-accurate. + * + * Authors: gloriouscow, + * Miran Grca, + * + * Copyright 2023 gloriouscow. + * Copyright 2023 Miran Grca. + */ +#include +#include +#include +#include +#include +#include + +#define HAVE_STDARG_H +#include <86box/86box.h> +#include "cpu.h" +#include "x86.h" +#include <86box/machine.h> +#include <86box/io.h> +#include <86box/mem.h> +#include <86box/rom.h> +#include <86box/nmi.h> +#include <86box/pic.h> +#include <86box/ppi.h> +#include <86box/timer.h> +#include <86box/gdbstub.h> +// #include "808x.h" +#include "queue.h" + +/* TODO: Move to cpu.h so this can eventually be reused for 286+ as well. */ +#define QUEUE_MAX 6 + +typedef struct queue_t +{ + size_t size; + size_t len; + size_t back; + size_t front; + uint8_t q[QUEUE_MAX]; + uint16_t preload; + queue_delay_t delay; +} queue_t; + +static queue_t queue; + +#ifdef ENABLE_QUEUE_LOG +int queue_do_log = ENABLE_QUEUE_LOG; + +static void +queue_log(const char *fmt, ...) +{ + va_list ap; + + if (queue_do_log) { + va_start(ap, fmt); + pclog_ex(fmt, ap); + va_end(ap); + } +} +#else +# define queue_log(fmt, ...) +#endif + +void +queue_set_size(size_t size) +{ + if (size > QUEUE_MAX) + fatal("Requested prefetch queue of %i bytes is too big\n", size); + + queue.size = size; +} + +size_t +queue_get_len(void) +{ + return queue.len; +} + +int +queue_is_full(void) +{ + return (queue.len != queue.size); +} + +uint16_t +queue_get_preload(void) +{ + uint16_t ret = queue.preload; + queue.preload = 0x0000; + + return ret; +} + +int +queue_has_preload(void) +{ + return (queue.preload & FLAG_PRELOADED) ? 1 : 0; +} + +void +queue_set_preload(void) +{ + uint8_t byte; + + if (queue.len > 0) { + byte = queue_pop(); + queue.preload = ((uint16_t) byte) | FLAG_PRELOADED; + } else + fatal("Tried to preload with empty queue\n"); +} + +void +queue_push8(uint8_t byte) +{ + if (queue.len < queue.size) { + queue.q[queue.front] = byte; + queue.front = (queue.front + 1) % queue.size; + queue.len++; + + if (queue.len == 3) + queue.delay = DELAY_WRITE; + else + queue.delay = DELAY_NONE; + } else + fatal("Queue overrun\n"); +} + +void +queue_push16(uint16_t word) +{ + queue_push8((uint8_t) (word & 0xff)); + queue_push8((uint8_t) ((word >> 8) & 0xff)); +} + +uint8_t +queue_pop(void) +{ + uint8_t byte = 0xff; + + if (queue.len > 0) { + byte = queue.q[queue.back]; + + queue.back = (queue.back + 1) % queue.size; + queue.len--; + + if (queue.len >= 3) + queue.delay = DELAY_READ; + else + queue.delay = DELAY_NONE; + } else + fatal("Queue underrun\n"); + + return byte; +} + +queue_delay_t +queue_get_delay(void) +{ + return queue.delay; +} + +void +queue_flush(void) +{ + memset(&queue, 0x00, sizeof(queue_t)); + + queue.delay = DELAY_NONE; +} + +void +queue_init(void) +{ + queue_flush(); + + if (is8086) + queue_set_size(6); + else + queue_set_size(4); +} diff --git a/src/cpu/808x/queue.h b/src/cpu/808x/queue.h new file mode 100644 index 000000000..544455784 --- /dev/null +++ b/src/cpu/808x/queue.h @@ -0,0 +1,43 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * Prefetch queue implementation header. + * + * Authors: gloriouscow, + * Miran Grca, + * + * Copyright 2023 gloriouscow. + * Copyright 2023 Miran Grca. + */ +#ifndef EMU_QUEUE_H +#define EMU_QUEUE_H + +typedef enum queue_delay_t +{ + DELAY_READ, + DELAY_WRITE, + DELAY_NONE +} queue_delay_t; + +#define FLAG_PRELOADED 0x8000 + +extern void queue_set_size(size_t size); +extern size_t queue_get_len(void); +extern int queue_is_full(void); +extern uint16_t queue_get_preload(void); +extern int queue_has_preload(void); +extern void queue_set_preload(void); +extern void queue_push8(uint8_t byte); +extern void queue_push16(uint16_t word); +extern uint8_t queue_pop(void); +extern queue_delay_t queue_get_delay(void); +extern void queue_flush(void); + +extern void queue_init(void); + +#endif /*EMU_QUEUE_H*/ diff --git a/src/cpu/CMakeLists.txt b/src/cpu/CMakeLists.txt index 18aa06023..d1011504e 100644 --- a/src/cpu/CMakeLists.txt +++ b/src/cpu/CMakeLists.txt @@ -35,3 +35,6 @@ endif() add_subdirectory(softfloat) target_link_libraries(86Box softfloat) + +add_subdirectory(808x) +target_link_libraries(86Box 808x) diff --git a/src/win/Makefile.mingw b/src/win/Makefile.mingw index 6946eff04..c5f6c61ce 100644 --- a/src/win/Makefile.mingw +++ b/src/win/Makefile.mingw @@ -242,7 +242,7 @@ PROG := 86Box # Nothing should need changing from here on.. # ######################################################################### VPATH := $(EXPATH) . $(CODEGEN) minitrace cpu cpu/softfloat \ - cdrom chipset device disk disk/minivhd floppy \ + cpu/808x cdrom chipset device disk disk/minivhd floppy \ game machine mem printer \ sio sound \ sound/munt sound/munt/c_interface sound/munt/sha1 \ @@ -547,6 +547,8 @@ MAINOBJ := 86box.o config.o log.o random.o timer.o io.o acpi.o apm.o dma.o ddma. MEMOBJ := catalyst_flash.o i2c_eeprom.o intel_flash.o mem.o rom.o row.o smram.o spd.o sst_flash.o +CPU808XOBJ := queue.o + CPUOBJ := $(DYNARECOBJ) \ $(CGTOBJ) \ cpu.o cpu_table.o fpu.o x86.o \ @@ -773,7 +775,7 @@ ifeq ($(RTMIDI), y) SNDOBJ += midi_rtmidi.o endif -OBJ := $(MAINOBJ) $(CPUOBJ) $(CHIPSETOBJ) $(MCHOBJ) $(DEVOBJ) $(MEMOBJ) \ +OBJ := $(MAINOBJ) $(CPU808XOBJ) $(CPUOBJ) $(CHIPSETOBJ) $(MCHOBJ) $(DEVOBJ) $(MEMOBJ) \ $(FDDOBJ) $(GAMEOBJ) $(CDROMOBJ) $(ZIPOBJ) $(MOOBJ) $(HDDOBJ) $(MINIVHDOBJ) \ $(NETOBJ) $(PRINTOBJ) $(SCSIOBJ) $(SIOOBJ) $(SNDOBJ) $(VIDOBJ) $(VOODOOOBJ) \ $(PLATOBJ) $(UIOBJ) $(FSYNTHOBJ) $(MUNTOBJ) $(DEVBROBJ) $(MINITRACEOBJ) $(THREADOBJ)