//edit: I should say that the problem is that I don't get the SY signal to work.
Here's the code I'm using to communicate with the chip:
- Code: Select all
#include <TimerThree.h>
constexpr int OPL_MASTERCLOCK = 3;
constexpr int OPL_IC = 1;
constexpr int OPL_D2 = 2;
constexpr int OPL_D3 = 0;
constexpr int OPL_D4 = 4;
constexpr int OPL_D5 = 5;
constexpr int OPL_D6 = 6;
constexpr int OPL_D7 = 7;
constexpr int OPL_SMP_BD = 8;
constexpr int OPL_SMP_AC = 9;
constexpr int OPL_DO_AB = 10;
constexpr int OPL_DO_CD = 11;
constexpr int OPL_SY = 12;
constexpr int OPL_D1 = 16;
constexpr int OPL_D0 = 17;
constexpr int OPL_TEST = 18;
constexpr int OPL_CS = 19;
constexpr int OPL_RD = 20;
constexpr int OPL_WR = 21;
constexpr int OPL_A1 = 22;
constexpr int OPL_A0 = 23;
constexpr unsigned int Masterclock = 12e6; // 14.31818e6;
constexpr unsigned int SmpRate = Masterclock/288;
constexpr unsigned int SyRate = SmpRate*36;
volatile int toggleCounter = 0;
int toggleTs = 0;
void masterToggle()
{
static bool state = false;
state = !state;
if(state) {
digitalWrite(OPL_MASTERCLOCK, HIGH);
digitalWrite(13, HIGH);
}
else {
digitalWrite(OPL_MASTERCLOCK, LOW);
digitalWrite(13, LOW);
}
++toggleCounter;
}
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode(OPL_IC, OUTPUT);
pinMode(OPL_A0, OUTPUT);
pinMode(OPL_A1, OUTPUT);
pinMode(OPL_WR, OUTPUT);
pinMode(OPL_RD, OUTPUT);
pinMode(OPL_CS, OUTPUT);
pinMode(OPL_TEST, INPUT);
pinMode(OPL_D0, INPUT);
pinMode(OPL_D1, INPUT);
pinMode(OPL_D2, INPUT);
pinMode(OPL_D3, INPUT);
pinMode(OPL_D4, INPUT);
pinMode(OPL_D5, INPUT);
pinMode(OPL_D6, INPUT);
pinMode(OPL_D7, INPUT);
pinMode(OPL_SMP_BD, INPUT);
pinMode(OPL_SMP_AC, INPUT);
pinMode(OPL_DO_AB, INPUT);
pinMode(OPL_DO_CD, INPUT);
pinMode(OPL_SY, INPUT);
pinMode(OPL_MASTERCLOCK, OUTPUT);
pinMode(13, OUTPUT);
//analogWriteResolution(1);
//analogWriteFrequency(OPL_MASTERCLOCK, Masterclock);
tone(OPL_MASTERCLOCK, Masterclock);
digitalWrite(OPL_CS, LOW);
delay(10);
digitalWrite(OPL_IC, HIGH);
delay(100);
digitalWrite(OPL_IC, LOW);
//Timer3.initialize(1); // 1.0e6/(Masterclock*2));
//Timer3.attachInterrupt(masterToggle);
}
int chAB[2] = {0,0};
int chCD[2] = {0,0};
int dataCtr = -1;
bool chBD = false; // second set of data?
void handleSy()
{
const bool acState = digitalRead(OPL_SMP_AC);
const bool bdState = digitalRead(OPL_SMP_BD);
if(dataCtr < 0) {
if( dataCtr == -1 ) {
if(acState) {
Serial.print("Sync start: ");
Serial.print(dataCtr);
Serial.print(" ");
Serial.print(acState);
Serial.print(" ");
Serial.println(bdState);
dataCtr = -2; // wait for fall
}
}
else if(dataCtr == -2) {
if(!acState) {
Serial.print("Sync stop: ");
Serial.print(dataCtr);
Serial.print(" ");
Serial.print(acState);
Serial.print(" ");
Serial.println(bdState);
dataCtr = 16; // wait 2 samples, then start
}
}
return;
}
int index = dataCtr%18;
int ofs = dataCtr/18;
if(digitalRead(OPL_DO_AB))
chAB[ofs] |= (1 << index);
if(digitalRead(OPL_DO_CD))
chCD[ofs] |= (1 << index);
++dataCtr;
//if(dataCtr == 36)
// Serial.println(chAB[0]);
dataCtr %= 36;
}
void loop() {
static bool s = false;
float delta = millis()-toggleTs;
if(delta>1000) {
toggleTs = millis();
Serial.print("Toggle: ");
//noInterrupts();
Serial.println(toggleCounter*1000/delta);
toggleCounter = 0;
//interrupts();
//digitalWrite(13, s ? HIGH : LOW);
s = !s;
}
Serial.print(digitalRead(OPL_SY));
Serial.print(" ");
Serial.print(digitalRead(OPL_DO_AB));
Serial.print(" ");
Serial.print(digitalRead(OPL_DO_CD));
Serial.print(" ");
Serial.print(digitalRead(OPL_SMP_AC));
Serial.print(" ");
Serial.print(digitalRead(OPL_SMP_BD));
Serial.print(" ");
Serial.print(digitalRead(OPL_TEST));
Serial.print(" ");
Serial.print(digitalRead(OPL_D7));
Serial.print(digitalRead(OPL_D6));
Serial.print(digitalRead(OPL_D5));
Serial.print(digitalRead(OPL_D4));
Serial.print(digitalRead(OPL_D3));
Serial.print(digitalRead(OPL_D2));
Serial.print(digitalRead(OPL_D1));
Serial.print(digitalRead(OPL_D0));
Serial.println("");
auto tm = pulseIn(OPL_SY, HIGH, 1e3);
if(tm != 0) {
Serial.print(SyRate);
Serial.print(" ");
Serial.println(1.0e6/tm);
}
handleSy();
}
Thanks in advance.