Skip to content

Instantly share code, notes, and snippets.

@JetForMe
Created April 23, 2023 05:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JetForMe/20f2607281ffc8ac61d06a3ebdc45a44 to your computer and use it in GitHub Desktop.
Save JetForMe/20f2607281ffc8ac61d06a3ebdc45a44 to your computer and use it in GitHub Desktop.
Arduino 6502 ROM Emulator
const char sAddrBus[] = { 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52 };
const char sDataBus[] = { 39, 41, 43, 45, 47, 49, 51, 53 };
#define qPinClock 2
#define qPinRW 3
const uint8_t sROM[] =
{
//00 0xFEFF
0xa2, 0x05, 0xca, 0xd0, 0xfd, 0xea, 0xea, 0xea, 0xa2, 0x04, 0xca, 0xd0, 0xfd, 0x4c, 0x00, 0xff,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
//64
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
//128
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
//192
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00,
//256
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const uint16_t kROMStartAddr = 0x0000 - sizeof(sROM);
uint8_t sRAM[1024];
void
setup()
{
for (int i = 0; i < sizeof(sAddrBus); i++)
{
pinMode(sAddrBus[i], INPUT);
}
for (int i = 0; i < sizeof(sDataBus); i++)
{
pinMode(sDataBus[i], INPUT);
}
pinMode(qPinClock, INPUT);
attachInterrupt(digitalPinToInterrupt(qPinClock), onClock, RISING);
pinMode(qPinRW, INPUT);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println("6502 ROM Emulator");
char s[32];
snprintf(s, sizeof(s), "ROM Start: 0x%04X", kROMStartAddr);
Serial.println(s);
snprintf(s, sizeof(s), "ROM End: 0x%04X", kROMStartAddr + sizeof(sROM));
Serial.println(s);
}
void
setDataBusDirection(bool inRead)
{
int mode = inRead ? OUTPUT : INPUT;
for (int i = 0; i < sizeof(sDataBus); i++)
{
pinMode(sDataBus[i], mode);
}
}
void
setDataBus(uint8_t inData)
{
for (int i = 0; i < sizeof(sDataBus); i++)
{
digitalWrite(sDataBus[i], inData & (1 << (7 - i)));
}
}
uint16_t
readBus(const char* inBus, int inWidth)
{
uint16_t value = 0;
for (int i = 0; i < inWidth; i++)
{
int bit = digitalRead(inBus[i]) ? 1 : 0;
// Serial.print(bit);
value = (value << 1) + bit;
}
return value;
}
void
onClock()
{
static char s[64];
// If the processor is reading, output values…
bool rw = digitalRead(qPinRW);
setDataBusDirection(rw);
uint16_t addr = readBus(sAddrBus, sizeof (sAddrBus));
uint8_t data = 0;
if (rw) // CPU is reading
{
if (addr >= kROMStartAddr)
{
int idx = addr - kROMStartAddr;
data = sROM[idx];
setDataBus(data);
// snprintf(s, sizeof(s), "Read from ROM 0x%04X (%4d): 0x%02X", addr, idx, data);
// Serial.println(s);
}
else
{
setDataBus(0xea); // NOP
}
}
else // CPU is writing
{
// TODO: Ignore for now
}
// Log the bus state…
data = readBus(sDataBus, sizeof (sDataBus));
snprintf(s, sizeof(s), "0x%04X %c 0x%02X", addr, rw ? 'r' : 'W', data);
Serial.println(s);
}
void
loop()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment