start with other sound chip

This commit is contained in:
Wolfgang Hottgenroth 2024-04-09 10:32:19 +02:00
parent 20c01f2efa
commit aeb7aeb7f2
4 changed files with 161 additions and 9 deletions

View File

@ -78,11 +78,11 @@ asm volatile (
); );
} }
uint8_t psgReadShadow(uint8_t address) { static uint8_t psgReadShadow(uint8_t address) {
return psgShadowRegisters[address]; return psgShadowRegisters[address];
} }
void psgWrite(uint8_t address, uint8_t data) { static void psgWrite(uint8_t address, uint8_t data) {
psgShadowRegisters[address] = data; psgShadowRegisters[address] = data;
// according to "State Timing" (p. 15) of datasheet // according to "State Timing" (p. 15) of datasheet
@ -109,7 +109,7 @@ void psgWrite(uint8_t address, uint8_t data) {
BUS_OP_NACT(); BUS_OP_NACT();
} }
void psgWriteFrequency(uint8_t channel, uint16_t frequencyCode) { 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_FINE_REG + (channel * 2), (frequencyCode & 0x00ff));
psgWrite(CHANNEL_A_TONE_PERIOD_COARSE_REG + (channel * 2), ((frequencyCode >> 8) & 0x000f)); psgWrite(CHANNEL_A_TONE_PERIOD_COARSE_REG + (channel * 2), ((frequencyCode >> 8) & 0x000f));
} }

View File

@ -42,11 +42,5 @@ void psgInit();
void psgPlayTone(uint8_t channel, t_octave octave, t_note note); void psgPlayTone(uint8_t channel, t_octave octave, t_note note);
void psgAmplitude(uint8_t channel, uint8_t volume); void psgAmplitude(uint8_t channel, uint8_t volume);
// low level
void psgWriteFrequency(uint8_t channel, uint16_t frequencyCode);
// very low level
void psgWrite(uint8_t address, uint8_t data);
uint8_t psgReadShadow(uint8_t address);
#endif // _PSG_H_ #endif // _PSG_H_

94
sound-driver/sn76489an.c Normal file
View File

@ -0,0 +1,94 @@
#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
inline static void BEGIN_WRITE(uint8_t chipNo) {
if (chipNo == 0) {
BUS_CTRL_REG &= ~_CS0;
} else {
BUS_CTRL_REG &= ~_CS1;
}
BUS_CTRL_REG &= ~_WE;
}
inline static void END_WRITE(uint8_t chipNo) {
while ((BUS_CTRL_IN_REG & ~READY) == 0);
BUS_CTRL_REG |= _WE;
if (chipNo == 0) {
BUS_CTRL_REG |= _CS0;
} else {
BUS_CTRL_REG |= _CS1;
}
}
static void psgWriteFrequency(uint8_t channel, uint16_t frequencyCode) {
uint8_t chipNo = channel / 3;
uint8_t channelAddr = (channel % 3) * 2;
uint8_t firstOctet = 0x01 | (channelAddr << 1) | ((frequencyCode & 0x03c0)
}
void psgPlayTone(uint8_t channel, t_octave octave, t_note note) {
}
void psgAmplitude(uint8_t channel, uint8_t volume) {
}
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;
}

View 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("};")