RTL8139 Driver Part 1.
As this project is going to be a tut too, i will start at the very basic (that where i always start ) and work our way up.
First i will be making a driver, for my RealTek 8139 ethernet card, there are drivers out there, but i find it better to code your own and may use other drivers as a referance, only if something does not work.
The drivers will start life as a normal program and be converted to a module later.
Also do not worry about differant drivers at this stage, as because of the way the module interface work, you just need to load the right module for your card.
So lets get coding first thing to do is test for a RTL8139 ethernet card on our PC.
Here's how we do that:
;=========================================================;
; Test for RTL8139 12/06/06 ;
;---------------------------------------------------------;
; By Dex. ;
; ;
; To assemble use fasm as follows ;
; c:\fasm RTL8139.asm RTL8139.dex ;
;=========================================================;
use32
ORG 0x200000 ; where our program is loaded to
jmp start ; jump to the start of program.
db 'DEX1' ; We check for this, to make sure it a valid Dex4u file.
Msg1: db 'Press any key to test for RTL8139 ethernet card.',13,0
Msg2: db 'RTL8139 ethernet card, found! ;) ',13,0
MsgError: db 'Error!, RTL8139 ethernet card not found :(.',13,0
;----------------------------------------------------;
; Start of program. ;
;----------------------------------------------------;
start:
mov ax,18h ; set ax to nonlinear base
mov ds,ax ; add them to ds
mov es,ax ; and es.
;----------------------------------------------------;
; Get calltable address. ;
;----------------------------------------------------;
mov edi,Functions ; this is the interrupt
mov al,0 ; we use to load the DexFunction.inc
mov ah,0x0a ; with the address to dex4u functions.
int 40h
;----------------------------------------------------;
; Try print string. ;
;----------------------------------------------------;
mov esi,Msg1 ; this point's to our string.
call [PrintString] ; this call the print function.
call [WaitForKeyPress] ; is the wait for keypress function.
mov eax,0x813910ec ; vendor + device ID
call [PciFindDevice] ; Scan PCI bus for RTL8139 card
jc CardNotFound ; IF no card print error and exit.
mov dword[PciEthAddress],eax
;----------------------------------------------------;
; Print card found. ;
;----------------------------------------------------;
mov esi,Msg2 ; this point's to our string.
call [PrintString] ; this call the print function.
call [WaitForKeyPress] ; is the wait for keypress function.
ret ; This returns to the CLI/GUI
;----------------------------------------------------;
; Print card not found. ;
;----------------------------------------------------;
CardNotFound:
mov esi,MsgError ; this point's to our string.
call [PrintString] ; this call the print function.
call [WaitForKeyPress] ; is the wait for keypress function.
ret ; This returns to the CLI/GUI
;----------------------------------------------------;
; Data. ;
;----------------------------------------------------;
PciEthAddress dd 0 ;put here temp.
include 'DexFunctions.inc' ; Here is where we includ our "DexFunctions.inc" file
Now let test the code first before i install the card.

Now lets try it with the card installed.

Good that seems to work 8).
RTL8139 Driver Part 2.
The RTL8139 driver ( or anyother ethernet driver ), will only need to implement 4 functions.
1. Probe ( enables it )
2. Reset ( a virgin state )
3. Poll ( test for a received packet )
4. Transmit (transmits a packet )
So we will start by writing each function, at the same time we will include some code, that will be used for testing and debuging, that can be removed later.
First function we will write is the "Probe" function, luck for us, we have some built in Dex4u functions to help us.
So let do some coding, first we will need some constans, that we will put in a include
file called "EtherNetConst.inc", the info to make these constants will come from doc like rt8139.pdf, Linu, menuetOS and we will add them as needed.
EtherNetConst.inc
PciRegCommand equ 0x04 ; command reg
PciBitPio equ 0 ; bit0: io space control
PciBitMmio equ 1 ; bit1: memory space control
PciBitMaster equ 2 ; bit2: device acts as a PCI master
Probe function code
Probe:
mov eax,[PciEthAddress]
add eax,PciRegCommand
call [PciRegRead32]
or dl, (1 shl PciBitMaster) or (1 shl PciBitPio)
and dl, not (1 shl PciBitMmio)