1 Commits

Author SHA1 Message Date
6a170522ac sound effects, not yet working 2024-04-18 13:26:18 +02:00
22 changed files with 256 additions and 176 deletions

View File

@ -76,7 +76,7 @@ int main() {
P1SEL |= BIT4 | BIT5 | BIT6 | BIT7;
P1SEL2 |= BIT4 | BIT5 | BIT6 | BIT7;
// most significant bit first, enable STE
UCB0CTL0 = UCCKPH | UCSYNC | UCMSB | UCMODE_2;
UCB0CTL0 = UCSYNC | UCMSB | UCMODE_2;
UCB0CTL1 = 0x00;
// enable RX interrupt
UC0IE |= UCB0RXIE;

Binary file not shown.

View File

@ -7,11 +7,11 @@ MCU=msp430g2553
CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
# for debugging
CFLAGS+= -g3 -ggdb -gdwarf-2
#CFLAGS+= -g3 -ggdb -gdwarf-2
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
$(ARTIFACT).elf: main.o spi.o scheduler.o canvas.o shapes.o game.o buttons.o myrand.o display.o sound.o eeprom.o
$(ARTIFACT).elf: main.o spi.o scheduler.o canvas.o shapes.o game.o buttons.o myrand.o display.o sound.o
$(CC) -o $@ $(LDFLAGS) $^
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt

View File

@ -10,8 +10,6 @@
#include "sound.h"
bool mutedFlag = true;
static uint8_t buttonsMoveLeftPressed() {
static uint8_t last = 0;
uint8_t current = (P2IN & BIT4);
@ -51,6 +49,7 @@ static uint8_t buttonsMoveDownPressed() {
void buttonsExec(void *handle) {
static uint32_t unmuteTimestamp;
uint32_t currentTimestamp = getSeconds();
static bool unmuteFlag = true;
if (! stoneIsValid()) {
@ -89,16 +88,16 @@ void buttonsExec(void *handle) {
if (buttonPressed == 1) {
canvasShow();
if (mutedFlag) {
if (! unmuteFlag) {
soundCtrl(SOUND_UNMUTE);
mutedFlag = false;
unmuteFlag = true;
}
unmuteTimestamp = currentTimestamp;
}
if ((! mutedFlag) && (unmuteTimestamp + MUTE_DELAY < currentTimestamp)) {
if (unmuteFlag && (unmuteTimestamp + MUTE_DELAY < currentTimestamp)) {
soundCtrl(SOUND_MUTE);
mutedFlag = true;
unmuteFlag = false;
}
}
@ -108,7 +107,3 @@ void buttonsInit() {
schAdd(buttonsExec, NULL, 0, 25);
}
bool isGameActive() {
return ! mutedFlag;
}

View File

@ -1,10 +1,7 @@
#ifndef _BUTTONS_H_
#define _BUTTONS_H_
#include <stdbool.h>
void buttonsInit();
bool isGameActive();
#endif // _BUTTONS_H_

View File

@ -1,75 +0,0 @@
#include <stdint.h>
#include "eeprom.h"
#include "spi.h"
#define MAGIC 0xaffe
#define HIGHSCORE_ADDR 0x00
#define DUMMY 0x00
#define CMD_READ 0b00000011
#define CMD_WRITE 0b00000010
#define CMD_WRDI 0b00000100
#define CMD_WREN 0b00000110
typedef union {
uint8_t buffer[4];
struct {
uint16_t magic;
uint16_t highScore;
} v;
} eepromBuf_t;
eepromBuf_t buf;
static void writeBuf() {
spiSendBegin(e_SPI_EEPROM);
spiSendOctet(CMD_WREN);
spiSendEnd(e_SPI_EEPROM);
spiSendBegin(e_SPI_EEPROM);
spiSendOctet(CMD_WRITE);
spiSendOctet(HIGHSCORE_ADDR);
spiSendOctet(buf.buffer[0]);
spiSendOctet(buf.buffer[1]);
spiSendOctet(buf.buffer[2]);
spiSendOctet(buf.buffer[3]);
spiSendEnd(e_SPI_EEPROM);
}
static void readBuf() {
spiSendBegin(e_SPI_EEPROM);
spiSendOctet(CMD_READ);
spiReceiveOctet();
spiSendOctet(HIGHSCORE_ADDR);
spiReceiveOctet();
spiSendOctet(DUMMY);
buf.buffer[0] = spiReceiveOctet();
spiSendOctet(DUMMY);
buf.buffer[1] = spiReceiveOctet();
spiSendOctet(DUMMY);
buf.buffer[2] = spiReceiveOctet();
spiSendOctet(DUMMY);
buf.buffer[3] = spiReceiveOctet();
spiSendEnd(e_SPI_EEPROM);
}
void eepromInit() {
readBuf();
if (buf.v.magic != MAGIC) {
buf.v.magic = MAGIC;
buf.v.highScore = 0;
writeBuf();
}
}
uint16_t eepromReadHighScore() {
return buf.v.highScore;
}
void eepromWriteHighScore(uint16_t v) {
buf.v.highScore = v;
writeBuf();
}

View File

@ -1,13 +0,0 @@
#ifndef _EEPROM_H_
#define _EEPROM_H_
#include <stdint.h>
void eepromInit();
uint16_t eepromReadHighScore();
void eepromWriteHighScore(uint16_t v);
#endif // _EEPROM_H_

View File

@ -8,8 +8,6 @@
#include "../rgb-driver/colors.h"
#include "display.h"
#include "sound.h"
#include "eeprom.h"
#include "buttons.h"
#define GAME_CYCLE_TIME 100
@ -37,7 +35,6 @@ void gameExec(void *handle) {
static uint8_t proceedDelay;
static uint8_t level;
static uint16_t score;
static bool newHighScoreAchieved;
// --- engine begin -------------------------------------------------------
switch (state) {
@ -47,7 +44,7 @@ void gameExec(void *handle) {
soundCtrl(SOUND_START);
level = 1;
score = 0;
newHighScoreAchieved = false;
displaySetValue(score);
phase = e_Phase_Game;
state = e_NewStone;
break;
@ -94,7 +91,7 @@ void gameExec(void *handle) {
case e_GameOverFill:
rowIndex--;
canvasFillRow(rowIndex, newHighScoreAchieved ? _green : _red);
canvasFillRow(rowIndex, _red);
if (rowIndex == 0) {
state = e_GameOverWipe;
}
@ -124,10 +121,6 @@ void gameExec(void *handle) {
for (uint8_t r = 0; r < CANVAS_HEIGHT; r++) {
if (canvasIsRowFilled(r)) {
score += level;
if (score > eepromReadHighScore()) {
newHighScoreAchieved = true;
eepromWriteHighScore(score);
}
displaySetValue(score);
canvasWipeRow(r);
canvasShow();
@ -138,12 +131,6 @@ void gameExec(void *handle) {
soundCtrl(SOUND_FANFARE);
}
}
if (isGameActive()) {
displaySetValue(score);
} else {
displaySetValue(eepromReadHighScore());
}
}
void gameInit() {

View File

@ -12,7 +12,6 @@
#include "myrand.h"
#include "spi.h"
#include "display.h"
#include "eeprom.h"
int main() {
@ -29,7 +28,6 @@ int main() {
schInit();
spiInit();
eepromInit();
displayInit();
myRandInit();
canvasInit();

View File

@ -3,7 +3,7 @@
void spiInit() {
// SPI in master mode, most significant bit first
UCB0CTL0 = UCCKPH | UCMST | UCMSB;
UCB0CTL0 = UCMST | UCMSB;
// SPI timing config
UCB0CTL1 = UCSSEL_3;
// Faster than 8 ends up in strange communication errors
@ -19,25 +19,25 @@ void spiInit() {
// BIT7: UCB0SIMO
P1SEL |= BIT5 | BIT6 | BIT7;
P1SEL2 |= BIT5 | BIT6 | BIT7;
// P1DIR |= BIT5 | BIT7;
P1DIR |= BIT5 | BIT7;
// Device Select Lines: 0: Canvas, 1: Display, 2: Sound, 4: EEPROM
P1DIR |= BIT0 | BIT1 | BIT2 | BIT4;
// Device Select Lines: 0: Canvas, 1: Display, 2: Sound
P1DIR |= BIT0 | BIT1 | BIT2;
// Disable all of them
P1OUT |= BIT0 | BIT1 | BIT2 | BIT4;
P1OUT |= BIT0 | BIT1 | BIT2;
// enable SPI module
UCB0CTL1 &= ~UCSWRST;
}
void spiSendBegin(t_SpiDeviceSelector d) {
uint16_t bit = ((uint16_t[]){ BIT0, BIT1, BIT2, BIT4 })[d];
uint16_t bit = ((uint16_t[]){ BIT0, BIT1, BIT2 })[d];
P1OUT &= ~bit;
}
void spiSendEnd(t_SpiDeviceSelector d) {
while (UCB0STAT & UCBUSY);
uint16_t bit = ((uint16_t[]){ BIT0, BIT1, BIT2, BIT4 })[d];
uint16_t bit = ((uint16_t[]){ BIT0, BIT1, BIT2 })[d];
P1OUT |= bit;
}
@ -48,9 +48,3 @@ void spiSendOctet(uint8_t v) {
UCB0TXBUF = v;
}
uint8_t spiReceiveOctet() {
while (!(UC0IFG & UCB0RXIFG));
uint8_t v = UCB0RXBUF;
return v;
}

View File

@ -4,13 +4,12 @@
#include <stdint.h>
typedef enum { e_SPI_CANVAS, e_SPI_DISPLAY, e_SPI_SOUND, e_SPI_EEPROM } t_SpiDeviceSelector;
typedef enum { e_SPI_CANVAS, e_SPI_DISPLAY, e_SPI_SOUND } t_SpiDeviceSelector;
void spiInit();
void spiSendBegin(t_SpiDeviceSelector d);
void spiSendEnd(t_SpiDeviceSelector d);
void spiSendOctet(uint8_t v);
uint8_t spiReceiveOctet();

View File

@ -123,7 +123,7 @@ init:
;; spi configuration
;; USCI B to slave mode, enable STE and most significant bit first
mov.b #UCCKPH|UCSYNC|UCMODE_2|UCMSB, &UCB0CTL0
mov.b #UCSYNC|UCMODE_2|UCMSB, &UCB0CTL0
mov.b #0x00, &UCB0CTL1
;; make sure the isr will not immediately start

View File

@ -13,7 +13,7 @@ CFLAGS+= -g3 -ggdb -gdwarf-2
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
$(ARTIFACT).elf: main.o scheduler.o spi.o spi_init.o sequencer.o melody_tetris.o melody_tusch1.o psg.o mute.o
$(ARTIFACT).elf: main.o scheduler.o spi.o spi_init.o sequencer.o melody_tetris.o melody_tusch1.o ay_3_8913.o mute.o effects.o
$(CC) -o $@ $(LDFLAGS) $^
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt

View File

@ -27,35 +27,6 @@ const uint16_t frequencyCodes[8][12] = {
#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[2][14];
inline static void BUS_OP_NACT() {
@ -94,11 +65,11 @@ asm volatile (
}
#endif
static uint8_t psgReadShadow(uint8_t chip, uint8_t address) {
uint8_t psgReadShadow(uint8_t chip, uint8_t address) {
return psgShadowRegisters[chip][address];
}
static void psgWrite(uint8_t chip, uint8_t address, uint8_t data) {
void psgWrite(uint8_t chip, uint8_t address, uint8_t data) {
psgShadowRegisters[chip][address] = data;
// according to "State Timing" (p. 15) of datasheet

18
sound-driver/effects.c Normal file
View File

@ -0,0 +1,18 @@
#include "effects.h"
#include "psg.h"
void effectsInit() {
}
void playEffect1() {
psgWrite(1, NOISE_PERIOD_REG, 0);
psgWrite(1, _ENABLE_REG, psgReadShadow(1, _ENABLE_REG) & ~0b00111000);
psgWrite(1, CHANNEL_A_AMPLITUDE_REG, 020);
psgWrite(1, CHANNEL_B_AMPLITUDE_REG, 020);
psgWrite(1, CHANNEL_C_AMPLITUDE_REG, 020);
psgWrite(1, ENVELOPE_PERIOD_COARSE_REG, 070);
psgWrite(1, ENVELOPE_SHAPE_REG, 0);
}

9
sound-driver/effects.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _EFFECTS_H_
#define _EFFECTS_H_
void effectsInit();
#endif // _EFFECTS_H_

View File

@ -9,6 +9,7 @@
#include "melody_tetris.h"
#include "melody_tusch1.h"
#include "mute.h"
#include "effects.h"
int main() {
WDTCTL = WDTPW | WDTHOLD;
@ -28,11 +29,10 @@ int main() {
psgInit();
muteInit();
sequencerInit();
effectsInit();
__enable_interrupt();
// playMelodyTetris();
while (1) {
schExec();
}

View File

@ -6,9 +6,7 @@
void muteInit() {
// BIT6: MuteCtrl
P1DIR |= BIT6;
// initially, mute
P1OUT |= BIT6;
P1OUT &= ~BIT6;
}
void mute() {

View File

@ -36,11 +36,41 @@ typedef enum {
#define e_As e_Gis
#define e_B e_Ais
#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
void psgInit();
void psgPlayTone(uint8_t chip, uint8_t channel, uint8_t volume, t_octave octave, t_note note);
void psgAmplitude(uint8_t chip, uint8_t channel, uint8_t volume);
uint8_t psgReadShadow(uint8_t chip, uint8_t address);
void psgWrite(uint8_t chip, uint8_t address, uint8_t data);
#endif // _PSG_H_

171
sound-driver/sn76489an.c Normal file
View 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);
}

View File

@ -44,6 +44,7 @@ spiCmdHandler_5:
spiCmdHandler_6:
bit #SOUND_LOCK, &cmd
jz spiCmdHandler_7
call #playEffect1
;; insert a call here
bic #SOUND_LOCK, &cmd
spiCmdHandler_7:

View File

@ -18,7 +18,7 @@ void spiInit() {
P1SEL2 |= BIT4 | BIT5 | BIT7;
// most significant bit first, enable STE
UCB0CTL0 = UCCKPH | UCSYNC | UCMSB | UCMODE_2;
UCB0CTL0 = UCSYNC | UCMSB | UCMODE_2;
UCB0CTL1 = 0x00;
// enable RX interrupt