Interesting Code Snippets and Other Stuff
Posted: Sun Mar 01, 2015 1:38 am
This thread is about interesting pieces of code in Genesis games, or stuff you just might want to know when coding. Feel free to add whatever you want to this thread.
Here are a few examples to get the thread rolling.
------------------------------------------------------
When multiplying a 32-bit number by 10 using this code:
Keep in mind that it's better to NOT replace the lsl.l #2 with two add.l's, because that makes it take 4 cycles longer. The code above always takes 32 cycles.
------------------------------------------------------
For the VRAM increment register ($0F), the value is always positive, so if you write $80, it will advance 128 bytes after each read or write. Therefore, it can advance up to 255 bytes.
------------------------------------------------------
To implement a 16-bit Linear Feedback Shift Register (LFSR), you could use this piece of code. Assume that a0 points to the LFSR's position in memory.
------------------------------------------------------
To copy the C flag to the X flag:
------------------------------------------------------
Much like how Sega used Kosinski compression in their games, Beam Software used a similar format. When I have time to study it, I will post my findings here. It can be found at http://segaretro.org/Beam_Software_Comp ... _Algorithm. Among my observations:
Another example, using a value of 85, in binary:
As stated before, if you find anything interesting regarding code snippets, post them here.
Here are a few examples to get the thread rolling.
------------------------------------------------------
When multiplying a 32-bit number by 10 using this code:
Code: Select all
add.l d0,d0
move.l d0,d1
lsl.l #2,d0
add.l d1,d0
------------------------------------------------------
For the VRAM increment register ($0F), the value is always positive, so if you write $80, it will advance 128 bytes after each read or write. Therefore, it can advance up to 255 bytes.
------------------------------------------------------
To implement a 16-bit Linear Feedback Shift Register (LFSR), you could use this piece of code. Assume that a0 points to the LFSR's position in memory.
Code: Select all
move.w (a0),d0
move.w d0,d1
lsr.w #1,d1
eor.w d1,d0
move.w d0,d1
lsr.w #2,d1
eor.w d1,d0
move.w d0,d1
lsr.w #4,d1
eor.w d1,d0
move.w d0,d1
lsr.w #8,d1
eor.w d1,d0
lsr.w #1,d0
move.w (a0),d0
addx.w d0,d0
move.w d0,(a0)
To copy the C flag to the X flag:
Code: Select all
scs d0
add.b d0,d0
Much like how Sega used Kosinski compression in their games, Beam Software used a similar format. When I have time to study it, I will post my findings here. It can be found at http://segaretro.org/Beam_Software_Comp ... _Algorithm. Among my observations:
- In the Radical Rex ROM, it's located at $0F7E30.
- The decompressor comes in two varieties: Single-byte and double-byte.
- It appears that a0 is the source pointer and a1 is the destination pointer.
- The header is 4 bytes: The first 2 are the file size, and the last 2 are the offset for the first bitfield to be read.
- The command bitfields are 32 bits long, whereas in Kosinski, they're 16 bits long. However, the first bitfield could be just 24 bits long.
- Bitfields are read big-endian.
- All bitfields are stored in one contiguous string, located after the data. In Kosinski, they are interspersed with the data.
- It seems like after every uncompressed byte or uncompressed byte sequence, there's an automatic backreference.
- Copy counts are encoded like this: Remove the leading 1 from the copy count. Insert a 0 after every bit, and append a 1 on the end.
- For backreferences, they are encoded as an absolute offset with plain binary. The number of bits read depends on how many bytes have been written so far.
- 00 [count] = Uncompressed Bytes
- 01 = Uncompressed Single Byte
- 1 [offset] 0 [count] = Backreference
- 1 [offset] 1 = Repeat Any Previous Byte
- Start with: 111
- Remove the leading 1: 11
- Insert 0's between bits: 101
- Append a 1: 1011
Another example, using a value of 85, in binary:
- Start with: 1010101
- Remove the leading 1: 010101
- Insert 0's between bits: 00100010001
- Append a 1: 001000100011
As stated before, if you find anything interesting regarding code snippets, post them here.