-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.pas
More file actions
213 lines (213 loc) · 8.49 KB
/
timer.pas
File metadata and controls
213 lines (213 loc) · 8.49 KB
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
205
206
207
208
209
210
211
212
213
(******************************************************************************)
(* This file conteins definitions, data sructures and the implementations of *)
(* a two channels 2681 serial device. *)
(* Designed to be interfaced with MC68k Class. *)
(* *)
(******************************************************************************)
unit timer;
(***********************************************************)
(**) interface (**)
(***********************************************************)
uses ictrl,sysutils;
const SaveFileName='.\sav\timer.sav';
type ETimerEx=class(exception);
type c2500timer=class
(***********************************************************)
(**) private (**)
(***********************************************************)
TimerControl : byte; //0x02120040
TimerRegister0 : word; //0x02120050
TimerRegister1 : word; //0x02120060
TimerRegister2 : word; //0x02120070
TimerCount0 : word; //0x02120050
TimerCount1 : word; //0x02120060
TimerCount2 : word; //0x02120070
fakeCpuCycles : int64;
precCpuCycles : int64;
NoUseButItsAddress:byte;
intc : ^IntrCtrl;
Cpucycles : ^int64;
// function lbendian(a:word):word;
(***********************************************************)
(**) protected (**)
(***********************************************************)
(***********************************************************)
(**) public (**)
(***********************************************************)
function InterfaceRead8 (address:word): byte; //By this function cpu access in reading mode to 8 bit memory mapped registers.
procedure InterfaceWrite8 (address:word;data:byte); //By this function cpu access in writing mode to 8 bit memory mapped registers.
function InterfaceRead16 (address:word): word; //By this function cpu access in reading mode to 16 bit memory mapped registers.
procedure InterfaceWrite16 (address:word;data:word); //By this function cpu access in writing mode to 16 bit memory mapped registers.
procedure SetCPUCyclescCounter(CPUCycl:pointer); //Links timer counters to cpu cyles couter.
procedure SetInterruptController(ic:pointer); //Assigns an interrupt controller to the device.
procedure StatusSave;
procedure StatusRestore;
procedure reset;
procedure tick; //Updates status of the device.
constructor create;
end;
(******************************************************************************)
(**) (**)
(**) implementation (**)
(**) (**)
(******************************************************************************)
procedure c2500timer.InterfaceWrite8 (address:word;data:byte);
begin
case address of
$00:begin //Timer Control
timercontrol:=data;
end;
$10:begin //Timer Register 0
end;
$20:begin //Timer Register 1
end;
$30:begin //Timer Register 2
end;
end;
end;
(******************************************************************************)
function c2500timer.InterfaceRead8 (address:word): byte;
begin
case address of
$00:begin //Timer Control
result:=timercontrol;
end;
$10:begin //Timer Register 0
end;
$20:begin //Timer Register 1
end;
$30:begin //Timer Register 2
end;
end;
end;
(******************************************************************************)
procedure c2500timer.InterfaceWrite16 (address:word;data:word);
begin
case address of
$00:begin //Timer Control
end;
$10:begin //Timer Register 0
TimerRegister0:=data;
timercount0:=0;
end;
$20:begin //Timer Register 1
TimerRegister1:=data;
timercount1:=0;
end;
$30:begin //Timer Register 2
TimerRegister2:=data;
timercount2:=0;
end;
end;
end;
(******************************************************************************)
function c2500timer.InterfaceRead16 (address:word):word;
begin
case address of
$00:begin //Timer Control
end;
$10:begin //Timer Register 0
result:=timercount0;
end;
$20:begin //Timer Register 1
result:=timercount1;
end;
$30:begin //Timer Register 2
result:=timercount2;
end;
end;
end;
(******************************************************************************)
constructor c2500timer.create;
begin
cpucycles:=@fakecpucycles;
end;
(******************************************************************************)
procedure c2500timer.SetCPUCyclescCounter(CPUCycl:pointer);
begin
cpucycles:=CPUCycl;
end;
(******************************************************************************)
procedure c2500timer.SetInterruptController(ic:pointer);
begin
intc:=ic;
end;
(******************************************************************************)
procedure c2500timer.tick;
var app:int64;
begin
app:=Cpucycles^-preccpucycles;
if timerregister0<>0 then
TimerCount0:=TimerCount0+app;
if timerregister1<>0 then
TimerCount1:=TimerCount1+app;
if timerregister2<>0 then
TimerCount2:=TimerCount2+app;
preccpucycles:=Cpucycles^;
if TimerCount0>timerregister0 then begin
TimerCount0:=0;
timerregister0:=0;
if timercontrol and $40<>0 then raise ETimerEx.Create('Watchdog is barking: Bau bau.');
end;
if TimerCount1>timerregister1 then begin
if timerregister1<>1 then intc^.irq($1f); //autovectored 7
TimerCount1:=0;
timerregister1:=0;
end;
if TimerCount2>timerregister2 then begin
if timerregister2<>1 then begin
//timerregister2:=0;
intc^.irq($1f); //autovectored 7
end
else asm
nop
end;
TimerCount2:=0;
end;
end;
(******************************************************************************)
procedure c2500timer.reset;
begin
TimerControl := 0;
TimerRegister0 := 0;
TimerRegister1 := 0;
TimerRegister2 := 0;
TimerCount0 := 0;
TimerCount1 := 0;
TimerCount2 := 0;
end;
(******************************************************************************)
procedure c2500timer.StatusSave;
var f:file;
function addrcalc(addr1,addr2:pointer):longword; assembler;
asm
push ebx
mov eax,addr1
mov ebx,addr2
sub eax,ebx
pop ebx
end;
begin
assignfile(f,SaveFileName);
rewrite(f,1);
blockwrite(f,TimerControl,addrcalc(@NoUseButItsAddress,@TimerControl));//29);
closefile(f);
end;
(******************************************************************************)
procedure c2500timer.StatusRestore;
var f:file;
function addrcalc(addr1,addr2:pointer):longword; assembler;
asm
push ebx
mov eax,addr1
mov ebx,addr2
sub eax,ebx
pop ebx
end;
begin
assignfile(f,SaveFileName);
system.Reset(f,1);
blockread(f,TimerControl,addrcalc(@NoUseButItsAddress,@TimerControl));//29);
closefile(f);
end;
end.