shithub: neindaw

Download patch

ref: 8b0c34fef29634833180f62135c3f8b33cc0be04
parent: 2954c28b541c70fef1d71416149233365eb86806
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Mar 19 09:19:56 EDT 2020

ay: fix bounds and steps

--- a/ay/ay.c
+++ b/ay/ay.c
@@ -3,12 +3,12 @@
 #include <fcall.h>
 #include <thread.h>
 #include <9p.h>
-#include "common.h"
-#include "aux.h"
 #define CHIPS_IMPL
 #define CHIPS_ASSERT assert
 #include "ay38910.h"
+#include "common.h"
 #include "uiglue.h"
+#include "aux.h"
 
 /*
 FIXME just a note, remove later
@@ -108,6 +108,12 @@
 }
 
 static int
+tp2hz(int tp)
+{
+	return tickhz / tp / 16;
+}
+
+static int
 hz2ep(int hz)
 {
 	return MAX(1, MIN(65535, tickhz / (MAX(1, hz) * 256)));
@@ -120,16 +126,6 @@
 }
 
 static void
-tone(ay38910_t *ay, int chan, int hz)
-{
-	int tp;
-
-	tp = hz2tp(hz);
-	regw(ay, chan*2+0, tp & 0xff); /* fine */
-	regw(ay, chan*2+1, (tp>>8) & 0x0f); /* coarse */
-}
-
-static void
 toneon(ay38910_t *ay, int chan)
 {
 	regw(ay, AY38910_REG_ENABLE, regr(ay, AY38910_REG_ENABLE) & ~(1<<chan));
@@ -178,6 +174,25 @@
 	return nil;
 }
 
+static int
+writefreq(UI *ui, int auxtype, char *s)
+{
+	Auxdsp *dsp;
+	int r, chan, tp;
+
+	if ((r = ui_writestr(ui, auxtype, s)) != 0)
+		return r;
+
+	dsp = ui->userdata;
+	for (chan = 0; ui->zone != &dsp->chan[chan].freq; chan++);
+	tp = hz2tp(*ui->zone);
+	regw(&dsp->ay, chan*2+0, tp & 0xff); /* fine */
+	regw(&dsp->ay, chan*2+1, (tp>>8) & 0x0f); /* coarse */
+	*ui->zone = tp2hz(tp);
+
+	return 0;
+}
+
 static void
 buildui(Auxdsp *dsp, UIGlue *ui)
 {
@@ -185,10 +200,11 @@
 	char s[32];
 	int i;
 
-	min = tickhz/65520.0f;
-	max = tickhz/16.0f;
-	step = (max-min)/4094.0f;
+	min = ceil(tickhz/65520);
+	max = floor(tickhz/16);
+	step = MAX(1, ceil(tickhz/65504 - tickhz/65520));
 
+	ui->userdata = dsp;
 	ui->openVerticalBox(ui->f, "AY-3-8910");
 
 	ui->openVerticalBox(ui->f, "Tone");
@@ -198,8 +214,10 @@
 
 		ui->declare(ui->f, &dsp->chan[i].freq, "0", "");
 		ui->declare(ui->f, &dsp->chan[i].freq, "unit", "Hz");
-		ui->addHorizontalSlider(ui->f, "Tone", &dsp->chan[i].freq, min+(max-min)/2.0f, min, max, step);
+		ui->writestr = writefreq;
+		ui->addHorizontalSlider(ui->f, "Tone", &dsp->chan[i].freq, min, min, max, step);
 
+		ui->writestr = nil;
 		ui->declare(ui->f, &dsp->chan[i].amp, "1", "");
 		ui->addHorizontalSlider(ui->f, "Volume", &dsp->chan[i].amp, 1.0f, 0.0f, 1.0f, 1.0f/15.0f);
 
@@ -216,12 +234,12 @@
 	}
 	ui->closeBox(ui->f);
 
-	min = tickhz/496.0f;
-	max = tickhz/16.0f;
-	step = (max-min)/30.0f;
+	min = ceil(tickhz/496);
+	max = floor(tickhz/16);
+	step = MAX(1, tickhz/480 - tickhz/496);
 
-	ui->declare(ui->f, &dsp->noise, "noise", "");
-	ui->addHorizontalSlider(ui->f, "Noise", &dsp->noise, 200, min, max, step);
+	ui->declare(ui->f, &dsp->noise, "unit", "Hz");
+	ui->addHorizontalSlider(ui->f, "Noise", &dsp->noise, 4000, min, max, step);
 
 	ui->openVerticalBox(ui->f, "Envelope");
 	ui->declare(ui->f, &dsp->hold, "0", "");