-
Notifications
You must be signed in to change notification settings - Fork 22
/
mario.cc
473 lines (428 loc) · 15.8 KB
/
mario.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
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
/* Ad-hoc programming editor for DOSBox -- (C) 2011-03-08 Joel Yliluoma */
#include "langdefs.hh"
#include "vga.hh"
#include "chartype.hh"
#ifdef __DJGPP__
# include <dos.h>
# include <dpmi.h>
# include <go32.h>
#elif defined(__BORLANDC__)
# include <dos.h>
#else
# include <thread>
# include <chrono>
#endif
//#include <string.h>
volatile unsigned long MarioTimer = 0;
static const char Mario[] =
//123456789ABCDEF 0123456789ABCDEF
// 0123456789ABCDEF
/*
" ##### ##### "
" ######### ##### ######### "
" %%%''%' ######### %%%''%' "
" %'%'''%''' %%%''%' %'%'''%''' "
" %'%%'''%''' %'%'''%''' %'%%'''%''' "
" %%''''%%%% %'%%'''%'' %%''''%%%% "
" ''''''' %%''''%%%% ''''''' "
" %%#%%% ''''''' %%%%##%% "
" %%%%##%% %%%%#% ' ''%%%%###%%%'''"
" %%%##'##' '%%%%%%''' ''' %%#'###%%''"
" %%%%##### ''#%%%%%'' '' ####### % "
" #%%'''### %%####### #########%% "
" #%''### %######## ##########%% "
" ###%%% %%### ### %%### ###%% "
" %%%%%%% % %%% %%% "
" %%%% %%%% %%% "*/
//123456789ABCDEF
// 0123456789ABCDEF
" ##### "
" ###### #'''''### "
" #''''''## #'''''''''# "
" #'''''''''# ###'.#.### "
" ###..#.### #..##.#....# "
" #..##.#....# #..##..#...# "
" #..##..#...# ##...##### "
" ##...##### ###.....# "
" ##.....# ##'''##''### "
" #''##''# #..''''##''#'# "
" #''''##''# #..'''######'.#"
" #''''##### #..####.##.#.#"
" #...##.## .#########''# "
" #..'''### #''######'''# "
" #'''''# #'''# #'''# "
" ##### ### ### ";
static const char Mario21[] =
" ##### "
" ##### ##'''Y'# "
" ##'''Y'# #''++YYY# "
" #''++YYY# #+'++###### "
" #+'++###### #+++######### "
" #+++######### #.###:#:#: "
" #.###:#:#: #.#.#:.#.#.## "
" #.#.#:.#.#.## #:#.##.......# "
" #:#.##.......# ##:.#..#:::::# "
" ##:.#..#:::::# ##::.####### "
" ##::.####### ##::::#### "
" ###:::#### #+#####% "
" #+####% #++'#,11% "
" #++',,11% ##...#..1.%## "
" ####,..1.% ###..#,..1.#Y##"
" #...#..1.% ###..#,,,1%####"
" #..#,,,11% ##%##,,,,%#### "
" #..#,,%,% ##Y# %%%% #### "
" ####### ## ## "
" ####Y#Y# "
" ######## ";
static const char Mario32[] =
//123456789ABCDEF0123456789ABCDEFGH0123456789ABCDEF0123456789ABCDEFGH0123456789ABCDEF0123456789ABCDEFGH""""""""
" "
" "
" "
" ## ## ## ## ## ## "
" #.###### #.###### #.###### "
" #..#,,,,# #..#,,,,# #..#,,,,# "
" #,..,,,,,# #,..,,,,,# #,..,,,,,# "
" #,,.,,,,,,,# #,,.,,,,,,,# #,,.,,,,,,,# "
" #,,,,,,,,,,# #,,,,,,,,,,# #,,,,,,,,,,# "
" #,,,,,,,,,,,# #,,,,,,,,,,,# #,,,,,,,,,,,# "
" #,,,,,,####,# #,,,,,,####,# #,,,,,,####,# "
" #,,,,,#..#.# #,,,,,#..#.# #,,,,,#..#.# "
" #,,,,,...#.### #,,,,,...#.### #,,,,,...#.### "
" #,,,,,........####### #,,,,,........####### #,,,,,........####### "
" #,,,,..#.....##....## #,,,,..#.....##....## #,,,,..#.....##....## "
" #,,,,.###...##.......# #,,,,.###...##.......# #,,,,.###...##.......# "
" ##,,...#####.######..## ##,,...#####.######..## ##,,...#####.######..## "
" ####....#..#,,,,,,#...# ####....#..#,,,,,,#...# ####....#..#,,,,,,#...#"
" #,#,,####.###,,,,,,###.. #,#,,####.###,,,,,,###.. #,#,,####.###,,,,,,###.."
" ##,,,,,####,,#,,,,#,,### ##,,,,,####,,#,,,,#,,### ##,,,,,####,,#,,,,#,,###"
" #,#,,,,,,,##,,,####,,,,,# #,#,,,,,,,##,,,####,,,,,# #,#,,,,,,,##,,,####,,,,,#"
" #,,#,,,,,#..#,#,,,,#,,,## #,,#,,,,,#..#,#,,,,#,,,## #,,#,,,,,#..#,#,,,,#,,,##"
" ###,,,##,,#....#,,,,,,#,## ######,,,##,,#....#,,,,,,#,## #########,,,##,,#....#,,,,,,#,## "
" ##..#,,,,,###....#,,,,,,,#,# ###,,..#,,,,,###....#,,,,,,,#,# ##,..,,...#,,,,###....#,,,,,,,#,# "
" #.,..#,,,,,,,##..##,,,,,,#,# ##..,,..#,,,,,,,##..##,,,,,,#,# #,,..,,...#,,,,,,##..##,,,,,,#,# "
" #...,.#,,,,,,,..##,,#,,,,#,,# #,,..,,#,,,,,,,,..##,,#,,,,#,,# #,,..,,..#,,,,,,,,.##.##,,,,#,,# "
" #,,..,# #,,,,,,,..##,,####,,# #,,..,,#,,,,,,,,,..,#,,####,,# ##,..,,##..,,,,,,,,...#,####,,# "
" #,,..# #,,,,,,,,,# ##,,,,## ##,,###,,,,,,,#,,,,,.##,,,,## #######..,,,,,###,...##,,,,## "
" #,,,# #,,,,,,## ###### ### #.,,,,# #,,,..####### #..,,### #..######## "
" ##,,# #,,,,,# #..,,# #,..# #..## ### "
" ### #......# #....# #..# ## "
" ######## #### ## "
;
static unsigned OverlayMarioByte(
unsigned pose,
register unsigned y, register unsigned absx,
register unsigned char OriginalByte,
signed int MarioOffset)
{
const char* data = nullptr;
int max_x = 16;
unsigned NumMarioPoses = 2;
if(VidCellHeight <= 16)
{
data = (Mario + 0 + pose * unsigned(16+0) + y * unsigned(0*(NumMarioPoses+1)+16*NumMarioPoses));
}
else if(VidCellHeight <= 20)
{
data = (Mario21 + 0 + pose * unsigned(16+0) + y * unsigned(0*(NumMarioPoses+1)+16*NumMarioPoses));
}
else
{
NumMarioPoses = 3;
data = (Mario32 + 0 + pose * unsigned(34+0) + y * unsigned(0*(NumMarioPoses+1)+34*NumMarioPoses));
max_x = 34;
}
static const unsigned char dither4x4[16] =
{0,12,3,15, 8,4,11,7, 2,14,1,13, 10,6,9,5};
const unsigned char* dithline = dither4x4 + (((y+MarioTimer) & 3u) * 4u);
unsigned char byte = 0x00;
int xoffs = MarioOffset;
for(register unsigned x=0; x<8; ++x, ++xoffs)
{
unsigned char dithval = dithline[ (x+absx)&3 ];
register unsigned char bit = 1;
char c = (unsigned(xoffs) < unsigned(max_x)) ? (data[xoffs] / 3) : ('?' / 3);
switch(c)
{
case '.' / 3: // 15
bit = 0; // 0%
break;
case 'Y' / 3: // 29
bit = dithval < 2; // 12.5%
break;
case ':' / 3: // 19
bit = dithval < 4; // 25%
break;
case ',' / 3: // 14
case '\'' / 3: // 13
bit = dithval >> 3; // 50%
break;
case '%' / 3: // 12
bit = dithval >= 4; // 75%
break;
case '#' / 3: // 11
break; // 100%
default:
case '?' / 3: // 21
case ' ' / 3: // 10
// transparent, no change
byte |= OriginalByte & (0x80 >> x);
continue;
}
if(bit)
byte |= 1u << (7-x);
}
return byte;
}
void MarioTranslate(
const EditorCharType* model,
unsigned short* target,
unsigned width)
{
const unsigned room_left = 240;
const unsigned room_right = 8;
const unsigned room_wide = width * (FatMode ? 16 : 8);
const unsigned xspanlength = room_wide + room_left + room_right;
//const unsigned twospans = xspanlength * 2u;
unsigned long mt = MarioTimer / 2;
const unsigned poses = (VidCellHeight >= 29) ? 4u : 2u;
const unsigned MarioStepInterval = VidCellHeight >= 29 ? 5 : 7;
//if(VidCellHeight >= 29) mt *= 2;
unsigned marioframe = (mt / MarioStepInterval) % poses;
if(marioframe == 3) marioframe = 1;
/*
unsigned mariopos = timer % twospans;
int mariox = mariopos < xspanlength
? (int)mariopos - (int)room_left
: (int)room_wide + (int)room_right - (int)(mariopos - xspanlength);
*/
int mariox = (mt % xspanlength) - room_left;
unsigned char RevisedFontData[ 6 * 32 ];
#define M(n) MakeMarioColor(n)
static const EditorCharType chartables[6*4] =
{ M(0xC0), M(0xC3), M(0xC4), M(0xC8), M(0xC9), M(0xCD),
M(0xC0), M(0xC3), M(0xC4), M(0xC8), M(0xC9), M(0xCD),
M(0x80), M(0x83), M(0x84), M(0x88), M(0x89), M(0x8D),
M(0x80), M(0x83), M(0x84), M(0x88), M(0x89), M(0x8D) };
static const unsigned char offsets[6*2] =
{ 0,1,2,3,4,5, 0,1,2,3,4,5 };
unsigned short base = FatMode ? 0x80 : 0xC0;
// shuffle the tiles on each frame to ensure that VGA caching
// will detect changes and re-draw the scanlines.
unsigned shuffle = (MarioTimer) % 6u;
const EditorCharType* const chartable = chartables + (FatMode ? 12 : 0) + shuffle;
unsigned numchars = 0;
unsigned spritewide = 16;
if(VidCellHeight >= 29) spritewide = 34;
unsigned last_pos = 0;
for(int basex = mariox - (mariox % 8)
; basex < int(mariox + spritewide)
&& basex < int(room_wide)
; basex += 8)
{
if(basex < 0) continue;
int offset = basex - mariox;
unsigned pos = basex >> 3;
EditorCharType word = model[pos];
unsigned char ch = ExtractCharCode(word);
if(ch == 0xDC || (ch >= 0xB0 && ch <= 0xB2))
ch = 0x20;
const unsigned char* SourceFontPtr = VgaFont + (ch * VidCellHeight);
unsigned fontdatasize = offsets[shuffle+numchars]*VidCellHeight;
for(unsigned y=0; y<VidCellHeight; ++y)
{
RevisedFontData[fontdatasize++] =
OverlayMarioByte(marioframe,y,mariox, SourceFontPtr[y], offset);
}
while(last_pos < pos) VidmemPutEditorChar(model[last_pos++], target);
VidmemPutEditorChar(RecolorBgOnly(chartable[numchars++], word), target);
++last_pos;
}
{for(unsigned pos = room_wide/8; last_pos < pos; ) VidmemPutEditorChar(model[last_pos++], target);}
//memcpy(target, model, room_wide/4);
#ifdef __BORLANDC__
if(numchars > 0)
{
// Update VGA font:
_asm {
push es
push bp
push di
mov ax, 0x1100
mov bh, 0
mov bl, VidCellHeight
mov di, base
mov si, bx
xchg bl,bh
push ss
pop es
lea bp, offset RevisedFontData
lea dx, [di+0x0]; mov cx, 1; int 0x10
add bp, si
shl si, 1
lea dx, [di+0x3]; mov cx, 2; int 0x10
add bp, si
lea dx, [di+0x8]; mov cx, 2; int 0x10
add bp, si
lea dx, [di+0xD]; mov cx, 1; int 0x10
pop di
pop bp
pop es
}
}
#else
if(numchars > 0)
{
// Remap 00, 03-04, 08-09 and 0D
VgaEnableFontAccess();
VgaSetFont(VidCellHeight, 1, base+0x0, RevisedFontData+0);
VgaSetFont(VidCellHeight, 2, base+0x3, RevisedFontData+VidCellHeight);
VgaSetFont(VidCellHeight, 2, base+0x8, RevisedFontData+VidCellHeight*3);
VgaSetFont(VidCellHeight, 1, base+0xD, RevisedFontData+VidCellHeight*5);
VgaDisableFontAccess();
}
#endif
}
#ifdef __BORLANDC__
static unsigned short Clock=0, Counter=0;
static void (interrupt *OldI08)();
static void interrupt MarioI08()
{
MarioTimer += 1;
_asm {
mov ax, Counter
add Clock, ax
jnc P1
}
OldI08();
goto P2;
P1: _asm { mov al, 0x20; out 0x20, al }
P2:;
}
#elif defined(__DJGPP__)
static unsigned Clock=0, Counter=0;
static _go32_dpmi_seginfo OldI08_rm, NewI08_rm;
static _go32_dpmi_seginfo OldI08_pm, NewI08_pm;
static _go32_dpmi_registers I08regs;
static bool Count()
{
MarioTimer += 2;
Clock += Counter;
if(Clock & 0x10000)
{
Clock &= 0xFFFF;
return true;
}
return false;
}
static void MarioI08_rm()
{
if(Count())
{
I08regs.x.cs = OldI08_rm.rm_segment;
I08regs.x.ip = OldI08_rm.rm_offset;
I08regs.x.ss = I08regs.x.sp = 0;
//__dpmi_simulate_real_mode_procedure_iret(&I08regs);
_go32_dpmi_simulate_fcall_iret(&I08regs);
//outportb(0x20,0x20);
}
else
{
outportb(0x20,0x20);
}
}
static void MarioI08_pm()
{
if(Count())
{
#if 0
_farpokel(_dos_ds, 0x46C, _farpeekl(_dos_ds, 0x46C)+1);
#else
I08regs.x.cs = OldI08_rm.rm_segment;
I08regs.x.ip = OldI08_rm.rm_offset;
I08regs.x.ss = I08regs.x.sp = 0;
__dpmi_simulate_real_mode_procedure_iret(&I08regs);
#endif
}
outportb(0x20,0x20);
}
#else
static std::thread MarioThread;
static volatile bool EndMarioThread = false;
static void MarioThreadRunner(unsigned rate)
{
std::chrono::duration<double> period(1.0 / rate);
while(!EndMarioThread)
{
MarioTimer += 2;
std::this_thread::sleep_for(period);
}
}
#endif
void FixMarioTimer()
{
#if defined(__BORLANDC__)
disable();
_asm { mov al, 0x34; out 0x43, al
mov ax, Counter; out 0x40, al
mov al, ah; out 0x40, al }
enable();
#elif defined(__DJGPP__)
disable();
outportb(0x43,0x34);
outportb(0x40, Counter);
outportb(0x40, Counter>>8);
enable();
#else
#endif
}
void InstallMario()
{
#ifdef __BORLANDC__
unsigned short rate = 120u;
#else
unsigned rate = 60u;
#endif
#ifdef __BORLANDC__
Counter = 0x1234DCUL / rate;
disable();
OldI08 = (void(interrupt*)()) *(void **)MK_FP(0, 8*4);
*(void **)MK_FP(0, 8*4) = (void *)MarioI08;
FixMarioTimer();
#elif defined(__DJGPP__)
Counter = 0x1234DCUL / rate;
_go32_dpmi_get_real_mode_interrupt_vector(8, &OldI08_rm);
NewI08_rm.pm_offset = (unsigned long)MarioI08_rm;
NewI08_rm.pm_selector = _go32_my_cs();
_go32_dpmi_allocate_real_mode_callback_iret(&NewI08_rm, &I08regs);
_go32_dpmi_set_real_mode_interrupt_vector(8, &NewI08_rm);
_go32_dpmi_get_protected_mode_interrupt_vector(8, &OldI08_pm);
NewI08_pm.pm_offset = reinterpret_cast<unsigned long>(MarioI08_pm);
NewI08_pm.pm_selector = _go32_my_cs();
_go32_dpmi_allocate_iret_wrapper(&NewI08_pm);
_go32_dpmi_set_protected_mode_interrupt_vector(8, &NewI08_pm);
FixMarioTimer();
#else
EndMarioThread = false;
MarioThread = std::thread(MarioThreadRunner, rate);
#endif
}
void DeInstallMario()
{
#ifdef __BORLANDC__
disable();
*(void **)MK_FP(0, 8*4) = (void *)OldI08;
enable();
Counter = 0;
FixMarioTimer();
#elif defined(__DJGPP__)
_go32_dpmi_set_real_mode_interrupt_vector(8, &OldI08_rm);
_go32_dpmi_set_protected_mode_interrupt_vector(8, &OldI08_pm);
_go32_dpmi_free_iret_wrapper(&NewI08_pm);
Counter = 0;
FixMarioTimer();
#else
EndMarioThread = true;
MarioThread.join();
#endif
}