diff options
| author | vin <git@vineetk.net> | 2024-06-29 09:51:49 -0400 |
|---|---|---|
| committer | vin <git@vineetk.net> | 2024-06-29 09:51:49 -0400 |
| commit | 4b7fae00103c9fbaeeba45a78a7d0c910d243dc0 (patch) | |
| tree | 161569839a9b8eb9f1d6598731e05121eef77f51 | |
| parent | 5d36aeb8856fa17d491b0c8c18d6593a9ec4997a (diff) | |
fix status flag ordering shenanigans
| -rw-r--r-- | cpu.c | 56 |
1 files changed, 33 insertions, 23 deletions
| @@ -326,13 +326,16 @@ brk(void) | |||
| 326 | static void | 326 | static void |
| 327 | bvc(uint8_t arg) | 327 | bvc(uint8_t arg) |
| 328 | { | 328 | { |
| 329 | if (regs.status.overflow == 0) | 329 | //regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) == 0; |
| 330 | //if (regs.status.overflow == 0) | ||
| 331 | if ((STATUS_TO_INT() & (1 << 6)) == 0) | ||
| 330 | regs.pc += arg; | 332 | regs.pc += arg; |
| 331 | } | 333 | } |
| 332 | 334 | ||
| 333 | static void | 335 | static void |
| 334 | bvs(uint8_t arg) | 336 | bvs(uint8_t arg) |
| 335 | { | 337 | { |
| 338 | regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) != 0; | ||
| 336 | if (regs.status.overflow == 1) | 339 | if (regs.status.overflow == 1) |
| 337 | regs.pc += arg; | 340 | regs.pc += arg; |
| 338 | } | 341 | } |
| @@ -559,11 +562,7 @@ pha(void) | |||
| 559 | static void | 562 | static void |
| 560 | php(void) | 563 | php(void) |
| 561 | { | 564 | { |
| 562 | uint8_t status; | 565 | PUSH(STATUS_TO_INT() | (1 << 4)); |
| 563 | |||
| 564 | status = STATUS_TO_INT(); | ||
| 565 | |||
| 566 | PUSH(status); | ||
| 567 | } | 566 | } |
| 568 | 567 | ||
| 569 | static void | 568 | static void |
| @@ -582,14 +581,14 @@ plp(void) | |||
| 582 | 581 | ||
| 583 | status = PULL(); | 582 | status = PULL(); |
| 584 | 583 | ||
| 585 | regs.status.carry = (status & (1 << 7)) != 0; | 584 | regs.status.carry = (status & 1) != 0; |
| 586 | regs.status.zero = (status & (1 << 6)) != 0; | 585 | regs.status.zero = (status & (1 << 1)) != 0; |
| 587 | regs.status.interrupt_disable = (status & (1 << 5)) != 0; | 586 | regs.status.interrupt_disable = (status & (1 << 2)) != 0; |
| 588 | regs.status.decimal_mode = (status & (1 << 4)) != 0; | 587 | regs.status.decimal_mode = (status & (1 << 3)) != 0; |
| 589 | regs.status.brk = (status & (1 << 3)) != 0; | 588 | regs.status.brk = 0; |
| 590 | regs.status.unused = (status & (1 << 2)) != 0; | 589 | regs.status.unused = 1; |
| 591 | regs.status.overflow = (status & (1 << 1)) != 0; | 590 | regs.status.overflow = (status & (1 << 6)) != 0; |
| 592 | regs.status.negative = (status & 1) != 0; | 591 | regs.status.negative = (status & (1 << 7)) != 0; |
| 593 | } | 592 | } |
| 594 | 593 | ||
| 595 | static void | 594 | static void |
| @@ -771,12 +770,14 @@ interpret(void) | |||
| 771 | uint16_t arg, mem, deref; | 770 | uint16_t arg, mem, deref; |
| 772 | uint32_t cycles_; | 771 | uint32_t cycles_; |
| 773 | enum addressing_mode mode; | 772 | enum addressing_mode mode; |
| 773 | struct registers regs_; | ||
| 774 | 774 | ||
| 775 | for (;;) { | 775 | for (;;) { |
| 776 | opcode = peek(regs.pc++); | 776 | opcode = peek(regs.pc++); |
| 777 | cycles_ = cycles; | 777 | cycles_ = cycles; |
| 778 | did_memwrite = 0; | 778 | did_memwrite = 0; |
| 779 | status = STATUS_TO_INT(); | 779 | status = STATUS_TO_INT(); |
| 780 | regs_ = regs; | ||
| 780 | 781 | ||
| 781 | printf("%04X %02X", regs.pc - 1, opcode); | 782 | printf("%04X %02X", regs.pc - 1, opcode); |
| 782 | 783 | ||
| @@ -928,23 +929,26 @@ interpret(void) | |||
| 928 | printf("ASL"); | 929 | printf("ASL"); |
| 929 | break; | 930 | break; |
| 930 | case 0x90: | 931 | case 0x90: |
| 931 | mode = AM_IMM; | 932 | mode = AM_REL; |
| 932 | arg = opcode_arg(mode); | 933 | arg = opcode_arg(mode); |
| 933 | bcc(arg); | 934 | bcc(arg); |
| 935 | arg += regs_.pc + 1; | ||
| 934 | cycles += 2; | 936 | cycles += 2; |
| 935 | printf("BCC"); | 937 | printf("BCC"); |
| 936 | break; | 938 | break; |
| 937 | case 0xb0: | 939 | case 0xb0: |
| 938 | mode = AM_IMM; | 940 | mode = AM_REL; |
| 939 | arg = opcode_arg(mode); | 941 | arg = opcode_arg(mode); |
| 940 | bcs(arg); | 942 | bcs(arg); |
| 943 | arg += regs_.pc + 1; | ||
| 941 | cycles += 2; | 944 | cycles += 2; |
| 942 | printf("BCS"); | 945 | printf("BCS"); |
| 943 | break; | 946 | break; |
| 944 | case 0xf0: | 947 | case 0xf0: |
| 945 | mode = AM_IMM; | 948 | mode = AM_REL; |
| 946 | arg = opcode_arg(mode); | 949 | arg = opcode_arg(mode); |
| 947 | beq(arg); | 950 | beq(arg); |
| 951 | arg += regs_.pc + 1; | ||
| 948 | cycles += 2; | 952 | cycles += 2; |
| 949 | printf("BEQ"); | 953 | printf("BEQ"); |
| 950 | break; | 954 | break; |
| @@ -989,23 +993,26 @@ interpret(void) | |||
| 989 | printf("BIT"); | 993 | printf("BIT"); |
| 990 | break; | 994 | break; |
| 991 | case 0x30: | 995 | case 0x30: |
| 992 | mode = AM_IMM; | 996 | mode = AM_REL; |
| 993 | arg = opcode_arg(mode); | 997 | arg = opcode_arg(mode); |
| 994 | bmi(arg); | 998 | bmi(arg); |
| 999 | arg += regs_.pc + 1; | ||
| 995 | cycles += 2; | 1000 | cycles += 2; |
| 996 | printf("BMI"); | 1001 | printf("BMI"); |
| 997 | break; | 1002 | break; |
| 998 | case 0xd0: | 1003 | case 0xd0: |
| 999 | mode = AM_IMM; | 1004 | mode = AM_REL; |
| 1000 | arg = opcode_arg(mode); | 1005 | arg = opcode_arg(mode); |
| 1001 | bne(arg); | 1006 | bne(arg); |
| 1007 | arg += regs_.pc + 1; | ||
| 1002 | cycles += 2; | 1008 | cycles += 2; |
| 1003 | printf("BNE"); | 1009 | printf("BNE"); |
| 1004 | break; | 1010 | break; |
| 1005 | case 0x10: | 1011 | case 0x10: |
| 1006 | mode = AM_IMM; | 1012 | mode = AM_REL; |
| 1007 | arg = opcode_arg(mode); | 1013 | arg = opcode_arg(mode); |
| 1008 | bpl(arg); | 1014 | bpl(arg); |
| 1015 | arg += regs_.pc + 1; | ||
| 1009 | cycles += 2; | 1016 | cycles += 2; |
| 1010 | printf("BPL"); | 1017 | printf("BPL"); |
| 1011 | break; | 1018 | break; |
| @@ -1016,16 +1023,18 @@ interpret(void) | |||
| 1016 | printf("\tBRK"); | 1023 | printf("\tBRK"); |
| 1017 | goto loop_exit; | 1024 | goto loop_exit; |
| 1018 | case 0x50: | 1025 | case 0x50: |
| 1019 | mode = AM_IMM; | 1026 | mode = AM_REL; |
| 1020 | arg = opcode_arg(mode); | 1027 | arg = opcode_arg(mode); |
| 1021 | bvc(arg); | 1028 | bvc(arg); |
| 1029 | arg += regs_.pc + 1; | ||
| 1022 | cycles += 2; | 1030 | cycles += 2; |
| 1023 | printf("BVC"); | 1031 | printf("BVC"); |
| 1024 | break; | 1032 | break; |
| 1025 | case 0x70: | 1033 | case 0x70: |
| 1026 | mode = AM_IMM; | 1034 | mode = AM_REL; |
| 1027 | arg = opcode_arg(mode); | 1035 | arg = opcode_arg(mode); |
| 1028 | bvs(arg); | 1036 | bvs(arg); |
| 1037 | arg += regs_.pc + 1; | ||
| 1029 | cycles += 2; | 1038 | cycles += 2; |
| 1030 | printf("BVS"); | 1039 | printf("BVS"); |
| 1031 | break; | 1040 | break; |
| @@ -1884,6 +1893,7 @@ interpret(void) | |||
| 1884 | if (did_memwrite) printf(" = %02X\t\t\t", ret); | 1893 | if (did_memwrite) printf(" = %02X\t\t\t", ret); |
| 1885 | else printf("\t\t\t\t"); | 1894 | else printf("\t\t\t\t"); |
| 1886 | break; | 1895 | break; |
| 1896 | case AM_REL: | ||
| 1887 | case AM_ABS: | 1897 | case AM_ABS: |
| 1888 | printf(" $%04X", arg); | 1898 | printf(" $%04X", arg); |
| 1889 | if (did_memwrite) printf(" = %02X\t\t\t", ret); | 1899 | if (did_memwrite) printf(" = %02X\t\t\t", ret); |
| @@ -1910,7 +1920,7 @@ interpret(void) | |||
| 1910 | } | 1920 | } |
| 1911 | 1921 | ||
| 1912 | printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n", | 1922 | printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n", |
| 1913 | regs.a, regs.x, regs.y, status, regs.sp, cycles_); | 1923 | regs_.a, regs_.x, regs_.y, status, regs_.sp, cycles_); |
| 1914 | } | 1924 | } |
| 1915 | loop_exit: | 1925 | loop_exit: |
| 1916 | } | 1926 | } |
