From ba05ac5d74e2e4108dbbb66c8cbde6547b9ea182 Mon Sep 17 00:00:00 2001 From: steve Date: Tue, 26 Sep 2023 11:35:47 +0100 Subject: [PATCH] move Apple1-dependent bits onto io class --- io.cpp | 23 +++++++++++++++++++++-- io.h | 2 ++ pia.cpp | 3 +++ pia.h | 39 +++++++++------------------------------ 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/io.cpp b/io.cpp index 4b5ac88..3ad6247 100644 --- a/io.cpp +++ b/io.cpp @@ -78,13 +78,14 @@ static const uint8_t shiftmap[] PROGMEM = { }; void io::down(uint8_t scan) { - set_porta(0); + PIA::write_porta(0); if (isshift(scan)) _shift = true; } void io::enter(uint8_t key) { - set_porta(key + 0x80); + PIA::write_porta(key + 0x80); + PIA::write_porta_cr(0xa7); } void io::up(uint8_t scan) { @@ -145,8 +146,17 @@ void io::write_portb(uint8_t b) { PIA::write_portb(b); } +void io::write_portb_cr(uint8_t b) { + if (PIA::read_portb_cr() < 0x80) + PIA::write_portb_cr(b); +} + uint8_t io::read_porta_cr() { uint8_t b = PIA::read_porta_cr(); + if (b & 0x80) { + PIA::write_porta_cr(0); + b = 0xa7; + } if (b != 0xa7) return b; @@ -159,6 +169,15 @@ uint8_t io::read_porta_cr() { return b; } +void io::write_porta_cr(uint8_t b) { + uint8_t cr = PIA::read_porta_cr(); + if (!(cr & 0x80) && b >= 0x80) + cr |= 0x80; + else + cr = b; + PIA::write_porta_cr(cr); +} + void io::checkpoint(Stream &s) { PIA::checkpoint(s); s.write(r); diff --git a/io.h b/io.h index e11a40e..81743bb 100644 --- a/io.h +++ b/io.h @@ -16,7 +16,9 @@ public: virtual void restore(Stream &); virtual void write_portb(uint8_t); + virtual void write_portb_cr(uint8_t); virtual uint8_t read_porta_cr(); + virtual void write_porta_cr(uint8_t); void load(); filer &files; diff --git a/pia.cpp b/pia.cpp index 82fa487..62217ad 100644 --- a/pia.cpp +++ b/pia.cpp @@ -2,6 +2,9 @@ #include #include "pia.h" +// see: https://github.com/mamedev/mame/blob/master/src/devices/machine/6821pia.cpp +// and: https://github.com/mamedev/mame/blob/master/src/devices/machine/6821pia.h + void PIA::write(Memory::address a, uint8_t b) { #if defined(DEBUGGING) Serial.print(millis()); diff --git a/pia.h b/pia.h index 3e0906f..e07c275 100644 --- a/pia.h +++ b/pia.h @@ -4,8 +4,8 @@ // https://en.wikipedia.org/wiki/Peripheral_Interface_Adapter class PIA { public: - PIA(): portb_cr(0), porta_cr(0) {} - virtual void reset() { portb_cr = porta_cr = 0; } + PIA(): portb(0), portb_cr(0), porta(0), porta_cr(0) {} + virtual void reset() { portb = portb_cr = porta = porta_cr = 0; } void write(Memory::address, uint8_t); uint8_t read(Memory::address); @@ -14,37 +14,16 @@ public: void restore(Stream &); protected: - // write to the "external" side of the port - void set_porta(uint8_t b) { - porta = b; - if (b & 0x80) - porta_cr = 0xa7; - } - // "device-side" operations (called from memory interface) - uint8_t read_porta() { return porta; } - virtual uint8_t read_porta_cr() { - if (porta_cr & 0x80) { - porta_cr = 0; - return 0xa7; - } - return porta_cr; - } - uint8_t read_portb() { return portb; } - uint8_t read_portb_cr() { return portb_cr; } + virtual uint8_t read_porta() { return porta; } + virtual uint8_t read_porta_cr() { return porta_cr; } + virtual uint8_t read_portb() { return portb; } + virtual uint8_t read_portb_cr() { return portb_cr; } - void write_porta(uint8_t b) { porta = b; } - void write_porta_cr(uint8_t b) { - if (!(porta_cr & 0x80) && b >= 0x80) - porta_cr |= 0x80; - else - porta_cr = b; - } + virtual void write_porta(uint8_t b) { porta = b; } + virtual void write_porta_cr(uint8_t b) { porta_cr = b; } virtual void write_portb(uint8_t b) { portb = b; } - void write_portb_cr(uint8_t b) { - if (portb_cr < 0x80) - portb_cr = b; - } + virtual void write_portb_cr(uint8_t b) { portb_cr = b; } private: uint8_t portb_cr, portb, porta_cr, porta;