Skip to navigation

Maths (Arithmetic): GetNextSeedAsBCD

Name: GetNextSeedAsBCD [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Set A to the next number from the landscape's sequence of seed numbers, converted to a binary coded decimal (BCD) number
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * CheckSecretCode (Part 1 of 2) calls GetNextSeedAsBCD * SpawnSecretCode3D calls GetNextSeedAsBCD
.GetNextSeedAsBCD JSR GetNextSeedNumber \ Set A to the next number from the landscape's sequence \ of seed numbers \ We now convert this into a binary coded decimal (BCD) \ number by ensuring that both the low nibble and high \ nibble are in the range 0 to 9 PHA \ Store A on the stack so we can retrieve it below AND #%00001111 \ Extract the low nibble of A, so it's in the range 0 to \ 15 CMP #10 \ If A >= 10 then set A = A - 6 BCC rbcd1 \ SBC #6 \ This reduces the number in A to the range 0 to 9, so \ it's suitable for the second digit in a BCD number \ \ The subtraction will work because the C flag is set by \ the time we reach the SBC instruction .rbcd1 STA lowNibbleBCD \ Store the low nibble of the result in lowNibbleBCD PLA \ Retrieve the original value of A that we stored on the \ stack above AND #%11110000 \ Extract the high nibble of A, so it's in the range 0 \ to 15 CMP #10<<4 \ If the high nibble in A >= 10 then subtract 6 from the BCC rbcd2 \ high nibble SBC #6<<4 \ \ This reduces the high nibble of the number in A to the \ range 0 to 9, so it's suitable for the first digit in \ a BCD number \ \ The subtraction will work because the C flag is set by \ the time we reach the SBC instruction .rbcd2 ORA lowNibbleBCD \ By this point A contains a BCD digit in the high \ nibble and lowNibbleBCD contains a BCD digit in the \ low nibble, so we can OR them together to produce a \ BCD number in A, which we can return as our result RTS \ Return from the subroutine