Compare commits
41 Commits
aaf709b0c9
...
sn76489an
Author | SHA1 | Date | |
---|---|---|---|
8f777c9ac4 | |||
aeb7aeb7f2 | |||
20c01f2efa
|
|||
40cb04bde5 | |||
a1457e6a69 | |||
0303bbdc3c | |||
162bdaefee | |||
b9fd0099a8 | |||
d09f8d240f
|
|||
ed6da383de
|
|||
9328e22425 | |||
48b9fc7578
|
|||
d4d494ae7b | |||
1ebf85cb9d | |||
5a8323bddb | |||
97dc6ede70 | |||
a4563cd393 | |||
b435396d67 | |||
38ea1a7fdb | |||
6632630303 | |||
fdb524c8d4 | |||
2c3bccd147 | |||
ee98ba12a3 | |||
33242a09c1
|
|||
2097280f2b
|
|||
70b3b25a9d | |||
5e1e9dfa92 | |||
f397d0737b
|
|||
1c2414463b | |||
2e629f12aa | |||
9989a52c38 | |||
73d2bbc730 | |||
5e2f120432 | |||
152f171c66 | |||
9ddb747f16 | |||
bd11d12620 | |||
e5c6669284 | |||
202e91bfb6 | |||
48c83f0b2d | |||
d8e34ec209 | |||
5a491140c7 |
BIN
display-driver/docs/MCU-Connection and Pattern.pdf
Executable file
BIN
display-driver/docs/MCU-Connection and Pattern.pdf
Executable file
Binary file not shown.
BIN
display-driver/docs/Pinout-Display.pdf
Executable file
BIN
display-driver/docs/Pinout-Display.pdf
Executable file
Binary file not shown.
@ -7,11 +7,11 @@ MCU=msp430g2553
|
|||||||
CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
|
CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
|
||||||
|
|
||||||
# for debugging
|
# for debugging
|
||||||
#CFLAGS+= -g3 -ggdb -gdwarf-2
|
CFLAGS+= -g3 -ggdb -gdwarf-2
|
||||||
|
|
||||||
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
|
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
|
||||||
|
|
||||||
$(ARTIFACT).elf: main.o
|
$(ARTIFACT).elf: main.o scheduler.o spi.o sequencer.o melody.o ay_3_8913.o
|
||||||
$(CC) -o $@ $(LDFLAGS) $^
|
$(CC) -o $@ $(LDFLAGS) $^
|
||||||
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt
|
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt
|
||||||
|
|
||||||
|
190
sound-driver/ay_3_8913.c
Normal file
190
sound-driver/ay_3_8913.c
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
#include <msp430g2553.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "psg.h"
|
||||||
|
#include "scheduler.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const uint16_t frequencyCodes[8][12] = {
|
||||||
|
// C, Cis, D, Dis, E, F, Fis, G, Gis, A, Ais, H
|
||||||
|
{ 06535, 06234, 05747, 05474, 05233, 05002, 04563, 04353, 04153, 03762, 03600, 03424 }, // Octave 1
|
||||||
|
{ 03256, 03116, 02764, 02636, 02515, 02401, 02271, 02165, 02065, 01771, 01700, 01612 }, // Octave 2
|
||||||
|
{ 01527, 01447, 01372, 01317, 01247, 01201, 01135, 01073, 01033, 00774, 00740, 00705 }, // Octave 3
|
||||||
|
{ 00654, 00624, 00575, 00550, 00523, 00500, 00456, 00435, 00415, 00376, 00360, 00342 }, // Octave 4
|
||||||
|
{ 00326, 00312, 00276, 00264, 00252, 00240, 00227, 00217, 00207, 00177, 00170, 00161 }, // Octave 5
|
||||||
|
{ 00153, 00145, 00137, 00132, 00125, 00120, 00114, 00107, 00103, 00100, 00074, 00071 }, // Octave 6
|
||||||
|
{ 00065, 00062, 00060, 00055, 00052, 00050, 00046, 00044, 00042, 00040, 00036, 00034 }, // Octave 7
|
||||||
|
{ 00033, 00031, 00030, 00026, 00025, 00024, 00023, 00022, 00021, 00020, 00017, 00016 } // Octave 8
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define ADDR_DATA_REG P2OUT
|
||||||
|
#define BUS_CTRL_REG P1OUT
|
||||||
|
#define BC1 BIT3
|
||||||
|
#define BDIR BIT1
|
||||||
|
#define _CS0 BIT2
|
||||||
|
#define _CS1 BIT0
|
||||||
|
|
||||||
|
#define R0 0
|
||||||
|
#define CHANNEL_A_TONE_PERIOD_FINE_REG R0
|
||||||
|
#define R1 1
|
||||||
|
#define CHANNEL_A_TONE_PERIOD_COARSE_REG R1
|
||||||
|
#define R2 2
|
||||||
|
#define CHANNEL_B_TONE_PERIOD_FINE_REG R2
|
||||||
|
#define R3 3
|
||||||
|
#define CHANNEL_B_TONE_PERIOD_COARSE_REG R3
|
||||||
|
#define R4 4
|
||||||
|
#define CHANNEL_C_TONE_PERIOD_FINE_REG R4
|
||||||
|
#define R5 5
|
||||||
|
#define CHANNEL_C_TONE_PERIOD_COARSE_REG R5
|
||||||
|
#define R6 6
|
||||||
|
#define NOISE_PERIOD_REG R6
|
||||||
|
#define R7 7
|
||||||
|
#define _ENABLE_REG R7
|
||||||
|
#define R10 010
|
||||||
|
#define CHANNEL_A_AMPLITUDE_REG R10
|
||||||
|
#define R11 011
|
||||||
|
#define CHANNEL_B_AMPLITUDE_REG R11
|
||||||
|
#define R12 012
|
||||||
|
#define CHANNEL_C_AMPLITUDE_REG R12
|
||||||
|
#define R13 013
|
||||||
|
#define ENVELOPE_PERIOD_FINE_REG R13
|
||||||
|
#define R14 014
|
||||||
|
#define ENVELOPE_PERIOD_COARSE_REG R13
|
||||||
|
#define R15 015
|
||||||
|
#define ENVELOPE_SHAPE_REG R15
|
||||||
|
|
||||||
|
uint8_t psgShadowRegisters[14];
|
||||||
|
|
||||||
|
inline static void BUS_OP_NACT() {
|
||||||
|
BUS_CTRL_REG &= ~(BDIR | BC1);
|
||||||
|
}
|
||||||
|
inline static void BUS_OP_INTAK() {
|
||||||
|
BUS_CTRL_REG |= BDIR | BC1;
|
||||||
|
}
|
||||||
|
inline static void BUS_OP_DWS() {
|
||||||
|
BUS_CTRL_REG |= BDIR;
|
||||||
|
BUS_CTRL_REG &= ~BC1;
|
||||||
|
}
|
||||||
|
inline static void BUS_OP_CS0_ENABLE() {
|
||||||
|
BUS_CTRL_REG &= ~_CS0;
|
||||||
|
}
|
||||||
|
inline static void BUS_OP_CS0_DISABLE() {
|
||||||
|
BUS_CTRL_REG |= _CS0;
|
||||||
|
}
|
||||||
|
inline static void BUS_OP_CS1_ENABLE() {
|
||||||
|
BUS_CTRL_REG &= ~_CS1;
|
||||||
|
}
|
||||||
|
inline static void BUS_OP_CS1_DISABLE() {
|
||||||
|
BUS_CTRL_REG |= _CS1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
static void delay() {
|
||||||
|
asm volatile (
|
||||||
|
"push r12\n"
|
||||||
|
"mov.w #5, r12\n"
|
||||||
|
"loop:\n"
|
||||||
|
"dec.w r12\n"
|
||||||
|
"jnz loop\n"
|
||||||
|
"pop r12\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static uint8_t psgReadShadow(uint8_t address) {
|
||||||
|
return psgShadowRegisters[address];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void psgWrite(uint8_t address, uint8_t data) {
|
||||||
|
psgShadowRegisters[address] = data;
|
||||||
|
|
||||||
|
// according to "State Timing" (p. 15) of datasheet
|
||||||
|
|
||||||
|
BUS_OP_CS1_ENABLE();
|
||||||
|
|
||||||
|
// put bus into inactive state
|
||||||
|
BUS_OP_NACT();
|
||||||
|
|
||||||
|
// put address on bus
|
||||||
|
ADDR_DATA_REG = address;
|
||||||
|
|
||||||
|
// address latch mode
|
||||||
|
BUS_OP_INTAK();
|
||||||
|
|
||||||
|
// latch address
|
||||||
|
BUS_OP_NACT();
|
||||||
|
|
||||||
|
// put data on bus
|
||||||
|
ADDR_DATA_REG = data;
|
||||||
|
|
||||||
|
// set write to psg
|
||||||
|
BUS_OP_DWS();
|
||||||
|
|
||||||
|
// set inactive again
|
||||||
|
BUS_OP_NACT();
|
||||||
|
|
||||||
|
BUS_OP_CS1_DISABLE();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void psgWriteFrequency(uint8_t channel, uint16_t frequencyCode) {
|
||||||
|
psgWrite(CHANNEL_A_TONE_PERIOD_FINE_REG + (channel * 2), (frequencyCode & 0x00ff));
|
||||||
|
psgWrite(CHANNEL_A_TONE_PERIOD_COARSE_REG + (channel * 2), ((frequencyCode >> 8) & 0x000f));
|
||||||
|
}
|
||||||
|
|
||||||
|
void psgPlayTone(uint8_t channel, uint8_t volume, t_octave octave, t_note note) {
|
||||||
|
if (note == e_Pause) {
|
||||||
|
psgWrite(_ENABLE_REG, psgReadShadow(_ENABLE_REG) | (1 << channel));
|
||||||
|
} else {
|
||||||
|
psgWrite(_ENABLE_REG, psgReadShadow(_ENABLE_REG) & ~(1 << channel));
|
||||||
|
psgAmplitude(channel, volume);
|
||||||
|
psgWriteFrequency(channel, frequencyCodes[octave][note]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void psgAmplitude(uint8_t channel, uint8_t volume) {
|
||||||
|
psgWrite(CHANNEL_A_AMPLITUDE_REG + channel, volume);
|
||||||
|
}
|
||||||
|
|
||||||
|
void psgInit() {
|
||||||
|
// address/data bus
|
||||||
|
P2DIR = 0xff;
|
||||||
|
P2SEL = 0;
|
||||||
|
P2SEL2 = 0;
|
||||||
|
|
||||||
|
// sound chip reset
|
||||||
|
// BIT2: /RST
|
||||||
|
// P1DIR |= BIT2;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// put sound chip into reset state
|
||||||
|
P1OUT &= ~BIT2;
|
||||||
|
delay();
|
||||||
|
delay();
|
||||||
|
delay();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// bus control lines
|
||||||
|
// BIT3: BC1
|
||||||
|
// BIT1: BDIR
|
||||||
|
// BIT0: _CS1
|
||||||
|
// BIT2: _CS0
|
||||||
|
P1DIR |= BIT0 | BIT1 | BIT2 | BIT3 ;
|
||||||
|
|
||||||
|
// put bus into inactive state
|
||||||
|
BUS_CTRL_REG &= ~(BDIR | BC1);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
// release sound chip from reset state
|
||||||
|
P1OUT |= BIT2;
|
||||||
|
delay();
|
||||||
|
delay();
|
||||||
|
delay();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// disable everything
|
||||||
|
psgWrite(_ENABLE_REG, 0xff);
|
||||||
|
}
|
||||||
|
|
BIN
sound-driver/docs/Audio-Amplifier.pdf
Executable file
BIN
sound-driver/docs/Audio-Amplifier.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/Clock-Generation.pdf
Executable file
BIN
sound-driver/docs/Clock-Generation.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/Frequency-Codes-76489AN.xlsx
Normal file
BIN
sound-driver/docs/Frequency-Codes-76489AN.xlsx
Normal file
Binary file not shown.
BIN
sound-driver/docs/Frequency-Codes.pdf
Executable file
BIN
sound-driver/docs/Frequency-Codes.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/Korobeiniki.svg.png
Normal file
BIN
sound-driver/docs/Korobeiniki.svg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
BIN
sound-driver/docs/MCU-Connection.pdf
Executable file
BIN
sound-driver/docs/MCU-Connection.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/Oktavskala.pdf
Executable file
BIN
sound-driver/docs/Oktavskala.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/Registers.pdf
Executable file
BIN
sound-driver/docs/Registers.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/TetrisThemeThreeVoices.pdf
Executable file
BIN
sound-driver/docs/TetrisThemeThreeVoices.pdf
Executable file
Binary file not shown.
BIN
sound-driver/docs/ay3-8910.pdf
Normal file
BIN
sound-driver/docs/ay3-8910.pdf
Normal file
Binary file not shown.
BIN
sound-driver/docs/lengths.xlsx
Normal file
BIN
sound-driver/docs/lengths.xlsx
Normal file
Binary file not shown.
2
sound-driver/firmware.gdb
Normal file
2
sound-driver/firmware.gdb
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
target remote localhost:2000
|
||||||
|
file firmware.elf
|
@ -2,14 +2,11 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "spi.h"
|
||||||
|
#include "psg.h"
|
||||||
void __attribute__ ((interrupt (USCIAB0RX_VECTOR))) receive() {
|
#include "scheduler.h"
|
||||||
if (UC0IFG & UCB0RXIFG) {
|
#include "sequencer.h"
|
||||||
// receive an octet
|
#include "melody.h"
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
WDTCTL = WDTPW | WDTHOLD;
|
WDTCTL = WDTPW | WDTHOLD;
|
||||||
@ -22,22 +19,19 @@ int main() {
|
|||||||
BCSCTL2 = 0;
|
BCSCTL2 = 0;
|
||||||
BCSCTL3 = 0;
|
BCSCTL3 = 0;
|
||||||
|
|
||||||
// SPI slave
|
|
||||||
// BIT4: UCB0STE
|
|
||||||
// BIT5: UCB0CLK
|
|
||||||
// BIT6: UCB0SOMI
|
|
||||||
// BIT7: UCB0SIMO
|
|
||||||
P1SEL |= BIT4 | BIT5 | BIT6 | BIT7;
|
|
||||||
P1SEL2 |= BIT4 | BIT5 | BIT6 | BIT7;
|
|
||||||
// most significant bit first, enable STE
|
|
||||||
UCB0CTL0 = UCSYNC | UCMSB | UCMODE_2;
|
|
||||||
UCB0CTL1 = 0x00;
|
|
||||||
// enable RX interrupt
|
|
||||||
UC0IE |= UCB0RXIE;
|
|
||||||
|
|
||||||
|
schInit();
|
||||||
|
|
||||||
|
// spiInit();
|
||||||
|
psgInit();
|
||||||
|
sequencerInit();
|
||||||
|
|
||||||
__enable_interrupt();
|
__enable_interrupt();
|
||||||
|
|
||||||
|
melodyInit();
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
schExec();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1051
sound-driver/melody.c
Normal file
1051
sound-driver/melody.c
Normal file
File diff suppressed because it is too large
Load Diff
8
sound-driver/melody.h
Normal file
8
sound-driver/melody.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _MELODY_H_
|
||||||
|
#define _MELODY_H_
|
||||||
|
|
||||||
|
|
||||||
|
void melodyInit();
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _MELODY_H_
|
46
sound-driver/psg.h
Normal file
46
sound-driver/psg.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#ifndef _PSG_H_
|
||||||
|
#define _PSG_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
e_O_1 = 0,
|
||||||
|
e_O_2,
|
||||||
|
e_O_3,
|
||||||
|
e_O_4,
|
||||||
|
e_O_5,
|
||||||
|
e_O_6,
|
||||||
|
e_O_7,
|
||||||
|
e_O_8,
|
||||||
|
e_O_Null
|
||||||
|
} t_octave;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
e_C = 0,
|
||||||
|
e_Cis,
|
||||||
|
e_D,
|
||||||
|
e_Dis,
|
||||||
|
e_E,
|
||||||
|
e_F,
|
||||||
|
e_Fis,
|
||||||
|
e_G,
|
||||||
|
e_Gis,
|
||||||
|
e_A,
|
||||||
|
e_Ais,
|
||||||
|
e_H,
|
||||||
|
e_Pause,
|
||||||
|
e_Null
|
||||||
|
} t_note;
|
||||||
|
|
||||||
|
#define e_Es e_Dis
|
||||||
|
#define e_As e_Gis
|
||||||
|
#define e_B e_Ais
|
||||||
|
|
||||||
|
|
||||||
|
void psgInit();
|
||||||
|
|
||||||
|
void psgPlayTone(uint8_t channel, uint8_t volume, t_octave octave, t_note note);
|
||||||
|
void psgAmplitude(uint8_t channel, uint8_t volume);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _PSG_H_
|
90
sound-driver/scheduler.c
Normal file
90
sound-driver/scheduler.c
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <msp430g2553.h>
|
||||||
|
#include "scheduler.h"
|
||||||
|
|
||||||
|
tTask tasks[MAX_NUM_OF_TASKS];
|
||||||
|
|
||||||
|
|
||||||
|
void schInit() {
|
||||||
|
TACCR0 = 19600;
|
||||||
|
TACCTL0 = CCIE;
|
||||||
|
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||||
|
tasks[i].delay = 0;
|
||||||
|
tasks[i].period = 0;
|
||||||
|
tasks[i].run = 0;
|
||||||
|
tasks[i].exec = NULL;
|
||||||
|
tasks[i].handle = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void __attribute__ ((interrupt (TIMER0_A0_VECTOR))) schUpdate() {
|
||||||
|
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||||
|
if (tasks[i].exec != NULL) {
|
||||||
|
if (tasks[i].delay == 0) {
|
||||||
|
tasks[i].delay = tasks[i].period;
|
||||||
|
tasks[i].run++;
|
||||||
|
} else {
|
||||||
|
tasks[i].delay--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period) {
|
||||||
|
uint16_t taskId = 0xffff;
|
||||||
|
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||||
|
if (tasks[i].exec == NULL) {
|
||||||
|
tasks[i].delay = delay;
|
||||||
|
tasks[i].period = period;
|
||||||
|
if (delay == 0 && period == 0) {
|
||||||
|
tasks[i].run = 1;
|
||||||
|
} else {
|
||||||
|
tasks[i].run = 0;
|
||||||
|
}
|
||||||
|
tasks[i].exec = exec;
|
||||||
|
tasks[i].handle = handle;
|
||||||
|
taskId = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return taskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void schDel(void (*exec)(void *), void *handle) {
|
||||||
|
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||||
|
if ((tasks[i].exec == exec) && (tasks[i].handle == handle)) {
|
||||||
|
tasks[i].exec = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
void schDel(uint16_t taskId) {
|
||||||
|
tasks[taskId].exec = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void schExec() {
|
||||||
|
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||||
|
// synchronize access to tasks[].run
|
||||||
|
__disable_interrupt();
|
||||||
|
if (tasks[i].exec != NULL && tasks[i].run > 0) {
|
||||||
|
tasks[i].run--;
|
||||||
|
// synchronize access to tasks[].run
|
||||||
|
// reenable interrupts before actually executing task
|
||||||
|
__enable_interrupt();
|
||||||
|
tasks[i].exec(tasks[i].handle);
|
||||||
|
if (tasks[i].period == 0) {
|
||||||
|
tasks[i].exec = NULL;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// synchronize access to tasks[].run
|
||||||
|
// reenable interrupts in case task is not yet executable
|
||||||
|
__enable_interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
28
sound-driver/scheduler.h
Normal file
28
sound-driver/scheduler.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef PONTCOOPSCHEDULER_H_
|
||||||
|
#define PONTCOOPSCHEDULER_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_NUM_OF_TASKS 8
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t delay;
|
||||||
|
uint16_t period;
|
||||||
|
uint8_t run;
|
||||||
|
void (*exec)(void *handle);
|
||||||
|
void *handle;
|
||||||
|
} tTask;
|
||||||
|
|
||||||
|
|
||||||
|
void schInit();
|
||||||
|
uint16_t schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period);
|
||||||
|
// void schDel(void (*exec)(void *), void *handle);
|
||||||
|
void schDel(uint16_t taskId);
|
||||||
|
void schExec();
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* PONTCOOPSCHEDULER_H_ */
|
99
sound-driver/sequencer.c
Normal file
99
sound-driver/sequencer.c
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include "sequencer.h"
|
||||||
|
#include "scheduler.h"
|
||||||
|
#include "psg.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void sequencerInit() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void sequencerExec(void *handle) {
|
||||||
|
static uint16_t lengths[e_L_LengthEnd];
|
||||||
|
t_melodies *melodies = (t_melodies*) handle;
|
||||||
|
|
||||||
|
if (melodies->firstRun) {
|
||||||
|
melodies->firstRun = false;
|
||||||
|
|
||||||
|
lengths[e_L_1_4] = 60000 / melodies->pace; // duration of a 1/4 tone in ms
|
||||||
|
lengths[e_L_1_2] = lengths[e_L_1_4] << 1;
|
||||||
|
lengths[e_L_1] = lengths[e_L_1_4] << 2;
|
||||||
|
lengths[e_L_1_8] = lengths[e_L_1_4] >> 1;
|
||||||
|
lengths[e_L_1_16] = lengths[e_L_1_4] >> 2;
|
||||||
|
lengths[e_L_1_32] = lengths[e_L_1_4] >> 4;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < e_L_LengthEnd; i++) {
|
||||||
|
lengths[i] /= SEQUENCER_PERIOD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t channel = 0; channel < melodies->numOfMelodies; channel++) {
|
||||||
|
t_melody *melody = &(melodies->melodies[channel]);
|
||||||
|
|
||||||
|
switch (melody->state) {
|
||||||
|
case e_Init:
|
||||||
|
melody->state = e_PlayTone;
|
||||||
|
break;
|
||||||
|
case e_PlayTone:
|
||||||
|
if (melody->tones[melody->idx].length == e_L_SyncMark) {
|
||||||
|
if (melodies->sync == 0) {
|
||||||
|
melodies->sync = melodies->numOfMelodies;
|
||||||
|
}
|
||||||
|
melodies->sync -= 1;
|
||||||
|
melody->state = e_Sync;
|
||||||
|
} else {
|
||||||
|
if (melody->tones[melody->idx].length == e_L_EndMark) {
|
||||||
|
melody->idx = 0;
|
||||||
|
}
|
||||||
|
psgPlayTone(channel, melody->amplitude, melody->tones[melody->idx].octave, melody->tones[melody->idx].note);
|
||||||
|
melody->lengthCnt = (melody->tones[melody->idx].staccato) ? (lengths[melody->tones[melody->idx].length] / 2) : lengths[melody->tones[melody->idx].length];
|
||||||
|
melody->state = e_HoldTone;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case e_Sync:
|
||||||
|
if (melodies->sync == 0) {
|
||||||
|
melody->state = e_SeparateTone;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case e_HoldTone:
|
||||||
|
melody->lengthCnt -= 1;
|
||||||
|
if (melody->lengthCnt == 0) {
|
||||||
|
melody->state = (melody->tones[melody->idx].staccato) ? e_StaccatoBreak : e_SeparateTone;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case e_StaccatoBreak:
|
||||||
|
psgPlayTone(channel, 0, e_O_Null, e_Pause);
|
||||||
|
melody->lengthCnt = lengths[melody->tones[melody->idx].length] / 2;
|
||||||
|
melody->state = e_HoldStaccatoBreak;
|
||||||
|
break;
|
||||||
|
case e_HoldStaccatoBreak:
|
||||||
|
melody->lengthCnt -= 1;
|
||||||
|
if (melody->lengthCnt == 0) {
|
||||||
|
melody->state = e_SeparateTone;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case e_SeparateTone:
|
||||||
|
if (! (melody->tones[melody->idx].legato)) {
|
||||||
|
psgPlayTone(channel, 0, e_O_Null, e_Pause);
|
||||||
|
}
|
||||||
|
melody->idx += 1;
|
||||||
|
melody->state = e_PlayTone;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t sequencerPlayMelodies(t_melodies *melodies) {
|
||||||
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
|
melodies->melodies[i].idx = 0;
|
||||||
|
melodies->melodies[i].lengthCnt = 0;
|
||||||
|
melodies->melodies[i].state = e_Init;
|
||||||
|
}
|
||||||
|
melodies->sync = 0;
|
||||||
|
melodies->firstRun = true;
|
||||||
|
|
||||||
|
return schAdd(sequencerExec, (void*) melodies, 0, SEQUENCER_PERIOD);
|
||||||
|
}
|
||||||
|
|
61
sound-driver/sequencer.h
Normal file
61
sound-driver/sequencer.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#ifndef _SEQUENCER_H_
|
||||||
|
#define _SEQUENCER_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "psg.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
e_L_1 = 0,
|
||||||
|
e_L_1_2 = 1,
|
||||||
|
e_L_1_4 = 2,
|
||||||
|
e_L_1_8 = 3,
|
||||||
|
e_L_1_16 = 4,
|
||||||
|
e_L_1_32 = 5,
|
||||||
|
e_L_LengthEnd = 6,
|
||||||
|
e_L_EndMark = 254,
|
||||||
|
e_L_SyncMark = 255,
|
||||||
|
} t_noteLength;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
t_octave octave;
|
||||||
|
t_note note;
|
||||||
|
t_noteLength length;
|
||||||
|
bool legato;
|
||||||
|
bool staccato;
|
||||||
|
} t_tone;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
e_Init,
|
||||||
|
e_PlayTone,
|
||||||
|
e_Sync,
|
||||||
|
e_HoldTone,
|
||||||
|
e_StaccatoBreak,
|
||||||
|
e_HoldStaccatoBreak,
|
||||||
|
e_SeparateTone
|
||||||
|
} t_sequencerState;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t idx;
|
||||||
|
uint16_t lengthCnt;
|
||||||
|
t_sequencerState state;
|
||||||
|
uint8_t amplitude;
|
||||||
|
const t_tone *tones;
|
||||||
|
} t_melody;
|
||||||
|
|
||||||
|
#define SEQUENCER_PERIOD 4 // ms
|
||||||
|
#define NUM_OF_CHANNELS 3
|
||||||
|
typedef struct {
|
||||||
|
uint8_t numOfMelodies;
|
||||||
|
bool firstRun;
|
||||||
|
uint8_t pace; // quarter notes per minute
|
||||||
|
uint8_t sync;
|
||||||
|
t_melody melodies[NUM_OF_CHANNELS];
|
||||||
|
} t_melodies;
|
||||||
|
|
||||||
|
void sequencerInit();
|
||||||
|
uint16_t sequencerPlayMelodies(t_melodies *melodies);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _SEQUENCER_H_
|
171
sound-driver/sn76489an.c
Normal file
171
sound-driver/sn76489an.c
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
#include <msp430g2553.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "psg.h"
|
||||||
|
#include "scheduler.h"
|
||||||
|
|
||||||
|
|
||||||
|
// generated using utils/calc-76489an.py
|
||||||
|
const uint16_t frequencyCodes[8][12] = {
|
||||||
|
{ 3420, 3229, 3047, 2876, 2715, 2562, 2419, 2283, 2155, 2034, 1920, 1812 },
|
||||||
|
{ 1710, 1614, 1524, 1438, 1357, 1281, 1209, 1141, 1077, 1017, 960, 906 },
|
||||||
|
{ 855, 807, 762, 719, 679, 641, 605, 571, 539, 508, 480, 453 },
|
||||||
|
{ 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226 },
|
||||||
|
{ 214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113 },
|
||||||
|
{ 107, 101, 95, 90, 85, 80, 76, 71, 67, 64, 60, 57 },
|
||||||
|
{ 53, 50, 48, 45, 42, 40, 38, 36, 34, 32, 30, 28 },
|
||||||
|
{ 27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14 }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define ADDR_DATA_REG P2OUT
|
||||||
|
#define BUS_CTRL_REG P1OUT
|
||||||
|
#define BUS_CTRL_IN_REG P1IN
|
||||||
|
#define _CS0 BIT0
|
||||||
|
#define _CS1 BIT1
|
||||||
|
#define _WE BIT2
|
||||||
|
#define READY BIT3
|
||||||
|
|
||||||
|
#define CHANNEL_A_PERIOD_ADDR 0
|
||||||
|
#define CHANNEL_A_ATTEN_ADDR 1
|
||||||
|
#define CHANNEL_B_PERIOD_ADDR 2
|
||||||
|
#define CHANNEL_B_ATTEN_ADDR 3
|
||||||
|
#define CHANNEL_C_PERIOD_ADDR 4
|
||||||
|
#define CHANNEL_C_ATTEN_ADDR 5
|
||||||
|
|
||||||
|
#define IGNORE_OCTET 0xff
|
||||||
|
|
||||||
|
uint8_t psgAmplitudeShadowValue[3];
|
||||||
|
|
||||||
|
static void delay() {
|
||||||
|
asm volatile (
|
||||||
|
"push r12\n"
|
||||||
|
"mov.w #5, r12\n"
|
||||||
|
"loop:\n"
|
||||||
|
"dec.w r12\n"
|
||||||
|
"jnz loop\n"
|
||||||
|
"pop r12\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline static void WRITE_CYCLE(uint8_t chipNo) {
|
||||||
|
if (chipNo == 0) {
|
||||||
|
BUS_CTRL_REG &= ~_CS0;
|
||||||
|
} else {
|
||||||
|
BUS_CTRL_REG &= ~_CS1;
|
||||||
|
}
|
||||||
|
|
||||||
|
BUS_CTRL_REG &= ~_WE;
|
||||||
|
|
||||||
|
delay();
|
||||||
|
|
||||||
|
while ((BUS_CTRL_IN_REG & READY) == 0);
|
||||||
|
|
||||||
|
BUS_CTRL_REG |= _WE;
|
||||||
|
|
||||||
|
if (chipNo == 0) {
|
||||||
|
BUS_CTRL_REG |= _CS0;
|
||||||
|
} else {
|
||||||
|
BUS_CTRL_REG |= _CS1;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void psgWrite(uint8_t chipNo, uint8_t value) {
|
||||||
|
ADDR_DATA_REG = value;
|
||||||
|
WRITE_CYCLE(chipNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void psgWriteFrequency(uint8_t channel, uint16_t frequencyCode) {
|
||||||
|
uint8_t chipNo = channel / 3;
|
||||||
|
uint8_t regAddr = (channel % 3) * 2;
|
||||||
|
|
||||||
|
// bit order in frequncyCode and order in octet on data bus are reversed
|
||||||
|
// see datacheat cp. 1 and cp. 6
|
||||||
|
uint8_t firstOctet = 0x01;
|
||||||
|
firstOctet |= ((regAddr & 0x04) > 1);
|
||||||
|
firstOctet |= ((regAddr & 0x02) < 1);
|
||||||
|
firstOctet |= ((regAddr & 0x01) < 3);
|
||||||
|
uint8_t lowerPart = frequencyCode & 0x0f;
|
||||||
|
firstOctet |= ((lowerPart & 0x08) << 1);
|
||||||
|
firstOctet |= ((lowerPart & 0x04) << 3);
|
||||||
|
firstOctet |= ((lowerPart & 0x02) << 5);
|
||||||
|
firstOctet |= ((lowerPart & 0x01) << 7);
|
||||||
|
|
||||||
|
uint8_t secondOctet = 0;
|
||||||
|
uint8_t upperPart = (frequencyCode & 0x03f0) >> 4;
|
||||||
|
secondOctet |= ((upperPart & 0x20) >> 3);
|
||||||
|
secondOctet |= ((upperPart & 0x10) >> 1);
|
||||||
|
secondOctet |= ((upperPart & 0x08) << 1);
|
||||||
|
secondOctet |= ((upperPart & 0x04) << 3);
|
||||||
|
secondOctet |= ((upperPart & 0x02) << 5);
|
||||||
|
secondOctet |= ((upperPart & 0x01) << 7);
|
||||||
|
|
||||||
|
ADDR_DATA_REG = firstOctet;
|
||||||
|
WRITE_CYCLE(chipNo);
|
||||||
|
|
||||||
|
ADDR_DATA_REG = secondOctet;
|
||||||
|
WRITE_CYCLE(chipNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
void psgAmplitude(uint8_t channel, uint8_t volume) {
|
||||||
|
psgAmplitudeShadowValue[channel] = volume;
|
||||||
|
uint8_t chipNo = channel / 3;
|
||||||
|
uint8_t regAddr = ((channel % 3) * 2) + 1;
|
||||||
|
|
||||||
|
uint8_t attenuation = 15 - volume;
|
||||||
|
|
||||||
|
uint8_t firstOctet = 0x01;
|
||||||
|
firstOctet |= ((regAddr & 0x04) >> 1);
|
||||||
|
firstOctet |= ((regAddr & 0x02) << 1);
|
||||||
|
firstOctet |= ((regAddr & 0x01) << 3);
|
||||||
|
firstOctet |= ((attenuation & 0x01) << 7);
|
||||||
|
firstOctet |= ((attenuation & 0x02) << 5);
|
||||||
|
firstOctet |= ((attenuation & 0x04) << 3);
|
||||||
|
firstOctet |= ((attenuation & 0x08) << 1);
|
||||||
|
|
||||||
|
ADDR_DATA_REG = firstOctet;
|
||||||
|
WRITE_CYCLE(chipNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
void psgPlayTone(uint8_t channel, uint8_t volume, t_octave octave, t_note note) {
|
||||||
|
if (note == e_Pause) {
|
||||||
|
psgAmplitude(channel, 0);
|
||||||
|
} else {
|
||||||
|
// if (psgAmplitudeShadowValue[channel] == 0) {
|
||||||
|
psgAmplitude(channel, volume);
|
||||||
|
// }
|
||||||
|
psgWriteFrequency(channel, frequencyCodes[octave][note]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void psgInit() {
|
||||||
|
// address/data bus
|
||||||
|
P2DIR = 0xff;
|
||||||
|
P2SEL = 0;
|
||||||
|
P2SEL2 = 0;
|
||||||
|
|
||||||
|
// bus control lines
|
||||||
|
// output:
|
||||||
|
// BIT0: /CS chip 0
|
||||||
|
// BIT1: /CS chip 1
|
||||||
|
// BIT2: /WE
|
||||||
|
// input:
|
||||||
|
// BIT3: READY
|
||||||
|
P1DIR |= BIT0 | BIT1 | BIT2;
|
||||||
|
P1DIR &= ~BIT3;
|
||||||
|
// immediately disable all outputs, all are active low
|
||||||
|
P1OUT |= BIT0 | BIT1 | BIT2;
|
||||||
|
|
||||||
|
// shutdown all channels including noise
|
||||||
|
psgWrite(0, 0b11111001);
|
||||||
|
psgWrite(0, 0b11111101);
|
||||||
|
psgWrite(0, 0b11111011);
|
||||||
|
psgWrite(0, 0b11111111);
|
||||||
|
|
||||||
|
// psgPlayTone(0, 5, e_O_3, e_A);
|
||||||
|
psgAmplitude(0, 3);
|
||||||
|
}
|
||||||
|
|
29
sound-driver/spi.c
Normal file
29
sound-driver/spi.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <msp430g2553.h>
|
||||||
|
#include "spi.h"
|
||||||
|
|
||||||
|
|
||||||
|
void __attribute__ ((interrupt (USCIAB0RX_VECTOR))) receive() {
|
||||||
|
if (UC0IFG & UCB0RXIFG) {
|
||||||
|
// receive an octet
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void spiInit() {
|
||||||
|
// SPI slave
|
||||||
|
// BIT4: UCB0STE
|
||||||
|
// BIT5: UCB0CLK
|
||||||
|
// BIT6: UCB0SOMI
|
||||||
|
// BIT7: UCB0SIMO
|
||||||
|
P1SEL |= BIT4 | BIT5 | BIT6 | BIT7;
|
||||||
|
P1SEL2 |= BIT4 | BIT5 | BIT6 | BIT7;
|
||||||
|
|
||||||
|
// most significant bit first, enable STE
|
||||||
|
UCB0CTL0 = UCSYNC | UCMSB | UCMODE_2;
|
||||||
|
UCB0CTL1 = 0x00;
|
||||||
|
|
||||||
|
// enable RX interrupt
|
||||||
|
UC0IE |= UCB0RXIE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
9
sound-driver/spi.h
Normal file
9
sound-driver/spi.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef _SPI_H_
|
||||||
|
#define _SPI_H_
|
||||||
|
|
||||||
|
|
||||||
|
void spiInit();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _SPI_H_
|
64
sound-driver/utils/calc-76489an.py
Normal file
64
sound-driver/utils/calc-76489an.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
N = 3579545.0
|
||||||
|
factor = 1.0594631
|
||||||
|
base = 440.0
|
||||||
|
|
||||||
|
frequencies = []
|
||||||
|
|
||||||
|
BEFORE_A = 46
|
||||||
|
f = base
|
||||||
|
for i in range (BEFORE_A):
|
||||||
|
idx = BEFORE_A - i - 1
|
||||||
|
print(f"{idx}: {f}")
|
||||||
|
frequencies.append(f)
|
||||||
|
f = base / factor
|
||||||
|
base = f
|
||||||
|
frequencies.reverse()
|
||||||
|
|
||||||
|
AFTER_A = 50
|
||||||
|
base = 440.0
|
||||||
|
for i in range(AFTER_A):
|
||||||
|
idx = BEFORE_A + i
|
||||||
|
f = base * factor
|
||||||
|
print(f"{idx}: {f}")
|
||||||
|
frequencies.append(f)
|
||||||
|
base = f
|
||||||
|
|
||||||
|
# print(f"{frequencies}")
|
||||||
|
|
||||||
|
codes = []
|
||||||
|
for i in range(len(frequencies)):
|
||||||
|
codes.append(round(N / (32.0 * frequencies[i])))
|
||||||
|
|
||||||
|
|
||||||
|
#const uint16_t frequencyCodes[8][12] = {
|
||||||
|
# // C, Cis, D, Dis, E, F, Fis, G, Gis, A, Ais, H
|
||||||
|
# { 06535, 06234, 05747, 05474, 05233, 05002, 04563, 04353, 04153, 03762, 03600, 03424 }, // Octave 1
|
||||||
|
# { 03256, 03116, 02764, 02636, 02515, 02401, 02271, 02165, 02065, 01771, 01700, 01612 }, // Octave 2
|
||||||
|
# { 01527, 01447, 01372, 01317, 01247, 01201, 01135, 01073, 01033, 00774, 00740, 00705 }, // Octave 3
|
||||||
|
# { 00654, 00624, 00575, 00550, 00523, 00500, 00456, 00435, 00415, 00376, 00360, 00342 }, // Octave 4
|
||||||
|
# { 00326, 00312, 00276, 00264, 00252, 00240, 00227, 00217, 00207, 00177, 00170, 00161 }, // Octave 5
|
||||||
|
# { 00153, 00145, 00137, 00132, 00125, 00120, 00114, 00107, 00103, 00100, 00074, 00071 }, // Octave 6
|
||||||
|
# { 00065, 00062, 00060, 00055, 00052, 00050, 00046, 00044, 00042, 00040, 00036, 00034 }, // Octave 7
|
||||||
|
# { 00033, 00031, 00030, 00026, 00025, 00024, 00023, 00022, 00021, 00020, 00017, 00016 } // Octave 8
|
||||||
|
#};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print("const uint16_t frequencyCodes[8][12] = {")
|
||||||
|
step = 12
|
||||||
|
for i in range(len(codes)):
|
||||||
|
if (i % step == 0):
|
||||||
|
print(" { ", end='')
|
||||||
|
print(f"{codes[i]}", end='')
|
||||||
|
if ((i+1) % step != 0):
|
||||||
|
print(", ", end='')
|
||||||
|
if ((i+1) % step == 0):
|
||||||
|
print(" }", end='')
|
||||||
|
if ((i+1) != len(codes)):
|
||||||
|
print(", ")
|
||||||
|
else:
|
||||||
|
print()
|
||||||
|
print("};")
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user