shithub: sms

Download patch

ref: c5426541e581e837e17098055b1766d382c493bb
parent: 6d9e0773724cdcd6a5159e4cf342a5c5a42195d2
author: Jean-André Santoni <jean.andre.santoni@gmail.com>
date: Fri Feb 27 11:27:55 EST 2026

Wire audio

--- a/dat.h
+++ b/dat.h
@@ -56,6 +56,7 @@
 	FREQ = 53203400,
 	Z80DIV = 15,
 	RATE = 44100,
+	SAMPDIV = FREQ / RATE,
 	SAVEFREQ = FREQ / 4,
 	PSGCLOCK = 3579545,
 	PSGDIV = 16,
--- a/fns.h
+++ b/fns.h
@@ -17,7 +17,8 @@
 u8int vdpvcounter(void);
 void cramwrite(u16int, u8int);
 int audioout(void);
+void audiosample(void);
 void psginit(const u16int, const int);
 void psgwrite(const u8int);
-u16int psgstep();
+u16int psgstep(void);
 void mapperinit(void);
--- a/psg.c
+++ b/psg.c
@@ -58,7 +58,7 @@
 	return v;
 }
 
-static inline u16int
+static inline s16int
 vol(u8int chn)
 {
 	return (flipflop[chn] ? 1 : -1) * vol_table[attn[chn]];
@@ -65,7 +65,7 @@
 }
 
 u16int
-psgstep()
+psgstep(void)
 {
 	while(cyc > 0){
 		for(u8int i = 0; i < 4; i++){
@@ -93,6 +93,21 @@
 	return vol(0) + vol(1) + vol(2) + vol(3);
 }
 
+void
+audiosample(void)
+{
+	s16int v;
+
+	if(sbufp == nil)
+		return;
+
+	v = (s16int)psgstep();
+	if(sbufp < sbuf + nelem(sbuf) - 1){
+		*sbufp++ = v;
+		*sbufp++ = v;
+	}
+}
+
 int
 audioout(void)
 {
@@ -121,4 +136,4 @@
 		return;
 
 	sbufp = sbuf;
-}
\ No newline at end of file
+}
--- a/sms.c
+++ b/sms.c
@@ -103,10 +103,15 @@
 		process_inputs();
 		t = z80step();
 		vdpclock -= t * Z80DIV;
+		psgclock += t * Z80DIV;
 
 		while(vdpclock < 0){
 			vdpstep();
 			vdpclock += 8;
+		}
+		while(psgclock >= SAMPDIV){
+			audiosample();
+			psgclock -= SAMPDIV;
 		}
 	}
 }
--