From 6848cff4dce8f98105ae3fae9ed7062af80552d7 Mon Sep 17 00:00:00 2001 From: Dimitris Panokostas Date: Thu, 2 Jul 2020 22:27:32 +0200 Subject: [PATCH] First implementation of consolehook --- Makefile | 1 + VisualGDB/Amiberry/Amiberry.vcxproj | 2 + VisualGDB/Amiberry/Amiberry.vcxproj.filters | 6 + src/consolehook.cpp | 125 +++++++++++++++++++ src/filesys.cpp | 127 +++++++++++++++++++- src/include/consolehook.h | 11 ++ src/include/sysdeps.h | 2 + src/main.cpp | 13 +- src/osdep/writelog.cpp | 36 ++++++ 9 files changed, 317 insertions(+), 6 deletions(-) create mode 100644 src/consolehook.cpp create mode 100644 src/include/consolehook.h diff --git a/Makefile b/Makefile index 65b3c959..c341c6b0 100644 --- a/Makefile +++ b/Makefile @@ -293,6 +293,7 @@ OBJS = \ src/cdtvcr.o \ src/cfgfile.o \ src/cia.o \ + src/consolehook.o \ src/crc32.o \ src/custom.o \ src/def_icons.o \ diff --git a/VisualGDB/Amiberry/Amiberry.vcxproj b/VisualGDB/Amiberry/Amiberry.vcxproj index 603522cb..a5df9294 100644 --- a/VisualGDB/Amiberry/Amiberry.vcxproj +++ b/VisualGDB/Amiberry/Amiberry.vcxproj @@ -204,6 +204,7 @@ + @@ -382,6 +383,7 @@ + diff --git a/VisualGDB/Amiberry/Amiberry.vcxproj.filters b/VisualGDB/Amiberry/Amiberry.vcxproj.filters index 4473894b..66974e13 100644 --- a/VisualGDB/Amiberry/Amiberry.vcxproj.filters +++ b/VisualGDB/Amiberry/Amiberry.vcxproj.filters @@ -628,6 +628,9 @@ Source files + + Source files + @@ -1029,5 +1032,8 @@ Source files\include\uae + + Source files\include + \ No newline at end of file diff --git a/src/consolehook.cpp b/src/consolehook.cpp new file mode 100644 index 00000000..6e51ce82 --- /dev/null +++ b/src/consolehook.cpp @@ -0,0 +1,125 @@ + + +#include "sysconfig.h" +#include "sysdeps.h" + +#include "options.h" +#include "memory.h" +#include "execlib.h" +#include "disk.h" +#include "rommgr.h" +#include "uae.h" +#include "threaddep/thread.h" +#include "keybuf.h" + +#include "consolehook.h" + +static uaecptr beginio; + +void consolehook_config (struct uae_prefs *p) +{ + struct uaedev_config_info ci = { 0 }; + int roms[] = { 15, 31, 16, 46, -1 }; + + default_prefs (p, true, 0); + //p->headless = 1; + p->produce_sound = 0; + p->gfx_resolution = 0; + p->gfx_vresolution = 0; + p->gfx_iscanlines = 0; + p->gfx_pscanlines = 0; + p->gfx_framerate = 10; + p->immediate_blits = 1; + p->collision_level = 0; + configure_rom (p, roms, 0); + p->cpu_model = 68020; + p->fpu_model = 68882; + p->m68k_speed = -1; + p->cachesize = 8192; + p->cpu_compatible = 0; + p->address_space_24 = 0; + p->chipmem_size = 0x00200000; + p->z3fastmem[0].size = 0x00800000; + p->bogomem_size = 0; + p->nr_floppies = 1; + p->floppyslots[1].dfxtype = DRV_NONE; + p->floppy_speed = 0; + p->start_gui = 0; + p->gfx_monitor.gfx_size.width = 320; + p->gfx_monitor.gfx_size.height = 256; + p->gfx_monitor.gfx_size_win.width = 320; + p->gfx_monitor.gfx_size_win.height = 256; + p->turbo_emulation = 0; + //p->win32_automount_drives = 2; + //p->win32_automount_cddrives = 2; + + _tcscpy (ci.rootdir, _T(".")); + _tcscpy (ci.volname, _T("CLIBOOT")); + _tcscpy (ci.devname, _T("DH0")); + ci.bootpri = 15; + ci.type = UAEDEV_DIR; + add_filesys_config (p, -1, &ci); +} + +static int console_thread (void *v) +{ + uae_set_thread_priority (NULL, 1); + for (;;) { + TCHAR wc = console_getch (); + char c[2]; + + c[0] = 0; + c[1] = 0; + ua_copy (c, 1, &wc); + record_key_direct ((0x10 << 1) | 0); + record_key_direct ((0x10 << 1) | 1); + } + return 0; +} + +int consolehook_activate (void) +{ + return console_emulation; +} + +void consolehook_ret(TrapContext *ctx, uaecptr condev, uaecptr oldbeginio) +{ + beginio = oldbeginio; + write_log (_T("console.device at %08X\n"), condev); + + uae_start_thread (_T("consolereader"), console_thread, NULL, NULL); +} + +uaecptr consolehook_beginio(TrapContext *ctx, uaecptr request) +{ + uae_u32 io_data = trap_get_long(ctx, request + 40); // 0x28 + uae_u32 io_length = trap_get_long(ctx, request + 36); // 0x24 + uae_u32 io_actual = trap_get_long(ctx, request + 32); // 0x20 + uae_u32 io_offset = trap_get_long(ctx, request + 44); // 0x2c + uae_u16 cmd = trap_get_word(ctx, request + 28); + + if (cmd == CMD_WRITE) { + int len = io_length; + char *dbuf; + TCHAR *buf; + if (len == -1) { + dbuf = xmalloc(char, MAX_DPATH); + trap_get_string(ctx, dbuf, io_data, MAX_DPATH); + len = strlen(dbuf); + } else { + dbuf = xmalloc(char, len); + trap_get_bytes(ctx, dbuf, io_data, len); + } + buf = xmalloc(TCHAR, len + 1); + au_copy(buf, len, dbuf); + buf[len] = 0; + f_out(stdout, _T("%s"), buf); + xfree(buf); + xfree(dbuf); + } else if (cmd == CMD_READ) { + + write_log (_T("%08x: CMD=%d LEN=%d OFF=%d ACT=%d\n"), request, cmd, io_length, io_offset, io_actual); + + } + return beginio; +} diff --git a/src/filesys.cpp b/src/filesys.cpp index a4b3a7cf..dfd0deff 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -47,6 +47,7 @@ #include "uaeresource.h" #include "inputdevice.h" #include "blkdev.h" +#include "consolehook.h" #include "isofs_api.h" #include "scsi.h" #include "newcpu.h" @@ -8221,6 +8222,39 @@ static uae_u32 REGPARAM2 mousehack_done (TrapContext *ctx) uaecptr dispinfo = trap_get_areg(ctx, 3); uaecptr vp = trap_get_areg(ctx, 4); return input_mousehack_status(ctx, mode, diminfo, dispinfo, vp, trap_get_dreg(ctx, 2)); + } + else if (mode == 10) { + //amiga_clipboard_die(ctx); + } + else if (mode == 11) { + //amiga_clipboard_got_data(ctx, trap_get_areg(ctx, 2), trap_get_dreg(ctx, 2), trap_get_dreg(ctx, 0) + 8); + } + else if (mode == 12) { + //return amiga_clipboard_want_data(ctx); + } + else if (mode == 13) { + //return amiga_clipboard_proc_start(ctx); + } + else if (mode == 14) { + //amiga_clipboard_task_start(ctx, trap_get_dreg(ctx, 0)); + } + else if (mode == 15) { + //amiga_clipboard_init(ctx); + } + else if (mode == 16) { + uaecptr a2 = trap_get_areg(ctx, 2); + input_mousehack_mouseoffset(a2); + } + else if (mode == 17) { + uae_u32 v = 0; + if (currprefs.clipboard_sharing) + v |= 1; + if (consolehook_activate()) + v |= 2; + if (currprefs.uaeboard > 2) + v |= 4; + trap_dos_active(); + return v; } else if (mode == 18) { put_long_host(rtarea_bank.baseaddr + RTAREA_EXTERTASK, trap_get_dreg(ctx, 0)); put_long_host(rtarea_bank.baseaddr + RTAREA_TRAPTASK, trap_get_dreg(ctx, 2)); @@ -8232,8 +8266,97 @@ static uae_u32 REGPARAM2 mousehack_done (TrapContext *ctx) } else if (mode == 20) { // boot rom copy done return 0; - } else { - write_log (_T("Unknown mousehack hook %d\n"), mode); + } + else if (mode == 21) { + // keymap hook +#ifdef RETROPLATFORM + rp_keymap(ctx, trap_get_areg(ctx, 1), trap_get_dreg(ctx, 0)); +#endif + return 1; + } + else if (mode == 101) { + consolehook_ret(ctx, trap_get_areg(ctx, 1), trap_get_areg(ctx, 2)); + } + else if (mode == 102) { + uaecptr ret = consolehook_beginio(ctx, trap_get_areg(ctx, 1)); + trap_put_long(ctx, trap_get_areg(ctx, 7) + 4 * 4, ret); + } + else if (mode == 200) { + //uae_u32 v; + //// a0 = data, d0 = length, a1 = task, d3 = stack size (in), stack ptr (out) + //// a2 = debugdata, d2 = debuglength + //// d4 = flags + //if ((trap_get_dreg(ctx, 4) & 3) != 1) { + // write_log(_T("unsupported uaedbg version\n")); + // return 0; + //} + //uae_u32 stack = trap_get_dreg(ctx, 3); + //v = debugmem_reloc(trap_get_areg(ctx, 0), trap_get_dreg(ctx, 0), + // trap_get_areg(ctx, 2), trap_get_dreg(ctx, 2), + // trap_get_areg(ctx, 1), &stack); + //trap_set_dreg(ctx, 2, stack); + //return v; + } + else if (mode == 201) { + //debugmem_break(8); + return 1; + } + else if (mode == 202) { + // a0 = seglist, a1 = name, d2 = lock + //debugmem_addsegs(ctx, trap_get_areg(ctx, 0), trap_get_areg(ctx, 1), trap_get_dreg(ctx, 2), true); + return 1; + } + else if (mode == 203) { + // a0 = seglist + //debugmem_remsegs(trap_get_areg(ctx, 0)); + return 1; + } + else if (mode == 204 || mode == 206) { + // d0 = size, a1 = flags + //uae_u32 v = debugmem_allocmem(mode == 206, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 1), trap_get_areg(ctx, 0)); + //if (v) { + // trap_set_areg(ctx, 0, v); + // return v; + //} + //else { + // trap_set_areg(ctx, 0, 0); + // trap_set_dreg(ctx, 1, trap_get_areg(ctx, 1)); + // return trap_get_dreg(ctx, 0); + //} + } + else if (mode == 205 || mode == 207) { + //return debugmem_freemem(mode == 207, trap_get_areg(ctx, 1), trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0)); + } + else if (mode == 208) { + // segtrack: bit 0 + // fsdebug: bit 1 + segtrack_mode = currprefs.debugging_features; + return segtrack_mode; + } + else if (mode == 209) { + // called if segtrack was enabled + return 0; + } + else if (mode == 210) { + // debug trapcode + //debugmem_trap(trap_get_areg(ctx, 0)); + } + else if (mode == 212) { + // a0 = seglist, a1 = name, d2 = lock + //debugmem_addsegs(ctx, trap_get_areg(ctx, 0), trap_get_areg(ctx, 1), trap_get_dreg(ctx, 2), false); + return 1; + } + else if (mode == 213) { + // a0 = seglist + //debugmem_remsegs(trap_get_areg(ctx, 0)); + return 1; + } + else if (mode == 299) { + //return debugmem_exit(); + + } + else { + write_log(_T("Unknown mousehack hook %d\n"), mode); } return 1; } diff --git a/src/include/consolehook.h b/src/include/consolehook.h new file mode 100644 index 00000000..d119644c --- /dev/null +++ b/src/include/consolehook.h @@ -0,0 +1,11 @@ +#ifndef UAE_CONSOLEHOOK_H +#define UAE_CONSOLEHOOK_H + +#include "uae/types.h" + +int consolehook_activate(void); +void consolehook_ret(TrapContext *ctx, uaecptr condev, uaecptr oldbeginio); +uaecptr consolehook_beginio(TrapContext *ctx, uaecptr request); +void consolehook_config(struct uae_prefs *p); + +#endif /* UAE_CONSOLEHOOK_H */ diff --git a/src/include/sysdeps.h b/src/include/sysdeps.h index 459613b9..5394eaa1 100644 --- a/src/include/sysdeps.h +++ b/src/include/sysdeps.h @@ -306,6 +306,8 @@ extern void mallocemu_free (void *ptr); #endif extern void write_log (const TCHAR *,...); +extern TCHAR console_getch(void); +extern void f_out(FILE*, const TCHAR*, ...); extern void gui_message (const TCHAR *,...); #ifndef O_BINARY diff --git a/src/main.cpp b/src/main.cpp index 08e3b478..d9fa43d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,8 @@ #include #include + +#include "consolehook.h" #include "keyboard.h" long int version = 256 * 65536L * UAEMAJOR + 65536L * UAEMINOR + UAESUBREV; @@ -52,6 +54,7 @@ int config_changed; bool no_gui = false, quit_to_gui = false; bool cloanto_rom = false; bool kickstart_rom = true; +bool console_emulation = 0; struct gui_info gui_data; @@ -769,6 +772,8 @@ static void parse_cmdline(int argc, TCHAR** argv) else write_log("Can't find extension ... %s\n", txt); } + else if (_tcsncmp(argv[i], _T("-cli"), 4) == 0) + console_emulation = true; else if (_tcscmp(argv[i], _T("-f")) == 0) { /* Check for new-style "-f xxx" argument, where xxx is config-file */ @@ -1049,10 +1054,10 @@ static int real_main2(int argc, TCHAR** argv) return -1; } - //if (console_emulation) { - // consolehook_config(&currprefs); - // fixup_prefs(&currprefs, true); - //} + if (console_emulation) { + consolehook_config(&currprefs); + fixup_prefs(&currprefs, true); + } if (!setup_sound()) { diff --git a/src/osdep/writelog.cpp b/src/osdep/writelog.cpp index be8c6e06..7c74fa85 100644 --- a/src/osdep/writelog.cpp +++ b/src/osdep/writelog.cpp @@ -16,6 +16,10 @@ #define WRITE_LOG_BUF_SIZE 4096 FILE *debugfile = NULL; +int consoleopen = 0; +static int realconsole; +static TCHAR* console_buffer; + void console_out (const TCHAR *format,...) { va_list parms; @@ -27,6 +31,38 @@ void console_out (const TCHAR *format,...) cout << buffer << endl; } +void f_out(FILE* f, const TCHAR* format, ...) +{ + if (f == NULL) { + return; + } + va_list arg_ptr; + va_start(arg_ptr, format); + vfprintf(f, format, arg_ptr); + va_end(arg_ptr); +} + +TCHAR console_getch(void) +{ + //flushmsgpump(); + if (console_buffer) { + return 0; + } + else if (realconsole) { + return getwc(stdin); + } + else if (consoleopen < 0) { + unsigned long len; + + for (;;) { + const auto out = getchar(); + putchar(out); + return out; + } + } + return 0; +} + void write_log(const char* format, ...) { if (amiberry_options.write_logfile)