Skip to content

Commit

Permalink
morse.b and morse(1) man page. work on devaudio, ethernds.c.
Browse files Browse the repository at this point in the history
  • Loading branch information
saoret.one committed Mar 15, 2009
1 parent eec13ac commit b958925
Show file tree
Hide file tree
Showing 18 changed files with 1,031 additions and 449 deletions.
42 changes: 27 additions & 15 deletions arm7/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ recintr(void *a)
TxSound *snd = a;

//print("snd7 %lx %lx %lx\n", (ulong)snd, (ulong)snd->d, snd->n);
if(snd->d && nrs++ < snd->n){
if(snd->fmt)
((ushort*)snd->d)[nrs] = mic_read(Tscgetmic12);
else
if(snd->inuse && nrs++ < snd->n){
switch(snd->flags & (AFlagsigned|AFlag8bit)){
case (AFlag8bit):
((uchar*)snd->d)[nrs] = mic_read(Tscgetmic8);
break;
case (AFlagsigned|AFlag8bit):
((uchar*)snd->d)[nrs] = mic_read(Tscgetmic8) ^ 0x80;
break;
case (AFlagsigned):
((ushort*)snd->d)[nrs] = (mic_read(Tscgetmic12) - 2048) << 4; // ^ 0x8000;
break;
default:
((ushort*)snd->d)[nrs] = mic_read(Tscgetmic12);
break;
}
}else
audiorec(snd, 0);

Expand All @@ -63,19 +73,21 @@ audiorec(TxSound *snd, int on)
{
TimerReg *t = TMRREG + AUDIOtimer;

if(!snd)
if(!snd || snd->d == nil)
return 0;

if(on){
nrs = 0;
snd->inuse = 1;

t->data = TIMER_BASE(Tmrdiv1) / snd->rate;
t->ctl = Tmrena | Tmrdiv1 | Tmrirq;
intrenable(TIMERAUDIObit, recintr, snd, 0);
}else{
t->ctl &= ~Tmrena;
intrmask(TIMERAUDIObit, 0);

snd->d = nil;
snd->inuse = 0;
return nrs;
}
}
Expand All @@ -90,17 +102,17 @@ audioplay(TxSound *snd, int on)

schan = SCHANREG + snd->chan;
schan->rpt = 0;
schan->tmr = SCHAN_BASE / (int)snd->rate;
schan->tmr = SCHAN_BASE / snd->rate;
schan->src = (ulong) snd->d;
schan->wlen = snd->fmt? snd->n>>2: snd->n>>1;
schan->wlen = snd->flags & AFlag8bit? snd->n>>1: snd->n>>2;
schan->cr.vol = snd->vol;
schan->cr.pan = snd->pan;
schan->cr.ctl |= SCena | SCrep1shot | (snd->fmt? SCpcm16bit: SCpcm8bit);
schan->cr.ctl |= SCena | SCrep1shot | (snd->flags & AFlag8bit? SCpcm8bit: SCpcm16bit);
return 1;
}

void
audiopower(int dir, int on)
audiopower(int dir, int l)
{
uchar pwr;

Expand All @@ -109,7 +121,7 @@ audiopower(int dir, int on)
default:
break;
case F9Aupowerout:
if(on){
if(l){
POWERREG->pcr |= 1<<POWER_SOUND;
SNDREG->cr.ctl = Sndena | Maxvol;
SNDREG->bias = 0x200;
Expand All @@ -121,16 +133,16 @@ audiopower(int dir, int on)
}
break;
case F9Aupowerin:
if(on){
if(l){
power_write(POWER_CONTROL, pwr | POWER_SOUND_AMP);

power_write(POWER_MIC_AMPL, POWER_MIC_AMPLON);
power_write(POWER_MIC_GAIN, POWER_MIC_GAIN_20);
power_write(POWER_MIC_GAIN, l);
}else{
power_write(POWER_CONTROL, pwr & ~POWER_SOUND_AMP);

power_write(POWER_MIC_AMPL, POWER_MIC_AMPLOFF);
power_write(POWER_MIC_GAIN, POWER_MIC_GAIN_20);
power_write(POWER_MIC_GAIN, l);
}
break;
}
Expand Down
30 changes: 22 additions & 8 deletions arm7/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,34 @@ struct SndReg {

enum {
Sndena = 1<<15,

/* TxSound.flags: */
AFlagin = 1<<0, /* direction: audio in/out */
AFlag8bit = 1<<1, /* sample size: 8 bit/16 bit (1/0) */
AFlagsigned = 1<<2, /* sample sign: signed/unsigned */
AFlagmono = 1<<3, /* sample chans: mono/stereo */

/* encoding */
AFlagpcm = 1<<4,
AFlagadpcm = 1<<5,
AFlagpsg = 1<<6,
};

typedef struct TxSound TxSound;
struct TxSound {
void *d; /* data samples */
ulong n; /* samples number */
ulong rate; /* rate (hz) */
ushort inuse;
ushort flags; /* audio flags */
TxSound *phys; /* uncached self-ptr to this TxSound */

void *d; /* data samples */
ulong n; /* samples number */
long rate; /* rate (hz) */

uchar chan; /* SCHANREG channel */
uchar vol;
uchar pan;
uchar fmt; /* pcm8bit/pcm16bit (0/1) */
uchar vol; /* volume */
uchar pan; /* panning */
uchar chan; /* SCHANREG channel */
};

int audiorec(TxSound *snd, int on);
int audioplay(TxSound *snd, int on);
void audiopower(int dir, int on);
void audiopower(int dir, int level);
1 change: 0 additions & 1 deletion arm7/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ fiforecvintr(void*)
{
ulong v, vv;
uchar ndstype[1];
TxSound *snd;
int power, ier;

while(!(FIFOREG->ctl & FifoRempty)) {
Expand Down
9 changes: 4 additions & 5 deletions clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,19 @@ ulong _mularsv(ulong m0, ulong m1, ulong a, ulong s);
#define MULDIVR64(x,a,b,n) ((ulong)_mularsv(x, FXDPTDIVR(a,b,n), 1<<((n)-1), (n)))


ushort
static ushort
timer_start(void)
{
return TMRREG->data;
}

ushort
static ushort
timer_ticks(ulong t0)
{
return TMRREG->data - t0;
}

void
static void
timer_delay(int t)
{
ulong t0 = timer_start();
Expand All @@ -190,11 +190,10 @@ timer_delay(int t)
void
microdelay(int us)
{
ulong t = US2TMR(t);
ulong t = US2TMR(us);
timer_delay(t);
}


void
delay(int ms)
{
Expand Down
106 changes: 81 additions & 25 deletions devaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ enum
Qaudioctl,

Fmono = 1,
Fin = 2,
Fin = 2,
Fout = 4,
F16bits = 8,

Fpcm = 16,
Fadpcm = 32,
Fpsg = 64,

Aclosed = 0,
Aread,
Awrite,
Expand Down Expand Up @@ -91,10 +94,9 @@ static struct
static char Emode[] = "illegal open mode";
static char Evolume[] = "illegal volume specifier";

/* TODO: use the 16 sound channels available */
static int chan = 1; /* audio sound channel */
static TxSound Snd[NSChannels+1]; /* play and record */
static TxSound* snd[NSChannels+1];
static TxSound mixer; /* mxvolume */
static TxSound record; /* recording */
static TxSound snd[NSChannels]; /* playing */

static void
mxvolume(void)
Expand All @@ -104,31 +106,78 @@ mxvolume(void)
rov = audio.rovol[Vaudio];
lov = audio.lovol[Vaudio];

snd[chan]->chan = (audio.flags & Fmono? 1: 2);
snd[chan]->rate = audio.lovol[Vspeed];
snd[chan]->vol = (Maxvol - Minvol) * (lov + rov) / (2*100);
snd[chan]->pan = (Maxvol - Minvol) * rov / (2*lov+1);
snd[chan]->fmt = audio.flags & F16bits;
mixer.rate = audio.lovol[Vspeed];
mixer.vol = (Maxvol - Minvol) * (lov + rov) / (2*100);
mixer.pan = (Maxvol - Minvol) * rov / (2*lov+1);

if(audio.flags & Fpcm)
mixer.flags |= AFlagpcm;
else if(audio.flags & Fadpcm)
mixer.flags |= AFlagadpcm;
else if(audio.flags & Fpsg)
mixer.flags &= ~AFlagpsg;

if(audio.flags & Fmono)
mixer.flags |= AFlagmono;
else
mixer.flags &= ~AFlagmono;

if(audio.flags & F16bits)
mixer.flags &= ~AFlag8bit;
else
mixer.flags |= AFlag8bit;
}

static int
getchannel(void)
{
int i;

for(i=0; i<NSChannels; i++)
if(!snd[i].inuse)
break;

if(i == NSChannels)
return -1;
return i;
}

static void
playaudio(void* d, ulong n)
{
snd[chan]->d = d;
snd[chan]->n = n;
snd[chan]->chan = chan;
fifoput(F9TAudio|F9Auplay, (ulong)snd[chan]);
int i;

if((i=getchannel()) == -1)
error(Einuse);
DPRINT("#A: play %d\n", i);
memmove(&snd[i], &mixer, sizeof(TxSound));
snd[i].phys = (TxSound*)uncached(&snd[i]);
//snd[i].phys->inuse = 1;
snd[i].phys->d = d;
snd[i].phys->n = n;
snd[i].phys->chan = i;
fifoput(F9TAudio|F9Auplay, (ulong)snd[i].phys);
}

static int
recording(void *a)
{
// tsleep(&up->sleep, recording, &record, 100);
TxSound *snd = (TxSound*)a;
return snd->inuse;
}

static void
recaudio(void* d, ulong n)
{
memmove(snd[0], snd[chan], sizeof(TxSound));
snd[0]->d = d;
snd[0]->n = n;
fifoput(F9TAudio|F9Aurec, (ulong)snd[0]);
while(snd[0]->d != nil)
;
while(record.inuse);
DPRINT("i %x r %d f %x\n", record.inuse, record.rate, record.flags);
memmove(&record, &mixer, sizeof(TxSound));
record.phys = (TxSound*)uncached(&record);
record.phys->d = d;
record.phys->n = n;
fifoput(F9TAudio|F9Aurec, (ulong)record.phys);
while(record.inuse);
}

static void
Expand All @@ -152,8 +201,8 @@ audioinit(void)
audio.amode = Aclosed;
resetlevel();

for(i=0; i< NSChannels+1; i++)
snd[i] = (TxSound*)uncached(&Snd[i]);
for(i=0; i< NSChannels; i++)
snd[i].phys = (TxSound*)uncached(&snd[i]);
}

static Chan*
Expand Down Expand Up @@ -257,7 +306,7 @@ audioclose(Chan *c)
}
if (audio.amode == 0) {
/* turn audio off */
DPRINT("#A: audio off");
DPRINT("#A: audio off\n");
//fifoput(F9TAudio|F9Aupowerin, 0);
//fifoput(F9TAudio|F9Aupowerout, 0);
}
Expand Down Expand Up @@ -288,6 +337,7 @@ audioread(Chan *c, void *v, long n, vlong offset)
buf[0] = 0;
s = buf;
e = buf + sizeof(buf);
// TODO add encoding
for(m=0; volumes[m].name; m++){
liv = audio.livol[m];
riv = audio.rivol[m];
Expand Down Expand Up @@ -394,7 +444,13 @@ audiowrite(Chan *c, void *vp, long n, vlong)
if(strcmp(field[i], "enc") == 0) {
if(++i >= nf)
error(Evolume);
if(strcmp(field[i], "pcm") != 0)
if(strcmp(field[i], "pcm") == 0)
audio.flags |= Apcm;
else if(strcmp(field[i], "adpcm") == 0)
audio.flags |= Aadpcm;
else if(strcmp(field[i], "psg") == 0)
audio.flags |= Apsg;
else
error(Evolume);
goto cont0;
}
Expand Down
Loading

0 comments on commit b958925

Please sign in to comment.