-
Notifications
You must be signed in to change notification settings - Fork 6
/
vessel.ino
592 lines (510 loc) · 14.4 KB
/
vessel.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Vessel
// Copyright 2019 Josh Bailey ([email protected])
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <Arduino.h>
#include <MIDI.h>
const char versionStr[] = {
0x16, // V
0x05, // E
0x13, // S
0x13, // S
0x05, // E
0x0C, // L
0x30, // 0
0x30, // 0
};
// TODO: custom optimized PIOC handler for PC2 int only.
// add void PIOC_Handler (void) __attribute__ ((weak)); to WInterrupts.c
void PIOC_Handler(void) {
// TODO: calculate PC2 mask.
if (PIOC->PIO_ISR & PIO_PC9) {
IoIsr();
}
}
// The C64 can send us a command by sending a reserved cmd byte, then another byte specifying the command number, then any other bytes the command requires.
// Each command has a fixed number of bytes expected after the command byte.
const byte vesselCmd = 0xfd;
#define PINDESC(pin) g_APinDescription[pin].ulPin
#define PINPPORT(pin, PPIO) g_APinDescription[pin].pPort->PPIO
#define DISABLE_PERIPHERAL_PIN(pin) g_APinDescription[pin].pPort->PIO_PER = g_APinDescription[pin].ulPin;
template<uint8_t pin>
class DigitalPin {
public:
DigitalPin(bool mode, bool value) {
pinMode(pin, mode);
write(value);
}
inline __attribute__((always_inline))
void high() { write(true); }
inline __attribute__((always_inline))
void low() { write(false); }
inline __attribute__((always_inline))
bool read() const {
return PINPPORT(pin, PIO_PDSR) & PINDESC(pin);
}
inline __attribute__((always_inline))
void write(bool value) {
if (value) {
PINPPORT(pin, PIO_SODR) = PINDESC(pin);
} else {
PINPPORT(pin, PIO_CODR) = PINDESC(pin);
}
}
};
// PC9 is 41
#define C64_PC2 41
#define C64_PA2 40
// 74 AKA PA25/MISO
#define C64_FLAG 74
#define DATA_DIR 44
#define CONT_DIR 43
#define C64_PB0 25
#define C64_PB1 26
#define C64_PB2 27
#define C64_PB3 28
#define C64_PB4 14
#define C64_PB5 15
#define C64_PB6 29
#define C64_PB7 11
const byte c64Pins[] = {C64_PB0, C64_PB1, C64_PB2, C64_PB3, C64_PB4, C64_PB5, C64_PB6, C64_PB7};
const uint32_t PB_PINS = 0xff; // PIO mask (PIO takes 32 bits, we want the lowest 8 bits)
const uint16_t OUT_BUF_SIZE = 256; // Ring buffer for bytes to send to C64
const uint16_t IN_BUF_SIZE = 256; // Ring buffer for bytes from C64.
DigitalPin<C64_PA2> pa2Pin(INPUT, LOW);
DigitalPin<C64_PC2> pc2Pin(INPUT, LOW);
DigitalPin<C64_FLAG> flagPin(OUTPUT, LOW);
DigitalPin<DATA_DIR> dataDirPin(OUTPUT, LOW);
DigitalPin<CONT_DIR> controlDirPin(OUTPUT, LOW);
DigitalPin<MOSI> statusPin(OUTPUT, LOW);
enum IsrModeEnum { ISR_INPUT, ISR_INPUT_CMD_BYTE, ISR_INPUT_CMD_DATA, ISR_OUTPUT, ISR_OUTPUT_DONE };
#define MAX_MIDI_CHANNEL 16
volatile byte inCmdBuf[IN_BUF_SIZE] = {};
volatile byte inBuf[IN_BUF_SIZE] = {};
volatile byte inCmdBufReadPtr = 0;
volatile byte inBufReadPtr = 0;
volatile byte inBufWritePtr = 0;
volatile byte outBuf[OUT_BUF_SIZE] = {};
volatile byte outBufWritePtr = 0;
volatile byte *outBufReadPtr = outBuf;
volatile IsrModeEnum isrMode = ISR_INPUT;
volatile bool nmiEnabled = false;
volatile bool transparent = false;
volatile bool status = false;
volatile uint16_t receiveChannelMask = 0;
volatile uint16_t receiveStatusMask = 0;
byte receiveCommandMask[MAX_MIDI_CHANNEL] = {};
void (*cmds[])(void) = {
resetCmd,
purgeCmd,
panicCmd,
versionCmd,
configFlagsCmd,
configChannelCmd,
configStatusCmd,
configCommandCmd,
};
void (*cmd)(void) = NULL;
const byte cmdLens[] = {
0,
0,
0,
0,
1,
2,
2,
1,
};
volatile byte cmdLen = 0;
const byte maxCmd = sizeof(cmdLens) - 1;
void inline blink() {
if (status) {
status = LOW;
} else {
status = HIGH;
}
statusPin.write(status);
}
class FakeSerial {
public:
void begin(int BaudRate __attribute__((unused))) {
_pending = false;
}
inline __attribute__((always_inline))
void set(byte i) {
c = i;
_pending = true;
}
inline __attribute__((always_inline))
byte read() {
_pending = false;
return c;
}
inline __attribute__((always_inline))
void write(byte i) {
outBuf[++(*outBufReadPtr)] = i;
}
inline __attribute__((always_inline))
unsigned available() {
if (_pending) {
return 1;
}
return 0;
}
byte c;
private:
bool _pending;
};
FakeSerial fs;
#define MIDI_CHANNEL MIDI_CHANNEL_OMNI
struct VesselSettings : public midi::DefaultSettings {
// cppcheck-suppress unusedStructMember
static const bool Use1ByteParsing = true;
static const int BaudRate = 0;
};
MIDI_CREATE_CUSTOM_INSTANCE(FakeSerial, fs, MIDI, VesselSettings);
#define NMI_WRAP(x) { if (nmiEnabled) { flagPin.write(HIGH); } x; }
#define NMI_MIDI_SEND(x) NMI_WRAP(MIDI.x)
#define NMI_STATUS_SEND(statusMsg, handler) if (statusMasked(statusMsg)) { NMI_MIDI_SEND(handler) }
#define NMI_CHANNEL_SEND(channel, handler) if (channelMasked(channel)) { NMI_MIDI_SEND(handler) }
#define NMI_COMMAND_SEND(command, channel, handler) if (commandMasked(command, channel)) { NMI_CHANNEL_SEND(channel, handler) }
inline void handleNoteOn(byte channel, byte note, byte velocity) {
NMI_COMMAND_SEND(1, channel, sendNoteOn(note, velocity, channel))
}
inline void handleNoteOff(byte channel, byte note, byte velocity) {
NMI_COMMAND_SEND(2, channel, sendNoteOff(note, velocity, channel))
}
inline void handleAfterTouchPoly(byte channel, byte note, byte pressure) {
NMI_COMMAND_SEND(3, channel, sendAfterTouch(note, pressure, channel))
}
inline void handleControlChange(byte channel, byte number, byte value) {
NMI_COMMAND_SEND(4, channel, sendControlChange(number, value, channel))
}
inline void handleProgramChange(byte channel, byte number) {
NMI_COMMAND_SEND(5, channel, sendProgramChange(number, channel))
}
inline void handleAfterTouchChannel(byte channel, byte pressure) {
NMI_COMMAND_SEND(6, channel, sendAfterTouch(pressure, channel))
}
inline void handlePitchBend(byte channel, int bend) {
NMI_COMMAND_SEND(7, channel, sendPitchBend(bend, channel))
}
inline void handleTimeCodeQuarterFrame(byte data) {
NMI_STATUS_SEND(midi::TimeCodeQuarterFrame, sendTimeCodeQuarterFrame(data))
}
inline void handleSongPosition(unsigned int beats) {
NMI_STATUS_SEND(midi::SongPosition, sendSongPosition(beats))
}
inline void handleSongSelect(byte songnumber) {
NMI_STATUS_SEND(midi::SongSelect, sendSongSelect(songnumber))
}
inline void handleTuneRequest() {
NMI_STATUS_SEND(midi::TuneRequest, sendTuneRequest())
}
inline void handleClock(void) {
NMI_STATUS_SEND(midi::Clock, sendClock())
}
inline void handleStart(void) {
NMI_STATUS_SEND(midi::Start, sendStart())
}
inline void handleContinue(void) {
NMI_STATUS_SEND(midi::Continue, sendContinue())
}
inline void handleStop(void) {
NMI_STATUS_SEND(midi::Stop, sendStop())
}
inline void handleSystemReset(void) {
NMI_STATUS_SEND(midi::SystemReset, sendSystemReset())
}
inline bool statusMasked(byte status) {
uint16_t mask = 1 << (status & 0xf);
return mask & receiveStatusMask;
}
inline bool channelMasked(byte channel) {
uint16_t mask = 1 << (channel - 1);
return mask & receiveChannelMask;
}
inline bool commandMasked(byte command, byte channel) {
return command & (receiveCommandMask[channel - 1]);
}
// TODO: would be cleaner to subclass UARTClass without interrupts or a ringbuffer.
// https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/UARTClass.cpp
// bypass all interrupt usage and do polling with our own ringbuffer.
inline bool uartTxready() {
return USART1->US_CSR & US_CSR_TXRDY;
}
inline void uartWrite(byte b) {
USART1->US_THR = b;
}
inline bool uartRxready() {
return USART1->US_CSR & US_CSR_RXRDY;
}
inline byte uartRead() {
return USART1->US_RHR;
}
inline void disableUartInts() {
USART2->US_IDR = 0xFFFFFFFF;
NVIC_ClearPendingIRQ(USART2_IRQn);
NVIC_DisableIRQ(USART2_IRQn);
USART1->US_IDR = 0xFFFFFFFF;
NVIC_ClearPendingIRQ(USART1_IRQn);
NVIC_DisableIRQ(USART1_IRQn);
USART0->US_IDR = 0xFFFFFFFF;
NVIC_ClearPendingIRQ(USART0_IRQn);
NVIC_DisableIRQ(USART0_IRQn);
}
inline void disablePeripherals() {
for (byte p = 0; p < sizeof(c64Pins); ++p) {
DISABLE_PERIPHERAL_PIN(c64Pins[p]);
}
DISABLE_PERIPHERAL_PIN(C64_PA2);
DISABLE_PERIPHERAL_PIN(C64_PC2);
DISABLE_PERIPHERAL_PIN(C64_FLAG);
}
inline bool inInputMode() {
return pa2Pin.read();
}
inline void setInputMode() {
dataDirPin.write(LOW);
REG_PIOD_ODR |= PB_PINS;
REG_PIOD_OWDR |= PB_PINS;
}
inline void setOutputMode() {
dataDirPin.write(HIGH);
REG_PIOD_OER |= PB_PINS;
REG_PIOD_OWER |= PB_PINS;
}
inline bool inBufWaiting() {
return inBufReadPtr != inBufWritePtr;
}
inline void drainInBuf() {
// Avoid buffering, ensure we write direct to UART without waiting.
if (inBufWaiting() && uartTxready()) {
uartWrite(inBuf[++inBufWritePtr]);
blink();
}
}
inline bool drainOutBuf() {
if (fs.available()) {
MIDI.read();
flagPin.write(LOW);
return true;
} else if (uartRxready()) {
if (transparent) {
NMI_WRAP(fs.write(uartRead()));
flagPin.write(LOW);
} else {
fs.set(uartRead());
}
blink();
return true;
}
return false;
}
inline void resetWritePtrs() {
outBufWritePtr = 0;
*outBufReadPtr = 0;
}
inline void inputMode() {
noInterrupts();
setInputMode();
isrMode = ISR_INPUT;
interrupts();
while (inInputMode()) {
if (!drainOutBuf()) {
drainInBuf();
}
}
}
byte getByte() {
return REG_PIOD_PDSR; // only need to return LSB
}
inline void readByte() {
volatile byte b = getByte();
if (b == vesselCmd) {
inCmdBufReadPtr = 0;
inCmdBuf[0] = b;
isrMode = ISR_INPUT_CMD_BYTE;
} else {
inBuf[++inBufReadPtr] = b;
blink();
}
}
inline void readCmdByte() {
inCmdBuf[++inCmdBufReadPtr] = getByte();
byte cmdNo = inCmdBuf[1];
if (cmdNo > maxCmd) {
isrMode = ISR_INPUT;
} else {
cmdLen = cmdLens[cmdNo];
cmd = cmds[cmdNo];
if (cmdLen) {
isrMode = ISR_INPUT_CMD_DATA;
} else {
runCmd();
}
}
}
inline void versionCmd() {
NMI_WRAP({
for (byte i = 0; i < sizeof(versionStr); ++i) {
fs.write(versionStr[i]);
}})
flagPin.write(LOW);
}
inline void resetCmd() {
receiveChannelMask = 0;
receiveStatusMask = 0;
memset(receiveCommandMask, 0, sizeof(receiveCommandMask));
nmiEnabled = false;
transparent = false;
MIDI.turnThruOff();
purgeCmd();
}
inline void purgeCmd() {
resetWritePtrs();
}
inline void panicCmd() {
// TODO: not yet implemented.
}
inline void configFlagsCmd() {
byte configFlags = inCmdBuf[2];
nmiEnabled = configFlags & 1;
if (configFlags & 2) {
MIDI.turnThruOn();
} else {
MIDI.turnThruOff();
}
transparent = configFlags & 4;
}
inline uint16_t get2bMask() {
return (inCmdBuf[2] << 8) + inCmdBuf[3];
}
inline byte getLoNib() {
return inCmdBuf[2] & 0x0f;
}
inline byte getHighNib() {
return (inCmdBuf[2] & 0xf0) >> 4;
}
inline void configChannelCmd() {
receiveChannelMask = get2bMask();
}
inline void configStatusCmd() {
receiveStatusMask = get2bMask();
}
inline void configCommandCmd() {
byte channel = getLoNib();
byte mask = getHighNib() & 0x07;
receiveCommandMask[channel] = mask;
}
inline void runCmd() {
isrMode = ISR_INPUT;
(*cmd)();
}
inline void readCmdData() {
inCmdBuf[++inCmdBufReadPtr] = getByte();
if (inCmdBufReadPtr > cmdLen) {
runCmd();
}
}
inline void IoIsr() {
switch (isrMode) {
case ISR_INPUT:
{
readByte();
}
break;
case ISR_INPUT_CMD_BYTE:
{
readCmdByte();
}
break;
case ISR_INPUT_CMD_DATA:
{
readCmdData();
}
break;
case ISR_OUTPUT:
{
writeByte();
// Last byte, and at least one byte written, reset for next cycle.
if (outBufWritePtr > *outBufReadPtr) {
isrMode = ISR_OUTPUT_DONE;
resetWritePtrs();
}
}
break;
case ISR_OUTPUT_DONE:
break;
default:
break;
}
}
inline void setByte(byte b) {
REG_PIOD_ODSR = b;
}
inline void writeByte() {
setByte(outBuf[outBufWritePtr++]);
}
inline void outputMode() {
noInterrupts();
setOutputMode();
writeByte();
if (*outBufReadPtr) {
isrMode = ISR_OUTPUT;
blink();
} else {
isrMode = ISR_OUTPUT_DONE;
resetWritePtrs();
}
interrupts();
while (!inInputMode()) {
drainInBuf();
}
}
void dummyIsr() {
}
void setup() {
statusPin.write(LOW);
SerialUSB.begin(115200);
Serial2.begin(31250);
while (Serial2.available()) {
Serial2.read();
}
disableUartInts();
disablePeripherals();
REG_PMC_PCER0 |= (1UL << ID_PIOD); // enable PIO controller.
controlDirPin.write(LOW);
flagPin.write(LOW);
resetWritePtrs();
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.turnThruOff();
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.setHandleAfterTouchPoly(handleAfterTouchPoly);
MIDI.setHandleControlChange(handleControlChange);
MIDI.setHandleProgramChange(handleProgramChange);
MIDI.setHandleAfterTouchChannel(handleAfterTouchChannel);
MIDI.setHandlePitchBend(handlePitchBend);
MIDI.setHandleStart(handleStart);
MIDI.setHandleContinue(handleContinue);
MIDI.setHandleStop(handleStop);
MIDI.setHandleClock(handleClock);
MIDI.setHandleSystemReset(handleSystemReset);
MIDI.setHandleTuneRequest(handleTuneRequest);
MIDI.setHandleTimeCodeQuarterFrame(handleTimeCodeQuarterFrame);
MIDI.setHandleSongPosition(handleSongPosition);
MIDI.setHandleSongSelect(handleSongSelect);
resetCmd();
attachInterrupt(digitalPinToInterrupt(C64_PC2), dummyIsr, RISING);
detachInterrupt(digitalPinToInterrupt(C64_PA2));
while (!inInputMode()) { };
}
void loop() {
// turnaround time between modes is critical
for (;;) {
inputMode();
outputMode();
}
}