Access the I/O and memory directly

Sometimes you simply MUST control I/O ports to manipulate specific hardware. Modula-2 does not offer this functionality as is. Therefore I made it for FST Modula-2 compiler.
This library is very short, so there will be no comments and I have cut out the GPL notices. Remember however that this software is GNU GPL.

DEFINITION MODULE for low level access

DEFINITION MODULE LowLevel;

TYPE    IOsize          = (Byte, Word);


PROCEDURE InPort (Port : CARDINAL; Size : IOsize) : CARDINAL;
(*
    Return value by reading I/O port <Port>.
    *)

PROCEDURE OutPort (Port, Value : CARDINAL; Size : IOsize);
(*
    Send byte <Value> to I/O port <Port>.
    *)

PROCEDURE Peek (segm, offs : CARDINAL) : CARDINAL;
(*
    Return the value of the word stored at segm:offs in PC memory.
    If 'offs' is odd, it will be decremented.
    *)

END LowLevel.
   

IMPLEMENTATION MODULE for low level access

IMPLEMENTATION MODULE LowLevel;

(*  This is FREE software, as described in the GNU General Public Licences.    *)

FROM    SYSTEM          IMPORT  ASSEMBLER;


PROCEDURE InPort (Port : CARDINAL; Size : IOsize) : CARDINAL;

(*  Return value by reading I/O port 'Port'.    *)

VAR Result  : CARDINAL;

BEGIN
    IF Size = Word THEN
        ASM
            MOV  DX, Port
            IN   AX, DX
            MOV  Result, AX
        END;
    ELSE
        ASM
            MOV  DX, Port
            IN   AL, DX
            MOV  AH, 0
            MOV  Result, AX
        END;
    END;
    RETURN Result;
END InPort;


PROCEDURE OutPort (Port, Value : CARDINAL; Size : IOsize);

(*  Send byte 'Value' to I/O port 'Port'.   *)

BEGIN
    IF Size = Word THEN
        ASM
            MOV  DX, Port
            MOV  AX, Value
            OUT  DX, AX
        END;
    ELSE
        ASM
            MOV  DX, Port
            MOV  AL, Value
            OUT  DX, AL
        END;
    END;
END OutPort;


PROCEDURE Peek (segm, offs  : CARDINAL) : CARDINAL;

VAR     res     : CARDINAL;

BEGIN
    ASM
        MOV     BX, segm
        MOV     ES, BX
        MOV     BX, offs
        AND     BX, 0FFFEH
        MOV     AX, ES:[BX]
        MOV     res, AX
    END;
    RETURN res
END Peek;


END LowLevel.
   

Page created years and years ago but