Skip to content

Instantly share code, notes, and snippets.

@socram8888
Created April 29, 2018 18:49
Show Gist options
  • Save socram8888/bbdd9aa4a95bf7a1ccbc31cc3ffc450d to your computer and use it in GitHub Desktop.
Save socram8888/bbdd9aa4a95bf7a1ccbc31cc3ffc450d to your computer and use it in GitHub Desktop.
#define RW_PIN 7
#define PHI_PIN 8
#define RST_PIN 9
#define SYNC_PIN 6
int DATA_PINS[] = { 10, 11, 12, 13, A0, A1, A2, A3 };
void ph8(uint8_t x) {
if (x < 0x10) {
Serial.print('0');
}
Serial.print(x, HEX);
}
void ph16(uint16_t x) {
if (x < 0x1000) {
Serial.print('0');
}
if (x < 0x100) {
Serial.print('0');
}
if (x < 0x10) {
Serial.print('0');
}
Serial.print(x, HEX);
}
void setup() {
Serial.begin(115200);
Serial.println("START");
pinMode(SYNC_PIN, INPUT);
pinMode(RW_PIN, INPUT);
pinMode(PHI_PIN, OUTPUT);
pinMode(RST_PIN, OUTPUT);
// Address pins
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
digitalWrite(RST_PIN, LOW);
for (int i = 0; i < 10000; i++) {
digitalWrite(PHI_PIN, LOW);
digitalWrite(PHI_PIN, HIGH);
}
digitalWrite(RST_PIN, HIGH);
}
int inst = 20;
void loop() {
if (inst == 0) return;
digitalWrite(PHI_PIN, LOW);
handleMem();
printStatus();
digitalWrite(PHI_PIN, HIGH);
inst--;
}
void handleMem() {
if (digitalRead(RW_PIN) == HIGH) {
dataDir(OUTPUT);
uint16_t mem = addrRead();
if (mem == 0x8004 || mem == 0x8005) {
dataWrite(0x00);
} else if (mem == 0x0000) {
dataWrite(0x4C);
} else if (mem == 0x0001 || mem == 0x0002) {
dataWrite(0x00);
} else {
dataWrite(0xEA);
}
} else {
dataDir(INPUT);
}
}
void dataDir(uint8_t dir) {
for (int i = 0; i < 8; i++) {
pinMode(DATA_PINS[i], dir);
}
}
void dataWrite(uint8_t data) {
for (int i = 0; i < 8; i++) {
digitalWrite(DATA_PINS[i], data & 1);
data = data >> 1;
}
}
uint8_t dataRead() {
uint8_t data;
for (int i = 7; i >= 0; i--) {
data = data << 1 | (digitalRead(DATA_PINS[i]) ? 1 : 0);
}
return data;
}
uint16_t addrRead() {
return digitalRead(5) << 15 | digitalRead(4) << 2 | digitalRead(3) << 1 | digitalRead(2) << 0;
}
void printStatus() {
Serial.print("A:");
ph16(addrRead());
Serial.print(" D:");
ph8(dataRead());
Serial.print(' ');
if (digitalRead(RW_PIN) == 0) {
Serial.print('W');
} else {
Serial.print('R');
}
if (digitalRead(SYNC_PIN)) {
Serial.print(" SYNC");
}
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment