; ; 6 5 C 0 2 E X T E N D E D O P C O D E S T E S T ; ; Copyright (C) 2013 Klaus Dormann ; ; This program is free software: you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; This program is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public License ; along with this program. If not, see . ; This program is designed to test all additional 65C02 opcodes, addressing ; modes and functionality not available in the NMOS version of the 6502. ; The 6502_functional_test is a prerequisite to this test. ; NMI, IRQ, BRK, STP & WAI are covered in the 6502_interrupt_test. ; ; version 16-aug-2013 ; contact info at http://2m5.de or email K@2m5.de ; ; assembled with AS65 from http://www.kingswood-consulting.co.uk/assemblers/ ; command line switches: -l -m -s2 -w -x -h0 ; | | | | | no page headers in listing ; | | | | 65C02 extensions ; | | | wide listing (133 char/col) ; | | write intel hex file instead of binary ; | expand macros in listing ; generate pass2 listing ; ; No IO - should be run from a monitor with access to registers. ; To run load intel hex image with a load command, than alter PC to 400 hex ; (code_segment) and enter a go command. ; Loop on program counter determines error or successful completion of test. ; Check listing for relevant traps (jump/branch *). ; Please note that in early tests some instructions will have to be used before ; they are actually tested! ; ; RESET, NMI or IRQ should not occur and will be trapped if vectors are enabled. ; Tests documented behavior of the original 65C02 only! ; Decimal ops will only be tested with valid BCD operands and the V flag will ; be ignored as it is absolutely useless in decimal mode. ; ; Debugging hints: ; Most of the code is written sequentially. if you hit a trap, check the ; immediately preceeding code for the instruction to be tested. Results are ; tested first, flags are checked second by pushing them onto the stack and ; pulling them to the accumulator after the result was checked. The "real" ; flags are no longer valid for the tested instruction at this time! ; If the tested instruction was indexed, the relevant index (X or Y) must ; also be checked. Opposed to the flags, X and Y registers are still valid. ; ; versions: ; 19-jul-2013 1st version distributed for testing ; 23-jul-2013 fixed BRA out of range due to larger trap macros ; added RAM integrity check ; 16-aug-2013 added error report to standard output option ; C O N F I G U R A T I O N ;ROM_vectors writable (0=no, 1=yes) ;if ROM vectors can not be used interrupts will not be trapped ;as a consequence BRK can not be tested but will be emulated to test RTI ROM_vectors = 1 ;load_data_direct (0=move from code segment, 1=load directly) ;loading directly is preferred but may not be supported by your platform ;0 produces only consecutive object code, 1 is not suitable for a binary image load_data_direct = 0 ;I_flag behavior (0=force enabled, 1=force disabled, 2=prohibit change, 3=allow ;change) 2 requires extra code and is not recommended. I_flag = 3 ;configure memory - try to stay away from memory used by the system ;zero_page memory start address, $4e (78) consecutive Bytes required ; add 2 if I_flag = 2 zero_page = $a ;data_segment memory start address, $5D (93) consecutive Bytes required ; + 12 Bytes at data_segment + $f9 (JMP indirect page cross test) data_segment = $900 !if (data_segment & $ff) != 0 { ERROR ERROR ERROR low byte of data_segment MUST be $00 !! } ;code_segment memory start address, 10kB of consecutive space required ; add 1 kB if I_flag = 2 ;parts of the code are self modifying and must reside in RAM code_segment = $a00 ;added WDC only opcodes WAI & STP (0=test as NOPs, >0=no test) wdc_op = 1 ;wdc_op = 0 ;added Rockwell & WDC opcodes BBR, BBS, RMB & SMB ;(0=test as NOPs, 1=full test, >1=no test) ;rkwl_wdc_op = 1 rkwl_wdc_op = 0 ;report errors through I/O channel (0=use standard self trap loops, 1=include ;report.i65 as I/O channel, add 3 kB) report = 1 rep_int = 0 ;RAM integrity test option. Checks for undesired RAM writes. ;set lowest non RAM or RAM mirror address page (-1=disable, 0=64k, $40=16k) ;leave disabled if a monitor, OS or background interrupt is allowed to alter RAM ram_top = -1 ;noopt ;do not take shortcuts ;macros for error & success traps to allow user modification ;example: ;!macro ;trap { ; jsr my_error_handler ; } ;!macro ;trap_eq { ; bne + ; +trap ;failed equal (zero) ;+ ; } ; ; my_error_handler should pop the calling address from the stack and report it. ; putting larger portions of code (more than 3 bytes) inside the trap macro ; may lead to branch range problems for some tests. !if report = 0 { !macro trap { jmp * ;failed anyway } !macro trap_eq { beq * ;failed equal (zero) } !macro trap_ne { bne * ;failed not equal (non zero) } !macro trap_cs { bcs * ;failed carry set } !macro trap_cc { bcc * ;failed carry clear } !macro trap_mi { bmi * ;failed minus (bit 7 set) } !macro trap_pl { bpl * ;failed plus (bit 7 clear) } !macro trap_vs { bvs * ;failed overflow set } !macro trap_vc { bvc * ;failed overflow clear } ; please observe that during the test the stack gets invalidated ; therefore a RTS inside the success macro is not possible !macro success { jmp * ;test passed, no errors } } !if report = 1 { !macro trap { jsr report_error } !macro trap_eq { bne + +trap ;failed equal (zero) + } !macro trap_ne { beq + +trap ;failed not equal (non zero) + } !macro trap_cs { bcc + +trap ;failed carry set + } !macro trap_cc { bcs + +trap ;failed carry clear + } !macro trap_mi { bpl + +trap ;failed minus (bit 7 set) + } !macro trap_pl { bmi + +trap ;failed plus (bit 7 clear) + } !macro trap_vs { bvc + +trap ;failed overflow set + } !macro trap_vc { bvs + +trap ;failed overflow clear + } ; please observe that during the test the stack gets invalidated ; therefore a RTS inside the success macro is not possible !macro success { jsr report_success } } carry = %00000001 ;flag bits in status zero = %00000010 intdis = %00000100 decmode = %00001000 break = %00010000 reserv = %00100000 overfl = %01000000 minus = %10000000 fc = carry fz = zero fzc = carry+zero fv = overfl fvz = overfl+zero fn = minus fnc = minus+carry fnz = minus+zero fnzc = minus+zero+carry fnv = minus+overfl fao = break+reserv ;bits always on after PHP, BRK fai = fao+intdis ;+ forced interrupt disable m8 = $ff ;8 bit mask m8i = $ff&!intdis ;8 bit mask - interrupt disable ;macros to allow masking of status bits. ;masking of interrupt enable/disable on load and compare ;masking of always on bits after PHP or BRK (unused & break) on compare !if I_flag = 0 { !macro load_flag arg1 { lda #arg1&m8i ;force enable interrupts (mask I) } !macro cmp_flag arg1 { cmp #(arg1|fao)&m8i ;I_flag is always enabled + always on bits } !macro eor_flag arg1 { eor #(arg1&m8i|fao) ;mask I, invert expected flags + always on bits } } !if I_flag = 1 { !macro load_flag arg1 { lda #arg1|intdis ;force disable interrupts } !macro cmp_flag arg1 { cmp #(arg1|fai)&m8 ;I_flag is always disabled + always on bits } !macro eor_flag arg1 { eor #(arg1|fai) ;invert expected flags + always on bits + I } } !if I_flag = 2 { !macro load_flag arg1 { lda #arg1 ora flag_I_on ;restore I-flag and flag_I_off } !macro cmp_flag arg1 { eor flag_I_on ;I_flag is never changed cmp #(arg1|fao)&m8i ;expected flags + always on bits, mask I } !macro eor_flag arg1 { eor flag_I_on ;I_flag is never changed eor #(arg1&m8i|fao) ;mask I, invert expected flags + always on bits } } !if I_flag = 3 { !macro load_flag arg1 { lda #arg1 ;allow test to change I-flag (no mask) } !macro cmp_flag arg1 { cmp #(arg1|fao)&m8 ;expected flags + always on bits } !macro eor_flag arg1 { eor #arg1|fao ;invert expected flags + always on bits } } ;macros to set (register|memory|zeropage) & status !macro set_stat arg1 { ;setting flags in the processor status register +load_flag arg1 pha ;use stack to load status plp } !macro set_a arg1,arg2 { ;precharging accu & status +load_flag arg2 pha ;use stack to load status lda #arg1 ;precharge accu plp } !macro set_x arg1,arg2 { ;precharging index & status +load_flag arg2 pha ;use stack to load status ldx #arg1 ;precharge index x plp } !macro set_y arg1,arg2 { ;precharging index & status +load_flag arg2 pha ;use stack to load status ldy #arg1 ;precharge index y plp } !macro set_ax arg1,arg2 { ;precharging indexed accu & immediate status +load_flag arg2 pha ;use stack to load status lda arg1,x ;precharge accu plp } !macro set_ay arg1,arg2 { ;precharging indexed accu & immediate status +load_flag arg2 pha ;use stack to load status lda arg1,y ;precharge accu plp } !macro set_z arg1,arg2 { ;precharging indexed zp & immediate status +load_flag arg2 pha ;use stack to load status lda arg1,x ;load to zeropage sta zpt plp } !macro set_zx { ;precharging zp,x & immediate status +load_flag arg2 pha ;use stack to load status lda arg1,x ;load to indexed zeropage sta zpt,x plp } !macro set_abs arg1,arg2 { ;precharging indexed memory & immediate status +load_flag arg2 pha ;use stack to load status lda arg1,x ;load to memory sta abst plp } !macro set_absx arg1,arg2 { ;precharging abs,x & immediate status +load_flag arg2 pha ;use stack to load status lda arg1,x ;load to indexed memory sta abst,x plp } ;macros to test (register|memory|zeropage) & status & (mask) !macro tst_stat arg1 { ;testing flags in the processor status register php ;save status php ;use stack to retrieve status pla +cmp_flag arg1 +trap_ne plp ;restore status } !macro tst_a arg1,arg2 { ;testing result in accu & flags php ;save flags php cmp #arg1 ;test result +trap_ne pla ;load status +cmp_flag arg2 +trap_ne plp ;restore status } !macro tst_as arg1,arg2 { ;testing result in accu & flags, save accu pha php ;save flags php cmp #arg1 ;test result +trap_ne pla ;load status +cmp_flag arg2 +trap_ne plp ;restore status pla } !macro tst_x arg1,arg2 { ;testing result in x index & flags php ;save flags php cpx #arg1 ;test result +trap_ne pla ;load status +cmp_flag arg2 +trap_ne plp ;restore status } !macro tst_y arg1,arg2 { ;testing result in y index & flags php ;save flags php cpy #arg1 ;test result +trap_ne pla ;load status +cmp_flag arg2 +trap_ne plp ;restore status } !macro tst_ax arg1,arg2,arg3 { ;indexed testing result in accu & flags php ;save flags cmp arg1,x ;test result +trap_ne pla ;load status +eor_flag arg3 cmp arg2,x ;test flags +trap_ne ; } !macro tst_ay arg1,arg2,arg3 { ;indexed testing result in accu & flags php ;save flags cmp arg1,y ;test result +trap_ne ; pla ;load status +eor_flag arg3 cmp arg2,y ;test flags +trap_ne } !macro tst_z arg1,arg2,arg3 { ;indexed testing result in zp & flags php ;save flags lda zpt cmp arg1,x ;test result +trap_ne pla ;load status +eor_flag arg3 cmp arg2,x ;test flags +trap_ne } !macro tst_zx arg1,arg2,arg3 { ;testing result in zp,x & flags php ;save flags lda zpt,x cmp arg1,x ;test result +trap_ne pla ;load status +eor_flag arg3 cmp arg2,x ;test flags +trap_ne } !macro tst_abs arg1,arg2,arg3 { ;indexed testing result in memory & flags php ;save flags lda abst cmp arg1,x ;test result +trap_ne pla ;load status +eor_flag arg3 cmp arg2,x ;test flags +trap_ne } !macro tst_absx arg1,arg2,arg3 { ;testing result in abs,x & flags php ;save flags lda abst,x cmp arg1,x ;test result +trap_ne pla ;load status +eor_flag arg3 cmp arg2,x ;test flags +trap_ne } ; RAM integrity test ; verifies that none of the previous tests has altered RAM outside of the ; designated write areas. ; uses zpt word as indirect pointer, zpt+2 word as checksum !if ram_top > -1 { !macro check_ram { cld lda #0 sta zpt ;set low byte of indirect pointer sta zpt+3 ;checksum high byte ldx #11 ;reset modifiable RAM ccs1\? sta jxi_tab,x ;JMP indirect page cross area dex bpl ccs1\? sta chkdadi ;self modifying code sta chkdsbi clc ldx #zp_bss-zero_page ;zeropage - write test area ccs3\? adc zero_page,x bcc ccs2\? inc zpt+3 ;carry to high byte clc ccs2\? inx bne ccs3\? ldx #>(data_segment) ;set high byte of indirect pointer stx zpt+1 ldy #<(data_bss) ;data after write test area ccs5\? adc (zpt),y bcc ccs4\? inc zpt+3 ;carry to high byte clc ccs4\? iny bne ccs5\? inx ;advance RAM high address stx zpt+1 cpx #ram_top bne ccs5\? sta zpt+2 ;checksum low is cmp ram_chksm ;checksum low expected +trap_ne ;checksum mismatch lda zpt+3 ;checksum high is cmp ram_chksm+1 ;checksum high expected +trap_ne ;checksum mismatch } } else { !macro check_ram { ;RAM check disabled - RAM size not set } } !macro next_test { ;make sure, tests don't jump the fence ; lda test_case ;previous test ; cmp #test_num ; +trap_ne ;test is out of sequence ;test_num = test_num + 1 ; lda #test_num ;*** next tests' number ; sta test_case ;check_ram ;uncomment to find altered RAM after each test inc test_case lda test_case sta $7e7 } !if load_data_direct = 1 { ;data } else { ;bss ;uninitialized segment, copy of data at end of code! } * = data_segment pg_x !byte 0,0 ;high JMP indirect address for page cross bug test_case !byte 0 ;current test number ram_chksm !byte 0,0 ;checksum for RAM integrity test ;add/subtract operand copy - abs tests write area abst ;5 bytes store/modify test area ada2 !byte 0 ;operand 2 sba2 !byte 0 ;operand 2 complemented for subtract !byte 0,0,0 ;fill remaining bytes data_bss abs1 !byte $c3,$82,$41,0 ;test patterns for LDx BIT ROL ROR ASL LSR abs7f !byte $7f ;test pattern for compare ;loads fLDx !byte fn,fn,0,fz ;expected flags for load ;shifts rASL ;expected result ASL & ROL -carry rROL !byte $86,$04,$82,0 ; " rROLc !byte $87,$05,$83,1 ;expected result ROL +carry rLSR ;expected result LSR & ROR -carry rROR !byte $61,$41,$20,0 ; " rRORc !byte $e1,$c1,$a0,$80 ;expected result ROR +carry fASL ;expected flags for shifts fROL !byte fnc,fc,fn,fz ;no carry in fROLc !byte fnc,fc,fn,0 ;carry in fLSR fROR !byte fc,0,fc,fz ;no carry in fRORc !byte fnc,fn,fnc,fn ;carry in ;increments (decrements) rINC !byte $7f,$80,$ff,0,1 ;expected result for INC/DEC fINC !byte 0,fn,fn,fz,0 ;expected flags for INC/DEC ;logical memory operand absOR !byte 0,$1f,$71,$80 ;test pattern for OR absAN !byte $0f,$ff,$7f,$80 ;test pattern for AND absEO !byte $ff,$0f,$8f,$8f ;test pattern for EOR ;logical accu operand absORa !byte 0,$f1,$1f,0 ;test pattern for OR absANa !byte $f0,$ff,$ff,$ff ;test pattern for AND absEOa !byte $ff,$f0,$f0,$0f ;test pattern for EOR ;logical results absrlo !byte 0,$ff,$7f,$80 absflo !byte fz,fn,0,fn data_bss_end ;define area for page crossing JMP (abs) & JMP (abs,x) test jxi_tab = data_segment + $100 - 7 ;JMP (jxi_tab,x) x=6 ji_tab = data_segment + $100 - 3 ;JMP (ji_tab+2) jxp_tab = data_segment + $100 ;JMP (jxp_tab-255) x=255 !pseudopc zero_page { !if I_flag = 2 { ;masking for I bit in status flag_I_on !byte 0 ;or mask to load flags flag_I_off !byte 0 ;and mask to load flags } zpt ;5 bytes store/modify test area ;add/subtract operand generation and result/flag prediction adfc !byte 0 ;carry flag before op ad1 !byte 0 ;operand 1 - accumulator ad2 !byte 0 ;operand 2 - memory / immediate adrl !byte 0 ;expected result bits 0-7 adrh !byte 0 ;expected result bit 8 (carry) adrf !byte 0 ;expected flags NV0000ZC (-V in decimal mode) sb2 !byte 0 ;operand 2 complemented for subtract zp_bss zp1 !byte $c3,$82,$41,0 ;test patterns for LDx BIT ROL ROR ASL LSR zp7f !byte $7f ;test pattern for compare ;logical zeropage operands zpOR !byte 0,$1f,$71,$80 ;test pattern for OR zpAN !byte $0f,$ff,$7f,$80 ;test pattern for AND zpEO !byte $ff,$0f,$8f,$8f ;test pattern for EOR ;indirect addressing pointers ind1 !word abs1 ;indirect pointer to pattern in absolute memory !word abs1+1 !word abs1+2 !word abs1+3 !word abs7f inw1 !word abs1-$f8 ;indirect pointer for wrap-test pattern indt !word abst ;indirect pointer to store area in absolute memory !word abst+1 !word abst+2 !word abst+3 inwt !word abst-$f8 ;indirect pointer for wrap-test store indAN !word absAN ;indirect pointer to AND pattern in absolute memory !word absAN+1 !word absAN+2 !word absAN+3 indEO !word absEO ;indirect pointer to EOR pattern in absolute memory !word absEO+1 !word absEO+2 !word absEO+3 indOR !word absOR ;indirect pointer to OR pattern in absolute memory !word absOR+1 !word absOR+2 !word absOR+3 ;add/subtract indirect pointers adi2 !word ada2 ;indirect pointer to operand 2 in absolute memory sbi2 !word sba2 ;indirect pointer to complemented operand 2 (SBC) adiy2 !word ada2-$ff ;with offset for indirect indexed sbiy2 !word sba2-$ff zp_bss_end } *=$0801 !word bend !word 10 !byte $9e !text "2061", 0 bend: !word 0 jmp start ;code * = code_segment start sei lda #$17 sta $d018 lda #$7f sta $dc0d lda $dc0d cld ldx #$ff txs lda #0 ;*** test 0 = initialize sta test_case test_num = 0 ;stop interrupts before initializing BSS !if I_flag = 1 { sei } ;initialize I/O for report channel !if report = 1 { jsr report_init } ;initialize BSS segment !if load_data_direct != 1 { ldx #zp_end-zp_init-1 ld_zp lda zp_init,x sta zp_bss,x dex bpl ld_zp ldx #data_end-data_init-1 ld_data lda data_init,x sta data_bss,x dex bpl ld_data !if ROM_vectors = 1 { ldx #5 ld_vect lda vec_init,x sta vec_bss,x dex bpl ld_vect } } ;retain status of interrupt flag !if I_flag = 2 { php pla and #4 ;isolate flag sta flag_I_on ;or mask eor #<(!4) ;reverse sta flag_I_off ;and mask } ;generate checksum for RAM integrity test !if ram_top > -1 { lda #0 sta zpt ;set low byte of indirect pointer sta ram_chksm+1 ;checksum high byte ldx #11 ;reset modifiable RAM gcs1 sta jxi_tab,x ;JMP indirect page cross area dex bpl gcs1 sta chkdadi ;self modifying code sta chkdsbi clc ldx #zp_bss-zero_page ;zeropage - write test area gcs3 adc zero_page,x bcc gcs2 inc ram_chksm+1 ;carry to high byte clc gcs2 inx bne gcs3 ldx #>(data_segment) ;set high byte of indirect pointer stx zpt+1 ldy #<(data_bss) ;data after write test area gcs5 adc (zpt),y bcc gcs4 inc ram_chksm+1 ;carry to high byte clc gcs4 iny bne gcs5 inx ;advance RAM high address stx zpt+1 cpx #ram_top bne gcs5 sta ram_chksm ;checksum complete } +next_test ;testing stack operations PHX PHY PLX PLY lda #$99 ;protect a ldx #$ff ;initialize stack txs ldx #$55 phx ldx #$aa phx cpx $1fe ;on stack ? +trap_ne tsx cpx #$fd ;sp decremented? +trap_ne ply cpy #$aa ;successful retreived from stack? +trap_ne ply cpy #$55 +trap_ne cpy $1ff ;remains on stack? +trap_ne tsx cpx #$ff ;sp incremented? +trap_ne ldy #$a5 phy ldy #$5a phy cpy $1fe ;on stack ? +trap_ne tsx cpx #$fd ;sp decremented? +trap_ne plx cpx #$5a ;successful retreived from stack? +trap_ne plx cpx #$a5 +trap_ne cpx $1ff ;remains on stack? +trap_ne tsx cpx #$ff ;sp incremented? +trap_ne cmp #$99 ;unchanged? +trap_ne +next_test ; test PHX does not alter flags or X but PLX does ldy #$aa ;protect y +set_x 1,$ff ;push phx +tst_x 1,$ff +set_x 0,0 phx +tst_x 0,0 +set_x $ff,$ff phx +tst_x $ff,$ff +set_x 1,0 phx +tst_x 1,0 +set_x 0,$ff phx +tst_x 0,$ff +set_x $ff,0 phx +tst_x $ff,0 +set_x 0,$ff ;pull plx +tst_x $ff,$ff-zero +set_x $ff,0 plx +tst_x 0,zero +set_x $fe,$ff plx +tst_x 1,$ff-zero-minus +set_x 0,0 plx +tst_x $ff,minus +set_x $ff,$ff plx +tst_x 0,$ff-minus +set_x $fe,0 plx +tst_x 1,0 cpy #$aa ;Y unchanged +trap_ne +next_test ; test PHY does not alter flags or Y but PLY does ldx #$55 ;x & a protected +set_y 1,$ff ;push phy +tst_y 1,$ff +set_y 0,0 phy +tst_y 0,0 +set_y $ff,$ff phy +tst_y $ff,$ff +set_y 1,0 phy +tst_y 1,0 +set_y 0,$ff phy +tst_y 0,$ff +set_y $ff,0 phy +tst_y $ff,0 +set_y 0,$ff ;pull ply +tst_y $ff,$ff-zero +set_y $ff,0 ply +tst_y 0,zero +set_y $fe,$ff ply +tst_y 1,$ff-zero-minus +set_y 0,0 ply +tst_y $ff,minus +set_y $ff,$ff ply +tst_y 0,$ff-minus +set_y $fe,0 ply +tst_y 1,0 cpx #$55 ;x unchanged? +trap_ne +next_test ; PC modifying instructions (BRA, BBR, BBS, 1, 2, 3 byte NOPs, JMP(abs,x)) ; testing unconditional branch BRA ldx #$81 ;protect unused registers ldy #$7e +set_a 0,$ff bra br1 ;branch should always be taken +trap br1 +tst_a 0,$ff +set_a $ff,0 bra br2 ;branch should always be taken +trap br2 +tst_a $ff,0 cpx #$81 +trap_ne cpy #$7e +trap_ne +next_test ldy #0 ;branch range test bra bra0 bra1 cpy #1 +trap_ne ;long range backward iny bra bra2 bra3 cpy #3 +trap_ne ;long range backward iny bra bra4 bra5 cpy #5 +trap_ne ;long range backward iny ldy #0 bra brf0 iny iny iny iny brf0 bra brf1 iny iny iny brf1 iny bra brf2 iny iny brf2 iny iny bra brf3 iny brf3 iny iny iny bra brf4 brf4 iny iny iny iny cpy #10 +trap_ne ;short range forward bra brb0 brb4 dey dey dey dey bra brb5 brb3 dey dey dey bra brb4 brb2 dey dey bra brb3 brb1 dey bra brb2 brb0 bra brb1 brb5 cpy #0 +trap_ne ;short range backward bra bra6 bra4 cpy #4 +trap_ne ;long range forward iny bra bra5 bra2 cpy #2 +trap_ne ;long range forward iny bra bra3 bra0 cpy #0 +trap_ne ;long range forward iny bra bra1 bra6 +next_test !if rkwl_wdc_op = 1 { ; testing BBR & BBS !macro bbt arg1 { ;\1 = bitnum lda #(1<(ji_px) ;high address if page cross bug sta pg_x +set_stat 0 lda #'I' ldx #'N' ldy #'D' ;N=0, V=0, Z=0, C=0 jmp (ji_tab) nop +trap_ne ;runover protection dey dey ji_ret php ;either SP or Y count will fail, if we do not hit dey dey dey plp +trap_eq ;returned flags OK? +trap_pl +trap_cc +trap_vc cmp #('I' xor $aa) ;returned registers OK? +trap_ne cpx #('N'+1) +trap_ne cpy #('D'-6) +trap_ne tsx ;SP check cpx #$ff +trap_ne +next_test ; jump indexed indirect ldx #11 ;prepare table jxi1 lda jxi_adr,x sta jxi_tab,x dex bpl jxi1 lda #>(jxi_px) ;high address if page cross bug sta pg_x +set_stat 0 lda #'X' ldx #4 ldy #'I' ;N=0, V=0, Z=0, C=0 jmp (jxi_tab,x) nop +trap_ne ;runover protection dey dey jxi_ret php ;either SP or Y count will fail, if we do not hit dey dey dey plp +trap_eq ;returned flags OK? +trap_pl +trap_cc +trap_vc cmp #('X'^$aa) ;returned registers OK? +trap_ne cpx #6 +trap_ne cpy #('I'-6) +trap_ne tsx ;SP check cpx #$ff +trap_ne lda #<(jxp_ok) ;test with index causing a page cross sta jxp_tab lda #>(jxp_ok) sta jxp_tab+1 lda #<(jxp_px) sta pg_x lda #>(jxp_px) sta pg_x+1 ldx #$ff jmp (jxp_tab-$ff,x) jxp_px +trap ;page cross by index to wrong page jxp_ok +next_test !if ROM_vectors = 1 { ; test BRK clears decimal mode sed brk nop brk_ret +next_test } ; testing accumulator increment/decrement INC A & DEC A ldx #$ac ;protect x & y ldy #$dc +set_a $fe,$ff inc ;ff +tst_as $ff,$ff-zero inc ;00 +tst_as 0,$ff-minus inc ;01 +tst_as 1,$ff-minus-zero dec ;00 +tst_as 0,$ff-minus dec ;ff +tst_as $ff,$ff-zero dec ;fe +set_a $fe,0 inc ;ff +tst_as $ff,minus inc ;00 +tst_as 0,zero inc ;01 +tst_as 1,0 dec ;00 +tst_as 0,zero dec ;ff +tst_as $ff,minus cpx #$ac +trap_ne ;x altered during test cpy #$dc +trap_ne ;y altered during test tsx cpx #$ff +trap_ne ;sp push/pop mismatch +next_test ; testing load / store accumulator LDA / STA (zp) ldx #$99 ;protect x & y ldy #$66 +set_stat 0 lda (ind1) php ;test stores do not alter flags eor #$c3 plp sta (indt) php ;flags after load/store sequence eor #$c3 cmp #$c3 ;test result +trap_ne pla ;load status +eor_flag 0 cmp fLDx ;test flags +trap_ne +set_stat 0 lda (ind1+2) php ;test stores do not alter flags eor #$c3 plp sta (indt+2) php ;flags after load/store sequence eor #$c3 cmp #$82 ;test result +trap_ne pla ;load status +eor_flag 0 cmp fLDx+1 ;test flags +trap_ne +set_stat 0 lda (ind1+4) php ;test stores do not alter flags eor #$c3 plp sta (indt+4) php ;flags after load/store sequence eor #$c3 cmp #$41 ;test result +trap_ne pla ;load status +eor_flag 0 cmp fLDx+2 ;test flags +trap_ne +set_stat 0 lda (ind1+6) php ;test stores do not alter flags eor #$c3 plp sta (indt+6) php ;flags after load/store sequence eor #$c3 cmp #0 ;test result +trap_ne pla ;load status +eor_flag 0 cmp fLDx+3 ;test flags +trap_ne cpx #$99 +trap_ne ;x altered during test cpy #$66 +trap_ne ;y altered during test ldy #3 ;testing store result ldx #0 tstai1 lda abst,y eor #$c3 cmp abs1,y +trap_ne ;store to indirect data txa sta abst,y ;clear dey bpl tstai1 ldx #$99 ;protect x & y ldy #$66 +set_stat $ff lda (ind1) php ;test stores do not alter flags eor #$c3 plp sta (indt) php ;flags after load/store sequence eor #$c3 cmp #$c3 ;test result +trap_ne pla ;load status +eor_flag (brk_ret) +trap_ne ;unexpected BRK jmp brk_ret } else { +trap_ne ;check stack for conditions at BRK } !if report = 1 { !src "report.asm" } ;copy of data to initialize BSS segment !if load_data_direct != 1 { zp_init zp1_ !byte $c3,$82,$41,0 ;test patterns for LDx BIT ROL ROR ASL LSR zp7f_ !byte $7f ;test pattern for compare ;logical zeropage operands zpOR_ !byte 0,$1f,$71,$80 ;test pattern for OR zpAN_ !byte $0f,$ff,$7f,$80 ;test pattern for AND zpEO_ !byte $ff,$0f,$8f,$8f ;test pattern for EOR ;indirect addressing pointers ind1_ !word abs1 ;indirect pointer to pattern in absolute memory !word abs1+1 !word abs1+2 !word abs1+3 !word abs7f inw1_ !word abs1-$f8 ;indirect pointer for wrap-test pattern indt_ !word abst ;indirect pointer to store area in absolute memory !word abst+1 !word abst+2 !word abst+3 inwt_ !word abst-$f8 ;indirect pointer for wrap-test store indAN_ !word absAN ;indirect pointer to AND pattern in absolute memory !word absAN+1 !word absAN+2 !word absAN+3 indEO_ !word absEO ;indirect pointer to EOR pattern in absolute memory !word absEO+1 !word absEO+2 !word absEO+3 indOR_ !word absOR ;indirect pointer to OR pattern in absolute memory !word absOR+1 !word absOR+2 !word absOR+3 ;add/subtract indirect pointers adi2_ !word ada2 ;indirect pointer to operand 2 in absolute memory sbi2_ !word sba2 ;indirect pointer to complemented operand 2 (SBC) adiy2_ !word ada2-$ff ;with offset for indirect indexed sbiy2_ !word sba2-$ff zp_end !if (zp_end - zp_init) != (zp_bss_end - zp_bss) { ;force assembler error if size is different ERROR ERROR ERROR ;mismatch between bss and zeropage data } data_init abs1_ !byte $c3,$82,$41,0 ;test patterns for LDx BIT ROL ROR ASL LSR abs7f_ !byte $7f ;test pattern for compare ;loads fLDx_ !byte fn,fn,0,fz ;expected flags for load ;shifts rASL_ ;expected result ASL & ROL -carry rROL_ !byte $86,$04,$82,0 ; " rROLc_ !byte $87,$05,$83,1 ;expected result ROL +carry rLSR_ ;expected result LSR & ROR -carry rROR_ !byte $61,$41,$20,0 ; " rRORc_ !byte $e1,$c1,$a0,$80 ;expected result ROR +carry fASL_ ;expected flags for shifts fROL_ !byte fnc,fc,fn,fz ;no carry in fROLc_ !byte fnc,fc,fn,0 ;carry in fLSR_ fROR_ !byte fc,0,fc,fz ;no carry in fRORc_ !byte fnc,fn,fnc,fn ;carry in ;increments (decrements) rINC_ !byte $7f,$80,$ff,0,1 ;expected result for INC/DEC fINC_ !byte 0,fn,fn,fz,0 ;expected flags for INC/DEC ;logical memory operand absOR_ !byte 0,$1f,$71,$80 ;test pattern for OR absAN_ !byte $0f,$ff,$7f,$80 ;test pattern for AND absEO_ !byte $ff,$0f,$8f,$8f ;test pattern for EOR ;logical accu operand absORa_ !byte 0,$f1,$1f,0 ;test pattern for OR absANa_ !byte $f0,$ff,$ff,$ff ;test pattern for AND absEOa_ !byte $ff,$f0,$f0,$0f ;test pattern for EOR ;logical results absrlo_ !byte 0,$ff,$7f,$80 absflo_ !byte fz,fn,0,fn data_end !if (data_end - data_init) != (data_bss_end - data_bss) { ;force assembler error if size is different ERROR ERROR ERROR ;mismatch between bss and data } vec_init !word nmi_trap !word res_trap !word irq_trap vec_bss = $fffa } ;end of RAM init data ; code at end of image due to the need to add blank space as required !if ($ff & (ji_ret - * - 2)) < ($ff & (jxi_ret - * - 2)) { ; JMP (abs) when $xxff and $xx00 are from same page ;ds <(ji_ret - * - 2) !byte <(ji_ret - * - 2) nop nop ji_px nop ;low address byte matched with ji_ret nop +trap ;jmp indirect page cross bug ; JMP (abs,x) when $xxff and $xx00 are from same page ;ds <(jxi_ret - * - 2) !byte <(jxi_ret - * - 2) nop nop jxi_px nop ;low address byte matched with jxi_ret nop +trap ;jmp indexed indirect page cross bug } else { ; JMP (abs,x) when $xxff and $xx00 are from same page ;ds <(jxi_ret - * - 2) !byte <(jxi_ret - * - 2) nop nop jxi_px nop ;low address byte matched with jxi_ret nop +trap ;jmp indexed indirect page cross bug ; JMP (abs) when $xxff and $xx00 are from same page ;ds <(ji_ret - * - 2) !byte <(ji_ret - * - 2) nop nop ji_px nop ;low address byte matched with ji_ret nop +trap ;jmp indirect page cross bug } !if (load_data_direct = 1) & (ROM_vectors = 1) { * = $fffa ;vectors !word nmi_trap !word res_trap !word irq_trap }