From 91c154ea7b79a270d4a3bbef275f091b06d99811 Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Mon, 13 May 2019 12:33:52 -0400 Subject: [PATCH] pt3: fix an issue with signed 8-bit comparisons signed 8-bit comparisons are a huge pain on 6502 --- pt3_player/README.pt3 | 16 ++++++++++++++++ pt3_player/pt3_lib.s | 28 +++++++++++++++++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/pt3_player/README.pt3 b/pt3_player/README.pt3 index 1d81c1fa..f7bf843b 100644 --- a/pt3_player/README.pt3 +++ b/pt3_player/README.pt3 @@ -3,3 +3,19 @@ PT3_player ASR = CMP #$80 / ROR + + + +Notes: + +signed 8-bit comparison +see http://6502.org/tutorials/compare_beyond.html#2.2 + + SEC ; prepare carry for SBC + SBC NUM ; A-NUM + BVC LABEL ; if V is 0, N eor V = N, otherwise N eor V = N eor 1 + EOR #$80 ; A = A eor $80, and N = N eor 1 + LABEL + +If the N flag is 1, then A (signed) < NUM (signed) and BMI will branch +If the N flag is 0, then A (signed) >= NUM (signed) and BPL will branch diff --git a/pt3_player/pt3_lib.s b/pt3_player/pt3_lib.s index cd873b3c..2e21ba89 100644 --- a/pt3_player/pt3_lib.s +++ b/pt3_player/pt3_lib.s @@ -601,17 +601,29 @@ no_tone_sliding: and #$40 beq amp_slide_down amp_slide_up: + ; if (a->amplitude_sliding < 15) { + ; a pain to do signed compares lda note_a+NOTE_AMPLITUDE_SLIDING,X - cmp #15 - bcs done_amp_sliding ; bge - ; if (a->amplitude_sliding < 15) { - inc note_a+NOTE_AMPLITUDE_SLIDING,X ; a->amplitude_sliding++; + sec + sbc #15 + bvc asu_signed + eor #$80 +asu_signed: + bpl done_amp_sliding ; skip if A>=15 + inc note_a+NOTE_AMPLITUDE_SLIDING,X ; a->amplitude_sliding++; jmp done_amp_sliding amp_slide_down: + ; if (a->amplitude_sliding > -15) { + ; a pain to do signed compares lda note_a+NOTE_AMPLITUDE_SLIDING,X - cmp #$f2 ; -14 1111 0010 - bcc done_amp_sliding ; if (a->amplitude_sliding > -15) { - dec note_a+NOTE_AMPLITUDE_SLIDING,X ; ; a->amplitude_sliding--; + sec + sbc #$f1 ; -15 + bvc asd_signed + eor #$80 +asd_signed: + bmi done_amp_sliding ; if A < -15, skip subtract + + dec note_a+NOTE_AMPLITUDE_SLIDING,X ; a->amplitude_sliding--; done_amp_sliding: @@ -710,6 +722,8 @@ last_envelope: adc pt3_envelope_add sta pt3_envelope_add ; pt3->envelope_add+=j; + jmp noise_slide_done ; skip else + noise_slide: ; Noise slide ; else {