Page 1 of 2

Communicating with hardware directly

Posted: Thu Jun 27, 2013 9:51 am
by computertrick
Hi, I am looking for a tutorial or some examples of communicating with hardware directly even the most basic example would be great. I have programmed PIC micro controllers in the past I assume it would be a bit like that where you define which bank and pin to output or receive input from? Or do you address hardware differently on intel processors

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 10:02 am
by Combuster
The only digital GPIO you'll find on a PC is the oldfashioned parallel port. I've never bothered with that thing but it will probably look pretty close to the GPIOs you find in all sorts of other devices.

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 10:19 am
by computertrick
Combuster wrote:The only digital GPIO you'll find on a PC is the oldfashioned parallel port. I've never bothered with that thing but it will probably look pretty close to the GPIOs you find in all sorts of other devices.
Thanks for your reply. In short term I just want to learn how to do this. In long term I'm hoping to read in visual input from a camera or something. Do you know of any tutorials for GPIOs or any tutorials to do with input/output

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 10:37 am
by Casm
The x86's in and out instructions are conceptually similar to programming a port on a microcontroller. Port numbers on a PC don't actually correspond to pins on the processor in a one to one sort of way, but it is possible to think of them in that way. For example, you can get a scan code from the keyboard by reading port 0x60.

But increasingly there is a trend towards devices being memory mapped on the PC. The i/o APIC and HPET being examples. With a memory mapped device you program its registers ostensibly by writing to reserved addresses in main memory. The write never actually reaches main memory, however, but is instead diverted by hardware to the device in question.

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 10:49 am
by computertrick
Casm wrote:The x86's in and out instructions are conceptually similar to programming a port on a microcontroller. Port numbers on a PC don't actually correspond to pins on the processor in a one to one sort of way, but it is possible to think of them in that way. For example, you can get a scan code from the keyboard by reading port 0x60.

But increasingly there is a trend towards devices being memory mapped on the PC. The i/o APIC and HPET being examples. With a memory mapped device you program its registers ostensibly by writing to reserved addresses in main memory. The write never actually reaches main memory, however, but is instead diverted by hardware to the device in question.
I did see something about the keyboard and 0x60 about 10 minutes ago. You can use the out instruction for outputting to hardware right? and input instruction

Code: Select all

out 0x00, ax        ; Just an example
in    ax, 0x60        ; Just an example
I haven't tested the in ax, 0x60 instruction would that bring in data? I read up a few months back that keyboards don't send keystrokes instead the data they send needs to be put through an algorithm to calculate the correct keystroke? I suppose because of this receiving keyboard input you would also need to do this

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 10:57 am
by Casm
computertrick wrote:I did see something about the keyboard and 0x60 about 10 minutes ago. You can use the out instruction for outputting to hardware right? and input instruction
It all depends upon the device in question. Some, like 0x60, can be read and written to, but others might be read only or write only. A port which reports whether some hardware device has successfully completed an operation might sensibly be read only, unless writing to that port accesses another of the device's internal registers.


I haven't tested the in ax, 0x60 instruction would that bring in data? I read up a few months back that keyboards don't send keystrokes instead the data they send needs to be put through an algorithm to calculate the correct keystroke? I suppose because of this receiving keyboard input you would also need to do this
The keyboard generates a hardware interrupt when it has a scan code it wants you to read. Doing an "in al, 0x60" would then get you that scan code.

Scan codes tell you which key has been pressed, but they do not correspond to ascii or unicode characters. Therefore they have to be put through a look up table to obtain the correct character code. For one thing, some keys, such as the shift key, do not correspond to any character, and, for another thing, somebody typing on a Greek keyboard would get the same scan codes, but he wouldn't have much interest in seeing them translated into Latin characters.

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 12:16 pm
by computertrick
Casm wrote:
computertrick wrote:I did see something about the keyboard and 0x60 about 10 minutes ago. You can use the out instruction for outputting to hardware right? and input instruction
It all depends upon the device in question. Some, like 0x60, can be read and written to, but others might be read only or write only. A port which reports whether some hardware device has successfully completed an operation might sensibly be read only, unless writing to that port accesses another of the device's internal registers.


I haven't tested the in ax, 0x60 instruction would that bring in data? I read up a few months back that keyboards don't send keystrokes instead the data they send needs to be put through an algorithm to calculate the correct keystroke? I suppose because of this receiving keyboard input you would also need to do this
The keyboard generates a hardware interrupt when it has a scan code it wants you to read. Doing an "in al, 0x60" would then get you that scan code.

Scan codes tell you which key has been pressed, but they do not correspond to ascii or unicode characters. Therefore they have to be put through a look up table to obtain the correct character code. For one thing, some keys, such as the shift key, do not correspond to any character, and, for another thing, somebody typing on a Greek keyboard would get the same scan codes, but he wouldn't have much interest in seeing them translated into Latin characters.
Hi, thanks for your response I just tried

Code: Select all

in 0x60, ax
. I ran the operating system in qemu and checked the registers and AX is equal to 0x30fa when no key is pressed. Once a key is pressed this changes do you know any documentation to explain how to generate an ascii code with this information?. Also do you know of any documentation to explain these addresses more the 0x60 address how does the processor know that's the keyboard. If I take my computer apart will I see my keyboard connected to pin 96?

I found a diagram of the processor
Image

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 12:53 pm
by Casm
computertrick wrote:Hi, thanks for your response I just tried

Code: Select all

in 0x60, ax
. I ran the operating system in qemu and checked the registers and AX is equal to 0x30fa when no key is pressed. Once a key is pressed this changes do you know any documentation to explain how to generate an ascii code with this information?. Also do you know of any documentation to explain these addresses more the 0x60 address how does the processor know that's the keyboard. If I take my computer apart will I see my keyboard connected to pin 96?
http://webpages.charter.net/danrollins/ ... p/0057.HTM

I found a diagram of the processor
Image
That diagram is about thirty years out of date. Present day x86 processors are square in shape, and have around 200 pins sticking out of the bottom of them.

http://www.google.co.uk/imgres?imgurl=h ... A&dur=4475

They also have a whacking great fan mounted on top of them, so that they don't fry themselves.

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 12:56 pm
by lkurusa
I think"in 0x60, ax" is illegal/not what you wanted.

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 1:02 pm
by Casm
Levex wrote:I think"in 0x60, ax" is illegal/not what you wanted.
"in al, 0x60" is what he wanted. The al is on the "outside" when you are writing out to a port: "out 0x60, al".

in al, 0x60 for eight bit ports

in ax, 0x60 if it was a 16 bit port

in eax, 0x60 if it was a 32 bit port

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 1:38 pm
by lkurusa
I was talking about if he wrote that in his binary it would either not assemble or dp undefined things which might be the reason why he received "scancodes" without a keyboard event.
OP, if you have written it the correct way, then the correct way is to clear the KB buffer on boot by polling 0x60 until it is empty. Then start receving kb IRQ, which is IRQ1 IIRC, and poll 0x60 when it has fired.

Cheers,

Levex.

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 3:27 pm
by computertrick
Levex wrote:I was talking about if he wrote that in his binary it would either not assemble or dp undefined things which might be the reason why he received "scancodes" without a keyboard event.
OP, if you have written it the correct way, then the correct way is to clear the KB buffer on boot by polling 0x60 until it is empty. Then start receving kb IRQ, which is IRQ1 IIRC, and poll 0x60 when it has fired.

Cheers,

Levex.
Sorry what do you mean keyboard event i accessed it directly right? Also what do you mean poll 0x60 until empty. Will the keyboard send b00000000 on empty ?. The program also assembled so it cant be a syntax problem. How come you have to poll until empty. And how would you poll until empty

Re: Communicating with hardware directly

Posted: Thu Jun 27, 2013 3:53 pm
by Mikemk
Read the wiki. Look up interrupts, idt/ivt, ps/2, keyboard input

PS to everyone, if testing on Qemu, make sure you have acpi set up before reading or writing port 0x64. It seems to be a cpu reset port.

Re: Communicating with hardware directly

Posted: Fri Jun 28, 2013 3:51 am
by lkurusa
I said that the keyboard encoder's buffer is mapped to I/O port 0x60, which you have been reading. It might contain undefined things and/or garbage. Better clear it. And yes it will return 0x00 when it has been emptied. :-)
Polling means executing the instruction "in al, 0x60" continously until a condition is met. (In this case until $al == 0)
What assembler are you using? If you use NASM then it shouldn't assemble, because NASM uses the instruction dest, src syntax.

Cheers,

Levex.

Re: Communicating with hardware directly

Posted: Fri Jun 28, 2013 5:17 am
by computertrick
Levex wrote:I said that the keyboard encoder's buffer is mapped to I/O port 0x60, which you have been reading. It might contain undefined things and/or garbage. Better clear it. And yes it will return 0x00 when it has been emptied. :-)
Polling means executing the instruction "in al, 0x60" continously until a condition is met. (In this case until $al == 0)
What assembler are you using? If you use NASM then it shouldn't assemble, because NASM uses the instruction dest, src syntax.

Cheers,

Levex.
Thank's for your reply I will empty the buffer. And yes I am using nasm and this code assembles fine.

Code: Select all

in al, 0x60
Do you think I should be using 16 bit registers in case the keyboard sends 16 bit values?

Also could you explain 0x60 to me. How does the CPU know 0x60 is the keyboard? is 0x60 a pin number or something?

I'm used to working with micro controllers this one

Image

When I want to send data I just set a bit to high on PORTA or PORTB and it turns on. And for receiving I just check if the pin is high. Surly their is some similarity with the Intel processors?