What Linux System Call Lets Me Talk to the A/D Converter?

Status
Not open for further replies.

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
Hello,

OK. So, my laptop, just like every other laptop, has a mic-in jack.

My mic, just like every other (I think) is an analog device.

My laptop (I'm certain) is a digital device.

I've surmised, therefore, that somewhere on my motherboard exists an A/D converter.

I would like to talk to this converter and grab the bit stream it creates when signals come in from the mic.

I know that I can use a Java class or Python module that will let me do this in 20 lines of code or less.

However, I'm NOT interested in using anything high level at all.

I want to use assembly, MAYBE C.

But, that's it.

I tried looking for the system call in lists of Linux system calls, and I can't find anything that stands out as being a candidate.

Do you guys have any suggestions?

Thanks.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,697
4,658
75
I would like to talk to this converter and grab the bit stream it creates when signals come in from the mic.
/dev/snd . It's really fun! :) You can read from it to read from the mic, or write to it to write to whatever's hooked up to the sound output. (Speakers, headphones, etc.)

Just be really careful not to type /dev/sda by accident! Especially when writing sound to it. I know from experience. :$

Edit: I should mention the last time I tried that was on Cygwin. Looks like it's a little more complicated now. But not much.
 
Last edited:

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
/dev/snd . It's really fun! :) You can read from it to read from the mic, or write to it to write to whatever's hooked up to the sound output. (Speakers, headphones, etc.)

Just be really careful not to type /dev/sda by accident! Especially when writing sound to it. I know from experience. :$

Edit: I should mention the last time I tried that was on Cygwin. Looks like it's a little more complicated now. But not much.

Man, you're fast!

OK. I didn't read the link yet, but I'm assuming I use some sort of fopen() call and pass /dev/snd as one of the parameters?

This makes sense to me in C, but what does this look like in assembly?

All the system call wrappers in C wrap themselves around OS API calls, right?

But, at the hardware level, what you're actually doing is making the 'int' hardware call with a direct integer argument and setting parameters in certain registers.

This is what I want to do.

What's the proper 'int' argument for fopen()?
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,697
4,658
75
You could probably write it in C, then compile with gcc -S to get the proper assembly code.

My personal preference would be to write everything in C except for whatever absolutely has to be in assembly; then put that in an _asm block.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
In this case it would look the same in assembly as C. As you are in user mode using file mapped I/O, all you would be doing in assembly is pushing parameters and performing writefile/readfile syscalls to the kernel. You can't access the hardware directly in user mode in a protected multitasking kernel. There is almost no point to writing user mode apps in assembly because all you're doing is calling kernel syscalls to do anything.

How is the kernel doing it? A driver and careful arbitration between multiple streams and a kernel sound mixer and so on.

What is the driver doing?

Probably reading/writing to the sound card registers at 22xh or whatever the hell they use now (look up old school DOS SoundBlaster digital audio programming, it's actually very simple), or rather programming DMA to dump your data that you've written to a buffer in user land 2 bytes at a time, 44,100 times per second or whatever your sample rate is or likewise reading data from whatever register is sampling 1 sample of data on the mic, also at 22,050 or 44,100 times per second to your file buffer so you can fread it. Do modern sound devices like the common onboard Realtek chip still use the classic SoundBlaster I/O space? Is legacy SoundBlaster compatibility still even a thing? I have no idea!

If I recall with the old SoundBlaster DOS programming the DAC isn't directly exposed to the host CPU, or at least not used, but rather there is at least like a 16 byte or more read/write FIFO per stereo channel on the sound board so that you can use DMA to read/write data faster than the sample rate so you have some bit of asynchronous operation for at least a few samples before you have to read/write again. Otherwise you have to have your CPU sit there and babysit the DAC or risk missing samples.

For what you want to do, DOS is still your best bet for learning. Or an old game console like the Gameboy Advance where all this stuff is WELL documented and accessible to your program with no OS or other processes to worry about. It's far easier to learn hardware tinkering without the complexities of modern OSes in the way or doing kernel/driver development.

I recommend get yourself a GameBoy Advance with a Flash cart, the Virtual Boy Advance emulator which has real time memory viewing and an assembly debugger with single stepping, and the GBA ARM7 GCC. You're also working from a ROM cartridge so you get to deal with the concepts of powering on in ROM and having to copy stuff to RAM, initialize the hardware to a known state, etc when you start up. And ARM7 is a dreamy instruction set and the GBA is crude enough to get the basics but with a nice clean flat linear 32 bit memory map with no segmenting bullshit and no cache or complex memory management to deal with. You get some VRAM with some hardware defined display modes (several tile and bitmapped modes), an audio DAC, a couple DMA channels, like a 3 instruction prefetch buffer, some RAM, sprites, etc.

In fact your first program is going to be a hello world that just sets the screen to a solid color via the palette to verify that you are compiling, linking, and executing correctly. Before that you can write a single instruction, then load your ROM in the emulator and look at the debugger and see if you see your instruction is there at the correct startup address. If you want text, you have to create your own character set tiles and make your own string functions write the tiles to VRAM and everything. It's new enough to be simple and easy without any technology limitations or backward compatibility with the 70s encumbering and complicating the architecture, but old enough that it's simple and not overwhelming like a modern computer.

And there is a DAC with 2 DMA channels dedicated for stereo that you can play with in assembly :D And some timer channels. Numerous examples and home brew support for writing a music tracker and mixer and all that.
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
But, at the hardware level, what you're actually doing is making the 'int' hardware call with a direct integer argument and setting parameters in certain registers.

This is what I want to do.

Like many of your recent questions, the answers is: you can't do that.

A modern OS like Linux/Unix or Windows is a managed playground. Teacher is watching you to make sure you don't do something you aren't supposed to. And AFAIK, directly interacting with hardware resources from a user mode program is very high on "not allowed" list.

If you want to tinker with things at that level, you need to either start writing your own OS, or start playing around with microcontrollers.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Just be really careful not to type /dev/sda by accident! Especially when writing sound to it. I know from experience.

Haha that's awesome. Where did it start writing? Or was it the other way around? Mother of all hardware white noise generators?

I miss the good old days of inp() and outp().

Yep it was pretty simple to write to low level hardware from C back in the day, or to output inline assembly code to do it. I used inline asm in a Turbo Pascal program to read/write the SoundBlaster's DAC and MIDI ports around 1990 or so I guess. Pretty much everything you could do back then that was fun involved putting stuff into ports or reading from them. Keyboard controller, 8042 timer chip, VGA registers, UARTS, DMA controller, that was some fun stuff. I've thought about getting into arduinos to get back into some of that low level hackery, but I haven't had the time for another hobby.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,697
4,658
75
Haha that's awesome. Where did it start writing? Or was it the other way around? Mother of all hardware white noise generators?
It overwrite the MBR and part of a system image partition. Fortunately, I figured out how to fix the MBR, and I didn't need the system image partition. :)
 

Exophase

Diamond Member
Apr 19, 2012
4,439
9
81
A modern OS like Linux/Unix or Windows is a managed playground. Teacher is watching you to make sure you don't do something you aren't supposed to. And AFAIK, directly interacting with hardware resources from a user mode program is very high on "not allowed" list.

Then again, there's always /dev/mem on Linux if you really want to go behind teacher's back:

http://linux.die.net/man/4/mem
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
Then again, there's always /dev/mem on Linux if you really want to go behind teacher's back:

http://linux.die.net/man/4/mem

Heh, teacher certainly can be a little schizophrenic in the case of Linux. It was only a few weeks ago that someone discovered that Linux makes it possible to brick your motherboard because it auto-mounts UEFI memory as read/write and allows you to delete or overwrite it without complaint. Fun times. :)
 

Exophase

Diamond Member
Apr 19, 2012
4,439
9
81
Heh, teacher certainly can be a little schizophrenic in the case of Linux. It was only a few weeks ago that someone discovered that Linux makes it possible to brick your motherboard because it auto-mounts UEFI memory as read/write and allows you to delete or overwrite it without complaint. Fun times. :)

Wow, that's something else. Big gaff on Linux's part, but that hardware should have never been very easy to write to begin with IMO. Should have needed some kind of elaborate ritual to unlock.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
Wow, that's something else. Big gaff on Linux's part, but that hardware should have never been very easy to write to begin with IMO. Should have needed some kind of elaborate ritual to unlock.

Well to be fair the hardware is writeable to allow for things like in-OS BIOS updates (totally legit). The real *redacted* is in the kernel mounting it as writeable and having it sitting out there in the file system. From what I read in the discussions apparently the kernel devs have the attitude of mount everything as writeable that can be written to, which seems a little... shortsighted.

Please don't swear in the tech forums -- Programming Moderator Ken g6
 
Last edited by a moderator:

Exophase

Diamond Member
Apr 19, 2012
4,439
9
81
Well to be fair the hardware is writeable to allow for things like in-OS BIOS updates (totally legit). The real *redacted* is in the kernel mounting it as writeable and having it sitting out there in the file system. From what I read in the discussions apparently the kernel devs have the attitude of mount everything as writeable that can be written to, which seems a little... shortsighted.

It's not that I think the hardware shouldn't be writeable, I just think you should have to go through a lot of hoops first. Not security hoops, just "are you sure this is really what you want to be doing" hoops. Ones that you have to routinely do again.

Then again, maybe you do and the kernel does it anyway.

Please don't quote swearing in the tech forums -- Programming Moderator Ken g6
 
Last edited by a moderator:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,697
4,658
75
The OP has his answer, and this thread has gone far enough off-topic that I'm closing it. -- Programming Moderator Ken g6
 
Status
Not open for further replies.