Skip to content

Change clc to sec in math operations - #906

Merged
inexorabletash merged 2 commits into
a2stuff:mainfrom
polluks:patch-5
Jul 19, 2026
Merged

Change clc to sec in math operations#906
inexorabletash merged 2 commits into
a2stuff:mainfrom
polluks:patch-5

Conversation

@polluks

@polluks polluks commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Analysis done.
See also .macpack generic.

@inexorabletash

Copy link
Copy Markdown
Collaborator

I can't really notice a behavior difference with this change. It only affects shuffle, and shuffle seems really busted both with and without this change.

Can you say more about what your fix does and how you verified it?

@polluks

polluks commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Well, this is like a "DivideBy11".

@inexorabletash

Copy link
Copy Markdown
Collaborator

FYI I just pushed a related fix 116e687 - the random number seed was... not random. Oops! Anyway, with that change the shuffle function seems to work, as opposed to always being stuck on the same number.

@inexorabletash

Copy link
Copy Markdown
Collaborator

Okay, from staring at the code for a bit, I think the original code at https://github.com/a2stuff/cdremote was indeed flawed.

  • The PRNGMathLoop2 is trying to do A mod HexTrackCount0Base but keeps subtracting one more.

  • The DivideBy10 loop as you note ends up subtracting 11 instead of 10 so it'll be off. If A = 10, it does 10-10-1 which wraps to $FF. Then the CMP sets the N flag and it exits the loop, does more math and ends up with something pretty random.

I'll make some comments in the review.

@inexorabletash inexorabletash left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience. Since this was based on the original 40 year old code, I wanted to be really sure the fixes were valid, and understand why it had mostly worked before.

Comment thread src/desk_acc/cd.remote.s Outdated
@@ -2284,7 +2284,7 @@ PRNGMathLoop1:
PRNGMathLoop2:
cmp HexTrackCount0Base
bmi ExitMathLoop2

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to bcc ? having this be signed doesn't help, we want an unsigned modulus.

Comment thread src/desk_acc/cd.remote.s Outdated
ldx #$00
DivideBy10:
cmp #$0a
bmi TenOrLess

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to bcc ? having this be signed doesn't help, we want an unsigned modulus.

Comment thread src/desk_acc/cd.remote.s Outdated
;;; ============================================================

;; TODO: Analysis - WTF is going on here?? This *seems* like an attempt to convert from BCD to binary, but it's... not. It's kinda wonky.
.proc Hex2BCDSorta

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Sorta can be removed from the proc name

Comment thread src/desk_acc/cd.remote.s Outdated
bmi TenOrLess
inx
clc
sec

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment that this changed from the original code at https://github.com/a2stuff/cdremote which was buggy: it had clc and so produced invalid BCD results for some inputs.

Comment thread src/desk_acc/cd.remote.s Outdated
cmp HexTrackCount0Base
bmi ExitMathLoop2
clc
sec

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment that this changed from the original code at https://github.com/a2stuff/cdremote which was buggy: it had clc and so could result in extra iterations to find a good track.

@polluks

polluks commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

done

@inexorabletash
inexorabletash merged commit 3f7444f into a2stuff:main Jul 19, 2026
1 check passed
@inexorabletash

Copy link
Copy Markdown
Collaborator

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants