forked from autch/piemu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcu.c
300 lines (267 loc) · 9.3 KB
/
bcu.c
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
/*
* bcu.c
*
* P/EMU - P/ECE Emulator
* Copyright (C) 2003 Naoyuki Sawa
*
* * Mon Apr 14 00:00:00 JST 2003 Naoyuki Sawa
* - 作成開始。
* * Wed Apr 22 05:21:00 JST 2003 Naoyuki Sawa
* - DMAからのメモリアクセス用に、mem_read_nowait()/mem_write_nowait()追加。
* これまで、DMAからのメモリアクセスでCLKが加算されていたのを修正しました。
*/
#include "app.h"
/****************************************************************************
* エリア定義
****************************************************************************/
typedef struct _AREA {
unsigned addr;
int (*read)(PIEMU_CONTEXT* context, unsigned ofs, int size);
void (*write)(PIEMU_CONTEXT* context, unsigned ofs, int data, int size);
int (*readB)(PIEMU_CONTEXT* context, unsigned ofs);
void (*writeB)(PIEMU_CONTEXT* context, unsigned ofs, int data);
int (*readH)(PIEMU_CONTEXT* context, unsigned ofs);
void (*writeH)(PIEMU_CONTEXT* context, unsigned ofs, int data);
int (*readW)(PIEMU_CONTEXT* context, unsigned ofs);
void (*writeW)(PIEMU_CONTEXT* context, unsigned ofs, int data);
} AREA;
#define DEF_AREA(addr, mod) \
{ \
addr, \
mod##_read, mod##_write, \
mod##_readB, mod##_writeB, \
mod##_readH, mod##_writeH, \
mod##_readW, mod##_writeW \
}
#define DEF_NONE(addr) { addr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
static AREA area_tbl[] = {
/* 0*/ DEF_AREA(0x0000000, fram), //{ 0x0000000, fram_read, fram_write, fram_readB, fram_writeB, fram_readH, fram_writeH, fram_readW, fram_writeW },
/* 1*/ DEF_AREA(0x0030000, iomem), //{ 0x0030000, iomem_read, iomem_write },
/* 2*/ DEF_NONE(0x0060000), //{ 0x0060000, NULL, NULL },
/* 3*/ DEF_NONE(0x0080000), //{ 0x0080000, NULL, NULL },
/* 4*/ DEF_AREA(0x0100000, sram), //{ 0x0100000, sram_read, sram_write },
/* 5*/ DEF_NONE(0x0200000), //{ 0x0200000, NULL, NULL },
/* 6*/ DEF_NONE(0x0300000), //{ 0x0300000, NULL, NULL },
/* 7*/ DEF_AREA(0x0400000, usbc), //{ 0x0400000, usbc_read, usbc_write },
/* 8*/ DEF_NONE(0x0600000), //{ 0x0600000, NULL, NULL },
/* 9*/ DEF_NONE(0x0800000), //{ 0x0800000, NULL, NULL },
/*10*/ DEF_AREA(0x0c00000, flash), //{ 0x0c00000, flash_read, flash_write },
/*11*/ DEF_NONE(0x1000000), //{ 0x1000000, NULL, NULL },
/*12*/ DEF_NONE(0x1800000), //{ 0x1800000, NULL, NULL },
/*13*/ DEF_NONE(0x2000000), //{ 0x2000000, NULL, NULL },
/*14*/ DEF_NONE(0x3000000), //{ 0x3000000, NULL, NULL },
/*15*/ DEF_NONE(0x4000000), //{ 0x4000000, NULL, NULL },
/*16*/ DEF_NONE(0x6000000), //{ 0x6000000, NULL, NULL },
/*17*/ DEF_NONE(0x8000000), //{ 0x8000000, NULL, NULL },
/*18*/ DEF_NONE(0xc000000), //{ 0xc000000, NULL, NULL },
};
static AREA*
area_sel(PIEMU_CONTEXT* context, unsigned addr, int size, int mode/*0:内部RDWR/1:外部RD/2:外部WR*/)
{
int no, sz, wt;
// 警告対策
no = sz = wt = 0;
/* 整列チェック。 */
switch(size) {
case 1: break;
case 2: if(addr & 1) DIE("bcu: odd-aligned access"); break;
case 4: if(addr & 3) DIE("bcu: odd-aligned access"); break;
default: DIE("bcu: odd-aligned access");
}
/* エリア選択。 */
addr &= (1 << 28) - 1; /* S1C33のアドレス空間は28ビット */
// これはうまくジャンプテーブルになるからこれでいい
switch((addr >> 24) & 0x0f)
{
case 0x0:
switch((addr >> 20) & 0x0f)
{
case 0x00:
{
switch((addr >> 16) & 0x0f)
{
case 0x00:
case 0x01:
case 0x02: no = 0; goto NO_WAIT;
case 0x03:
case 0x04:
case 0x05: no = 1; goto NO_WAIT;
case 0x06:
case 0x07: no = 2; goto NO_WAIT;
case 0x08:
case 0x09:
case 0x0a:
case 0x0b:
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f: no = 3; goto NO_WAIT;
}
DIE();
}
case 0x01: no = 4; sz = bA6_4_A5SZ; wt = bA6_4_A5WT; goto WAIT;
case 0x02: no = 5; sz = bA6_4_A5SZ; wt = bA6_4_A5WT; goto WAIT;
case 0x03: no = 6; sz = addr < 0x0380000; wt = bA6_4_A6WT; goto WAIT;
case 0x04:
case 0x05: no = 7; sz = bA8_7_A8SZ; wt = bA8_7_A8WT; goto WAIT;
case 0x06:
case 0x07: no = 8; sz = bA8_7_A8SZ; wt = bA8_7_A8WT; goto WAIT;
case 0x08:
case 0x09:
case 0x0a:
case 0x0b: no = 9; sz = bA10_9_A10SZ; wt = bA10_9_A10WT; goto WAIT;
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f: no = 10; sz = bA10_9_A10SZ; wt = bA10_9_A10WT; goto WAIT;
}
DIE();
case 0x1:
{
switch((addr >> 20) & 0x0f)
{
case 0x0:
case 0x1:
case 0x2:
case 0x3:
case 0x4:
case 0x5:
case 0x6:
case 0x7: no = 11; sz = bA12_11_A12SZ; wt = bA12_11_A12WT; goto WAIT;
case 0x8:
case 0x9:
case 0xa:
case 0xb:
case 0xc:
case 0xd:
case 0xe:
case 0xf: no = 12; sz = bA12_11_A12SZ; wt = bA12_11_A12WT; goto WAIT;
}
DIE();
}
case 0x2: no = 13; sz = bA14_13_A14SZ; wt = bA14_13_A14WT; goto WAIT;
case 0x3: no = 14; sz = bA14_13_A14SZ; wt = bA14_13_A14WT; goto WAIT;
case 0x4:
case 0x5: no = 15; sz = bA18_15_A16SZ; wt = bA18_15_A16WT; goto WAIT;
case 0x6:
case 0x7: no = 16; sz = bA18_15_A16SZ; wt = bA18_15_A16WT; goto WAIT;
case 0x8:
case 0x9:
case 0xa:
case 0xb: no = 17; sz = bA18_15_A18SZ; wt = bA18_15_A18WT; goto WAIT;
case 0xc:
case 0xd:
case 0xe:
case 0xf: no = 18; sz = bA18_15_A18SZ; wt = bA18_15_A18WT; goto WAIT;
}
DIE();
/* ウェイト加算。 */
WAIT:
if(!mode) goto NO_WAIT;
if(sz) { /* 8ビットデバイス */
CLK += wt * size;
// switch(size) {
// case 1: CLK += wt * 1; break;
// case 2: CLK += wt * 2; break;
// case 4: CLK += wt * 4; break;
// default: DIE();
// }
} else { /* 16ビットデバイス */
CLK += wt << (size >> 2);
// switch(size) {
// case 1: CLK += wt * 1; break;
// case 2: CLK += wt * 1; break;
// case 4: CLK += wt * 2; break;
// default: DIE();
// }
}
/* * 内蔵RAM以外へのアクセスは、その領域のウェイトサイクルに加えて、さらに1サイクル必要です。
* (S1C33000コアCPUマニュアル 33000Core-J.pdf p.34「3.2.2 命令の実行サイクル数 (2)」参照)
* * 2003/04/26追記
* S1C33000コアCPUマニュアル 33000Core-J.pdf B-II-4-20「外部システムインターフェイスのバスサイクル」を見ると、
* 外部メモリのREADは前述の通り+1サイクルですが、WRITEは+2サイクルみたいなので、そのように修正しました。
*/
CLK += mode;
NO_WAIT:
return &area_tbl[no];
}
/****************************************************************************
* グローバル関数
****************************************************************************/
void
bcu_init(PIEMU_CONTEXT* context)
{
/* TODO */
}
#define READ(ctx, addr, size, wait) \
{ \
AREA* area = area_sel(context, addr, size, wait); \
if(!area->read) return -1; \
return area->read(context, addr - area->addr, size); \
}
#define WRITE(ctx, addr, data, size, wait) \
{ \
AREA* area = area_sel(context, addr, size, wait); \
if(!area->write) return; \
area->write(context, addr - area->addr, data, size); \
}
#define READ_S(type, ctx, addr, size, wait) \
{ \
AREA* area = area_sel(context, addr, size, wait); \
if(!area->read##type) READ(ctx, addr, size, wait) \
return area->read##type(context, addr - area->addr); \
}
#define WRITE_S(type, ctx, addr, data, size, wait) \
{ \
AREA* area = area_sel(context, addr, size, wait); \
if(!area->write##type) WRITE(ctx, addr, data, size, wait) \
area->write##type(context, addr - area->addr, data); \
}
#define DEFUN_READ_S(type, size) \
int mem_read##type(PIEMU_CONTEXT* context, unsigned addr) \
{ \
READ_S(type, context, addr, size, 1) \
}
#define DEFUN_READ_S_NW(type, size) \
int mem_read##type##_nowait(PIEMU_CONTEXT* context, unsigned addr) \
{ \
READ_S(type, context, addr, size, 0) \
}
#define DEFUN_WRITE_S(type, size) \
void mem_write##type(PIEMU_CONTEXT* context, unsigned addr, int data) \
{ \
WRITE_S(type, context, addr, data, size, 2) \
}
#define DEFUN_WRITE_S_NW(type, size) \
void mem_write##type##_nowait(PIEMU_CONTEXT* context, unsigned addr, int data) \
{ \
WRITE_S(type, context, addr, data, size, 0) \
}
#define DEFUN_S(type, size) \
DEFUN_READ_S(type, size) \
DEFUN_READ_S_NW(type, size) \
DEFUN_WRITE_S(type, size) \
DEFUN_WRITE_S_NW(type, size)
DEFUN_S(B, 1);
DEFUN_S(H, 2);
DEFUN_S(W, 4);
int
mem_read(PIEMU_CONTEXT* context, unsigned addr, int size)
{
READ(context, addr, size, 1);
}
void
mem_write(PIEMU_CONTEXT* context, unsigned addr, int data, int size)
{
WRITE(context, addr, data, size, 2);
}
int
mem_read_nowait(PIEMU_CONTEXT* context, unsigned addr, int size)
{
READ(context, addr, size, 0);
}
void
mem_write_nowait(PIEMU_CONTEXT* context, unsigned addr, int data, int size)
{
WRITE(context, addr, data, size, 0);
}