2 // Virtual 6821 Peripheral Interface Adapter (PIA) Emulator v1.0
5 // (C) 2022 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 12/27/2022 Created this file. :-)
18 // A "1" bit in the DDR means that line is an output
20 V6821PIA::V6821PIA(): ddra(0), ddrb(0), pa(0), pb(0), cra(0), crb(0), ora(0), orb(0), ca1(false), ca2(false), cb1(false), cb2(false), irqa1(false), irqa2(false), irqb1(false), irqb2(false), irqaState(false), irqbState(false), IRQA(NULL), IRQB(NULL)
24 V6821PIA::V6821PIA(void (* a)(bool), void (*b)(bool)): ddra(0), ddrb(0), pa(0), pb(0), cra(0), crb(0), ora(0), orb(0), ca1(false), ca2(false), cb1(false), cb2(false), irqa1(false), irqa2(false), irqb1(false), irqb2(false), irqaState(false), irqbState(false), IRQA(a), IRQB(b)
28 void V6821PIA::Reset(void)
32 uint8_t V6821PIA::Read(uint8_t addr)
39 // IRQ flags are implicitly cleared by a read
40 irqa1 = irqa2 = false;
49 return cra | (irqa1 ? 0x80 : 0x00) | (irqa2 ? 0x40 : 0x00);
54 // IRQ flags are implicitly cleared by a read
55 irqb1 = irqb2 = false;
64 return crb | (irqb1 ? 0x80 : 0x00) | (irqb2 ? 0x40 : 0x00);
67 // This is only here to silence the the compiler... :-/
71 void V6821PIA::Write(uint8_t addr, uint8_t data)
84 cra = data & 0x3F; // Bits 6-7 are RO
97 crb = data & 0x3F; // Bits 6-7 are RO
102 void V6821PIA::CA1(bool state)
104 // Do nothing if nothing changed...
108 // Check if there's been a transition (bit 1 = lo to hi transition)
109 if ((state && (cra & 0x02)) || (!state && !(cra & 0x02)))
118 void V6821PIA::CA2(bool state)
123 void V6821PIA::CB1(bool state)
125 // Do nothing if nothing changed...
129 // Check if there's been a transition (bit 1 = lo to hi transition)
130 if ((state && (crb & 0x02)) || (!state && !(crb & 0x02)))
139 void V6821PIA::CB2(bool state)
144 void V6821PIA::HandleIRQs(void)
146 bool newState = (irqa1 && (cra & 0x01)) || (irqa2 && (cra & 0x08));
148 if (newState != irqaState)
150 irqaState = newState;
156 newState = (irqb1 && (crb & 0x01)) || (irqb2 && (crb & 0x08));
158 if (newState != irqbState)
160 irqbState = newState;