-
Notifications
You must be signed in to change notification settings - Fork 0
/
dam.cpp
272 lines (246 loc) · 5.41 KB
/
dam.cpp
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
// Doze - Dave's optimized Z80 emulator
// Code Maker
#include "dam.h"
static char *OutName="dozea.asm";
static FILE *Out=NULL;
char DamPc[]="si";
char DamCycles[]="dword [_nDozeCycles]";
int ot(const char *Format,...)
{
va_list Arg; va_start(Arg,Format);
if (Out!=NULL) vfprintf(Out,Format,Arg);
va_end(Arg);
return 0;
}
void DamAlign() { ot("times ($$-$) & 3 db 0\n\n"); }
static int DamVariables()
{
DamAlign();
ot("; Z80 Registers\n");
ot("global _Doze\n");
ot("_Doze:\n");
ot("DozeAF dw 0\n");
ot("DozeBC dw 0\n");
ot("DozeDE dw 0\n");
ot("DozeHL dw 0\n");
ot("DozeIX dw 0\n");
ot("DozeIY dw 0\n");
ot("DozePC dw 0\n");
ot("DozeSP dw 0\n");
ot("DozeAF2 dw 0\n");
ot("DozeBC2 dw 0\n");
ot("DozeDE2 dw 0\n");
ot("DozeHL2 dw 0\n");
ot("DozeIR dw 0\n");
ot("DozeIFF dw 0\n");
ot("DozeIM db 0\n");
ot("\n");
DamAlign();
ot("global _nDozeCycles\n");
ot("_nDozeCycles: dd 0 ; Cycles left (in T-states)\n");
ot("global _nDozeEi\n");
ot("_nDozeEi: dd 0 ; 1 = assembler quit on EI, 2 = assembler did quit on EI\n");
ot("SaveReg: times 6 dd 0\n");
ot("Tmp16: dw 0\n");
ot("TmpFlag: db 0\n");
DamAlign();
ot("global _DozeMemFetch\n");
ot("_DozeMemFetch: times 0x100 " DPTR " 0\n");
ot("global _DozeMemRead\n");
ot("_DozeMemRead: times 0x100 " DPTR " 0\n");
ot("global _DozeMemWrite\n");
ot("_DozeMemWrite: times 0x100 " DPTR " 0\n");
return 0;
}
int DamOpStart(unsigned int op)
{
ot(";****************************************************************\n");
DamAlign();
ot("Op%-6.2X: INC_R\n\n",op);
return 0;
}
int DamOpDone(int nCycles,int bDontEnd)
{
ot("\n sub %s,%d\n",DamCycles,nCycles);
if (bDontEnd==0)
{
ot(" jle near DozeRunEnd\n");
ot(" FETCH_OP\n\n");
}
return 0;
}
static int DamCall()
{
DamAlign();
ot(
"extern _DozeRead\n"
"Read:\n"
" REG_TO_DOZE\n"
" " RPUSH "dx\n " RPUSH "di\n"
#ifndef __x86_64
" " RPUSH "di\n"
#endif
" call _DozeRead\n"
#ifndef __x86_64
" add esp," PTRSIZEB "\n"
#endif
" " RPOP "di\n " RPOP "dx\n\n"
" mov dl,al\n"
" DOZE_TO_REG\n"
" ret\n\n"
);
DamAlign();
ot(
"extern _DozeWrite\n"
"Write:\n"
" REG_TO_DOZE\n"
" " RPUSH "dx\n " RPUSH "di\n"
#ifdef __x86_64
" mov rsi,rdx\n"
#else
" " RPUSH "dx\n"
" " RPUSH "di\n"
#endif
" call _DozeWrite\n"
#ifndef __x86_64
" add esp," PTRSIZEB "*2\n"
#endif
" " RPOP "di\n " RPOP "dx\n\n"
" DOZE_TO_REG\n"
" ret\n\n"
);
DamAlign();
ot(
"extern _DozeIn\n"
"PortIn:\n"
" REG_TO_DOZE\n"
" " RPUSH "dx\n " RPUSH "di\n"
#ifndef __x86_64
" " RPUSH "di\n"
#endif
" call _DozeIn\n"
#ifndef __x86_64
" add esp," PTRSIZEB "\n"
#endif
" " RPOP "di\n " RPOP "dx\n\n"
" mov dl,al\n"
" DOZE_TO_REG\n"
" ret\n\n"
);
DamAlign();
ot(
"extern _DozeOut\n"
"PortOut:\n"
" REG_TO_DOZE\n"
" " RPUSH "dx\n " RPUSH "di\n"
#ifdef __x86_64
" mov rsi,rdx\n"
#else
" " RPUSH "dx\n"
" " RPUSH "di\n"
#endif
" call _DozeOut\n"
#ifndef __x86_64
" add esp," PTRSIZEB "*2\n"
#endif
" " RPOP "di\n " RPOP "dx\n\n"
" DOZE_TO_REG\n"
" ret\n\n"
);
DamAlign();
ot(
"; Call a routine\n"
"global _DozeAsmCall\n"
"_DozeAsmCall:\n"
#ifdef __x86_64
" mov ax,di\n"
#else
" mov ax,word[esp+" PTRSIZEB "] ; Get address\n"
#endif
" mov word[Tmp16],ax\n"
" SAVE_REGS\n"
" REG_BLANK\n"
" DOZE_TO_REG\n"
" INC_R\n"
" sub word [DozeSP],2\n"
" mov dx,si\n"
" mov di,word [DozeSP]\n"
" DAM_WRITE16\n"
" mov dx,word[Tmp16]\n"
" mov si,dx\n"
" REG_TO_DOZE\n"
" RESTORE_REGS\n"
" ret\n\n"
);
DamAlign();
ot(
"; Read a byte from memory\n"
"global _DozeAsmRead\n"
"_DozeAsmRead:\n"
#ifdef __x86_64
" mov ax,di\n"
#else
" mov ax,word[esp+" PTRSIZEB "] ; Get address\n"
#endif
" mov word[Tmp16],ax\n"
" SAVE_REGS\n"
" REG_BLANK\n"
" DOZE_TO_REG\n"
" mov di,word [Tmp16]\n"
" DAM_READ8\n"
" REG_TO_DOZE\n"
" xor eax,eax\n"
" mov al,dl\n"
" RESTORE_REGS\n"
" ret\n\n"
);
return 0;
}
static int DamMain()
{
int i=0;
ot("; Doze - Dave's Z80 Emulator - Assembler output\n\n");
#ifdef __x86_64
ot("bits 64\n\n");
#else
ot("bits 32\n\n");
#endif
ot("section .data\n\n");
DamVariables();
ot("section .text\n\n");
DamMacros(); // make macros
DamCall();
DamAlign();
ot("global _DozeAsmRun\n");
ot("_DozeAsmRun:\n");
ot("\n");
ot(" SAVE_REGS\n");
ot(" REG_BLANK\n");
ot(" DOZE_TO_REG\n");
ot(" FETCH_OP ; Fetch first opcode\n\n");
ot("DozeRunEnd: ; After cycles have run out, we come back here\n\n");
ot(" REG_TO_DOZE\n");
ot(" RESTORE_REGS\n");
ot(" ret\n\n\n");
memset(DamJump ,0,sizeof(DamJump));
// Fill in jump table if the instruction is available
for (i=0x00;i<0x100;i++) DamJump[ i]=DamaOp ( i);
for (i=0x00;i<0x100;i++) DamJump[0x100+i]=DamcOpCb( 0xCB00+i);
for (i=0x00;i<0x100;i++) DamJump[0x200+i]=DameOpEd( 0xED00+i);
for (i=0x00;i<0x100;i++) DamJump[0x300+i]=DamaOp ( 0xDD00+i);
for (i=0x00;i<0x100;i++) DamJump[0x400+i]=DamaOp ( 0xFD00+i);
// Note that DDCB__xx is represented as DDCBxx here
for (i=0x00;i<0x100;i++) DamJump[0x500+i]=DamcOpCb(0xDDCB00+i);
for (i=0x00;i<0x100;i++) DamJump[0x600+i]=DamcOpCb(0xFDCB00+i);
DamJumpTab(); // Jump table
DamTables(); // Other tables
return 0;
}
int main()
{
Out=fopen(OutName,"wt");
if (Out==NULL) return 1;
DamMain();
fclose(Out); Out=NULL;
return 0;
}