From 646d79fff5f653271dbc87f7cd37a31424f71642 Mon Sep 17 00:00:00 2001 From: Andrew Alderwick Date: Wed, 5 Jan 2022 13:28:22 +0000 Subject: [PATCH] Factor out common parts of system_dei/deo. --- build.sh | 1 + src/devices/system.c | 23 +++++++++++++++++++++++ src/devices/system.h | 14 ++++++++++++++ src/uxncli.c | 21 ++++----------------- src/uxnemu.c | 19 +++---------------- 5 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 src/devices/system.h diff --git a/build.sh b/build.sh index 851f2ca..c9ca61f 100755 --- a/build.sh +++ b/build.sh @@ -14,6 +14,7 @@ then echo "Formatting.." clang-format -i src/uxn.h clang-format -i src/uxn.c + clang-format -i src/devices/system.h clang-format -i src/devices/system.c clang-format -i src/devices/screen.h clang-format -i src/devices/screen.c diff --git a/src/devices/system.c b/src/devices/system.c index a7e6fc2..a6eff79 100644 --- a/src/devices/system.c +++ b/src/devices/system.c @@ -1,4 +1,5 @@ #include "../uxn.h" +#include "system.h" #include @@ -27,3 +28,25 @@ uxn_halt(Uxn *u, Uint8 error, Uint16 addr) fprintf(stderr, "Halted: %s#%04x, at 0x%04x\n", errors[error], u->ram[addr], addr); return 0; } + +/* IO */ + +Uint8 +system_dei(Device *d, Uint8 port) +{ + switch(port) { + case 0x2: return d->u->wst.ptr; + case 0x3: return d->u->rst.ptr; + default: return d->dat[port]; + } +} + +void +system_deo(Device *d, Uint8 port) +{ + switch(port) { + case 0x2: d->u->wst.ptr = d->dat[port]; break; + case 0x3: d->u->rst.ptr = d->dat[port]; break; + default: system_deo_special(d, port); + } +} diff --git a/src/devices/system.h b/src/devices/system.h new file mode 100644 index 0000000..35f81a6 --- /dev/null +++ b/src/devices/system.h @@ -0,0 +1,14 @@ +/* +Copyright (c) 2022 Devine Lu Linvega, Andrew Alderwick + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE. +*/ + +Uint8 system_dei(Device *d, Uint8 port); +void system_deo(Device *d, Uint8 port); +void system_deo_special(Device *d, Uint8 port); diff --git a/src/uxncli.c b/src/uxncli.c index 692ed6c..4126411 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -3,6 +3,7 @@ #include #include #include "uxn.h" +#include "devices/system.h" #include "devices/file.h" /* @@ -45,26 +46,12 @@ inspect(Stack *s, char *name) #pragma mark - Devices -static Uint8 -system_dei(Device *d, Uint8 port) +void +system_deo_special(Device *d, Uint8 port) { - switch(port) { - case 0x2: return d->u->wst.ptr; - case 0x3: return d->u->rst.ptr; - default: return d->dat[port]; - } -} - -static void -system_deo(Device *d, Uint8 port) -{ - switch(port) { - case 0x2: d->u->wst.ptr = d->dat[port]; break; - case 0x3: d->u->rst.ptr = d->dat[port]; break; - case 0xe: + if(port == 0xe) { inspect(&d->u->wst, "Working-stack"); inspect(&d->u->rst, "Return-stack"); - break; } } diff --git a/src/uxnemu.c b/src/uxnemu.c index 8aaba28..bc6bac2 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -8,6 +8,7 @@ #pragma GCC diagnostic ignored "-Wpedantic" #pragma clang diagnostic ignored "-Wtypedef-redefinition" #include +#include "devices/system.h" #include "devices/screen.h" #include "devices/audio.h" #include "devices/file.h" @@ -169,23 +170,9 @@ init(void) #pragma mark - Devices -static Uint8 -system_dei(Device *d, Uint8 port) +void +system_deo_special(Device *d, Uint8 port) { - switch(port) { - case 0x2: return d->u->wst.ptr; - case 0x3: return d->u->rst.ptr; - default: return d->dat[port]; - } -} - -static void -system_deo(Device *d, Uint8 port) -{ - switch(port) { - case 0x2: d->u->wst.ptr = d->dat[port]; break; - case 0x3: d->u->rst.ptr = d->dat[port]; break; - } if(port > 0x7 && port < 0xe) screen_palette(&uxn_screen, &d->dat[0x8]); }