# Difference between revisions of "Tactics Ogre:Tutorials"

## Decompression at \$81/F7CC

In Tactics Ogre V 1.0, you can find a subroutine at \$81/F7CC that decompresses data. Here is how it works.

When this subroutine is called, X and A contain the address of compressed data (X the address and A the bank, of course), while the address in [\$23] contains the commands for decompression.

### [\$23] = #\$01

The first argument byte is a number between 1 and 6 or 7 (I think). The first byte of Compressed Data actually contains two values, and the first argument byte tells how many digits belong to the first number. The first number is the number of bytes to do (with some alternations), the second number is the offset from the current \$23-position as source address for the then-upcoming MVN command.

## Decompression at \$81/F9E5

In Tactics Ogre V 1.0, you can find a subroutine at \$81/F9E5 that decompresses data. Here is how it works.

When this subroutine is called, X and A contain the address of compressed data (X the address and A the bank, of course).

The first two bytes of the compressed data give the size of the decompressed data. After that, the next byte contains flags and commands for the decompression subroutines:

### #%1xxx.xxxx Repeat Bytes

If the MSB is set, this means that already decompressed stuff has to be repeated. The second byte also belongs to this command and gets loaded after it is clear this decompression function is meant. The corresponding command double byte is structured like this:

```#%1aaa.abbb.bbbb.bbbb
```
```1 = The set MSB is the signal for this command
a = these four bit are the number of bytes to repeat minus three
b = these bits are the number of bytes to go backwards from the current position minus one
```

### #%0000.0000 Go Read the Next Bank

I think this part of the subroutine is never used and was only built in for savety reasons: This subroutine increments the bank number of the Compressed Data's read address and sets the address back to \$8000 - the start of a new bank. This was built in if the compressed data overleaps into a new bank.

### #%01xx.xxxx Copy directly from compressed data

```#%01aa.aaaa

a = These bits form the number of Bytes minus two
```

This transfers the next (a+1) bytes after this command unchanged into the decompressed data.

### #%001x.xxxx Write \$00 Bytes

```#%001a.aaaa

a = These bits form the number of Bytes minus two
```

This writes \$00 bytes into the decompressed data. There will be at least two and thirty-three at most (a is the number of bytes minus two)

### #%000x.xxxx Repeat lots of Bytes from far back

This function of the decompression uses three bytes for commands. It is the same as the first repeat function, but it can move further back and can transfer more bytes than the other one. The downside of this subroutine is that it takes more time and two more bytes in compressed data.

```FIRST BYTE

#%000a.bbbb

a = Actually not always obligatory bit to be set for this function
b = Part of the number of bytes to transfer

THIRD AND SECOND BYTE (set together in this order as one 16 bit value)

#%ccdd.dddd.dddd.dddd

c = Part of the number of bytes to transfer
d = Number of bytes to move backwards
```

Note that this subroutine does not work with the first byte being completely empty. So, if you want to set the whole "b" part to zero, you should set the "a" bit.

The number of bytes to transfer is a bit tricky. Here is how it is calculated:

```cc * #\$10 + bbbb + #\$04

Number of Bytes to transfer = #%00cc.bbbb + #\$04
```

Three is added, and the fourth additional byte comes from the structure of the MVN command.

Internal Data for Tactics Ogre