AGI/Specifications/Other/AGI256-2/Implementation Details

From ScummVM :: Wiki
< AGI‎ | Specifications‎ | Other
Revision as of 00:02, 17 July 2007 by Buddha^ (talk | contribs) (Added general info on cel blitting routine's modifications.)
Jump to navigation Jump to search

As AGI256-2 is an extension to AGI256 all its implementation details apply unless otherwise noted.

Cel blitting routine is modified from AGI256's version (Be sure to read its general description) so that raw image data instead of run length encoded data can be written and also so that pixels aren't ANDed with a mask of 0x0F but all 256 colors are let through. Also with this routine (As with AGI256's version) pixels are written to the 256-color part (i.e. the right 160x168 half) of the 320x168 memory buffer.

Used external info

I used Nick Sonneveld's disassemblies of various PC AGI interpreter versions when trying to figure out AGI256-2. Those IDA databases were highly useful for understanding. Thanks Nick. You can check out the disassemblies if you're interested at the AGI Development Site.

Differences between AGI256 and AGI256-2's files

The only coded changes are in AGIOBJS.OVL. Data files have more differences though.

Reverse engineered C version of AGI256-2's AGIOBJS.OVL

Things may be wrong, misleading etc. Caveat emptor.

This is not a straight assembly to C conversion of the source data. The routine has been modified to look better, agi256_2 parameter has been added and possibly other things. It should give you an idea how it works though.

So to get an idea how the blitting routine runs with 256-color views give boolean value of true as the agi256_2 parameter to it.

struct blitStruct {
	uint8 b0;
	uint8 b1;
	uint8 notEgo; // Sort of a guess based on variable's usage, may also be dontUpdateEgoVisibility or something like that
	uint16 xPos;
	uint16 yPos; // Is this named correctly?
	uint8 b7;
	uint8 b8;
	uint8 b9;
	uint8 b10;
	uint8 b11;
	uint8 b12;
	uint8 b13;
	uint8 b14;
	uint8 b15;
	struct celStruct *celPtr; // Offset into segment DS really, 16 bits
	uint8 b18;
	uint8 b19;
	uint8 b20;
	uint8 b21;
	uint8 b22;
	uint8 b23;
	uint8 b24;
	uint8 b25;
	uint8 b26;
	uint8 b27;
	uint8 b28;
	uint8 b29;
	uint8 b30;
	uint8 b31;
	uint8 b32;
	uint8 b33;
	uint8 b34;
	uint8 b35;
	uint8 celPriority;
};

struct celStruct {
	uint8 width; // This is a guess
	uint8 height;
	union {
		uint8 flags; // Uses values from enum flagsEnum
		uint8 transparentColor; // Only lower 4 bits
	};
	uint8 rleData[?];
};

enum flagsEnum {
	F_MIRROR = 1 << 7;
};

#define _WIDTH 160
#define _HEIGHT 168

uint16 screenOffset(uint8 x, uint8 y) {
	return y * (_WIDTH * 2) + x;
}

void agi256BlitCel(struct blitStruct *s, bool agi256_2) {
	uint8 *pic = &agi256PicSeg[0];
	const uint16 endPos = (_WIDTH * 2) * (_HEIGHT - 1); // I would have put (((_WIDTH * 2) * (_HEIGHT - 1)) - _WIDTH) here
	bool celInvisible = true;
	uint8 height = s->celPtr->height;
	uint8 celPriority = s->celPriority;
	
	if (!agi256_2 && s->celPtr->flags & F_MIRROR) // Test for mirroring
		blitMirrorCell();

	uint8 *rleData = s->celPtr->rleData;
	uint16 pos = screenOffset(s->xPos, s->yPos - height + 1);	
	uint8 transparentColor = s->celPtr->transparentColor;
	
	do {
		uint16 oldPos = pos;
		
		while (uint8 rleByte = *(rleData++)) { // rleByte != 0
			uint8 runColor = agi256_2 ? rleByte : rleByte >> 4;
			uint8 runLength = agi256_2 ? 1 : rleByte & 0x0f;
				
			if (runColor == transparentColor) { // Check for transparency
				pos += runLength;
			} else do {
				uint8 pixelPriority = pic[pos] >> 4; // Read pixel's priority/control line info
					
				if (pixelPriority < 3) {
					pixelPriority = 0;
					for (uint16 ySearchPos = pos + _WIDTH * 2;
						ySearchPos < endPos && (pixelPriority = pic[ySearchPos] >> 4) < 3;
						ySearchPos += _WIDTH * 2);
				}
				
				if (celPriority >= pixelPriority) {
					pic[pos + _WIDTH] = runColor;
					celInvisible = false;
				}
				
				pos++;
			} while (--runLength > 0);
		}
		
		pos = oldPos + _WIDTH * 2;
	} while (--height > 0);
	
	if (!s->notEgo)
		setFlag(f1, celInvisible);
}

Disassembly of AGIOBJS.OVL showing differences between AGI256 and AGI256-2

This disassembly is from AGI256-2's AGIOBJS.OVL.

Comments show what the lines were in AGI256 so you can compare them more easily. I've used the asterisk character in the comments to designate that the previous instruction is continued in that position too.

The comments aren't very up to date. So they may be plain wrong sometimes as I've partially guessed them at the time I wrote them. See the C version of this subroutine to better understand it.

seg000:0000 ; Segment type: Pure code
seg000:0000 seg000          segment byte public 'CODE' use16
seg000:0000                 assume cs:seg000
seg000:0000                 assume es:nothing, ss:nothing, ds:nothing, fs:nothing, gs:nothing
seg000:0000                 jmp     loc_9
seg000:0003 ; ---------------------------------------------------------------------------
seg000:0003                 jmp     loc_48
seg000:0006 ; ---------------------------------------------------------------------------
seg000:0006                 jmp     loc_85
seg000:0009 ; ---------------------------------------------------------------------------
seg000:0009 
seg000:0009 loc_9:                                  ; CODE XREF: seg000:0000�j
seg000:0009                 push    si
seg000:000A                 push    di
seg000:000B                 push    bp
seg000:000C                 mov     bp, sp
seg000:000E                 sub     sp, 2
seg000:0011                 mov     bp, [bp+8]
seg000:0014                 mov     al, [bp+8]
seg000:0017                 mov     ah, [bp+6]
seg000:001A                 call    near ptr 0B8B6h
seg000:001D                 mov     si, di
seg000:001F                 mov     ah, [bp+0Ch]
seg000:0022                 mov     al, [bp+0Ah]
seg000:0025                 mov     di, [bp+0Eh]
seg000:0028                 push    ds
seg000:0029                 mov     ds, ds:136Fh
seg000:002D                 xor     dx, dx
seg000:002F                 mov     cx, dx
seg000:0031                 jmp     loc_155
seg000:0034 ; ---------------------------------------------------------------------------
seg000:0034                 nop
seg000:0035                 nop
seg000:0036 
seg000:0036 loc_36:                                 ; CODE XREF: seg000:003E�j
seg000:0036                                         ; seg000:0175�j
seg000:0036                 mov     cl, al
seg000:0038                 repe movsb
seg000:003A                 add     si, dx
seg000:003C                 dec     ah
seg000:003E                 jnz     loc_36
seg000:0040                 pop     ds
seg000:0041                 add     sp, 2
seg000:0044                 pop     bp
seg000:0045                 pop     di
seg000:0046                 pop     si
seg000:0047                 retn
seg000:0048 ; ---------------------------------------------------------------------------
seg000:0048 
seg000:0048 loc_48:                                 ; CODE XREF: seg000:0003�j
seg000:0048                 push    si
seg000:0049                 push    di
seg000:004A                 push    bp
seg000:004B                 mov     bp, sp
seg000:004D                 sub     sp, 2
seg000:0050                 mov     bp, [bp+8]
seg000:0053                 mov     al, [bp+8]
seg000:0056                 mov     ah, [bp+6]
seg000:0059                 call    near ptr 0B8B6h
seg000:005C                 mov     si, [bp+0Eh]
seg000:005F                 mov     ah, [bp+0Ch]
seg000:0062                 mov     al, [bp+0Ah]
seg000:0065                 push    es
seg000:0066                 mov     es, ds:136Fh
seg000:006A                 xor     dx, dx
seg000:006C                 mov     cx, dx
seg000:006E                 jmp     loc_161
seg000:0071 ; ---------------------------------------------------------------------------
seg000:0071                 nop
seg000:0072                 nop
seg000:0073 
seg000:0073 loc_73:                                 ; CODE XREF: seg000:007B�j
seg000:0073                                         ; seg000:016E�j
seg000:0073                 mov     cl, al
seg000:0075                 repe movsb
seg000:0077                 add     di, dx
seg000:0079                 dec     ah
seg000:007B                 jnz     loc_73
seg000:007D                 pop     es
seg000:007E                 add     sp, 2
seg000:0081                 pop     bp
seg000:0082                 pop     di
seg000:0083                 pop     si
seg000:0084                 retn
seg000:0085 ; ---------------------------------------------------------------------------
seg000:0085 
seg000:0085 loc_85:                                 ; CODE XREF: seg000:0006�j
seg000:0085                 push    si
seg000:0086                 push    di
seg000:0087                 push    bp
seg000:0088                 mov     bp, sp
seg000:008A                 sub     sp, 2
seg000:008D                 mov     bp, [bp+8]
seg000:0090                 mov     al, [bp+2]
seg000:0093                 push    ax
seg000:0094                 mov     si, [bp+10h]
seg000:0097                 test    byte ptr [si+2], 80h
seg000:009B                 jmp     short loc_A5    ; jz      loc_A5 (Was mirror testing, now we ignore mirroring)
seg000:009D ; ---------------------------------------------------------------------------
seg000:009D                 push    bp
seg000:009E                 nop                     ; call    near ptr 0BACDh (Was _BlitMirrorCell, now not used)
seg000:009F                 nop                     ; *
seg000:00A0                 nop                     ; *
seg000:00A1                 pop     bp              ; *
seg000:00A2                 mov     si, [bp+10h]
seg000:00A5 
seg000:00A5 loc_A5:                                 ; CODE XREF: seg000:009B�j
seg000:00A5                 inc     si
seg000:00A6                 lodsw
seg000:00A7                 mov     dx, ax
seg000:00A9                 mov     ah, [bp+3]
seg000:00AC                 mov     al, [bp+5]
seg000:00AF                 sub     al, dl
seg000:00B1                 inc     al
seg000:00B3                 call    near ptr 0B8B6h
seg000:00B6                 nop                     ; shl     dh, 1
seg000:00B7                 nop                     ; *
seg000:00B8                 nop                     ; shl     dh, 1
seg000:00B9                 nop                     ; *
seg000:00BA                 nop                     ; shl     dh, 1
seg000:00BB                 nop                     ; *
seg000:00BC                 nop                     ; shl     dh, 1
seg000:00BD                 nop                     ; *
seg000:00BD                                         ; (These were probably for putting color info into upper nibble)
seg000:00BE                 push    es
seg000:00BF                 mov     es, ds:136Fh
seg000:00C3                 mov     bl, 1
seg000:00C5                 mov     bh, [bp+24h]
seg000:00C8                 shl     bh, 1
seg000:00CA                 shl     bh, 1
seg000:00CC                 shl     bh, 1
seg000:00CE                 shl     bh, 1
seg000:00D0                 mov     bp, di
seg000:00D2                 xor     cx, cx
seg000:00D4                 jmp     short loc_DA
seg000:00D6 ; ---------------------------------------------------------------------------
seg000:00D6                 nop
seg000:00D7 
seg000:00D7 loc_D7:                                 ; CODE XREF: seg000:00E6�j
seg000:00D7                 cbw
seg000:00D8                 add     di, ax
seg000:00DA 
seg000:00DA loc_DA:                                 ; CODE XREF: seg000:00D4�j
seg000:00DA                                         ; seg000:0108�j ...
seg000:00DA                 lodsb
seg000:00DB                 or      al, al
seg000:00DD                 jz      loc_10A
seg000:00DF                 mov     ah, al
seg000:00E1                 mov     al, 1           ; and     ax, 0F00Fh
seg000:00E3                 nop                     ; *
seg000:00E3                                         ; (Was probably for checking run length... now not needed, always 1)
seg000:00E4                 cmp     ah, dh
seg000:00E6                 jz      loc_D7
seg000:00E8                 mov     cl, 1           ; mov     cl, al (Was for run length, now always 1)
seg000:00EA                 nop                     ; shr     ah, 1
seg000:00EB                 nop                     ; *
seg000:00EC                 nop                     ; shr     ah, 1
seg000:00ED                 nop                     ; *
seg000:00EE                 nop                     ; shr     ah, 1
seg000:00EF                 nop                     ; *
seg000:00F0                 nop                     ; shr     ah, 1
seg000:00F1                 nop                     ; *
seg000:00F1                                         ; (These were probably for getting color info from upper to lower nibble)
seg000:00F2 
seg000:00F2 loc_F2:                                 ; CODE XREF: seg000:0136�j
seg000:00F2                 mov     al, es:[di]
seg000:00F5                 and     al, 0F0h
seg000:00F7                 cmp     al, 20h ; ' '
seg000:00F9                 jbe     loc_116
seg000:00FB                 cmp     al, bh
seg000:00FD                 ja      loc_135
seg000:00FF                 mov     al, ah          ; mov     al, bh ??
seg000:0101 
seg000:0101 loc_101:                                ; CODE XREF: seg000:0133�j
seg000:0101                 mov     al, ah          ; or      al, ah
seg000:0103                 jmp     loc_178
seg000:0106 ; ---------------------------------------------------------------------------
seg000:0106 
seg000:0106 loc_106:                                ; CODE XREF: seg000:0185�j
seg000:0106                 nop                     ; loop    loc_F2
seg000:0107                 nop                     ; *
seg000:0108                 jmp     short loc_DA
seg000:010A ; ---------------------------------------------------------------------------
seg000:010A 
seg000:010A loc_10A:                                ; CODE XREF: seg000:00DD�j
seg000:010A                 dec     dl
seg000:010C                 jz      loc_13A
seg000:010E                 add     bp, 140h
seg000:0112                 mov     di, bp
seg000:0114                 jmp     short loc_DA
seg000:0116 ; ---------------------------------------------------------------------------
seg000:0116 
seg000:0116 loc_116:                                ; CODE XREF: seg000:00F9�j
seg000:0116                 push    di
seg000:0117                 xor     ch, ch
seg000:0119 
seg000:0119 loc_119:                                ; CODE XREF: seg000:012C�j
seg000:0119                 cmp     di, 0D0C0h
seg000:011D                 jnb     loc_12E
seg000:011F                 add     di, 140h
seg000:0123                 mov     ch, es:[di]
seg000:0126                 and     ch, 0F0h
seg000:0129                 cmp     ch, 20h ; ' '
seg000:012C                 jbe     loc_119
seg000:012E 
seg000:012E loc_12E:                                ; CODE XREF: seg000:011D�j
seg000:012E                 pop     di
seg000:012F                 cmp     ch, bh
seg000:0131                 mov     ch, 0
seg000:0133                 jbe     loc_101         ; or      al, ah
seg000:0135 
seg000:0135 loc_135:                                ; CODE XREF: seg000:00FD�j
seg000:0135                 inc     di
seg000:0136                 loop    loc_F2
seg000:0138                 jmp     short loc_DA
seg000:013A ; ---------------------------------------------------------------------------
seg000:013A 
seg000:013A loc_13A:                                ; CODE XREF: seg000:010C�j
seg000:013A                 pop     es
seg000:013B                 pop     ax
seg000:013C                 or      al, al
seg000:013E                 jnz     loc_14E
seg000:0140                 mov     al, 1
seg000:0142                 test    bl, bl
seg000:0144                 jnz     loc_14B
seg000:0146                 call    near ptr 0D744h
seg000:0149                 jmp     short loc_14E
seg000:014B ; ---------------------------------------------------------------------------
seg000:014B 
seg000:014B loc_14B:                                ; CODE XREF: seg000:0144�j
seg000:014B                 call    near ptr 0D73Eh
seg000:014E 
seg000:014E loc_14E:                                ; CODE XREF: seg000:013E�j
seg000:014E                                         ; seg000:0149�j
seg000:014E                 add     sp, 2
seg000:0151                 pop     bp
seg000:0152                 pop     di
seg000:0153                 pop     si
seg000:0154                 retn
seg000:0155 ; ---------------------------------------------------------------------------
seg000:0155 
seg000:0155 loc_155:                                ; CODE XREF: seg000:0031�j
seg000:0155                 mov     dx, 140h
seg000:0158                 push    ax
seg000:0159                 xor     ah, ah
seg000:015B                 sub     dx, ax
seg000:015D                 pop     ax
seg000:015E                 jmp     loc_171
seg000:0161 ; ---------------------------------------------------------------------------
seg000:0161 
seg000:0161 loc_161:                                ; CODE XREF: seg000:006E�j
seg000:0161                 mov     dx, 140h
seg000:0164                 push    ax
seg000:0165                 xor     ah, ah
seg000:0167                 sub     dx, ax
seg000:0169                 pop     ax
seg000:016A                 add     di, 0A0h ; 'á'
seg000:016E                 jmp     loc_73
seg000:0171 ; ---------------------------------------------------------------------------
seg000:0171 
seg000:0171 loc_171:                                ; CODE XREF: seg000:015E�j
seg000:0171                 add     si, 0A0h ; 'á'
seg000:0175                 jmp     loc_36
seg000:0178 ; ---------------------------------------------------------------------------
seg000:0178 
seg000:0178 loc_178:                                ; CODE XREF: seg000:0103�j
seg000:0178                 add     di, 0A0h ; 'á'
seg000:017C                 and     al, 0FFh        ; and     al, 0Fh
seg000:017E                 stosb
seg000:017F                 sub     di, 0A0h ; 'á'
seg000:0183                 xor     bl, bl
seg000:0185                 jmp     loc_106         ; loop    loc_F2
seg000:0185 ; ---------------------------------------------------------------------------
seg000:0188                 db 78h dup(0)
seg000:0188 seg000          ends
seg000:0188 
seg000:0188 
seg000:0188                 end