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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
#include "dreamweb/dreamweb.h"
#include "engines/util.h"
#include "graphics/surface.h"
namespace DreamGen {
Common::String getFilename(Context &context) {
uint16 name_ptr = context.dx;
Common::String name;
uint8 c;
while((c = context.cs.byte(name_ptr++)) != 0)
name += (char)c;
return name;
}
void DreamGenContext::multiget() {
unsigned w = (uint8)cl, h = (uint8)ch;
unsigned x = (uint16)di, y = (uint16)bx;
unsigned src = x + y * kScreenwidth;
unsigned dst = (uint16)si;
es = ds;
ds = data.word(kWorkspace);
if (y + h > 200)
h = 200 - y;
if (x + w > 320)
w = 320 - x;
//debug(1, "multiget %u,%u %ux%u -> segment: %04x->%04x", x, y, w, h, (uint16)ds, (uint16)es);
for(unsigned l = 0; l < h; ++l) {
uint8 *src_p = ds.ptr(src + kScreenwidth * l, w);
uint8 *dst_p = es.ptr(dst + w * l, w);
memcpy(dst_p, src_p, w);
}
si += w * h;
di = src + kScreenwidth * h;
cx = 0;
}
void DreamGenContext::multiput() {
unsigned w = (uint8)cl, h = (uint8)ch;
unsigned x = (uint16)di, y = (uint16)bx;
unsigned src = (uint16)si;
unsigned dst = x + y * kScreenwidth;
es = data.word(kWorkspace);
if (y + h > 200)
h = 200 - y;
if (x + w > 320)
w = 320 - x;
//debug(1, "multiput %ux%u -> segment: %04x->%04x", w, h, (uint16)ds, (uint16)es);
for(unsigned l = 0; l < h; ++l) {
uint8 *src_p = ds.ptr(src + w * l, w);
uint8 *dst_p = es.ptr(dst + kScreenwidth * l, w);
memcpy(dst_p, src_p, w);
}
si += w * h;
di = dst + kScreenwidth * h;
cx = 0;
}
void DreamGenContext::multidump() {
ds = data.word(kWorkspace);
int w = (uint8)cl, h = (uint8)ch;
int x = (int16)di, y = (int16)bx;
unsigned offset = x + y * kScreenwidth;
//debug(1, "multidump %ux%u(segment: %04x) -> %d,%d(address: %d)", w, h, (uint16)ds, x, y, offset);
engine->blit(ds.ptr(offset, w * h), kScreenwidth, x, y, w, h);
si = di = offset + h * kScreenwidth;
cx = 0;
}
void DreamGenContext::worktoscreen() {
ds = data.word(kWorkspace);
uint size = 320 * 200;
engine->blit(ds.ptr(0, size), 320, 0, 0, 320, 200);
di = si = size;
cx = 0;
}
void DreamGenContext::printundermon() {
engine->printUnderMonitor();
}
void DreamGenContext::cls() {
engine->cls();
}
void DreamGenContext::frameoutnm() {
unsigned w = (uint8)cl, h = (uint8)ch;
unsigned pitch = (uint16)dx;
unsigned src = (uint16)si;
int x = (uint16)di, y = (uint16)bx;
unsigned dst = x + y * pitch;
//debug(1, "framenm %ux%u[pitch: %u]-> %d,%d, segment: %04x->%04x", w, h, pitch, x, y, (uint16)ds, (uint16)es);
for(unsigned l = 0; l < h; ++l) {
uint8 *src_p = ds.ptr(src + w * l, w);
uint8 *dst_p = es.ptr(dst + pitch * l, w);
memcpy(dst_p, src_p, w);
}
di += dst + pitch * h;
si += w * h;
cx = 0;
}
void DreamGenContext::seecommandtail() {
data.word(kSoundbaseadd) = 0x220;
data.byte(kSoundint) = 5;
data.byte(kSounddmachannel) = 1;
data.byte(kBrightness) = 1;
data.word(kHowmuchalloc) = 0x9360;
}
void DreamGenContext::randomnumber() {
al = engine->randomNumber();
}
void DreamGenContext::quickquit() {
engine->quit();
}
void DreamGenContext::quickquit2() {
engine->quit();
}
void DreamGenContext::keyboardread() {
::error("keyboardread"); //this keyboard int handler, must never be called
}
void DreamGenContext::resetkeyboard() {
}
void DreamGenContext::setkeyboardint() {
}
void DreamGenContext::readfromfile() {
uint16 dst_offset = dx;
uint16 size = cx;
debug(1, "readfromfile(%04x:%u, %u)", (uint16)ds, dst_offset, size);
ax = engine->readFromFile(ds.ptr(dst_offset, size), size);
flags._c = false;
}
void DreamGenContext::closefile() {
engine->closeFile();
data.byte(kHandle) = 0;
}
void DreamGenContext::openforsave() {
const char *name = (const char *)ds.ptr(dx, 13);
debug(1, "openforsave(%s)", name);
engine->openSaveFileForWriting(name);
}
void DreamGenContext::openfilenocheck() {
const char *name = (const char *)ds.ptr(dx, 13);
debug(1, "checksavefile(%s)", name);
bool ok = engine->openSaveFileForReading(name);
flags._c = !ok;
}
void DreamGenContext::openfilefromc() {
openfilenocheck();
}
void DreamGenContext::openfile() {
Common::String name = getFilename(*this);
debug(1, "opening file: %s", name.c_str());
engine->openFile(name);
cs.word(kHandle) = 1; //only one handle
flags._c = false;
}
void DreamGenContext::createfile() {
::error("createfile");
}
void DreamGenContext::dontloadseg() {
ax = es.word(di);
_add(di, 2);
dx = ax;
cx = 0;
unsigned pos = engine->skipBytes(dx);
dx = pos >> 16;
ax = pos & 0xffff;
flags._c = false;
}
void DreamGenContext::mousecall() {
engine->mouseCall();
}
void DreamGenContext::setmouse() {
data.word(kOldpointerx) = 0xffff;
}
void DreamGenContext::gettime() {
TimeDate t;
g_system->getTimeAndDate(t);
debug(1, "\tgettime: %02d:%02d:%02d", t.tm_hour, t.tm_min, t.tm_sec);
ch = t.tm_hour;
cl = t.tm_min;
dh = t.tm_sec;
data.byte(kSecondcount) = dh;
data.byte(kMinutecount) = cl;
data.byte(kHourcount) = ch;
}
void DreamGenContext::allocatemem() {
uint size = (bx + 2) * 16;
debug(1, "allocate mem, %u bytes", size);
flags._c = false;
SegmentRef seg = allocateSegment(size);
ax = (uint16)seg;
debug(1, "\tsegment address -> %04x", (uint16)ax);
}
void DreamGenContext::deallocatemem() {
uint16 id = (uint16)es;
debug(1, "deallocating segment %04x", id);
deallocateSegment(id);
//fixing invalid entries in the sprite table
es = data;
uint tsize = 16 * 32;
uint16 bseg = data.word(kBuffers);
if (!bseg)
return;
SegmentRef buffers(this);
buffers = bseg;
uint8 *ptr = buffers.ptr(kSpritetable, tsize);
for(uint i = 0; i < tsize; i += 32) {
uint16 seg = READ_LE_UINT16(ptr + i + 6);
//debug(1, "sprite segment = %04x", seg);
if (seg == id)
memset(ptr + i, 0xff, 32);
}
}
void DreamGenContext::removeemm() {
::error("removeemm");
}
void DreamGenContext::setupemm() {
//good place for early initialization
switch(engine->getLanguage()) {
case Common::EN_ANY:
case Common::EN_GRB:
case Common::EN_USA:
return;
default:
data.byte(kForeignrelease) = 1;
}
}
void DreamGenContext::pitinterupt() {
::error("pitinterupt");
}
void DreamGenContext::getridofpit() {
::error("getridofpit");
}
void DreamGenContext::setuppit() {
::error("setuppit");
}
void DreamGenContext::startdmablock() {
::error("startdmablock");
}
void DreamGenContext::dmaend() {
::error("dmaend");
}
void DreamGenContext::restoreems() {
::error("restoreems");
}
void DreamGenContext::saveems() {
::error("saveems");
}
void DreamGenContext::bothchannels() {
::error("bothchannels");
}
void DreamGenContext::channel1only() {
::error("channel1only");
}
void DreamGenContext::channel0only() {
::error("channel0only");
}
void DreamGenContext::out22c() {
::error("out22c");
}
void DreamGenContext::soundstartup() {}
void DreamGenContext::soundend() {}
void DreamGenContext::interupttest() {}
void DreamGenContext::disablesoundint() {}
void DreamGenContext::enablesoundint() {}
void DreamGenContext::checksoundint() {
data.byte(kTestresult) = 1;
}
void DreamGenContext::setsoundoff() {
warning("setsoundoff: STUB");
}
void DreamGenContext::loadsample() {
engine->loadSounds(0, (const char *)data.ptr(dx, 13));
}
void DreamGenContext::loadsecondsample() {
uint8 ch0 = data.byte(kCh0playing);
if (ch0 >= 12 && ch0 != 255)
cancelch0();
uint8 ch1 = data.byte(kCh1playing);
if (ch1 >= 12)
cancelch1();
engine->loadSounds(1, (const char *)data.ptr(dx, 13));
}
void DreamGenContext::loadspeech() {
cancelch1();
data.byte(kSpeechloaded) = 0;
createname();
const char *name = (const char *)data.ptr(di, 13);
//warning("name = %s", name);
if (engine->loadSpeech(name))
data.byte(kSpeechloaded) = 1;
}
void DreamGenContext::saveseg() {
cx = es.word(di);
_add(di, 2);
savefilewrite();
}
void DreamGenContext::savefilewrite() {
ax = engine->writeToSaveFile(ds.ptr(dx, cx), cx);
}
void DreamGenContext::savefileread() {
ax = engine->readFromSaveFile(ds.ptr(dx, cx), cx);
}
void DreamGenContext::loadseg() {
ax = es.word(di);
di += 2;
uint16 dst_offset = dx;
uint16 size = ax;
debug(1, "loadseg(%04x:%u, %u)", (uint16)ds, dst_offset, size);
ax = engine->readFromFile(ds.ptr(dst_offset, size), size);
flags._c = false;
}
void DreamGenContext::error() {
::error("error");
}
void DreamGenContext::generalerror() {
::error("generalerror");
}
void DreamGenContext::dosreturn() {
_cmp(data.byte(kCommandtype), 250);
if (!flags.z()) {
data.byte(kCommandtype) = 250;
al = 46;
commandonly();
}
ax = data.word(kMousebutton);
_and(ax, 1);
if (flags.z())
return;
data.word(kMousebutton) = 0;
engine->quit();
}
void DreamGenContext::set16colpalette() {
//fixme: this is a bit hackish, set16colpalette called after initialization and nearly before main loop.
engine->enableSavingOrLoading();
}
void DreamGenContext::mode640x480() {
// Video mode 12h: 640x480 pixels, 16 colors, I believe
al = 0x12 + 128;
ah = 0;
initGraphics(640, 480, true);
}
void DreamGenContext::showgroup() {
engine->setPalette();
}
void DreamGenContext::fadedos() {
engine->fadeDos();
}
void DreamGenContext::doshake() {
uint8 &counter = data.byte(kShakecounter);
_cmp(counter, 48);
if (flags.z())
return;
_add(counter, 1);
static const int shakeTable[] = {
0, -2, 3, -2, 0, 2, 4, -1,
1, -3, 3, 2, 0, -2, 3, -2,
0, 2, 4, -1, 1, -3, 3, 2,
0, -2, 3, -2, 0, 2, 4, -1,
1, -3, 3, 2, 0, -2, 3, -2,
0, 2, 4, -1, 1, -3, 3, 2,
0, -2, 3, -2, 0, 2, 4, -1,
1, -3, 3, 2, 0, -2, 3, -2,
0, 2, 4, -1, 1, -3, 3, 2,
0, -2, 3, -2, 0, 2, 4, -1,
1, -3, 3, 2, 0, -2, 3, -2,
0, 2, 4, -1, 1, -3, 3, 2,
0, -2, 3, -2, 0, 2, 4, -1,
1, -3, 3, 0,
};
int offset = shakeTable[counter];
engine->setShakePos(offset >= 0? offset: -offset);
}
void DreamGenContext::vsync() {
push(ax);
push(bx);
push(cx);
push(dx);
push(si);
push(di);
push(es);
push(ds);
engine->waitForVSync();
ds = pop();
es = pop();
di = pop();
si = pop();
dx = pop();
cx = pop();
bx = pop();
ax = pop();
}
void DreamGenContext::setmode() {
vsync();
initGraphics(320, 200, false);
}
void DreamGenContext::showpcx() {
Common::String name = getFilename(*this);
Common::File pcxFile;
if (!pcxFile.open(name)) {
warning("showpcx: Could not open '%s'", name.c_str());
return;
}
uint8 *maingamepal;
int i, j;
// Read the 16-color palette into the 'maingamepal' buffer. Note that
// the color components have to be adjusted from 8 to 6 bits.
pcxFile.seek(16, SEEK_SET);
es = data.word(kBuffers);
maingamepal = es.ptr(kMaingamepal, 768);
pcxFile.read(maingamepal, 48);
memset(maingamepal + 48, 0xff, 720);
for (i = 0; i < 48; i++) {
maingamepal[i] >>= 2;
}
// Decode the image data.
Graphics::Surface *s = g_system->lockScreen();
Common::Rect rect(640, 480);
s->fillRect(rect, 0);
pcxFile.seek(128, SEEK_SET);
for (int y = 0; y < 480; y++) {
byte *dst = (byte *)s->getBasePtr(0, y);
int decoded = 0;
while (decoded < 320) {
byte col = pcxFile.readByte();
byte len;
if ((col & 0xc0) == 0xc0) {
len = col & 0x3f;
col = pcxFile.readByte();
} else {
len = 1;
}
// The image uses 16 colors and is stored as four bit
// planes, one for each bit of the color, least
// significant bit plane first.
for (i = 0; i < len; i++) {
int plane = decoded / 80;
int pos = decoded % 80;
for (j = 0; j < 8; j++) {
byte bit = (col >> (7 - j)) & 1;
dst[8 * pos + j] |= (bit << plane);
}
decoded++;
}
}
}
g_system->unlockScreen();
pcxFile.close();
}
void DreamGenContext::frameoutv() {
uint16 pitch = dx;
uint16 width = cx & 0xff;
uint16 height = cx >> 8;
uint16 stride = pitch - width;
const uint8* src = ds.ptr(si, width * height);
uint8* base = es.ptr(di, stride * height);
uint8* dst = base + pitch * bx;
// NB: Original code assumes non-zero width and height, "for" are unneeded, do-while would suffice but would be less readable
for (uint16 y = 0; y < height; ++y) {
for (uint16 x = 0; x < width; ++x) {
uint8 pixel = *src++;
if (pixel)
*dst = pixel;
++dst;
}
dst += stride;
}
}
void DreamGenContext::modifychar() {
al = engine->modifyChar(al);
}
void DreamGenContext::lockmon() {
// Pressing space pauses text output in the monitor. We use the "hard"
// key because calling readkey() drains characters from the input
// buffer, we we want the user to be able to type ahead while the text
// is being printed.
if (data.byte(kLasthardkey) == 57) {
// Clear the keyboard buffer. Otherwise the space that caused
// the pause will be read immediately in the pause loop.
do {
readkey();
} while (data.byte(kCurrentkey) != 0);
locklighton();
while (!engine->shouldQuit()) {
engine->waitForVSync();
readkey();
if (data.byte(kCurrentkey) == ' ')
break;
}
// Forget the last "hard" key, otherwise the space that caused
// the unpausing will immediately re-pause the game.
data.byte(kLasthardkey) = 0;
locklightoff();
}
}
} /*namespace dreamgen */
|