play NES SMB Underground theme for testing

This commit is contained in:
Vineet K 2024-04-21 20:46:46 -04:00 committed by Shokara Kou
parent 6a28f45c51
commit ece65dcc2a

213
notes.c
View File

@ -8,6 +8,9 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16e6
#include <util/delay.h>
#define T1_PRESCALAR 8
#define FREQ(f) (16e6 / f / T1_PRESCALAR / 2)
@ -18,8 +21,11 @@
#define P_TX 1
#define P_SPKR 2
#define MARIO 50
unsigned short freqs[] = {
// PD3-7
#if 0
FREQ(100),
FREQ(200),
FREQ(250),
@ -27,18 +33,74 @@ unsigned short freqs[] = {
FREQ(350),
// PB0-4
FREQ(400),
#else
FREQ(131), // middle c
FREQ(262),
FREQ(110),
FREQ(220),
FREQ(117),
FREQ(233),
#endif
FREQ(500),
FREQ(600),
FREQ(700),
FREQ(750),
FREQ(6000),
FREQ(5000),
FREQ(5500),
// PC0-4
FREQ(1000),
FREQ(1250),
FREQ(1500),
FREQ(1750),
FREQ(2000),
FREQ(4000),
FREQ(7250),
FREQ(6500),
FREQ(5750),
FREQ(5000),
FREQ(5000),
FREQ(6000),
FREQ(7000),
FREQ(8000),
FREQ(8000),
FREQ(8000),
FREQ(8000),
FREQ(8000),
[32] = 0 // used as a 'silent' note
// notes
[MARIO] = FREQ(131), // C3
FREQ(262), // C4
FREQ(110), // A2
FREQ(220), // A3
FREQ(117), // Bb2
FREQ(233), // Bb3
FREQ(87),
FREQ(175),
FREQ(73),
FREQ(147),
FREQ(78),
FREQ(156),
FREQ(156),
FREQ(147),
FREQ(139),
FREQ(131),
FREQ(156),
FREQ(147),
FREQ(104),
FREQ(98),
FREQ(139),
FREQ(156),
FREQ(185),
FREQ(175),
FREQ(165),
FREQ(233),
FREQ(220),
FREQ(208),
FREQ(156),
FREQ(123),
FREQ(123),
FREQ(110),
FREQ(104),
['"'] = FREQ(16000),
[255] = 0 // used as a 'silent' note
};
unsigned char note = 0;
// 0 = buttons write note+cycles+\xff to UART
@ -108,6 +170,36 @@ usart_read(void)
return UDR0;
}
void
playtone(void)
{
// Prepare Timer/Counter
TCNT1 = 1;
OCR1A = freqs[note];
TCCR1B |= 1 << CS11;
// Monitor OutPut Compare Flag
while ((TIFR1 & (1 << OCF1A)) == 0);
// Toggles port For Speaker
PORTD ^= 1 << P_SPKR;
TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11);
}
void
play_note(unsigned char _note, unsigned short _cycles)
{
note = _note;
if (note != 255) {
for (unsigned short i = 0; i < _cycles; i++)
playtone();
} else {
_delay_ms(_cycles);
}
}
int
main(void)
{
@ -139,7 +231,7 @@ main(void)
usart_send(mode);
}
// check if any buttons are pressed, otherwise don't play a note
if (mode != '1' && ((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) {
if (mode == '0' && ((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) {
// if a button was released, then send note and cycles played
if (cycles > 0) {
usart_send(tmp);
@ -152,23 +244,14 @@ main(void)
}
if (mode == '0') {
// Prepare Timer/Counter
TCNT1 = 1;
tmp = note;
OCR1A = freqs[note];
TCCR1B |= 1 << CS11;
// Monitor OutPut Compare Flag
while ((TIFR1 & (1 << OCF1A)) == 0);
playtone();
// Toggles port For Speaker
PORTD ^= 1 << P_SPKR;
TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11);
cycles = (cycles + 1) % 0xffff;
} else if (mode == '1') {
note = usart_read();
if (note >= '0') note -= '0';
if (note >= 'Z') note -= '0';
tmp = usart_read();
cycles = tmp << 8;
tmp = usart_read();
@ -176,26 +259,88 @@ main(void)
tmp = usart_read();
// reset to default mode when an invalid note is read
if (tmp != 'z' || note > '@') {
if (tmp != 'z' || note >= '|') {
mode = '0';
continue;
}
for (unsigned short i = 0; i < cycles; i++) {
usart_send(HIGH(cycles));
usart_send(LOW(cycles));
usart_send(0);
for (unsigned short i = 0; i < cycles; i++)
playtone();
} else if (mode == '2') {
play_note(MARIO + 0, 131 * 0.3);
play_note(MARIO + 1, 262 * 0.3);
play_note(MARIO + 2, 110 * 0.3);
play_note(MARIO + 3, 220 * 0.3);
play_note(MARIO + 4, 117 * 0.3);
play_note(MARIO + 5, 233 * 0.3);
play_note(255, 1800);
play_note(MARIO + 0, 131 * 0.3);
play_note(MARIO + 1, 262 * 0.3);
play_note(MARIO + 2, 110 * 0.3);
play_note(MARIO + 3, 220 * 0.3);
play_note(MARIO + 4, 117 * 0.3);
play_note(MARIO + 5, 233 * 0.3);
play_note(255, 1800);
TCNT1 = 1;
OCR1A = freqs[note];
TCCR1B |= 1 << CS11;
play_note(MARIO + 6, 87 * 0.3);
play_note(MARIO + 7, 175 * 0.3);
play_note(MARIO + 8, 73 * 0.3);
play_note(MARIO + 9, 147 * 0.3);
play_note(MARIO + 10, 78 * 0.3);
play_note(MARIO + 11, 156 * 0.3);
play_note(255, 1800);
play_note(MARIO + 6, 87 * 0.3);
play_note(MARIO + 7, 175 * 0.3);
play_note(MARIO + 8, 73 * 0.3);
play_note(MARIO + 9, 147 * 0.3);
play_note(MARIO + 10, 78 * 0.3);
play_note(MARIO + 11, 156 * 0.3);
play_note(255, 1800);
while ((TIFR1 & (1 << OCF1A)) == 0);
play_note(MARIO + 12, 156 * 0.3);
play_note(255, 25);
play_note(MARIO + 13, 147 * 0.3);
play_note(255, 25);
play_note(MARIO + 14, 139 * 0.3);
play_note(255, 25);
play_note(MARIO + 15, 131 * 0.6);
play_note(255, 25);
play_note(MARIO + 16, 156 * 0.3);
play_note(255, 25);
play_note(MARIO + 17, 147 * 0.6);
play_note(255, 25);
play_note(MARIO + 18, 104 * 0.6);
play_note(255, 25);
play_note(MARIO + 19, 98 * 0.6);
play_note(255, 25);
play_note(MARIO + 20, 139 * 0.6);
PORTD ^= 1 << P_SPKR;
TIFR1 |= 1 << OCF1A;
TCCR1B &= ~(1 << CS11);
}
play_note(MARIO + 21, 156 * 0.3);
play_note(255, 25);
play_note(MARIO + 22, 185 * 0.3);
play_note(255, 25);
play_note(MARIO + 23, 175 * 0.3);
play_note(255, 25);
play_note(MARIO + 24, 165 * 0.3);
play_note(255, 25);
play_note(MARIO + 25, 233 * 0.3);
play_note(255, 25);
play_note(MARIO + 26, 220 * 0.3);
play_note(255, 25);
play_note(MARIO + 27, 208 * 0.6);
play_note(255, 25);
play_note(MARIO + 28, 156 * 0.6);
play_note(255, 25);
play_note(MARIO + 29, 123 * 0.6);
play_note(255, 25);
play_note(MARIO + 30, 123 * 0.6);
play_note(255, 25);
play_note(MARIO + 31, 110 * 0.6);
play_note(255, 25);
play_note(MARIO + 32, 104 * 0.6);
play_note(255, 25);
play_note(255, 3600);
}
}
}