-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmac.cc
400 lines (377 loc) · 9.34 KB
/
dmac.cc
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* DMA controller of Geyser CPU
Copyright (c) 2021 Amano laboratory, Keio University.
Author: Takuya Kojima
This file is part of CubeSim, a cycle accurate simulator for 3-D stacked system.
CubeSim is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
CubeSim 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CubeSim. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dmac.h"
#include "accesstypes.h"
#include "vmips.h"
#include "options.h"
#include "excnames.h"
DMAC::DMAC(Mapper &m) : bus(&m)
{
config = new DMACConfig();
bus->map_at_physical_address(config, DMAC_ADDR_BASE);
block_words = machine->opt->option("dcachebsize")->num >> 2;
buffer = new uint32[block_words];
mem_bandwidth = machine->opt->option("mem_bandwidth")->num;
}
void DMAC::exception(uint16 excCode, int mode, int coprocno)
{
if (excCode == DBE) {
exception_pending = true;
}
}
bool DMAC::address_valid(uint32 addr)
{
uint32 align = query.burst ? block_words * 4 : 4;
return (addr % align) == 0;
}
void DMAC::step()
{
uint32 addr;
if (config->isEnabled()) {
next_status = status;
switch (status) {
case DMAC_STAT_IDLE:
if (config->isKicked()) {
config->assertBusy();
config->negateKicked();
query = config->getQuery();
counter = 0;
word_counter = 0;
if (query.zero_write) {
if (address_valid(query.src)) {
next_status = DMAC_STAT_WRITE_REQ;
} else {
next_status = DMAC_STAT_EXIT;
config->assertAddrErr();
}
} else {
if (address_valid(query.src)) {
next_status = DMAC_STAT_READ_REQ;
} else {
next_status = DMAC_STAT_EXIT;
config->assertAddrErr();
}
}
}
break;
case DMAC_STAT_READ_REQ:
if (bus->acquire_bus(this)) {
if (query.burst) {
addr = query.src +
block_words * counter * 4;
for (int i = 0; i < block_words; i++) {
bus->request_word(addr + 4 * i,
DATALOAD, this);
}
} else {
bus->request_word(query.src + 4 * counter,
DATALOAD, this);
}
word_counter = 0;
next_status = DMAC_STAT_READING;
}
break;
case DMAC_STAT_READING:
for (int i = 0; i < mem_bandwidth; i++) {
if (query.burst) {
addr = query.src + block_words * counter * 4 +
4 * word_counter;
} else {
addr = query.src + 4 * counter;
}
if (bus->ready(addr, DATALOAD, this)) {
buffer[word_counter] =
bus->fetch_word(addr, DATALOAD, this);
if (++word_counter == block_words ||
!query.burst) {
next_status = DMAC_STAT_READ_DONE;
break;
}
}
}
break;
case DMAC_STAT_READ_DONE:
bus->release_bus(this);
if (address_valid(query.dst)) {
config->setIsLastWrite(false);
next_status = DMAC_STAT_WRITE_REQ;
} else if (config->isAbort()) {
next_status = DMAC_STAT_EXIT;
} else {
config->setIsLastWrite(true);
next_status = DMAC_STAT_EXIT;
config->assertAddrErr();
}
break;
case DMAC_STAT_WRITE_REQ:
if (bus->acquire_bus(this)) {
if (query.burst) {
addr = query.dst +
block_words * counter * 4;
for (int i = 0; i < block_words; i++) {
bus->request_word(addr + 4 * i,
DATASTORE, this);
}
} else {
bus->request_word(query.dst + 4 * counter,
DATASTORE, this);
}
word_counter = 0;
next_status = DMAC_STAT_WRITING;
}
break;
case DMAC_STAT_WRITING:
uint32 data;
for (int i = 0; i < mem_bandwidth; i++) {
if (query.zero_write) {
data = 0;
} else {
data = buffer[word_counter];
}
if (query.burst) {
addr = query.dst + block_words * counter * 4 +
4 * word_counter;
} else {
addr = query.dst + 4 * counter;
}
if (bus->ready(addr, DATASTORE, this)) {
bus->store_word(addr, data, this);
if (++word_counter == block_words ||
!query.burst) {
next_status = DMAC_STAT_WRITE_DONE;
break;
}
}
}
break;
case DMAC_STAT_WRITE_DONE:
if (++counter == query.len) {
next_status = DMAC_STAT_EXIT;
} else if (config->isAbort()) {
next_status = DMAC_STAT_EXIT;
} else {
if (query.zero_write) {
next_status = DMAC_STAT_WRITE_REQ;
} else {
next_status = DMAC_STAT_READ_REQ;
}
}
config->setIsLastWrite(true);
break;
case DMAC_STAT_EXIT:
bus->release_bus(this);
config->assertDone();
config->negateBusy();
config->set_success_len(counter);
next_status = DMAC_STAT_IDLE;
break;
}
if (exception_pending) {
config->assertBusErr();
next_status = DMAC_STAT_EXIT;
exception_pending = false;
}
status = next_status;
}
if (config->isDone() && config->isIRQEn()) {
assertInt(IRQ5);
} else {
deassertInt(IRQ5);
}
}
void DMAC::reset()
{
config->reset();
status = DMAC_STAT_IDLE;
exception_pending = false;
}
//to override DeviceInt
const char *DMAC::descriptor_str() const
{
return "DMA Controller";
}
DMACConfig::DMACConfig()
: Range(0, DMAC_CONF_SIZE, 0, MEM_READ_WRITE)
{
bool opt_bigendian = machine->opt->option("bigendian")->flag;
byteswapped = (((opt_bigendian) && (!machine->host_bigendian))
|| ((!opt_bigendian) && machine->host_bigendian));
reset();
}
void DMACConfig::reset()
{
kicked = enabled = irq_en = abort = busy = done = addr_err
= bus_err = end_stat = zero_write = burst = false;
dma_len = dma_src = dma_dst = success_len = 0;
}
uint32 DMACConfig::fetch_word(uint32 offset, int mode,
DeviceExc *client)
{
uint32 ret_data;
switch (offset) {
case DMAC_ENABLE_OFFSET:
ret_data = enabled ? 1 : 0;
break;
case DMAC_INTEN_OFFSET:
ret_data = irq_en ? 1 : 0;
break;
case DMAC_CTRL_OFFSET:
ret_data = abort ? DMAC_ABORT_BIT : 0;
break;
case DMAC_STATUS_OFFSET:
ret_data = (busy ? DMAC_BUSY_BIT : 0) |
(done ? DMAC_DONE_BIT : 0) |
(addr_err ? DMAC_ADRERR_BIT : 0) |
(bus_err ? DMAC_BUSERR_BIT : 0) |
(end_stat ? DMAC_ESTATE_BIT : 0);
break;
case DMAC_ATTR_OFFSET:
ret_data = (zero_write ? DMAC_ZERO_BIT : 0) |
(burst ? DMAC_BURST_BIT : 0);
break;
case DMAC_LEN_OFFSET:
ret_data = dma_len;
break;
case DMAC_SRC_OFFSET:
ret_data = dma_src;
break;
case DMAC_DST_OFFSET:
ret_data = dma_dst;
break;
case DMAC_DONELEN_OFFSET:
ret_data = success_len;
break;
default:
ret_data = 0;
}
return ret_data;
}
uint16 DMACConfig::fetch_halfword(uint32 offset, DeviceExc *client)
{
uint32 word_data;
if (byteswapped) {
//big endian
if (offset % 4 == 2) {
word_data = fetch_word(offset & ~0x3, DATALOAD, client);
} else {
word_data = 0;
}
} else {
//little endian
if (offset % 4 == 0) {
word_data = fetch_word(offset, DATALOAD, client);
} else {
word_data = 0;
}
}
return (uint16)(word_data & 0xFFFF);
}
uint8 DMACConfig::fetch_byte(uint32 offset, DeviceExc *client)
{
uint32 word_data;
if (byteswapped) {
//big endian
if (offset % 4 == 3) {
word_data = fetch_word(offset & ~0x3, DATALOAD, client);
} else {
word_data = 0;
}
} else {
//little endian
if (offset % 4 == 0) {
word_data = fetch_word(offset, DATALOAD, client);
} else {
word_data = 0;
}
}
return (uint8)(word_data & 0xFF);
}
void DMACConfig::store_word(uint32 offset, uint32 data,
DeviceExc *client)
{
switch (offset) {
case DMAC_ENABLE_OFFSET:
enabled = (data & DMAC_ENABLED_BIT) != 0;
break;
case DMAC_INTEN_OFFSET:
irq_en = (data & DMAC_INTEN_BIT) != 0;
break;
case DMAC_CTRL_OFFSET:
if (!busy) {
kicked = (data & DMAC_START_BIT) != 0;
}
abort = (data & DMAC_ABORT_BIT) != 0;
break;
case DMAC_STATUS_OFFSET:
done = done & ((data & DMAC_DONE_BIT) == 0);
addr_err = addr_err & ((data & DMAC_ADRERR_BIT) == 0);
bus_err = bus_err & ((data & DMAC_BUSERR_BIT) == 0);
end_stat = end_stat & ((data & DMAC_ESTATE_BIT) == 0);
break;
case DMAC_ATTR_OFFSET:
zero_write = (data & DMAC_ZERO_BIT) != 0;
burst = (data & DMAC_BURST_BIT) != 0;
break;
case DMAC_LEN_OFFSET:
dma_len = data;
break;
case DMAC_SRC_OFFSET:
dma_src = data;
break;
case DMAC_DST_OFFSET:
dma_dst = data;
break;
case DMAC_DONELEN_OFFSET:
success_len = data;
break;
}
}
void DMACConfig::store_halfword(uint32 offset, uint16 data,
DeviceExc *client)
{
if (byteswapped) {
//big endian
if (offset % 4 == 2) {
store_word(offset & ~0x3, (uint32)data, client);
}
} else {
//little endian
if (offset % 4 == 0) {
store_word(offset, (uint32)data, client);
}
}
}
void DMACConfig::store_byte(uint32 offset, uint8 data,
DeviceExc *client)
{
if (byteswapped) {
//big endian
if (offset % 4 == 3) {
store_word(offset & ~0x3, (uint32)data, client);
}
} else {
//little endian
if (offset % 4 == 0) {
store_word(offset, (uint32)data, client);
}
}
}
DMA_query_t DMACConfig::getQuery()
{
DMA_query_t q = {dma_src, dma_dst, dma_len,
zero_write, burst};
return q;
}