-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Z80.pas
287 lines (236 loc) · 7.49 KB
/
Z80.pas
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(* ______ ______ ______
/\___ \/\ __ \\ __ \
____ \/__/ /\_\ __ \\ \/\ \ ______________________________________
| /\_____\\_____\\_____\ |
| Zilog \/_____//_____//_____/ CPU Emulator - Pascal Binding |
| Copyright (C) 2022-2023 Zoran Vučenović. |
| |
| Permission to use, copy, modify, and/or distribute this software |
| for any purpose with or without fee is hereby granted. |
| |
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL |
| THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR |
| CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| |
'===================================================================*)
unit Z80;
{$mode ObjFPC}{$H+}
{$J-}
{$inline on}
{$PackRecords C}
interface
uses
SysUtils,
dynlibs;
const
DefaultZ80LibPath =
{$if defined(mswindows)}
'Z80.dll';
{$elseif defined(darwin)}
'libZ80.dylib';
{$elseif defined(unix)}
'libZ80.so';
{$else}
'';
{$endif}
Z80_MAXIMUM_CYCLES = SizeUInt(SizeUInt(SizeUInt.MaxValue) - 30);
Z80_MAXIMUM_CYCLES_PER_STEP = 23;
Z80_HOOK = $64;
Z80_SF = 128;
Z80_ZF = 64;
Z80_YF = 32;
Z80_HF = 16;
Z80_XF = 8;
Z80_PF = 4;
Z80_NF = 2;
Z80_CF = 1;
type
PZ80 = ^TZ80;
TZ80RegisterPair = packed record
case Integer of
0: (word: UInt16);
{$ifdef endian_little}
1: (bytes: packed record low: UInt8; high: UInt8 end);
{$else}
1: (bytes: packed record high: UInt8; low: UInt8 end);
{$endif}
end;
Z80Read = function(context: Pointer; address: UInt16): UInt8; cdecl;
Z80Write = procedure(context: Pointer; address: UInt16; value: UInt8); cdecl;
Z80Halt = procedure(context: Pointer; state: UInt8); cdecl;
Z80Notify = procedure(context: Pointer); cdecl;
Z80Illegal = function(cpu: PZ80; opcode: UInt8): UInt8; cdecl;
TZ80 = record
cycles: SizeUInt;
cycle_limit: SizeUInt;
context: Pointer;
fetch_opcode: Z80Read;
fetch: Z80Read;
read: Z80Read;
write: Z80Write;
input: Z80Read;
output: Z80Write;
halt: Z80Halt;
nop: Z80Read;
nmia: Z80Read;
inta: Z80Read;
int_fetch: Z80Read;
ld_i_a: Z80Notify;
ld_r_a: Z80Notify;
reti: Z80Notify;
retn: Z80Notify;
hook: Z80Read;
illegal: Z80Illegal;
data: array [0..3] of UInt8;
ix_iy: array [0..1] of TZ80RegisterPair;
pc: TZ80RegisterPair;
sp: TZ80RegisterPair;
xy: TZ80RegisterPair;
memptr: TZ80RegisterPair;
af: TZ80RegisterPair;
bc: TZ80RegisterPair;
de: TZ80RegisterPair;
hl: TZ80RegisterPair;
af_: TZ80RegisterPair;
bc_: TZ80RegisterPair;
de_: TZ80RegisterPair;
hl_: TZ80RegisterPair;
r: UInt8;
i: UInt8;
r7: UInt8;
im: UInt8;
request: UInt8;
resume: UInt8;
iff1: UInt8;
iff2: UInt8;
q: UInt8;
options: UInt8;
int_line: UInt8;
halt_line: UInt8;
end;
const
Z80_OPTION_OUT_VC_255 = 1;
Z80_OPTION_LD_A_IR_BUG = 2;
Z80_OPTION_HALT_SKIP = 4;
Z80_OPTION_XQ = 8;
Z80_OPTION_IM0_RETX_NOTIFICATIONS = 16;
Z80_OPTION_YQ = 32;
Z80_MODEL_ZILOG_NMOS = (Z80_OPTION_LD_A_IR_BUG or Z80_OPTION_XQ or Z80_OPTION_YQ);
Z80_MODEL_ZILOG_CMOS = (Z80_OPTION_OUT_VC_255 or Z80_OPTION_XQ or Z80_OPTION_YQ);
Z80_MODEL_NEC_NMOS = Z80_OPTION_LD_A_IR_BUG;
Z80_MODEL_ST_CMOS = (Z80_OPTION_OUT_VC_255 or Z80_OPTION_LD_A_IR_BUG or Z80_OPTION_YQ);
Z80_REQUEST_REJECT_NMI = 2;
Z80_REQUEST_NMI = 4;
Z80_REQUEST_INT = 8;
Z80_REQUEST_SPECIAL_RESET = 16;
Z80_RESUME_HALT = 1;
Z80_RESUME_XY = 2;
Z80_RESUME_IM0_XY = 3;
Z80_HALT_EXIT = 0;
Z80_HALT_ENTER = 1;
Z80_HALT_EXIT_EARLY = 2;
Z80_HALT_CANCEL = 3;
var
z80_power: procedure(self: PZ80; state: ByteBool); cdecl;
z80_instant_reset: procedure(self: PZ80); cdecl;
z80_special_reset: procedure(self: PZ80); cdecl;
z80_int: procedure(self: PZ80; state: ByteBool); cdecl;
z80_nmi: procedure(self: PZ80); cdecl;
z80_execute: function(self: PZ80; cycles: SizeUInt): SizeUInt; cdecl;
z80_run: function(self: PZ80; cycles: SizeUInt): SizeUInt; cdecl;
function z80_r(const self: PZ80): UInt8; inline;
function z80_refresh_address(const self: PZ80): UInt16; inline;
function z80_in_cycle(const self: PZ80): UInt8; inline;
function z80_out_cycle(const self: PZ80): UInt8; inline;
function IsLibraryLoaded: Boolean;
function LoadLibrary(const LibPath: String = ''): Boolean;
function UnloadLibrary: Boolean;
implementation
procedure z80_break(self: PZ80);
begin
self^.cycle_limit := 0;
end;
function z80_r(const self: PZ80): UInt8;
begin
Result := (self^.r and 127) or (Self^.r7 and 128);
end;
function z80_refresh_address(const self: PZ80): UInt16;
begin
Result := UInt16(self^.i) shl 8;
Result := Result or ((self^.r - 1) and 127) or (self^.r7 and 128);
end;
function z80_in_cycle(const self: PZ80): UInt8;
begin
if self^.data[0] = $DB then begin
Result := 7;
end else begin
Result := 8 + (self^.data[1] shr 7);
end;
end;
function z80_out_cycle(const self: PZ80): UInt8;
begin
if self^.data[0] = $D3 then begin
Result := 7;
end else begin
Result := 8 + ((self^.data[1] shr 7) shl 2);
end;
end;
var
LibHandle: TLibHandle;
function IsLibraryLoaded: Boolean;
begin
Result := LibHandle <> NilHandle;
end;
function LoadLibrary(const LibPath: String): Boolean;
var
LibName: String;
begin
if LibHandle = NilHandle then begin
Result := False;
if LibPath = '' then begin
LibName := DefaultZ80LibPath;
end else begin
LibName := LibPath;
end;
LibHandle := SafeLoadLibrary(LibName);
if LibHandle = NilHandle then Exit;
Pointer(z80_power) := GetProcAddress(LibHandle, 'z80_power');
Pointer(z80_instant_reset) := GetProcAddress(LibHandle, 'z80_instant_reset');
Pointer(z80_special_reset) := GetProcAddress(LibHandle, 'z80_special_reset');
Pointer(z80_int) := GetProcAddress(LibHandle, 'z80_int');
Pointer(z80_nmi) := GetProcAddress(LibHandle, 'z80_nmi');
Pointer(z80_execute) := GetProcAddress(LibHandle, 'z80_execute');
Pointer(z80_run) := GetProcAddress(LibHandle, 'z80_run');
end;
Result := True;
end;
function UnloadLibrary: Boolean;
begin
if IsLibraryLoaded then begin
if not dynlibs.UnloadLibrary(LibHandle) then Exit(False);
LibHandle := NilHandle;
end;
z80_power := nil;
z80_instant_reset := nil;
z80_special_reset := nil;
z80_int := nil;
z80_nmi := nil;
z80_execute := nil;
z80_run := nil;
Result := True;
end;
procedure Init;
begin
LibHandle := NilHandle;
UnloadLibrary;
end;
initialization
Init;
finalization
UnloadLibrary;
end.