My initialization code now looks something like this (6502 assembly):
Code: Select all
// Motorola 6821 PIA (Peripheral Interface Adapter)
const uint PORTA = 0xF010; // Peripheral A Data Register
const uint PORTB = 0xF011; // Peripheral B Data Register
const uint CRA = 0xF012; // Control Register A
const uint CRB = 0xF013; // Control Register B
// Data Direction Registers
const uint DDRA = 0xF010; // Data Direction Register A (Shared with PORTA)
const uint DDRB = 0xF011; // Data Direction Register B (Shared with PORTB)
// Motorola 6821 PIA
// soft reset by zeroing all 6 registers (like a hard reset would do)
//LDA #0b00000000 // Select DDRA
LDA #0b00110000 // Bits 5-3=110 (CA2 output high), bit 2=0 (select DDRA)
STA CRA
LDA #0b00000000 // Set all pins of PORTA as inputs (DDRA)
STA DDRA
//LDA #0b00000100 // Select PRA (PORTA) and clear interrupt flags in CRA
LDA #0b00110100 // Bits 5-3=110 (CA2 output high), bit 2=1 (select PORTA)
STA CRA
LDA PORTA // READ to clear interrupt flags!
LDA #0b00000000 // Clear all output latches
STA PORTA // This writes to output register even though pins are inputs
LDA #0b00000000 // Select DDRB
STA CRB
LDA #0b00000000 // Set all pins of PORTB as inputs (DDRB)
STA DDRB
LDA #0b00000100 // Select PRB (PORTB) and clear interrupt flags in CRB
STA CRB
LDA PORTB // READ to clear interrupt flags!
LDA #0b00000000 // Clear all output latches
STA PORTB // This writes to output register even though pins are inputsCode: Select all
PinMode(byte pin, PinModeOption pinMode)
{
uint ddr = (pin <= 7) ? DDRA : DDRB;
#ifdef M6821_PIA
uint cr = (pin <= 7) ? CRA : CRB;
// Select the DDR
byte crValue = (pin <= 7) ? 0b00110000 : 0b00000000;
Memory.WriteByte(cr, crValue);
#endif
pin = pin & 0b00000111;
pin = 1 << pin;
if (pinMode == PinModeOption.Input)
{
Memory.WriteByte(ddr, Memory.ReadByte(ddr) & ~pin);
}
else
{
Memory.WriteByte(ddr, Memory.ReadByte(ddr) | pin);
}
}
bool DigitalRead(byte pin)
{
uint port = (pin <= 7) ? PORTA : PORTB;
#ifdef M6821_PIA
// Select the port register
uint cr = (pin <= 7) ? CRA : CRB;
byte crValue = (pin <= 7) ? 0b00110100 : 0b00000100;
Memory.WriteByte(cr, crValue);
#endif
byte b = Memory.ReadByte(port);
WriteLn(b.ToString());
return ((Memory.ReadByte(port) & (1 << (pin & 0b00000111))) != 0);
}
DigitalWrite(byte pin, bool value)
{
uint port = (pin <= 7) ? PORTA : PORTB;
#ifdef M6821_PIA
// Select the port register
uint cr = (pin <= 7) ? CRA : CRB;
byte crValue = (pin <= 7) ? 0b00110100 : 0b00000100;
Memory.WriteByte(cr, crValue);
pin = 1 << (pin & 0b00000111);
if (port == PORTB)
{
if (value)
{
portBShadow = portBShadow | pin;
}
else
{
portBShadow = portBShadow & ~pin;
}
Memory.WriteByte(port, portBShadow);
}
else
{
if (value)
{
Memory.WriteByte(port, Memory.ReadByte(port) | pin);
}
else
{
Memory.WriteByte(port, Memory.ReadByte(port) & ~pin);
}
}
#else
pin = 1 << (pin & 0b00000111);
if (value)
{
Memory.WriteByte(port, Memory.ReadByte(port) | pin);
}
else
{
Memory.WriteByte(port, Memory.ReadByte(port) & ~pin);
}
#endif
}Thanks,
Michael