ca65 V2.18 - Git 5552824 Main file : /data/emu/65c02-python/reference/build/dormann/65C02_decimal_test.ca65 Current file: /data/emu/65c02-python/reference/build/dormann/65C02_decimal_test.ca65 000000r 1 ; Verify decimal mode behavior 000000r 1 ; Written by Bruce Clark. This code is public domain. 000000r 1 ; see http://www.6502.org/tutorials/decimal_mode.html 000000r 1 ; 000000r 1 ; Returns: 000000r 1 ; ERROR = 0 if the test passed 000000r 1 ; ERROR = 1 if the test failed 000000r 1 ; modify the code at the DONE label for desired program end 000000r 1 ; 000000r 1 ; This routine requires 17 bytes of RAM -- 1 byte each for: 000000r 1 ; AR, CF, DA, DNVZC, ERROR, HA, HNVZC, N1, N1H, N1L, N2, N2L, NF, VF, and ZF 000000r 1 ; and 2 bytes for N2H 000000r 1 ; 000000r 1 ; Variables: 000000r 1 ; N1 and N2 are the two numbers to be added or subtracted 000000r 1 ; N1H, N1L, N2H, and N2L are the upper 4 bits and lower 4 bits of N1 and N2 000000r 1 ; DA and DNVZC are the actual accumulator and flag results in decimal mode 000000r 1 ; HA and HNVZC are the accumulator and flag results when N1 and N2 are 000000r 1 ; added or subtracted using binary arithmetic 000000r 1 ; AR, NF, VF, ZF, and CF are the predicted decimal mode accumulator and 000000r 1 ; flag results, calculated using binary arithmetic 000000r 1 ; 000000r 1 ; This program takes approximately 1 minute at 1 MHz (a few seconds more on 000000r 1 ; a 65C02 than a 6502 or 65816) 000000r 1 ; 000000r 1 000000r 1 ; Configuration: 000000r 1 cputype = 1 ; 0 = 6502, 1 = 65C02, 2 = 65C816 000000r 1 vld_bcd = 0 ; 0 = allow invalid bcd, 1 = valid bcd only 000000r 1 chk_a = 1 ; check accumulator 000000r 1 chk_n = 1 ; check sign (negative) flag 000000r 1 chk_v = 1 ; check overflow flag 000000r 1 chk_z = 1 ; check zero flag 000000r 1 chk_c = 1 ; check carry flag 000000r 1 000000r 1 .macro end_of_test 000000r 1 jmp * ;test passed, no errors. 000000r 1 .endmacro 000000r 1 000000r 1 .ZEROPAGE 000000r 1 .org 0 000000 1 ; operands - register Y = carry in 000000 1 00 N1: .res 1,0 000001 1 00 N2: .res 1,0 000002 1 ; binary result 000002 1 00 HA: .res 1,0 000003 1 00 HNVZC: .res 1,0 000004 1 ;04 000004 1 ; decimal result 000004 1 00 DA: .res 1,0 000005 1 00 DNVZC: .res 1,0 000006 1 ; predicted results 000006 1 00 AR: .res 1,0 000007 1 00 NF: .res 1,0 000008 1 ;08 000008 1 00 VF: .res 1,0 000009 1 00 ZF: .res 1,0 00000A 1 00 CF: .res 1,0 00000B 1 00 ERROR: .res 1,0 00000C 1 ;0C 00000C 1 ; workspace 00000C 1 00 N1L: .res 1,0 00000D 1 00 N1H: .res 1,0 00000E 1 00 N2L: .res 1,0 00000F 1 00 00 N2H: .res 2,0 000011 1 000011 1 .CODE 000011 1 .org $400 000400 1 A0 01 TEST: ldy #1 ; initialize Y (used to loop through carry flag values) 000402 1 84 0B sty ERROR ; store 1 in ERROR until the test passes 000404 1 A9 00 lda #0 ; initialize N1 and N2 000406 1 85 00 sta N1 000408 1 85 01 sta N2 00040A 1 A5 01 LOOP1: lda N2 ; N2L = N2 & $0F 00040C 1 29 0F and #$0F ; [1] see text 00040E 1 .if vld_bcd = 1 00040E 1 cmp #$0a 00040E 1 bcs NEXT2 00040E 1 .endif 00040E 1 85 0E sta N2L 000410 1 A5 01 lda N2 ; N2H = N2 & $F0 000412 1 29 F0 and #$F0 ; [2] see text 000414 1 .if vld_bcd = 1 000414 1 cmp #$a0 000414 1 bcs NEXT2 000414 1 .endif 000414 1 85 0F sta N2H 000416 1 09 0F ora #$0F ; N2H+1 = (N2 & $F0) + $0F 000418 1 85 10 sta N2H+1 00041A 1 A5 00 LOOP2: lda N1 ; N1L = N1 & $0F 00041C 1 29 0F and #$0F ; [3] see text 00041E 1 .if vld_bcd = 1 00041E 1 cmp #$0a 00041E 1 bcs NEXT1 00041E 1 .endif 00041E 1 85 0C sta N1L 000420 1 A5 00 lda N1 ; N1H = N1 & $F0 000422 1 29 F0 and #$F0 ; [4] see text 000424 1 .if vld_bcd = 1 000424 1 cmp #$a0 000424 1 bcs NEXT1 000424 1 .endif 000424 1 85 0D sta N1H 000426 1 20 4E 04 jsr ADD 000429 1 20 F1 04 jsr A6502 00042C 1 20 CC 04 jsr COMPARE 00042F 1 D0 1A bne DONE 000431 1 20 92 04 jsr SUB 000434 1 20 FA 04 jsr S6502 000437 1 20 CC 04 jsr COMPARE 00043A 1 D0 0F bne DONE 00043C 1 E6 00 NEXT1: inc N1 ; [5] see text 00043E 1 D0 DA bne LOOP2 ; loop through all 256 values of N1 000440 1 E6 01 NEXT2: inc N2 ; [6] see text 000442 1 D0 C6 bne LOOP1 ; loop through all 256 values of N2 000444 1 88 dey 000445 1 10 C3 bpl LOOP1 ; loop through both values of the carry flag 000447 1 A9 00 lda #0 ; test passed, so store 0 in ERROR 000449 1 85 0B sta ERROR 00044B 1 DONE: 00044B 1 4C 4B 04 end_of_test 00044E 1 00044E 1 ; Calculate the actual decimal mode accumulator and flags, the accumulator 00044E 1 ; and flag results when N1 is added to N2 using binary arithmetic, the 00044E 1 ; predicted accumulator result, the predicted carry flag, and the predicted 00044E 1 ; V flag 00044E 1 ; 00044E 1 F8 ADD: sed ; decimal mode 00044F 1 C0 01 cpy #1 ; set carry if Y = 1, clear carry if Y = 0 000451 1 A5 00 lda N1 000453 1 65 01 adc N2 000455 1 85 04 sta DA ; actual accumulator result in decimal mode 000457 1 08 php 000458 1 68 pla 000459 1 85 05 sta DNVZC ; actual flags result in decimal mode 00045B 1 D8 cld ; binary mode 00045C 1 C0 01 cpy #1 ; set carry if Y = 1, clear carry if Y = 0 00045E 1 A5 00 lda N1 000460 1 65 01 adc N2 000462 1 85 02 sta HA ; accumulator result of N1+N2 using binary arithmetic 000464 1 000464 1 08 php 000465 1 68 pla 000466 1 85 03 sta HNVZC ; flags result of N1+N2 using binary arithmetic 000468 1 C0 01 cpy #1 00046A 1 A5 0C lda N1L 00046C 1 65 0E adc N2L 00046E 1 C9 0A cmp #$0A 000470 1 A2 00 ldx #0 000472 1 90 06 bcc A1 000474 1 E8 inx 000475 1 69 05 adc #5 ; add 6 (carry is set) 000477 1 29 0F and #$0F 000479 1 38 sec 00047A 1 05 0D A1: ora N1H 00047C 1 ; 00047C 1 ; if N1L + N2L < $0A, then add N2 & $F0 00047C 1 ; if N1L + N2L >= $0A, then add (N2 & $F0) + $0F + 1 (carry is set) 00047C 1 ; 00047C 1 75 0F adc N2H,x 00047E 1 08 php 00047F 1 B0 04 bcs A2 000481 1 C9 A0 cmp #$A0 000483 1 90 03 bcc A3 000485 1 69 5F A2: adc #$5F ; add $60 (carry is set) 000487 1 38 sec 000488 1 85 06 A3: sta AR ; predicted accumulator result 00048A 1 08 php 00048B 1 68 pla 00048C 1 85 0A sta CF ; predicted carry result 00048E 1 68 pla 00048F 1 ; 00048F 1 ; note that all 8 bits of the P register are stored in VF 00048F 1 ; 00048F 1 85 08 sta VF ; predicted V flags 000491 1 60 rts 000492 1 000492 1 ; Calculate the actual decimal mode accumulator and flags, and the 000492 1 ; accumulator and flag results when N2 is subtracted from N1 using binary 000492 1 ; arithmetic 000492 1 ; 000492 1 F8 SUB: sed ; decimal mode 000493 1 C0 01 cpy #1 ; set carry if Y = 1, clear carry if Y = 0 000495 1 A5 00 lda N1 000497 1 E5 01 sbc N2 000499 1 85 04 sta DA ; actual accumulator result in decimal mode 00049B 1 08 php 00049C 1 68 pla 00049D 1 85 05 sta DNVZC ; actual flags result in decimal mode 00049F 1 D8 cld ; binary mode 0004A0 1 C0 01 cpy #1 ; set carry if Y = 1, clear carry if Y = 0 0004A2 1 A5 00 lda N1 0004A4 1 E5 01 sbc N2 0004A6 1 85 02 sta HA ; accumulator result of N1-N2 using binary arithmetic 0004A8 1 0004A8 1 08 php 0004A9 1 68 pla 0004AA 1 85 03 sta HNVZC ; flags result of N1-N2 using binary arithmetic 0004AC 1 60 rts 0004AD 1 0004AD 1 .if cputype <> 1 0004AD 1 ; Calculate the predicted SBC accumulator result for the 6502 and 65816 0004AD 1 ; 0004AD 1 SUB1: cpy #1 ; set carry if Y = 1, clear carry if Y = 0 0004AD 1 lda N1L 0004AD 1 sbc N2L 0004AD 1 ldx #0 0004AD 1 bcs S11 0004AD 1 inx 0004AD 1 sbc #5 ; subtract 6 (carry is clear) 0004AD 1 and #$0F 0004AD 1 clc 0004AD 1 S11: ora N1H 0004AD 1 ; 0004AD 1 ; if N1L - N2L >= 0, then subtract N2 & $F0 0004AD 1 ; if N1L - N2L < 0, then subtract (N2 & $F0) + $0F + 1 (carry is clear) 0004AD 1 ; 0004AD 1 sbc N2H,x 0004AD 1 bcs S12 0004AD 1 sbc #$5F ; subtract $60 (carry is clear) 0004AD 1 S12: sta AR 0004AD 1 rts 0004AD 1 .endif 0004AD 1 0004AD 1 .if cputype = 1 0004AD 1 ; Calculate the predicted SBC accumulator result for the 6502 and 65C02 0004AD 1 ; 0004AD 1 C0 01 SUB2: cpy #1 ; set carry if Y = 1, clear carry if Y = 0 0004AF 1 A5 0C lda N1L 0004B1 1 E5 0E sbc N2L 0004B3 1 A2 00 ldx #0 0004B5 1 B0 04 bcs S21 0004B7 1 E8 inx 0004B8 1 29 0F and #$0F 0004BA 1 18 clc 0004BB 1 05 0D S21: ora N1H 0004BD 1 ; 0004BD 1 ; if N1L - N2L >= 0, then subtract N2 & $F0 0004BD 1 ; if N1L - N2L < 0, then subtract (N2 & $F0) + $0F + 1 (carry is clear) 0004BD 1 ; 0004BD 1 F5 0F sbc N2H,x 0004BF 1 B0 02 bcs S22 0004C1 1 E9 5F sbc #$5F ; subtract $60 (carry is clear) 0004C3 1 E0 00 S22: cpx #0 0004C5 1 F0 02 beq S23 0004C7 1 E9 06 sbc #6 0004C9 1 85 06 S23: sta AR ; predicted accumulator result 0004CB 1 60 rts 0004CC 1 .endif 0004CC 1 0004CC 1 ; Compare accumulator actual results to predicted results 0004CC 1 ; 0004CC 1 ; Return: 0004CC 1 ; Z flag = 1 (BEQ branch) if same 0004CC 1 ; Z flag = 0 (BNE branch) if different 0004CC 1 ; 0004CC 1 COMPARE: 0004CC 1 .if chk_a = 1 0004CC 1 A5 04 lda DA 0004CE 1 C5 06 cmp AR 0004D0 1 D0 1E bne C1 0004D2 1 .endif 0004D2 1 .if chk_n = 1 0004D2 1 A5 05 lda DNVZC ; [7] see text 0004D4 1 45 07 eor NF 0004D6 1 29 80 and #$80 ; mask off N flag 0004D8 1 D0 16 bne C1 0004DA 1 .endif 0004DA 1 .if chk_v = 1 0004DA 1 A5 05 lda DNVZC ; [8] see text 0004DC 1 45 08 eor VF 0004DE 1 29 40 and #$40 ; mask off V flag 0004E0 1 D0 0E bne C1 ; [9] see text 0004E2 1 .endif 0004E2 1 .if chk_z = 1 0004E2 1 A5 05 lda DNVZC 0004E4 1 45 09 eor ZF ; mask off Z flag 0004E6 1 29 02 and #2 0004E8 1 D0 06 bne C1 ; [10] see text 0004EA 1 .endif 0004EA 1 .if chk_c = 1 0004EA 1 A5 05 lda DNVZC 0004EC 1 45 0A eor CF 0004EE 1 29 01 and #1 ; mask off C flag 0004F0 1 .endif 0004F0 1 60 C1: rts 0004F1 1 0004F1 1 ; These routines store the predicted values for ADC and SBC for the 6502, 0004F1 1 ; 65C02, and 65816 in AR, CF, NF, VF, and ZF 0004F1 1 0004F1 1 .if cputype = 0 0004F1 1 0004F1 1 A6502: lda VF ; 6502 0004F1 1 ; 0004F1 1 ; since all 8 bits of the P register were stored in VF, bit 7 of VF contains 0004F1 1 ; the N flag for NF 0004F1 1 ; 0004F1 1 sta NF 0004F1 1 lda HNVZC 0004F1 1 sta ZF 0004F1 1 rts 0004F1 1 0004F1 1 S6502: jsr SUB1 0004F1 1 lda HNVZC 0004F1 1 sta NF 0004F1 1 sta VF 0004F1 1 sta ZF 0004F1 1 sta CF 0004F1 1 rts 0004F1 1 0004F1 1 .endif 0004F1 1 .if cputype = 1 0004F1 1 0004F1 1 A5 06 A6502: lda AR ; 65C02 0004F3 1 08 php 0004F4 1 68 pla 0004F5 1 85 07 sta NF 0004F7 1 85 09 sta ZF 0004F9 1 60 rts 0004FA 1 0004FA 1 20 AD 04 S6502: jsr SUB2 0004FD 1 A5 06 lda AR 0004FF 1 08 php 000500 1 68 pla 000501 1 85 07 sta NF 000503 1 85 09 sta ZF 000505 1 A5 03 lda HNVZC 000507 1 85 08 sta VF 000509 1 85 0A sta CF 00050B 1 60 rts 00050C 1 00050C 1 .endif 00050C 1 .if cputype = 2 00050C 1 00050C 1 A6502: lda AR ; 65C816 00050C 1 php 00050C 1 pla 00050C 1 sta NF 00050C 1 sta ZF 00050C 1 rts 00050C 1 00050C 1 S6502: jsr SUB1 00050C 1 lda AR 00050C 1 php 00050C 1 pla 00050C 1 sta NF 00050C 1 sta ZF 00050C 1 lda HNVZC 00050C 1 sta VF 00050C 1 sta CF 00050C 1 rts 00050C 1 00050C 1 .endif 00050C 1