Direct I/O access with Visual Basic
Although, nearly everyone programmed with it, some programmer smile if they are asked about the programming language Basic. But there are some experienced programmers, who create professional programs with Basic to a high quality, which is sometimes even better than the common programming languages nowadays.
Therefore, this is one reason that big software producers with Visual Basic provide the usage of external DDL and the allowance of the delivery of function and data addresses. This is why the foundation for the usage of Kithara Toolkits is made. No Basic programmer has to fear about direct and time critical hardware access anymore.
Sound on / Sound off
With this article we would like to show you how easy it is to access the I/O ports and memory in Basic. For the demonstration, we will use the good-old PC speaker to get some sound - remember, everything is running under Win32 with Visual Basic!
It is necessary to declare the functions of an external DLL for usage in Delphi or Java. Because of this are all declarations included in the delivery of our Toolkits with completely interfaces modul.
Almost all functions return an "error code". With this code you can trace the cause of the error by the function. So, a reliable monitoring for work is guaranteed. Because of the simplification we will save an error analysis.
Now in detail: For the access to I/O ports of PC it is first necessary to activate them. The following I/O ports are required for the sound output : 42 and 43 Hex and 61 Hex. To do this, the individual I/O basic address, the number of connected ports and the size of ports have to be quoted (Byte, Word or DWord)
error = KS_enableIoRange(&H42, 2, KSF_SIZE_8_BIT) If error Then ... ' error handlungFehlerbehandlung End If error = KS_enableIoRange(&H61, 1, KSF_SIZE_8_BIT)
Now we can access to the I/O addresses. Therefore we use a frequence in Hertz to get sound out of the PC speaker.
Dim frequency As Long,value As Long, temp As Long frequency = 440 ' Kammerton a' value = &H1234DD\ frequency ' Part frequenceTeilfrequenz temp = KS_inpb(&H61) ' Port H61 read If (temp And &H3) <> &H3 Then ' already active? KS_outpb &H61, temp Or &H3 ' No? -> acitivate! KS_outpb &H43, &HB6 ' Set state End If KS_outpb &H42, value ' Set frequence (Low) KS_outpb &H42, value \ 2 ^ 8 ' Set frequcene (High)
OK. And how to get them silent, we will reveal the next time ...
Jokes aside. The "pc-squeaker" is already annoying, so we want to swtich it off. Which is easier:
KS_outpb &H61, KS_inpb(&H61) And &HFC
Hint: The I/O ports we accessed are important parts of the units of the PC timer circuit. An erroneous reprogramming of PC hardware could appear some bad followings for the working on operating systems! Either you are really sure what you are doing or you can wait for the "BlueScreen".
Physical memory
Another thread is the direct access to the physical memory of PCs. Visual Basic does not support a working with address pointer, so you use the special functions KS_readMem or KS_writeMem. At first, you have to blend the physical memory sector into the address room of the application.
E.g. an integration card is in the intern of a PC and the dual-port RAM reserves a 16 Kbyte memory sector on the address D800:0000, with the following function you can access to it.
Public pPhysMem As Long ' Pointer on memory KS_mapPhysMem(pPhysMem, 0, &HD8000, &H4000, KSF_MAP_ISA)
The detected pointer pPhysMem enables access to the blended memory to read external data from card. The routine could look like:
Public pMemBuf(&HFF) As Byte ' Puffer for memory data Dim offset As Long, size As Long error = KS_readMem(pMemBuf(0), pPhysMem + offset, size)
At the end of the program its advisably to tidy up, so following shows how to close the memory mapping.
error = KS_unmapPhysMem(pPhysMem)
Thats all!
Conclusion
Now you can see you only need some source text rows and a few functions from the Kithara Toolkit to e.g. access I/O ports or the physical memory of PCs. These tools simplify the programming under Visual Basic so that you don't have to work all night long on kernel programming.


