flat assembler
Message board for the users of flat assembler.

Index > Main > I/O Signals I/O ports: "PC" speaker, ISA, PCI, ...

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 23 Oct 2012, 21:00
Hi, I'm interested in how the I/O instructions work with devices. I found some examples to beep Internal Speakers, but I don't get how it works. on Windows, in device manager, I found I/O ports, but I don't know which signals should I send to them using IN and OUT instructions. Can anyone point me to the right direction where can I get started ?
Thank you.
Post 23 Oct 2012, 21:00
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 23 Oct 2012, 21:37
You have already started.

To use those privileged instructions you have to run in DOS mode or in ring0 usin a driver.

You can try also using debug.exe to play with some ports.
Post 23 Oct 2012, 21:37
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 23 Oct 2012, 23:29
In general, IO mappings are derived from PCI device enumeration (this includes legacy controllers that are bridged). Each device has an identifier that should have a corresponding driver; this driver should know how to use the IO ports according the the device's specification.

The exceptions are the legacy controllers such as PIT, PIC, RTC, CMOS, PS2, ... these have fixed IO mappings but still have a specification on how to use them. I believe the speaker is driven by the PIT Question and simply gates clock signals to produce a tone. (The PIT was originally the "intel 8253" chip)

----

in/out are not privileged instructions - they can actually be used in ring 3 as long as the IOPL in the TSS allows it; though, yes, most OSs disable them all.
Post 23 Oct 2012, 23:29
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 23 Oct 2012, 23:50
I'm using DosBox for testing, but I think you guys don't understand me.
For example, take this example:
Internal Speaker

How these things are happening? I though system speaker port was 61h, but for me, seems arbitrary values.

I need list of values to send, to control them.
Thanks! Smile
Post 23 Oct 2012, 23:50
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 24 Oct 2012, 18:24
The enable is port 0x61, which is gating the output of the i8253 PIT. The signal is defined by the magic value "182" in your example, which if you read the specification, means "select channel 2, LSB then MSB, square wave mode, binary input" into port 0x43 (PIT command register). It then loads the "frequency number" (PIT channel 2 register), which is actually the clock divider of the master clock, one byte at a time LSB then MSB. The frequency is 0x1234DD/divider and divider must be 1..65535. The duration is being controlled based on how long it takes the CPU to count ~1.6M times. (It would be better to use the channel 0 or RTC IRQ for real timing)

In short, you choose when to toggle the speaker and load a divider to get the frequency you want.
Post 24 Oct 2012, 18:24
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 24 Oct 2012, 19:52
Okay, you gave me the little tutorial here, where can I find all the "magic values" that does something?
Post 24 Oct 2012, 19:52
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 21 Nov 2012, 16:46
Overflowz,

No magic there, i8253/54 datasheets are available freely (even Wiki has an article about it).
Post 21 Nov 2012, 16:46
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 29 Nov 2012, 12:12
cod3b453 wrote:
in/out are not privileged instructions - they can actually be used in ring 3 as long as the IOPL in the TSS allows it; though, yes, most OSs disable them all.


Well then can you explain why Windows produces a "Privileged Instruction" Exception when you use in/out?

Maybe I might try one day injecting a thread into user32/kernel32 and use the IN/OUT instructions because it seems like these DLLs use them all the time.

// Added to TODO list. Wink
Post 29 Nov 2012, 12:12
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 29 Nov 2012, 17:52
typedef wrote:
cod3b453 wrote:
in/out are not privileged instructions - they can actually be used in ring 3 as long as the IOPL in the TSS allows it; though, yes, most OSs disable them all.


Well then can you explain why Windows produces a "Privileged Instruction" Exception when you use in/out?
Windows runs with IOPL=0. While x86 documentation distinguishes between "privileged instructions" (CPL=0 only; e.g. HLT, LGDT, MOV CRx) vs. "IOPL-sensitive instructions" (CPL<=IOPL: CLI, STI, IN, OUT), Windows raises the same kind of exception (STATUS_PRIVILEGED_INSTRUCTION) on the offending thread regardless.

Quote:
Maybe I might try one day injecting a thread into user32/kernel32 and use the IN/OUT instructions because it seems like these DLLs use them all the time.
"injecting a thread into user32/kernel32" doesn't make any sense; they're not processes, they're just libraries. As with any library, their code runs with no more privileges than any other code in the process they're loaded into (so they can't use IN/OUT either).
Post 29 Nov 2012, 17:52
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 30 Nov 2012, 22:11
typedef,

Your code have to have enough permissions (via IOPL or I/O permission map thru TSS) to use in/out instructions seamlessly. NTVDM (SoftPC evolved) intrusively emulates direct access to hardware ports for 16-bit code. With enough privileges you can use ready-made WinIO.Sys driver or something similar in Win32.

Intel SDM/AMD APM give clear understanding when (and why) privileged instructions (such as in/out) cause #PF.
Post 30 Nov 2012, 22:11
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.