Malfunction wrote:
Normally when I connect a PCI device I bought, it communicates with PC through PCI.
Yes.
Malfunction wrote:
To do this registers of the device are mapped to memory or i/o address space. Then I write and read these with appropriate instructions. Things seem OK on PC side.
Correct.
Malfunction wrote:
But what's happening on the device side? How does it talk to via PCI. What hardware or protocol do they use?
Short answer they don't talk to the PC. They talk to the PCI Controller using the PCI bus. They provide an info sheet which describes which ports and MMIO addresses the device use. The PC side (or should we call it the CPU side) talks to the same PCI Controller, specifying the PCI address in IO registers (0xCF8). Then the PCI Controller provides the device's PCI info in it's data registers (0xCFC). The CPU can't directly access the PCI bus, only through the PCI controller. For PCIe, you don't use IO ports, rather MMIO. But this is irrelevant from the device's point of view, as it's connected to the PCI bus regardless.
Very simply put, devices are "talking" to the CPU with the PCI Controller translating between them.
Malfunction wrote:
If that device too for example has an x86 cpu inside, when it connects to my main PC what happens. How I/O and PCI stuff are done on device side?
I'm not sure anyone ever tried to use an x86 CPU on a PCI card. But I've used an X25 amateur radio card once which had a Z80 CPU on it. It worked like a totally separated machine, it couldn't access the default PCI Controller (or any other IO) ports as the main CPU did, it's access was strictly limited to the PCI bus' interface.
Malfunction wrote:
I also wanna know what would happen if we change bus from pci to i2c in previous question.
They are not replaceable.
PCI is a complex bus system with wide addresses and high speed. It also has an address space standard with a hierarchical structure (see the aforementioned wiki page) and a main controller.
I2c on the other hand uses as few bits for the communication as possible, therefore it is a serial protocol with 2 bits only where speed was never a goal. It does not have any address space standards or hierarchy or designated controller like the PCI, rather uses packets between the equal nodes, where each device has it's "node address" and can be either a sender (master) or receiver (slave). Note this is a very-very simplified description for both PCI and i2c, but hope it helps you.
Cheers,
bzt