summaryrefslogtreecommitdiff
path: root/rtl/emx/ports.pas
blob: 28eebd110d0ff7a3ccb3b89bcbe6322e5076cb21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2000 by the Free Pascal development team.

    These files adds support for TP styled port accesses (port[],
    portw[] and portl[] constructs) using Delphi classes.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

(*
  Warning:
  1) You have to enable port access in your CONFIG.SYS (IOPL directive),
     either globally (IOPL=YES), or just for particular application/-s with
     a need for port access (IOPL=app_name1, appname2, ...).
  2) Once you access some port, access to this port is enabled all the time
     for all EMX applications until EMX.DLL is unloaded from memory (i.e.
     all applications using this library finish).
*)

unit Ports;

{ This unit uses classes so ObjFpc mode is required. }
{$Mode ObjFpc}

interface

type
 TPort = class
  protected
   procedure WritePort (P: word; Data: byte);
   function ReadPort (P: word): byte;
  public
   property PP [W: word]: byte read readport write writeport; default;
 end;

 TPortW = class
  protected
   procedure WritePort (P: word; Data: word);
   function ReadPort (P: word): word;
  public
   property PP [W: word]: word read readport write writeport; default;
 end;

 TPortL = class
  protected
   procedure WritePort (P: word; Data: longint);
   function ReadPort (P: word): longint;
  public
   property PP [W: word]: longint read readport write writeport; default;
 end;

 { Non-instantiated vars. As yet, they don't have to be instantiated,
   because neither member variables nor virtual methods are accessed }

var
 Port, PortB: TPort;
 PortW: TPortW;
 PortL: TPortL;

implementation

{Import syscall to call it nicely from assembler procedures.}

procedure syscall; external name '___SYSCALL';

{$AsmMode ATT}

procedure TPort.WritePort (P: word; Data: byte); assembler;
asm
 xorl %ecx, %ecx
{$IFDEF REGCALL}
 movw %ax, %cx
 pushl %edx
 pushl %ecx
{$ELSE REGCALL}
 movw P, %cx
{$ENDIF REGCALL}
 movl %ecx, %edx
 movw $0x7F12, %ax
 call syscall
{$IFDEF REGCALL}
 popl %edx
 popl %eax
{$ELSE REGCALL}
 movw P, %dx
 movb Data, %al
{$ENDIF REGCALL}
 outb %al, %dx
end {['eax', 'ecx', 'edx']};

function TPort.ReadPort (P: word): byte; assembler;
asm
 xorl %ecx, %ecx
{$IFDEF REGCALL}
 movw %ax, %cx
{$ELSE REGCALL}
 movw P, %cx
 pushl %ecx
{$ENDIF REGCALL}
 movl %ecx, %edx
 movw $0x7F12, %ax
 call syscall
{$IFDEF REGCALL}
 popl %edx
{$ELSE REGCALL}
 movw P, %dx
{$ENDIF REGCALL}
 inb %dx, %al
end {['eax', 'ecx', 'edx']};

procedure TPortW.WritePort (P: word; Data : word); assembler;
asm
 xorl %ecx, %ecx
{$IFDEF REGCALL}
 movw %ax, %cx
 pushl %edx
 pushl %ecx
{$ELSE REGCALL}
 movw P, %cx
{$ENDIF REGCALL}
 movl %ecx, %edx
 movw $0x7F12, %ax
 call syscall
{$IFDEF REGCALL}
 popl %edx
 popl %eax
{$ELSE REGCALL}
 movw P, %dx
 movw Data, %ax
{$ENDIF REGCALL}
 outw %ax, %dx
end {['eax', 'ecx', 'edx']};

function TPortW.ReadPort (P: word): word; assembler;
asm
 xorl %ecx, %ecx
{$IFDEF REGCALL}
 movw %ax, %cx
 pushl %ecx
{$ELSE REGCALL}
 movw P, %cx
{$ENDIF REGCALL}
 movl %ecx, %edx
 movw $0x7F12, %ax
 call syscall
{$IFDEF REGCALL}
 popl %edx
{$ELSE REGCALL}
 movw P, %dx
{$ENDIF REGCALL}
 inw %dx, %ax
end {['eax', 'ecx', 'edx']};

procedure TPortL.WritePort (P: word; Data: longint); assembler;
asm
 xorl %ecx, %ecx
{$IFDEF REGCALL}
 movw %ax, %cx
 pushl %edx
 pushl %ecx
{$ELSE REGCALL}
 movw P, %cx
{$ENDIF REGCALL}
 movl %ecx, %edx
 movw $0x7F12, %ax
 call syscall
{$IFDEF REGCALL}
 popl %edx
 popl %eax
{$ELSE REGCALL}
 movw P, %dx
 movl Data, %eax
{$ENDIF REGCALL}
 outl %eax, %dx
end {['eax', 'ecx', 'edx']};

function TPortL.ReadPort (P: word): longint; assembler;
asm
 xorl %ecx, %ecx
{$IFDEF REGCALL}
 movw %ax, %cx
 pushl %ecx
{$ELSE REGCALL}
 movw P, %cx
{$ENDIF REGCALL}
 movl %ecx, %edx
 movw $0x7F12, %ax
 call syscall
{$IFDEF REGCALL}
 popl %edx
{$ELSE REGCALL}
 movw P, %dx
{$ENDIF REGCALL}
 inl %dx, %eax
end {['eax', 'ecx', 'edx']};

end.