From 28ab2c9f38aeb9178e424fdc35ec8ffaaed22527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20V=C3=A9zina?= Date: Mon, 28 Apr 2025 13:46:40 -0400 Subject: [PATCH 01/19] MMC1 512K PRG support Added new banks to prg-rom-banked.ld and CHR register bit sets to set_prg_bank --- mos-platform/nes-mmc1/mapper.s | 23 +++++++ mos-platform/nes-mmc1/prg-rom-banked.ld | 84 ++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 8774e69a8..9bfca72a4 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -101,6 +101,29 @@ __set_prg_bank: set_prg_bank: tay .Lset: + lda __PRG_BANK ; Load the value of __PRG_BANK into the accumulator + cmp #16 ; Compare it with 16 + bcc set_bits_to_0 ; If __PRG_BANK is less than 16, branch to set_bits_to_0 + +set_bits_to_1: + lda __CHR_BANK0 ; Load the value of __CHR_BANK0 + ora #%00010000 ; Set the 4th bit + sta __CHR_BANK0 ; Store the updated value back + + lda _CHR_BANK1 ; Load the value of _CHR_BANK1 + ora #%00010000 ; Set the 4th bit + sta _CHR_BANK1 ; Store the updated value back + rts ; Return from subroutine + +set_bits_to_0: + lda __CHR_BANK0 ; Load the value of __CHR_BANK0 + and #%11101111 ; Clear the 4th bit + sta __CHR_BANK0 ; Store the updated value back + + lda _CHR_BANK1 ; Load the value of _CHR_BANK1 + and #%11101111 ; Clear the 4th bit + sta _CHR_BANK1 ; Store the updated value back + inc __reset_mmc1_byte ldx #1 stx _IN_PROGRESS diff --git a/mos-platform/nes-mmc1/prg-rom-banked.ld b/mos-platform/nes-mmc1/prg-rom-banked.ld index 42e6af847..409ebd372 100644 --- a/mos-platform/nes-mmc1/prg-rom-banked.ld +++ b/mos-platform/nes-mmc1/prg-rom-banked.ld @@ -15,6 +15,22 @@ __prg_rom_11_lma = 0x0b8000; __prg_rom_12_lma = 0x0c8000; __prg_rom_13_lma = 0x0d8000; __prg_rom_14_lma = 0x0e8000; +__prg_rom_15_lma = 0x0f8000; +__prg_rom_16_lma = 0x108000; +__prg_rom_17_lma = 0x118000; +__prg_rom_18_lma = 0x128000; +__prg_rom_19_lma = 0x138000; +__prg_rom_20_lma = 0x148000; +__prg_rom_21_lma = 0x158000; +__prg_rom_22_lma = 0x168000; +__prg_rom_23_lma = 0x178000; +__prg_rom_24_lma = 0x188000; +__prg_rom_25_lma = 0x198000; +__prg_rom_26_lma = 0x1A8000; +__prg_rom_27_lma = 0x1B8000; +__prg_rom_28_lma = 0x1C8000; +__prg_rom_29_lma = 0x1D8000; +__prg_rom_30_lma = 0x1E8000; __prg_rom_fixed_lma = 0xc000; __prg_rom_0_offset = 0x00000; @@ -32,6 +48,22 @@ __prg_rom_11_offset = 0x2c000; __prg_rom_12_offset = 0x30000; __prg_rom_13_offset = 0x34000; __prg_rom_14_offset = 0x38000; +__prg_rom_15_offset = 0x3C000; +__prg_rom_16_offset = 0x40000; +__prg_rom_17_offset = 0x44000; +__prg_rom_18_offset = 0x48000; +__prg_rom_19_offset = 0x4c000; +__prg_rom_20_offset = 0x50000; +__prg_rom_21_offset = 0x54000; +__prg_rom_22_offset = 0x58000; +__prg_rom_23_offset = 0x5c000; +__prg_rom_24_offset = 0x60000; +__prg_rom_25_offset = 0x64000; +__prg_rom_26_offset = 0x68000; +__prg_rom_27_offset = 0x6c000; +__prg_rom_28_offset = 0x70000; +__prg_rom_29_offset = 0x74000; +__prg_rom_30_offset = 0x78000; __prg_rom_fixed_offset = __prg_rom_size * 1024 - 0x4000; MEMORY { @@ -51,6 +83,22 @@ MEMORY { prg_rom_12 : ORIGIN = __prg_rom_12_lma, LENGTH = __prg_rom_size >= 256 ? 0x4000 - 12 : 0 prg_rom_13 : ORIGIN = __prg_rom_13_lma, LENGTH = __prg_rom_size >= 256 ? 0x4000 - 12 : 0 prg_rom_14 : ORIGIN = __prg_rom_14_lma, LENGTH = __prg_rom_size >= 256 ? 0x4000 - 12 : 0 + prg_rom_15 : ORIGIN = __prg_rom_15_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_16 : ORIGIN = __prg_rom_16_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_17 : ORIGIN = __prg_rom_17_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_18 : ORIGIN = __prg_rom_18_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_19 : ORIGIN = __prg_rom_19_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_20 : ORIGIN = __prg_rom_20_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_21 : ORIGIN = __prg_rom_21_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_22 : ORIGIN = __prg_rom_22_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_23 : ORIGIN = __prg_rom_23_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_24 : ORIGIN = __prg_rom_24_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_25 : ORIGIN = __prg_rom_25_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_26 : ORIGIN = __prg_rom_26_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_27 : ORIGIN = __prg_rom_27_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_28 : ORIGIN = __prg_rom_28_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_29 : ORIGIN = __prg_rom_29_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 + prg_rom_30 : ORIGIN = __prg_rom_30_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 prg_rom_fixed : ORIGIN = __prg_rom_fixed_lma, LENGTH = 0x4000 - 12 /* Reset stub. Arbitrarily put its VMA in the first bank. */ @@ -78,6 +126,22 @@ SECTIONS { .prg_rom_12 : { *(.prg_rom_12 .prg_rom_12.*) } >prg_rom_12 .prg_rom_13 : { *(.prg_rom_13 .prg_rom_13.*) } >prg_rom_13 .prg_rom_14 : { *(.prg_rom_14 .prg_rom_14.*) } >prg_rom_14 + .prg_rom_15 : { *(.prg_rom_15 .prg_rom_15.*) } >prg_rom_15 + .prg_rom_16 : { *(.prg_rom_16 .prg_rom_16.*) } >prg_rom_16 + .prg_rom_17 : { *(.prg_rom_17 .prg_rom_17.*) } >prg_rom_17 + .prg_rom_18 : { *(.prg_rom_18 .prg_rom_18.*) } >prg_rom_18 + .prg_rom_19 : { *(.prg_rom_19 .prg_rom_19.*) } >prg_rom_19 + .prg_rom_20 : { *(.prg_rom_20 .prg_rom_20.*) } >prg_rom_20 + .prg_rom_21 : { *(.prg_rom_21 .prg_rom_21.*) } >prg_rom_21 + .prg_rom_22 : { *(.prg_rom_22 .prg_rom_22.*) } >prg_rom_22 + .prg_rom_23 : { *(.prg_rom_23 .prg_rom_23.*) } >prg_rom_23 + .prg_rom_24 : { *(.prg_rom_24 .prg_rom_24.*) } >prg_rom_24 + .prg_rom_25 : { *(.prg_rom_25 .prg_rom_25.*) } >prg_rom_25 + .prg_rom_26 : { *(.prg_rom_26 .prg_rom_26.*) } >prg_rom_26 + .prg_rom_27 : { *(.prg_rom_27 .prg_rom_27.*) } >prg_rom_27 + .prg_rom_28 : { *(.prg_rom_28 .prg_rom_28.*) } >prg_rom_28 + .prg_rom_29 : { *(.prg_rom_29 .prg_rom_29.*) } >prg_rom_29 + .prg_rom_30 : { *(.prg_rom_30 .prg_rom_30.*) } >prg_rom_30 .prg_rom_fixed : { *(.prg_rom_fixed .prg_rom_fixed.*) } >prg_rom_fixed } @@ -88,7 +152,7 @@ SECTIONS { .reset_fixed : { KEEP(*(.reset_fixed)) } >reset_fixed } -PROVIDE(__prg_rom_size = 256); +PROVIDE(__prg_rom_size = 512); OUTPUT_FORMAT { INCLUDE ines-header.ld @@ -108,5 +172,23 @@ OUTPUT_FORMAT { FULL(prg_rom_13) FULL(reset, 0, LENGTH(prg_rom_13) ? LENGTH(reset) : 0) FULL(prg_rom_14) FULL(reset, 0, LENGTH(prg_rom_14) ? LENGTH(reset) : 0) FULL(prg_rom_fixed) FULL(reset_fixed) + + FULL(prg_rom_16) FULL(reset, 0, LENGTH(prg_rom_16) ? LENGTH(reset) : 0) + FULL(prg_rom_17) FULL(reset, 0, LENGTH(prg_rom_17) ? LENGTH(reset) : 0) + FULL(prg_rom_18) FULL(reset, 0, LENGTH(prg_rom_18) ? LENGTH(reset) : 0) + FULL(prg_rom_19) FULL(reset, 0, LENGTH(prg_rom_19) ? LENGTH(reset) : 0) + FULL(prg_rom_20) FULL(reset, 0, LENGTH(prg_rom_20) ? LENGTH(reset) : 0) + FULL(prg_rom_21) FULL(reset, 0, LENGTH(prg_rom_21) ? LENGTH(reset) : 0) + FULL(prg_rom_22) FULL(reset, 0, LENGTH(prg_rom_22) ? LENGTH(reset) : 0) + FULL(prg_rom_23) FULL(reset, 0, LENGTH(prg_rom_23) ? LENGTH(reset) : 0) + FULL(prg_rom_24) FULL(reset, 0, LENGTH(prg_rom_24) ? LENGTH(reset) : 0) + FULL(prg_rom_25) FULL(reset, 0, LENGTH(prg_rom_25) ? LENGTH(reset) : 0) + FULL(prg_rom_26) FULL(reset, 0, LENGTH(prg_rom_26) ? LENGTH(reset) : 0) + FULL(prg_rom_27) FULL(reset, 0, LENGTH(prg_rom_27) ? LENGTH(reset) : 0) + FULL(prg_rom_28) FULL(reset, 0, LENGTH(prg_rom_28) ? LENGTH(reset) : 0) + FULL(prg_rom_29) FULL(reset, 0, LENGTH(prg_rom_29) ? LENGTH(reset) : 0) + FULL(prg_rom_30) FULL(reset, 0, LENGTH(prg_rom_30) ? LENGTH(reset) : 0) + FULL(prg_rom_fixed) FULL(reset_fixed) + FULL(chr_rom) } From 17b9b48019fafe1a19c064abf33abcab5897e214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20V=C3=A9zina?= Date: Mon, 28 Apr 2025 14:55:50 -0400 Subject: [PATCH 02/19] Fixed double __ in register names --- .gitignore | 1 + mos-platform/nes-mmc1/mapper.s | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9bbf4e736..c36dee92d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build/** build* *~ *.rej +.qodo diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 9bfca72a4..70a74d135 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -101,14 +101,14 @@ __set_prg_bank: set_prg_bank: tay .Lset: - lda __PRG_BANK ; Load the value of __PRG_BANK into the accumulator + lda _PRG_BANK ; Load the value of _PRG_BANK into the accumulator cmp #16 ; Compare it with 16 - bcc set_bits_to_0 ; If __PRG_BANK is less than 16, branch to set_bits_to_0 + bcc set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 set_bits_to_1: - lda __CHR_BANK0 ; Load the value of __CHR_BANK0 + lda _CHR_BANK0 ; Load the value of _CHR_BANK0 ora #%00010000 ; Set the 4th bit - sta __CHR_BANK0 ; Store the updated value back + sta _CHR_BANK0 ; Store the updated value back lda _CHR_BANK1 ; Load the value of _CHR_BANK1 ora #%00010000 ; Set the 4th bit @@ -116,9 +116,9 @@ set_bits_to_1: rts ; Return from subroutine set_bits_to_0: - lda __CHR_BANK0 ; Load the value of __CHR_BANK0 + lda _CHR_BANK0 ; Load the value of _CHR_BANK0 and #%11101111 ; Clear the 4th bit - sta __CHR_BANK0 ; Store the updated value back + sta _CHR_BANK0 ; Store the updated value back lda _CHR_BANK1 ; Load the value of _CHR_BANK1 and #%11101111 ; Clear the 4th bit From 88e2cb5cde7effbdfa6f9e362cc674b6d488eb09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20V=C3=A9zina?= Date: Mon, 28 Apr 2025 14:58:58 -0400 Subject: [PATCH 03/19] vscode plugin ignore - not needed --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index c36dee92d..9bbf4e736 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ build/** build* *~ *.rej -.qodo From a00393e59930bf71b6e8b3ccbb1011719d4b7985 Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 28 Apr 2025 17:11:37 -0400 Subject: [PATCH 04/19] skip set CHR bits if switch to bank < 16 --- mos-platform/nes-mmc1/mapper.s | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 70a74d135..34da9c0ac 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -104,7 +104,8 @@ set_prg_bank: lda _PRG_BANK ; Load the value of _PRG_BANK into the accumulator cmp #16 ; Compare it with 16 bcc set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 - + bne skip_512_prg + set_bits_to_1: lda _CHR_BANK0 ; Load the value of _CHR_BANK0 ora #%00010000 ; Set the 4th bit @@ -123,7 +124,7 @@ set_bits_to_0: lda _CHR_BANK1 ; Load the value of _CHR_BANK1 and #%11101111 ; Clear the 4th bit sta _CHR_BANK1 ; Store the updated value back - +skip_512_prg: inc __reset_mmc1_byte ldx #1 stx _IN_PROGRESS From 9993ac22a073699c1c537ca3f18100cab33aa1bf Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 28 Apr 2025 17:16:54 -0400 Subject: [PATCH 05/19] fix switching to a high bank was exiting early in the middle of the branch --- mos-platform/nes-mmc1/mapper.s | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 34da9c0ac..19963d3a6 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -104,9 +104,11 @@ set_prg_bank: lda _PRG_BANK ; Load the value of _PRG_BANK into the accumulator cmp #16 ; Compare it with 16 bcc set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 - bne skip_512_prg - -set_bits_to_1: + lda _PRG_BANK ; Load the value of _PRG_BANK into the accumulator + cmp #16 ; Compare it with 16 + bcc .set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 + +.set_bits_to_1: lda _CHR_BANK0 ; Load the value of _CHR_BANK0 ora #%00010000 ; Set the 4th bit sta _CHR_BANK0 ; Store the updated value back @@ -114,9 +116,9 @@ set_bits_to_1: lda _CHR_BANK1 ; Load the value of _CHR_BANK1 ora #%00010000 ; Set the 4th bit sta _CHR_BANK1 ; Store the updated value back - rts ; Return from subroutine + bne .continue_bankswitch -set_bits_to_0: +.set_bits_to_0: lda _CHR_BANK0 ; Load the value of _CHR_BANK0 and #%11101111 ; Clear the 4th bit sta _CHR_BANK0 ; Store the updated value back @@ -124,7 +126,8 @@ set_bits_to_0: lda _CHR_BANK1 ; Load the value of _CHR_BANK1 and #%11101111 ; Clear the 4th bit sta _CHR_BANK1 ; Store the updated value back -skip_512_prg: + +.continue_bankswitch: inc __reset_mmc1_byte ldx #1 stx _IN_PROGRESS From d11ec301ca94f428eb780b53ed5a54b1207ecdce Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 28 Apr 2025 17:23:09 -0400 Subject: [PATCH 06/19] replaced cmp/bne w and/beq --- mos-platform/nes-mmc1/mapper.s | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 19963d3a6..b8bd21db5 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -102,11 +102,8 @@ set_prg_bank: tay .Lset: lda _PRG_BANK ; Load the value of _PRG_BANK into the accumulator - cmp #16 ; Compare it with 16 - bcc set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 - lda _PRG_BANK ; Load the value of _PRG_BANK into the accumulator - cmp #16 ; Compare it with 16 - bcc .set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 + and #%00010000 ; Compare it with 16 + beq set_bits_to_0 ; If _PRG_BANK is less than 16, branch to set_bits_to_0 .set_bits_to_1: lda _CHR_BANK0 ; Load the value of _CHR_BANK0 @@ -126,7 +123,7 @@ set_prg_bank: lda _CHR_BANK1 ; Load the value of _CHR_BANK1 and #%11101111 ; Clear the 4th bit sta _CHR_BANK1 ; Store the updated value back - + .continue_bankswitch: inc __reset_mmc1_byte ldx #1 From 085fec1572396ba1a783979b777a19fad852f0f3 Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 28 Apr 2025 17:37:25 -0400 Subject: [PATCH 07/19] update actual registers --- mos-platform/nes-mmc1/mapper.s | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index b8bd21db5..e322a8b14 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -109,21 +109,25 @@ set_prg_bank: lda _CHR_BANK0 ; Load the value of _CHR_BANK0 ora #%00010000 ; Set the 4th bit sta _CHR_BANK0 ; Store the updated value back + mmc1_register_write MMC1_CHR0 lda _CHR_BANK1 ; Load the value of _CHR_BANK1 ora #%00010000 ; Set the 4th bit sta _CHR_BANK1 ; Store the updated value back + mmc1_register_write MMC1_CHR1 bne .continue_bankswitch .set_bits_to_0: lda _CHR_BANK0 ; Load the value of _CHR_BANK0 and #%11101111 ; Clear the 4th bit sta _CHR_BANK0 ; Store the updated value back + mmc1_register_write MMC1_CHR0 lda _CHR_BANK1 ; Load the value of _CHR_BANK1 and #%11101111 ; Clear the 4th bit sta _CHR_BANK1 ; Store the updated value back - + mmc1_register_write MMC1_CHR1 + .continue_bankswitch: inc __reset_mmc1_byte ldx #1 From 659a4a0714dac74115bf91ea33a7c0ab619bd8a9 Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 28 Apr 2025 18:30:19 -0400 Subject: [PATCH 08/19] nes-mmc1 prg 512 unit tests --- .gitignore | 1 + mos-platform/CMakeLists.txt | 2 +- mos-platform/nes-mmc1/mapper.s | 3 + mos-platform/nes-mmc1/prg-rom-banked.ld | 2 + test/nes-mmc1/prg-rom-512.c | 77 +++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/nes-mmc1/prg-rom-512.c diff --git a/.gitignore b/.gitignore index 9bbf4e736..c36dee92d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build/** build* *~ *.rej +.qodo diff --git a/mos-platform/CMakeLists.txt b/mos-platform/CMakeLists.txt index 5dd0b8803..b9140f79d 100644 --- a/mos-platform/CMakeLists.txt +++ b/mos-platform/CMakeLists.txt @@ -85,4 +85,4 @@ add_subdirectory(pce) add_subdirectory(pce-cd) add_subdirectory(rp6502) add_subdirectory(supervision) -add_subdirectory(vic20) +# add_subdirectory(vic20) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index e322a8b14..82b1d7b80 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -101,6 +101,9 @@ __set_prg_bank: set_prg_bank: tay .Lset: + lda #= 512 ? 1 : 0; + OUTPUT_FORMAT { INCLUDE ines-header.ld FULL(prg_rom_0) FULL(reset, 0, LENGTH(prg_rom_0) ? LENGTH(reset) : 0) diff --git a/test/nes-mmc1/prg-rom-512.c b/test/nes-mmc1/prg-rom-512.c new file mode 100644 index 000000000..e5f72f575 --- /dev/null +++ b/test/nes-mmc1/prg-rom-512.c @@ -0,0 +1,77 @@ +#include +#include +#include + +MAPPER_PRG_ROM_KB(512); + +volatile const char c[15000] = {1, [14999] = 2}; + +__attribute__((section(".prg_rom_0.rodata"))) volatile const char d[15000] = { + 3, [14999] = 4}; + +__attribute__((section(".prg_rom_14.rodata"))) volatile const char e[15000] = { + 5, [14999] = 6}; + +__attribute__((section(".prg_rom_29.rodata"))) volatile const char f[15000] = { + 7, [14999] = 8}; + + +__attribute__((noinline, section(".prg_rom_0.text"))) char bank_0_fn(void) { + asm volatile(""); + return 7; +} + +__attribute__((noinline, section(".prg_rom_14.text"))) char bank_14_fn(void) { + asm volatile(""); + return 8; +} + +__attribute__((noinline, section(".prg_rom_29.text"))) char bank_29_fn(void) { + asm volatile(""); + return 10; + } + +int main(void) { + if ((unsigned)c < 0xc000) + return EXIT_FAILURE; + if ((unsigned)d >= 0xc000) + return EXIT_FAILURE; + if ((unsigned)e >= 0xc000) + return EXIT_FAILURE; + if ((unsigned)f >= 0xc000) + return EXIT_FAILURE; + if (c[0] != 1 || c[14999] != 2) + return EXIT_FAILURE; + + set_prg_bank(0); + if (c[0] != 1 || c[14999] != 2) + return EXIT_FAILURE; + if (d[0] != 3 || d[14999] != 4) + return EXIT_FAILURE; + + set_prg_bank(14); + if (c[0] != 1 || c[14999] != 2) + return EXIT_FAILURE; + if (e[0] != 5 || e[14999] != 6) + return EXIT_FAILURE; + + set_prg_bank(29); + if (c[0] != 1 || c[14999] != 2) + return EXIT_FAILURE; + if (e[0] != 7 || e[14999] != 8) + return EXIT_FAILURE; + + set_prg_bank(0); + if ((unsigned)bank_0_fn >= 0xc000 || bank_0_fn() != 7) + return EXIT_FAILURE; + + set_prg_bank(14); + if ((unsigned)bank_14_fn >= 0xc000 || bank_14_fn() != 8) + return EXIT_FAILURE; + + set_prg_bank(29); + if ((unsigned)bank_29_fn >= 0xc000 || bank_29_fn() != 10) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} From 2610b300d2369c08f46b6d5583d2da0aa73ad5ee Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 28 Apr 2025 18:56:36 -0400 Subject: [PATCH 09/19] hex to lowercase + removed references to prg 15 --- mos-platform/nes-mmc1/prg-rom-banked.ld | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/mos-platform/nes-mmc1/prg-rom-banked.ld b/mos-platform/nes-mmc1/prg-rom-banked.ld index f458e733a..fd2049389 100644 --- a/mos-platform/nes-mmc1/prg-rom-banked.ld +++ b/mos-platform/nes-mmc1/prg-rom-banked.ld @@ -15,7 +15,6 @@ __prg_rom_11_lma = 0x0b8000; __prg_rom_12_lma = 0x0c8000; __prg_rom_13_lma = 0x0d8000; __prg_rom_14_lma = 0x0e8000; -__prg_rom_15_lma = 0x0f8000; __prg_rom_16_lma = 0x108000; __prg_rom_17_lma = 0x118000; __prg_rom_18_lma = 0x128000; @@ -26,11 +25,11 @@ __prg_rom_22_lma = 0x168000; __prg_rom_23_lma = 0x178000; __prg_rom_24_lma = 0x188000; __prg_rom_25_lma = 0x198000; -__prg_rom_26_lma = 0x1A8000; -__prg_rom_27_lma = 0x1B8000; -__prg_rom_28_lma = 0x1C8000; -__prg_rom_29_lma = 0x1D8000; -__prg_rom_30_lma = 0x1E8000; +__prg_rom_26_lma = 0x1a8000; +__prg_rom_27_lma = 0x1b8000; +__prg_rom_28_lma = 0x1c8000; +__prg_rom_29_lma = 0x1d8000; +__prg_rom_30_lma = 0x1e8000; __prg_rom_fixed_lma = 0xc000; __prg_rom_0_offset = 0x00000; @@ -48,7 +47,6 @@ __prg_rom_11_offset = 0x2c000; __prg_rom_12_offset = 0x30000; __prg_rom_13_offset = 0x34000; __prg_rom_14_offset = 0x38000; -__prg_rom_15_offset = 0x3C000; __prg_rom_16_offset = 0x40000; __prg_rom_17_offset = 0x44000; __prg_rom_18_offset = 0x48000; @@ -83,7 +81,6 @@ MEMORY { prg_rom_12 : ORIGIN = __prg_rom_12_lma, LENGTH = __prg_rom_size >= 256 ? 0x4000 - 12 : 0 prg_rom_13 : ORIGIN = __prg_rom_13_lma, LENGTH = __prg_rom_size >= 256 ? 0x4000 - 12 : 0 prg_rom_14 : ORIGIN = __prg_rom_14_lma, LENGTH = __prg_rom_size >= 256 ? 0x4000 - 12 : 0 - prg_rom_15 : ORIGIN = __prg_rom_15_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 prg_rom_16 : ORIGIN = __prg_rom_16_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 prg_rom_17 : ORIGIN = __prg_rom_17_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 prg_rom_18 : ORIGIN = __prg_rom_18_lma, LENGTH = __prg_rom_size >= 512 ? 0x4000 - 12 : 0 @@ -126,7 +123,6 @@ SECTIONS { .prg_rom_12 : { *(.prg_rom_12 .prg_rom_12.*) } >prg_rom_12 .prg_rom_13 : { *(.prg_rom_13 .prg_rom_13.*) } >prg_rom_13 .prg_rom_14 : { *(.prg_rom_14 .prg_rom_14.*) } >prg_rom_14 - .prg_rom_15 : { *(.prg_rom_15 .prg_rom_15.*) } >prg_rom_15 .prg_rom_16 : { *(.prg_rom_16 .prg_rom_16.*) } >prg_rom_16 .prg_rom_17 : { *(.prg_rom_17 .prg_rom_17.*) } >prg_rom_17 .prg_rom_18 : { *(.prg_rom_18 .prg_rom_18.*) } >prg_rom_18 From cc334b9171514900e5722f2a3da73c6e1e62495e Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Tue, 29 Apr 2025 08:29:21 -0400 Subject: [PATCH 10/19] jrowboy's set_prg_bank --- mos-platform/nes-mmc1/mapper.s | 83 +++++++++++++++------------------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 82b1d7b80..e26151ccf 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -99,54 +99,45 @@ get_prg_bank: .weak set_prg_bank __set_prg_bank: set_prg_bank: - tay + bit .Luse_suxrom_bankswitching + bpl .Lcontinue_bank_switch + ; save the new bank byte on the stack for safe keeping + pha + ; check which bits changed + eor _PRG_BANK + ; if the outer bank bit changed then we need to set a new outerbank + and #%00010000 + beq .Lset_inner_bank +.Lset_new_outer_bank: + ; Flip the outer bank bit for _CHR_BANK0 and _CHR_BANK1 + lda _CHR_BANK0 + eor #%00010000 + jsr set_chr_bank_0_retry + lda _CHR_BANK1 + eor #%00010000 + jsr set_chr_bank_1_retry +.Lset_inner_bank: + ; restore the bank byte and put it in y so we can reload it on retry + pla +.Lcontinue_bank_switch: + tay + ; original code below .Lset: - lda # Date: Tue, 29 Apr 2025 08:52:03 -0400 Subject: [PATCH 11/19] review https://github.com/llvm-mos/llvm-mos-sdk/pull/396#discussion_r2064892507 --- mos-platform/nes-mmc1/prg-rom-banked.ld | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mos-platform/nes-mmc1/prg-rom-banked.ld b/mos-platform/nes-mmc1/prg-rom-banked.ld index fd2049389..39a628f50 100644 --- a/mos-platform/nes-mmc1/prg-rom-banked.ld +++ b/mos-platform/nes-mmc1/prg-rom-banked.ld @@ -148,7 +148,7 @@ SECTIONS { .reset_fixed : { KEEP(*(.reset_fixed)) } >reset_fixed } -PROVIDE(__prg_rom_size = 512); +PROVIDE(__prg_rom_size = 256); prg_rom_is_512 = __prg_rom_size >= 512 ? 1 : 0; @@ -169,7 +169,7 @@ OUTPUT_FORMAT { FULL(prg_rom_12) FULL(reset, 0, LENGTH(prg_rom_12) ? LENGTH(reset) : 0) FULL(prg_rom_13) FULL(reset, 0, LENGTH(prg_rom_13) ? LENGTH(reset) : 0) FULL(prg_rom_14) FULL(reset, 0, LENGTH(prg_rom_14) ? LENGTH(reset) : 0) - FULL(prg_rom_fixed) FULL(reset_fixed) + FULL(prg_rom_fixed) FULL(reset_fixed, 0, __prg_rom_size >= 512 ? 0x4000 - 12 : 0) FULL(prg_rom_16) FULL(reset, 0, LENGTH(prg_rom_16) ? LENGTH(reset) : 0) FULL(prg_rom_17) FULL(reset, 0, LENGTH(prg_rom_17) ? LENGTH(reset) : 0) @@ -186,7 +186,7 @@ OUTPUT_FORMAT { FULL(prg_rom_28) FULL(reset, 0, LENGTH(prg_rom_28) ? LENGTH(reset) : 0) FULL(prg_rom_29) FULL(reset, 0, LENGTH(prg_rom_29) ? LENGTH(reset) : 0) FULL(prg_rom_30) FULL(reset, 0, LENGTH(prg_rom_30) ? LENGTH(reset) : 0) - FULL(prg_rom_fixed) FULL(reset_fixed) + FULL(prg_rom_fixed) FULL(reset_fixed, 0, __prg_rom_size >= 512 ? 0x4000 - 12 : 0) FULL(chr_rom) } From 16612940133af46a3c6f4d2be4ea0c0401b3ec33 Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Tue, 29 Apr 2025 13:03:53 -0400 Subject: [PATCH 12/19] fix https://github.com/llvm-mos/llvm-mos-sdk/pull/396#discussion_r2066968657 --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index c36dee92d..9bbf4e736 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ build/** build* *~ *.rej -.qodo From a93c409717a4b11bbcca102c01b101f745a3f56e Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Tue, 29 Apr 2025 13:04:13 -0400 Subject: [PATCH 13/19] fix https://github.com/llvm-mos/llvm-mos-sdk/pull/396#discussion_r2066968542 --- mos-platform/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mos-platform/CMakeLists.txt b/mos-platform/CMakeLists.txt index b9140f79d..5dd0b8803 100644 --- a/mos-platform/CMakeLists.txt +++ b/mos-platform/CMakeLists.txt @@ -85,4 +85,4 @@ add_subdirectory(pce) add_subdirectory(pce-cd) add_subdirectory(rp6502) add_subdirectory(supervision) -# add_subdirectory(vic20) +add_subdirectory(vic20) From 48992f02d550b6ed9dac45e190ebe1d1d60edd01 Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Tue, 29 Apr 2025 13:14:48 -0400 Subject: [PATCH 14/19] latest version of set_prg_bank but still use prg_rom_is_512 which shouldnt work --- mos-platform/nes-mmc1/mapper.s | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index e26151ccf..d3b4b166f 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -99,8 +99,8 @@ get_prg_bank: .weak set_prg_bank __set_prg_bank: set_prg_bank: - bit .Luse_suxrom_bankswitching - bpl .Lcontinue_bank_switch + ldx # Date: Tue, 29 Apr 2025 13:15:45 -0400 Subject: [PATCH 15/19] attempt at fixing the fixed bank output format --- mos-platform/nes-mmc1/prg-rom-banked.ld | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mos-platform/nes-mmc1/prg-rom-banked.ld b/mos-platform/nes-mmc1/prg-rom-banked.ld index 39a628f50..56feee054 100644 --- a/mos-platform/nes-mmc1/prg-rom-banked.ld +++ b/mos-platform/nes-mmc1/prg-rom-banked.ld @@ -150,7 +150,7 @@ SECTIONS { PROVIDE(__prg_rom_size = 256); -prg_rom_is_512 = __prg_rom_size >= 512 ? 1 : 0; +__prg_rom_is_512 = __prg_rom_size >= 512 ? 1 : 0; OUTPUT_FORMAT { INCLUDE ines-header.ld @@ -169,7 +169,7 @@ OUTPUT_FORMAT { FULL(prg_rom_12) FULL(reset, 0, LENGTH(prg_rom_12) ? LENGTH(reset) : 0) FULL(prg_rom_13) FULL(reset, 0, LENGTH(prg_rom_13) ? LENGTH(reset) : 0) FULL(prg_rom_14) FULL(reset, 0, LENGTH(prg_rom_14) ? LENGTH(reset) : 0) - FULL(prg_rom_fixed) FULL(reset_fixed, 0, __prg_rom_size >= 512 ? 0x4000 - 12 : 0) + FULL(prg_rom_fixed) FULL(reset_fixed) FULL(prg_rom_16) FULL(reset, 0, LENGTH(prg_rom_16) ? LENGTH(reset) : 0) FULL(prg_rom_17) FULL(reset, 0, LENGTH(prg_rom_17) ? LENGTH(reset) : 0) @@ -186,7 +186,9 @@ OUTPUT_FORMAT { FULL(prg_rom_28) FULL(reset, 0, LENGTH(prg_rom_28) ? LENGTH(reset) : 0) FULL(prg_rom_29) FULL(reset, 0, LENGTH(prg_rom_29) ? LENGTH(reset) : 0) FULL(prg_rom_30) FULL(reset, 0, LENGTH(prg_rom_30) ? LENGTH(reset) : 0) - FULL(prg_rom_fixed) FULL(reset_fixed, 0, __prg_rom_size >= 512 ? 0x4000 - 12 : 0) + + FULL(prg_rom_fixed, 0, __prg_rom_size >= 512 ? LENGTH(prg_rom_fixed) : 0) + FULL(reset_fixed, 0, __prg_rom_size >= 512 ? LENGTH(reset_fixed) : 0) FULL(chr_rom) } From ed2856da90624862deb6a76e938d02c80bb0a29c Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Tue, 29 Apr 2025 14:31:13 -0400 Subject: [PATCH 16/19] added __ prefix to prg_rom_is_512 --- mos-platform/nes-mmc1/mapper.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index d3b4b166f..615e0936f 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -99,7 +99,7 @@ get_prg_bank: .weak set_prg_bank __set_prg_bank: set_prg_bank: - ldx # Date: Tue, 29 Apr 2025 17:41:05 -0400 Subject: [PATCH 17/19] try to preserve the PRG RAM bit --- mos-platform/nes-mmc1/mapper.s | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index 615e0936f..fe0f64f19 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -120,6 +120,14 @@ set_prg_bank: ; restore the bank byte and put it in y so we can reload it on retry pla .Lcontinue_bank_switch: + ; bit 4 is PRG RAM en/disable, so use those from the current bank value + ldx __rc2 ; preserve the current __rc2 value in X so we can use it as a temp + and #%00001111 + sta __rc2 ; store the new bank bits + lda _PRG_BANK ; load the current prg_ram enable bit + and #%00010000 + ora __rc2 ; combine them together + stx __rc2 ; restore the tmp register we used tay ; original code below .Lset: From 500564c0bd6f4c7f9e77a0863bcb109795f8de8d Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Wed, 30 Apr 2025 15:52:30 -0400 Subject: [PATCH 18/19] Added prg-rom-512 test to list --- test/nes-mmc1/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/nes-mmc1/CMakeLists.txt b/test/nes-mmc1/CMakeLists.txt index c9deb2153..49a2cd7fc 100644 --- a/test/nes-mmc1/CMakeLists.txt +++ b/test/nes-mmc1/CMakeLists.txt @@ -19,6 +19,7 @@ add_nes_test(prg-rom-32) add_nes_test(prg-rom-64) add_nes_test(prg-rom-128) add_nes_test(prg-rom-256) +add_nes_test(prg-rom-512) add_nes_test(prg-ram) From b62a60f501682da621b755ce0df819b03bbdc05b Mon Sep 17 00:00:00 2001 From: Sebastien Vezina Date: Mon, 5 May 2025 14:53:40 -0400 Subject: [PATCH 19/19] banked_call fiddling because set_prg_bank clobbers rc2 and/or rc3 --- mos-platform/nes-mmc1/mapper.s | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/mos-platform/nes-mmc1/mapper.s b/mos-platform/nes-mmc1/mapper.s index fe0f64f19..e38aa4234 100644 --- a/mos-platform/nes-mmc1/mapper.s +++ b/mos-platform/nes-mmc1/mapper.s @@ -148,16 +148,25 @@ set_prg_bank: .section .text.banked_call,"ax",@progbits .weak banked_call banked_call: - tay - lda _PRG_BANK - pha - tya - jsr __set_prg_bank - lda __rc2 - sta __rc18 - lda __rc3 - sta __rc19 + tay ; save current bank in y + lda _PRG_BANK ; load new bank in A + pha ; push new bank to stack + + lda __rc2 ; push function pointer to stack + pha + lda __rc3 + pha + + tya ; restore current bank from A + + jsr __set_prg_bank ; set the new bank + + pla ; restore function pointer from stack + sta __rc19 + pla + sta __rc18 + jsr __call_indir pla jsr __set_prg_bank - rts + rts \ No newline at end of file