-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbgmem.cpp
333 lines (303 loc) · 9.15 KB
/
dbgmem.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
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
#include "std.h"
#include "emul.h"
#include "vars.h"
#include "debug.h"
#include "dbgpaint.h"
#include "dbgcmd.h"
#include "dbgmem.h"
#include "wd93crc.h"
#include "util.h"
TRKCACHE edited_track;
unsigned sector_offset, sector;
void findsector(unsigned addr)
{
for (sector_offset = sector = 0; sector < edited_track.s; sector++, sector_offset += edited_track.hdr[sector].datlen)
if (addr >= sector_offset && addr < sector_offset + edited_track.hdr[sector].datlen)
return;
errexit("internal diskeditor error");
}
unsigned char *editam(unsigned addr)
{
Z80 &cpu = CpuMgr.Cpu();
if (editor == ED_CMOS) return &cmos[addr & (sizeof(cmos)-1)];
if (editor == ED_NVRAM) return &nvram[addr & (sizeof(nvram)-1)];
if (editor == ED_MEM) return cpu.DirectMem(addr);
if (!edited_track.trkd) return 0;
if (editor == ED_PHYS) return edited_track.trkd + addr;
// editor == ED_LOG
findsector(addr); return edited_track.hdr[sector].data + addr - sector_offset;
}
void editwm(unsigned addr, unsigned char byte)
{
if (editrm(addr) == byte) return;
unsigned char *ptr = editam(addr);
if (!ptr) return; *ptr = byte;
if (editor == ED_MEM || editor == ED_CMOS || editor == ED_NVRAM) return;
if (editor == ED_PHYS) { comp.wd.fdd[mem_disk].optype |= 2; return; }
comp.wd.fdd[mem_disk].optype |= 1;
// recalc sector checksum
findsector(addr);
*(unsigned short*)(edited_track.hdr[sector].data + edited_track.hdr[sector].datlen) =
wd93_crc(edited_track.hdr[sector].data - 1, edited_track.hdr[sector].datlen + 1);
}
unsigned memadr(unsigned addr)
{
if (editor == ED_MEM) return (addr & 0xFFFF);
if (editor == ED_CMOS) return (addr & (sizeof(cmos)-1));
if (editor == ED_NVRAM) return (addr & (sizeof(nvram)-1));
// else if (editor == ED_PHYS || editor == ED_LOG)
if (!mem_max) return 0;
while ((int)addr < 0) addr += mem_max;
while (addr >= mem_max) addr -= mem_max;
return addr;
}
void showmem()
{
Z80 &cpu = CpuMgr.Cpu();
char line[80]; unsigned ii;
unsigned cursor_found = 0;
if (mem_dump) mem_ascii = 1, mem_sz = 32; else mem_sz = 8;
if (editor == ED_LOG || editor == ED_PHYS)
{
edited_track.clear();
edited_track.seek(comp.wd.fdd+mem_disk, mem_track/2, mem_track & 1, (editor == ED_LOG)? LOAD_SECTORS : JUST_SEEK);
if (!edited_track.trkd) { // no track
for (ii = 0; ii < mem_size; ii++) {
sprintf(line, (ii == mem_size/2)?
" track not found ":
" ");
tprint(mem_x, mem_y+ii, line, (activedbg == WNDMEM) ? W_SEL : W_NORM);
}
mem_max = 0;
goto title;
}
mem_max = edited_track.trklen;
if (editor == ED_LOG)
for (mem_max = ii = 0; ii < edited_track.s; ii++)
mem_max += edited_track.hdr[ii].datlen;
}
else if(editor == ED_MEM)
mem_max = 0x10000;
else if(editor == ED_CMOS)
mem_max = sizeof(cmos);
else if(editor == ED_NVRAM)
mem_max = sizeof(nvram);
redraw:
cpu.mem_curs = memadr(cpu.mem_curs);
cpu.mem_top = memadr(cpu.mem_top);
for (ii = 0; ii < mem_size; ii++)
{
unsigned ptr = memadr(cpu.mem_top + ii*mem_sz);
sprintf(line, "%04X ", ptr);
unsigned cx = 0;
if (mem_dump)
{ // 0000 0123456789ABCDEF0123456789ABCDEF
for (unsigned dx = 0; dx < 32; dx++)
{
if (ptr == cpu.mem_curs) cx = dx+5;
unsigned char c = editrm(ptr); ptr = memadr(ptr+1);
line[dx+5] = c ? c : '.';
}
}
else
{ // 0000 11 22 33 44 55 66 77 88 abcdefgh
for (unsigned dx = 0; dx < 8; dx++)
{
if (ptr == cpu.mem_curs) cx = (mem_ascii) ? dx+29 : dx*3 + 5 + cpu.mem_second;
unsigned char c = editrm(ptr); ptr = memadr(ptr+1);
sprintf(line+5+3*dx, "%02X", c); line[7+3*dx] = ' ';
line[29+dx] = c ? c : '.';
}
}
line[37] = 0;
tprint(mem_x, mem_y+ii, line, (activedbg == WNDMEM) ? W_SEL : W_NORM);
cursor_found |= cx;
if (cx && (activedbg == WNDMEM))
txtscr[(mem_y+ii)*80+mem_x+cx+80*30] = W_CURS;
}
if (!cursor_found) { cursor_found=1; cpu.mem_top=cpu.mem_curs & ~(mem_sz-1); goto redraw; }
title:
const char *MemName = 0;
if (editor == ED_MEM)
MemName = "memory";
else if (editor == ED_CMOS)
MemName = "cmos";
else if (editor == ED_NVRAM)
MemName = "nvram";
if(editor == ED_MEM || editor == ED_CMOS || editor == ED_NVRAM)
{
sprintf(line, "%s: %04X gsdma: %06X", MemName, cpu.mem_curs & 0xFFFF, temp.gsdmaaddr);
}
else if (editor == ED_PHYS)
sprintf(line, "disk %c, trk %02X, offs %04X", mem_disk+'A', mem_track, cpu.mem_curs);
else
{ // ED_LOG
if (mem_max)
findsector(cpu.mem_curs);
sprintf(line, "disk %c, trk %02X, sec %02X[%02X], offs %04X",
mem_disk+'A', mem_track, sector, edited_track.hdr[sector].n,
cpu.mem_curs-sector_offset);
}
tprint(mem_x, mem_y-1, line, W_TITLE);
frame(mem_x,mem_y,37,mem_size,FRAME);
}
/* ------------------------------------------------------------- */
void mstl()
{
Z80 &cpu = CpuMgr.Cpu();
if (mem_max) cpu.mem_curs &= ~(mem_sz-1), cpu.mem_second = 0;
}
void mendl()
{
Z80 &cpu = CpuMgr.Cpu();
if (mem_max) cpu.mem_curs |= (mem_sz-1), cpu.mem_second = 1;
}
void mup()
{
Z80 &cpu = CpuMgr.Cpu();
if (mem_max) cpu.mem_curs -= mem_sz;
}
void mpgdn()
{
Z80 &cpu = CpuMgr.Cpu();
if (mem_max) cpu.mem_curs += mem_size*mem_sz, cpu.mem_top += mem_size*mem_sz;
}
void mpgup()
{
Z80 &cpu = CpuMgr.Cpu();
if (mem_max) cpu.mem_curs -= mem_size*mem_sz, cpu.mem_top -= mem_size*mem_sz;
}
void mdown()
{
if (!mem_max) return;
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs += mem_sz;
if (((cpu.mem_curs - cpu.mem_top + mem_max) % mem_max) / mem_sz >= mem_size) cpu.mem_top += mem_sz;
}
void mleft()
{
if (!mem_max) return;
Z80 &cpu = CpuMgr.Cpu();
if (mem_ascii || !cpu.mem_second) cpu.mem_curs--;
if (!mem_ascii) cpu.mem_second ^= 1;
}
void mright()
{
Z80 &cpu = CpuMgr.Cpu();
if (!mem_max) return;
if (mem_ascii || cpu.mem_second) cpu.mem_curs++;
if (!mem_ascii) cpu.mem_second ^= 1;
if (((cpu.mem_curs - cpu.mem_top + mem_max) % mem_max) / mem_sz >= mem_size) cpu.mem_top += mem_sz;
}
char dispatch_mem()
{
Z80 &cpu = CpuMgr.Cpu();
if (!mem_max)
return 0;
if (mem_ascii)
{
u8 Kbd[256];
GetKeyboardState(Kbd);
unsigned short k;
if (ToAscii(input.lastkey,0,Kbd,&k,0) != 1)
return 0;
k &= 0xFF;
if (k < 0x20 || k >= 0x80)
return 0;
editwm(cpu.mem_curs, (unsigned char)k);
mright();
return 1;
}
else
{
if ((input.lastkey >= '0' && input.lastkey <= '9') || (input.lastkey >= 'A' && input.lastkey <= 'F'))
{
unsigned char k = (input.lastkey >= 'A') ? input.lastkey-'A'+10 : input.lastkey-'0';
unsigned char c = editrm(cpu.mem_curs);
if (cpu.mem_second) editwm(cpu.mem_curs, (c & 0xF0) | k);
else editwm(cpu.mem_curs, (c & 0x0F) | (k << 4));
mright();
return 1;
}
}
return 0;
}
void mtext()
{
Z80 &cpu = CpuMgr.Cpu();
unsigned rs = find1dlg(cpu.mem_curs);
if (rs != -1) cpu.mem_curs = rs;
}
void mcode()
{
Z80 &cpu = CpuMgr.Cpu();
unsigned rs = find2dlg(cpu.mem_curs);
if (rs != -1) cpu.mem_curs = rs;
}
void mgoto()
{
Z80 &cpu = CpuMgr.Cpu();
unsigned v = input4(mem_x, mem_y, cpu.mem_top);
if (v != -1) cpu.mem_top = (v & ~(mem_sz-1)), cpu.mem_curs = v;
}
void mswitch() { mem_ascii ^= 1; }
void msp()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.sp;
}
void mpc()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.pc;
}
void mbc()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.bc;
}
void mde()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.de;
}
void mhl()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.hl;
}
void mix()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.ix;
}
void miy()
{
Z80 &cpu = CpuMgr.Cpu();
cpu.mem_curs = cpu.iy;
}
void mmodemem() { editor = ED_MEM; }
void mmodephys() { editor = ED_PHYS; }
void mmodelog() { editor = ED_LOG; }
void mdiskgo()
{
Z80 &cpu = CpuMgr.Cpu();
if (editor == ED_MEM) return;
for (;;) {
*(unsigned*)str = mem_disk + 'A';
if (!inputhex(mem_x+5, mem_y-1, 1, true)) return;
if (*str >= 'A' && *str <= 'D') break;
}
mem_disk = *str-'A'; showmem();
unsigned x = input2(mem_x+12, mem_y-1, mem_track);
if (x == -1) return;
mem_track = x;
if (editor == ED_PHYS) return;
showmem();
// enter sector
for (;;) {
findsector(cpu.mem_curs); x = input2(mem_x+20, mem_y-1, sector);
if (x == -1) return; if (x < edited_track.s) break;
}
for (cpu.mem_curs = 0; x; x--) cpu.mem_curs += edited_track.hdr[x-1].datlen;
}