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
// Portions of this file are derived from the T80 Z80-compatible microprocessor core,
// Copyright (c) 2001-2002 Daniel Wallner, and from the T80N modifications made for the
// ZX Spectrum Next Project, Copyright 2020 Fabio Belavenuto, Victor Trucco, Charlie Ingley,
// Garry Lancaster, ACX. Redistributed under the three-clause BSD licence reproduced in NOTICE.
//! The interface between the core and the machine it sits in.
use crateZ80nCommand;
/// What the core is doing on the bus during a given T-state.
///
/// Passed to [`Host::wait_states`] and [`Host::bus_edge`], so a host can add wait states or watch
/// activity at the exact point in an instruction the hardware would.
/// A whole machine cycle, reported once it has run.
///
/// A machine that works a bus cycle at a time — charging contention, advancing a clocked
/// peripheral — wants one report per cycle rather than one per edge, and wants the cycle's length
/// so it can advance by that much in a single step. Coalescing edges by hand to get there is easy
/// to get wrong: a peripheral clocked once per cycle that is ticked once per T-state runs several
/// times too fast.
///
/// The report arrives at the first edge of the *following* cycle, because a cycle's length is not
/// known until it has run. An opcode fetch is the plain case: the opcode has not been read at the
/// point the fetch begins, so how long the fetch takes cannot be known then either. Reports arrive
/// in order, before that following cycle's own transfer, so a clock advanced by them stays in
/// step.
/// The machine the core sits in: its memory, its ports, and the signals reaching the CPU.
///
/// Only the four transfer methods are required. The rest cover things many machines don't have —
/// wait states, interrupt vectors, per-edge observation, telling an opcode fetch from a data read —
/// and default to a machine without them.
///
/// # When a transfer happens
///
/// Every transfer carries `at`: how far into its machine cycle the transfer **completes on the
/// bus**, counted in whole T-states from the cycle opening — the same opening [`Host::wait_states`]
/// is asked at. A machine that timestamps memory activity adds it to the time at which it saw that
/// cycle open.
///
/// | cycle | `at` |
/// |---|---|
/// | opcode fetch | `2 + waits` |
/// | memory read | `3 + waits` |
/// | memory write | `3 + waits` |
/// | port read | `4 + waits` |
/// | port write | `4 + waits` |
///
/// `waits` is what [`Host::wait_states`] returned for the cycle. Two things about this table are
/// easy to assume wrongly. **A port transfer is one T-state later than a memory transfer**, because
/// a port cycle carries an extra wait of its own. And **an opcode fetch completes a T-state earlier
/// than a data read**, because the processor takes the opcode at the end of `T2` so it can decode
/// during the refresh half.
///
/// This is a property of the cycle, not a report of when the core happened to call. The core may
/// read a byte earlier than `at` and hold it, which is an implementation detail; `at` is when the
/// hardware would have completed the transfer.